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