]> git.cryptolib.org Git - avr-crypto-lib.git/blobdiff - nessie_bc_test.c
insereated GPLv3 stub
[avr-crypto-lib.git] / nessie_bc_test.c
index c8f925738af7b964a8e99f1b4259ef9419e362d2..f9a91e547a8f82f745e9f9df73201d0da8245e50 100644 (file)
@@ -1,3 +1,21 @@
+/* nessie_bc_test.c */
+/*
+    This file is part of the Crypto-avr-lib/microcrypt-lib.
+    Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
+
+    This program is free software: you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+*/
 /**
  * 
  * author: Daniel Otte
 #include <stdint.h>
 #include <string.h>
 #include "nessie_bc_test.h"
+#include "nessie_common.h"
 #include "uart.h"
 
-
-
 nessie_bc_ctx_t nessie_bc_ctx;
 
-static void printblock(uint8_t* block, uint16_t blocksize_bit){
-       char tab [] = {'0', '1', '2', '3', 
-                                  '4', '5', '6', '7', 
-                                  '8', '9', 'A', 'B', 
-                                  'C', 'D', 'E', 'F'};
-       uint16_t i;
-       for(i=0; i<(blocksize_bit+7)/8; ++i){
-               uart_putc(tab[(block[i])>>4]);
-               uart_putc(tab[(block[i])&0xf]);
-       }                                  
+void nessie_bc_init(void){
+       memset(&nessie_bc_ctx, 0, sizeof(nessie_bc_ctx_t));     
+}
+static
+void nessie_bc_free(void* ctx){
+       if(nessie_bc_ctx.cipher_free)
+               nessie_bc_ctx.cipher_free(ctx);
 }
-
-#define SPACES 31
-#define BYTESPERLINE 16
-
-static void printitem(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){
-               uart_putstr_P(PSTR("\r\n!!! formatting error !!!\r\n"));
-               return;
-       }
-       uart_putstr_P(PSTR("\r\n"));
-       for(i=0; i<SPACES-name_len-1; ++i){
-               uart_putc(' ');
-       }
-       uart_putstr(name);
-       uart_putc('=');
-       /* now the data printing begins */
-       if(size_B<=BYTESPERLINE){
-               /* one line seems sufficient */
-               printblock(buffer, size_B*8);
-       } else {
-               /* we need more lines */
-               printblock(buffer, BYTESPERLINE*8); /* first line */
-               int16_t toprint = size_B - BYTESPERLINE;
-               buffer += BYTESPERLINE;
-               while(toprint > 0){
-                       uart_putstr_P(PSTR("\r\n"));
-                       for(i=0; i<SPACES; ++i){
-                               uart_putc(' ');
-                       }
-                       printblock(buffer, ((toprint>BYTESPERLINE)?BYTESPERLINE:toprint)*8);
-                       buffer  += BYTESPERLINE;
-                       toprint -= BYTESPERLINE;
-               }
-       }
-} 
 
 void nessie_bc_enc(uint8_t* key, uint8_t* pt){
        uint8_t ctx[nessie_bc_ctx.ctx_size_B];
@@ -72,28 +48,31 @@ void nessie_bc_enc(uint8_t* key, uint8_t* pt){
        uint16_t i;
        
        /* single test */
-       printitem("key", key, (nessie_bc_ctx.keysize_b+7)/8);
+       nessie_print_item("key", key, (nessie_bc_ctx.keysize_b+7)/8);
        nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
+       
        memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
-       printitem("plain", buffer, nessie_bc_ctx.blocksize_B);
+       nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B);
        nessie_bc_ctx.cipher_enc(buffer, ctx);
-       printitem("cipher", buffer, nessie_bc_ctx.blocksize_B);
-       nessie_bc_ctx.cipher_dec(buffer, ctx);
-       printitem("decrypted", buffer, nessie_bc_ctx.blocksize_B);
-       
+       nessie_print_item("cipher", buffer, nessie_bc_ctx.blocksize_B);
+       if(nessie_bc_ctx.cipher_dec){
+               nessie_bc_ctx.cipher_dec(buffer, ctx);
+               nessie_print_item("decrypted", buffer, nessie_bc_ctx.blocksize_B);
+       }
        /* 100 times test */
        memcpy(buffer, pt, nessie_bc_ctx.blocksize_B);
        for(i=0; i<100; ++i){
                nessie_bc_ctx.cipher_enc(buffer, ctx);
        }
-       printitem("Iterated 100 times", buffer, nessie_bc_ctx.blocksize_B);
+       nessie_print_item("Iterated 100 times", buffer, nessie_bc_ctx.blocksize_B);
 #ifndef NESSIE_NO1KTEST        
        /* 1000 times test, we use the 100 precedig steps to fasten things a bit */
        for(; i<1000; ++i){
                nessie_bc_ctx.cipher_enc(buffer, ctx);
        }
-       printitem("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
+       nessie_print_item("Iterated 1000 times", buffer, nessie_bc_ctx.blocksize_B);
 #endif
+       nessie_bc_free(ctx);
 }
 
 void nessie_bc_dec(uint8_t* key, uint8_t* ct){
@@ -101,82 +80,15 @@ void nessie_bc_dec(uint8_t* key, uint8_t* ct){
        uint8_t buffer[nessie_bc_ctx.blocksize_B];
        
        /* single test */
-       printitem("key", key, (nessie_bc_ctx.keysize_b+7)/8);
+       nessie_print_item("key", key, (nessie_bc_ctx.keysize_b+7)/8);
        nessie_bc_ctx.cipher_genctx(key, nessie_bc_ctx.keysize_b, ctx);
        memcpy(buffer, ct, nessie_bc_ctx.blocksize_B);
-       printitem("cipher", buffer, nessie_bc_ctx.blocksize_B);
+       nessie_print_item("cipher", buffer, nessie_bc_ctx.blocksize_B);
        nessie_bc_ctx.cipher_dec(buffer, ctx);
-       printitem("plain", buffer, nessie_bc_ctx.blocksize_B);
+       nessie_print_item("plain", buffer, nessie_bc_ctx.blocksize_B);
        nessie_bc_ctx.cipher_enc(buffer, ctx);
-       printitem("encrypted", buffer, nessie_bc_ctx.blocksize_B);
-       
-}
-
-static void print_set_vector(uint8_t set, uint16_t vector){
-       uart_putstr_P(PSTR("\r\n\r\nSet "));
-       uart_putc('0'+set%10);
-       uart_putstr_P(PSTR(", vector#"));
-       uart_putc((vector<100)?' ':'0'+vector/100);
-       uart_putc((vector<10 )?' ':'0'+(vector/10)%10);
-       uart_putc('0'+vector%10);
-       uart_putc(':');
-}
-
-/* example:
-Test vectors -- set 3
-=====================
- */ 
-static void print_setheader(uint8_t set){
-       uart_putstr_P(PSTR("\r\n\r\nTest vectors -- set "));
-       uart_putc('0'+set%10);
-       uart_putstr_P(PSTR("\r\n====================="));
-}
-
-/* example:
-********************************************************************************
-*Project NESSIE - New European Schemes for Signature, Integrity, and Encryption*
-********************************************************************************
-
-Primitive Name: Serpent
-=======================
-Key size: 256 bits
-Block size: 128 bits
-*/
-
-static void print_header(void){
-       uint16_t i;
-       uart_putstr_P(PSTR("\r\n\r\n"
-       "********************************************************************************\r\n"
-       "* micro-cryt - crypto primitives for microcontrolles by Daniel Otte            *\r\n"
-       "********************************************************************************\r\n"
-       "\r\n"));
-       uart_putstr_P(PSTR("Primitive Name: "));
-       uart_putstr(nessie_bc_ctx.name);
-       uart_putstr_P(PSTR("\r\n"));
-       for(i=0; i<16+strlen(nessie_bc_ctx.name); ++i){
-               uart_putc('=');
-       }
-       uart_putstr_P(PSTR("\r\nKey size: "));
-       if(nessie_bc_ctx.keysize_b>100){
-               uart_putc('0'+nessie_bc_ctx.keysize_b/100);
-       }
-       if(nessie_bc_ctx.keysize_b>10){
-               uart_putc('0'+(nessie_bc_ctx.keysize_b/10)%10);
-       }
-       uart_putc('0'+nessie_bc_ctx.keysize_b%10);
-       uart_putstr_P(PSTR(" bits\r\nBlock size: "));
-       if(nessie_bc_ctx.blocksize_B*8>100){
-               uart_putc('0'+(nessie_bc_ctx.blocksize_B*8)/100);
-       }
-       if(nessie_bc_ctx.blocksize_B*8>10){
-               uart_putc('0'+((nessie_bc_ctx.blocksize_B*8)/10)%10);
-       }
-       uart_putc('0'+(nessie_bc_ctx.blocksize_B*8)%10);
-       uart_putstr_P(PSTR(" bits"));
-}
-
-static void print_footer(void){
-       uart_putstr_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n"));
+       nessie_print_item("encrypted", buffer, nessie_bc_ctx.blocksize_B);
+       nessie_bc_free(ctx);
 }
 
 void nessie_bc_run(void){
@@ -185,12 +97,13 @@ void nessie_bc_run(void){
        uint8_t key[(nessie_bc_ctx.keysize_b+7)/8];
        uint8_t buffer[nessie_bc_ctx.blocksize_B];
        
-       print_header();
+       nessie_print_header(nessie_bc_ctx.name, nessie_bc_ctx.keysize_b,
+                           nessie_bc_ctx.blocksize_B*8, 0, 0, 0);
        /* test set 1 */
        set=1;
-       print_setheader(set);
+       nessie_print_setheader(set);
        for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
-               print_set_vector(set, i);
+               nessie_print_set_vector(set, i);
                memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
                key[i/8] |= 0x80>>(i%8);
                memset(buffer, 0, nessie_bc_ctx.blocksize_B);
@@ -198,9 +111,9 @@ void nessie_bc_run(void){
        }
        /* test set 2 */
        set=2;
-       print_setheader(set);
+       nessie_print_setheader(set);
        for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
-               print_set_vector(set, i);
+               nessie_print_set_vector(set, i);
                memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
                memset(buffer, 0, nessie_bc_ctx.blocksize_B);
                buffer[i/8] |= 0x80>>(i%8);
@@ -208,18 +121,18 @@ void nessie_bc_run(void){
        }
        /* test set 3 */
        set=3;
-       print_setheader(set);
+       nessie_print_setheader(set);
        for(i=0; i<256; ++i){
-               print_set_vector(set, i);
+               nessie_print_set_vector(set, i);
                memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
                memset(buffer, i, nessie_bc_ctx.blocksize_B);
                nessie_bc_enc(key, buffer);
        }
        /* test set 4 */
        set=4;
-       print_setheader(set);
+       nessie_print_setheader(set);
        /* 4 - 0*/
-       print_set_vector(set, 0);
+       nessie_print_set_vector(set, 0);
        for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
                key[i]=i;
        }
@@ -228,7 +141,7 @@ void nessie_bc_run(void){
        }
        nessie_bc_enc(key, buffer);
        /* 4 - 1 */
-       print_set_vector(set, 1);       
+       nessie_print_set_vector(set, 1);        
     /* This is the test vectors in Kasumi */
     static uint8_t kasumi_key[] = {
            0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
@@ -243,11 +156,13 @@ void nessie_bc_run(void){
        }
        nessie_bc_enc(key, buffer);
        /* half done ;-) */
+       if(nessie_bc_ctx.cipher_dec==NULL)
+               return;
        /* test set 5 */
        set=5;
-       print_setheader(set);
+       nessie_print_setheader(set);
        for(i=0; i<nessie_bc_ctx.keysize_b; ++i){
-               print_set_vector(set, i);
+               nessie_print_set_vector(set, i);
                memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
                key[i/8] |= 0x80>>(i%8);
                memset(buffer, 0, nessie_bc_ctx.blocksize_B);
@@ -255,9 +170,9 @@ void nessie_bc_run(void){
        }
        /* test set 6 */
        set=6;
-       print_setheader(set);
+       nessie_print_setheader(set);
        for(i=0; i<nessie_bc_ctx.blocksize_B*8; ++i){
-               print_set_vector(set, i);
+               nessie_print_set_vector(set, i);
                memset(key, 0, (nessie_bc_ctx.keysize_b+7)/8);
                memset(buffer, 0, nessie_bc_ctx.blocksize_B);
                buffer[i/8] |= 0x80>>(i%8);
@@ -265,18 +180,18 @@ void nessie_bc_run(void){
        }
        /* test set 7 */
        set=7;
-       print_setheader(set);
+       nessie_print_setheader(set);
        for(i=0; i<256; ++i){
-               print_set_vector(set, i);
+               nessie_print_set_vector(set, i);
                memset(key, i, (nessie_bc_ctx.keysize_b+7)/8);
                memset(buffer, i, nessie_bc_ctx.blocksize_B);
                nessie_bc_dec(key, buffer);
        }
        /* test set 8 */
        set=8;
-       print_setheader(set);
+       nessie_print_setheader(set);
        /* 8 - 0*/
-       print_set_vector(set, 0);
+       nessie_print_set_vector(set, 0);
        for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
                key[i]=i;
        }
@@ -285,7 +200,7 @@ void nessie_bc_run(void){
        }
        nessie_bc_dec(key, buffer);
        /* 8 - 1 */
-       print_set_vector(set, 1);       
+       nessie_print_set_vector(set, 1);        
        for(i=0; i<(nessie_bc_ctx.keysize_b+7)/8; ++i){
                key[i]=kasumi_key[i%sizeof(kasumi_key)];
        }
@@ -293,5 +208,5 @@ void nessie_bc_run(void){
                buffer[i]=kasumi_plain[i%sizeof(kasumi_plain)];
        }
        nessie_bc_dec(key, buffer);
-       print_footer();
+       nessie_print_footer();
 }