blob: 7fadb351407044e093cd13b75433f488d7b4ec8d [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
Pete Bentleya5c947b2019-08-09 14:24:27 +0000125static void TestKeyWrapWithPadding(FileTest *t) {
126 std::vector<uint8_t> key, plaintext, ciphertext;
127 ASSERT_TRUE(t->GetBytes(&key, "Key"));
128 ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext"));
129 ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext"));
130
131 // Test encryption.
132 AES_KEY aes_key;
133 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key));
134 std::unique_ptr<uint8_t[]> buf(new uint8_t[plaintext.size() + 15]);
135 size_t len;
136 ASSERT_TRUE(AES_wrap_key_padded(&aes_key, buf.get(), &len,
137 plaintext.size() + 15, plaintext.data(),
138 plaintext.size()));
139 EXPECT_EQ(Bytes(ciphertext), Bytes(buf.get(), static_cast<size_t>(len)));
140
141 // Test decryption
142 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key));
143 buf.reset(new uint8_t[ciphertext.size() - 8]);
144 ASSERT_TRUE(AES_unwrap_key_padded(&aes_key, buf.get(), &len,
145 ciphertext.size() - 8, ciphertext.data(),
146 ciphertext.size()));
147 ASSERT_EQ(len, plaintext.size());
148 EXPECT_EQ(Bytes(plaintext), Bytes(buf.get(), static_cast<size_t>(len)));
149}
150
Robert Sloan8ff03552017-06-14 12:40:58 -0700151TEST(AESTest, TestVectors) {
152 FileTestGTest("crypto/fipsmodule/aes/aes_tests.txt", [](FileTest *t) {
153 if (t->GetParameter() == "Raw") {
154 TestRaw(t);
155 } else if (t->GetParameter() == "KeyWrap") {
156 TestKeyWrap(t);
Pete Bentleya5c947b2019-08-09 14:24:27 +0000157 } else if (t->GetParameter() == "KeyWrapWithPadding") {
158 TestKeyWrapWithPadding(t);
Robert Sloan8ff03552017-06-14 12:40:58 -0700159 } else {
160 ADD_FAILURE() << "Unknown mode " << t->GetParameter();
161 }
162 });
Kenny Rootb8494592015-09-25 02:29:14 +0000163}
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100164
165TEST(AESTest, WycheproofKeyWrap) {
166 FileTestGTest("third_party/wycheproof_testvectors/kw_test.txt",
167 [](FileTest *t) {
168 std::string key_size;
169 ASSERT_TRUE(t->GetInstruction(&key_size, "keySize"));
170 std::vector<uint8_t> ct, key, msg;
171 ASSERT_TRUE(t->GetBytes(&ct, "ct"));
172 ASSERT_TRUE(t->GetBytes(&key, "key"));
173 ASSERT_TRUE(t->GetBytes(&msg, "msg"));
174 ASSERT_EQ(static_cast<unsigned>(atoi(key_size.c_str())), key.size() * 8);
175 WycheproofResult result;
176 ASSERT_TRUE(GetWycheproofResult(t, &result));
177
178 if (result != WycheproofResult::kInvalid) {
179 ASSERT_GE(ct.size(), 8u);
180
181 AES_KEY aes;
182 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes));
183 std::vector<uint8_t> out(ct.size() - 8);
184 int len = AES_unwrap_key(&aes, nullptr, out.data(), ct.data(), ct.size());
185 ASSERT_EQ(static_cast<int>(out.size()), len);
186 EXPECT_EQ(Bytes(msg), Bytes(out));
187
188 out.resize(msg.size() + 8);
189 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes));
190 len = AES_wrap_key(&aes, nullptr, out.data(), msg.data(), msg.size());
191 ASSERT_EQ(static_cast<int>(out.size()), len);
192 EXPECT_EQ(Bytes(ct), Bytes(out));
193 } else {
194 AES_KEY aes;
195 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes));
196 std::vector<uint8_t> out(ct.size() < 8 ? 0 : ct.size() - 8);
197 int len = AES_unwrap_key(&aes, nullptr, out.data(), ct.data(), ct.size());
198 EXPECT_EQ(-1, len);
199 }
200 });
201}
202
Pete Bentleya5c947b2019-08-09 14:24:27 +0000203TEST(AESTest, WycheproofKeyWrapWithPadding) {
204 FileTestGTest("third_party/wycheproof_testvectors/kwp_test.txt",
205 [](FileTest *t) {
206 std::string key_size;
207 ASSERT_TRUE(t->GetInstruction(&key_size, "keySize"));
208 std::vector<uint8_t> ct, key, msg;
209 ASSERT_TRUE(t->GetBytes(&ct, "ct"));
210 ASSERT_TRUE(t->GetBytes(&key, "key"));
211 ASSERT_TRUE(t->GetBytes(&msg, "msg"));
212 ASSERT_EQ(static_cast<unsigned>(atoi(key_size.c_str())), key.size() * 8);
213 WycheproofResult result;
214 ASSERT_TRUE(GetWycheproofResult(t, &result));
215
216 // Wycheproof contains test vectors with empty messages that it believes
217 // should pass. However, both RFC 5649 and SP 800-38F section 5.3.1 say that
218 // the minimum length is one. Therefore we consider test cases with an empty
219 // message to be invalid.
220 if (result != WycheproofResult::kInvalid && !msg.empty()) {
221 AES_KEY aes;
222 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes));
223 std::vector<uint8_t> out(ct.size() - 8);
224 size_t len;
225 ASSERT_TRUE(AES_unwrap_key_padded(&aes, out.data(), &len, ct.size() - 8,
226 ct.data(), ct.size()));
227 EXPECT_EQ(Bytes(msg), Bytes(out.data(), len));
228
229 out.resize(msg.size() + 15);
230 ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes));
231 ASSERT_TRUE(AES_wrap_key_padded(&aes, out.data(), &len, msg.size() + 15,
232 msg.data(), msg.size()));
233 EXPECT_EQ(Bytes(ct), Bytes(out.data(), len));
234 } else {
235 AES_KEY aes;
236 ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes));
237 std::vector<uint8_t> out(ct.size());
238 size_t len;
239 ASSERT_FALSE(AES_unwrap_key_padded(&aes, out.data(), &len, ct.size(),
240 ct.data(), ct.size()));
241 }
242 });
243}
244
Adam Vartanianbfcf3a72018-08-10 14:55:24 +0100245TEST(AESTest, WrapBadLengths) {
246 uint8_t key[128/8] = {0};
247 AES_KEY aes;
248 ASSERT_EQ(0, AES_set_encrypt_key(key, 128, &aes));
249
250 // Input lengths to |AES_wrap_key| must be a multiple of 8 and at least 16.
251 static const size_t kLengths[] = {0, 1, 2, 3, 4, 5, 6, 7, 8,
252 9, 10, 11, 12, 13, 14, 15, 20};
253 for (size_t len : kLengths) {
254 SCOPED_TRACE(len);
255 std::vector<uint8_t> in(len);
256 std::vector<uint8_t> out(len + 8);
257 EXPECT_EQ(-1,
258 AES_wrap_key(&aes, nullptr, out.data(), in.data(), in.size()));
259 }
260}
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800261
Robert Sloanf63bd1f2019-04-16 09:26:20 -0700262TEST(AESTest, InvalidKeySize) {
263 static const uint8_t kZero[8] = {0};
264 AES_KEY key;
265 EXPECT_LT(AES_set_encrypt_key(kZero, 42, &key), 0);
266 EXPECT_LT(AES_set_decrypt_key(kZero, 42, &key), 0);
267}
268
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800269#if defined(SUPPORTS_ABI_TEST)
270TEST(AESTest, ABI) {
271 for (int bits : {128, 192, 256}) {
272 SCOPED_TRACE(bits);
273 const uint8_t kKey[256/8] = {0};
274 AES_KEY key;
275 uint8_t block[AES_BLOCK_SIZE];
276 uint8_t buf[AES_BLOCK_SIZE * 64] = {0};
277 std::vector<int> block_counts;
278 if (bits == 128) {
279 block_counts = {0, 1, 2, 3, 4, 8, 16, 31};
280 } else {
281 // Unwind tests are very slow. Assume that the various input sizes do not
282 // differ significantly by round count for ABI purposes.
283 block_counts = {0, 1, 8};
284 }
285
286 CHECK_ABI(aes_nohw_set_encrypt_key, kKey, bits, &key);
287 CHECK_ABI(aes_nohw_encrypt, block, block, &key);
288#if defined(AES_NOHW_CBC)
289 for (size_t blocks : block_counts) {
290 SCOPED_TRACE(blocks);
291 CHECK_ABI(aes_nohw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
292 block, AES_ENCRYPT);
293 }
294#endif
295
296 CHECK_ABI(aes_nohw_set_decrypt_key, kKey, bits, &key);
297 CHECK_ABI(aes_nohw_decrypt, block, block, &key);
298#if defined(AES_NOHW_CBC)
299 for (size_t blocks : block_counts) {
300 SCOPED_TRACE(blocks);
301 CHECK_ABI(aes_nohw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
302 block, AES_DECRYPT);
303 }
304#endif
305
306 if (bsaes_capable()) {
307 aes_nohw_set_encrypt_key(kKey, bits, &key);
308 for (size_t blocks : block_counts) {
309 SCOPED_TRACE(blocks);
310 if (blocks != 0) {
311 CHECK_ABI(bsaes_ctr32_encrypt_blocks, buf, buf, blocks, &key, block);
312 }
313 }
314
315 aes_nohw_set_decrypt_key(kKey, bits, &key);
316 for (size_t blocks : block_counts) {
317 SCOPED_TRACE(blocks);
318 CHECK_ABI(bsaes_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
319 block, AES_DECRYPT);
320 }
321 }
322
323 if (vpaes_capable()) {
324 CHECK_ABI(vpaes_set_encrypt_key, kKey, bits, &key);
325 CHECK_ABI(vpaes_encrypt, block, block, &key);
326 for (size_t blocks : block_counts) {
327 SCOPED_TRACE(blocks);
328 CHECK_ABI(vpaes_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
329 block, AES_ENCRYPT);
Robert Sloan89678152019-03-12 14:24:00 -0700330#if defined(VPAES_CTR32)
331 CHECK_ABI(vpaes_ctr32_encrypt_blocks, buf, buf, blocks, &key, block);
332#endif
Robert Sloan4c22c5f2019-03-01 15:53:37 -0800333 }
334
335 CHECK_ABI(vpaes_set_decrypt_key, kKey, bits, &key);
336 CHECK_ABI(vpaes_decrypt, block, block, &key);
337 for (size_t blocks : block_counts) {
338 SCOPED_TRACE(blocks);
339 CHECK_ABI(vpaes_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
340 block, AES_DECRYPT);
341 }
342 }
343
344 if (hwaes_capable()) {
345 CHECK_ABI(aes_hw_set_encrypt_key, kKey, bits, &key);
346 CHECK_ABI(aes_hw_encrypt, block, block, &key);
347 for (size_t blocks : block_counts) {
348 SCOPED_TRACE(blocks);
349 CHECK_ABI(aes_hw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
350 block, AES_ENCRYPT);
351 CHECK_ABI(aes_hw_ctr32_encrypt_blocks, buf, buf, blocks, &key, block);
352#if defined(HWAES_ECB)
353 CHECK_ABI(aes_hw_ecb_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
354 AES_ENCRYPT);
355#endif
356 }
357
358 CHECK_ABI(aes_hw_set_decrypt_key, kKey, bits, &key);
359 CHECK_ABI(aes_hw_decrypt, block, block, &key);
360 for (size_t blocks : block_counts) {
361 SCOPED_TRACE(blocks);
362 CHECK_ABI(aes_hw_cbc_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
363 block, AES_DECRYPT);
364#if defined(HWAES_ECB)
365 CHECK_ABI(aes_hw_ecb_encrypt, buf, buf, AES_BLOCK_SIZE * blocks, &key,
366 AES_DECRYPT);
367#endif
368 }
369 }
370 }
371}
372#endif // SUPPORTS_ABI_TEST