28 * modes: cbc, ecb, ...
29 * need Hashes, asymetrics, signatures, ...
33 #include "serial-tools.h"
37 #include "sha256-asm.h"
47 /*****************************************************************************
48 * additional validation-functions *
49 *****************************************************************************/
51 void shavs_rnd(sha256_hash_t seed){
52 uint8_t md[4][SHA256_HASH_BITS/8], buffer[3*SHA256_HASH_BITS/8];
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);
69 uart_putstr("\r\nMD = ");
70 uart_hexdump(md[i%4], SHA256_HASH_BITS/8);
72 memcpy(seed, (md[i%4]), SHA256_HASH_BITS/8);
76 /*****************************************************************************
78 *****************************************************************************/
79 void testrun_sha256(void){
80 uint8_t block[SHA256_BLOCK_BITS/8];
82 uart_putstr("\r\nsha256(\"\", 0)= ");
83 sha256((void*)block, (void*)"\x00", 0);
84 uart_hexdump(block, SHA256_HASH_BITS/8);
86 uart_putstr("\r\nsha256(\"abc\", 24)= ");
87 sha256((void*)block, (void*)"abc", 24);
88 uart_hexdump(block, SHA256_HASH_BITS/8);
90 uart_putstr("\r\nsha256(\"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq\", 24)= ");
91 sha256((void*)block, (void*) "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 448);
92 uart_hexdump(block, SHA256_HASH_BITS/8);
94 uart_putstr("\r\nsha256(1,000,000 x 'a')= ");
99 memset(block,'a',SHA256_BLOCK_BITS/8);
100 for(i=0; i<(1000000/(SHA256_BLOCK_BITS/8)); ++i){ /* 15625 times*/
101 sha256_nextBlock(&s, block);
103 sha256_lastBlock(&s, block, 0);
104 sha256_ctx2hash((void*)block, &s);
106 uart_hexdump(block, SHA256_HASH_BITS/8);
109 void testrun_xtea(void){
110 uint8_t block[8], block2[8];
113 memcpy (block, "abcdefgh", 8);
115 memset (block2, 0, 8);
116 uart_putstr("\r\nxtea_enc(\"abcdefgh\", 0)= ");
117 xtea_enc((void*)block2, (void*)block, (void*)key);
118 uart_hexdump(block2, 8);
119 uart_putstr("\r\nxtea_dec(form above)= ");
120 xtea_dec((void*)block, (void*)block2, (void*)key);
121 uart_hexdump(block, 8);
123 memcpy (key, "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f", 16);
124 uart_putstr("\r\nxtea_enc(\"abcdefgh\", 000102030405060708090a0b0c0d0e0f)= ");
125 xtea_enc((void*)block, (void*)block, (void*)key);
126 uart_hexdump(block, 8);
127 uart_putstr("\r\nxtea_dec(form above)= ");
128 xtea_dec((void*)block, (void*)block, (void*)key);
129 uart_hexdump(block, 8);
132 void testrun_arcfour(void){
135 /* using wikipedia test-vectors:
136 * RC4( "Key", "Plaintext" ) == "bbf316e8 d940af0a d3"
137 * RC4( "Wiki", "pedia" ) == "1021bf0420"
138 * RC4( "Secret", "Attack at dawn" ) == "45a01f64 5fc35b38 3552544b 9bf5"
140 uart_putstr("\r\narcfour(\"Plaintext\", \"Key\")=");
141 arcfour_init(&s, "Key", 3);
144 *b++ ^= arcfour_gen(&s);
145 uart_hexdump(b-9, 9);
147 uart_putstr("\r\narcfour(\"pedia\", \"Wiki\")=");
148 arcfour_init(&s, "Wiki", 4);
151 *b++ ^= arcfour_gen(&s);
152 uart_hexdump(b-5, 5);
154 uart_putstr("\r\narcfour(\"Attack at dawn\", \"Secret\")=");
155 arcfour_init(&s, "Secret", 6);
158 *b++ ^= arcfour_gen(&s);
159 uart_hexdump(b-14, 14);
161 uart_putstr("\r\narcfour(00.00.00.00.00.00.00.00, 01.23.45.67.89.AB.CD.EF)=");
162 arcfour_init(&s, "\x01\x23\x45\x67\x89\xAB\xCD\xEF", 8);
167 a[i++] ^= arcfour_gen(&s);
171 void testrun_prng(void){
173 uart_putstr("\r\naddEntropy(32, 0x00000000)");
174 addEntropy(32,"\x00\x00\x00\x00");
176 getRandomBlock((void*)block);
178 uart_hexdump(block, 32);
182 void testrun_cast5(void){
185 uart_putstr("\r\nCAST5:\r\nkey: 01 23 45 67 34 56 78 23 45 67 89 34 56 78 9A");
186 cast5_init(&s, "\x01\x23\x45\x67\x12\x34\x56\x78\x23\x45\x67\x89\x34\x56\x78\x9A", 128);
187 uint8_t block[] = {0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF};
188 uart_putstr("\r\nplaintext: ");
189 uart_hexdump(block, 8);
190 cast5_enc(&s, block);
191 uart_putstr("\r\nciphertext: ");
192 uart_hexdump(block, 8);
194 uart_putstr("\r\nK"); uart_putc('0'+(i+1)/10); uart_putc('0'+(i+1)%10); uart_putstr(": ");
195 uart_hexdump(&(s.mask[i]),4);
199 /*****************************************************************************
201 *****************************************************************************/
208 uint8_t block[SHA256_BLOCK_BITS/8];
215 uart_putstr("\r\n\r\nCrypto-VS\r\nloaded and running\r\n");
218 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W1\r\n"); goto error;}
219 if (strcmp(str, "REQ")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
220 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W2\r\n"); goto error;}
221 if (strcmp(str, "SHA256")) {
222 if (strcmp(str, "test")){DEBUG_S("DBG: 1d\r\n"); goto error;};
223 /* use some fixed test-vectors and all Algos */
224 uart_putstr("\r\n intergrated selftests:\r\n");
236 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W4\r\n"); goto error;}
237 if (strcmp(str, "Len=")) {
238 /* 1d9370cdccba99b23670e2e0d6514001006f50d3c7a453201d2776f03c5e58fd */
239 /* f41ece26 13e45739 15696b5a dcd51ca3
240 28be3bf5 66a9ca99 c9ceb027 9c1cb0a7
242 if(strcmp(str, "rnd")){DEBUG_S("DBG: 2b\r\n"); goto error;}
243 sha256_hash_t seed = {0x1d, 0x93, 0x70, 0xcd, 0xcc, 0xba, 0x99, 0xb2, 0x36, 0x70,
244 0xe2, 0xe0, 0xd6, 0x51, 0x40, 0x01, 0x00, 0x6f, 0x50, 0xd3,
245 0xc7, 0xa4, 0x53, 0x20, 0x1d, 0x27, 0x76, 0xf0, 0x3c, 0x5e,
247 { 0xf4, 0x1e, 0xce, 0x26, 0x13, 0xe4, 0x57, 0x39, 0x15, 0x69, 0x6b, 0x5a, 0xdc, 0xd5, 0x1c, 0xa3,
248 0x28, 0xbe, 0x3b, 0xf5, 0x66, 0xa9, 0xca, 0x99, 0xc9, 0xce, 0xb0, 0x27, 0x9c, 0x1c, 0xb0, 0xa7 };
254 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W5\r\n"); goto error;}
258 while (str[i]){ /* we should check for error here */
260 length += str[i++] - '0';
263 // DEBUG_S("\r\nDBG: Length="); DEBUG_B(length&0xff); DEBUG_S("\r\n");
267 if (!getnextwordn(str,20)) {DEBUG_S("DBG: W6\r\n"); goto error;}
269 if (strcmp(str, "Msg=")) {DEBUG_S("DBG: 4b\r\n"); goto error;}
272 memset(block, 0, SHA256_BLOCK_BITS/8);
274 while (length>=SHA256_BLOCK_BITS){
275 readhex2buffer(block, SHA256_BLOCK_BITS/8);
277 sha256_nextBlock(&s, block);
279 length -= SHA256_BLOCK_BITS;
282 readhex2buffer(block, (length/8) + ((length&0x7)?1:0) + ((length)?0:1));
284 sha256_lastBlock(&s, block, length);
286 sha256_ctx2hash((void*)block, &s);
287 uart_putstr("\n\rMD= ");
288 uart_hexdump(block, SHA256_HASH_BITS/8);
289 uart_putstr("\n\r\n\r");
293 uart_putstr("ERROR\r\n");