void nessie_print_item(char* name, uint8_t* buffer, uint16_t size_B){ uint8_t name_len; uint8_t i; name_len=strlen(name); if(name_len>SPACES-1){ NESSIE_PUTSTR_P(PSTR("\r\n!!! formatting error !!!\r\n")); return; } NESSIE_PUTSTR_P(PSTR("\r\n")); for(i=0; i<SPACES-name_len-1; ++i){ NESSIE_PUTC(' '); } NESSIE_PUTSTR(name); NESSIE_PUTC('='); /* now the data printing begins */ if(size_B<=BYTESPERLINE){ /* one line seems sufficient */ nessie_print_block(buffer, size_B*8); } else { /* we need more lines */ nessie_print_block(buffer, BYTESPERLINE*8); /* first line */ int16_t toprint = size_B - BYTESPERLINE; buffer += BYTESPERLINE; while(toprint > 0){ NESSIE_PUTSTR_P(PSTR("\r\n")); for(i=0; i<SPACES; ++i){ NESSIE_PUTC(' '); } nessie_print_block(buffer, ((toprint>BYTESPERLINE)?BYTESPERLINE:toprint)*8); buffer += BYTESPERLINE; toprint -= BYTESPERLINE; } } }
static void zero_hash(uint16_t n){ uint8_t ctx[nessie_hash_ctx.ctx_size_B]; uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8]; uint8_t block[nessie_hash_ctx.blocksize_B]; NESSIE_PUTSTR_P(PSTR("\r\n message=")); if(n>=10000) NESSIE_PUTC('0'+n/10000); if(n>=1000) NESSIE_PUTC('0'+(n/1000)%10); if(n>=100) NESSIE_PUTC('0'+(n/100)%10); if(n>=10) NESSIE_PUTC('0'+(n/10)%10); NESSIE_PUTC('0'+n%10); NESSIE_PUTSTR_P(PSTR(" zero bits")); memset(block, 0, nessie_hash_ctx.blocksize_B); nessie_hash_ctx.hash_init(ctx); while(n>=nessie_hash_ctx.blocksize_B*8){ nessie_hash_ctx.hash_next(ctx, block); n -= nessie_hash_ctx.blocksize_B*8; } nessie_hash_ctx.hash_last(ctx, block, n); nessie_hash_ctx.hash_conv(hash, ctx); nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); }
static void zero_mac(uint16_t n, uint8_t* key){ uint8_t ctx[nessie_mac_ctx.ctx_size_B]; uint8_t mac[MACSIZE_B]; uint8_t block[nessie_mac_ctx.blocksize_B]; NESSIE_PUTSTR_P(PSTR("\r\n message=")); if(n>=10000) NESSIE_PUTC('0'+n/10000); if(n>=1000) NESSIE_PUTC('0'+(n/1000)%10); if(n>=100) NESSIE_PUTC('0'+(n/100)%10); if(n>=10) NESSIE_PUTC('0'+(n/10)%10); NESSIE_PUTC('0'+n%10); NESSIE_PUTSTR_P(PSTR(" zero bits")); PRINTKEY; memset(block, 0, nessie_mac_ctx.blocksize_B); nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b,ctx);; while(n>nessie_mac_ctx.blocksize_B*8){ nessie_mac_ctx.mac_next(block, ctx); n -= nessie_mac_ctx.blocksize_B*8; } nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx); nessie_mac_ctx.mac_conv(mac, ctx); PRINTMAC; }
void nessie_print_set_vector(uint8_t set, uint16_t vector){ NESSIE_PUTSTR_P(PSTR("\r\n\r\nSet ")); NESSIE_PUTC('0'+set%10); NESSIE_PUTSTR_P(PSTR(", vector#")); NESSIE_PUTC((vector<1000)?' ':'0'+vector/1000); NESSIE_PUTC((vector<100)?' ':'0'+(vector/100)%10); NESSIE_PUTC((vector<10 )?' ':'0'+(vector/10)%10); NESSIE_PUTC('0'+vector%10); NESSIE_PUTC(':'); }
static void tv4_mac(uint8_t* key){ uint8_t ctx[nessie_mac_ctx.ctx_size_B]; uint8_t mac[MACSIZE_B]; uint8_t block[256/8]; uint16_t n=256; uint32_t i; NESSIE_PUTSTR_P(PSTR("\r\n message=")); NESSIE_PUTSTR(PSTR("256 zero bits")); memset(block, 0, 256/8); nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);; while(n>nessie_mac_ctx.blocksize_B*8){ nessie_mac_ctx.mac_next(block, ctx); n -= nessie_mac_ctx.blocksize_B*8; } nessie_mac_ctx.mac_last(block, n, key, nessie_mac_ctx.keysize_b, ctx); nessie_mac_ctx.mac_conv(mac, ctx); PRINTMAC; for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */ nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx);; nessie_mac_ctx.mac_last(mac, nessie_mac_ctx.macsize_b, key, nessie_mac_ctx.keysize_b, ctx); nessie_mac_ctx.mac_conv(mac, ctx); NESSIE_SEND_ALIVE_A(i); } nessie_print_item("iterated 100000 times", mac, MACSIZE_B); }
static void one_in512_mac(uint16_t pos, uint8_t* key){ uint8_t ctx[nessie_mac_ctx.ctx_size_B]; uint8_t mac[MACSIZE_B]; uint8_t block[nessie_mac_ctx.blocksize_B]; uint16_t n=512; char* tab[8]={"80", "40", "20", "10", "08", "04", "02", "01" }; pos&=511; NESSIE_PUTSTR_P(PSTR("\r\n message=")); NESSIE_PUTSTR_P(PSTR("512-bit string: ")); if((pos/8) >=10){ NESSIE_PUTC('0'+(pos/8/10)%10); } else { NESSIE_PUTC(' '); } NESSIE_PUTC('0'+(pos/8)%10); NESSIE_PUTSTR_P(PSTR("*00,")); NESSIE_PUTSTR(tab[pos&7]); NESSIE_PUTC(','); if(63-(pos/8) >=10){ NESSIE_PUTC('0'+((63-pos/8)/10)%10); } else { NESSIE_PUTC(' '); } NESSIE_PUTC('0'+(63-pos/8)%10); NESSIE_PUTSTR_P(PSTR("*00")); PRINTKEY; /* now the real stuff */ memset(block, 0, 512/8); block[pos>>3] = 0x80>>(pos&0x7); uint8_t* bp; bp = block; nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx); while(n>nessie_mac_ctx.blocksize_B*8){ nessie_mac_ctx.mac_next(bp, ctx); n -= nessie_mac_ctx.blocksize_B*8; bp += nessie_mac_ctx.blocksize_B; } nessie_mac_ctx.mac_last(bp, n, key, nessie_mac_ctx.keysize_b, ctx); nessie_mac_ctx.mac_conv(mac, ctx); PRINTMAC; }
static void one_in512_hash(uint16_t pos){ uint8_t ctx[nessie_hash_ctx.ctx_size_B]; uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8]; uint8_t block[nessie_hash_ctx.blocksize_B]; uint16_t n=512; char* tab[8]={"80", "40", "20", "10", "08", "04", "02", "01" }; pos&=511; NESSIE_PUTSTR_P(PSTR("\r\n message=")); NESSIE_PUTSTR_P(PSTR("512-bit string: ")); if((pos/8) >=10){ NESSIE_PUTC('0'+(pos/8/10)%10); } else { NESSIE_PUTC(' '); } NESSIE_PUTC('0'+(pos/8)%10); NESSIE_PUTSTR_P(PSTR("*00,")); NESSIE_PUTSTR(tab[pos&7]); NESSIE_PUTC(','); if(63-(pos/8) >=10){ NESSIE_PUTC('0'+((63-pos/8)/10)%10); } else { NESSIE_PUTC(' '); } NESSIE_PUTC('0'+(63-pos/8)%10); NESSIE_PUTSTR_P(PSTR("*00")); /* now the real stuff */ memset(block, 0, 512/8); block[pos>>3] = 0x80>>(pos&0x7); nessie_hash_ctx.hash_init(ctx); while(n>=nessie_hash_ctx.blocksize_B*8){ nessie_hash_ctx.hash_next(ctx, block); n -= nessie_hash_ctx.blocksize_B*8; } nessie_hash_ctx.hash_last(ctx, block, n); nessie_hash_ctx.hash_conv(hash, ctx); nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); }
static void amillion_hash(void){ uint8_t ctx[nessie_hash_ctx.ctx_size_B]; uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8]; uint8_t block[nessie_hash_ctx.blocksize_B]; uint32_t n=1000000LL; uint16_t i=0; NESSIE_PUTSTR_P(PSTR("\r\n message=")); NESSIE_PUTSTR_P(PSTR("1 million times \"a\"")); memset(block, 'a', nessie_hash_ctx.blocksize_B); nessie_hash_ctx.hash_init(ctx); while(n>=nessie_hash_ctx.blocksize_B){ nessie_hash_ctx.hash_next(ctx, block); n -= nessie_hash_ctx.blocksize_B; NESSIE_SEND_ALIVE_A(i++); } nessie_hash_ctx.hash_last(ctx, block, n*8); nessie_hash_ctx.hash_conv(hash, ctx); nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); }
static void tv4_hash(void){ uint8_t ctx[nessie_hash_ctx.ctx_size_B]; uint8_t hash[(nessie_hash_ctx.hashsize_b+7)/8]; uint8_t block[nessie_hash_ctx.hashsize_b/8]; uint16_t n=nessie_hash_ctx.hashsize_b; uint32_t i; NESSIE_PUTSTR_P(PSTR("\r\n message=")); if(nessie_hash_ctx.hashsize_b>=10000) NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10000)%10); if(nessie_hash_ctx.hashsize_b>=1000) NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/1000)%10); if(nessie_hash_ctx.hashsize_b>=100) NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/100)%10); if(nessie_hash_ctx.hashsize_b>=10) NESSIE_PUTC('0' + (nessie_hash_ctx.hashsize_b/10)%10); NESSIE_PUTC('0' + nessie_hash_ctx.hashsize_b%10); NESSIE_PUTSTR_P(PSTR(" zero bits")); memset(block, 0, 256/8); nessie_hash_ctx.hash_init(ctx); while(n>=nessie_hash_ctx.blocksize_B*8){ nessie_hash_ctx.hash_next(ctx, block); n -= nessie_hash_ctx.blocksize_B*8; } nessie_hash_ctx.hash_last(ctx, block, n); nessie_hash_ctx.hash_conv(hash, ctx); nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); if(nessie_hash_quick) return; for(i=1; i<100000L; ++i){ /* this assumes BLOCKSIZE >= HASHSIZE */ nessie_hash_ctx.hash_init(ctx); nessie_hash_ctx.hash_last(ctx, hash, nessie_hash_ctx.hashsize_b); nessie_hash_ctx.hash_conv(hash, ctx); NESSIE_SEND_ALIVE_A(i); } nessie_print_item("iterated 100000 times", hash, (nessie_hash_ctx.hashsize_b+7)/8); }
static void ascii_hash_P(PGM_P data, PGM_P desc){ uint8_t ctx[nessie_hash_ctx.ctx_size_B]; uint8_t hash[HASHSIZE_B]; uint16_t sl; uint8_t buffer[BLOCKSIZE_B]; NESSIE_PUTSTR_P(PSTR("\r\n message=")); NESSIE_PUTSTR_P(desc); nessie_hash_ctx.hash_init(ctx); sl = strlen_P(data); while(sl>=BLOCKSIZE_B){ memcpy_P(buffer, data, BLOCKSIZE_B); nessie_hash_ctx.hash_next(ctx, buffer); data += BLOCKSIZE_B; sl -= BLOCKSIZE_B; } memcpy_P(buffer, data, sl); nessie_hash_ctx.hash_last(ctx, buffer, sl*8); nessie_hash_ctx.hash_conv(hash, ctx); nessie_print_item("hash", hash, (nessie_hash_ctx.hashsize_b+7)/8); }
static void amillion_mac(uint8_t* key){ uint8_t ctx[nessie_mac_ctx.ctx_size_B]; uint8_t mac[MACSIZE_B]; uint8_t block[nessie_mac_ctx.blocksize_B]; uint32_t n=1000000LL; uint16_t i=0; NESSIE_PUTSTR_P(PSTR("\r\n message=")); NESSIE_PUTSTR_P(PSTR("1 million times \"a\"")); PRINTKEY; memset(block, 'a', nessie_mac_ctx.blocksize_B); nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx); while(n>nessie_mac_ctx.blocksize_B){ nessie_mac_ctx.mac_next(block, ctx); n -= nessie_mac_ctx.blocksize_B; NESSIE_SEND_ALIVE_A(i++); } nessie_mac_ctx.mac_last(block, n*8, key, nessie_mac_ctx.keysize_b, ctx); nessie_mac_ctx.mac_conv(mac, ctx); PRINTMAC; }
static void ascii_mac(char* data, char* desc, uint8_t* key){ uint8_t ctx[nessie_mac_ctx.ctx_size_B]; uint8_t mac[MACSIZE_B]; uint16_t sl; NESSIE_PUTSTR_P(PSTR("\r\n message=")); NESSIE_PUTSTR(desc); PRINTKEY; nessie_mac_ctx.mac_init(key, nessie_mac_ctx.keysize_b, ctx); sl = strlen(data); while(sl>nessie_mac_ctx.blocksize_B){ nessie_mac_ctx.mac_next(data, ctx); data += nessie_mac_ctx.blocksize_B; sl -= nessie_mac_ctx.blocksize_B; } nessie_mac_ctx.mac_last(data, sl*8, key, nessie_mac_ctx.keysize_b, ctx); nessie_mac_ctx.mac_conv(mac, ctx); PRINTMAC; }
void nessie_print_footer(void){ NESSIE_PUTSTR_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n")); }
void nessie_print_header(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; NESSIE_PUTSTR_P(PSTR("\r\n\r\n" "********************************************************************************\r\n" "* AVR-Crypto-Lib - crypto primitives for AVR microcontrolles by Daniel Otte *\r\n" "********************************************************************************\r\n" "\r\n")); NESSIE_PUTSTR_P(PSTR("Primitive Name: ")); NESSIE_PUTSTR(name); NESSIE_PUTSTR_P(PSTR("\r\n")); /* underline */ for(i=0; i<16+strlen(name); ++i){ NESSIE_PUTC('='); } char str[6]; /* must catch numbers up to 65535 + terminatin \0 */ if(keysize_b){ NESSIE_PUTSTR_P(PSTR("\r\nKey size: ")); utoa(keysize_b, str, 10); NESSIE_PUTSTR(str); NESSIE_PUTSTR_P(PSTR(" bits")); } if(blocksize_b){ NESSIE_PUTSTR_P(PSTR("\r\nBlock size: ")); utoa(blocksize_b, str, 10); NESSIE_PUTSTR(str); NESSIE_PUTSTR_P(PSTR(" bits")); } if(hashsize_b){ NESSIE_PUTSTR_P(PSTR("\r\nHash size: ")); utoa(hashsize_b, str, 10); NESSIE_PUTSTR(str); NESSIE_PUTSTR_P(PSTR(" bits")); } if(macsize_b){ NESSIE_PUTSTR_P(PSTR("\r\nMac size: ")); utoa(macsize_b, str, 10); NESSIE_PUTSTR(str); NESSIE_PUTSTR_P(PSTR(" bits")); } if(ivsize_b){ if(ivsize_b==(uint16_t)-1){ NESSIE_PUTSTR_P(PSTR("\r\nNo initial value (IV) mode")); }else{ NESSIE_PUTSTR_P(PSTR("\r\nIV size: ")); utoa(ivsize_b, str, 10); NESSIE_PUTSTR(str); NESSIE_PUTSTR_P(PSTR(" bits")); } } NESSIE_PUTSTR_P(PSTR("\r\n")); }
/* example: Test vectors -- set 3 ===================== */ void nessie_print_setheader(uint8_t set){ NESSIE_PUTSTR_P(PSTR("\r\n\r\nTest vectors -- set ")); NESSIE_PUTC('0'+set%10); NESSIE_PUTSTR_P(PSTR("\r\n=====================")); }