]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main-seed-test.c
1dc4259b8be8daa6b280aa936771aac1f8dde9d3
[avr-crypto-lib.git] / main-seed-test.c
1 /* main-seed-test.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  * \file        main-seed-test.c
21  * \author      Daniel Otte
22  * \email       daniel.otte@rub.de
23  * \date        2007-06-01
24  * \brief       test suit for SEED
25  * \par License 
26  * GPLv3 or later
27  * 
28  */
29 #include "config.h"
30 #include "serial-tools.h"
31 #include "uart.h"
32 #include "debug.h"
33
34 #include "seed.h"
35 #include "nessie_bc_test.h"
36 #include "cli.h"
37 #include "performance_test.h"
38
39 #include <stdint.h>
40 #include <string.h>
41 #include <stdlib.h>
42
43 char* cipher_name = "Seed";
44
45 /*****************************************************************************
46  *  additional validation-functions                                          *
47  *****************************************************************************/
48 void seed_genctx_dummy(uint8_t* key, uint16_t keysize, void* ctx){
49         seed_init(key, ctx);
50 }
51
52 void testrun_nessie_seed(void){
53         nessie_bc_ctx.blocksize_B =  16;
54         nessie_bc_ctx.keysize_b   = 128;
55         nessie_bc_ctx.name        = cipher_name;
56         nessie_bc_ctx.ctx_size_B  = sizeof(seed_ctx_t);
57         nessie_bc_ctx.cipher_enc  = (nessie_bc_enc_fpt)seed_encrypt;
58         nessie_bc_ctx.cipher_dec  = (nessie_bc_dec_fpt)seed_decrypt;
59         nessie_bc_ctx.cipher_genctx  = (nessie_bc_gen_fpt)seed_genctx_dummy;
60         
61         nessie_bc_run();
62         
63 }
64
65
66 void testrun_performance_seed(void){
67         uint16_t i,c;
68         uint64_t t;
69         char str[16];
70         uint8_t key[16], data[16];
71         seed_ctx_t ctx;
72         
73         calibrateTimer();
74         getOverhead(&c, &i);
75         uart_putstr_P(PSTR("\r\n\r\n=== benchmark ==="));
76         utoa(c, str, 10);
77         uart_putstr_P(PSTR("\r\n\tconst overhead:     "));
78         uart_putstr(str);
79         utoa(i, str, 10);
80         uart_putstr_P(PSTR("\r\n\tinterrupt overhead: "));
81         uart_putstr(str);
82         
83         memset(key,  0, 16);
84         memset(data, 0, 16);
85         
86         startTimer(1);
87         seed_init(key, &ctx);
88         t = stopTimer();
89         uart_putstr_P(PSTR("\r\n\tctx-gen time: "));
90         ultoa((unsigned long)t, str, 10);
91         uart_putstr(str);
92         
93         
94         startTimer(1);
95         seed_encrypt(data, &ctx);
96         t = stopTimer();
97         uart_putstr_P(PSTR("\r\n\tencrypt time: "));
98         ultoa((unsigned long)t, str, 10);
99         uart_putstr(str);
100         
101         
102         startTimer(1);
103         seed_decrypt(data, &ctx);
104         t = stopTimer();
105         uart_putstr_P(PSTR("\r\n\tdecrypt time: "));
106         ultoa((unsigned long)t, str, 10);
107         uart_putstr(str);
108         
109         uart_putstr_P(PSTR("\r\n"));
110 }
111
112 /*****************************************************************************
113  *  self tests                                                               *
114  *****************************************************************************/
115
116 void testencrypt(uint8_t* block, uint8_t* key){
117         seed_ctx_t ctx;
118         uart_putstr("\r\n==testy-encrypt==\r\n key: ");
119         uart_hexdump(key,16);
120         seed_init(key, &ctx);
121         uart_putstr("\r\n plain: ");
122         uart_hexdump(block,16);
123         seed_encrypt(block, &ctx);
124         uart_putstr("\r\n crypt: ");
125         uart_hexdump(block,16);
126 }
127
128 void testdecrypt(uint8_t* block, uint8_t* key){
129         seed_ctx_t ctx;
130         uart_putstr("\r\n==testy-decrypt==\r\n key: ");
131         uart_hexdump(key,16);
132         seed_init(key, &ctx);
133         uart_putstr("\r\n crypt: ");
134         uart_hexdump(block,16);
135         seed_decrypt(block, &ctx);
136         uart_putstr("\r\n plain: ");
137         uart_hexdump(block,16);
138 }
139
140 void testrun_seed(void){
141         uint8_t keys[4][16]=
142                 { {     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
143                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
144                   {     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
145                         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
146                   { 0x47, 0x06, 0x48, 0x08, 0x51, 0xE6, 0x1B, 0xE8,
147                         0x5D, 0x74, 0xBF, 0xB3, 0xFD, 0x95, 0x61, 0x85 },
148                   { 0x28, 0xDB, 0xC3, 0xBC, 0x49, 0xFF, 0xD8, 0x7D,
149                         0xCF, 0xA5, 0x09, 0xB1, 0x1D, 0x42, 0x2B, 0xE7,}
150                 };
151         uint8_t datas[4][16]=
152                 { {     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
153                         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f },
154                   {     0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
155                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
156                   { 0x83, 0xA2, 0xF8, 0xA2, 0x88, 0x64, 0x1F, 0xB9, 
157                         0xA4, 0xE9, 0xA5, 0xCC, 0x2F, 0x13, 0x1C, 0x7D },
158                   { 0xB4, 0x1E, 0x6B, 0xE2, 0xEB, 0xA8, 0x4A, 0x14, 
159                         0x8E, 0x2E, 0xED, 0x84, 0x59, 0x3C, 0x5E, 0xC7 }
160                 };
161         uint8_t i=0;
162         for(i=0; i<4; ++i){
163                 testencrypt(datas[i],keys[i]);
164                 testdecrypt(datas[i],keys[i]);  
165         }
166 }
167
168
169
170 /*****************************************************************************
171  *  main                                                                     *
172  *****************************************************************************/
173
174 int main (void){
175         char str[20];
176
177         DEBUG_INIT();
178
179         uart_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
180         uart_putstr(cipher_name);
181         uart_putstr_P(PSTR(")\r\nloaded and running\r\n"));
182
183         PGM_P    u   = PSTR("nessie\0test\0performance\0");
184         void_fpt v[] = {testrun_nessie_seed, testrun_seed, testrun_performance_seed};
185
186         while(1){ 
187                 if (!getnextwordn(str,20)){DEBUG_S("DBG: W1\r\n"); goto error;}
188                 if(execcommand_d0_P(str, u, v)<0){
189                         uart_putstr_P(PSTR("\r\nunknown command\r\n"));
190                 }
191                 continue;
192         error:
193                 uart_putstr("ERROR\r\n");
194         }
195         
196 }
197