debug in trajectory manager
[aversive.git] / modules / crypto / test / main.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: main.c,v 1.3.4.2 2007-05-23 17:18:12 zer0 Exp $
19  *
20  */
21
22 #include <stdio.h>
23 #include <string.h>
24 #include <math.h>
25
26 #include <aversive/pgmspace.h>
27 #include <uart.h>
28 #include <aversive.h>
29 #include <aversive/wait.h>
30
31 #include "md5.h"
32 #include "aes_locl.h"
33 #include "aes.h"
34 #include "hmac_md5.h"
35
36 #include <rc4.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #define SKEY_BINKEY_SIZE        8
41
42     
43
44
45 /*test crypto*/
46 unsigned char text_in[] = "666Doyourecognizethisnumber?Thisisthenumberofthebeast4567890abcd";
47 unsigned char text_im[64];
48 unsigned char text_out[64];
49 unsigned char my_key[] = "ThisIsMySecret!!AndTheRestOfdata";
50
51
52 unsigned char test_hmac_md5_key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
53 unsigned char test_hmac_md5_data[] = "Hi There";
54
55 unsigned char test_aes_ctr_key[] = "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00";
56 unsigned char test_aes_counterblock[] = "\x00\x00\x00\x00\x00\x00\x00\x00";
57 unsigned char test_aes_text[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789aa";
58 unsigned long test_aes_Nonce = 0x30;
59
60
61    
62
63 /*
64  * ARC4 tests vectors from OpenSSL (crypto/rc4/rc4test.c)
65  */
66
67 void rc4_crypt(unsigned char *data, int length )
68 {
69   int i;
70
71   for (i=0 ; i<length ; i++)
72     data[i] = rc4_crypt_char(data[i]);
73 }
74
75
76 static unsigned char rc4_keys[7][30]={
77         {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
78         {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
79         {8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
80         {4,0xef,0x01,0x23,0x45},
81         {8,0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef},
82         {4,0xef,0x01,0x23,0x45},
83         };
84
85 static unsigned char rc4_data_len[7]={8,8,8,20,28,10};
86 static unsigned char rc4_data[7][30]={
87         {0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,0xff},
88         {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
89         {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
90         {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
91            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
92            0x00,0x00,0x00,0x00,0xff},
93         {0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
94            0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
95            0x12,0x34,0x56,0x78,0x9A,0xBC,0xDE,0xF0,
96            0x12,0x34,0x56,0x78,0xff},
97         {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xff},
98         {0},
99         };
100
101 static unsigned char rc4_output[7][30]={
102         {0x75,0xb7,0x87,0x80,0x99,0xe0,0xc5,0x96,0x00},
103         {0x74,0x94,0xc2,0xe7,0x10,0x4b,0x08,0x79,0x00},
104         {0xde,0x18,0x89,0x41,0xa3,0x37,0x5d,0x3a,0x00},
105         {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,
106          0xbd,0x61,0x5a,0x11,0x62,0xe1,0xc7,0xba,
107          0x36,0xb6,0x78,0x58,0x00},
108         {0x66,0xa0,0x94,0x9f,0x8a,0xf7,0xd6,0x89,
109          0x1f,0x7f,0x83,0x2b,0xa8,0x33,0xc0,0x0c,
110          0x89,0x2e,0xbe,0x30,0x14,0x3c,0xe2,0x87,
111          0x40,0x01,0x1e,0xcf,0x00},
112         {0xd6,0xa1,0x41,0xa7,0xec,0x3c,0x38,0xdf,0xbd,0x61,0x00},
113         {0},
114         };
115
116 #if 0
117 void dump_hex(unsigned char* in, unsigned int len)
118 {
119         unsigned int i;
120         printf("[__\n");
121         for (i=0;i<len;i++) {
122                 if (i && !(i%16))
123                         printf("\n");
124                 printf("%.2X ", in[i]&0xFF);
125         }    
126         printf("\n__]\n");
127 }    
128 #endif
129
130 int key_len = 128;
131 int main(void)
132 {
133         MD5_CTX h1;
134         unsigned char out[32];
135         unsigned char text[16];
136         AES_KEY key;
137
138         unsigned char counter[AES_BLOCK_SIZE] = {0};
139         unsigned char ecount_buf[AES_BLOCK_SIZE] = {0};
140         unsigned int     num = 0;
141         int i;
142
143         uint8_t rc4_buffer[30];
144
145 #ifndef HOST_VERSION
146         /* UART */
147         uart_init();
148         fdevopen(uart0_dev_send, uart0_dev_recv);
149
150         sei();
151 #endif
152
153         /************ TEST RC4 */
154
155     for( i = 0; i < 6; i++ ) {
156                 printf( " Test %d ", i + 1 );
157
158         memcpy( rc4_buffer, rc4_data[i], rc4_data_len[i] );
159
160         rc4_init( &rc4_keys[i][1], rc4_keys[i][0] );
161         rc4_crypt( rc4_buffer, rc4_data_len[i] );
162
163         if( memcmp( rc4_buffer, rc4_output[i], rc4_data_len[i] ) ) {
164                         printf( "RC4 failed!\n" );
165                 }
166                 else {
167                         printf( "RC4 test passed.\n" );
168                 }
169         }
170         
171
172         memset(my_key, 0, 16);
173         strcpy((char*)my_key, "toto");
174
175         
176
177         /*TEST HASH MD5*/
178         MD5Init(&h1);
179         MD5Update(&h1, text_in, 64);
180         MD5Update(&h1, my_key, 32);
181         MD5Final(out, &h1);
182         
183         if (memcmp(out,"\xB8\x65\xA8\x46\xFC\x9F\x81\xFC\x4B\x98\x73\x4B\xAB\x1D\x4E\x65",16))
184                 printf( "Hash MD5 failed!\n" );
185         else
186                 printf( "Hash MD5 passed\n" );
187
188
189
190
191     
192         /*TEST AES ENCRYPT*/
193         AES_set_encrypt_key(my_key, key_len, &key);
194         AES_encrypt(text_in, out, &key);
195         if (memcmp(out,"\x7F\xA0\x88\xA5\xDB\x57\xE8\x63\x44\x35\xF6\xF5\x7F\xE6\x4A\xA1",16))
196                 printf( "AES encrypt failed!\n" );
197         else
198                 printf( "AES encrypt passed\n" );
199
200
201         /*TEST AES DECRYPT*/
202         AES_set_decrypt_key(my_key, key_len, &key);
203         AES_decrypt(out, text, &key);
204         if (memcmp(text,"\x36\x36\x36\x44\x6F\x79\x6F\x75\x72\x65\x63\x6F\x67\x6E\x69\x7A",16))
205                 printf( "AES decrypt failed!\n" );
206         else
207                 printf( "AES decrypt passed\n" );
208
209
210
211         /*TEST AES MODE CTR ENCRYPT*/
212         memset(counter, 0, AES_BLOCK_SIZE);
213         memset(ecount_buf, 0, AES_BLOCK_SIZE);
214         num = 0;
215         AES_set_encrypt_key(test_aes_ctr_key, key_len, &key);
216         for (i=0;i<sizeof(test_aes_text)-1;i++)
217                 AES_ctr128_encrypt(&test_aes_text[i], &text_im[i], 1, &key,counter,ecount_buf,&num);
218         if (memcmp(text_im, "\x07\x8B\x28\xB0\x8A\xEC\x4B\x53\xE1\x26\x91\x35\xA7\x5A\x44\x5E"
219                    "\x09\xF9\xBE\x65\x6B\xB9\x04\xAC\x9E\xF7\x6F\x63\xFD\xB1\x11\x26"
220                    "\xF2\xF8\x7D\x6B\x95\xC7\xF2\xB2\x01\xFD\xBD\x24\x20\x6D\xFC\xEB"
221                    "\xCD\x38\xEC\x77\xBC\xCA\x84\xB5\x63\x4E\x4E\x82\x1E\x9E\x72\x77", 64))
222                 printf( "AES counter encrypt failed!\n" );
223         else
224                 printf( "AES counter encrypt passed\n" );
225
226
227         /*TEST AES MODE CTR DECRYPT*/
228         memset(counter, 0, AES_BLOCK_SIZE);
229         memset(ecount_buf, 0, AES_BLOCK_SIZE);
230         num=0;
231         AES_set_encrypt_key(test_aes_ctr_key, key_len, &key);
232         for (i=0;i<64;i++)
233                 AES_ctr128_encrypt(&text_im[i], &text_out[i], 1, &key,counter,ecount_buf,&num);
234         if (memcmp(text_out, test_aes_text, 64))
235                 printf( "AES counter encrypt failed!\n" );
236         else
237                 printf( "AES counter encrypt passed\n" );
238
239
240         /*TEST HASH MAC MD5 rfc2104*/
241         /*RESULT 0x9294727a3638bb1c13f48ef8158bfc9d*/
242         HMAC_MD5(out, test_hmac_md5_data, test_hmac_md5_key, 8, 16);
243         if (memcmp(out,"\x92\x94\x72\x7A\x36\x38\xBB\x1C\x13\xF4\x8E\xF8\x15\x8B\xFC\x9D", 16))
244                 printf( "AES counter encrypt failed!\n" );
245         else
246                 printf( "AES counter encrypt passed\n" );
247         
248         printf( "End\n");
249         while(1);
250
251         return 0;
252 }
253