]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_common.c
new hash function abstraction layer + shavs + dump util + ...
[avr-crypto-lib.git] / test_src / nessie_common.c
1 /* nessie_common.c */
2 /*
3     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 "nessie_common.h"
34
35
36 #ifdef NESSIE_ALIVE
37 void nessie_send_alive(void){
38         NESSIE_PUTC(NESSIE_ALIVE_CHAR);
39 }
40
41 void nessie_send_alive_a(uint16_t i){
42         if((i&63)==63)
43                 NESSIE_PUTC(NESSIE_ALIVE_CHAR);
44 }
45 #endif
46
47 void nessie_print_block(uint8_t* block, uint16_t blocksize_bit){
48         char tab [] = {'0', '1', '2', '3', 
49                                    '4', '5', '6', '7', 
50                                    '8', '9', 'A', 'B', 
51                                    'C', 'D', 'E', 'F'};
52         uint16_t i;
53         for(i=0; i<(blocksize_bit+7)/8; ++i){
54                 NESSIE_PUTC(tab[(block[i])>>4]);
55                 NESSIE_PUTC(tab[(block[i])&0xf]);
56         }                                  
57 }
58
59 #define SPACES 31
60 #define BYTESPERLINE 16
61
62 void nessie_print_item(char* name, uint8_t* buffer, uint16_t size_B){
63         uint8_t name_len;
64         uint8_t i;
65         name_len=strlen(name);
66         if(name_len>SPACES-1){
67                 NESSIE_PUTSTR_P(PSTR("\r\n!!! formatting error !!!\r\n"));
68                 return;
69         }
70         NESSIE_PUTSTR_P(PSTR("\r\n"));
71         for(i=0; i<SPACES-name_len-1; ++i){
72                 NESSIE_PUTC(' ');
73         }
74         NESSIE_PUTSTR(name);
75         NESSIE_PUTC('=');
76         /* now the data printing begins */
77         if(size_B<=BYTESPERLINE){
78                 /* one line seems sufficient */
79                 nessie_print_block(buffer, size_B*8);
80         } else {
81                 /* we need more lines */
82                 nessie_print_block(buffer, BYTESPERLINE*8); /* first line */
83                 int16_t toprint = size_B - BYTESPERLINE;
84                 buffer += BYTESPERLINE;
85                 while(toprint > 0){
86                         NESSIE_PUTSTR_P(PSTR("\r\n"));
87                         for(i=0; i<SPACES; ++i){
88                                 NESSIE_PUTC(' ');
89                         }
90                         nessie_print_block(buffer, ((toprint>BYTESPERLINE)?BYTESPERLINE:toprint)*8);
91                         buffer  += BYTESPERLINE;
92                         toprint -= BYTESPERLINE;
93                 }
94         }
95
96
97
98 void nessie_print_set_vector(uint8_t set, uint16_t vector){
99         NESSIE_PUTSTR_P(PSTR("\r\n\r\nSet "));
100         NESSIE_PUTC('0'+set%10);
101         NESSIE_PUTSTR_P(PSTR(", vector#"));
102         NESSIE_PUTC((vector<1000)?' ':'0'+vector/1000);
103         NESSIE_PUTC((vector<100)?' ':'0'+(vector/100)%10);
104         NESSIE_PUTC((vector<10 )?' ':'0'+(vector/10)%10);
105         NESSIE_PUTC('0'+vector%10);
106         NESSIE_PUTC(':');
107 }
108
109 /* example:
110 Test vectors -- set 3
111 =====================
112  */ 
113 void nessie_print_setheader(uint8_t set){
114         NESSIE_PUTSTR_P(PSTR("\r\n\r\nTest vectors -- set "));
115         NESSIE_PUTC('0'+set%10);
116         NESSIE_PUTSTR_P(PSTR("\r\n====================="));
117 }
118
119 /* example:
120 ********************************************************************************
121 *Project NESSIE - New European Schemes for Signature, Integrity, and Encryption*
122 ********************************************************************************
123
124 Primitive Name: Serpent
125 =======================
126 Key size: 256 bits
127 Block size: 128 bits
128 */
129
130 void nessie_print_header(char* name,
131                          uint16_t keysize_b, 
132                          uint16_t blocksize_b,
133                          uint16_t hashsize_b, 
134                          uint16_t macsize_b,
135                          uint16_t ivsize_b ){
136         uint16_t i;
137         NESSIE_PUTSTR_P(PSTR("\r\n\r\n"
138         "********************************************************************************\r\n"
139         "* micro-crypt - crypto primitives for microcontrolles by Daniel Otte           *\r\n"
140         "********************************************************************************\r\n"
141         "\r\n"));
142         NESSIE_PUTSTR_P(PSTR("Primitive Name: "));
143         NESSIE_PUTSTR(name);
144         NESSIE_PUTSTR_P(PSTR("\r\n"));
145         /* underline */ 
146         for(i=0; i<16+strlen(name); ++i){
147                 NESSIE_PUTC('=');
148         }
149         char str[6]; /* must catch numbers up to 65535 + terminatin \0 */
150         if(keysize_b){
151                 NESSIE_PUTSTR_P(PSTR("\r\nKey size: "));
152                 utoa(keysize_b, str, 10);
153                 NESSIE_PUTSTR(str);
154                 NESSIE_PUTSTR_P(PSTR(" bits"));
155         }
156         if(blocksize_b){
157                 NESSIE_PUTSTR_P(PSTR("\r\nBlock size: "));
158                 utoa(blocksize_b, str, 10);
159                 NESSIE_PUTSTR(str);
160                 NESSIE_PUTSTR_P(PSTR(" bits"));
161         }
162         if(hashsize_b){
163                 NESSIE_PUTSTR_P(PSTR("\r\nHash size: "));
164                 utoa(hashsize_b, str, 10);
165                 NESSIE_PUTSTR(str);
166                 NESSIE_PUTSTR_P(PSTR(" bits"));
167         }
168         if(macsize_b){
169                 NESSIE_PUTSTR_P(PSTR("\r\nMac size: "));
170                 utoa(macsize_b, str, 10);
171                 NESSIE_PUTSTR(str);
172                 NESSIE_PUTSTR_P(PSTR(" bits"));
173         }
174         if(ivsize_b){
175                 if(ivsize_b==(uint16_t)-1){
176                         NESSIE_PUTSTR_P(PSTR("\r\nNo initial value (IV) mode"));
177                 }
178                 {
179                         NESSIE_PUTSTR_P(PSTR("\r\nIV size: "));
180                         utoa(ivsize_b, str, 10);
181                         NESSIE_PUTSTR(str);
182                         NESSIE_PUTSTR_P(PSTR(" bits"));
183                 }
184         }
185         NESSIE_PUTSTR_P(PSTR("\r\n"));
186 }
187
188 void nessie_print_footer(void){
189         NESSIE_PUTSTR_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n"));
190 }
191