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