]> git.cryptolib.org Git - avr-crypto-lib.git/blob - test_src/main-skipjack-test.c
new skipjack test
[avr-crypto-lib.git] / test_src / main-skipjack-test.c
1 /* main-skipjack-test.c */
2 /*
3     This file is part of the AVR-Crypto-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  * skipjack test-suit
21  * 
22 */
23
24 #include "config.h"
25
26 #include "uart_i.h"
27 #include "debug.h"
28
29 #include "skipjack.h"
30 #include "nessie_bc_test.h"
31 #include "cli.h"
32 #include "performance_test.h"
33 #include "bcal-performance.h"
34 #include "bcal-nessie.h"
35 #include "bcal_skipjack.h"
36
37 #include <stdint.h>
38 #include <string.h>
39 #include <stdlib.h>
40
41
42 char* algo_name = "Skipjack";
43
44 const bcdesc_t* const algolist[] PROGMEM = {
45         (bcdesc_t*)&skipjack_desc,
46         NULL
47 };
48 /*****************************************************************************
49  *  additional validation-functions                                                                                      *
50  *****************************************************************************/
51 void skipjack_genctx_dummy(uint8_t* key, uint16_t keysize, void* ctx){
52         memcpy(ctx, key, 10);
53 }
54
55 void testrun_nessie_skipjack(void){
56         bcal_nessie_multiple(algolist);
57 }
58
59
60 void testrun_performance_skipjack(void){
61         bcal_performance_multiple(algolist);
62 }
63
64 int test_enc(const void* buffer, void* key){
65         uint8_t data[8];
66         int r;
67         memcpy(data, buffer, 8);
68         skipjack_enc(data, key);
69         cli_putstr_P(PSTR("  key = "));
70         cli_hexdump(key, 10);
71         cli_putstr_P(PSTR(" plaintext = "));
72         cli_hexdump(buffer, 8);
73         cli_putstr_P(PSTR(" ciphertext = "));
74         cli_hexdump(data, 8);
75         skipjack_dec(data, key);
76         r = memcmp(data, buffer, 8);
77         cli_putstr_P(PSTR(" decrypt: "));
78         if(r){
79                 cli_putstr_P(PSTR("fail"));
80         }else{
81                 cli_putstr_P(PSTR("ok"));
82         }
83         return r;
84 }
85
86 void testrun_nist_vectors(void){
87         uint8_t key[10];
88         uint8_t data[8];
89         uint8_t i;
90
91         cli_putstr_P(PSTR("\r\n\r\n=== NIST vectors run 1 ==="));
92         memset(key, 0, 10);
93         for(i=0; i<64; ++i){
94                 memset(data, 0, 8);
95                 data[i>>3] |= 0x80 >> (i & 7);
96                 cli_putstr_P(PSTR("\r\n round: 0x"));
97                 cli_hexdump_byte(i);
98                 test_enc(data, key);
99         }
100
101         cli_putstr_P(PSTR("\r\n\r\n=== NIST vectors run 2 ==="));
102         memset(data, 0, 8);
103         for(i=0; i<80; ++i){
104                 memset(key, 0, 10);
105                 key[i>>3] |= 0x80 >> (i & 7);
106                 cli_putstr_P(PSTR("\r\n round: 0x"));
107                 cli_hexdump_byte(i);
108                 test_enc(data, key);
109         }
110 }
111
112 /*****************************************************************************
113  *  self tests                                                                                                                           *
114  *****************************************************************************/
115
116 void testencrypt(uint8_t* block, uint8_t* key){
117         cli_putstr("\r\n==testy-encrypt==\r\n key: ");
118         cli_hexdump(key,10);
119         cli_putstr("\r\n plain: ");
120         cli_hexdump(block,8);
121         skipjack_enc(block,key);
122         cli_putstr("\r\n crypt: ");
123         cli_hexdump(block,8);
124 }
125
126 void testdecrypt(uint8_t* block, uint8_t* key){
127         cli_putstr("\r\n==testy-decrypt==\r\n key: ");
128         cli_hexdump(key,10);
129         cli_putstr("\r\n crypt: ");
130         cli_hexdump(block,8);
131         skipjack_dec(block,key);
132         cli_putstr("\r\n plain: ");
133         cli_hexdump(block,8);
134 }
135
136 void testrun_skipjack(void){
137         uint8_t key[2][10]={
138                              { 0x00, 0x99, 0x88, 0x77, 0x66,
139                                0x55, 0x44, 0x33, 0x22, 0x11 },
140                              { 0x11, 0x22, 0x33, 0x44, 0x55,
141                                0x66, 0x77, 0x88, 0x99, 0x00 }
142                                            };   
143                          
144         uint8_t data[2][8]={
145                                  { 0x33, 0x22, 0x11, 0x00, 
146                                    0xdd, 0xcc, 0xbb, 0xaa },
147                                  { 0xaa, 0xbb, 0xcc, 0xdd,
148                                    0x00, 0x11, 0x22, 0x33 }
149                                            };
150         testencrypt(data[0],key[0]);
151         testdecrypt(data[0],key[0]);
152         testencrypt(data[1],key[1]);
153         testdecrypt(data[1],key[1]);    
154 }
155
156
157
158 /*****************************************************************************
159  *  main                                                                                                                                         *
160  *****************************************************************************/
161
162 const char nessie_str[]      PROGMEM = "nessie";
163 const char test_str[]        PROGMEM = "test";
164 const char nist_str[]        PROGMEM = "nist";
165 const char performance_str[] PROGMEM = "performance";
166 const char echo_str[]        PROGMEM = "echo";
167
168 const cmdlist_entry_t cmdlist[] PROGMEM = {
169         { nessie_str,      NULL, testrun_nessie_skipjack},
170         { test_str,        NULL, testrun_skipjack},
171         { nist_str,        NULL, testrun_nist_vectors},
172         { performance_str, NULL, testrun_performance_skipjack},
173         { echo_str,    (void*)1, (void_fpt)echo_ctrl},
174         { NULL,            NULL, NULL}
175 };
176
177 int main (void){
178         DEBUG_INIT();
179         
180         cli_rx = (cli_rx_fpt)uart0_getc;
181         cli_tx = (cli_tx_fpt)uart0_putc;                
182         for(;;){
183                 cli_putstr_P(PSTR("\r\n\r\nCrypto-VS ("));
184                 cli_putstr(algo_name);
185                 cli_putstr_P(PSTR(")\r\nloaded and running\r\n"));
186                 cmd_interface(cmdlist);
187         }
188 }