• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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];
24   uint8_t plaintext[16];
25   uint8_t iv[8];
26   uint8_t ecb_ciphertext[16];
27   uint8_t cbc_ciphertext[24];
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 
TEST(CAST,ECB)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;
68     ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_cast5_ecb(), nullptr,
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));
81     ASSERT_TRUE(
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)));
86     ASSERT_TRUE(
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 
TEST(CAST,CBC)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;
104     ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_cast5_cbc(), nullptr,
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 }
126