]> git.cryptolib.org Git - avr-crypto-lib.git/blob - nessie_common.c
insereated GPLv3 stub
[avr-crypto-lib.git] / nessie_common.c
1 /* nessie_common.c */
2 /*
3     This file is part of the Crypto-avr-lib/microcrypt-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<100)?' ':'0'+vector/100);
91         uart_putc((vector<10 )?' ':'0'+(vector/10)%10);
92         uart_putc('0'+vector%10);
93         uart_putc(':');
94 }
95
96 /* example:
97 Test vectors -- set 3
98 =====================
99  */ 
100 void nessie_print_setheader(uint8_t set){
101         uart_putstr_P(PSTR("\r\n\r\nTest vectors -- set "));
102         uart_putc('0'+set%10);
103         uart_putstr_P(PSTR("\r\n====================="));
104 }
105
106 /* example:
107 ********************************************************************************
108 *Project NESSIE - New European Schemes for Signature, Integrity, and Encryption*
109 ********************************************************************************
110
111 Primitive Name: Serpent
112 =======================
113 Key size: 256 bits
114 Block size: 128 bits
115 */
116
117 void nessie_print_header(char* name,
118                          uint16_t keysize_b, 
119                          uint16_t blocksize_b,
120                          uint16_t hashsize_b, 
121                          uint16_t macsize_b,
122                          uint16_t ivsize_b ){
123         uint16_t i;
124         uart_putstr_P(PSTR("\r\n\r\n"
125         "********************************************************************************\r\n"
126         "* micro-crypt - crypto primitives for microcontrolles by Daniel Otte           *\r\n"
127         "********************************************************************************\r\n"
128         "\r\n"));
129         uart_putstr_P(PSTR("Primitive Name: "));
130         uart_putstr(name);
131         uart_putstr_P(PSTR("\r\n"));
132         /* underline */ 
133         for(i=0; i<16+strlen(name); ++i){
134                 uart_putc('=');
135         }
136         char str[6]; /* must catch numbers up to 65535 + terminatin \0 */
137         if(keysize_b){
138                 uart_putstr_P(PSTR("\r\nKey size: "));
139                 utoa(keysize_b, str, 10);
140                 uart_putstr(str);
141                 uart_putstr_P(PSTR(" bits"));
142         }
143         if(blocksize_b){
144                 uart_putstr_P(PSTR("\r\nBlock size: "));
145                 utoa(blocksize_b, str, 10);
146                 uart_putstr(str);
147                 uart_putstr_P(PSTR(" bits"));
148         }
149         if(hashsize_b){
150                 uart_putstr_P(PSTR("\r\nHash size: "));
151                 utoa(hashsize_b, str, 10);
152                 uart_putstr(str);
153                 uart_putstr_P(PSTR(" bits"));
154         }
155         if(macsize_b){
156                 uart_putstr_P(PSTR("\r\nMac size: "));
157                 utoa(macsize_b, str, 10);
158                 uart_putstr(str);
159                 uart_putstr_P(PSTR(" bits"));
160         }
161         if(ivsize_b){
162                 if(ivsize_b==(uint16_t)-1){
163                         uart_putstr_P(PSTR("\r\nNo initial value (IV) mode"));
164                 }
165                 {
166                         uart_putstr_P(PSTR("\r\nIV size: "));
167                         utoa(ivsize_b, str, 10);
168                         uart_putstr(str);
169                         uart_putstr_P(PSTR(" bits"));
170                 }
171         }
172         uart_putstr_P(PSTR("\r\n"));
173 }
174
175 void nessie_print_footer(void){
176         uart_putstr_P(PSTR("\r\n\r\n\r\n\r\nEnd of test vectors\r\n\r\n"));
177 }
178