Commit 2bee7dda authored by YUSHIQIAN's avatar YUSHIQIAN

final version of hamming weight, hamming distance with 1bit, 4bits

parent d4275e07
......@@ -98,7 +98,6 @@ int
main (int argc, char **argv)
{
int n; /* Number of experiments to use. */
int target_bit; /* Index of target bit. */
char *key_bits[8] = {"1-6", "7-12", "13-18", "19-24", "25-30", "31-36", "37-42", "43-48"};
int sbox_idx[8] = {0,0,0,0,0,0,0,0};
......@@ -120,9 +119,9 @@ main (int argc, char **argv)
/*************************************/
/* If invalid number of arguments (including program name), exit with error
* message. */
if (argc != 4)
if (argc != 3)
{
ERROR (-1, "usage: dpa <file> <n> <b>\n <file>: name of the traces file in HWSec format\n (e.g. /datas/teaching/courses/HWSec/labs/data/HWSecTraces10000x00800.hws)\n <n>: number of experiments to use\n <b>: index of target bit in L15 (1 to 32, as in DES standard)\n");
ERROR (-1, "usage: dpa <file> <n> <b>\n <file>: name of the traces file in HWSec format\n (e.g. /datas/teaching/courses/HWSec/labs/data/HWSecTraces10000x00800.hws)\n <n>: number of experiments to use\n");
}
/* Number of experiments to use is argument #2, convert it to integer and
* store the result in variable n. */
......@@ -132,14 +131,6 @@ main (int argc, char **argv)
ERROR (-1, "invalid number of experiments: %d (shall be greater than 1)", n);
}
/* Target bit is argument #3, convert it to integer and store the result in
* variable target_bit. */
target_bit = atoi (argv[3]);
if (target_bit == 0)
{
printf("target all 32 bits \n");
}
/* Read power traces and ciphertexts. Name of data file is argument #1. n is
* the number of experiments to use. */
read_datafile (argv[1], n);
......@@ -151,8 +142,6 @@ main (int argc, char **argv)
*****************************************************************************/
average ("average");
printf ("...begin attack.....\n");
/***************************************************************
* Attack target bit in L15=R14 with P. Kocher's DPA technique *
***************************************************************/
......@@ -161,14 +150,13 @@ main (int argc, char **argv)
/*****************
* Print summary *
*****************/
printf ("DES chart.................\n");
int w = 20;
printf ("%*s%*s%*s%*s\n", w, "key bits", w, "best guess", w, "max amplitue", w, "max index");
int w = 18;
printf ("%*s%*s%*s%*s\n", w, "key bits", w, "best guess", w, "max pcc", w, "max index");
for (i = 0; i < nBytes; i++) /* for every sbox */
{
for (j = 0; j < nBest/2; j++)
{
printf ("%*s (0x%02x),%d (0x%02x)%d%15.2f,%5.2f%16d,%d\n", w, key_bits[i],
printf ("%*s (0x%02x),%2d (0x%02x)%2d %10.2f,%5.2f%10d,%3d\n", w, key_bits[i],
best_guess[i][2*j], best_guess[i][2*j],
best_guess[i][2*j+1], best_guess[i][2*j+1],
best_max[i][2*j], best_max[i][2*j+1],
......@@ -279,7 +267,6 @@ decision (uint64_t ct, int s, int hd[64]) /* compute the hamming distance betwee
uint64_t r15; /* L15 (as in DES standard) */
uint64_t invp_l15; /* inverse of P permutation of l15, L15 = R14 */
uint64_t l15;
r16l16 = des_ip (ct); /* Compute R16|L16 */
l16 = des_right_half (r16l16); /* Extract right half */
......@@ -292,18 +279,10 @@ decision (uint64_t ct, int s, int hd[64]) /* compute the hamming distance betwee
for (g = 0, rk = UINT64_C (0); g < 64; g++, rk += UINT64_C (0x041041041041))
{
invp_l15 = des_n_p (r16) ^ (des_sboxes (er15 ^ rk)); /* Compute L15 deducing scatter */
l15 = r16 ^ des_p (des_sboxes (er15 ^ rk));
hd[g] = hamming_distance (
(invp_l15 >> (4*(nBytes-s-1))) & UINT64_C(15),
(r15 >> (4*(nBytes-s-1))) & UINT64_C(15)
(des_n_p (l16) >> (4*(nBytes-s-1))) & UINT64_C(15)
); /* calculate hamming distance of r15 and r14 */
/*printf("r15: %llx, invp_l15: %llx, %llx\n", r15, invp_l15, des_n_p(l15));
printf("hamming distance between %llx and %llx is %d\n",
(invp_l15 >> (4*(nBytes-s-1))) & UINT64_C(15),
(r15 >> (4*(nBytes-s-1))) & UINT64_C(15),
hd[g]);*/
} /* End for guesses */
return;
}
......@@ -373,23 +352,16 @@ dpa_attack (void)
/*printf("get pcc======\n");*/
pcc[g] = tr_pcc_get_pcc(pcc_ctx, g);
/*printf("PCC(X[.], Y%d)[.] =", g);*/
for(i = 0; i < l; i++)
/*for (i = 0; i < l; i++)
{
/*printf(" %lf", pcc[i]);
printf("\n");*/
if (i < 300 || i > 600)
if (i < 550 || i > 625)
{
pcc[g][i] = 0.0;
}
}
}*/
/* pick two maximum dpa */
max = tr_max (ctx, pcc[g], &idx); /* Get max and argmax of DPA trace */
if (s == 0 && g == 60){printf("======%f, %d", max, idx);}
if (max > best_max[s][0] || g == 0) /* If better than current best max (or if first guess) */
{
......
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment