SEC: Add blowfish cipher.
[bertos.git] / bertos / sec / cipher / blowfish_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2006 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Blowfish testsuite
34  * \author Giovanni Bajo <rasky@develer.com>
35  *
36  */
37
38 #include "blowfish.h"
39 #include <cpu/byteorder.h>
40
41 /*
42  *  Author     :  Randy L. Milbert
43  *  E-mail     :  rmilbert@mit.edu
44  *  Date       :  18 Jun 97
45  *  Description:  Eric Young's test vectors for Blowfish.
46  */
47
48 #define NUM_VARIABLE_KEY_TESTS  34
49 #define NUM_SET_KEY_TESTS                       24
50
51 /* plaintext bytes -- left halves */
52 static const uint32_t plaintext_l[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
53    0x00000000l, 0xFFFFFFFFl, 0x10000000l, 0x11111111l, 0x11111111l,
54    0x01234567l, 0x00000000l, 0x01234567l, 0x01A1D6D0l, 0x5CD54CA8l,
55    0x0248D438l, 0x51454B58l, 0x42FD4430l, 0x059B5E08l, 0x0756D8E0l,
56    0x762514B8l, 0x3BDD1190l, 0x26955F68l, 0x164D5E40l, 0x6B056E18l,
57    0x004BD6EFl, 0x480D3900l, 0x437540C8l, 0x072D43A0l, 0x02FE5577l,
58    0x1D9D5C50l, 0x30553228l, 0x01234567l, 0x01234567l, 0x01234567l,
59    0xFFFFFFFFl, 0x00000000l, 0x00000000l, 0xFFFFFFFFl, 0xFEDCBA98l,
60    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
61    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
62    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
63    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l,
64    0xFEDCBA98l, 0xFEDCBA98l, 0xFEDCBA98l };
65
66 /* plaintext bytes -- right halves */
67 static const uint32_t plaintext_r[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
68    0x00000000l, 0xFFFFFFFFl, 0x00000001l, 0x11111111l, 0x11111111l,
69    0x89ABCDEFl, 0x00000000l, 0x89ABCDEFl, 0x39776742l, 0x3DEF57DAl,
70    0x06F67172l, 0x2DDF440Al, 0x59577FA2l, 0x51CF143Al, 0x774761D2l,
71    0x29BF486Al, 0x49372802l, 0x35AF609Al, 0x4F275232l, 0x759F5CCAl,
72    0x09176062l, 0x6EE762F2l, 0x698F3CFAl, 0x77075292l, 0x8117F12Al,
73    0x18F728C2l, 0x6D6F295Al, 0x89ABCDEFl, 0x89ABCDEFl, 0x89ABCDEFl,
74    0xFFFFFFFFl, 0x00000000l, 0x00000000l, 0xFFFFFFFFl, 0x76543210l,
75    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
76    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
77    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
78    0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l, 0x76543210l,
79    0x76543210l, 0x76543210l, 0x76543210l };
80
81 /* key bytes for variable key tests */
82 static const uint8_t variable_key[NUM_VARIABLE_KEY_TESTS][8] = {
83    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
84    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
85    { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
86    { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 },
87    { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
88    { 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11 },
89    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
90    { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 },
91    { 0x7C, 0xA1, 0x10, 0x45, 0x4A, 0x1A, 0x6E, 0x57 },
92    { 0x01, 0x31, 0xD9, 0x61, 0x9D, 0xC1, 0x37, 0x6E },
93    { 0x07, 0xA1, 0x13, 0x3E, 0x4A, 0x0B, 0x26, 0x86 },
94    { 0x38, 0x49, 0x67, 0x4C, 0x26, 0x02, 0x31, 0x9E },
95    { 0x04, 0xB9, 0x15, 0xBA, 0x43, 0xFE, 0xB5, 0xB6 },
96    { 0x01, 0x13, 0xB9, 0x70, 0xFD, 0x34, 0xF2, 0xCE },
97    { 0x01, 0x70, 0xF1, 0x75, 0x46, 0x8F, 0xB5, 0xE6 },
98    { 0x43, 0x29, 0x7F, 0xAD, 0x38, 0xE3, 0x73, 0xFE },
99    { 0x07, 0xA7, 0x13, 0x70, 0x45, 0xDA, 0x2A, 0x16 },
100    { 0x04, 0x68, 0x91, 0x04, 0xC2, 0xFD, 0x3B, 0x2F },
101    { 0x37, 0xD0, 0x6B, 0xB5, 0x16, 0xCB, 0x75, 0x46 },
102    { 0x1F, 0x08, 0x26, 0x0D, 0x1A, 0xC2, 0x46, 0x5E },
103    { 0x58, 0x40, 0x23, 0x64, 0x1A, 0xBA, 0x61, 0x76 },
104    { 0x02, 0x58, 0x16, 0x16, 0x46, 0x29, 0xB0, 0x07 },
105    { 0x49, 0x79, 0x3E, 0xBC, 0x79, 0xB3, 0x25, 0x8F },
106    { 0x4F, 0xB0, 0x5E, 0x15, 0x15, 0xAB, 0x73, 0xA7 },
107    { 0x49, 0xE9, 0x5D, 0x6D, 0x4C, 0xA2, 0x29, 0xBF },
108    { 0x01, 0x83, 0x10, 0xDC, 0x40, 0x9B, 0x26, 0xD6 },
109    { 0x1C, 0x58, 0x7F, 0x1C, 0x13, 0x92, 0x4F, 0xEF },
110    { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
111    { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E },
112    { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
113    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
114    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
115    { 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF },
116    { 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10 }};
117
118 /* key bytes for set key tests */
119 static const uint8_t set_key[24] = {
120    0xF0, 0xE1, 0xD2, 0xC3, 0xB4, 0xA5, 0x96, 0x87,
121    0x78, 0x69, 0x5A, 0x4B, 0x3C, 0x2D, 0x1E, 0x0F,
122    0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77 };
123
124 /* ciphertext bytes -- left halves */
125 static const uint32_t ciphertext_l[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
126    0x4EF99745l, 0x51866FD5l, 0x7D856F9Al, 0x2466DD87l, 0x61F9C380l,
127    0x7D0CC630l, 0x4EF99745l, 0x0ACEAB0Fl, 0x59C68245l, 0xB1B8CC0Bl,
128    0x1730E577l, 0xA25E7856l, 0x353882B1l, 0x48F4D088l, 0x432193B7l,
129    0x13F04154l, 0x2EEDDA93l, 0xD887E039l, 0x5F99D04Fl, 0x4A057A3Bl,
130    0x452031C1l, 0x7555AE39l, 0x53C55F9Cl, 0x7A8E7BFAl, 0xCF9C5D7Al,
131    0xD1ABB290l, 0x55CB3774l, 0xFA34EC48l, 0xA7907951l, 0xC39E072Dl,
132    0x014933E0l, 0xF21E9A77l, 0x24594688l, 0x6B5C5A9Cl, 0xF9AD597Cl,
133    0xE91D21C1l, 0xE9C2B70Al, 0xBE1E6394l, 0xB39E4448l, 0x9457AA83l,
134    0x8BB77032l, 0xE87A244El, 0x15750E7Al, 0x122BA70Bl, 0x3A833C9Al,
135    0x9409DA87l, 0x884F8062l, 0x1F85031Cl, 0x79D9373Al, 0x93142887l,
136    0x03429E83l, 0xA4299E27l, 0xAFD5AED1l, 0x10851C0El, 0xE6F51ED7l,
137    0x64A6E14Al, 0x80C7D7D4l, 0x05044B62l };
138
139 /* ciphertext bytes -- right halves */
140 static const uint32_t ciphertext_r[NUM_VARIABLE_KEY_TESTS + NUM_SET_KEY_TESTS] = {
141    0x6198DD78l, 0xB85ECB8Al, 0x613063F2l, 0x8B963C9Dl, 0x2281B096l,
142    0xAFDA1EC7l, 0x6198DD78l, 0xC6A0A28Dl, 0xEB05282Bl, 0x250F09A0l,
143    0x8BEA1DA4l, 0xCF2651EBl, 0x09CE8F1Al, 0x4C379918l, 0x8951FC98l,
144    0xD69D1AE5l, 0xFFD39C79l, 0x3C2DA6E3l, 0x5B163969l, 0x24D3977Bl,
145    0xE4FADA8El, 0xF59B87BDl, 0xB49FC019l, 0x937E89A3l, 0x4986ADB5l,
146    0x658BC778l, 0xD13EF201l, 0x47B268B2l, 0x08EA3CAEl, 0x9FAC631Dl,
147    0xCDAFF6E4l, 0xB71C49BCl, 0x5754369Al, 0x5D9E0A5Al, 0x49DB005El,
148    0xD961A6D6l, 0x1BC65CF3l, 0x08640F05l, 0x1BDB1E6El, 0xB1928C0Dl,
149    0xF960629Dl, 0x2CC85E82l, 0x4F4EC577l, 0x3AB64AE0l, 0xFFC537F6l,
150    0xA90F6BF2l, 0x5060B8B4l, 0x19E11968l, 0x714CA34Fl, 0xEE3BE15Cl,
151    0x8CE2D14Bl, 0x469FF67Bl, 0xC1BC96A8l, 0x3858DA9Fl, 0x9B9DB21Fl,
152    0xFD36B46Fl, 0x5A5479ADl, 0xFA52D080l };
153
154
155
156 int blowfish_testSetup(void)
157 {
158         kdbg_init();
159         return 0;
160 }
161
162 int blowfish_testTearDown(void)
163 {
164         return 0;
165 }
166
167 int blowfish_testRun(void)
168 {
169         static BlowfishContext ctx;
170         blowfish_init(&ctx);
171         BlockCipher *c = &ctx.c; //blowfish_stackinit();
172
173         for (int i=0; i<NUM_VARIABLE_KEY_TESTS; ++i)
174         {
175                 uint32_t data[2] = { cpu_to_be32(plaintext_l[i]), cpu_to_be32(plaintext_r[i]) };
176
177                 cipher_set_vkey(c, variable_key[i], 8);
178
179                 cipher_ecb_encrypt(c, data);
180                 ASSERT(data[0] == cpu_to_be32(ciphertext_l[i]));
181                 ASSERT(data[1] == cpu_to_be32(ciphertext_r[i]));
182
183                 cipher_ecb_decrypt(c, data);
184                 ASSERT(data[0] == cpu_to_be32(plaintext_l[i]));
185                 ASSERT(data[1] == cpu_to_be32(plaintext_r[i]));
186         }
187
188         for (int j=0; j<NUM_SET_KEY_TESTS; ++j)
189         {
190                 int i = j + NUM_VARIABLE_KEY_TESTS;
191                 uint32_t data[2] = { cpu_to_be32(plaintext_l[i]), cpu_to_be32(plaintext_r[i]) };
192
193                 cipher_set_vkey(c, &set_key[0], j+1);
194
195                 cipher_ecb_encrypt(c, data);
196                 ASSERT(data[0] == cpu_to_be32(ciphertext_l[i]));
197                 ASSERT(data[1] == cpu_to_be32(ciphertext_r[i]));
198
199                 cipher_ecb_decrypt(c, data);
200                 ASSERT(data[0] == cpu_to_be32(plaintext_l[i]));
201                 ASSERT(data[1] == cpu_to_be32(plaintext_r[i]));
202         }
203
204         uint8_t data[8];
205         memset(data, 0, 8);
206         cipher_set_vkey(c, "0123456789", 10);
207         for (int i=0;i<1000;++i) cipher_ecb_encrypt(c, data);
208         for (int i=0;i<1000;++i) cipher_ecb_decrypt(c, data);
209         ASSERT(memcmp(data, "\x00\x00\x00\x00\x00\x00\x00\x00", 8) == 0);
210
211         return 0;
212 }
213
214 TEST_MAIN(blowfish);