]> git.cryptolib.org Git - avr-crypto-lib.git/blob - nessie_common.c
b5f64c84437437aab808dce27169f169ebd3ab84
[avr-crypto-lib.git] / nessie_common.c
1 /**
2  * 
3  * author: Daniel Otte
4  * email:  daniel.otte@rub.de
5  * license: GPLv3
6  * 
7  * common function for nessie-tests
8  * 
9  * */
10
11 #include <string.h>
12 #include <stdint.h>
13 #include <avr/pgmspace.h>
14 #include <stdlib.h> /* utoa() */
15 #include "uart.h"
16
17 void nessie_print_block(uint8_t* block, uint16_t blocksize_bit){
18         char tab [] = {'0', '1', '2', '3', 
19                                    '4', '5', '6', '7', 
20                                    '8', '9', 'A', 'B', 
21                                    'C', 'D', 'E', 'F'};
22         uint16_t i;
23         for(i=0; i<(blocksize_bit+7)/8; ++i){
24                 uart_putc(tab[(block[i])>>4]);
25                 uart_putc(tab[(block[i])&0xf]);
26         }                                  
27 }
28
29 #define SPACES 31
30 #define BYTESPERLINE 16
31
32 void nessie_print_item(char* name, uint8_t* buffer, uint16_t size_B){
33         uint8_t name_len;
34         uint8_t i;
35         name_len=strlen(name);
36         if(name_len>SPACES-1){
37                 uart_putstr_P(PSTR("\r\n!!! formatting error !!!\r\n"));
38                 return;
39         }
40         uart_putstr_P(PSTR("\r\n"));
41         for(i=0; i<SPACES-name_len-1; ++i){
42                 uart_putc(' ');
43         }
44         uart_putstr(name);
45         uart_putc('=');
46         /* now the data printing begins */
47         if(size_B<=BYTESPERLINE){
48                 /* one line seems sufficient */
49                 nessie_print_block(buffer, size_B*8);
50         } else {
51                 /* we need more lines */
52                 nessie_print_block(buffer, BYTESPERLINE*8); /* first line */
53                 int16_t toprint = size_B - BYTESPERLINE;
54                 buffer += BYTESPERLINE;
55                 while(toprint > 0){
56                         uart_putstr_P(PSTR("\r\n"));
57                         for(i=0; i<SPACES; ++i){
58                                 uart_putc(' ');
59                         }
60                         nessie_print_block(buffer, ((toprint>BYTESPERLINE)?BYTESPERLINE:toprint)*8);
61                         buffer  += BYTESPERLINE;
62                         toprint -= BYTESPERLINE;
63                 }
64         }
65
66
67
68 void nessie_print_set_vector(uint8_t set, uint16_t vector){
69         uart_putstr_P(PSTR("\r\n\r\nSet "));
70         uart_putc('0'+set%10);
71         uart_putstr_P(PSTR(", vector#"));
72         uart_putc((vector<100)?' ':'0'+vector/100);
73         uart_putc((vector<10 )?' ':'0'+(vector/10)%10);
74         uart_putc('0'+vector%10);
75         uart_putc(':');
76 }
77
78 /* example:
79 Test vectors -- set 3
80 =====================
81  */ 
82 void nessie_print_setheader(uint8_t set){
83         uart_putstr_P(PSTR("\r\n\r\nTest vectors -- set "));
84         uart_putc('0'+set%10);
85         uart_putstr_P(PSTR("\r\n====================="));
86 }
87
88 /* example:
89 ********************************************************************************
90 *Project NESSIE - New European Schemes for Signature, Integrity, and Encryption*
91 ********************************************************************************
92
93 Primitive Name: Serpent
94 =======================
95 Key size: 256 bits
96 Block size: 128 bits
97 */
98
99 void nessie_print_header(char* name,
100                          uint16_t keysize_b, 
101                          uint16_t blocksize_b,
102                          uint16_t hashsize_b, 
103                          uint16_t macsize_b,
104                          uint16_t ivsize_b ){
105         uint16_t i;
106         uart_putstr_P(PSTR("\r\n\r\n"
107         "********************************************************************************\r\n"
108         "* micro-crypt - crypto primitives for microcontrolles by Daniel Otte           *\r\n"
109         "********************************************************************************\r\n"
110         "\r\n"));
111         uart_putstr_P(PSTR("Primitive Name: "));
112         uart_putstr(name);
113         uart_putstr_P(PSTR("\r\n"));
114         /* underline */ 
115         for(i=0; i<16+strlen(name); ++i){
116                 uart_putc('=');
117         }
118         char str[6]; /* must catch numbers up to 65535 + terminatin \0 */
119         if(keysize_b){
120                 uart_putstr_P(PSTR("\r\nKey size: "));
121                 utoa(keysize_b, str, 10);
122                 uart_putstr(str);
123                 uart_putstr_P(PSTR(" bits"));
124         }
125         if(blocksize_b){
126                 uart_putstr_P(PSTR("\r\nBlock size: "));
127                 utoa(blocksize_b, str, 10);
128                 uart_putstr(str);
129                 uart_putstr_P(PSTR(" bits"));
130         }
131         if(hashsize_b){
132                 uart_putstr_P(PSTR("\r\nHash size: "));
133                 utoa(hashsize_b, str, 10);
134                 uart_putstr(str);
135                 uart_putstr_P(PSTR(" bits"));
136         }
137         if(macsize_b){
138                 uart_putstr_P(PSTR("\r\nMac size: "));
139                 utoa(macsize_b, str, 10);
140                 uart_putstr(str);
141                 uart_putstr_P(PSTR(" bits"));
142         }
143         if(ivsize_b){
144                 if(ivsize_b==(uint16_t)-1){
145                         uart_putstr_P(PSTR("\r\nNo initial value (IV) mode"));
146                 }
147                 {
148                         uart_putstr_P(PSTR("\r\nIV size: "));
149                         utoa(ivsize_b, str, 10);
150                         uart_putstr(str);
151                         uart_putstr_P(PSTR(" bits"));
152                 }
153         }
154         uart_putstr_P(PSTR("\r\n"));
155 }
156
157 void nessie_print_footer(void){
158         uart_putstr_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n"));
159 }
160