Go to the documentation of this file.
62 #include <gtest/gtest.h>
72 #include "../test/file_test.h"
73 #include "../test/test_util.h"
74 #include "../test/wycheproof_util.h"
79 if (
name ==
"DES-CBC") {
81 }
else if (
name ==
"DES-ECB") {
83 }
else if (
name ==
"DES-EDE") {
85 }
else if (
name ==
"DES-EDE3") {
87 }
else if (
name ==
"DES-EDE-CBC") {
89 }
else if (
name ==
"DES-EDE3-CBC") {
91 }
else if (
name ==
"RC4") {
93 }
else if (
name ==
"AES-128-ECB") {
95 }
else if (
name ==
"AES-256-ECB") {
97 }
else if (
name ==
"AES-128-CBC") {
99 }
else if (
name ==
"AES-128-GCM") {
101 }
else if (
name ==
"AES-128-OFB") {
103 }
else if (
name ==
"AES-192-CBC") {
105 }
else if (
name ==
"AES-192-CTR") {
107 }
else if (
name ==
"AES-192-ECB") {
109 }
else if (
name ==
"AES-192-OFB") {
111 }
else if (
name ==
"AES-256-CBC") {
113 }
else if (
name ==
"AES-128-CTR") {
115 }
else if (
name ==
"AES-256-CTR") {
117 }
else if (
name ==
"AES-256-GCM") {
119 }
else if (
name ==
"AES-256-OFB") {
126 bssl::Span<const uint8_t>
in,
size_t chunk,
128 size_t max_out =
in.size();
132 max_out += block_size - (max_out % block_size);
134 out->resize(max_out);
142 while (!
in.empty()) {
146 static_cast<int>(
todo))) {
163 bool copy,
bool in_place,
size_t chunk_size,
164 const std::vector<uint8_t> &
key,
165 const std::vector<uint8_t> &iv,
168 const std::vector<uint8_t> &aad,
169 const std::vector<uint8_t> &
tag) {
170 const std::vector<uint8_t> *
in, *
out;
181 bssl::ScopedEVP_CIPHER_CTX ctx1;
184 if (t->HasAttribute(
"IV")) {
193 bssl::ScopedEVP_CIPHER_CTX
ctx2;
200 if (is_aead && !encrypt) {
219 std::vector<uint8_t>
result;
222 if (encrypt && is_aead) {
240 if (is_ctr || is_cbc || is_ofb) {
242 if (encrypt || !is_cbc) {
249 bssl::Span<const uint8_t>
input = *
in;
261 ASSERT_EQ(iv.size(),
size_t{AES_BLOCK_SIZE});
268 if (chunk_size == 0) {
270 ivec, ecount_buf, &
num);
278 }
while (!
input.empty());
283 if (chunk_size == 0) {
293 }
while (!
input.empty());
298 if (chunk_size == 0) {
308 }
while (!
input.empty());
318 ASSERT_TRUE(t->GetAttribute(&cipher_str,
"Cipher"));
339 if (t->HasAttribute(
"Operation")) {
341 if (
str ==
"ENCRYPT") {
342 operation = kEncrypt;
343 }
else if (
str ==
"DECRYPT") {
344 operation = kDecrypt;
346 FAIL() <<
"Unknown operation: " <<
str;
350 const std::vector<size_t> chunk_sizes = {0, 1, 2, 5, 7, 8, 9, 15, 16,
351 17, 31, 32, 33, 63, 64, 65, 512};
353 for (
size_t chunk_size : chunk_sizes) {
355 for (
bool copy : {
false,
true}) {
357 for (
bool in_place : {
false,
true}) {
361 if (operation != kDecrypt) {
367 if (operation != kEncrypt) {
377 TEST(CipherTest, TestVectors) {
381 TEST(CipherTest, CAVP_AES_128_CBC) {
382 FileTestGTest(
"crypto/cipher_extra/test/nist_cavp/aes_128_cbc.txt",
386 TEST(CipherTest, CAVP_AES_128_CTR) {
387 FileTestGTest(
"crypto/cipher_extra/test/nist_cavp/aes_128_ctr.txt",
391 TEST(CipherTest, CAVP_AES_192_CBC) {
392 FileTestGTest(
"crypto/cipher_extra/test/nist_cavp/aes_192_cbc.txt",
396 TEST(CipherTest, CAVP_AES_192_CTR) {
397 FileTestGTest(
"crypto/cipher_extra/test/nist_cavp/aes_192_ctr.txt",
401 TEST(CipherTest, CAVP_AES_256_CBC) {
402 FileTestGTest(
"crypto/cipher_extra/test/nist_cavp/aes_256_cbc.txt",
406 TEST(CipherTest, CAVP_AES_256_CTR) {
407 FileTestGTest(
"crypto/cipher_extra/test/nist_cavp/aes_256_ctr.txt",
411 TEST(CipherTest, CAVP_TDES_CBC) {
415 TEST(CipherTest, CAVP_TDES_ECB) {
419 TEST(CipherTest, WycheproofAESCBC) {
421 "third_party/wycheproof_testvectors/aes_cbc_pkcs5_test.txt",
423 t->IgnoreInstruction(
"type");
424 t->IgnoreInstruction(
"ivSize");
427 ASSERT_TRUE(t->GetInstruction(&key_size,
"keySize"));
429 switch (atoi(key_size.c_str())) {
440 FAIL() <<
"Unsupported key size: " << key_size;
443 std::vector<uint8_t>
key, iv,
msg, ct;
453 bssl::ScopedEVP_CIPHER_CTX
ctx;
454 std::vector<uint8_t>
out;
455 const std::vector<size_t> chunk_sizes = {
456 0, 1, 2, 5, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 512};
457 for (
size_t chunk : chunk_sizes) {
459 for (
bool in_place : {
false,
true}) {
463 key.data(), iv.data()));
468 key.data(), iv.data()));
473 key.data(), iv.data()));
481 TEST(CipherTest, SHA1WithSecretSuffix) {
489 constexpr
size_t kSkip = 3;
492 static_assert(kSkip < 8,
"kSkip is too large");
501 for (
size_t max_len = 0; max_len < 3 *
SHA_CBLOCK; max_len += kSkip) {
503 for (
size_t len = 0;
len <= max_len;
len += kSkip) {
511 size_t secret_len =
len;
#define EVP_get_cipherbyname
#define EVP_CIPHER_CTX_copy
#define EXPECT_FALSE(condition)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_192_ofb(void)
#define AES_set_encrypt_key
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_192_ecb(void)
#define AES_ofb128_encrypt
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_256_ecb(void)
const OPENSSL_EXPORT EVP_CIPHER * EVP_des_cbc(void)
const OPENSSL_EXPORT EVP_CIPHER * EVP_des_ede3(void)
static void TestOperation(FileTest *t, const EVP_CIPHER *cipher, bool encrypt, bool copy, bool in_place, size_t chunk_size, const std::vector< uint8_t > &key, const std::vector< uint8_t > &iv, const std::vector< uint8_t > &plaintext, const std::vector< uint8_t > &ciphertext, const std::vector< uint8_t > &aad, const std::vector< uint8_t > &tag)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_256_cbc(void)
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_192_cbc(void)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_128_ctr(void)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_128_gcm(void)
#define EVP_CIPHER_iv_length
static struct test_ctx ctx
#define CONSTTIME_DECLASSIFY(x, y)
#define ASSERT_LE(val1, val2)
#define EXPECT_LE(val1, val2)
#define EVP_CIPHER_CTX_ctrl
static bool DoCipher(EVP_CIPHER_CTX *ctx, std::vector< uint8_t > *out, bssl::Span< const uint8_t > in, size_t chunk, bool in_place)
#define EVP_CIPHER_CTX_iv_length
const OPENSSL_EXPORT EVP_CIPHER * EVP_des_ecb(void)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_192_ctr(void)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_256_ctr(void)
#define EVP_DecryptInit_ex
#define EVP_CTRL_AEAD_SET_IVLEN
void FileTestGTest(const char *path, std::function< void(FileTest *)> run_test)
#define SCOPED_TRACE(message)
#define AES_ctr128_encrypt
TEST(CipherTest, TestVectors)
static void * tag(intptr_t t)
#define EVP_CIPHER_CTX_set_padding
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_128_ecb(void)
#define EVP_CIPHER_CTX_block_size
#define NID_aes_192_ofb128
#define EVP_CIPHER_CTX_flags
static void * OPENSSL_memcpy(void *dst, const void *src, size_t n)
#define EVP_CIPHER_CTX_set_key_length
bool GetWycheproofResult(FileTest *t, WycheproofResult *out)
#define EVP_CTRL_AEAD_GET_TAG
const OPENSSL_EXPORT EVP_CIPHER * EVP_des_ede_cbc(void)
#define EVP_sha1_final_with_secret_suffix
static void TestCipher(FileTest *t)
const OPENSSL_EXPORT EVP_CIPHER * EVP_des_ede3_cbc(void)
#define NID_aes_256_ofb128
#define EVP_CTRL_AEAD_SET_TAG
#define EVP_EncryptInit_ex
static struct echo_ctx ctx2
#define AES_set_decrypt_key
const OPENSSL_EXPORT EVP_CIPHER * EVP_des_ede(void)
#define EVP_CipherFinal_ex
#define EVP_CIPH_NO_PADDING
#define EVP_CIPHER_CTX_encrypting
#define EVP_CipherInit_ex
#define NID_aes_128_ofb128
static const char prefix[]
#define ASSERT_TRUE(condition)
#define EVP_CIPHER_key_length
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_256_gcm(void)
#define SHA_DIGEST_LENGTH
#define EXPECT_GE(val1, val2)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_128_ofb(void)
#define CONSTTIME_SECRET(x, y)
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_256_ofb(void)
#define EVP_get_cipherbynid
const OPENSSL_EXPORT EVP_CIPHER * EVP_aes_128_cbc(void)
static const EVP_CIPHER * GetCipher(const std::string &name)
constexpr Span< T > MakeSpan(T *ptr, size_t size) noexcept
#define ASSERT_EQ(val1, val2)
#define EVP_CIPH_GCM_MODE
constexpr Span< const T > MakeConstSpan(T *ptr, size_t size) noexcept
grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:45