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