2 * Copyright Droids Corporation, Microb Technology, Eirbot (2005)
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.
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.
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
18 * Revision : $Id: main.c,v 1.3.4.2 2007-05-23 17:18:12 zer0 Exp $
26 #include <aversive/pgmspace.h>
29 #include <aversive/wait.h>
40 #define SKEY_BINKEY_SIZE 8
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";
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";
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;
64 * ARC4 tests vectors from OpenSSL (crypto/rc4/rc4test.c)
67 void rc4_crypt(unsigned char *data, int length )
71 for (i=0 ; i<length ; i++)
72 data[i] = rc4_crypt_char(data[i]);
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},
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},
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},
117 void dump_hex(unsigned char* in, unsigned int len)
121 for (i=0;i<len;i++) {
124 printf("%.2X ", in[i]&0xFF);
134 unsigned char out[32];
135 unsigned char text[16];
138 unsigned char counter[AES_BLOCK_SIZE] = {0};
139 unsigned char ecount_buf[AES_BLOCK_SIZE] = {0};
140 unsigned int num = 0;
143 uint8_t rc4_buffer[30];
148 fdevopen(uart0_dev_send, uart0_dev_recv);
153 /************ TEST RC4 */
155 for( i = 0; i < 6; i++ ) {
156 printf( " Test %d ", i + 1 );
158 memcpy( rc4_buffer, rc4_data[i], rc4_data_len[i] );
160 rc4_init( &rc4_keys[i][1], rc4_keys[i][0] );
161 rc4_crypt( rc4_buffer, rc4_data_len[i] );
163 if( memcmp( rc4_buffer, rc4_output[i], rc4_data_len[i] ) ) {
164 printf( "RC4 failed!\n" );
167 printf( "RC4 test passed.\n" );
172 memset(my_key, 0, 16);
173 strcpy((char*)my_key, "toto");
179 MD5Update(&h1, text_in, 64);
180 MD5Update(&h1, my_key, 32);
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" );
186 printf( "Hash MD5 passed\n" );
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" );
198 printf( "AES encrypt passed\n" );
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" );
207 printf( "AES decrypt passed\n" );
211 /*TEST AES MODE CTR ENCRYPT*/
212 memset(counter, 0, AES_BLOCK_SIZE);
213 memset(ecount_buf, 0, AES_BLOCK_SIZE);
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" );
224 printf( "AES counter encrypt passed\n" );
227 /*TEST AES MODE CTR DECRYPT*/
228 memset(counter, 0, AES_BLOCK_SIZE);
229 memset(ecount_buf, 0, AES_BLOCK_SIZE);
231 AES_set_encrypt_key(test_aes_ctr_key, key_len, &key);
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" );
237 printf( "AES counter encrypt passed\n" );
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" );
246 printf( "AES counter encrypt passed\n" );