[Title] Add packaging/nettle.spec to build nettle on OBS system
[external/nettle.git] / testsuite / cbc-test.c
1 #include "testutils.h"
2 #include "aes.h"
3 #include "cbc.h"
4 #include "knuth-lfib.h"
5
6 /* Test with more data and inplace decryption, to check that the
7  * cbc_decrypt buffering works. */
8 #define CBC_BULK_DATA 0x2710 /* 10000 */
9
10 static void
11 test_cbc_bulk(void)
12 {
13   struct knuth_lfib_ctx random;
14   
15   uint8_t clear[CBC_BULK_DATA];
16   
17   uint8_t cipher[CBC_BULK_DATA + 1];
18
19   const uint8_t *key = H("966c7bf00bebe6dc 8abd37912384958a"
20                          "743008105a08657d dcaad4128eee38b3");
21   
22   const uint8_t *start_iv = H("11adbff119749103 207619cfa0e8d13a");
23   const uint8_t *end_iv = H("c7a42a569b421224 d0c23e52f46f97f5");
24   
25   struct CBC_CTX(struct aes_ctx, AES_BLOCK_SIZE) aes;
26   
27   knuth_lfib_init(&random, CBC_BULK_DATA);
28   knuth_lfib_random(&random, CBC_BULK_DATA, clear);
29
30   /* Byte that should not be overwritten */
31   cipher[CBC_BULK_DATA] = 17;
32   
33   aes_set_encrypt_key(&aes.ctx, 32, key);
34   CBC_SET_IV(&aes, start_iv);
35
36   CBC_ENCRYPT(&aes, aes_encrypt, CBC_BULK_DATA, cipher, clear);
37
38   if (cipher[CBC_BULK_DATA] != 17)
39     FAIL();
40
41   if (verbose)
42     {
43       printf("IV after bulk encryption: ");
44       print_hex(AES_BLOCK_SIZE, aes.iv);
45       printf("\n");
46     }
47
48   if (!MEMEQ(AES_BLOCK_SIZE, aes.iv, end_iv))
49     FAIL();
50   
51   /* Decrypt, in place */
52   aes_set_decrypt_key(&aes.ctx, 32, key);
53   CBC_SET_IV(&aes, start_iv);
54   CBC_DECRYPT(&aes, aes_decrypt, CBC_BULK_DATA, cipher, cipher);
55
56   if (cipher[CBC_BULK_DATA] != 17)
57     FAIL();
58
59   if (verbose)
60     {
61       printf("IV after bulk decryption: ");
62       print_hex(AES_BLOCK_SIZE, aes.iv);
63       printf("\n");
64     }
65
66   if (!MEMEQ(AES_BLOCK_SIZE, aes.iv, end_iv))
67     FAIL();
68
69   if (!MEMEQ(CBC_BULK_DATA, clear, cipher))
70     FAIL();
71 }
72
73 int
74 test_main(void)
75 {
76   static const uint8_t msg[2 * AES_BLOCK_SIZE] = "Listen, I'll say this only once!";
77   
78   /* Intermediate values:
79    *   iv XOR first message block:
80    *       "a5 ce 55 d4 21 15 a1 c6 4a a4 0c b2 ca a6 d1 37"
81    *   First ciphertext block, c1:
82    *       "1f 94 fc 85 f2 36 21 06 4a ea e3 c9 cc 38 01 0e"
83    *   c1 XOR second message block:
84    *       "3f e0 94 ec 81 16 4e 68 26 93 c3 a6 a2 5b 64 2f"
85    *   Second ciphertext block, c1:
86    *       "7b f6 5f c5 02 59 2e 71 af bf 34 87 c0 36 2a 16"
87    */
88
89   test_cipher_cbc(&nettle_aes256,
90                   HL("8d ae 93 ff fc 78 c9 44"
91                      "2a bd 0c 1e 68 bc a6 c7"
92                      "05 c7 84 e3 5a a9 11 8b"
93                      "d3 16 aa 54 9b 44 08 9e"),
94                   2 * AES_BLOCK_SIZE, msg,
95                   H("1f 94 fc 85 f2 36 21 06"
96                     "4a ea e3 c9 cc 38 01 0e"
97                     "7b f6 5f c5 02 59 2e 71"
98                     "af bf 34 87 c0 36 2a 16"),
99                   H("e9 a7 26 a0 44 7b 8d e6  03 83 60 de ea d5 b0 4e"));
100
101   /* From NIST spec 800-38a on AES modes.
102    *
103    * F.2  CBC Example Vectors 
104    * F.2.1 CBC-AES128.Encrypt
105    */
106
107   /* Intermediate values, blocks input to AES:
108    *
109    *   6bc0bce12a459991e134741a7f9e1925 
110    *   d86421fb9f1a1eda505ee1375746972c 
111    *   604ed7ddf32efdff7020d0238b7c2a5d 
112    *   8521f2fd3c8eef2cdc3da7e5c44ea206 
113    */
114   test_cipher_cbc(&nettle_aes128,
115                   HL("2b7e151628aed2a6abf7158809cf4f3c"),
116                   HL("6bc1bee22e409f96e93d7e117393172a"
117                      "ae2d8a571e03ac9c9eb76fac45af8e51"
118                      "30c81c46a35ce411e5fbc1191a0a52ef"
119                      "f69f2445df4f9b17ad2b417be66c3710"),
120                   H("7649abac8119b246cee98e9b12e9197d"
121                     "5086cb9b507219ee95db113a917678b2"
122                     "73bed6b8e3c1743b7116e69e22229516"
123                     "3ff1caa1681fac09120eca307586e1a7"),
124                   H("000102030405060708090a0b0c0d0e0f"));
125   
126   /* F.2.3 CBC-AES192.Encrypt */
127   
128   /* Intermediate values, blcoks input to AES:
129    *
130    *   6bc0bce12a459991e134741a7f9e1925 
131    *   e12f97e55dbfcfa1efcf7796da0fffb9
132    *   8411b1ef0e2109e5001cf96f256346b5 
133    *   a1840065cdb4e1f7d282fbd7db9d35f0
134    */
135
136   test_cipher_cbc(&nettle_aes192,
137                   HL("8e73b0f7da0e6452c810f32b809079e5"
138                      "62f8ead2522c6b7b"),
139                   HL("6bc1bee22e409f96e93d7e117393172a"
140                      "ae2d8a571e03ac9c9eb76fac45af8e51"
141                      "30c81c46a35ce411e5fbc1191a0a52ef"
142                      "f69f2445df4f9b17ad2b417be66c3710"),
143                   H("4f021db243bc633d7178183a9fa071e8"
144                     "b4d9ada9ad7dedf4e5e738763f69145a"
145                     "571b242012fb7ae07fa9baac3df102e0"
146                     "08b0e27988598881d920a9e64f5615cd"),
147                   H("000102030405060708090a0b0c0d0e0f"));
148    
149   /* F.2.5 CBC-AES256.Encrypt */
150
151   /* Intermediate values, blcoks input to AES:
152    *
153    *   6bc0bce12a459991e134741a7f9e1925 
154    *   5ba1c653c8e65d26e929c4571ad47587 
155    *   ac3452d0dd87649c8264b662dc7a7e92
156    *   cf6d172c769621d8081ba318e24f2371 
157    */
158
159   test_cipher_cbc(&nettle_aes256,
160                   HL("603deb1015ca71be2b73aef0857d7781"
161                      "1f352c073b6108d72d9810a30914dff4"),
162                   HL("6bc1bee22e409f96e93d7e117393172a"
163                      "ae2d8a571e03ac9c9eb76fac45af8e51"
164                      "30c81c46a35ce411e5fbc1191a0a52ef"
165                      "f69f2445df4f9b17ad2b417be66c3710"),
166                   H("f58c4c04d6e5f1ba779eabfb5f7bfbd6"
167                     "9cfc4e967edb808d679f777bc6702c7d"
168                     "39f23369a9d9bacfa530e26304231461"
169                     "b2eb05e2c39be9fcda6c19078c6a9d1b"),
170                   H("000102030405060708090a0b0c0d0e0f"));
171
172   test_cbc_bulk();
173    
174   SUCCESS();
175 }
176
177 /*
178 IV 
179   000102030405060708090a0b0c0d0e0f 
180 Block #1 
181 Plaintext      6bc1bee22e409f96e93d7e117393172a 
182 Input Block     6bc0bce12a459991e134741a7f9e1925 
183 Output Block  7649abac8119b246cee98e9b12e9197d 
184 Ciphertext 7649abac8119b246cee98e9b12e9197d 
185 Block #2 
186 Plaintext      ae2d8a571e03ac9c9eb76fac45af8e51 
187 Input Block     d86421fb9f1a1eda505ee1375746972c 
188 Output Block  5086cb9b507219ee95db113a917678b2 
189 Ciphertext 5086cb9b507219ee95db113a917678b2 
190 Block #3 
191 Plaintext      30c81c46a35ce411e5fbc1191a0a52ef 
192 Input Block     604ed7ddf32efdff7020d0238b7c2a5d 
193 Output Block  73bed6b8e3c1743b7116e69e22229516 
194 Ciphertext 73bed6b8e3c1743b7116e69e22229516 
195 Block #4 
196 Plaintext      f69f2445df4f9b17ad2b417be66c3710 
197 Input Block     8521f2fd3c8eef2cdc3da7e5c44ea206 
198 Output Block  3ff1caa1681fac09120eca307586e1a7 
199 Ciphertext 3ff1caa1681fac09120eca307586e1a7 
200  F.2.2 CBC-AES128.Decrypt 
201 Key 
202   2b7e151628aed2a6abf7158809cf4f3c 
203 IV 
204   000102030405060708090a0b0c0d0e0f 
205 Block #1 
206 Ciphertext 7649abac8119b246cee98e9b12e9197d 
207 Input Block     7649abac8119b246cee98e9b12e9197d 
208 Output Block  6bc0bce12a459991e134741a7f9e1925 
209 Plaintext      6bc1bee22e409f96e93d7e117393172a 
210 Block #2 
211 Ciphertext 5086cb9b507219ee95db113a917678b2 
212 Input Block     5086cb9b507219ee95db113a917678b2 
213 Output Block  d86421fb9f1a1eda505ee1375746972c 
214 Plaintext      ae2d8a571e03ac9c9eb76fac45af8e51 
215 Block #3 
216 Ciphertext 73bed6b8e3c1743b7116e69e22229516 
217 Input Block     73bed6b8e3c1743b7116e69e22229516 
218 Output Block  604ed7ddf32efdff7020d0238b7c2a5d 
219 Plaintext      30c81c46a35ce411e5fbc1191a0a52ef 
220 Block #4 
221 Ciphertext 3ff1caa1681fac09120eca307586e1a7 
222 Input Block     3ff1caa1681fac09120eca307586e1a7 
223
224
225 Output Block  8521f2fd3c8eef2cdc3da7e5c44ea206 
226 Plaintext      f69f2445df4f9b17ad2b417be66c3710 
227  F.2.3 CBC-AES192.Encrypt 
228 Key 
229   8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b
230 IV 
231   000102030405060708090a0b0c0d0e0f 
232 Block #1 
233 Plaintext      6bc1bee22e409f96e93d7e117393172a 
234 Input Block     6bc0bce12a459991e134741a7f9e1925 
235 Output Block  4f021db243bc633d7178183a9fa071e8 
236 Ciphertext 4f021db243bc633d7178183a9fa071e8 
237 Block #2 
238 Plaintext      ae2d8a571e03ac9c9eb76fac45af8e51 
239 Input Block     e12f97e55dbfcfa1efcf7796da0fffb9 
240 Output Block  b4d9ada9ad7dedf4e5e738763f69145a 
241 Ciphertext b4d9ada9ad7dedf4e5e738763f69145a 
242 Block #3 
243 Plaintext      30c81c46a35ce411e5fbc1191a0a52ef 
244 Input Block     8411b1ef0e2109e5001cf96f256346b5 
245 Output Block  571b242012fb7ae07fa9baac3df102e0 
246 Ciphertext 571b242012fb7ae07fa9baac3df102e0 
247 Block #4 
248 Plaintext      f69f2445df4f9b17ad2b417be66c3710 
249 Input Block     a1840065cdb4e1f7d282fbd7db9d35f0 
250 Output Block  08b0e27988598881d920a9e64f5615cd 
251 Ciphertext 08b0e27988598881d920a9e64f5615cd 
252  F.2.4 CBC-AES192.Decrypt 
253 Key 
254   8e73b0f7da0e6452c810f32b809079e562f8ead2522c6b7b
255 IV 
256   000102030405060708090a0b0c0d0e0f 
257 Block #1 
258 Ciphertext 4f021db243bc633d7178183a9fa071e8 
259 Input Block     4f021db243bc633d7178183a9fa071e8 
260 Output Block  6bc0bce12a459991e134741a7f9e1925 
261 Plaintext      6bc1bee22e409f96e93d7e117393172a 
262 Block #2 
263 Ciphertext b4d9ada9ad7dedf4e5e738763f69145a 
264 Input Block     b4d9ada9ad7dedf4e5e738763f69145a 
265 Output Block  e12f97e55dbfcfa1efcf7796da0fffb9 
266 Plaintext      ae2d8a571e03ac9c9eb76fac45af8e51 
267 Block #3 
268 Ciphertext 571b242012fb7ae07fa9baac3df102e0 
269 Input Block     571b242012fb7ae07fa9baac3df102e0 
270 Output Block  8411b1ef0e2109e5001cf96f256346b5 
271 Plaintext      30c81c46a35ce411e5fbc1191a0a52ef 
272 Block #4 
273 Ciphertext 08b0e27988598881d920a9e64f5615cd 
274 Input Block     08b0e27988598881d920a9e64f5615cd 
275 Output Block  a1840065cdb4e1f7d282fbd7db9d35f0 
276 Plaintext      f69f2445df4f9b17ad2b417be66c3710 
277  F.2.5 CBC-AES256.Encrypt 
278 Key 
279   603deb1015ca71be2b73aef0857d7781 
280 1f352c073b6108d72d9810a30914dff4 
281   IV 
282   000102030405060708090a0b0c0d0e0f 
283 Block #1 
284 Plaintext      6bc1bee22e409f96e93d7e117393172a 
285 Input Block     6bc0bce12a459991e134741a7f9e1925 
286 Output Block  f58c4c04d6e5f1ba779eabfb5f7bfbd6 
287 Ciphertext f58c4c04d6e5f1ba779eabfb5f7bfbd6 
288 Block #2 
289 Plaintext      ae2d8a571e03ac9c9eb76fac45af8e51 
290 Input Block     5ba1c653c8e65d26e929c4571ad47587 
291 Output Block  9cfc4e967edb808d679f777bc6702c7d 
292 Ciphertext 9cfc4e967edb808d679f777bc6702c7d
293 Block #3 
294 Plaintext      30c81c46a35ce411e5fbc1191a0a52ef 
295 Input Block     ac3452d0dd87649c8264b662dc7a7e92 
296 Output Block  39f23369a9d9bacfa530e26304231461 
297 Ciphertext 39f23369a9d9bacfa530e26304231461 
298 Block #4 
299 Plaintext      f69f2445df4f9b17ad2b417be66c3710 
300 Input Block     cf6d172c769621d8081ba318e24f2371 
301 Output Block  b2eb05e2c39be9fcda6c19078c6a9d1b 
302 Ciphertext b2eb05e2c39be9fcda6c19078c6a9d1b
303  F.2.6 CBC-AES256.Decrypt 
304 Key 
305   603deb1015ca71be2b73aef0857d7781 
306    1f352c073b6108d72d9810a30914dff4 
307 IV 
308   000102030405060708090a0b0c0d0e0f 
309 Block #1 
310 Ciphertext f58c4c04d6e5f1ba779eabfb5f7bfbd6 
311 Input Block     f58c4c04d6e5f1ba779eabfb5f7bfbd6 
312 Output Block  6bc0bce12a459991e134741a7f9e1925 
313 Plaintext      6bc1bee22e409f96e93d7e117393172a 
314 Block #2 
315 Ciphertext 9cfc4e967edb808d679f777bc6702c7d 
316 Input Block     9cfc4e967edb808d679f777bc6702c7d 
317 Output Block  5ba1c653c8e65d26e929c4571ad47587 
318 Plaintext      ae2d8a571e03ac9c9eb76fac45af8e51 
319 Block #3 
320 Ciphertext 39f23369a9d9bacfa530e26304231461 
321 Input Block     39f23369a9d9bacfa530e26304231461 
322 Output Block  ac3452d0dd87649c8264b662dc7a7e92 
323 Plaintext      30c81c46a35ce411e5fbc1191a0a52ef 
324 Block #4 
325 Ciphertext b2eb05e2c39be9fcda6c19078c6a9d1b 
326 Input Block     b2eb05e2c39be9fcda6c19078c6a9d1b 
327 Output Block  cf6d172c769621d8081ba318e24f2371 
328 Plaintext      f69f2445df4f9b17ad2b417be66c3710 
329 */