/*---------------------------------------------------------------------------*/ void ecc_gen_private_key(NN_DIGIT *PrivateKey) { NN_UINT order_digit_len; NN_UINT order_bit_len; char done = FALSE; uint8_t ri; NN_DIGIT digit_mask; order_bit_len = NN_Bits(param.r, NUMWORDS); order_digit_len = NN_Digits(param.r, NUMWORDS); while (!done) { prng((uint8_t *)PrivateKey, order_digit_len * sizeof(NN_Digits)); for (ri = order_digit_len; ri < NUMWORDS; ri++) { PrivateKey[ri] = 0; } if (order_bit_len % NN_DIGIT_BITS != 0) { digit_mask = MAX_NN_DIGIT >> (NN_DIGIT_BITS - order_bit_len % NN_DIGIT_BITS); PrivateKey[order_digit_len - 1] = PrivateKey[order_digit_len - 1] & digit_mask; } NN_ModSmall(PrivateKey, param.r, NUMWORDS); if (NN_Zero(PrivateKey, NUMWORDS) != 1) { done = TRUE; } }
/* * P0 = n * P1 */ void ecc_mul ( point_t* P0, point_t* P1, NN_DIGIT* n ) { int16_t i, tmp; NN_DIGIT Z0[NUMWORDS]; NN_DIGIT Z1[NUMWORDS]; /* clear point */ p_clear ( P0 ); /* convert to Jprojective coordinate */ NN_AssignZero ( Z0, NUMWORDS ); NN_AssignZero ( Z1, NUMWORDS ); Z1[0] = 0x01; tmp = NN_Bits ( n, NUMWORDS ); for ( i = tmp-1; i >= 0; i-- ) { ecc_dbl_proj ( P0, Z0, P0, Z0 ); if ( b_testbit ( n, i ) ) { #ifdef ADD_MIX c_add_mix ( P0, Z0, P0, Z0, P1 ); #else ecc_add_proj ( P0, Z0, P0, Z0, P1, Z1 ); #endif } } /* convert back to affine coordinate */ if ( !Z_is_one ( Z0 ) ) { NN_ModInv ( Z1, Z0, param.p, NUMWORDS ); NN_ModMultOpt ( Z0, Z1, Z1, param.p, param.omega, NUMWORDS ); NN_ModMultOpt ( P0->x, P0->x, Z0, param.p, param.omega, NUMWORDS ); NN_ModMultOpt ( Z0, Z0, Z1, param.p, param.omega, NUMWORDS ); NN_ModMultOpt ( P0->y, P0->y, Z0, param.p, param.omega, NUMWORDS ); } }
/** * \param PrivateKey Must IKE_DH_SCALAR_CONTIKIECC_LEN bytes long */ void ecc_gen_private_key ( NN_DIGIT* PrivateKey ) { srand(NULL); NN_UINT order_digit_len, order_bit_len; bool done = FALSE; uint8_t ri; NN_DIGIT digit_mask; order_bit_len = NN_Bits ( param.r, NUMWORDS ); order_digit_len = NN_Digits ( param.r, NUMWORDS ); while ( !done ) { //PrivateKey[] = { 323FA316 9D8E9C65 93F59476 BC142000 AB5BE0E2 49C43426 }; /* PrivateKey[24] = 0x0; PrivateKey[23] = 0x32; PrivateKey[22] = 0x3F; PrivateKey[21] = 0xA3; PrivateKey[20] = 0x16; PrivateKey[19] = 0x9D; PrivateKey[18] = 0x8E; PrivateKey[17] = 0x9C; PrivateKey[16] = 0x65; PrivateKey[15] = 0x93; PrivateKey[14] = 0xF5; PrivateKey[13] = 0x94; PrivateKey[12] = 0x76; PrivateKey[11] = 0xBC; PrivateKey[10] = 0x14; PrivateKey[9] = 0x20; PrivateKey[8] = 0x00; PrivateKey[7] = 0xAB; PrivateKey[6] = 0x5B; PrivateKey[5] = 0xE0; PrivateKey[4] = 0xE2; PrivateKey[3] = 0x49; PrivateKey[2] = 0xC4; PrivateKey[1] = 0x34; PrivateKey[0] = 0x26; */ for ( ri = 0; ri < order_digit_len; ri++ ) { #ifdef THIRTYTWO_BIT_PROCESSOR PrivateKey[ri] = rand32(); #else PrivateKey[ri] = rand16(); #endif } for ( ri = order_digit_len; ri < NUMWORDS; ri++ ) { PrivateKey[ri] = 0; } if ( order_bit_len % NN_DIGIT_BITS != 0 ) { digit_mask = MAX_NN_DIGIT >> ( NN_DIGIT_BITS - order_bit_len % NN_DIGIT_BITS ); PrivateKey[order_digit_len - 1] = PrivateKey[order_digit_len - 1] & digit_mask; } NN_ModSmall ( PrivateKey, param.r, NUMWORDS ); if ( NN_Zero ( PrivateKey, NUMWORDS ) != 1 ) done = TRUE; }