cast_test.cc
Go to the documentation of this file.
1 // Copyright (c) 2019, 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 <openssl/cipher.h>
16 
17 #include <gtest/gtest.h>
18 
19 #include "../../crypto/internal.h"
20 #include "../../crypto/test/test_util.h"
21 
22 struct CastTestCase {
23  uint8_t key[16];
25  uint8_t iv[8];
28 };
29 
30 static const CastTestCase kTests[] = {
31  // Randomly generated test cases. Checked against vanilla OpenSSL.
32  {
33  {0xbb, 0x56, 0xb1, 0x27, 0x7c, 0x4c, 0xdd, 0x5a, 0x99, 0x90, 0x1e, 0x6f,
34  0xeb, 0x36, 0x6c, 0xf3},
35  {0xa6, 0x5b, 0xe0, 0x99, 0xad, 0x5d, 0x91, 0x98, 0x37, 0xc1, 0xa4, 0x7f,
36  0x01, 0x24, 0x9a, 0x6b},
37  {0xd5, 0x8a, 0x5c, 0x29, 0xeb, 0xee, 0xed, 0x76},
38  {0x01, 0x8d, 0x1b, 0x42, 0xb8, 0x77, 0xc8, 0x84, 0x25, 0x7d, 0xd4, 0x89,
39  0x8d, 0xc1, 0xbc, 0x2a},
40  {0xc1, 0x05, 0xa1, 0x9a, 0xb4, 0xc4, 0xd0, 0x15,
41  0x9d, 0xfd, 0xea, 0xd0, 0xc3, 0x54, 0xe5, 0x33,
42  0x26, 0xac, 0x25, 0xf3, 0x48, 0xbc, 0xf6, 0xa2},
43  },
44  {
45  {0x5d, 0x98, 0xa9, 0xd2, 0x27, 0x5d, 0xc8, 0x8c, 0x8c, 0xee, 0x23, 0x7f,
46  0x8e, 0x2b, 0xd4, 0x8d},
47  {0x60, 0xec, 0x31, 0xda, 0x25, 0x07, 0x02, 0x14, 0x84, 0x44, 0x96, 0xa6,
48  0x04, 0x81, 0xca, 0x4e},
49  {0x96, 0x4c, 0xa4, 0x07, 0xee, 0x1c, 0xd1, 0xfb},
50  {0x58, 0x62, 0x29, 0x62, 0x23, 0x69, 0x9e, 0xe8, 0x27, 0xc2, 0xcd, 0x5b,
51  0x35, 0xf1, 0xdf, 0xa4},
52  {0x1c, 0xd0, 0x29, 0xe5, 0xf3, 0xdb, 0x65, 0x60,
53  0x05, 0xde, 0x01, 0x2b, 0x10, 0x09, 0x44, 0x56,
54  0x59, 0x44, 0x00, 0x26, 0xdb, 0xb3, 0x2d, 0x98},
55  },
56 };
57 
58 TEST(CAST, ECB) {
59  unsigned test_num = 0;
60  for (const auto &test : kTests) {
61  test_num++;
62  SCOPED_TRACE(test_num);
63 
64  uint8_t out[sizeof(test.ecb_ciphertext)];
65  int out_bytes, final_bytes;
66 
67  bssl::ScopedEVP_CIPHER_CTX ctx;
69  test.key, nullptr));
70  ASSERT_TRUE(EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding */));
71  ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
72  sizeof(test.plaintext)));
73  ASSERT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
74  EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
75  sizeof(test.plaintext));
76  EXPECT_EQ(Bytes(test.ecb_ciphertext), Bytes(out));
77 
78  bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
79  ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_cast5_ecb(), nullptr,
80  test.key, nullptr));
82  EVP_CIPHER_CTX_set_padding(decrypt_ctx.get(), 0 /* no padding */));
83  ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
84  test.ecb_ciphertext,
85  sizeof(test.ecb_ciphertext)));
87  EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
88  EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
89  sizeof(test.plaintext));
90  EXPECT_EQ(Bytes(test.plaintext), Bytes(out));
91  }
92 }
93 
94 TEST(CAST, CBC) {
95  unsigned test_num = 0;
96  for (const auto &test : kTests) {
97  test_num++;
98  SCOPED_TRACE(test_num);
99 
100  uint8_t out[sizeof(test.cbc_ciphertext)];
101  int out_bytes, final_bytes;
102 
103  bssl::ScopedEVP_CIPHER_CTX ctx;
105  test.key, test.iv));
106  ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
107  sizeof(test.plaintext)));
108  EXPECT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
109  EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
110  sizeof(test.cbc_ciphertext));
111  EXPECT_EQ(Bytes(test.cbc_ciphertext), Bytes(out));
112 
113  bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
114  ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_cast5_cbc(), nullptr,
115  test.key, test.iv));
116  ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
117  test.cbc_ciphertext,
118  sizeof(test.cbc_ciphertext)));
119  EXPECT_TRUE(
120  EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
121  EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
122  sizeof(test.plaintext));
123  EXPECT_EQ(Bytes(test.plaintext), Bytes(out, out_bytes + final_bytes));
124  }
125 }
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
Bytes
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:38
ctx
Definition: benchmark-async.c:30
test
Definition: spinlock_test.cc:36
EVP_EncryptUpdate
#define EVP_EncryptUpdate
Definition: boringssl_prefix_symbols.h:1532
ctx
static struct test_ctx ctx
Definition: test-ipc-send-recv.c:65
CastTestCase::iv
uint8_t iv[8]
Definition: cast_test.cc:25
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
CAST
#define CAST(p)
Definition: linux-inotify.c:47
EVP_DecryptInit_ex
#define EVP_DecryptInit_ex
Definition: boringssl_prefix_symbols.h:1504
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
SCOPED_TRACE
#define SCOPED_TRACE(message)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2264
TEST
TEST(CAST, ECB)
Definition: cast_test.cc:58
EVP_cast5_ecb
const EVP_CIPHER * EVP_cast5_ecb(void)
Definition: cast.c:456
EVP_CIPHER_CTX_set_padding
#define EVP_CIPHER_CTX_set_padding
Definition: boringssl_prefix_symbols.h:1482
cipher.h
CastTestCase::ecb_ciphertext
uint8_t ecb_ciphertext[16]
Definition: cast_test.cc:26
EVP_EncryptInit_ex
#define EVP_EncryptInit_ex
Definition: boringssl_prefix_symbols.h:1531
CastTestCase::plaintext
uint8_t plaintext[16]
Definition: cast_test.cc:24
EVP_EncryptFinal_ex
#define EVP_EncryptFinal_ex
Definition: boringssl_prefix_symbols.h:1529
EVP_DecryptUpdate
#define EVP_DecryptUpdate
Definition: boringssl_prefix_symbols.h:1505
CastTestCase::key
uint8_t key[16]
Definition: cast_test.cc:23
kTests
static const CastTestCase kTests[]
Definition: cast_test.cc:30
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
EVP_DecryptFinal_ex
#define EVP_DecryptFinal_ex
Definition: boringssl_prefix_symbols.h:1502
EVP_cast5_cbc
const EVP_CIPHER * EVP_cast5_cbc(void)
Definition: cast.c:458
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
CastTestCase::cbc_ciphertext
uint8_t cbc_ciphertext[24]
Definition: cast_test.cc:27
CastTestCase
Definition: cast_test.cc:22


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:52