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