void testrun_long_cscipher(void){ uint8_t data[8]; char str[10]; uint16_t i; uint8_t j; uint8_t key[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }; cscipher_ctx_t ctx; cli_putstr("\r\n== CS-Cipher test==\r\nkey: "); cli_hexdump(key, 16); cscipher_init(key, &ctx); memset(data, 0, 8); cli_putstr("\r\nplain: "); cli_hexdump(data, 8); cli_putstr("\r\nencrypting 1,000,000 times:\r\n"); for(i=0; i<10000;++i){ for(j=0;j<100;++j){ cscipher_enc(data, &ctx); } if(i%128==0){ sprintf(str, "%d", i); cli_putstr("\r\n"); cli_putstr(str); cli_putstr(": "); } cli_putc('*'); } cli_putstr("\r\ncipher: "); cli_hexdump(data, 8); cli_putstr("\r\ndecrypting 1,000,000 times:"); for(i=0; i<10000;++i){ for(j=0;j<100;++j){ cscipher_dec(data, &ctx); } if(i%128==0){ sprintf(str, "%d", i); cli_putstr("\r\n"); cli_putstr(str); cli_putstr(": "); } cli_putc('*'); } cli_putstr("\r\nplain: "); cli_hexdump(data, 8); }
void hexdump128(void* data){ uint8_t i; for(i=0; i<16; ++i){ cli_hexdump(data, 1); cli_putc(' '); data = (uint8_t*)data +1; } }
void nessie_print_header(const char* name, uint16_t keysize_b, uint16_t blocksize_b, uint16_t hashsize_b, uint16_t macsize_b, uint16_t ivsize_b ){ uint16_t i; cli_putstr("\r\n\r\n" "********************************************************************************\r\n" "* ARM-Crypto-Lib - crypto primitives for ARM microcontrollers by Daniel Otte *\r\n" "********************************************************************************\r\n" "\r\n"); cli_putstr("Primitive Name: "); cli_putstr(name); cli_putstr("\r\n"); /* underline */ for(i=0; i<16+strlen(name); ++i){ cli_putc('='); } char str[6]; /* must catch numbers up to 65535 + terminating \0 */ if(keysize_b){ cli_putstr("\r\nKey size: "); ustoa(keysize_b, str, 10); cli_putstr(str); cli_putstr(" bits"); } if(blocksize_b){ cli_putstr("\r\nBlock size: "); ustoa(blocksize_b, str, 10); cli_putstr(str); cli_putstr(" bits"); } if(hashsize_b){ cli_putstr("\r\nHash size: "); ustoa(hashsize_b, str, 10); cli_putstr(str); cli_putstr(" bits"); } if(macsize_b){ cli_putstr("\r\nMac size: "); ustoa(macsize_b, str, 10); cli_putstr(str); cli_putstr(" bits"); } if(ivsize_b){ if(ivsize_b==(uint16_t)-1){ cli_putstr("\r\nNo initial value (IV) mode"); }else{ cli_putstr("\r\nIV size: "); ustoa(ivsize_b, str, 10); cli_putstr(str); cli_putstr(" bits"); } } cli_putstr("\r\n"); }
void testrun_testkey_aes128(void){ uint8_t key[16] = { 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c}; aes128_ctx_t ctx; uint8_t i; aes128_init(key, &ctx); cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "); cli_hexdump(key, 16); for(i=0; i<11; ++i){ cli_putstr("\r\n index: "); cli_putc('0'+i/10); cli_putc('0'+i%10); cli_putstr(" roundkey "); cli_hexdump(ctx.key[i].ks, 16); } }
static void md5_core(uint32_t* a, void* block, uint8_t as, uint8_t s, uint8_t i, uint8_t fi){ uint32_t t; md5_func_t* funcs[]={md5_F, md5_G, md5_H, md5_I}; as &= 0x3; /* a = b + ((a + F(b,c,d) + X[k] + T[i]) <<< s). */ #ifdef DEBUG char funcc[]={'*', '-', '+', '~'}; cli_putstr("\r\n DBG: md5_core ["); cli_putc(funcc[fi]); cli_hexdump(&as, 1); cli_putc(' '); cli_hexdump(&k, 1); cli_putc(' '); cli_hexdump(&s, 1); cli_putc(' '); cli_hexdump(&i, 1); cli_putc(']'); #endif t = a[as] + funcs[fi](a[(as+1)&3], a[(as+2)&3], a[(as+3)&3]) + *((uint32_t*)block) + pgm_read_dword(md5_T+i) ; a[as]=a[(as+1)&3] + ROTL32(t, s); }
void dump_m(const uint8_t* m){ uint8_t i,j; for(i=0; i<8; ++i){ cli_putstr("\r\n"); for(j=0; j<8; ++j){ cli_putc(' '); cli_hexdump(m+8*i+j, 1); } } }
static void printvalue(unsigned long v){ char str[20]; int i; ultoa(v, str, 10); for(i=0; i<10-strlen(str); ++i){ cli_putc(' '); } cli_putstr(str); }
void print_time(const char* s, uint64_t t){ char sv[22]; uint8_t c; cli_putstr("\r\n"); cli_putstr(s); ulltoa(t, sv, 10); for(c=strlen(sv); c<11; ++c){ cli_putc(' '); } cli_putstr(sv); }
void testrun_testkey_aes192(void){ uint8_t key[24] = { 0x8e, 0x73, 0xb0, 0xf7, 0xda, 0x0e, 0x64, 0x52, 0xc8, 0x10, 0xf3, 0x2b, 0x80, 0x90, 0x79, 0xe5, 0x62, 0xf8, 0xea, 0xd2, 0x52, 0x2c, 0x6b, 0x7b}; aes192_ctx_t ctx; uint8_t i; memset(&ctx, 0, sizeof(aes192_ctx_t)); aes192_init(key, &ctx); cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "); cli_hexdump(key, 24); for(i=0; i<13; ++i){ cli_putstr("\r\n index: "); cli_putc('0'+i/10); cli_putc('0'+i%10); cli_putstr(" roundkey "); cli_hexdump(ctx.key[i].ks, 16); } }
void test_sbox(void){ uint8_t i=0,x; cli_putstr_P(PSTR("\r\nKhazad Sbox:\r\n\t")); do{ x = khazad_sbox(i); cli_hexdump_byte(x); cli_putc(' '); if(i%16==15){ cli_putstr_P(PSTR("\r\n\t")); } ++i; }while(i); }
void testrun_testkey_aes256(void){ uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4}; aes256_ctx_t ctx; uint8_t i; memset(&ctx, 0, sizeof(aes256_ctx_t)); aes256_init(key, &ctx); cli_putstr("\r\n\r\n keyschedule test (FIPS 197):\r\n key: "); cli_hexdump(key, 32); for(i=0; i<15; ++i){ cli_putstr("\r\n index: "); cli_putc('0'+i/10); cli_putc('0'+i%10); cli_putstr(" roundkey "); cli_hexdump(ctx.key[i].ks, 16); } }
void testrun_nessie2(void){ uint16_t i; uint8_t j; uint8_t data[128], hash[64]; whirlpool_ctx_t ctx; char str[8]; memset(data, 0, 128); cli_putstr_P(PSTR("\r\nMessage digests of strings of 0-bits and length L:")); for(i=0; i<1024; ++i){ cli_putstr_P(PSTR("\r\n L = ")); itoa(i, str, 10); j=4; j-= strlen(str); if(j>3){ j=0; } while(j--){ cli_putc(' '); } cli_putstr(str); cli_putstr_P(PSTR(": ")); whirlpool_init(&ctx); whirlpool_lastBlock(&ctx, data, i); whirlpool_ctx2hash(hash, &ctx); cli_hexdump(hash, 64); } cli_putstr_P(PSTR("\r\nMessage digests of all 512-bit strings S containing a single 1-bit:")); for(i=0; i<512; ++i){ cli_putstr_P(PSTR("\r\n S = ")); data[i/8] = 0x80>>(i&7); cli_hexdump(data, 64); cli_putstr_P(PSTR(": ")); whirlpool_init(&ctx); whirlpool_lastBlock(&ctx, data, 512); whirlpool_ctx2hash(hash, &ctx); data[i/8] = 0x00; cli_hexdump(hash, 64); } cli_putstr_P(PSTR("\r\nIterated message digest computation (100000000 times): ")); uint32_t c; memset(hash, 0, 64); for(c=0; c<1000000; ++c){ whirlpool_init(&ctx); whirlpool_lastBlock(&ctx, hash, 512); whirlpool_ctx2hash(hash, &ctx); } cli_hexdump(hash, 64); cli_putstr_P(PSTR("\r\n/* finish generating testvectors */\r\n")); }
void nessie_print_set_vector(uint8_t set, uint16_t vector){ cli_putstr("\r\n\r\nSet "); cli_putc('0'+set%10); cli_putstr(", vector#"); cli_putc((vector<1000)?' ':'0'+vector/1000); cli_putc((vector<100)?' ':'0'+(vector/100)%10); cli_putc((vector<10 )?' ':'0'+(vector/10)%10); cli_putc('0'+vector%10); cli_putc(':'); }
void testrun_entropium(void){ char c, str[16]; uint8_t data[32]; uint32_t i=0; while('q'!=cli_getc()){ entropium_getRandomBlock(data); cli_putstr_P(PSTR("\r\n ")); ultoa(i, str, 10); for(c=strlen(str); c<11; ++c){ cli_putc(' '); } cli_putstr(str); ++i; cli_putstr_P(PSTR(" : ")); cli_hexdump(data, 32); } cli_putstr_P(PSTR("\r\n\r\n")); }
int main (void){ sysclk_set_freq(SYS_FREQ); sysclk_mosc_verify_enable(); uart_init(UART_0, 115200, 8, UART_PARATY_NONE, UART_STOPBITS_ONE); gptm_set_timer_32periodic(TIMER0); cli_rx = (cli_rx_fpt)uart0_getc; cli_tx = (cli_tx_fpt)uart0_putc; for(;;){ cli_putstr("\r\n\r\nARM-Crypto-Lib VS ("); cli_putstr(algo_name); cli_putstr("; "); cli_putstr(__DATE__); cli_putc(' '); cli_putstr(__TIME__); cli_putstr(")\r\nloaded and running\r\n"); cmd_interface(cmdlist); } }
void dsa_print_item(bigint_t *a, PGM_P pstr){ uint8_t *p; cli_putstr_P(PSTR("\r\n")); cli_putstr_P(pstr); cli_putstr_P(PSTR(": ")); uint16_t i; p = a->wordv + a->length_W -1; for(i=0; i<a->length_W-1; ++i){ if(i%16==0){ cli_putstr_P(PSTR("\r\n ")); } cli_hexdump(p, 1); cli_putc(':'); --p; } if(i%16==0){ cli_putstr_P(PSTR("\r\n ")); } cli_hexdump(p, 1); }
void dsa_print_item(bigint_t* a, const char* pstr){ uint8_t *p; cli_putstr("\r\n"); cli_putstr(pstr); cli_putstr(": "); uint16_t i; p = (uint8_t*)a->wordv + a->length_W*sizeof(bigint_word_t) -1; for(i=0; i<a->length_W*sizeof(bigint_word_t)-1; ++i){ if(i%16==0){ cli_putstr("\r\n "); } cli_hexdump(p, 1); cli_putc(':'); --p; } if(i%16==0){ cli_putstr("\r\n "); } cli_hexdump(p, 1); }
void testrun_aes128_eax(void){ uint8_t key[16]; uint8_t nonce[16]; uint8_t header[8]; uint8_t tag[16]; uint8_t msg[21]; uint8_t msg_len; const void* msg_p; const void* cipher_p; uint8_t i, r; bcal_eax_ctx_t ctx; msg_p = eax_msg; cipher_p = eax_cipher; for(i=0; i<10; ++i){ cli_putstr("\r\n\r\n** AES128-EAX-TEST #"); cli_putc('0'+i); cli_putstr(" **"); msg_len = eax_msg_len[i]; memcpy(key, eax_key+16*i, 16); memcpy(nonce, eax_nonce+16*i, 16); memcpy(header, eax_header+8*i, 8); memcpy(msg, msg_p, msg_len); msg_p = (uint8_t*)msg_p+msg_len; cli_putstr("\r\n key: "); cli_hexdump(key, 16); cli_putstr("\r\n msg: "); if(msg_len){ cli_hexdump(msg, msg_len); } cli_putstr("\r\n nonce: "); cli_hexdump(nonce, 16); cli_putstr("\r\n header: "); cli_hexdump(header, 8); r = bcal_eax_init(&aes128_desc, key, 128, &ctx); cli_putstr("\r\n init = 0x"); cli_hexdump(&r, 1); if(r) return; bcal_eax_loadNonce(nonce, 16*8, &ctx); bcal_eax_addLastHeader(header, 8*8, &ctx); bcal_eax_encLastBlock(msg, msg_len*8, &ctx); bcal_eax_ctx2tag(tag, 128, &ctx); cli_putstr("\r\n cipher: "); cli_hexdump_block(msg, msg_len, 4, 16); cli_putstr("\r\n tag: "); cli_hexdump_block(tag, 16, 4, 16); if(memcmp(msg, cipher_p, msg_len)){ cli_putstr("\r\n cipher: [fail]\r\n should: "); memcpy(msg, cipher_p, msg_len); cli_hexdump_block(msg, msg_len, 4, 16); }else{ cli_putstr("\r\n cipher: [pass]"); } cipher_p = ((uint8_t*)cipher_p)+msg_len; // * if(memcmp(tag, cipher_p, 16)){ cli_putstr("\r\n tag: [fail]"); }else{ cli_putstr("\r\n tag: [pass]"); } cipher_p = ((uint8_t*)cipher_p)+16; bcal_eax_free(&ctx); } }
void groestl_small_rounds(uint8_t *m, uint8_t q){ uint8_t r,i,j; uint8_t tmp[8]; #if DEBUG cli_putstr_P(PSTR("\r\n:: BEGIN ")); cli_putc(q?'Q':'P'); #endif for(r=0; r<ROUNDS; ++r){ if(q){ for(i=0; i<8*8; ++i){ m[i] ^= 0xff; } for(i=0; i<8; ++i){ m[7+i*8] ^= r ^ (i<<4); } }else{ for(i=0; i<8; ++i){ m[i*8] ^= r ^ (i<<4); } } #if DEBUG // if(r<2){ cli_putstr_P(PSTR("\r\npost add-const")); dump_m(m); // } #endif for(i=0;i<8*8; ++i){ m[i] = pgm_read_byte(aes_sbox+m[i]); } if(!q){ shift_columns(m, p_shifts); }else{ shift_columns(m, q_shifts); } #if DEBUG if(r<2){ cli_putstr_P(PSTR("\r\npost shift-bytes")); dump_m(m); } #endif for(i=0; i<8; ++i){ memcpy(tmp, m+8*i, 8); for(j=0; j<8; ++j){ m[j+i*8] = gf256mul(pgm_read_byte(matrix+8*j+0),tmp[0], POLYNOM) ^ gf256mul(pgm_read_byte(matrix+8*j+1),tmp[1], POLYNOM) ^ gf256mul(pgm_read_byte(matrix+8*j+2),tmp[2], POLYNOM) ^ gf256mul(pgm_read_byte(matrix+8*j+3),tmp[3], POLYNOM) ^ gf256mul(pgm_read_byte(matrix+8*j+4),tmp[4], POLYNOM) ^ gf256mul(pgm_read_byte(matrix+8*j+5),tmp[5], POLYNOM) ^ gf256mul(pgm_read_byte(matrix+8*j+6),tmp[6], POLYNOM) ^ gf256mul(pgm_read_byte(matrix+8*j+7),tmp[7], POLYNOM); } } #if DEBUG if(r<2){ cli_putstr_P(PSTR("\r\npost mix-bytes")); dump_m(m); } #endif } }
void shavs_test3(void){ /* Monte Carlo tests for SHA-3 */ uint16_t expected_input; uint16_t count; uint8_t v; uint8_t index=0; char c; if(!shavs_algo){ cli_putstr("\r\nERROR: select algorithm first!"); return; } uint8_t ml=shavs_algo->hashsize_b/8; uint8_t m[ml+128]; for(;;){ while((c=cli_getc_cecho())!='S' && c!='s'){ if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (1) [0x"); cli_hexdump(&c, 1); cli_putstr("]!\r\n"); return; } } if((c=cli_getc_cecho())!='e' && c!='E'){ cli_putstr("\r\nERROR: wrong input (2)!\r\n"); return; } if((c=cli_getc_cecho())!='e' && c!='E'){ cli_putstr("\r\nERROR: wrong input (3)!\r\n"); return; } if((c=cli_getc_cecho())!='d' && c!='D'){ cli_putstr("\r\nERROR: wrong input (4)!\r\n"); return; } while((c=cli_getc_cecho())!='='){ if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (5)!\r\n"); return; } } expected_input = 1024/4; memset(m+ml, 0, 1024/8); do{ v=0xff; c=cli_getc_cecho(); if(c>='0' && c<='9'){ v = c - '0'; }else{ c |= 'A'^'a'; if(c>='a' && c<='f'){ v = c - 'a' +10; } } if(v<0x10){ c=m[ml+index/2]; if(index&1){ c |= v; }else{ c |=v<<4; } m[ml+index/2]=c; index++; expected_input--; } }while(expected_input); /* so we have the seed */ cli_putstr("\r\nstarting processing"); uint16_t j; for(count=0; count<100; ++count){ for(j=0; j<1000; ++j){ hfal_hash_mem(shavs_algo, m, m+ml, 1024); memmove(m+ml, m, 1024/8); } cli_putstr("\r\n\r\nj = "); if(count>=10){ cli_putc(count/10+'0'); } cli_putc(count%10+'0'); cli_putstr("\r\nMD = "); cli_hexdump(m+ml, ml); } } }
void shavs_test1(void){ /* KAT tests */ uint32_t length=0; int32_t expect_input=0; if(!shavs_algo){ cli_putstr("\r\nERROR: select algorithm first!"); return; } char c; uint8_t diggest[shavs_algo->hashsize_b/8]; shavs_ctx.buffersize_B=shavs_algo->blocksize_b/8; uint8_t buffer[shavs_ctx.buffersize_B+5]; shavs_ctx.buffer = buffer; cli_putstr("\r\nbuffer_size = 0x"); cli_hexdump_rev(&(shavs_ctx.buffersize_B), 2); cli_putstr(" bytes"); for(;;){ shavs_ctx.blocks = 0; memset(buffer, 0, shavs_ctx.buffersize_B); length = getLength(); if((int32_t)length<0){ #if DEBUG cli_putstr("\r\n(x) Len == "); cli_hexdump_rev(&length, 4); uart_flush(0); #endif return; } #if DEBUG cli_putstr("\r\nLen == "); cli_hexdump_rev(&length, 4); uart_flush(0); #endif if(length==0){ expect_input=2; }else{ expect_input=((length + 7) >> 2) & (~1L); } #if DEBUG cli_putstr("\r\nexpected_input == "); cli_hexdump_rev(&expect_input, 4); if(expect_input==0) cli_putstr("\r\nexpected_input == 0 !!!"); #endif shavs_ctx.buffer_idx = 0; shavs_ctx.in_byte = 0; shavs_ctx.blocks = 0; uint8_t ret; #if DEBUG cli_putstr("\r\n HFAL init"); cli_putstr("\r\n (2) expected_input == "); cli_hexdump_rev(&expect_input, 4); #endif ret = hfal_hash_init(shavs_algo, &(shavs_ctx.ctx)); if(ret){ cli_putstr("\r\n HFAL init returned with: "); cli_hexdump(&ret, 1); return; } #if DEBUG cli_putstr("\r\n (3) expected_input == "); cli_hexdump_rev(&expect_input, 4); cli_putstr("\r\n"); #endif while((c=cli_getc_cecho())!='M' && c!='m'){ if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (1) [0x"); cli_hexdump(&c, 1); cli_putstr("]!\r\n"); hfal_hash_free(&(shavs_ctx.ctx)); return; } } if((c=cli_getc_cecho())!='s' && c!='S'){ cli_putstr("\r\nERROR: wrong input (2)!\r\n"); hfal_hash_free(&(shavs_ctx.ctx)); return; } if((c=cli_getc_cecho())!='g' && c!='G'){ cli_putstr("\r\nERROR: wrong input (3)!\r\n"); hfal_hash_free(&(shavs_ctx.ctx)); return; } while((c=cli_getc_cecho())!='='){ if(!isblank((uint8_t)c)){ cli_putstr("\r\nERROR: wrong input (4)!\r\n"); hfal_hash_free(&(shavs_ctx.ctx)); return; } } #if DEBUG cli_putstr("\r\nparsing started"); #endif shavs_ctx.buffer_idx = 0; shavs_ctx.in_byte = 0; shavs_ctx.blocks = 0; while(expect_input>0){ c=cli_getc_cecho(); #if DEBUG cli_putstr("\r\n\t("); cli_hexdump_rev(&expect_input, 4); cli_putstr(") "); #endif if(buffer_add(c)==0){ --expect_input; }else{ if(!isblank((uint16_t)c)){ cli_putstr("\r\nERROR: wrong input (5) ("); cli_putc(c); cli_putstr(")!\r\n"); hfal_hash_free(&(shavs_ctx.ctx)); return; } } } #if DEBUG cli_putstr("\r\nBuffer-A:"); cli_hexdump_block(buffer, shavs_ctx.buffersize_B, 5, 8); cli_putstr("\r\n starting finalization"); cli_putstr("\r\n\tblocks == "); cli_hexdump_rev(&(shavs_ctx.blocks),4); cli_putstr("\r\n\tbuffer_idx == "); cli_hexdump_rev(&(shavs_ctx.buffer_idx),2); cli_putstr("\r\n\tin_byte == "); cli_hexdump_rev(&(shavs_ctx.in_byte),1); cli_putstr("\r\n starting last block"); cli_putstr("\r\n\tlength == "); cli_hexdump_rev(&length,4); cli_putstr("\r\n\tbuffersize_B == "); cli_hexdump_rev(&(shavs_ctx.buffersize_B),2); uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8); cli_putstr("\r\n\t (temp) == "); cli_hexdump_rev(&temp,2); #else uint16_t temp=length-(shavs_ctx.blocks)*((shavs_ctx.buffersize_B)*8); #endif hfal_hash_lastBlock( &(shavs_ctx.ctx), buffer, /* be aware of freaking compilers!!! */ temp ); #if DEBUG cli_putstr("\r\n starting ctx2hash"); #endif hfal_hash_ctx2hash(diggest, &(shavs_ctx.ctx)); #if DEBUG cli_putstr("\r\n starting hash free"); #endif hfal_hash_free(&(shavs_ctx.ctx)); cli_putstr("\r\n MD = "); cli_hexdump(diggest, shavs_algo->hashsize_b/8); } }
void nessie_send_alive_a(uint16_t i){ if((i&31)==0) cli_putc(NESSIE_ALIVE_CHAR); }
void nessie_send_alive(void){ cli_putc(NESSIE_ALIVE_CHAR); }
/* example: Test vectors -- set 3 ===================== */ void nessie_print_setheader(uint8_t set){ cli_putstr("\r\n\r\nTest vectors -- set "); cli_putc('0'+set%10); cli_putstr("\r\n====================="); }
void run_seed_test(void){ uint8_t *msg, *ciph, *msg_; uint16_t ciph_len, msg_len; uint16_t msg_len_; uint16_t seed_len; uint8_t *seed, *seed_out; char read_int_str[18]; cli_putstr_P(PSTR("\r\n== test with given seed ==")); cli_putstr_P(PSTR("\r\n = message =")); cli_putstr_P(PSTR("\r\n length: ")); cli_getsn(read_int_str, 16); msg_len = own_atou(read_int_str); seed_len = rsa_pkcs1v15_compute_padlength_B(&pub_key.modulus, msg_len); seed = malloc(seed_len); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @seed: 0x")); cli_hexdump_rev(&seed, 2); #endif if(!seed){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } seed_out = malloc(seed_len); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @seed_out: 0x")); cli_hexdump_rev(&seed_out, 2); #endif if(!seed_out){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } msg = malloc(msg_len); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @msg: 0x")); cli_hexdump_rev(&msg, 2); #endif if(!msg){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } ciph = malloc(bigint_length_B(&pub_key.modulus)); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @ciph: 0x")); cli_hexdump_rev(&ciph, 2); #endif if(!ciph){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } msg_ = malloc(bigint_length_B(&pub_key.modulus) /* + sizeof(bigint_word_t) */ ); #if DEBUG cli_putstr_P(PSTR("\r\nDBG: @msg_: 0x")); cli_hexdump_rev(&msg_, 2); #endif if(!msg_){ cli_putstr_P(PSTR("\r\nERROR: OOM!")); return; } cli_putstr_P(PSTR("\r\n data: ")); read_os(msg, msg_len, NULL); cli_putstr_P(PSTR("\r\n seed (0x")); cli_hexdump_rev(&seed_len, 2); cli_putstr_P(PSTR(" bytes): ")); read_os(seed, seed_len, NULL); cli_putstr_P(PSTR("\r\n encrypting ...")); /* cli_putstr_P(PSTR("\r\n plaintext:")); cli_hexdump_block(msg, msg_len, 4, 16); cli_putstr_P(PSTR("\r\n seed:")); cli_hexdump_block(seed, seed_len, 4, 16); */ #if DEBUG cli_putstr_P(PSTR("\r\n first prime:")); bigint_print_hex(&priv_key.components[0]); #endif rsa_encrypt_pkcs1v15(ciph, &ciph_len, msg, msg_len, &pub_key, seed); cli_putstr_P(PSTR("\r\n ciphertext:")); cli_hexdump_block(ciph, ciph_len, 4, 16); #if DEBUG cli_putstr_P(PSTR("\r\n first prime:")); bigint_print_hex(&priv_key.components[0]); #endif cli_putstr_P(PSTR("\r\n decrypting ... ")); rsa_decrypt_pkcs1v15(msg_, &msg_len_, ciph, ciph_len, &priv_key, seed_out); cli_putstr_P(PSTR("[done]")); if(msg_len != msg_len_){ char tstr[16]; cli_putstr_P(PSTR("\r\nERROR: wrong decrypted message length (")); itoa(msg_len_, tstr, 10); cli_putstr(tstr); cli_putstr_P(PSTR(" instead of ")); itoa(msg_len, tstr, 10); cli_putstr(tstr); cli_putc(')'); goto end; } if(memcmp(msg, msg_, msg_len)){ cli_putstr_P(PSTR("\r\nERROR: wrong decrypted message:")); cli_hexdump_block(msg_, msg_len_, 4, 16); cli_putstr_P(PSTR("\r\nreference:")); cli_hexdump_block(msg, msg_len, 4, 16); goto end; } if(memcmp(seed, seed_out, seed_len)){ cli_putstr_P(PSTR("\r\nERROR: wrong decrypted seed:")); cli_hexdump_block(seed_out, seed_len, 4, 16); cli_putstr_P(PSTR("\r\nreference:")); cli_hexdump_block(seed, seed_len, 4, 16); goto end; } cli_putstr_P(PSTR("\r\n >>OK<<")); end: free(msg_); free(ciph); free(msg); free(seed_out); free(seed); }
void testrun_aes128_cfb1(void){ uint8_t key[16]; uint8_t iv[16]; uint8_t plain[64]; bcal_cfb_b_ctx_t ctx; uint8_t r; memcpy(key, modes_key, 16); memcpy(iv, modes_iv, 16); memcpy(plain, modes_plain, 64); cli_putstr("\r\n** AES128-CFB1-TEST **"); r = bcal_cfb_b_init(&aes128_desc, key, 128, 1, &ctx); cli_putstr("\r\n init = 0x"); cli_hexdump(&r, 1); cli_putstr("\r\n key: "); cli_hexdump(key, 128/8); cli_putstr("\r\n IV: "); cli_hexdump(iv, 128/8); cli_putstr("\r\n plaintext:"); cli_hexdump_block(plain, 2, 4, 16); if(r) return; uint8_t i, bit_offset, byte_offset; bcal_cfb_b_loadIV(iv, &ctx); for(i=0; i<16; ++i){ byte_offset = i/8; bit_offset = i&7; cli_putstr("\r\n plain bit: "); cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); bcal_cfb_b_encNext(plain+byte_offset, bit_offset, &ctx); cli_putstr("\r\n cipher bit: "); cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); } cli_putstr("\r\n ciphertext: "); cli_hexdump_block(plain, 2, 4, 16); bcal_cfb_b_loadIV(iv, &ctx); for(i=0; i<16; ++i){ byte_offset = i/8; bit_offset = i&7; cli_putstr("\r\n plain bit: "); cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); bcal_cfb_b_decNext(plain+byte_offset, bit_offset, &ctx); cli_putstr("\r\n cipher bit: "); cli_putc((plain[byte_offset]&(1<<(7-bit_offset)))?'1':'0'); } cli_putstr("\r\n plaintext: "); cli_hexdump_block(plain, 2, 4, 16); bcal_cfb_b_encMsg(iv, plain, 0, 64*8, &ctx); cli_putstr("\r\n ciphertext: "); cli_hexdump_block(plain, 64, 4, 16); bcal_cfb_b_decMsg(iv, plain, 0, 64*8, &ctx); cli_putstr("\r\n plaintext: "); cli_hexdump_block(plain, 64, 4, 16); bcal_cfb_b_free(&ctx); }