Add EVP_CIPHER support for Blowfish and CAST to decrepit.
Postgres contains a “pqcrypto” module that showcases the worst of 90's crypto, including Blowfish and CAST5 in CFB, CBC, and ECB modes. (Also, 64-bit keys for both of those.) In order to minimise the patching needed to build Postgres, put these things in decrepit. Change-Id: I8390c5153dd7227eef07293a4363878d79df8b21 Reviewed-on: https://boringssl-review.googlesource.com/c/34044 Reviewed-by: Adam Langley <agl@google.com> Reviewed-by: David Benjamin <davidben@google.com> Commit-Queue: Adam Langley <agl@google.com>
This commit is contained in:
parent
f77c8a38be
commit
6effbf24bc
@ -231,3 +231,7 @@ DEFINE_METHOD_FUNCTION(EVP_CIPHER, EVP_des_ede3) {
|
||||
out->init = des_ede3_init_key;
|
||||
out->cipher = des_ede_ecb_cipher;
|
||||
}
|
||||
|
||||
const EVP_CIPHER* EVP_des_ede3_ecb(void) {
|
||||
return EVP_des_ede3();
|
||||
}
|
||||
|
@ -29,8 +29,10 @@ target_link_libraries(decrepit crypto ssl)
|
||||
add_executable(
|
||||
decrepit_test
|
||||
|
||||
ripemd/ripemd_test.cc
|
||||
blowfish/blowfish_test.cc
|
||||
cast/cast_test.cc
|
||||
cfb/cfb_test.cc
|
||||
ripemd/ripemd_test.cc
|
||||
|
||||
$<TARGET_OBJECTS:boringssl_gtest_main>
|
||||
$<TARGET_OBJECTS:test_support>
|
||||
|
@ -55,7 +55,10 @@
|
||||
* [including the GNU Public Licence.] */
|
||||
|
||||
#include <openssl/blowfish.h>
|
||||
#include <openssl/cipher.h>
|
||||
#include <openssl/obj.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "../../crypto/internal.h"
|
||||
@ -492,3 +495,130 @@ void BF_set_key(BF_KEY *key, size_t len, const uint8_t *data) {
|
||||
p[i + 1] = in[1];
|
||||
}
|
||||
}
|
||||
|
||||
static void BF_cfb64_encrypt(const uint8_t *in, uint8_t *out, size_t length,
|
||||
const BF_KEY *schedule, uint8_t *ivec, int *num,
|
||||
int encrypt) {
|
||||
uint32_t v0, v1, t;
|
||||
int n = *num;
|
||||
size_t l = length;
|
||||
uint32_t ti[2];
|
||||
uint8_t c, cc;
|
||||
|
||||
uint8_t *iv = ivec;
|
||||
if (encrypt) {
|
||||
while (l--) {
|
||||
if (n == 0) {
|
||||
n2l(iv, v0);
|
||||
ti[0] = v0;
|
||||
n2l(iv, v1);
|
||||
ti[1] = v1;
|
||||
BF_encrypt(ti, schedule);
|
||||
iv = ivec;
|
||||
t = ti[0];
|
||||
l2n(t, iv);
|
||||
t = ti[1];
|
||||
l2n(t, iv);
|
||||
iv = ivec;
|
||||
}
|
||||
c = *(in++) ^ iv[n];
|
||||
*(out++) = c;
|
||||
iv[n] = c;
|
||||
n = (n + 1) & 0x07;
|
||||
}
|
||||
} else {
|
||||
while (l--) {
|
||||
if (n == 0) {
|
||||
n2l(iv, v0);
|
||||
ti[0] = v0;
|
||||
n2l(iv, v1);
|
||||
ti[1] = v1;
|
||||
BF_encrypt(ti, schedule);
|
||||
iv = ivec;
|
||||
t = ti[0];
|
||||
l2n(t, iv);
|
||||
t = ti[1];
|
||||
l2n(t, iv);
|
||||
iv = ivec;
|
||||
}
|
||||
cc = *(in++);
|
||||
c = iv[n];
|
||||
iv[n] = cc;
|
||||
*(out++) = c ^ cc;
|
||||
n = (n + 1) & 0x07;
|
||||
}
|
||||
}
|
||||
|
||||
*num = n;
|
||||
}
|
||||
|
||||
static int bf_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, int enc) {
|
||||
BF_KEY *bf_key = ctx->cipher_data;
|
||||
BF_set_key(bf_key, ctx->key_len, key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int bf_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
size_t len) {
|
||||
BF_KEY *bf_key = ctx->cipher_data;
|
||||
|
||||
while (len >= BF_BLOCK) {
|
||||
BF_ecb_encrypt(in, out, bf_key, ctx->encrypt);
|
||||
in += BF_BLOCK;
|
||||
out += BF_BLOCK;
|
||||
len -= BF_BLOCK;
|
||||
}
|
||||
assert(len == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int bf_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
size_t len) {
|
||||
BF_KEY *bf_key = ctx->cipher_data;
|
||||
BF_cbc_encrypt(in, out, len, bf_key, ctx->iv, ctx->encrypt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int bf_cfb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
size_t len) {
|
||||
BF_KEY *bf_key = ctx->cipher_data;
|
||||
int num = ctx->num;
|
||||
BF_cfb64_encrypt(in, out, len, bf_key, ctx->iv, &num, ctx->encrypt);
|
||||
ctx->num = num;
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const EVP_CIPHER bf_ecb = {
|
||||
NID_undef, BF_BLOCK /* block_size */,
|
||||
16 /* key_size */, BF_BLOCK /* iv_len */,
|
||||
sizeof(BF_KEY), EVP_CIPH_ECB_MODE | EVP_CIPH_VARIABLE_LENGTH,
|
||||
NULL /* app_data */, bf_init_key,
|
||||
bf_ecb_cipher, NULL /* cleanup */,
|
||||
NULL /* ctrl */,
|
||||
};
|
||||
|
||||
static const EVP_CIPHER bf_cbc = {
|
||||
NID_undef, BF_BLOCK /* block_size */,
|
||||
16 /* key_size */, BF_BLOCK /* iv_len */,
|
||||
sizeof(BF_KEY), EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH,
|
||||
NULL /* app_data */, bf_init_key,
|
||||
bf_cbc_cipher, NULL /* cleanup */,
|
||||
NULL /* ctrl */,
|
||||
};
|
||||
|
||||
static const EVP_CIPHER bf_cfb = {
|
||||
NID_undef, 1 /* block_size */,
|
||||
16 /* key_size */, BF_BLOCK /* iv_len */,
|
||||
sizeof(BF_KEY), EVP_CIPH_CFB_MODE | EVP_CIPH_VARIABLE_LENGTH,
|
||||
NULL /* app_data */, bf_init_key,
|
||||
bf_cfb_cipher, NULL /* cleanup */,
|
||||
NULL /* ctrl */,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_bf_ecb(void) { return &bf_ecb; }
|
||||
|
||||
const EVP_CIPHER *EVP_bf_cbc(void) { return &bf_cbc; }
|
||||
|
||||
const EVP_CIPHER *EVP_bf_cfb(void) { return &bf_cfb; }
|
||||
|
163
decrepit/blowfish/blowfish_test.cc
Normal file
163
decrepit/blowfish/blowfish_test.cc
Normal file
@ -0,0 +1,163 @@
|
||||
// Copyright (c) 2019, Google Inc.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include <openssl/cipher.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "../../crypto/internal.h"
|
||||
#include "../../crypto/test/test_util.h"
|
||||
|
||||
struct TestCase {
|
||||
uint8_t key[16];
|
||||
uint8_t plaintext[16];
|
||||
uint8_t iv[8];
|
||||
uint8_t ecb_ciphertext[16];
|
||||
uint8_t cbc_ciphertext[24];
|
||||
uint8_t cfb_ciphertext[16];
|
||||
};
|
||||
|
||||
static const TestCase kTests[] = {
|
||||
// Randomly generated test cases. Checked against vanilla OpenSSL.
|
||||
{
|
||||
{0xbb, 0x56, 0xb1, 0x27, 0x7c, 0x4c, 0xdd, 0x5a, 0x99, 0x90, 0x1e, 0x6f,
|
||||
0xeb, 0x36, 0x6c, 0xf3},
|
||||
{0xa6, 0x5b, 0xe0, 0x99, 0xad, 0x5d, 0x91, 0x98, 0x37, 0xc1, 0xa4, 0x7f,
|
||||
0x01, 0x24, 0x9a, 0x6b},
|
||||
{0xd5, 0x8a, 0x5c, 0x29, 0xeb, 0xee, 0xed, 0x76},
|
||||
{0xda, 0x6e, 0x18, 0x9c, 0x03, 0x59, 0x12, 0x61, 0xfa, 0x20, 0xd9, 0xce,
|
||||
0xee, 0x43, 0x9d, 0x05},
|
||||
{0x4f, 0x8b, 0x3e, 0x17, 0xa5, 0x35, 0x9b, 0xcb,
|
||||
0xdf, 0x3c, 0x52, 0xfb, 0xe5, 0x20, 0xdd, 0x53,
|
||||
0xd5, 0xf8, 0x1a, 0x6c, 0xf0, 0x99, 0x34, 0x94},
|
||||
{0xfd, 0x65, 0xc5, 0xa6, 0x07, 0x07, 0xb5, 0xf3, 0x2e, 0xfb, 0x12, 0xc3,
|
||||
0x7f, 0x45, 0x37, 0x54},
|
||||
},
|
||||
{
|
||||
{0x5d, 0x98, 0xa9, 0xd2, 0x27, 0x5d, 0xc8, 0x8c, 0x8c, 0xee, 0x23, 0x7f,
|
||||
0x8e, 0x2b, 0xd4, 0x8d},
|
||||
{0x60, 0xec, 0x31, 0xda, 0x25, 0x07, 0x02, 0x14, 0x84, 0x44, 0x96, 0xa6,
|
||||
0x04, 0x81, 0xca, 0x4e},
|
||||
{0x96, 0x4c, 0xa4, 0x07, 0xee, 0x1c, 0xd1, 0xfb},
|
||||
{0x83, 0x8a, 0xef, 0x18, 0x53, 0x96, 0xec, 0xf3, 0xf4, 0xd9, 0xe8, 0x4b,
|
||||
0x2c, 0x3f, 0xe7, 0x27},
|
||||
{0xad, 0x78, 0x70, 0x06, 0x2e, 0x5e, 0xa5, 0x21,
|
||||
0xdd, 0xe8, 0xa0, 0xb9, 0xdb, 0x0c, 0x81, 0x1d,
|
||||
0x0a, 0xd3, 0xa9, 0x63, 0x78, 0xac, 0x82, 0x64},
|
||||
{0x43, 0x2f, 0xf3, 0x23, 0xf4, 0x5c, 0xbf, 0x05, 0x53, 0x3c, 0x9e, 0xd6,
|
||||
0xd3, 0xd2, 0xc0, 0xf0},
|
||||
},
|
||||
};
|
||||
|
||||
TEST(Blowfish, ECB) {
|
||||
unsigned test_num = 0;
|
||||
for (const auto &test : kTests) {
|
||||
test_num++;
|
||||
SCOPED_TRACE(test_num);
|
||||
|
||||
uint8_t out[sizeof(test.ecb_ciphertext)];
|
||||
int out_bytes, final_bytes;
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX ctx;
|
||||
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_bf_ecb(), nullptr, test.key,
|
||||
nullptr));
|
||||
ASSERT_TRUE(EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding */));
|
||||
ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
|
||||
sizeof(test.plaintext)));
|
||||
ASSERT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.ecb_ciphertext), Bytes(out));
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
|
||||
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_bf_ecb(), nullptr,
|
||||
test.key, nullptr));
|
||||
ASSERT_TRUE(
|
||||
EVP_CIPHER_CTX_set_padding(decrypt_ctx.get(), 0 /* no padding */));
|
||||
ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
|
||||
test.ecb_ciphertext,
|
||||
sizeof(test.ecb_ciphertext)));
|
||||
ASSERT_TRUE(
|
||||
EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.plaintext), Bytes(out));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Blowfish, CBC) {
|
||||
unsigned test_num = 0;
|
||||
for (const auto &test : kTests) {
|
||||
test_num++;
|
||||
SCOPED_TRACE(test_num);
|
||||
|
||||
uint8_t out[sizeof(test.cbc_ciphertext)];
|
||||
int out_bytes, final_bytes;
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX ctx;
|
||||
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_bf_cbc(), nullptr, test.key,
|
||||
test.iv));
|
||||
ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
|
||||
sizeof(test.plaintext)));
|
||||
EXPECT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.cbc_ciphertext));
|
||||
EXPECT_EQ(Bytes(test.cbc_ciphertext), Bytes(out));
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
|
||||
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_bf_cbc(), nullptr,
|
||||
test.key, test.iv));
|
||||
ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
|
||||
test.cbc_ciphertext,
|
||||
sizeof(test.cbc_ciphertext)));
|
||||
EXPECT_TRUE(
|
||||
EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.plaintext), Bytes(out, out_bytes + final_bytes));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Blowfish, CFB) {
|
||||
unsigned test_num = 0;
|
||||
for (const auto &test : kTests) {
|
||||
test_num++;
|
||||
SCOPED_TRACE(test_num);
|
||||
|
||||
uint8_t out[sizeof(test.cfb_ciphertext)];
|
||||
int out_bytes, final_bytes;
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX ctx;
|
||||
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_bf_cfb(), nullptr, test.key,
|
||||
test.iv));
|
||||
ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
|
||||
sizeof(test.plaintext)));
|
||||
ASSERT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.cfb_ciphertext), Bytes(out));
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
|
||||
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_bf_cfb(), nullptr,
|
||||
test.key, test.iv));
|
||||
ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
|
||||
test.cfb_ciphertext,
|
||||
sizeof(test.cfb_ciphertext)));
|
||||
ASSERT_TRUE(
|
||||
EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.plaintext), Bytes(out));
|
||||
}
|
||||
}
|
@ -55,6 +55,8 @@
|
||||
* [including the GNU Public Licence.]. */
|
||||
|
||||
#include <openssl/cast.h>
|
||||
#include <openssl/cipher.h>
|
||||
#include <openssl/obj.h>
|
||||
|
||||
#if defined(OPENSSL_WINDOWS)
|
||||
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
||||
@ -62,6 +64,7 @@ OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
||||
OPENSSL_MSVC_PRAGMA(warning(pop))
|
||||
#endif
|
||||
|
||||
#include "../../crypto/internal.h"
|
||||
#include "internal.h"
|
||||
#include "../macros.h"
|
||||
|
||||
@ -406,3 +409,54 @@ void CAST_cfb64_encrypt(const uint8_t *in, uint8_t *out, long length,
|
||||
v0 = v1 = ti[0] = ti[1] = t = c = cc = 0;
|
||||
*num = n;
|
||||
}
|
||||
|
||||
static int cast_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
|
||||
const uint8_t *iv, int enc) {
|
||||
CAST_KEY *cast_key = ctx->cipher_data;
|
||||
CAST_set_key(cast_key, ctx->key_len, key);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cast_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
size_t len) {
|
||||
CAST_KEY *cast_key = ctx->cipher_data;
|
||||
|
||||
while (len >= CAST_BLOCK) {
|
||||
CAST_ecb_encrypt(in, out, cast_key, ctx->encrypt);
|
||||
in += CAST_BLOCK;
|
||||
out += CAST_BLOCK;
|
||||
len -= CAST_BLOCK;
|
||||
}
|
||||
assert(len == 0);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int cast_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in,
|
||||
size_t len) {
|
||||
CAST_KEY *cast_key = ctx->cipher_data;
|
||||
CAST_cbc_encrypt(in, out, len, cast_key, ctx->iv, ctx->encrypt);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const EVP_CIPHER cast5_ecb = {
|
||||
NID_undef, CAST_BLOCK,
|
||||
CAST_KEY_LENGTH, CAST_BLOCK /* iv_len */,
|
||||
sizeof(CAST_KEY), EVP_CIPH_ECB_MODE | EVP_CIPH_VARIABLE_LENGTH,
|
||||
NULL /* app_data */, cast_init_key,
|
||||
cast_ecb_cipher, NULL /* cleanup */,
|
||||
NULL /* ctrl */,
|
||||
};
|
||||
|
||||
static const EVP_CIPHER cast5_cbc = {
|
||||
NID_undef, CAST_BLOCK,
|
||||
CAST_KEY_LENGTH, CAST_BLOCK /* iv_len */,
|
||||
sizeof(CAST_KEY), EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH,
|
||||
NULL /* app_data */, cast_init_key,
|
||||
cast_cbc_cipher, NULL /* cleanup */,
|
||||
NULL /* ctrl */,
|
||||
};
|
||||
|
||||
const EVP_CIPHER *EVP_cast5_ecb(void) { return &cast5_ecb; }
|
||||
|
||||
const EVP_CIPHER *EVP_cast5_cbc(void) { return &cast5_cbc; }
|
||||
|
125
decrepit/cast/cast_test.cc
Normal file
125
decrepit/cast/cast_test.cc
Normal file
@ -0,0 +1,125 @@
|
||||
// Copyright (c) 2019, Google Inc.
|
||||
//
|
||||
// Permission to use, copy, modify, and/or distribute this software for any
|
||||
// purpose with or without fee is hereby granted, provided that the above
|
||||
// copyright notice and this permission notice appear in all copies.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
||||
// SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
||||
// OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
||||
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
|
||||
#include <openssl/cipher.h>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include "../../crypto/internal.h"
|
||||
#include "../../crypto/test/test_util.h"
|
||||
|
||||
struct TestCase {
|
||||
uint8_t key[16];
|
||||
uint8_t plaintext[16];
|
||||
uint8_t iv[8];
|
||||
uint8_t ecb_ciphertext[16];
|
||||
uint8_t cbc_ciphertext[24];
|
||||
};
|
||||
|
||||
static const TestCase kTests[] = {
|
||||
// Randomly generated test cases. Checked against vanilla OpenSSL.
|
||||
{
|
||||
{0xbb, 0x56, 0xb1, 0x27, 0x7c, 0x4c, 0xdd, 0x5a, 0x99, 0x90, 0x1e, 0x6f,
|
||||
0xeb, 0x36, 0x6c, 0xf3},
|
||||
{0xa6, 0x5b, 0xe0, 0x99, 0xad, 0x5d, 0x91, 0x98, 0x37, 0xc1, 0xa4, 0x7f,
|
||||
0x01, 0x24, 0x9a, 0x6b},
|
||||
{0xd5, 0x8a, 0x5c, 0x29, 0xeb, 0xee, 0xed, 0x76},
|
||||
{0x01, 0x8d, 0x1b, 0x42, 0xb8, 0x77, 0xc8, 0x84, 0x25, 0x7d, 0xd4, 0x89,
|
||||
0x8d, 0xc1, 0xbc, 0x2a},
|
||||
{0xc1, 0x05, 0xa1, 0x9a, 0xb4, 0xc4, 0xd0, 0x15,
|
||||
0x9d, 0xfd, 0xea, 0xd0, 0xc3, 0x54, 0xe5, 0x33,
|
||||
0x26, 0xac, 0x25, 0xf3, 0x48, 0xbc, 0xf6, 0xa2},
|
||||
},
|
||||
{
|
||||
{0x5d, 0x98, 0xa9, 0xd2, 0x27, 0x5d, 0xc8, 0x8c, 0x8c, 0xee, 0x23, 0x7f,
|
||||
0x8e, 0x2b, 0xd4, 0x8d},
|
||||
{0x60, 0xec, 0x31, 0xda, 0x25, 0x07, 0x02, 0x14, 0x84, 0x44, 0x96, 0xa6,
|
||||
0x04, 0x81, 0xca, 0x4e},
|
||||
{0x96, 0x4c, 0xa4, 0x07, 0xee, 0x1c, 0xd1, 0xfb},
|
||||
{0x58, 0x62, 0x29, 0x62, 0x23, 0x69, 0x9e, 0xe8, 0x27, 0xc2, 0xcd, 0x5b,
|
||||
0x35, 0xf1, 0xdf, 0xa4},
|
||||
{0x1c, 0xd0, 0x29, 0xe5, 0xf3, 0xdb, 0x65, 0x60,
|
||||
0x05, 0xde, 0x01, 0x2b, 0x10, 0x09, 0x44, 0x56,
|
||||
0x59, 0x44, 0x00, 0x26, 0xdb, 0xb3, 0x2d, 0x98},
|
||||
},
|
||||
};
|
||||
|
||||
TEST(CAST, ECB) {
|
||||
unsigned test_num = 0;
|
||||
for (const auto &test : kTests) {
|
||||
test_num++;
|
||||
SCOPED_TRACE(test_num);
|
||||
|
||||
uint8_t out[sizeof(test.ecb_ciphertext)];
|
||||
int out_bytes, final_bytes;
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX ctx;
|
||||
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_cast5_ecb(), nullptr,
|
||||
test.key, nullptr));
|
||||
ASSERT_TRUE(EVP_CIPHER_CTX_set_padding(ctx.get(), 0 /* no padding */));
|
||||
ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
|
||||
sizeof(test.plaintext)));
|
||||
ASSERT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.ecb_ciphertext), Bytes(out));
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
|
||||
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_cast5_ecb(), nullptr,
|
||||
test.key, nullptr));
|
||||
ASSERT_TRUE(
|
||||
EVP_CIPHER_CTX_set_padding(decrypt_ctx.get(), 0 /* no padding */));
|
||||
ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
|
||||
test.ecb_ciphertext,
|
||||
sizeof(test.ecb_ciphertext)));
|
||||
ASSERT_TRUE(
|
||||
EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.plaintext), Bytes(out));
|
||||
}
|
||||
}
|
||||
|
||||
TEST(CAST, CBC) {
|
||||
unsigned test_num = 0;
|
||||
for (const auto &test : kTests) {
|
||||
test_num++;
|
||||
SCOPED_TRACE(test_num);
|
||||
|
||||
uint8_t out[sizeof(test.cbc_ciphertext)];
|
||||
int out_bytes, final_bytes;
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX ctx;
|
||||
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_cast5_cbc(), nullptr,
|
||||
test.key, test.iv));
|
||||
ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out, &out_bytes, test.plaintext,
|
||||
sizeof(test.plaintext)));
|
||||
EXPECT_TRUE(EVP_EncryptFinal_ex(ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.cbc_ciphertext));
|
||||
EXPECT_EQ(Bytes(test.cbc_ciphertext), Bytes(out));
|
||||
|
||||
bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
|
||||
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_cast5_cbc(), nullptr,
|
||||
test.key, test.iv));
|
||||
ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), out, &out_bytes,
|
||||
test.cbc_ciphertext,
|
||||
sizeof(test.cbc_ciphertext)));
|
||||
EXPECT_TRUE(
|
||||
EVP_DecryptFinal_ex(decrypt_ctx.get(), out + out_bytes, &final_bytes));
|
||||
EXPECT_EQ(static_cast<size_t>(out_bytes + final_bytes),
|
||||
sizeof(test.plaintext));
|
||||
EXPECT_EQ(Bytes(test.plaintext), Bytes(out, out_bytes + final_bytes));
|
||||
}
|
||||
}
|
@ -419,9 +419,27 @@ OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ctr(void);
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_gcm(void);
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ofb(void);
|
||||
|
||||
// EVP_des_ede3_ecb is an alias for |EVP_des_ede3|. Use the former instead.
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede3_ecb(void);
|
||||
|
||||
// EVP_aes_128_cfb128 is only available in decrepit.
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cfb128(void);
|
||||
|
||||
// EVP_bf_ecb is Blowfish in ECB mode and is only available in decrepit.
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_bf_ecb(void);
|
||||
|
||||
// EVP_bf_cbc is Blowfish in CBC mode and is only available in decrepit.
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_bf_cbc(void);
|
||||
|
||||
// EVP_bf_cfb is Blowfish in 64-bit CFB mode and is only available in decrepit.
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_bf_cfb(void);
|
||||
|
||||
// EVP_cast5_ecb is CAST5 in ECB mode and is only available in decrepit.
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_cast5_ecb(void);
|
||||
|
||||
// EVP_cast5_cbc is CAST5 in CBC mode and is only available in decrepit.
|
||||
OPENSSL_EXPORT const EVP_CIPHER *EVP_cast5_cbc(void);
|
||||
|
||||
// The following flags do nothing and are included only to make it easier to
|
||||
// compile code with BoringSSL.
|
||||
#define EVP_CIPH_CCM_MODE (-1)
|
||||
|
Loading…
x
Reference in New Issue
Block a user