// Copyright (c) The University of Cincinnati. All rights reserved. // UC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF // THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED // TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A // PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UC SHALL NOT BE LIABLE // FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, // RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS // DERIVATIVES. // By using or copying this Software, Licensee agrees to abide by the // intellectual property laws, and all other applicable laws of the // U.S., and the terms of this license. // Author : Antarpreet Singh // #include #include #include #include using namespace std; typedef vector double_vector; typedef vector bool_vector; void CPA_HD(vector&, vector&, vector&, int, vector&); void TEST1(vector *, vector *, vector *); bool DPA_HD_CPA(int no_of_output_bits, int max_key_comb, int vectors, vector &ptext, vector ¤t_data, vector &diffwf, int *final_key, vector &kg, double *max, int clock_posedge_time_min, int clock_posedge_time_max, int correct_key, ofstream &fplog, string testname, string algorithm) { vector ctext; vector ctext_temp; vector diffwf_temp; int i,j,k; double absmax; bool done; int keyvalue; int ctext_size; vector prev_ctext(no_of_output_bits,0); for(j=0;j ctext_size) { cout<<" All the vectors are exhausted. Unable to find key"<<"\n"; fplog<<" All the vectors are exhausted. Unable to find key"<<"\n"; exit(1); } //Perform DPA CPA_HD(current_data,ctext,diffwf_temp,vectors,prev_ctext); diffwf.push_back(diffwf_temp); diffwf_temp.clear(); ctext.clear(); } for(j=0;j max[j]) { *(max+j) = diffwf[j][i]; } } } absmax = *max; keyvalue = 0; for(j=1;j absmax) { absmax = *(max+j); keyvalue = j; } } cout<<"Key Guess is : "< ctext_size) { cout<<"Found Incorrect key\n"; fplog<<"Found Incorrect key\n"; exit(2); } else { cout<<"Correlation For Correct key : "< ¤t_data, vector &ctext, vector &corr, int vectors, vector &prev_ctext) { double hwt,prod,sum_hw,sum_curr,sq_hw,sq_curr; int i,j; double cofficient; //Power Model for DPA int no_of_samples_x = current_data[0].size(); for (j=0; j < no_of_samples_x ; j++) { hwt=0;prod=0;sum_hw=0;sum_curr=0;sq_hw=0;sq_curr=0; for(i=0; i < vectors; i++) { hwt = HD(ctext[i],prev_ctext); prod += (hwt)*(current_data[i][j]); sum_hw += hwt; sum_curr += current_data[i][j]; sq_hw += (hwt*hwt); sq_curr += (current_data[i][j])*(current_data[i][j]); } cofficient = ((vectors*prod) - (sum_hw*sum_curr))/(sqrt((vectors*sq_curr)-(sum_curr*sum_curr))*sqrt((vectors*sq_hw)-(sum_hw*sum_hw))); corr.push_back(cofficient); } } /******************************************************************/