| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <limits.h> |
| 16 | #include <stdlib.h> |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <algorithm> |
| 20 | #include <string> |
| 21 | #include <vector> |
| 22 |
| 23 | #include <gtest/gtest.h> |
| 24 |
| 25 | #include <openssl/aes.h> |
| 26 | #include <openssl/cipher.h> |
| 27 | #include <openssl/err.h> |
| 28 | #include <openssl/nid.h> |
| 29 | #include <openssl/rand.h> |
| 30 | #include <openssl/sha.h> |
| 31 | #include <openssl/span.h> |
| 32 |
| 33 | #include "../internal.h" |
| 34 | #include "../test/file_test.h" |
| 35 | #include "../test/test_util.h" |
| 36 | #include "../test/wycheproof_util.h" |
| 37 | #include "./internal.h" |
| 38 |
| 39 |
| 40 | static const EVP_CIPHER *GetCipher(const std::string &name) { |
| 41 | if (name == "DES-CBC") { |
| 42 | return EVP_des_cbc(); |
| 43 | } else if (name == "DES-ECB") { |
| 44 | return EVP_des_ecb(); |
| 45 | } else if (name == "DES-EDE") { |
| 46 | return EVP_des_ede(); |
| 47 | } else if (name == "DES-EDE3") { |
| 48 | return EVP_des_ede3(); |
| 49 | } else if (name == "DES-EDE-CBC") { |
| 50 | return EVP_des_ede_cbc(); |
| 51 | } else if (name == "DES-EDE3-CBC") { |
| 52 | return EVP_des_ede3_cbc(); |
| 53 | } else if (name == "RC4") { |
| 54 | return EVP_rc4(); |
| 55 | } else if (name == "AES-128-ECB") { |
| 56 | return EVP_aes_128_ecb(); |
| 57 | } else if (name == "AES-256-ECB") { |
| 58 | return EVP_aes_256_ecb(); |
| 59 | } else if (name == "AES-128-CBC") { |
| 60 | return EVP_aes_128_cbc(); |
| 61 | } else if (name == "AES-128-GCM") { |
| 62 | return EVP_aes_128_gcm(); |
| 63 | } else if (name == "AES-128-OFB") { |
| 64 | return EVP_aes_128_ofb(); |
| 65 | } else if (name == "AES-192-CBC") { |
| 66 | return EVP_aes_192_cbc(); |
| 67 | } else if (name == "AES-192-CTR") { |
| 68 | return EVP_aes_192_ctr(); |
| 69 | } else if (name == "AES-192-ECB") { |
| 70 | return EVP_aes_192_ecb(); |
| 71 | } else if (name == "AES-192-GCM") { |
| 72 | return EVP_aes_192_gcm(); |
| 73 | } else if (name == "AES-192-OFB") { |
| 74 | return EVP_aes_192_ofb(); |
| 75 | } else if (name == "AES-256-CBC") { |
| 76 | return EVP_aes_256_cbc(); |
| 77 | } else if (name == "AES-128-CTR") { |
| 78 | return EVP_aes_128_ctr(); |
| 79 | } else if (name == "AES-256-CTR") { |
| 80 | return EVP_aes_256_ctr(); |
| 81 | } else if (name == "AES-256-GCM") { |
| 82 | return EVP_aes_256_gcm(); |
| 83 | } else if (name == "AES-256-OFB") { |
| 84 | return EVP_aes_256_ofb(); |
| 85 | } |
| 86 | return nullptr; |
| 87 | } |
| 88 |
| 89 | enum class Operation { |
| 90 | // kBoth tests both encryption and decryption. |
| 91 | kBoth, |
| 92 | // kEncrypt tests encryption. The result of encryption should always |
| 93 | // successfully decrypt, so this should only be used if the test file has a |
| 94 | // matching decrypt-only vector. |
| 95 | kEncrypt, |
| 96 | // kDecrypt tests decryption. This should only be used if the test file has a |
| 97 | // matching encrypt-only input, or if multiple ciphertexts are valid for |
| 98 | // a given plaintext and this is a non-canonical ciphertext. |
| 99 | kDecrypt, |
| 100 | // kInvalidDecrypt tests decryption and expects it to fail, e.g. due to |
| 101 | // invalid tag or padding. |
| 102 | kInvalidDecrypt, |
| 103 | }; |
| 104 |
| 105 | static const char *OperationToString(Operation op) { |
| 106 | switch (op) { |
| 107 | case Operation::kBoth: |
| 108 | return "Both"; |
| 109 | case Operation::kEncrypt: |
| 110 | return "Encrypt"; |
| 111 | case Operation::kDecrypt: |
| 112 | return "Decrypt"; |
| 113 | case Operation::kInvalidDecrypt: |
| 114 | return "InvalidDecrypt"; |
| 115 | } |
| 116 | abort(); |
| 117 | } |
| 118 |
| 119 | // MaybeCopyCipherContext, if |copy| is true, replaces |*ctx| with a, hopefully |
| 120 | // equivalent, copy of it. |
| 121 | static bool MaybeCopyCipherContext(bool copy, |
| 122 | bssl::UniquePtr<EVP_CIPHER_CTX> *ctx) { |
| 123 | if (!copy) { |
| 124 | return true; |
| 125 | } |
| 126 | bssl::UniquePtr<EVP_CIPHER_CTX> ctx2(EVP_CIPHER_CTX_new()); |
| 127 | if (!ctx2 || !EVP_CIPHER_CTX_copy(ctx2.get(), ctx->get())) { |
| 128 | return false; |
| 129 | } |
| 130 | *ctx = std::move(ctx2); |
| 131 | return true; |
| 132 | } |
| 133 |
| 134 | static void TestCipherAPI(const EVP_CIPHER *cipher, Operation op, bool padding, |
| 135 | bool copy, bool in_place, bool use_evp_cipher, |
| 136 | size_t chunk_size, bssl::Span<const uint8_t> key, |
| 137 | bssl::Span<const uint8_t> iv, |
| 138 | bssl::Span<const uint8_t> plaintext, |
| 139 | bssl::Span<const uint8_t> ciphertext, |
| 140 | bssl::Span<const uint8_t> aad, |
| 141 | bssl::Span<const uint8_t> tag) { |
| 142 | bool encrypt = op == Operation::kEncrypt; |
| 143 | bool is_custom_cipher = |
| 144 | EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_CUSTOM_CIPHER; |
| 145 | bssl::Span<const uint8_t> in = encrypt ? plaintext : ciphertext; |
| 146 | bssl::Span<const uint8_t> expected = encrypt ? ciphertext : plaintext; |
| 147 | bool is_aead = EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE; |
| 148 |
| 149 | // Some |EVP_CIPHER|s take a variable-length key, and need to first be |
| 150 | // configured with the key length, which requires configuring the cipher. |
| 151 | bssl::UniquePtr<EVP_CIPHER_CTX> ctx(EVP_CIPHER_CTX_new()); |
| 152 | ASSERT_TRUE(ctx); |
| 153 | ASSERT_TRUE(EVP_CipherInit_ex(ctx.get(), cipher, /*engine=*/nullptr, |
| 154 | /*key=*/nullptr, /*iv=*/nullptr, |
| 155 | encrypt ? 1 : 0)); |
| 156 | ASSERT_TRUE(EVP_CIPHER_CTX_set_key_length(ctx.get(), key.size())); |
| 157 | if (!padding) { |
| 158 | ASSERT_TRUE(EVP_CIPHER_CTX_set_padding(ctx.get(), 0)); |
| 159 | } |
| 160 |
| 161 | // Configure the key. |
| 162 | ASSERT_TRUE(MaybeCopyCipherContext(copy, &ctx)); |
| 163 | ASSERT_TRUE(EVP_CipherInit_ex(ctx.get(), /*cipher=*/nullptr, |
| 164 | /*engine=*/nullptr, key.data(), /*iv=*/nullptr, |
| 165 | /*enc=*/-1)); |
| 166 |
| 167 | // Configure the IV to run the actual operation. Callers that wish to use a |
| 168 | // key for multiple, potentially concurrent, operations will likely copy at |
| 169 | // this point. The |EVP_CIPHER_CTX| API uses the same type to represent a |
| 170 | // pre-computed key schedule and a streaming operation. |
| 171 | ASSERT_TRUE(MaybeCopyCipherContext(copy, &ctx)); |
| 172 | if (is_aead) { |
| 173 | ASSERT_LE(iv.size(), size_t{INT_MAX}); |
| 174 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IVLEN, |
| 175 | static_cast<int>(iv.size()), nullptr)); |
| 176 | ASSERT_EQ(EVP_CIPHER_CTX_iv_length(ctx.get()), iv.size()); |
| 177 | } else { |
| 178 | ASSERT_EQ(iv.size(), EVP_CIPHER_CTX_iv_length(ctx.get())); |
| 179 | } |
| 180 | ASSERT_TRUE(EVP_CipherInit_ex(ctx.get(), /*cipher=*/nullptr, |
| 181 | /*engine=*/nullptr, |
| 182 | /*key=*/nullptr, iv.data(), /*enc=*/-1)); |
| 183 |
| 184 | if (is_aead && !encrypt) { |
| 185 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_TAG, |
| 186 | tag.size(), |
| 187 | const_cast<uint8_t *>(tag.data()))); |
| 188 | } |
| 189 |
| 190 | // Note: the deprecated |EVP_CIPHER|-based AEAD API is sensitive to whether |
| 191 | // parameters are NULL, so it is important to skip the |in| and |aad| |
| 192 | // |EVP_CipherUpdate| calls when empty. |
| 193 | while (!aad.empty()) { |
| 194 | size_t todo = |
| 195 | chunk_size == 0 ? aad.size() : std::min(aad.size(), chunk_size); |
| 196 | if (use_evp_cipher) { |
| 197 | // AEADs always use the "custom cipher" return value convention. Passing a |
| 198 | // null output pointer triggers the AAD logic. |
| 199 | ASSERT_TRUE(is_custom_cipher); |
| 200 | ASSERT_EQ(static_cast<int>(todo), |
| 201 | EVP_Cipher(ctx.get(), nullptr, aad.data(), todo)); |
| 202 | } else { |
| 203 | int len; |
| 204 | ASSERT_TRUE(EVP_CipherUpdate(ctx.get(), nullptr, &len, aad.data(), todo)); |
| 205 | // Although it doesn't output anything, |EVP_CipherUpdate| should claim to |
| 206 | // output the input length. |
| 207 | EXPECT_EQ(len, static_cast<int>(todo)); |
| 208 | } |
| 209 | aad = aad.subspan(todo); |
| 210 | } |
| 211 |
| 212 | // Set up the output buffer. |
| 213 | size_t max_out = in.size(); |
| 214 | size_t block_size = EVP_CIPHER_CTX_block_size(ctx.get()); |
| 215 | if (block_size > 1 && |
| 216 | (EVP_CIPHER_CTX_flags(ctx.get()) & EVP_CIPH_NO_PADDING) == 0 && |
| 217 | EVP_CIPHER_CTX_encrypting(ctx.get())) { |
| 218 | max_out += block_size - (max_out % block_size); |
| 219 | } |
| 220 | std::vector<uint8_t> result(max_out); |
| 221 | if (in_place) { |
| 222 | std::copy(in.begin(), in.end(), result.begin()); |
| 223 | in = bssl::Span(result).first(in.size()); |
| 224 | } |
| 225 |
| 226 | size_t total = 0; |
| 227 | int len; |
| 228 | while (!in.empty()) { |
| 229 | size_t todo = chunk_size == 0 ? in.size() : std::min(in.size(), chunk_size); |
| 230 | EXPECT_LE(todo, static_cast<size_t>(INT_MAX)); |
| 231 | ASSERT_TRUE(MaybeCopyCipherContext(copy, &ctx)); |
| 232 | if (use_evp_cipher) { |
| 233 | // |EVP_Cipher| sometimes returns the number of bytes written, or -1 on |
| 234 | // error, and sometimes 1 or 0, implicitly writing |in_len| bytes. |
| 235 | if (is_custom_cipher) { |
| 236 | len = EVP_Cipher(ctx.get(), result.data() + total, in.data(), todo); |
| 237 | } else { |
| 238 | ASSERT_EQ( |
| 239 | 1, EVP_Cipher(ctx.get(), result.data() + total, in.data(), todo)); |
| 240 | len = static_cast<int>(todo); |
| 241 | } |
| 242 | } else { |
| 243 | ASSERT_TRUE(EVP_CipherUpdate(ctx.get(), result.data() + total, &len, |
| 244 | in.data(), static_cast<int>(todo))); |
| 245 | } |
| 246 | ASSERT_GE(len, 0); |
| 247 | total += static_cast<size_t>(len); |
| 248 | in = in.subspan(todo); |
| 249 | } |
| 250 | if (op == Operation::kInvalidDecrypt) { |
| 251 | if (use_evp_cipher) { |
| 252 | // Only the "custom cipher" return value convention can report failures. |
| 253 | // Passing all nulls should act like |EVP_CipherFinal_ex|. |
| 254 | ASSERT_TRUE(is_custom_cipher); |
| 255 | EXPECT_EQ(-1, EVP_Cipher(ctx.get(), nullptr, nullptr, 0)); |
| 256 | } else { |
| 257 | // Invalid padding and invalid tags all appear as a failed |
| 258 | // |EVP_CipherFinal_ex|. |
| 259 | EXPECT_FALSE(EVP_CipherFinal_ex(ctx.get(), result.data() + total, &len)); |
| 260 | } |
| 261 | } else { |
| 262 | if (use_evp_cipher) { |
| 263 | if (is_custom_cipher) { |
| 264 | // Only the "custom cipher" convention has an |EVP_CipherFinal_ex| |
| 265 | // equivalent. |
| 266 | len = EVP_Cipher(ctx.get(), nullptr, nullptr, 0); |
| 267 | } else { |
| 268 | len = 0; |
| 269 | } |
| 270 | } else { |
| 271 | ASSERT_TRUE(EVP_CipherFinal_ex(ctx.get(), result.data() + total, &len)); |
| 272 | } |
| 273 | ASSERT_GE(len, 0); |
| 274 | total += static_cast<size_t>(len); |
| 275 | result.resize(total); |
| 276 | EXPECT_EQ(Bytes(expected), Bytes(result)); |
| 277 | if (encrypt && is_aead) { |
| 278 | uint8_t rtag[16]; |
| 279 | ASSERT_LE(tag.size(), sizeof(rtag)); |
| 280 | ASSERT_TRUE(MaybeCopyCipherContext(copy, &ctx)); |
| 281 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_GET_TAG, |
| 282 | tag.size(), rtag)); |
| 283 | EXPECT_EQ(Bytes(tag), Bytes(rtag, tag.size())); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 |
| 288 | static void TestLowLevelAPI( |
| 289 | const EVP_CIPHER *cipher, Operation op, bool in_place, size_t chunk_size, |
| 290 | bssl::Span<const uint8_t> key, bssl::Span<const uint8_t> iv, |
| 291 | bssl::Span<const uint8_t> plaintext, bssl::Span<const uint8_t> ciphertext) { |
| 292 | bool encrypt = op == Operation::kEncrypt; |
| 293 | bssl::Span<const uint8_t> in = encrypt ? plaintext : ciphertext; |
| 294 | bssl::Span<const uint8_t> expected = encrypt ? ciphertext : plaintext; |
| 295 | int nid = EVP_CIPHER_nid(cipher); |
| 296 | bool is_ctr = nid == NID_aes_128_ctr || nid == NID_aes_192_ctr || |
| 297 | nid == NID_aes_256_ctr; |
| 298 | bool is_cbc = nid == NID_aes_128_cbc || nid == NID_aes_192_cbc || |
| 299 | nid == NID_aes_256_cbc; |
| 300 | bool is_ofb = nid == NID_aes_128_ofb128 || nid == NID_aes_192_ofb128 || |
| 301 | nid == NID_aes_256_ofb128; |
| 302 | if (!is_ctr && !is_cbc && !is_ofb) { |
| 303 | return; |
| 304 | } |
| 305 |
| 306 | // Invalid ciphertexts are not possible in any of the ciphers where this API |
| 307 | // applies. |
| 308 | ASSERT_NE(op, Operation::kInvalidDecrypt); |
| 309 |
| 310 | AES_KEY aes; |
| 311 | if (encrypt || !is_cbc) { |
| 312 | ASSERT_EQ(0, AES_set_encrypt_key(key.data(), key.size() * 8, &aes)); |
| 313 | } else { |
| 314 | ASSERT_EQ(0, AES_set_decrypt_key(key.data(), key.size() * 8, &aes)); |
| 315 | } |
| 316 |
| 317 | std::vector<uint8_t> result; |
| 318 | if (in_place) { |
| 319 | result.assign(in.begin(), in.end()); |
| 320 | } else { |
| 321 | result.resize(expected.size()); |
| 322 | } |
| 323 | bssl::Span<uint8_t> out = bssl::Span(result); |
| 324 | // Input and output sizes for all the low-level APIs should match. |
| 325 | ASSERT_EQ(in.size(), out.size()); |
| 326 |
| 327 | // The low-level APIs all use block-size IVs. |
| 328 | ASSERT_EQ(iv.size(), size_t{AES_BLOCK_SIZE}); |
| 329 | uint8_t ivec[AES_BLOCK_SIZE]; |
| 330 | OPENSSL_memcpy(ivec, iv.data(), iv.size()); |
| 331 |
| 332 | if (is_ctr) { |
| 333 | unsigned num = 0; |
| 334 | uint8_t ecount_buf[AES_BLOCK_SIZE]; |
| 335 | if (chunk_size == 0) { |
| 336 | AES_ctr128_encrypt(in.data(), out.data(), in.size(), &aes, ivec, |
| 337 | ecount_buf, &num); |
| 338 | } else { |
| 339 | do { |
| 340 | size_t todo = std::min(in.size(), chunk_size); |
| 341 | AES_ctr128_encrypt(in.data(), out.data(), todo, &aes, ivec, ecount_buf, |
| 342 | &num); |
| 343 | in = in.subspan(todo); |
| 344 | out = out.subspan(todo); |
| 345 | } while (!in.empty()); |
| 346 | } |
| 347 | EXPECT_EQ(Bytes(expected), Bytes(result)); |
| 348 | } else if (is_cbc && chunk_size % AES_BLOCK_SIZE == 0) { |
| 349 | // Note |AES_cbc_encrypt| requires block-aligned chunks. |
| 350 | if (chunk_size == 0) { |
| 351 | AES_cbc_encrypt(in.data(), out.data(), in.size(), &aes, ivec, encrypt); |
| 352 | } else { |
| 353 | do { |
| 354 | size_t todo = std::min(in.size(), chunk_size); |
| 355 | AES_cbc_encrypt(in.data(), out.data(), todo, &aes, ivec, encrypt); |
| 356 | in = in.subspan(todo); |
| 357 | out = out.subspan(todo); |
| 358 | } while (!in.empty()); |
| 359 | } |
| 360 | EXPECT_EQ(Bytes(expected), Bytes(result)); |
| 361 | } else if (is_ofb) { |
| 362 | int num = 0; |
| 363 | if (chunk_size == 0) { |
| 364 | AES_ofb128_encrypt(in.data(), out.data(), in.size(), &aes, ivec, &num); |
| 365 | } else { |
| 366 | do { |
| 367 | size_t todo = std::min(in.size(), chunk_size); |
| 368 | AES_ofb128_encrypt(in.data(), out.data(), todo, &aes, ivec, &num); |
| 369 | in = in.subspan(todo); |
| 370 | out = out.subspan(todo); |
| 371 | } while (!in.empty()); |
| 372 | } |
| 373 | EXPECT_EQ(Bytes(expected), Bytes(result)); |
| 374 | } |
| 375 | } |
| 376 |
| 377 | static void TestCipher(const EVP_CIPHER *cipher, Operation input_op, |
| 378 | bool padding, bssl::Span<const uint8_t> key, |
| 379 | bssl::Span<const uint8_t> iv, |
| 380 | bssl::Span<const uint8_t> plaintext, |
| 381 | bssl::Span<const uint8_t> ciphertext, |
| 382 | bssl::Span<const uint8_t> aad, |
| 383 | bssl::Span<const uint8_t> tag) { |
| 384 | size_t block_size = EVP_CIPHER_block_size(cipher); |
| 385 | std::vector<Operation> ops; |
| 386 | if (input_op == Operation::kBoth) { |
| 387 | ops = {Operation::kEncrypt, Operation::kDecrypt}; |
| 388 | } else { |
| 389 | ops = {input_op}; |
| 390 | } |
| 391 | for (Operation op : ops) { |
| 392 | SCOPED_TRACE(OperationToString(op)); |
| 393 | // Zero indicates a single-shot API. |
| 394 | static const size_t kChunkSizes[] = {0, 1, 2, 5, 7, 8, 9, 15, 16, |
| 395 | 17, 31, 32, 33, 63, 64, 65, 512}; |
| 396 | for (size_t chunk_size : kChunkSizes) { |
| 397 | SCOPED_TRACE(chunk_size); |
| 398 | if (chunk_size > plaintext.size() && chunk_size > ciphertext.size() && |
| 399 | chunk_size > aad.size()) { |
| 400 | continue; |
| 401 | } |
| 402 | for (bool in_place : {false, true}) { |
| 403 | SCOPED_TRACE(in_place); |
| 404 | for (bool copy : {false, true}) { |
| 405 | SCOPED_TRACE(copy); |
| 406 | TestCipherAPI(cipher, op, padding, copy, in_place, |
| 407 | /*use_evp_cipher=*/false, chunk_size, key, iv, |
| 408 | plaintext, ciphertext, aad, tag); |
| 409 | if (!padding && chunk_size % block_size == 0) { |
| 410 | TestCipherAPI(cipher, op, padding, copy, in_place, |
| 411 | /*use_evp_cipher=*/true, chunk_size, key, iv, |
| 412 | plaintext, ciphertext, aad, tag); |
| 413 | } |
| 414 | } |
| 415 | if (!padding) { |
| 416 | TestLowLevelAPI(cipher, op, in_place, chunk_size, key, iv, plaintext, |
| 417 | ciphertext); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | } |
| 422 | } |
| 423 |
| 424 | static void CipherFileTest(FileTest *t) { |
| 425 | std::string cipher_str; |
| 426 | ASSERT_TRUE(t->GetAttribute(&cipher_str, "Cipher")); |
| 427 | const EVP_CIPHER *cipher = GetCipher(cipher_str); |
| 428 | ASSERT_TRUE(cipher); |
| 429 |
| 430 | std::vector<uint8_t> key, iv, plaintext, ciphertext, aad, tag; |
| 431 | ASSERT_TRUE(t->GetBytes(&key, "Key")); |
| 432 | ASSERT_TRUE(t->GetBytes(&plaintext, "Plaintext")); |
| 433 | ASSERT_TRUE(t->GetBytes(&ciphertext, "Ciphertext")); |
| 434 | if (EVP_CIPHER_iv_length(cipher) > 0) { |
| 435 | ASSERT_TRUE(t->GetBytes(&iv, "IV")); |
| 436 | } |
| 437 | if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE) { |
| 438 | ASSERT_TRUE(t->GetBytes(&aad, "AAD")); |
| 439 | ASSERT_TRUE(t->GetBytes(&tag, "Tag")); |
| 440 | } |
| 441 |
| 442 | Operation op = Operation::kBoth; |
| 443 | if (t->HasAttribute("Operation")) { |
| 444 | const std::string &str = t->GetAttributeOrDie("Operation"); |
| 445 | if (str == "Encrypt" || str == "ENCRYPT") { |
| 446 | op = Operation::kEncrypt; |
| 447 | } else if (str == "Decrypt" || str == "DECRYPT") { |
| 448 | op = Operation::kDecrypt; |
| 449 | } else if (str == "InvalidDecrypt") { |
| 450 | op = Operation::kInvalidDecrypt; |
| 451 | } else { |
| 452 | FAIL() << "Unknown operation: " << str; |
| 453 | } |
| 454 | } |
| 455 |
| 456 | TestCipher(cipher, op, /*padding=*/false, key, iv, plaintext, ciphertext, aad, |
| 457 | tag); |
| 458 | } |
| 459 |
| 460 | TEST(CipherTest, TestVectors) { |
| 461 | FileTestGTest("crypto/cipher/test/cipher_tests.txt", CipherFileTest); |
| 462 | } |
| 463 |
| 464 | TEST(CipherTest, CAVP_AES_128_CBC) { |
| 465 | FileTestGTest("crypto/cipher/test/nist_cavp/aes_128_cbc.txt", CipherFileTest); |
| 466 | } |
| 467 |
| 468 | TEST(CipherTest, CAVP_AES_128_CTR) { |
| 469 | FileTestGTest("crypto/cipher/test/nist_cavp/aes_128_ctr.txt", CipherFileTest); |
| 470 | } |
| 471 |
| 472 | TEST(CipherTest, CAVP_AES_192_CBC) { |
| 473 | FileTestGTest("crypto/cipher/test/nist_cavp/aes_192_cbc.txt", CipherFileTest); |
| 474 | } |
| 475 |
| 476 | TEST(CipherTest, CAVP_AES_192_CTR) { |
| 477 | FileTestGTest("crypto/cipher/test/nist_cavp/aes_192_ctr.txt", CipherFileTest); |
| 478 | } |
| 479 |
| 480 | TEST(CipherTest, CAVP_AES_256_CBC) { |
| 481 | FileTestGTest("crypto/cipher/test/nist_cavp/aes_256_cbc.txt", CipherFileTest); |
| 482 | } |
| 483 |
| 484 | TEST(CipherTest, CAVP_AES_256_CTR) { |
| 485 | FileTestGTest("crypto/cipher/test/nist_cavp/aes_256_ctr.txt", CipherFileTest); |
| 486 | } |
| 487 |
| 488 | TEST(CipherTest, CAVP_TDES_CBC) { |
| 489 | FileTestGTest("crypto/cipher/test/nist_cavp/tdes_cbc.txt", CipherFileTest); |
| 490 | } |
| 491 |
| 492 | TEST(CipherTest, CAVP_TDES_ECB) { |
| 493 | FileTestGTest("crypto/cipher/test/nist_cavp/tdes_ecb.txt", CipherFileTest); |
| 494 | } |
| 495 |
| 496 | TEST(CipherTest, WycheproofAESCBC) { |
| 497 | FileTestGTest("third_party/wycheproof_testvectors/aes_cbc_pkcs5_test.txt", |
| 498 | [](FileTest *t) { |
| 499 | t->IgnoreInstruction("type"); |
| 500 | t->IgnoreInstruction("ivSize"); |
| 501 |
| 502 | std::string key_size; |
| 503 | ASSERT_TRUE(t->GetInstruction(&key_size, "keySize")); |
| 504 | const EVP_CIPHER *cipher; |
| 505 | switch (atoi(key_size.c_str())) { |
| 506 | case 128: |
| 507 | cipher = EVP_aes_128_cbc(); |
| 508 | break; |
| 509 | case 192: |
| 510 | cipher = EVP_aes_192_cbc(); |
| 511 | break; |
| 512 | case 256: |
| 513 | cipher = EVP_aes_256_cbc(); |
| 514 | break; |
| 515 | default: |
| 516 | FAIL() << "Unsupported key size: " << key_size; |
| 517 | } |
| 518 |
| 519 | std::vector<uint8_t> key, iv, msg, ct; |
| 520 | ASSERT_TRUE(t->GetBytes(&key, "key")); |
| 521 | ASSERT_TRUE(t->GetBytes(&iv, "iv")); |
| 522 | ASSERT_TRUE(t->GetBytes(&msg, "msg")); |
| 523 | ASSERT_TRUE(t->GetBytes(&ct, "ct")); |
| 524 | WycheproofResult result; |
| 525 | ASSERT_TRUE(GetWycheproofResult(t, &result)); |
| 526 | TestCipher(cipher, |
| 527 | result.IsValid() ? Operation::kBoth |
| 528 | : Operation::kInvalidDecrypt, |
| 529 | /*padding=*/true, key, iv, msg, ct, /*aad=*/{}, |
| 530 | /*tag=*/{}); |
| 531 | }); |
| 532 | } |
| 533 |
| 534 | TEST(CipherTest, SHA1WithSecretSuffix) { |
| 535 | uint8_t buf[SHA_CBLOCK * 4]; |
| 536 | RAND_bytes(buf, sizeof(buf)); |
| 537 | // Hashing should run in time independent of the bytes. |
| 538 | CONSTTIME_SECRET(buf, sizeof(buf)); |
| 539 |
| 540 | // Exhaustively testing interesting cases in this function is cubic in the |
| 541 | // block size, so we test in 3-byte increments. |
| 542 | constexpr size_t kSkip = 3; |
| 543 | // This value should be less than 8 to test the edge case when the 8-byte |
| 544 | // length wraps to the next block. |
| 545 | static_assert(kSkip < 8, "kSkip is too large"); |
| 546 |
| 547 | // |EVP_sha1_final_with_secret_suffix| is sensitive to the public length of |
| 548 | // the partial block previously hashed. In TLS, this is the HMAC prefix, the |
| 549 | // header, and the public minimum padding length. |
| 550 | for (size_t prefix = 0; prefix < SHA_CBLOCK; prefix += kSkip) { |
| 551 | SCOPED_TRACE(prefix); |
| 552 | // The first block is treated differently, so we run with up to three |
| 553 | // blocks of length variability. |
| 554 | for (size_t max_len = 0; max_len < 3 * SHA_CBLOCK; max_len += kSkip) { |
| 555 | SCOPED_TRACE(max_len); |
| 556 | for (size_t len = 0; len <= max_len; len += kSkip) { |
| 557 | SCOPED_TRACE(len); |
| 558 |
| 559 | uint8_t expected[SHA_DIGEST_LENGTH]; |
| 560 | SHA1(buf, prefix + len, expected); |
| 561 | CONSTTIME_DECLASSIFY(expected, sizeof(expected)); |
| 562 |
| 563 | // Make a copy of the secret length to avoid interfering with the loop. |
| 564 | size_t secret_len = len; |
| 565 | CONSTTIME_SECRET(&secret_len, sizeof(secret_len)); |
| 566 |
| 567 | SHA_CTX ctx; |
| 568 | SHA1_Init(&ctx); |
| 569 | SHA1_Update(&ctx, buf, prefix); |
| 570 | uint8_t computed[SHA_DIGEST_LENGTH]; |
| 571 | ASSERT_TRUE(EVP_sha1_final_with_secret_suffix( |
| 572 | &ctx, computed, buf + prefix, secret_len, max_len)); |
| 573 |
| 574 | CONSTTIME_DECLASSIFY(computed, sizeof(computed)); |
| 575 | EXPECT_EQ(Bytes(expected), Bytes(computed)); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | } |
| 580 |
| 581 | TEST(CipherTest, SHA256WithSecretSuffix) { |
| 582 | uint8_t buf[SHA256_CBLOCK * 4]; |
| 583 | RAND_bytes(buf, sizeof(buf)); |
| 584 | // Hashing should run in time independent of the bytes. |
| 585 | CONSTTIME_SECRET(buf, sizeof(buf)); |
| 586 |
| 587 | // Exhaustively testing interesting cases in this function is cubic in the |
| 588 | // block size, so we test in 3-byte increments. |
| 589 | constexpr size_t kSkip = 3; |
| 590 | // This value should be less than 8 to test the edge case when the 8-byte |
| 591 | // length wraps to the next block. |
| 592 | static_assert(kSkip < 8, "kSkip is too large"); |
| 593 |
| 594 | // |EVP_sha256_final_with_secret_suffix| is sensitive to the public length of |
| 595 | // the partial block previously hashed. In TLS, this is the HMAC prefix, the |
| 596 | // header, and the public minimum padding length. |
| 597 | for (size_t prefix = 0; prefix < SHA256_CBLOCK; prefix += kSkip) { |
| 598 | SCOPED_TRACE(prefix); |
| 599 | // The first block is treated differently, so we run with up to three |
| 600 | // blocks of length variability. |
| 601 | for (size_t max_len = 0; max_len < 3 * SHA256_CBLOCK; max_len += kSkip) { |
| 602 | SCOPED_TRACE(max_len); |
| 603 | for (size_t len = 0; len <= max_len; len += kSkip) { |
| 604 | SCOPED_TRACE(len); |
| 605 |
| 606 | uint8_t expected[SHA256_DIGEST_LENGTH]; |
| 607 | SHA256(buf, prefix + len, expected); |
| 608 | CONSTTIME_DECLASSIFY(expected, sizeof(expected)); |
| 609 |
| 610 | // Make a copy of the secret length to avoid interfering with the loop. |
| 611 | size_t secret_len = len; |
| 612 | CONSTTIME_SECRET(&secret_len, sizeof(secret_len)); |
| 613 |
| 614 | SHA256_CTX ctx; |
| 615 | SHA256_Init(&ctx); |
| 616 | SHA256_Update(&ctx, buf, prefix); |
| 617 | uint8_t computed[SHA256_DIGEST_LENGTH]; |
| 618 | ASSERT_TRUE(EVP_sha256_final_with_secret_suffix( |
| 619 | &ctx, computed, buf + prefix, secret_len, max_len)); |
| 620 |
| 621 | CONSTTIME_DECLASSIFY(computed, sizeof(computed)); |
| 622 | EXPECT_EQ(Bytes(expected), Bytes(computed)); |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 |
| 628 | TEST(CipherTest, GetCipher) { |
| 629 | const EVP_CIPHER *cipher = EVP_get_cipherbynid(NID_aes_128_gcm); |
| 630 | ASSERT_TRUE(cipher); |
| 631 | EXPECT_EQ(NID_aes_128_gcm, EVP_CIPHER_nid(cipher)); |
| 632 |
| 633 | cipher = EVP_get_cipherbyname("aes-128-gcm"); |
| 634 | ASSERT_TRUE(cipher); |
| 635 | EXPECT_EQ(NID_aes_128_gcm, EVP_CIPHER_nid(cipher)); |
| 636 |
| 637 | cipher = EVP_get_cipherbyname("AES-128-GCM"); |
| 638 | ASSERT_TRUE(cipher); |
| 639 | EXPECT_EQ(NID_aes_128_gcm, EVP_CIPHER_nid(cipher)); |
| 640 |
| 641 | // We support a tcpdump-specific alias for 3DES. |
| 642 | cipher = EVP_get_cipherbyname("3des"); |
| 643 | ASSERT_TRUE(cipher); |
| 644 | EXPECT_EQ(NID_des_ede3_cbc, EVP_CIPHER_nid(cipher)); |
| 645 | } |
| 646 |
| 647 | // Test the AES-GCM EVP_CIPHER's internal IV management APIs. OpenSSH uses these |
| 648 | // APIs. |
| 649 | TEST(CipherTest, GCMIncrementingIV) { |
| 650 | const EVP_CIPHER *kCipher = EVP_aes_128_gcm(); |
| 651 | static const uint8_t kKey[16] = {0, 1, 2, 3, 4, 5, 6, 7, |
| 652 | 8, 9, 10, 11, 12, 13, 14, 15}; |
| 653 | static const uint8_t kInput[] = {'h', 'e', 'l', 'l', 'o'}; |
| 654 |
| 655 | auto expect_iv = [&](EVP_CIPHER_CTX *ctx, bssl::Span<const uint8_t> iv, |
| 656 | bool enc) { |
| 657 | // Make a reference ciphertext. |
| 658 | bssl::ScopedEVP_CIPHER_CTX ref; |
| 659 | ASSERT_TRUE(EVP_EncryptInit_ex(ref.get(), kCipher, /*impl=*/nullptr, |
| 660 | kKey, /*iv=*/nullptr)); |
| 661 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ref.get(), EVP_CTRL_AEAD_SET_IVLEN, |
| 662 | static_cast<int>(iv.size()), nullptr)); |
| 663 | ASSERT_TRUE(EVP_EncryptInit_ex(ref.get(), /*cipher=*/nullptr, |
| 664 | /*impl=*/nullptr, /*key=*/nullptr, |
| 665 | iv.data())); |
| 666 | uint8_t ciphertext[sizeof(kInput)]; |
| 667 | int ciphertext_len; |
| 668 | ASSERT_TRUE(EVP_EncryptUpdate(ref.get(), ciphertext, &ciphertext_len, |
| 669 | kInput, sizeof(kInput))); |
| 670 | int extra_len; |
| 671 | ASSERT_TRUE(EVP_EncryptFinal_ex(ref.get(), nullptr, &extra_len)); |
| 672 | ASSERT_EQ(extra_len, 0); |
| 673 | uint8_t tag[16]; |
| 674 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ref.get(), EVP_CTRL_AEAD_GET_TAG, |
| 675 | sizeof(tag), tag)); |
| 676 |
| 677 | if (enc) { |
| 678 | uint8_t actual[sizeof(kInput)]; |
| 679 | int actual_len; |
| 680 | ASSERT_TRUE( |
| 681 | EVP_EncryptUpdate(ctx, actual, &actual_len, kInput, sizeof(kInput))); |
| 682 | ASSERT_TRUE(EVP_EncryptFinal_ex(ctx, nullptr, &extra_len)); |
| 683 | ASSERT_EQ(extra_len, 0); |
| 684 | EXPECT_EQ(Bytes(actual, actual_len), Bytes(ciphertext, ciphertext_len)); |
| 685 | uint8_t actual_tag[16]; |
| 686 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, |
| 687 | sizeof(actual_tag), actual_tag)); |
| 688 | EXPECT_EQ(Bytes(actual_tag), Bytes(tag)); |
| 689 | } else { |
| 690 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, sizeof(tag), |
| 691 | const_cast<uint8_t *>(tag))); |
| 692 | uint8_t actual[sizeof(kInput)]; |
| 693 | int actual_len; |
| 694 | ASSERT_TRUE(EVP_DecryptUpdate(ctx, actual, &actual_len, ciphertext, |
| 695 | sizeof(ciphertext))); |
| 696 | ASSERT_TRUE(EVP_DecryptFinal_ex(ctx, nullptr, &extra_len)); |
| 697 | ASSERT_EQ(extra_len, 0); |
| 698 | EXPECT_EQ(Bytes(actual, actual_len), Bytes(kInput)); |
| 699 | } |
| 700 | }; |
| 701 |
| 702 | { |
| 703 | // Passing in a fixed IV length of -1 sets the whole IV, but then configures |
| 704 | // |EVP_CIPHER_CTX| to increment the bottom 8 bytes of the IV. |
| 705 | static const uint8_t kIV1[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; |
| 706 | static const uint8_t kIV2[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13}; |
| 707 | static const uint8_t kIV3[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14}; |
| 708 | static const uint8_t kIV4[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15}; |
| 709 |
| 710 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 711 | ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 712 | /*iv=*/nullptr)); |
| 713 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, -1, |
| 714 | const_cast<uint8_t *>(kIV1))); |
| 715 |
| 716 | // EVP_CTRL_GCM_IV_GEN both configures and returns the IV. |
| 717 | uint8_t iv[12]; |
| 718 | ASSERT_TRUE( |
| 719 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 720 | EXPECT_EQ(Bytes(iv), Bytes(kIV1)); |
| 721 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV1, /*enc=*/true)); |
| 722 |
| 723 | // Continuing to run EVP_CTRL_GCM_IV_GEN should increment the IV. |
| 724 | ASSERT_TRUE( |
| 725 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 726 | EXPECT_EQ(Bytes(iv), Bytes(kIV2)); |
| 727 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV2, /*enc=*/true)); |
| 728 |
| 729 | // Passing in a shorter length outputs the suffix portion. |
| 730 | uint8_t suffix[8]; |
| 731 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, |
| 732 | sizeof(suffix), suffix)); |
| 733 | EXPECT_EQ(Bytes(suffix), Bytes(bssl::Span(kIV3).last(sizeof(suffix)))); |
| 734 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV3, /*enc=*/true)); |
| 735 |
| 736 | // A length of -1 returns the whole IV. |
| 737 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, -1, iv)); |
| 738 | EXPECT_EQ(Bytes(iv), Bytes(kIV4)); |
| 739 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV4, /*enc=*/true)); |
| 740 | } |
| 741 |
| 742 | { |
| 743 | // Similar to the above, but for decrypting. |
| 744 | static const uint8_t kIV1[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; |
| 745 | static const uint8_t kIV2[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13}; |
| 746 |
| 747 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 748 | ASSERT_TRUE(EVP_DecryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 749 | /*iv=*/nullptr)); |
| 750 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, -1, |
| 751 | const_cast<uint8_t *>(kIV1))); |
| 752 |
| 753 | uint8_t iv[12]; |
| 754 | ASSERT_TRUE( |
| 755 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 756 | EXPECT_EQ(Bytes(iv), Bytes(kIV1)); |
| 757 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV1, /*enc=*/false)); |
| 758 |
| 759 | ASSERT_TRUE( |
| 760 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 761 | EXPECT_EQ(Bytes(iv), Bytes(kIV2)); |
| 762 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV2, /*enc=*/false)); |
| 763 | } |
| 764 |
| 765 | { |
| 766 | // Test that only the bottom 8 bytes are used as a counter. |
| 767 | static const uint8_t kIV1[12] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 768 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
| 769 | static const uint8_t kIV2[12] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 770 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
| 771 | static const uint8_t kIV3[12] = {0xff, 0xff, 0xff, 0xff, 0x00, 0x00, |
| 772 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}; |
| 773 |
| 774 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 775 | ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 776 | /*iv=*/nullptr)); |
| 777 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, -1, |
| 778 | const_cast<uint8_t *>(kIV1))); |
| 779 |
| 780 | uint8_t iv[12]; |
| 781 | ASSERT_TRUE( |
| 782 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 783 | EXPECT_EQ(Bytes(iv), Bytes(kIV1)); |
| 784 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV1, /*enc=*/true)); |
| 785 |
| 786 | ASSERT_TRUE( |
| 787 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 788 | EXPECT_EQ(Bytes(iv), Bytes(kIV2)); |
| 789 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV2, /*enc=*/true)); |
| 790 |
| 791 | ASSERT_TRUE( |
| 792 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 793 | EXPECT_EQ(Bytes(iv), Bytes(kIV3)); |
| 794 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV3, /*enc=*/true)); |
| 795 | } |
| 796 |
| 797 | { |
| 798 | // Test with a longer IV length. |
| 799 | static const uint8_t kIV1[16] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 800 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 801 | 0xff, 0xff, 0xff, 0xff}; |
| 802 | static const uint8_t kIV2[16] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 803 | 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
| 804 | 0x00, 0x00, 0x00, 0x00}; |
| 805 | static const uint8_t kIV3[16] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| 806 | 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, |
| 807 | 0x00, 0x00, 0x00, 0x01}; |
| 808 |
| 809 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 810 | ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 811 | /*iv=*/nullptr)); |
| 812 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IVLEN, |
| 813 | sizeof(kIV1), nullptr)); |
| 814 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, -1, |
| 815 | const_cast<uint8_t *>(kIV1))); |
| 816 |
| 817 | uint8_t iv[16]; |
| 818 | ASSERT_TRUE( |
| 819 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 820 | EXPECT_EQ(Bytes(iv), Bytes(kIV1)); |
| 821 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV1, /*enc=*/true)); |
| 822 |
| 823 | ASSERT_TRUE( |
| 824 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 825 | EXPECT_EQ(Bytes(iv), Bytes(kIV2)); |
| 826 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV2, /*enc=*/true)); |
| 827 |
| 828 | ASSERT_TRUE( |
| 829 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, sizeof(iv), iv)); |
| 830 | EXPECT_EQ(Bytes(iv), Bytes(kIV3)); |
| 831 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV3, /*enc=*/true)); |
| 832 | } |
| 833 |
| 834 | { |
| 835 | // When decrypting, callers are expected to configure the fixed half and |
| 836 | // invocation half separately. The two will get stitched together into the |
| 837 | // final IV. |
| 838 | const uint8_t kIV[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; |
| 839 |
| 840 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 841 | ASSERT_TRUE(EVP_DecryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 842 | /*iv=*/nullptr)); |
| 843 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 4, |
| 844 | const_cast<uint8_t *>(kIV))); |
| 845 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IV_INV, 8, |
| 846 | const_cast<uint8_t *>(kIV + 4))); |
| 847 | // EVP_CTRL_GCM_SET_IV_INV is sufficient to configure the IV. There is no |
| 848 | // need to call EVP_CTRL_GCM_IV_GEN. |
| 849 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV, /*enc=*/false)); |
| 850 | } |
| 851 |
| 852 | { |
| 853 | // Stitching together a decryption IV that exceeds the standard IV length. |
| 854 | const uint8_t kIV[16] = {1, 2, 3, 4, 5, 6, 7, 8, |
| 855 | 9, 10, 11, 12, 13, 14, 15, 16}; |
| 856 |
| 857 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 858 | ASSERT_TRUE(EVP_DecryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 859 | /*iv=*/nullptr)); |
| 860 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IVLEN, |
| 861 | sizeof(kIV), nullptr)); |
| 862 |
| 863 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 4, |
| 864 | const_cast<uint8_t *>(kIV))); |
| 865 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_SET_IV_INV, 12, |
| 866 | const_cast<uint8_t *>(kIV + 4))); |
| 867 | // EVP_CTRL_GCM_SET_IV_INV is sufficient to configure the IV. There is no |
| 868 | // need to call EVP_CTRL_GCM_IV_GEN. |
| 869 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), kIV, /*enc=*/false)); |
| 870 | } |
| 871 |
| 872 | { |
| 873 | // Fixed IVs must be at least 4 bytes and admit at least an 8 byte counter. |
| 874 | const uint8_t kIV[16] = {1, 2, 3, 4, 5, 6, 7, 8, |
| 875 | 9, 10, 11, 12, 13, 14, 15, 16}; |
| 876 |
| 877 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 878 | ASSERT_TRUE(EVP_DecryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 879 | /*iv=*/nullptr)); |
| 880 |
| 881 | // This means the default IV length only allows a 4/8 split. |
| 882 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 0, |
| 883 | const_cast<uint8_t *>(kIV))); |
| 884 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 3, |
| 885 | const_cast<uint8_t *>(kIV))); |
| 886 | EXPECT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 4, |
| 887 | const_cast<uint8_t *>(kIV))); |
| 888 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 5, |
| 889 | const_cast<uint8_t *>(kIV))); |
| 890 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 16, |
| 891 | const_cast<uint8_t *>(kIV))); |
| 892 |
| 893 | // A longer IV allows a wider range. |
| 894 | ASSERT_TRUE( |
| 895 | EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IVLEN, 16, nullptr)); |
| 896 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 0, |
| 897 | const_cast<uint8_t *>(kIV))); |
| 898 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 3, |
| 899 | const_cast<uint8_t *>(kIV))); |
| 900 | EXPECT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 4, |
| 901 | const_cast<uint8_t *>(kIV))); |
| 902 | EXPECT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 6, |
| 903 | const_cast<uint8_t *>(kIV))); |
| 904 | EXPECT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 8, |
| 905 | const_cast<uint8_t *>(kIV))); |
| 906 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 9, |
| 907 | const_cast<uint8_t *>(kIV))); |
| 908 | EXPECT_FALSE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, 16, |
| 909 | const_cast<uint8_t *>(kIV))); |
| 910 | } |
| 911 |
| 912 | { |
| 913 | // When encrypting, setting a fixed IV randomizes the counter portion. |
| 914 | const uint8_t kFixedIV[4] = {1, 2, 3, 4}; |
| 915 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 916 | ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 917 | /*iv=*/nullptr)); |
| 918 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, |
| 919 | sizeof(kFixedIV), |
| 920 | const_cast<uint8_t *>(kFixedIV))); |
| 921 | uint8_t counter[8]; |
| 922 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, |
| 923 | sizeof(counter), counter)); |
| 924 |
| 925 | uint8_t iv[12]; |
| 926 | memcpy(iv, kFixedIV, sizeof(kFixedIV)); |
| 927 | memcpy(iv + sizeof(kFixedIV), counter, sizeof(counter)); |
| 928 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), iv, /*enc=*/true)); |
| 929 |
| 930 | // The counter continues to act as a counter. |
| 931 | uint8_t counter2[8]; |
| 932 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, |
| 933 | sizeof(counter2), counter2)); |
| 934 | EXPECT_EQ(CRYPTO_load_u64_be(counter2), CRYPTO_load_u64_be(counter) + 1); |
| 935 | memcpy(iv + sizeof(kFixedIV), counter2, sizeof(counter2)); |
| 936 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), iv, /*enc=*/true)); |
| 937 | } |
| 938 |
| 939 | { |
| 940 | // Same as above, but with a larger IV. |
| 941 | const uint8_t kFixedIV[8] = {1, 2, 3, 4, 5, 6, 7, 8}; |
| 942 | bssl::ScopedEVP_CIPHER_CTX ctx; |
| 943 | ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), kCipher, /*impl=*/nullptr, kKey, |
| 944 | /*iv=*/nullptr)); |
| 945 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IVLEN, |
| 946 | sizeof(kFixedIV) + 8, nullptr)); |
| 947 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_AEAD_SET_IV_FIXED, |
| 948 | sizeof(kFixedIV), |
| 949 | const_cast<uint8_t *>(kFixedIV))); |
| 950 | uint8_t counter[8]; |
| 951 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, |
| 952 | sizeof(counter), counter)); |
| 953 |
| 954 | uint8_t iv[16]; |
| 955 | memcpy(iv, kFixedIV, sizeof(kFixedIV)); |
| 956 | memcpy(iv + sizeof(kFixedIV), counter, sizeof(counter)); |
| 957 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), iv, /*enc=*/true)); |
| 958 |
| 959 | // The counter continues to act as a counter. |
| 960 | uint8_t counter2[8]; |
| 961 | ASSERT_TRUE(EVP_CIPHER_CTX_ctrl(ctx.get(), EVP_CTRL_GCM_IV_GEN, |
| 962 | sizeof(counter2), counter2)); |
| 963 | EXPECT_EQ(CRYPTO_load_u64_be(counter2), CRYPTO_load_u64_be(counter) + 1); |
| 964 | memcpy(iv + sizeof(kFixedIV), counter2, sizeof(counter2)); |
| 965 | ASSERT_NO_FATAL_FAILURE(expect_iv(ctx.get(), iv, /*enc=*/true)); |
| 966 | } |
| 967 | } |
| 968 |