]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_common.c
renaming to AVR-Crypto-Lib
[avr-crypto-lib.git] / test_src / nessie_common.c
1 /* nessie_common.c */
2 /*
3     This file is part of the This file is part of the AVR-Crypto-Lib.
4     Copyright (C) 2008  Daniel Otte (daniel.otte@rub.de)
5
6     This program is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 /**
20  * 
21  * author: Daniel Otte
22  * email:  daniel.otte@rub.de
23  * license: GPLv3
24  * 
25  * common function for nessie-tests
26  * 
27  * */
28
29 #include <string.h>
30 #include <stdint.h>
31 #include <avr/pgmspace.h>
32 #include <stdlib.h> /* utoa() */
33 #include "uart.h"
34
35 void nessie_print_block(uint8_t* block, uint16_t blocksize_bit){
36         char tab [] = {'0', '1', '2', '3', 
37                                    '4', '5', '6', '7', 
38                                    '8', '9', 'A', 'B', 
39                                    'C', 'D', 'E', 'F'};
40         uint16_t i;
41         for(i=0; i<(blocksize_bit+7)/8; ++i){
42                 uart_putc(tab[(block[i])>>4]);
43                 uart_putc(tab[(block[i])&0xf]);
44         }                                  
45 }
46
47 #define SPACES 31
48 #define BYTESPERLINE 16
49
50 void nessie_print_item(char* name, uint8_t* buffer, uint16_t size_B){
51         uint8_t name_len;
52         uint8_t i;
53         name_len=strlen(name);
54         if(name_len>SPACES-1){
55                 uart_putstr_P(PSTR("\r\n!!! formatting error !!!\r\n"));
56                 return;
57         }
58         uart_putstr_P(PSTR("\r\n"));
59         for(i=0; i<SPACES-name_len-1; ++i){
60                 uart_putc(' ');
61         }
62         uart_putstr(name);
63         uart_putc('=');
64         /* now the data printing begins */
65         if(size_B<=BYTESPERLINE){
66                 /* one line seems sufficient */
67                 nessie_print_block(buffer, size_B*8);
68         } else {
69                 /* we need more lines */
70                 nessie_print_block(buffer, BYTESPERLINE*8); /* first line */
71                 int16_t toprint = size_B - BYTESPERLINE;
72                 buffer += BYTESPERLINE;
73                 while(toprint > 0){
74                         uart_putstr_P(PSTR("\r\n"));
75                         for(i=0; i<SPACES; ++i){
76                                 uart_putc(' ');
77                         }
78                         nessie_print_block(buffer, ((toprint>BYTESPERLINE)?BYTESPERLINE:toprint)*8);
79                         buffer  += BYTESPERLINE;
80                         toprint -= BYTESPERLINE;
81                 }
82         }
83
84
85
86 void nessie_print_set_vector(uint8_t set, uint16_t vector){
87         uart_putstr_P(PSTR("\r\n\r\nSet "));
88         uart_putc('0'+set%10);
89         uart_putstr_P(PSTR(", vector#"));
90         uart_putc((vector<1000)?' ':'0'+vector/1000);
91         uart_putc((vector<100)?' ':'0'+(vector/100)%10);
92         uart_putc((vector<10 )?' ':'0'+(vector/10)%10);
93         uart_putc('0'+vector%10);
94         uart_putc(':');
95 }
96
97 /* example:
98 Test vectors -- set 3
99 =====================
100  */ 
101 void nessie_print_setheader(uint8_t set){
102         uart_putstr_P(PSTR("\r\n\r\nTest vectors -- set "));
103         uart_putc('0'+set%10);
104         uart_putstr_P(PSTR("\r\n====================="));
105 }
106
107 /* example:
108 ********************************************************************************
109 *Project NESSIE - New European Schemes for Signature, Integrity, and Encryption*
110 ********************************************************************************
111
112 Primitive Name: Serpent
113 =======================
114 Key size: 256 bits
115 Block size: 128 bits
116 */
117
118 void nessie_print_header(char* name,
119                          uint16_t keysize_b, 
120                          uint16_t blocksize_b,
121                          uint16_t hashsize_b, 
122                          uint16_t macsize_b,
123                          uint16_t ivsize_b ){
124         uint16_t i;
125         uart_putstr_P(PSTR("\r\n\r\n"
126         "********************************************************************************\r\n"
127         "* micro-crypt - crypto primitives for microcontrolles by Daniel Otte           *\r\n"
128         "********************************************************************************\r\n"
129         "\r\n"));
130         uart_putstr_P(PSTR("Primitive Name: "));
131         uart_putstr(name);
132         uart_putstr_P(PSTR("\r\n"));
133         /* underline */ 
134         for(i=0; i<16+strlen(name); ++i){
135                 uart_putc('=');
136         }
137         char str[6]; /* must catch numbers up to 65535 + terminatin \0 */
138         if(keysize_b){
139                 uart_putstr_P(PSTR("\r\nKey size: "));
140                 utoa(keysize_b, str, 10);
141                 uart_putstr(str);
142                 uart_putstr_P(PSTR(" bits"));
143         }
144         if(blocksize_b){
145                 uart_putstr_P(PSTR("\r\nBlock size: "));
146                 utoa(blocksize_b, str, 10);
147                 uart_putstr(str);
148                 uart_putstr_P(PSTR(" bits"));
149         }
150         if(hashsize_b){
151                 uart_putstr_P(PSTR("\r\nHash size: "));
152                 utoa(hashsize_b, str, 10);
153                 uart_putstr(str);
154                 uart_putstr_P(PSTR(" bits"));
155         }
156         if(macsize_b){
157                 uart_putstr_P(PSTR("\r\nMac size: "));
158                 utoa(macsize_b, str, 10);
159                 uart_putstr(str);
160                 uart_putstr_P(PSTR(" bits"));
161         }
162         if(ivsize_b){
163                 if(ivsize_b==(uint16_t)-1){
164                         uart_putstr_P(PSTR("\r\nNo initial value (IV) mode"));
165                 }
166                 {
167                         uart_putstr_P(PSTR("\r\nIV size: "));
168                         utoa(ivsize_b, str, 10);
169                         uart_putstr(str);
170                         uart_putstr_P(PSTR(" bits"));
171                 }
172         }
173         uart_putstr_P(PSTR("\r\n"));
174 }
175
176 void nessie_print_footer(void){
177         uart_putstr_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n"));
178 }
179