]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/nessie_common.c
fixing E-Mail-Address & Copyright
[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) 2006-2015 Daniel Otte (bg@nerilex.org)
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:  bg@nerilex.org
23  * license: GPLv3
24  * 
25  * common function for nessie-tests
26  * 
27  * */
28
29 #include <string.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <avr/pgmspace.h>
33 #include "nessie_common.h"
34 #include "hexdigit_tab.h"
35
36 #define nessie_out_file stdout
37
38 void nessie_set_output_stream(FILE *out_stream){
39     nessie_out_file = out_stream;
40 }
41
42
43 #ifdef NESSIE_ALIVE
44 void nessie_send_alive(void){
45         putc(NESSIE_ALIVE_CHAR, nessie_out_file);
46 }
47
48 void nessie_send_alive_a(uint16_t i){
49         if((i&31)==0)
50                 putc(NESSIE_ALIVE_CHAR, nessie_out_file);
51 }
52 #endif
53
54 void nessie_print_block(uint8_t *block, uint16_t blocksize_bit){
55         uint16_t i;
56         for(i=0; i<(blocksize_bit+7)/8; ++i){
57                 putc(pgm_read_byte(hexdigit_tab_uc_P + ((block[i]) >>   4)), nessie_out_file);
58                 putc(pgm_read_byte(hexdigit_tab_uc_P + ((block[i]) &  0xf)), nessie_out_file);
59         }                                  
60 }
61
62 #define SPACES 31
63 #define BYTESPERLINE 16
64
65 void nessie_print_item(const char *name, uint8_t *buffer, uint16_t size_B){
66         uint8_t name_len;
67         uint8_t i;
68         name_len = strlen(name);
69         if(name_len > SPACES - 1){
70                 fputs_P(PSTR("\n!!! formatting error !!!\n"), nessie_out_file);
71                 return;
72         }
73         putc('\n', nessie_out_file);
74         for(i = 0; i < SPACES-name_len - 1; ++i){
75                 putc(' ', nessie_out_file);
76         }
77         fputs(name, stdout);
78         putc('=', nessie_out_file);
79         /* now the data printing begins */
80         if(size_B <= BYTESPERLINE){
81                 /* one line seems sufficient */
82                 nessie_print_block(buffer, size_B * 8);
83         } else {
84                 /* we need more lines */
85                 nessie_print_block(buffer, BYTESPERLINE * 8); /* first line */
86                 int16_t toprint = size_B - BYTESPERLINE;
87                 buffer += BYTESPERLINE;
88                 while(toprint > 0){
89                         putc('\n', nessie_out_file);
90                         for(i = 0; i < SPACES; ++i){
91                                 putc(' ', nessie_out_file);
92                         }
93                         nessie_print_block(buffer, ((toprint > BYTESPERLINE) ? BYTESPERLINE : toprint) * 8);
94                         buffer  += BYTESPERLINE;
95                         toprint -= BYTESPERLINE;
96                 }
97         }
98
99
100
101 void nessie_print_set_vector(uint8_t set, uint16_t vector){
102         fprintf_P(nessie_out_file, PSTR("\n\nSet %"PRIu8", vector#%4"PRIu16":"), set, vector);
103 }
104
105 /* example:
106 Test vectors -- set 3
107 =====================
108  */ 
109 void nessie_print_setheader(uint8_t set){
110         fprintf_P(nessie_out_file, PSTR("\r\n\r\nTest vectors -- set %"PRIu8"\n====================="), set);
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         fputs_P(PSTR("\n\n"
132         "********************************************************************************\n"
133         "* AVR-Crypto-Lib - crypto primitives for AVR microcontrollers by Daniel Otte   *\n"
134         "********************************************************************************\n\n"),
135         nessie_out_file);
136         fprintf_P(nessie_out_file, PSTR("Primitive Name: %s\n"), name);
137         /* underline */ 
138         for(i=0; i<16+strlen(name); ++i){
139                 putc('=', nessie_out_file);
140         }
141         if(keysize_b){
142                 fprintf_P(nessie_out_file, PSTR("\nKey size: %"PRIu16" bits"), keysize_b);
143         }
144         if(blocksize_b){
145         fprintf_P(nessie_out_file, PSTR("\nBlock size: %"PRIu16" bits"), blocksize_b);
146         }
147         if(hashsize_b){
148             fprintf_P(nessie_out_file, PSTR("\nHash size: %"PRIu16" bits"), hashsize_b);
149
150         }
151         if(macsize_b){
152                 fprintf_P(nessie_out_file, PSTR("\nMac size: %"PRIu16" bits"), macsize_b);
153         }
154         if(ivsize_b){
155                 if(ivsize_b==(uint16_t)-1){
156                         fputs_P(PSTR("\nNo initial value (IV) mode"), stdout);
157                 }else{
158                     fprintf_P(nessie_out_file, PSTR("\nIV size: %"PRIu16" bits"), ivsize_b);
159                 }
160         }
161         putc('\n', nessie_out_file);
162 }
163
164 void nessie_print_footer(void){
165         puts_P(PSTR("\n\n\n\nEnd of test vectors\n\n"));
166 }
167