blob: 2222b63d72fa2dd8878efbf8136ddaceede0f04d [file] [log] [blame]
Kenny Rootb8494592015-09-25 02:29:14 +00001/* Copyright (c) 2015, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <stdio.h>
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010016#include <stdlib.h>
Kenny Rootb8494592015-09-25 02:29:14 +000017#include <string.h>
18
Steven Valdezbb1ceac2016-10-07 10:34:51 -040019#include <memory>
20#include <vector>
21
Robert Sloan8ff03552017-06-14 12:40:58 -070022#include <gtest/gtest.h>
23
Kenny Rootb8494592015-09-25 02:29:14 +000024#include <openssl/aes.h>
Kenny Rootb8494592015-09-25 02:29:14 +000025
Robert Sloan4c22c5f2019-03-01 15:53:37 -080026#include "internal.h"
Robert Sloan572a4e22017-04-17 10:52:19 -070027#include "../../internal.h"
Robert Sloan4c22c5f2019-03-01 15:53:37 -080028#include "../../test/abi_test.h"
Robert Sloan572a4e22017-04-17 10:52:19 -070029#include "../../test/file_test.h"
Robert Sloan8ff03552017-06-14 12:40:58 -070030#include "../../test/test_util.h"
Adam Vartanianbfcf3a72018-08-10 14:55:24 +010031#include "../../test/wycheproof_util.h"
Kenny Rootb8494592015-09-25 02:29:14 +000032
Steven Valdezbb1ceac2016-10-07 10:34:51 -040033
Robert Sloan8ff03552017-06-14 12:40:58 -070034static void TestRaw(FileTest *t) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -040035 std::vector<uint8_t> key, plaintext, ciphertext;
Robert Sloan8ff03552017-06-14 12:40:58 -070036 ASSERT_TRUE(t->GetBytes(&key, "Key"));
37 ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext"));
38 ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext"));
Steven Valdezbb1ceac2016-10-07 10:34:51 -040039
Robert Sloan8ff03552017-06-14 12:40:58 -070040 ASSERT_EQ(static_cast<unsigned>(AES_BLOCK_SIZE), plaintext.size());
41 ASSERT_EQ(static_cast<unsigned>(AES_BLOCK_SIZE), ciphertext.size());
Steven Valdezbb1ceac2016-10-07 10:34:51 -040042
Kenny Rootb8494592015-09-25 02:29:14 +000043 AES_KEY aes_key;
Robert Sloan8ff03552017-06-14 12:40:58 -070044 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key));
Kenny Rootb8494592015-09-25 02:29:14 +000045
46 // Test encryption.
47 uint8_t block[AES_BLOCK_SIZE];
Steven Valdezbb1ceac2016-10-07 10:34:51 -040048 AES_encrypt(plaintext.data(), block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070049 EXPECT_EQ(Bytes(ciphertext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000050
51 // Test in-place encryption.
Robert Sloan69939df2017-01-09 10:53:07 -080052 OPENSSL_memcpy(block, plaintext.data(), AES_BLOCK_SIZE);
Kenny Rootb8494592015-09-25 02:29:14 +000053 AES_encrypt(block, block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070054 EXPECT_EQ(Bytes(ciphertext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000055
Robert Sloan8ff03552017-06-14 12:40:58 -070056 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key));
Kenny Rootb8494592015-09-25 02:29:14 +000057
58 // Test decryption.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040059 AES_decrypt(ciphertext.data(), block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070060 EXPECT_EQ(Bytes(plaintext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000061
62 // Test in-place decryption.
Robert Sloan69939df2017-01-09 10:53:07 -080063 OPENSSL_memcpy(block, ciphertext.data(), AES_BLOCK_SIZE);
Kenny Rootb8494592015-09-25 02:29:14 +000064 AES_decrypt(block, block, &aes_key);
Robert Sloan8ff03552017-06-14 12:40:58 -070065 EXPECT_EQ(Bytes(plaintext), Bytes(block));
Kenny Rootb8494592015-09-25 02:29:14 +000066}
67
Robert Sloan8ff03552017-06-14 12:40:58 -070068static void TestKeyWrap(FileTest *t) {
Steven Valdezbb1ceac2016-10-07 10:34:51 -040069 // All test vectors use the default IV, so test both with implicit and
70 // explicit IV.
71 //
72 // TODO(davidben): Find test vectors that use a different IV.
73 static const uint8_t kDefaultIV[] = {
74 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6,
75 };
Kenny Rootb8494592015-09-25 02:29:14 +000076
Steven Valdezbb1ceac2016-10-07 10:34:51 -040077 std::vector<uint8_t> key, plaintext, ciphertext;
Robert Sloan8ff03552017-06-14 12:40:58 -070078 ASSERT_TRUE(t->GetBytes(&key, "Key"));
79 ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext"));
80 ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext"));
Kenny Rootb8494592015-09-25 02:29:14 +000081
Robert Sloan8ff03552017-06-14 12:40:58 -070082 ASSERT_EQ(plaintext.size() + 8, ciphertext.size())
83 << "Invalid Plaintext and Ciphertext lengths.";
Steven Valdezbb1ceac2016-10-07 10:34:51 -040084
Robert Sloan8ff03552017-06-14 12:40:58 -070085 // Test encryption.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040086 AES_KEY aes_key;
Robert Sloan8ff03552017-06-14 12:40:58 -070087 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key));
Steven Valdezbb1ceac2016-10-07 10:34:51 -040088
Robert Sloan8ff03552017-06-14 12:40:58 -070089 // Test with implicit IV.
Steven Valdezbb1ceac2016-10-07 10:34:51 -040090 std::unique_ptr<uint8_t[]> buf(new uint8_t[ciphertext.size()]);
Robert Sloan8ff03552017-06-14 12:40:58 -070091 int len = AES_wrap_key(&aes_key, nullptr /* iv */, buf.get(),
92 plaintext.data(), plaintext.size());
93 ASSERT_GE(len, 0);
94 EXPECT_EQ(Bytes(ciphertext), Bytes(buf.get(), static_cast<size_t>(len)));
Steven Valdezbb1ceac2016-10-07 10:34:51 -040095
Robert Sloan8ff03552017-06-14 12:40:58 -070096 // Test with explicit IV.
Robert Sloan69939df2017-01-09 10:53:07 -080097 OPENSSL_memset(buf.get(), 0, ciphertext.size());
Robert Sloan8ff03552017-06-14 12:40:58 -070098 len = AES_wrap_key(&aes_key, kDefaultIV, buf.get(), plaintext.data(),
99 plaintext.size());
100 ASSERT_GE(len, 0);
101 EXPECT_EQ(Bytes(ciphertext), Bytes(buf.get(), static_cast<size_t>(len)));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400102
Robert Sloan8ff03552017-06-14 12:40:58 -0700103 // Test decryption.
104 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400105
Robert Sloan8ff03552017-06-14 12:40:58 -0700106 // Test with implicit IV.
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400107 buf.reset(new uint8_t[plaintext.size()]);
Robert Sloan8ff03552017-06-14 12:40:58 -0700108 len = AES_unwrap_key(&aes_key, nullptr /* iv */, buf.get(), ciphertext.data(),
109 ciphertext.size());
110 ASSERT_GE(len, 0);
111 EXPECT_EQ(Bytes(plaintext), Bytes(buf.get(), static_cast<size_t>(len)));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400112
Robert Sloan8ff03552017-06-14 12:40:58 -0700113 // Test with explicit IV.
Robert Sloan69939df2017-01-09 10:53:07 -0800114 OPENSSL_memset(buf.get(), 0, plaintext.size());
Robert Sloan8ff03552017-06-14 12:40:58 -0700115 len = AES_unwrap_key(&aes_key, kDefaultIV, buf.get(), ciphertext.data(),
116 ciphertext.size());
117 ASSERT_GE(len, 0);
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400118
Robert Sloan8ff03552017-06-14 12:40:58 -0700119 // Test corrupted ciphertext.
Robert Sloan69939df2017-01-09 10:53:07 -0800120 ciphertext[0] ^= 1;
Robert Sloan8ff03552017-06-14 12:40:58 -0700121 EXPECT_EQ(-1, AES_unwrap_key(&aes_key, nullptr /* iv */, buf.get(),
122 ciphertext.data(), ciphertext.size()));
Steven Valdezbb1ceac2016-10-07 10:34:51 -0400123}
124
Robert Sloan8ff03552017-06-14 12:40:58 -0700125TEST(AESTest, TestVectors) {
126 FileTestGTest("crypto/fipsmodule/aes/aes_tests.txt", [](FileTest *t) {
127 if (t->GetParameter() == "Raw") {
128 TestRaw(t);
129 } else if (t->GetParameter() == "KeyWrap") {
130 TestKeyWrap(t);
131 } else {
132 ADD_FAILURE() << "Unknown mode " << t->GetParameter();
133 }
134 });
Kenny Rootb8494592015-09-25 02:29:14 +0000135}
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100136
137TEST(AESTest, WycheproofKeyWrap) {
138 FileTestGTest("third_party/wycheproof_testvectors/kw_test.txt",
139 [](FileTest *t) {
140 std::string key_size;
141 ASSERT_TRUE(t->GetInstruction(&key_size, "keySize"));
142 std::vector<uint8_t> ct, key, msg;
143 ASSERT_TRUE(t->GetBytes(&ct, "ct"));
144 ASSERT_TRUE(t->GetBytes(&key, "key"));
145 ASSERT_TRUE(t->GetBytes(&msg, "msg"));
146 ASSERT_EQ(static_cast<unsigned>(atoi(key_size.c_str())), key.size() * 8);
147 WycheproofResult result;
148 ASSERT_TRUE(GetWycheproofResult(t, &result));
149
150 if (result != WycheproofResult::kInvalid) {
151 ASSERT_GE(ct.size(), 8u);
152
153 AES_KEY aes;
154 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes));
155 std::vector<uint8_t> out(ct.size() - 8);
156 int len = AES_unwrap_key(&aes, nullptr, out.data(), ct.data(), ct.size());
157 ASSERT_EQ(static_cast<int>(out.size()), len);
158 EXPECT_EQ(Bytes(msg), Bytes(out));
159
160 out.resize(msg.size() + 8);
161 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes));
162 len = AES_wrap_key(&aes, nullptr, out.data(), msg.data(), msg.size());
163 ASSERT_EQ(static_cast<int>(out.size()), len);
164 EXPECT_EQ(Bytes(ct), Bytes(out));
165 } else {
166 AES_KEY aes;
167 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes));
168 std::vector<uint8_t> out(ct.size() < 8 ? 0 : ct.size() - 8);
169 int len = AES_unwrap_key(&aes, nullptr, out.data(), ct.data(), ct.size());
170 EXPECT_EQ(-1, len);
171 }
172 });
173}
174
175TEST(AESTest, WrapBadLengths) {
176 uint8_t key[128/8] = {0};
177 AES_KEY aes;
178 ASSERT_EQ(0, AES_set_encrypt_key(key, 128, &aes));
179
180 // Input lengths to |AES_wrap_key| must be a multiple of 8 and at least 16.
181 static const size_t kLengths[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
182 9, 10, 11, 12, 13, 14, 15, 20};
183 for (size_t len : kLengths) {
184 SCOPED_TRACE(len);
185 std::vector<uint8_t> in(len);
186 std::vector<uint8_t> out(len + 8);
187 EXPECT_EQ(-1,
188 AES_wrap_key(&aes, nullptr, out.data(), in.data(), in.size()));
189 }
190}
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800191
192#if defined(SUPPORTS_ABI_TEST)
193TEST(AESTest, ABI) {
194 for (int bits : {128, 192, 256}) {
195 SCOPED_TRACE(bits);
196 const uint8_t kKey[256/8] = {0};
197 AES_KEY key;
198 uint8_t block[AES_BLOCK_SIZE];
199 uint8_t buf[AES_BLOCK_SIZE * 64] = {0};
200 std::vector<int> block_counts;
201 if (bits == 128) {
202 block_counts = {0, 1, 2, 3, 4, 8, 16, 31};
203 } else {
204 // Unwind tests are very slow. Assume that the various input sizes do not
205 // differ significantly by round count for ABI purposes.
206 block_counts = {0, 1, 8};
207 }
208
209 CHECK_ABI(aes_nohw_set_encrypt_key, kKey, bits, &key);
210 CHECK_ABI(aes_nohw_encrypt, block, block, &key);
211#if defined(AES_NOHW_CBC)
212 for (size_t blocks : block_counts) {
213 SCOPED_TRACE(blocks);
214 CHECK_ABI(aes_nohw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
215 block, AES_ENCRYPT);
216 }
217#endif
218
219 CHECK_ABI(aes_nohw_set_decrypt_key, kKey, bits, &key);
220 CHECK_ABI(aes_nohw_decrypt, block, block, &key);
221#if defined(AES_NOHW_CBC)
222 for (size_t blocks : block_counts) {
223 SCOPED_TRACE(blocks);
224 CHECK_ABI(aes_nohw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
225 block, AES_DECRYPT);
226 }
227#endif
228
229 if (bsaes_capable()) {
230 aes_nohw_set_encrypt_key(kKey, bits, &key);
231 for (size_t blocks : block_counts) {
232 SCOPED_TRACE(blocks);
233 if (blocks != 0) {
234 CHECK_ABI(bsaes_ctr32_encrypt_blocks, buf, buf, blocks, &key, block);
235 }
236 }
237
238 aes_nohw_set_decrypt_key(kKey, bits, &key);
239 for (size_t blocks : block_counts) {
240 SCOPED_TRACE(blocks);
241 CHECK_ABI(bsaes_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
242 block, AES_DECRYPT);
243 }
244 }
245
246 if (vpaes_capable()) {
247 CHECK_ABI(vpaes_set_encrypt_key, kKey, bits, &key);
248 CHECK_ABI(vpaes_encrypt, block, block, &key);
249 for (size_t blocks : block_counts) {
250 SCOPED_TRACE(blocks);
251 CHECK_ABI(vpaes_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
252 block, AES_ENCRYPT);
Robert Sloan89678152019-03-12 14:24:00 -0700253#if defined(VPAES_CTR32)
254 CHECK_ABI(vpaes_ctr32_encrypt_blocks, buf, buf, blocks, &key, block);
255#endif
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800256 }
257
258 CHECK_ABI(vpaes_set_decrypt_key, kKey, bits, &key);
259 CHECK_ABI(vpaes_decrypt, block, block, &key);
260 for (size_t blocks : block_counts) {
261 SCOPED_TRACE(blocks);
262 CHECK_ABI(vpaes_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
263 block, AES_DECRYPT);
264 }
265 }
266
267 if (hwaes_capable()) {
268 CHECK_ABI(aes_hw_set_encrypt_key, kKey, bits, &key);
269 CHECK_ABI(aes_hw_encrypt, block, block, &key);
270 for (size_t blocks : block_counts) {
271 SCOPED_TRACE(blocks);
272 CHECK_ABI(aes_hw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
273 block, AES_ENCRYPT);
274 CHECK_ABI(aes_hw_ctr32_encrypt_blocks, buf, buf, blocks, &key, block);
275#if defined(HWAES_ECB)
276 CHECK_ABI(aes_hw_ecb_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
277 AES_ENCRYPT);
278#endif
279 }
280
281 CHECK_ABI(aes_hw_set_decrypt_key, kKey, bits, &key);
282 CHECK_ABI(aes_hw_decrypt, block, block, &key);
283 for (size_t blocks : block_counts) {
284 SCOPED_TRACE(blocks);
285 CHECK_ABI(aes_hw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
286 block, AES_DECRYPT);
287#if defined(HWAES_ECB)
288 CHECK_ABI(aes_hw_ecb_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
289 AES_DECRYPT);
290#endif
291 }
292 }
293 }
294}
295#endif // SUPPORTS_ABI_TEST