// -3d1d 6db7 8251 f371 * -7a18 3791 d18b b7c5 = 1d25ce4fdf93390f8d6c709f4d711cf5 // -20538248dece6d29068d * 400b1411b874f81394c6 = -81646b193d95136a6fedb73cee6d30c39fb950e // -BC8B 7D53 4921 853D * 0DDA 6044 00CE DDE6 = -a33eb0c5847db8837589c22db395dce void test_mul_simple(void){ bigint_t a, b, c; // uint8_t a_b[10] = {0x8d, 0x06, 0x29, 0x6d, 0xce, 0xde, 0x48, 0x82, 0x53, 0x20}; // uint8_t b_b[10] = {0xc6, 0x94, 0x13, 0xf8, 0x74, 0xb8, 0x11, 0x14, 0x0b, 0x40}; uint8_t a_b[8] = {0x3d, 0x85, 0x21, 0x49, 0x53, 0x7d, 0x8b, 0xbc}; uint8_t b_b[8] = {0xe6, 0xdd, 0xce, 0x00, 0x44, 0x60, 0xda, 0x0d}; uint8_t c_b[16]; a.wordv=a_b; b.wordv=b_b; c.wordv=c_b; a.length_B = 8; b.length_B = 8; a.info=0x80; bigint_adjust(&a); bigint_adjust(&b); bigint_mul_s(&c, &a, &b); cli_putstr_P(PSTR("\r\n test: ")); bigint_print_hex(&a); cli_putstr_P(PSTR(" * ")); bigint_print_hex(&b); cli_putstr_P(PSTR(" = ")); bigint_print_hex(&c); }
void test_gcdext_simple(void){ bigint_t a, b, c, d, e; uint8_t a_b[5] = {0x71, 0x07, 0x00, 0x09, 0x16}; uint8_t b_b[5] = {0x72, 0x7D, 0x57, 0xAC, 0X6F}; uint8_t c_b[6], d_b[6], e_b[6]; a.wordv=a_b; a.length_B = 5; a.info=0x00; bigint_adjust(&a); b.wordv=b_b; b.length_B = 5; b.info=0x00; bigint_adjust(&b); c.wordv = c_b; d.wordv = d_b; e.wordv = e_b; bigint_gcdext(&c, &d, &e, &a, &b); cli_putstr_P(PSTR("\r\n test: gcd( ")); bigint_print_hex(&a); cli_putstr_P(PSTR(", ")); bigint_print_hex(&b); cli_putstr_P(PSTR(") => a = ")); bigint_print_hex(&d); cli_putstr_P(PSTR("; b = ")); bigint_print_hex(&e); cli_putstr_P(PSTR("; gcd = ")); bigint_print_hex(&c); }
void load_rsa_key_blob(rsa_ctx_t* ctx){ if(ctx->modulus.wordv){ free(ctx->modulus.wordv); } ctx->modulus.wordv = malloc(ALL_LEN_B); if(ctx->modulus.wordv==NULL){ cli_putstr_P(PSTR("\r\nERROR: OUT OF MEMORY!!!")); return; } ctx->modulus.info = ctx->privexp.info = ctx->pubexp.info = 0; memcpy_P(ctx->modulus.wordv, rsa_key_blob, ALL_LEN_B); ctx->modulus.length_B=N_LEN_B; ctx->pubexp.wordv = ctx->modulus.wordv+N_LEN_B; ctx->pubexp.length_B = E_LEN_B; ctx->privexp.wordv = ctx->pubexp.wordv+E_LEN_B; ctx->privexp.length_B = D_LEN_B; bigint_changeendianess(&(ctx->modulus)); bigint_changeendianess(&(ctx->pubexp)); bigint_changeendianess(&(ctx->privexp)); bigint_adjust(&(ctx->modulus)); bigint_adjust(&(ctx->pubexp)); bigint_adjust(&(ctx->privexp)); }
void testrun_performance_multiply_bigint(void){ printf_P(PSTR("\n=== performance measurement (invert) ===\n")); unsigned i,j; uint64_t time_a = 0, time_b = 0; uint32_t tmp; bigint_t a, b, v; bigint_word_t v_w[192 * 2 / BIGINT_WORD_SIZE]; bigint_word_t a_w[192 / BIGINT_WORD_SIZE]; bigint_word_t b_w[192 / BIGINT_WORD_SIZE]; a.wordv = a_w; b.wordv = b_w; v.wordv = v_w; for(j = 0; j < 32; ++j){ for(i = 0; i < 192 / BIGINT_WORD_SIZE; ++i){ ((uint8_t*)a_w)[i] = random(); } a.length_W = 192 / BIGINT_WORD_SIZE; a.info = 0; bigint_adjust(&a); for(i = 0; i < 192 / BIGINT_WORD_SIZE; ++i){ ((uint8_t*)b_w)[i] = random(); } b.length_W = 192 / BIGINT_WORD_SIZE; b.info = 0; bigint_adjust(&b); for(i = 0; i < 16; ++i){ startTimer(1); START_TIMER; bigint_mul_u(&v,&a, &b); STOP_TIMER; tmp = stopTimer(); time_a += tmp; time_b += tmp; START_TIMER; bigint_reduce_p192(&v); STOP_TIMER; tmp = stopTimer(); time_b += tmp; } } time_a >>= 8; ++time_a; time_a >>= 1; time_b >>= 8; ++time_b; time_b >>= 1; printf_P(PSTR(" multiply costs %7"PRIu32" cycles\n"), (uint32_t)time_a); printf_P(PSTR(" multiply + reduce costs %7"PRIu32" cycles\n"), (uint32_t)time_b); }
void testrun_performance_invert_bigint(void){ printf_P(PSTR("\n=== performance measurement (invert) ===\n")); unsigned i,j; uint64_t time = 0; bigint_t a, v; bigint_word_t v_w[192 / BIGINT_WORD_SIZE]; bigint_word_t a_w[192 / BIGINT_WORD_SIZE]; a.wordv = a_w; v.wordv = v_w; for(j = 0; j < 32; ++j){ for(i = 0; i < 192 / BIGINT_WORD_SIZE; ++i){ ((uint8_t*)v_w)[i] = random(); } v.length_W = 192 / BIGINT_WORD_SIZE; v.info = 0; bigint_adjust(&v); for(i = 0; i < 16; ++i){ startTimer(1); START_TIMER; bigint_inverse(&a, &v, &nist_curve_p192_p); STOP_TIMER; time += stopTimer(); } } time >>= 8; ++time; time >>= 1; printf_P(PSTR(" invert costs %"PRIu32" cycles\n"), (uint32_t)time); }
uint8_t read_bigint(bigint_t *a, char *prompt){ uint16_t read_length, actual_length; uint8_t off; uint8_t *buffer; char read_int_str[18]; cli_putstr(prompt); cli_putstr_P(PSTR("\r\n length: ")); cli_getsn(read_int_str, 16); read_length = own_atou(read_int_str); off = (sizeof(bigint_word_t) - (read_length % sizeof(bigint_word_t))) % sizeof(bigint_word_t); buffer = malloc(((read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t)) * sizeof(bigint_word_t)); if(!buffer){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return 2; } cli_putstr_P(PSTR("\r\n data: ")); memset(buffer, 0, sizeof(bigint_word_t)); actual_length = read_os(buffer + off, read_length, NULL); if(actual_length != read_length){ cli_putstr_P(PSTR("\r\nERROR: unexpected end of data!")); free(buffer); return 1; } a->wordv = (bigint_word_t*)buffer; a->length_W = (read_length + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); a->info = 0; bigint_changeendianess(a); bigint_adjust(a); return 0; }
uint8_t dsa_verify_message(const dsa_signature_t* s, const void* m, uint16_t m_len_b, const hfdesc_t* hash_desc, const dsa_ctx_t* ctx) { bigint_t z; uint8_t n_B = ctx->domain.q.length_B; uint8_t hash_value[(hfal_hash_getHashsize(hash_desc)+7)/8]; hfal_hash_mem(hash_desc, hash_value, m, m_len_b); z.wordv=hash_value; z.length_B=n_B; bigint_changeendianess(&z); bigint_adjust(&z); return dsa_verify_bigint(s, &z, ctx); }
uint8_t load_bigint_from_os(bigint_t *a, PGM_VOID_P os, uint16_t length_B){ a->length_W = BIGINT_CEIL(length_B) / sizeof(bigint_word_t); a->wordv = malloc(BIGINT_CEIL(length_B)); if(!a->wordv){ cli_putstr_P(PSTR("\r\nOOM!\r\n")); return 1; } memset(a->wordv, 0, sizeof(bigint_word_t)); memcpy_P((uint8_t*)a->wordv + BIGINT_OFF(length_B), os, length_B); a->info = 0; bigint_changeendianess(a); bigint_adjust(a); return 0; }
// [fail (c)]: A862 % 2752 = 0D1A ; should a862 % 2752 = b1a void test_reduce_simple(void){ bigint_t a, b, c; uint8_t a_b[2] = {0x62, 0xA8}; uint8_t b_b[2] = {0x52, 0x27}; uint8_t c_b[2]; a.wordv=a_b; a.length_B = 2; a.info=0x00; bigint_adjust(&a); b.wordv=b_b; b.length_B = 2; b.info=0x00; bigint_adjust(&b); c.wordv = c_b; bigint_copy(&c, &a); bigint_reduce(&c, &b); cli_putstr_P(PSTR("\r\n test: ")); bigint_print_hex(&a); cli_putstr_P(PSTR(" % ")); bigint_print_hex(&b); cli_putstr_P(PSTR(" = ")); bigint_print_hex(&c); }
void test_square_simple(void){ bigint_t a, c; uint8_t a_b[11] = {0xe6, 0x70, 0x7d, 0x43, 0x74, 0x07, 0x20, 0x22, 0x6a, 0xb8, 0xf4}; uint8_t c_b[22]; a.wordv=a_b; c.wordv=c_b; a.length_B = 11; a.info=0x00; bigint_adjust(&a); bigint_square(&c, &a); cli_putstr_P(PSTR("\r\n test: ")); bigint_print_hex(&a); cli_putstr_P(PSTR("**2 = ")); bigint_print_hex(&c); }
uint8_t bigint_read_hex_echo(bigint_t *a) { uint16_t allocated = 0; uint8_t shift4 = 0; uint16_t t, idx = 0; a->length_W = 0; a->wordv = NULL; a->info = 0; for (;;) { if (allocated - idx < 1) { bigint_word_t *p; p = realloc(a->wordv, allocated += BLOCKSIZE); if (p == NULL) { cli_putstr("\r\nERROR: Out of memory!"); free(a->wordv); return 0xff; } memset((uint8_t*)p + allocated - BLOCKSIZE, 0, BLOCKSIZE); a->wordv = p; } t = read_byte(); if (idx == 0) { if (t & 0x0400) { /* got minus */ a->info |= BIGINT_NEG_MASK; continue; } else { if (t == 0x0100) { free(a->wordv); a->wordv = NULL; return 1; } } } if (t <= 0x00ff) { ((uint8_t*)(a->wordv))[idx++] = (uint8_t)t; } else { if (t & 0x0200) { shift4 = 1; ((uint8_t*)(a->wordv))[idx++] = (uint8_t)((t & 0x0f) << 4); } break; } } /* we have to reverse the byte array */ uint8_t tmp; uint8_t *p, *q; a->length_W = (idx + sizeof(bigint_word_t) - 1) / sizeof(bigint_word_t); p = (uint8_t*)(a->wordv); q = (uint8_t*)a->wordv + a->length_W * sizeof(bigint_word_t) - 1; while (q > p) { tmp = *p; *p = *q; *q = tmp; p++; q--; } bigint_adjust(a); if (shift4) { bigint_shiftright(a, 4); } if(a->length_W == 1 && a->wordv[0] == 0){ a->length_W = 0; a->info = 0; } return 0; }
void test_sign2(void){ bigint_word_t d_w[sizeof(ecdsa_test_2_d)]; uint8_t rnd[sizeof(ecdsa_test_2_k)]; uint8_t *hash; bigint_t d; const hfdesc_t *hash_desc; ecc_combi_point_t q; ecdsa_signature_t sign; ecdsa_ctx_t ctx; uint8_t r; putchar('\n'); d.wordv = d_w; memcpy_P(rnd, ecdsa_test_2_k, sizeof(ecdsa_test_2_k)); memcpy_P(d_w, ecdsa_test_2_d, sizeof(ecdsa_test_2_d) * sizeof(bigint_word_t)); d.length_W = sizeof(ecdsa_test_2_d) / sizeof(bigint_word_t); d.info = 0; bigint_adjust(&d); hash_desc = &sha224_desc; //hash_select(); hash = malloc(hfal_hash_getHashsize(hash_desc) / 8); if(hash == NULL){ printf_P(PSTR("DBG: XXX <%S %s %d>\n"), PSTR(__FILE__), __func__, __LINE__); } hash_mem_P(hash_desc, hash, ecdsa_test_2_msg, sizeof(ecdsa_test_1_msg) * 8); printf_P(PSTR("msg hash: ")); cli_hexdump(hash, hfal_hash_getHashsize(hash_desc) / 8); putchar('\n'); ecc_chudnovsky_point_alloc(&q.chudnovsky, nist_curve_p192_p.length_W * sizeof(bigint_word_t)); ctx.basepoint = &nist_curve_p192_basepoint.chudnovsky; ctx.priv = &d; ctx.curve = &nist_curve_p192; printf("\n d: "); bigint_print_hex(&d); printf_P(PSTR("\n Gx: ")); bigint_print_hex(&nist_curve_p192_basepoint.affine.x); printf_P(PSTR("\n Gy: ")); bigint_print_hex(&nist_curve_p192_basepoint.affine.y); r = ecc_chudnovsky_multiplication(&q.chudnovsky, &d, &nist_curve_p192_basepoint.chudnovsky, &nist_curve_p192); if(r){ printf_P(PSTR("ERROR: ecc_chudnovsky_multiplication() returned: %"PRIu8"\n"), r); } r = ecc_chudnovsky_to_affine_point(&q.affine, &q.chudnovsky, &nist_curve_p192); if(r){ printf_P(PSTR("ERROR: ecc_chudnovsky_to_affine_point() returned: %"PRIu8"\n"), r); } printf_P(PSTR("\n Qx: ")); bigint_print_hex(&q.affine.x); printf_P(PSTR("\n Qy: ")); bigint_print_hex(&q.affine.y); putchar('\n'); ctx.pub = &q.affine; ecdsa_signature_alloc(&sign, sizeof(ecdsa_test_2_d) * sizeof(bigint_word_t)); r = ecdsa_sign_hash(&sign, hash, hfal_hash_getHashsize(hash_desc) / 8, &ctx, rnd); if(r){ printf_P(PSTR("ERROR: ecdsa_sign_message() returned: %"PRIu8"\n"), r); } printf_P(PSTR(" r: ")); bigint_print_hex(&sign.r); printf_P(PSTR("\n s: ")); bigint_print_hex(&sign.s); free(hash); ecdsa_signature_free(&sign); ecc_chudnovsky_point_free(&q.chudnovsky); }
void testrun_performance_reduce_bigint(void){ printf_P(PSTR("\n=== performance measurement (reduce) ===\n")); unsigned i, j; bigint_t a,b,v; bigint_word_t v_w[192 * 2 / BIGINT_WORD_SIZE]; bigint_word_t a_w[192 * 2 / BIGINT_WORD_SIZE]; bigint_word_t b_w[192 * 2 / BIGINT_WORD_SIZE]; uint32_t time_a, time_b; int32_t time_diff; int16_t faster_percent; v.wordv = v_w; for(j = 0; j < 32; ++j){ do{ for(i = 0; i < 192 * 2 / BIGINT_WORD_SIZE; ++i){ ((uint8_t*)v_w)[i] = random(); } v.length_W = 192 * 2 / BIGINT_WORD_SIZE; v.info = 0; bigint_adjust(&v); }while(0); // printf_P(PSTR("candidate:\n")); // bigint_print_hex(&v); a.wordv = a_w; b.wordv = b_w; calibrateTimer(); // printf_P(PSTR("\n going to test optimized version: ...\n")); uart0_flush(); time_a = 0; for(i = 0; i < 16; ++i){ bigint_copy(&a, &v); startTimer(1); START_TIMER; bigint_reduce_p192(&a); STOP_TIMER; time_a += stopTimer(); } // printf_P(PSTR(" took: %"PRIu32" cycles\nresult:"), time); // bigint_print_hex(&a); // printf_P(PSTR("\n going to test not-optimized version: ...\n")); // uart0_flush(); time_b = 0; for(i = 0; i < 16; ++i){ bigint_copy(&b, &v); startTimer(1); START_TIMER; bigint_reduce(&b, &nist_curve_p192_p); STOP_TIMER; time_b += stopTimer(); } // printf_P(PSTR(" took: %"PRIu32" cycles\nresult:"), time); // bigint_print_hex(&b); time_diff = time_b - time_a; faster_percent = (time_diff * 100) / time_b; printf_P(PSTR(" delta: %7"PRId32" (%3"PRId16"%%) :-"), time_diff, faster_percent); if(bigint_cmp_u(&a, &b)){ printf_P(PSTR("(\n")); } else { printf_P(PSTR(")\n")); } uart0_flush(); } }
uint8_t rsa_encrypt_oaep(void* dest, uint16_t* out_length, const void* src, uint16_t length_B, rsa_publickey_t* key, const rsa_oaep_parameter_t *p, const rsa_label_t* label, const void* seed){ if(!p){ p = &rsa_oaep_default_parameter; } if(!label){ label = &rsa_oaep_default_label; } uint16_t hv_len = (hfal_hash_getHashsize(p->hf)+7)/8; if(length_B > bigint_length_B(&key->modulus) - 2*hv_len - 2){ /* message too long */ return 1; } uint16_t buffer_len = bigint_length_B(&key->modulus); #if DEBUG cli_putstr("\r\n buffer_len = "); cli_hexdump_rev(&buffer_len, 2); cli_putstr("\r\n modulus_len = "); cli_hexdump_rev(&key->modulus.length_B, 2); #endif uint8_t* buffer = (uint8_t*)dest; uint8_t off; /* the following needs some explanation: * off is the offset which is used for compensating the effect of * changeendian() when it operates on multi-byte words. * */ off = (sizeof(bigint_word_t) - (bigint_get_first_set_bit(&key->modulus)/8+1) % sizeof(bigint_word_t)) % (sizeof(bigint_word_t)); buffer += off; buffer_len -= off; uint8_t* seed_buffer = buffer + 1; uint16_t db_len = buffer_len - hv_len - 1; uint8_t* db = seed_buffer + hv_len; uint16_t maskbuffer_len = db_len>hv_len?db_len:hv_len; uint8_t maskbuffer[maskbuffer_len]; bigint_t x; memset(dest, 0, seed_buffer - buffer + off); memset(db + hv_len, 0, db_len - hv_len - length_B -1); hfal_hash_mem(p->hf, db, label->label, label->length_b); db[db_len - length_B - 1] = 0x01; memcpy(db+db_len - length_B, src, length_B); if(seed){ memcpy(seed_buffer, seed, hv_len); }else{ /* generate random seed */ if(!prng_get_byte){ return 2; /* ERROR: no random generator specified */ } uint16_t i; for(i=0; i<hv_len; ++i){ seed_buffer[i] = prng_get_byte(); } } #if DEBUG cli_putstr("\r\n msg (raw, pre-feistel):\r\n"); cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16); #endif p->mgf(maskbuffer, seed_buffer, hv_len, db_len, p->mgf_parameter); memxor(db, maskbuffer, db_len); p->mgf(maskbuffer, db, db_len, hv_len, p->mgf_parameter); memxor(seed_buffer, maskbuffer, hv_len); #if DEBUG cli_putstr("\r\n msg (raw, post-feistel):\r\n"); cli_hexdump_block(dest, bigint_length_B(&key->modulus), 4, 16); #endif x.info = 0; x.length_B = key->modulus.length_B; x.wordv = dest; bigint_adjust(&x); rsa_os2ip(&x, NULL, bigint_length_B(&key->modulus)); #if DEBUG cli_putstr("\r\ninput-msg (pre enc):\r\n"); cli_hexdump_rev(&src, 2); cli_hexdump_block(src, length_B, 4, 16); #endif rsa_enc(&x, key); #if DEBUG cli_putstr("\r\ninput-msg (post enc):\r\n"); cli_hexdump_rev(&src, 2); cli_hexdump_block(src, length_B, 4, 16); #endif rsa_i2osp(NULL, &x, out_length); return 0; }
uint8_t rsa_decrypt_oaep(void* dest, uint16_t* out_length, const void* src, uint16_t length_B, rsa_privatekey_t* key, const rsa_oaep_parameter_t *p, const rsa_label_t* label, void* seed){ // cli_putstr("\r\n -->rsa_decrypt_oaep()"); uart_flush(0); if(!label){ label = &rsa_oaep_default_label; } if(!p){ p = &rsa_oaep_default_parameter; } uint16_t x_len, data_len; bigint_t x; uint16_t hv_len = hfal_hash_getHashsize(p->hf)/8; uint8_t label_hv[hv_len]; uint16_t msg_len = bigint_get_first_set_bit(&key->modulus) / 8 + 1; uint16_t db_len = msg_len - hv_len - 1; uint8_t maskbuffer[db_len>hv_len?db_len:hv_len]; uint8_t *seed_buffer = dest; uint8_t *db_buffer = seed_buffer + hv_len; x_len = bigint_get_first_set_bit(&key->modulus)/8; memset(dest, 0, bigint_length_B(&key->modulus) - length_B); memcpy((uint8_t*)dest + bigint_length_B(&key->modulus) - length_B, src, length_B); // cli_putc('a'); uart_flush(0); x.wordv = dest; x.length_B = key->modulus.length_B; x.info = 0; bigint_adjust(&x); // cli_putc('b'); uart_flush(0); rsa_os2ip(&x, NULL, bigint_length_B(&key->modulus)); #if DEBUG cli_putstr_P(PSTR("\r\n rsa decrypting ...")); #endif rsa_dec(&x, key); #if DEBUG cli_putstr_P(PSTR(" [done]")); #endif rsa_i2osp(NULL, &x, &data_len); // cli_putstr("\r\n msg (raw, pre-move):\r\n"); // cli_hexdump_block(dest, bigint_length_B(key->modulus), 4, 16); if(data_len > x_len){ return 7; } /* cli_putstr("\r\n moving some bytes; x_len = "); cli_hexdump_rev(&x_len, 2); cli_putstr(" data_len = "); cli_hexdump_rev(&data_len, 2); uart_flush(0); */ if(x_len != data_len){ memmove((uint8_t*)dest + x_len - data_len, dest, data_len); // cli_putstr(" (oh, not dead yet?!)"); // uart_flush(0); memset(dest, 0, x_len - data_len); } hfal_hash_mem(p->hf, label_hv, label->label, label->length_b); /* cli_putstr("\r\n msg (raw, pre-feistel):\r\n"); cli_hexdump_block(seed_buffer, bigint_length_B(key->modulus), 4, 16); uart_flush(0); */ p->mgf(maskbuffer, db_buffer, db_len, hv_len, p->mgf_parameter); memxor(seed_buffer, maskbuffer, hv_len); p->mgf(maskbuffer, seed_buffer, hv_len, db_len, p->mgf_parameter); memxor(db_buffer, maskbuffer, db_len); if(memcmp(label_hv, db_buffer, hv_len)){ // cli_putstr("\r\nDBG: DB:\r\n"); // cli_hexdump_block(db_buffer, db_len, 4, 16); return 2; } uint16_t ps_len=0; while(db_buffer[hv_len + ps_len++] == 0) ; --ps_len; if(db_buffer[hv_len + ps_len] != 1){ return 3; } if(seed){ memcpy(seed, seed_buffer, hv_len); } msg_len = db_len - hv_len - 1 - ps_len; memmove(dest, db_buffer + hv_len + ps_len + 1, msg_len); *out_length = msg_len; return 0; }