Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1 | /* 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 Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 16 | #include <stdlib.h> |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 17 | #include <string.h> |
| 18 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 19 | #include <memory> |
| 20 | #include <vector> |
| 21 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 22 | #include <gtest/gtest.h> |
| 23 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 24 | #include <openssl/aes.h> |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 25 | |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 26 | #include "internal.h" |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 27 | #include "../../internal.h" |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 28 | #include "../../test/abi_test.h" |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 29 | #include "../../test/file_test.h" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 30 | #include "../../test/test_util.h" |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 31 | #include "../../test/wycheproof_util.h" |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 32 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 33 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 34 | static void TestRaw(FileTest *t) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 35 | std::vector<uint8_t> key, plaintext, ciphertext; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 36 | ASSERT_TRUE(t->GetBytes(&key, "Key")); |
| 37 | ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext")); |
| 38 | ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext")); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 39 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 40 | ASSERT_EQ(static_cast<unsigned>(AES_BLOCK_SIZE), plaintext.size()); |
| 41 | ASSERT_EQ(static_cast<unsigned>(AES_BLOCK_SIZE), ciphertext.size()); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 42 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 43 | AES_KEY aes_key; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 44 | ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key)); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 45 | |
| 46 | // Test encryption. |
| 47 | uint8_t block[AES_BLOCK_SIZE]; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 48 | AES_encrypt(plaintext.data(), block, &aes_key); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 49 | EXPECT_EQ(Bytes(ciphertext), Bytes(block)); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 50 | |
| 51 | // Test in-place encryption. |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 52 | OPENSSL_memcpy(block, plaintext.data(), AES_BLOCK_SIZE); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 53 | AES_encrypt(block, block, &aes_key); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 54 | EXPECT_EQ(Bytes(ciphertext), Bytes(block)); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 55 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 56 | ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key)); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 57 | |
| 58 | // Test decryption. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 59 | AES_decrypt(ciphertext.data(), block, &aes_key); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 60 | EXPECT_EQ(Bytes(plaintext), Bytes(block)); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 61 | |
| 62 | // Test in-place decryption. |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 63 | OPENSSL_memcpy(block, ciphertext.data(), AES_BLOCK_SIZE); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 64 | AES_decrypt(block, block, &aes_key); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 65 | EXPECT_EQ(Bytes(plaintext), Bytes(block)); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 68 | static void TestKeyWrap(FileTest *t) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 69 | // 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 Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 76 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 77 | std::vector<uint8_t> key, plaintext, ciphertext; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 78 | ASSERT_TRUE(t->GetBytes(&key, "Key")); |
| 79 | ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext")); |
| 80 | ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext")); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 81 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 82 | ASSERT_EQ(plaintext.size() + 8, ciphertext.size()) |
| 83 | << "Invalid Plaintext and Ciphertext lengths."; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 84 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 85 | // Test encryption. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 86 | AES_KEY aes_key; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 87 | ASSERT_EQ(0, AES_set_encrypt_key(key.data(), 8 * key.size(), &aes_key)); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 88 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 89 | // Test with implicit IV. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 90 | std::unique_ptr<uint8_t[]> buf(new uint8_t[ciphertext.size()]); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 91 | 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 Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 95 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 96 | // Test with explicit IV. |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 97 | OPENSSL_memset(buf.get(), 0, ciphertext.size()); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 98 | 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 Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 102 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 103 | // Test decryption. |
| 104 | ASSERT_EQ(0, AES_set_decrypt_key(key.data(), 8 * key.size(), &aes_key)); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 105 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 106 | // Test with implicit IV. |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 107 | buf.reset(new uint8_t[plaintext.size()]); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 108 | 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 Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 112 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 113 | // Test with explicit IV. |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 114 | OPENSSL_memset(buf.get(), 0, plaintext.size()); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 115 | len = AES_unwrap_key(&aes_key, kDefaultIV, buf.get(), ciphertext.data(), |
| 116 | ciphertext.size()); |
| 117 | ASSERT_GE(len, 0); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 118 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 119 | // Test corrupted ciphertext. |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 120 | ciphertext[0] ^= 1; |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 121 | EXPECT_EQ(-1, AES_unwrap_key(&aes_key, nullptr /* iv */, buf.get(), |
| 122 | ciphertext.data(), ciphertext.size())); |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Pete Bentley | a5c947b | 2019-08-09 14:24:27 +0000 | [diff] [blame] | 125 | static 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 Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 151 | TEST(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 Bentley | a5c947b | 2019-08-09 14:24:27 +0000 | [diff] [blame] | 157 | } else if (t->GetParameter() == "KeyWrapWithPadding") { |
| 158 | TestKeyWrapWithPadding(t); |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame] | 159 | } else { |
| 160 | ADD_FAILURE() << "Unknown mode " << t->GetParameter(); |
| 161 | } |
| 162 | }); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 163 | } |
Adam Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 164 | |
| 165 | TEST(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 Bentley | a5c947b | 2019-08-09 14:24:27 +0000 | [diff] [blame] | 203 | TEST(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 Vartanian | bfcf3a7 | 2018-08-10 14:55:24 +0100 | [diff] [blame] | 245 | TEST(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 Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 261 | |
Robert Sloan | f63bd1f | 2019-04-16 09:26:20 -0700 | [diff] [blame] | 262 | TEST(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 Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 269 | #if defined(SUPPORTS_ABI_TEST) |
| 270 | TEST(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 Sloan | 8967815 | 2019-03-12 14:24:00 -0700 | [diff] [blame] | 330 | #if defined(VPAES_CTR32) |
| 331 | CHECK_ABI(vpaes_ctr32_encrypt_blocks, buf, buf, blocks, &key, block); |
| 332 | #endif |
Robert Sloan | 4c22c5f | 2019-03-01 15:53:37 -0800 | [diff] [blame] | 333 | } |
| 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 |