]> git.cryptolib.org Git - avr-crypto-lib.git/blob - obsolete/main-rc6-test.c
(virtual file add)
[avr-crypto-lib.git] / obsolete / main-rc6-test.c
1 /*
2  * rc6 test-suit
3  * 
4 */
5
6 #include "config.h"
7 #include "serial-tools.h"
8 #include "uart.h"
9 #include "debug.h"
10
11 #include "rc6.h"
12
13 #include <stdint.h>
14 #include <string.h>
15 #include <avr/pgmspace.h>
16
17
18 #ifndef BOOL
19 #define BOOL
20  #ifndef __BOOL
21  #define __BOOL
22   #ifndef __BOOL__
23   #define __BOOL__
24         typedef enum{false=0,true=1} bool;
25   #endif
26  #endif
27 #endif
28
29
30
31 /*****************************************************************************
32  *  additional validation-functions                                                                                      *
33  *****************************************************************************/
34
35 /*****************************************************************************
36  *  self tests                                                                                                                           *
37  *****************************************************************************/
38
39 void test_encrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
40         rc6_ctx_t s;
41         if (print){
42                 uart_putstr("\r\nRC6 (enc):\r\n key:\t");
43                 uart_hexdump(key, keylength/8);
44                 uart_putstr("\r\n plaintext:\t");
45                 uart_hexdump(block, 16);
46         }
47         if (rc6_init(&s, key, keylength)){
48                 uart_putstr("RC6 init failed!");
49                 return;
50         }       
51         rc6_enc(&s, block);
52         if (print){
53                 uart_putstr("\r\n ciphertext:\t");
54                 uart_hexdump(block, 16);
55         }
56         rc6_free(&s);
57
58
59 void test_decrypt(uint8_t *block, uint8_t *key, uint16_t keylength, bool print){
60         rc6_ctx_t s;
61         if (print){
62                 uart_putstr("\r\nRC6 (dec):\r\n key:\t");
63                 uart_hexdump(key, keylength/8);
64                 uart_putstr("\r\n ciphertext:\t");
65                 uart_hexdump(block, 16);
66         }
67         if (rc6_init(&s, key, keylength)){
68                 uart_putstr("RC6 init failed!");
69                 return;
70         }
71         rc6_dec(&s, block);
72         if (print){
73                 uart_putstr("\r\n plaintext:\t");
74                 uart_hexdump(block, 16);
75         }
76         rc6_free(&s);
77
78
79 /*
80  *               Test vectors for encryption with RC6
81         plaintext  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
82         user key   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
83         ciphertext 8f c3 a5 36 56 b1 f7 78 c1 29 df 4e 98 48 a4 1e
84         
85         plaintext  02 13 24 35 46 57 68 79 8a 9b ac bd ce df e0 f1
86         user key   01 23 45 67 89 ab cd ef 01 12 23 34 45 56 67 78
87         ciphertext 52 4e 19 2f 47 15 c6 23 1f 51 f6 36 7e a4 3f 18
88         
89         plaintext  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
90         user key   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
91                    00 00 00 00 00 00 00 00
92         ciphertext 6c d6 1b cb 19 0b 30 38 4e 8a 3f 16 86 90 ae 82
93         
94         plaintext  02 13 24 35 46 57 68 79 8a 9b ac bd ce df e0 f1
95         user key   01 23 45 67 89 ab cd ef 01 12 23 34 45 56 67 78
96                    89 9a ab bc cd de ef f0
97         ciphertext 68 83 29 d0 19 e5 05 04 1e 52 e9 2a f9 52 91 d4
98         
99         plaintext  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
100         user key   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
101                    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
102         ciphertext 8f 5f bd 05 10 d1 5f a8 93 fa 3f da 6e 85 7e c2
103         
104         plaintext  02 13 24 35 46 57 68 79 8a 9b ac bd ce df e0 f1
105         user key   01 23 45 67 89 ab cd ef 01 12 23 34 45 56 67 78
106                    89 9a ab bc cd de ef f0 10 32 54 76 98 ba dc fe
107         ciphertext c8 24 18 16 f0 d7 e4 89 20 ad 16 a1 67 4e 5d 48
108  * 
109  */
110
111         uint8_t PROGMEM testkey[6][256/8]={
112                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
113                 {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78},
114                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
115                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
116                 {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
117                         0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0},
118                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
119                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
120                 {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
121                         0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe}};
122         uint8_t PROGMEM testplain[2][128/8]={
123                 {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
124                 {0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, 0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1}};
125         uint8_t PROGMEM testcipher[6][128/8]={
126                 {0x8f, 0xc3, 0xa5, 0x36, 0x56, 0xb1, 0xf7, 0x78, 0xc1, 0x29, 0xdf, 0x4e, 0x98, 0x48, 0xa4, 0x1e},
127                 {0x52, 0x4e, 0x19, 0x2f, 0x47, 0x15, 0xc6, 0x23, 0x1f, 0x51, 0xf6, 0x36, 0x7e, 0xa4, 0x3f, 0x18},
128                 {0x6c, 0xd6, 0x1b, 0xcb, 0x19, 0x0b, 0x30, 0x38, 0x4e, 0x8a, 0x3f, 0x16, 0x86, 0x90, 0xae, 0x82},
129                 {0x68, 0x83, 0x29, 0xd0, 0x19, 0xe5, 0x05, 0x04, 0x1e, 0x52, 0xe9, 0x2a, 0xf9, 0x52, 0x91, 0xd4},
130                 {0x8f, 0x5f, 0xbd, 0x05, 0x10, 0xd1, 0x5f, 0xa8, 0x93, 0xfa, 0x3f, 0xda, 0x6e, 0x85, 0x7e, 0xc2},
131                 {0xc8, 0x24, 0x18, 0x16, 0xf0, 0xd7, 0xe4, 0x89, 0x20, 0xad, 0x16, 0xa1, 0x67, 0x4e, 0x5d, 0x48}};
132
133
134 void testrun_rc6(void){
135         uint16_t keysize[]={128, 128, 192, 192, 256, 256};
136         uint8_t i;
137         uint8_t block[16];
138         uint8_t key[32];
139         memset(block, 0, 16);
140         memset(key, 0, 16);
141         
142         test_encrypt(block, key, 128, true);
143         for(i=0; i<6; ++i){
144                 memcpy_P(block, testplain[i&1], 128/8);
145                 memcpy_P(key, testkey[i], keysize[i]/8);
146                 test_encrypt(block, key, keysize[i], true);
147                 memcpy_P(key, testkey[i], keysize[i]/8);
148                 test_decrypt(block, key, keysize[i], true);
149         }
150 }
151
152
153
154 /*****************************************************************************
155  *  main                                                                                                                                         *
156  *****************************************************************************/
157
158 int main (void){
159         char str[20];
160
161         
162         DEBUG_INIT();
163         uart_putstr("\r\n");
164
165         uart_putstr("\r\n\r\nCrypto-VS (RC6)\r\nloaded and running\r\n");
166 restart:
167         while(1){ 
168                 if (!getnextwordn(str,20))  {DEBUG_S("DBG: W1\r\n"); goto error;}
169                 if (strcmp(str, "test")) {DEBUG_S("DBG: 1b\r\n"); goto error;}
170                         testrun_rc6();
171                 goto restart;           
172                 continue;
173         error:
174                 uart_putstr("ERROR\r\n");
175         } /* while (1) */
176 }
177