]> git.cryptolib.org Git - avr-crypto-lib.git/blob - main.c
arcfour got its own testsuit now + some modifications of the build process
[avr-crypto-lib.git] / main.c
1 /*
2  * crypto-test
3  * 
4 */
5
6
7 #include "config.h"
8 #include "serial-tools.h"
9 #include "uart.h"
10 #include "debug.h"
11
12 #include "sha256.h"
13 #include "xtea.h"
14 #include "prng.h"
15 #include "cast5.h"
16
17 #include <stdint.h>
18 #include <string.h>
19
20
21 /*****************************************************************************
22  *  additional validation-functions                                                                                      *
23 *****************************************************************************/
24
25 void shavs_rnd(sha256_hash_t seed){
26         uint8_t md[4][SHA256_HASH_BITS/8], buffer[3*SHA256_HASH_BITS/8];
27         uint8_t j;
28         uint16_t i;
29         
30         for(j=0; j< 100; ++j){
31                 memcpy(md[0], seed, SHA256_HASH_BITS/8);
32                 memcpy(md[1], seed, SHA256_HASH_BITS/8);
33                 memcpy(md[2], seed, SHA256_HASH_BITS/8);
34                 for(i=3; i<1003; ++i){
35                         memcpy(buffer+0*(SHA256_HASH_BITS/8), md[(i-3)%4], SHA256_HASH_BITS/8);
36                         memcpy(buffer+1*(SHA256_HASH_BITS/8), md[(i-2)%4], SHA256_HASH_BITS/8);
37                         memcpy(buffer+2*(SHA256_HASH_BITS/8), md[(i-1)%4], SHA256_HASH_BITS/8);
38                         sha256(((void*)md[i%4]), buffer, 3*SHA256_HASH_BITS);
39                         uart_putc('.');
40                 }
41                 /* OUTPUT */
42                 --i;
43                 uart_putstr("\r\nMD = ");
44                 uart_hexdump(md[i%4], SHA256_HASH_BITS/8);
45                 uart_putstr("\r\n");
46                 memcpy(seed, (md[i%4]), SHA256_HASH_BITS/8);
47         }
48 }
49
50 /*****************************************************************************
51  *  self tests                                                                                                                           *
52 *****************************************************************************/
53 void testrun_sha256(void){
54         uint8_t block[SHA256_BLOCK_BITS/8];
55         
56         uart_putstr("\r\nsha256(\"\", 0)= ");
57         sha256((void*)block, (void*)"\x00", 0);
58         uart_hexdump(block, SHA256_HASH_BITS/8);
59         
60
61         uart_putstr("\r\nsha256(0x80, 8)= ");
62         sha256((void*)block, (void*)"\x80", 8);
63         uart_hexdump(block, SHA256_HASH_BITS/8);
64
65         uart_putstr("\r\nsha256(0x02, 8)= ");
66         sha256((void*)block, (void*)"\x02", 8);
67         uart_hexdump(block, SHA256_HASH_BITS/8);
68
69
70         uart_putstr("\r\nsha256(\"abc\", 24)= ");
71         sha256((void*)block, (void*)"abc", 24);
72         uart_hexdump(block, SHA256_HASH_BITS/8);
73         
74         uart_putstr("\r\nsha256(\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\", 24)= ");
75         sha256((void*)block, (void*) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 448);
76         uart_hexdump(block, SHA256_HASH_BITS/8);
77         
78         uart_putstr("\r\nsha256(1,000,000 x 'a')= ");
79         {
80                 uint16_t i;
81                 sha256_ctx_t s;
82                 sha256_init(&s);
83                 memset(block,'a',SHA256_BLOCK_BITS/8);
84                 for(i=0; i<(1000000/(SHA256_BLOCK_BITS/8)); ++i){ /* 15625 times*/
85                         sha256_nextBlock(&s, block);
86                 } 
87                 sha256_lastBlock(&s, block, 0);
88                 sha256_ctx2hash((void*)block, &s);
89         }
90         uart_hexdump(block, SHA256_HASH_BITS/8);
91 }
92
93 void testrun_xtea(void){
94         uint8_t block[8], block2[8];
95         uint8_t key [16];
96         
97         memcpy (block, "abcdefgh", 8);
98         memset (key, 0, 16);
99         memset (block2, 0, 8);
100         uart_putstr("\r\nxtea_enc(\"abcdefgh\", 0)= ");
101         xtea_enc((void*)block2, (void*)block, (void*)key);
102         uart_hexdump(block2, 8);
103         uart_putstr("\r\nxtea_dec(form above)= ");
104         xtea_dec((void*)block, (void*)block2, (void*)key);
105         uart_hexdump(block, 8);
106
107         memcpy (key, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16);
108         uart_putstr("\r\nxtea_enc(\"abcdefgh\", 000102030405060708090a0b0c0d0e0f)= ");
109         xtea_enc((void*)block, (void*)block, (void*)key);
110         uart_hexdump(block, 8);
111         uart_putstr("\r\nxtea_dec(form above)= ");
112         xtea_dec((void*)block, (void*)block, (void*)key);
113         uart_hexdump(block, 8); 
114 }
115
116 #if 0
117
118 void testrun_arcfour(void){
119         arcfour_ctx_t s;
120         char *b;
121         /* using wikipedia test-vectors:
122          *      RC4( "Key", "Plaintext" ) == "bbf316e8 d940af0a d3"
123          *      RC4( "Wiki", "pedia" ) == "1021bf0420"
124          *      RC4( "Secret", "Attack at dawn" ) == "45a01f64 5fc35b38 3552544b 9bf5"
125          **/
126         uart_putstr("\r\narcfour(\"Plaintext\", \"Key\")=");
127         arcfour_init(&s, (uint8_t*)"Key", 3);
128         b="Plaintext";
129         while (*b)
130                 *b++ ^= arcfour_gen(&s);
131         uart_hexdump(b-9, 9);
132         
133         uart_putstr("\r\narcfour(\"pedia\", \"Wiki\")=");
134         arcfour_init(&s, (uint8_t*)"Wiki", 4);
135         b="pedia";
136         while (*b)
137                 *b++ ^= arcfour_gen(&s);
138         uart_hexdump(b-5, 5);
139         
140         uart_putstr("\r\narcfour(\"Attack at dawn\", \"Secret\")=");
141         arcfour_init(&s, (uint8_t*)"Secret", 6);
142         b="Attack at dawn";
143         while (*b)
144                 *b++ ^= arcfour_gen(&s);
145         uart_hexdump(b-14, 14);
146         
147         uart_putstr("\r\narcfour(00.00.00.00.00.00.00.00, 01.23.45.67.89.AB.CD.EF)=");
148         arcfour_init(&s, (uint8_t*)"\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8);
149         int i=0;
150         uint8_t a[8];
151         memset(a, 0 , 8);
152         while (i < 8)
153                 a[i++] ^= arcfour_gen(&s);
154         uart_hexdump(a, 8);     
155 }
156
157 #endif
158
159 void testrun_prng(void){
160         uint8_t i,block[32];
161         uart_putstr("\r\naddEntropy(32, 0x00000000)");
162         addEntropy(32,"\x00\x00\x00\x00");
163         for(i=0;i<12;++i){
164                 getRandomBlock((void*)block);
165                 uart_putstr("\r\n");
166                 uart_hexdump(block, 32);
167         }
168 }
169
170 void testrun_cast5(void){
171         cast5_ctx_t s;
172         uint8_t i;
173         uart_putstr("\r\nCAST5:\r\nkey: 01 23 45 67 34 56 78 23 45 67 89 34 56 78 9A");
174         cast5_init(&s, (uint8_t*)"\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A", 128);
175         uint8_t block[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
176         uart_putstr("\r\nplaintext: ");
177         uart_hexdump(block, 8);
178         cast5_enc(&s, block);
179         uart_putstr("\r\nciphertext: ");
180         uart_hexdump(block, 8);
181         for(i=0; i<16; ++i){
182                 uart_putstr("\r\nK"); uart_putc('0'+(i+1)/10); uart_putc('0'+(i+1)%10); uart_putstr(": ");
183                 uart_hexdump(&(s.mask[i]),4);
184         }
185 }
186
187 /*****************************************************************************
188  *  main                                                                                                                                         *
189  *****************************************************************************/
190
191 int main (void){
192         uint64_t length=0;
193         sha256_ctx_t s;
194         char str[20];
195         int i;
196         uint8_t block[SHA256_BLOCK_BITS/8];
197         
198         DEBUG_INIT();
199         
200         sha256_init(&s);
201         uart_putstr("\r\n");
202
203         uart_putstr("\r\n\r\nCrypto-VS\r\nloaded and running\r\n");
204 restart:
205         while(1){ 
206                 if (!getnextwordn(str,20))  {DEBUG_S("DBG: W1\r\n"); goto error;}
207                 if (strcmp(str, "REQ")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
208                 if (!getnextwordn(str,20))  {DEBUG_S("DBG: W2\r\n"); goto error;}
209                 if (strcmp(str, "SHA256")) {
210                         if (strcmp(str, "test")){DEBUG_S("DBG: 1d\r\n"); goto error;};
211                         /* use some fixed test-vectors and all Algos */
212                                         uart_putstr("\r\n intergrated selftests:\r\n");
213                                 testrun_xtea();
214                                         uart_putstr("\r\n");
215                                 testrun_prng();
216                                         uart_putstr("\r\n");
217                                 testrun_cast5();
218                                         uart_putstr("\r\n");
219                 //              testrun_arcfour();
220                 //                      uart_putstr("\r\n");
221                                 testrun_sha256();
222                         goto restart;
223                 }
224                 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W4\r\n"); goto error;}
225                 if (strcmp(str, "Len=")) {
226         /* 1d9370cdccba99b23670e2e0d6514001006f50d3c7a453201d2776f03c5e58fd */
227         /*      f41ece26 13e45739 15696b5a dcd51ca3
228                 28be3bf5 66a9ca99 c9ceb027 9c1cb0a7
229          */
230                         if(strcmp(str, "rnd")){DEBUG_S("DBG: 2b\r\n"); goto error;}
231                         sha256_hash_t seed = {0x1d, 0x93, 0x70, 0xcd, 0xcc, 0xba, 0x99, 0xb2, 0x36, 0x70,
232                                                                   0xe2, 0xe0, 0xd6, 0x51, 0x40, 0x01, 0x00, 0x6f, 0x50, 0xd3,
233                                                           0xc7, 0xa4, 0x53, 0x20, 0x1d, 0x27, 0x76, 0xf0, 0x3c, 0x5e,
234                                                                   0x58, 0xfd  }; /*
235                                 { 0xf4, 0x1e, 0xce, 0x26,  0x13, 0xe4, 0x57, 0x39,  0x15, 0x69, 0x6b, 0x5a,  0xdc, 0xd5, 0x1c, 0xa3, 
236                                 0x28, 0xbe, 0x3b, 0xf5,  0x66, 0xa9, 0xca, 0x99,  0xc9, 0xce, 0xb0, 0x27,  0x9c, 0x1c, 0xb0, 0xa7 };
237                         //      */
238                         shavs_rnd(seed);
239                         goto restart;
240                         
241                         }
242                 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W5\r\n"); goto error;}
243                 {       
244                         length=0;
245                         i=0;
246                         while (str[i]){ /* we should check for error here */
247                                 length *= 10;
248                                 length += str[i++] - '0';
249                         }
250                 };
251 //              DEBUG_S("\r\nDBG: Length="); DEBUG_B(length&0xff); DEBUG_S("\r\n");
252 //              DEBUG_S("A");
253                 sha256_init(&s);
254 //              DEBUG_S("B");
255                 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W6\r\n"); goto error;}
256 //              DEBUG_S("b2");
257                 if (strcmp(str, "Msg=")) {DEBUG_S("DBG: 4b\r\n"); goto error;}  
258 //              DEBUG_S("b3");
259                 {
260                         memset(block, 0, SHA256_BLOCK_BITS/8);
261 //                      DEBUG_S("b3.0");
262                         while (length>=SHA256_BLOCK_BITS){
263                                 readhex2buffer(block, SHA256_BLOCK_BITS/8);
264 //                      DEBUG_S("b3.1");
265                                 sha256_nextBlock(&s, block); 
266 //                      DEBUG_S("b3.2");
267                                 length -= SHA256_BLOCK_BITS;
268                         }
269 //              DEBUG_S("C");   
270                         readhex2buffer(block, (length/8) + ((length&0x7)?1:0) + ((length)?0:1));
271 //              DEBUG_S("D");   
272                         sha256_lastBlock(&s, block, length);
273 //              DEBUG_S("E");   
274                         sha256_ctx2hash((void*)block, &s);
275                         uart_putstr("\n\rMD= ");
276                         uart_hexdump(block, SHA256_HASH_BITS/8);
277                         uart_putstr("\n\r\n\r");
278                 }               
279                 continue;
280         error:
281                 uart_putstr("ERROR\r\n");
282         } /* while (1) */
283 }
284