|
@@ -0,0 +1,180 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (C) 2012 Michael Brown <mbrown@fensystems.co.uk>.
|
|
3
|
+ *
|
|
4
|
+ * This program is free software; you can redistribute it and/or
|
|
5
|
+ * modify it under the terms of the GNU General Public License as
|
|
6
|
+ * published by the Free Software Foundation; either version 2 of the
|
|
7
|
+ * License, or any later version.
|
|
8
|
+ *
|
|
9
|
+ * This program is distributed in the hope that it will be useful, but
|
|
10
|
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11
|
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
12
|
+ * General Public License for more details.
|
|
13
|
+ *
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+FILE_LICENCE ( GPL2_OR_LATER );
|
|
20
|
+
|
|
21
|
+/** @file
|
|
22
|
+ *
|
|
23
|
+ * AES-in-CBC-mode tests
|
|
24
|
+ *
|
|
25
|
+ * These test vectors are provided by NIST as part of the
|
|
26
|
+ * Cryptographic Toolkit Examples, downloadable from:
|
|
27
|
+ *
|
|
28
|
+ * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/AES_CBC.pdf
|
|
29
|
+ *
|
|
30
|
+ */
|
|
31
|
+
|
|
32
|
+/* Forcibly enable assertions */
|
|
33
|
+#undef NDEBUG
|
|
34
|
+
|
|
35
|
+#include <assert.h>
|
|
36
|
+#include <string.h>
|
|
37
|
+#include <ipxe/aes.h>
|
|
38
|
+#include <ipxe/test.h>
|
|
39
|
+#include "cbc_test.h"
|
|
40
|
+
|
|
41
|
+/** Define inline key */
|
|
42
|
+#define KEY(...) { __VA_ARGS__ }
|
|
43
|
+
|
|
44
|
+/** Define inline initialisation vector */
|
|
45
|
+#define IV(...) { __VA_ARGS__ }
|
|
46
|
+
|
|
47
|
+/** Define inline plaintext data */
|
|
48
|
+#define PLAINTEXT(...) { __VA_ARGS__ }
|
|
49
|
+
|
|
50
|
+/** Define inline ciphertext data */
|
|
51
|
+#define CIPHERTEXT(...) { __VA_ARGS__ }
|
|
52
|
+
|
|
53
|
+/** An AES-in-CBC-mode test */
|
|
54
|
+struct aes_cbc_test {
|
|
55
|
+ /** Key */
|
|
56
|
+ const void *key;
|
|
57
|
+ /** Length of key */
|
|
58
|
+ size_t key_len;
|
|
59
|
+ /** Initialisation vector */
|
|
60
|
+ const void *iv;
|
|
61
|
+ /** Length of initialisation vector */
|
|
62
|
+ size_t iv_len;
|
|
63
|
+ /** Plaintext */
|
|
64
|
+ const void *plaintext;
|
|
65
|
+ /** Length of plaintext */
|
|
66
|
+ size_t plaintext_len;
|
|
67
|
+ /** Ciphertext */
|
|
68
|
+ const void *ciphertext;
|
|
69
|
+ /** Length of ciphertext */
|
|
70
|
+ size_t ciphertext_len;
|
|
71
|
+};
|
|
72
|
+
|
|
73
|
+/**
|
|
74
|
+ * Define an AES-in-CBC-mode test
|
|
75
|
+ *
|
|
76
|
+ * @v name Test name
|
|
77
|
+ * @v key_array Key
|
|
78
|
+ * @v iv_array Initialisation vector
|
|
79
|
+ * @v plaintext_array Plaintext
|
|
80
|
+ * @v ciphertext_array Ciphertext
|
|
81
|
+ * @ret test AES-in-CBC-mode test
|
|
82
|
+ */
|
|
83
|
+#define AES_CBC_TEST( name, key_array, iv_array, plaintext_array, \
|
|
84
|
+ ciphertext_array ) \
|
|
85
|
+ static const uint8_t name ## _key [] = key_array; \
|
|
86
|
+ static const uint8_t name ## _iv [] = iv_array; \
|
|
87
|
+ static const uint8_t name ## _plaintext [] = plaintext_array; \
|
|
88
|
+ static const uint8_t name ## _ciphertext [] = ciphertext_array; \
|
|
89
|
+ static const struct aes_cbc_test name = { \
|
|
90
|
+ .key = name ## _key, \
|
|
91
|
+ .key_len = sizeof ( name ## _key ), \
|
|
92
|
+ .iv = name ## _iv, \
|
|
93
|
+ .iv_len = sizeof ( name ## _iv ), \
|
|
94
|
+ .plaintext = name ## _plaintext, \
|
|
95
|
+ .plaintext_len = sizeof ( name ## _plaintext ), \
|
|
96
|
+ .ciphertext = name ## _ciphertext, \
|
|
97
|
+ .ciphertext_len = sizeof ( name ## _ciphertext ), \
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+/**
|
|
101
|
+ * Report AES-in-CBC-mode
|
|
102
|
+ *
|
|
103
|
+ * @v state HMAC_DRBG internal state
|
|
104
|
+ * @v test Instantiation test
|
|
105
|
+ */
|
|
106
|
+#define aes_cbc_ok( test ) do { \
|
|
107
|
+ struct cipher_algorithm *cipher = &aes_cbc_algorithm; \
|
|
108
|
+ \
|
|
109
|
+ assert ( (test)->iv_len == cipher->blocksize ); \
|
|
110
|
+ assert ( (test)->plaintext_len == (test)->ciphertext_len ); \
|
|
111
|
+ cbc_encrypt_ok ( cipher, (test)->key, (test)->key_len, \
|
|
112
|
+ (test)->iv, (test)->plaintext, \
|
|
113
|
+ (test)->ciphertext, (test)->plaintext_len ); \
|
|
114
|
+ cbc_decrypt_ok ( cipher, (test)->key, (test)->key_len, \
|
|
115
|
+ (test)->iv, (test)->ciphertext, \
|
|
116
|
+ (test)->plaintext, (test)->ciphertext_len ); \
|
|
117
|
+ } while ( 0 )
|
|
118
|
+
|
|
119
|
+/** CBC_AES128 */
|
|
120
|
+AES_CBC_TEST ( test_128,
|
|
121
|
+ KEY ( 0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15,
|
|
122
|
+ 0x88, 0x09, 0xcf, 0x4f, 0x3c ),
|
|
123
|
+ IV ( 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
|
|
124
|
+ 0x0b, 0x0c, 0x0d, 0x0e, 0x0f ),
|
|
125
|
+ PLAINTEXT ( 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
|
|
126
|
+ 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
|
|
127
|
+ 0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c,
|
|
128
|
+ 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
|
|
129
|
+ 0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11,
|
|
130
|
+ 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
|
|
131
|
+ 0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17,
|
|
132
|
+ 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10 ),
|
|
133
|
+ CIPHERTEXT ( 0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46,
|
|
134
|
+ 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
|
|
135
|
+ 0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee,
|
|
136
|
+ 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
|
|
137
|
+ 0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b,
|
|
138
|
+ 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
|
|
139
|
+ 0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09,
|
|
140
|
+ 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7 ) );
|
|
141
|
+
|
|
142
|
+/** CBC_AES256 */
|
|
143
|
+AES_CBC_TEST ( test_256,
|
|
144
|
+ KEY ( 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae,
|
|
145
|
+ 0xf0, 0x85, 0x7d, 0x77, 0x81, 0x1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 0xb2, 0xeb, 0x05, 0xe2, 0xc3, 0x9b, 0xe9, 0xfc,
|
|
164
|
+ 0xda, 0x6c, 0x19, 0x07, 0x8c, 0x6a, 0x9d, 0x1b ) );
|
|
165
|
+
|
|
166
|
+/**
|
|
167
|
+ * Perform AES-in-CBC-mode self-test
|
|
168
|
+ *
|
|
169
|
+ */
|
|
170
|
+static void aes_cbc_test_exec ( void ) {
|
|
171
|
+
|
|
172
|
+ aes_cbc_ok ( &test_128 );
|
|
173
|
+ aes_cbc_ok ( &test_256 );
|
|
174
|
+}
|
|
175
|
+
|
|
176
|
+/** AES-in-CBC-mode self-test */
|
|
177
|
+struct self_test aes_cbc_test __self_test = {
|
|
178
|
+ .name = "aes_cbc",
|
|
179
|
+ .exec = aes_cbc_test_exec,
|
|
180
|
+};
|