Add EVP AES-128 CFB128 support via decrepit.

Change-Id: I37a438b5b4b18d18756ba4aeb9f8548caa333981
Reviewed-on: https://boringssl-review.googlesource.com/20384
Reviewed-by: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
CQ-Verified: CQ bot account: commit-bot@chromium.org <commit-bot@chromium.org>
This commit is contained in:
Adam Langley 2017-09-14 10:14:34 -07:00 committed by CQ bot account: commit-bot@chromium.org
parent 9a127b43b8
commit e64ef27cbe
5 changed files with 166 additions and 0 deletions

View File

@ -3,6 +3,7 @@ include_directories(../include)
add_subdirectory(bio) add_subdirectory(bio)
add_subdirectory(blowfish) add_subdirectory(blowfish)
add_subdirectory(cast) add_subdirectory(cast)
add_subdirectory(cfb)
add_subdirectory(des) add_subdirectory(des)
add_subdirectory(dh) add_subdirectory(dh)
add_subdirectory(dsa) add_subdirectory(dsa)
@ -21,6 +22,7 @@ add_library(
$<TARGET_OBJECTS:bio_decrepit> $<TARGET_OBJECTS:bio_decrepit>
$<TARGET_OBJECTS:blowfish> $<TARGET_OBJECTS:blowfish>
$<TARGET_OBJECTS:cast> $<TARGET_OBJECTS:cast>
$<TARGET_OBJECTS:cfb>
$<TARGET_OBJECTS:des_decrepit> $<TARGET_OBJECTS:des_decrepit>
$<TARGET_OBJECTS:dh_decrepit> $<TARGET_OBJECTS:dh_decrepit>
$<TARGET_OBJECTS:dsa_decrepit> $<TARGET_OBJECTS:dsa_decrepit>
@ -40,6 +42,7 @@ add_executable(
decrepit_test decrepit_test
ripemd/ripemd_test.cc ripemd/ripemd_test.cc
cfb/cfb_test.cc
$<TARGET_OBJECTS:gtest_main> $<TARGET_OBJECTS:gtest_main>
$<TARGET_OBJECTS:test_support> $<TARGET_OBJECTS:test_support>

View File

@ -0,0 +1,9 @@
include_directories(../../include)
add_library(
cfb
OBJECT
cfb.c
)

60
decrepit/cfb/cfb.c Normal file
View File

@ -0,0 +1,60 @@
// Copyright (c) 2017, 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 <string.h>
#include <openssl/aes.h>
#include <openssl/obj.h>
#include "../../crypto/internal.h"
typedef struct {
AES_KEY ks;
} EVP_CFB_CTX;
static int aes_cfb_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
const uint8_t *iv, int enc) {
if (key) {
EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
AES_set_encrypt_key(key, ctx->key_len * 8, &cfb_ctx->ks);
}
return 1;
}
static int aes_cfb128_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
const uint8_t *in, size_t len) {
if (!out || !in) {
return 0;
}
EVP_CFB_CTX *cfb_ctx = ctx->cipher_data;
int num = ctx->num;
AES_cfb128_encrypt(in, out, len, &cfb_ctx->ks, ctx->iv, &num,
ctx->encrypt ? AES_ENCRYPT : AES_DECRYPT);
ctx->num = num;
return 1;
}
static const EVP_CIPHER aes_128_cfb128 = {
NID_aes_128_cfb128, 1 /* block_size */, 16 /* key_size */,
16 /* iv_len */, sizeof(EVP_CFB_CTX), EVP_CIPH_CFB_MODE,
NULL /* app_data */, aes_cfb_init_key, aes_cfb128_cipher,
NULL /* cleanup */, NULL /* ctrl */,
};
const EVP_CIPHER *EVP_aes_128_cfb128(void) { return &aes_128_cfb128; }

91
decrepit/cfb/cfb_test.cc Normal file
View File

@ -0,0 +1,91 @@
// Copyright (c) 2017, 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 CFBTestCase {
uint8_t key[16];
uint8_t iv[16];
uint8_t plaintext[16*4];
uint8_t ciphertext[16*4];
};
static const CFBTestCase kCFBTestCases[] = {
{
// This is the test case from
// http://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38a.pdf,
// section F.3.13.
{0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c},
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
{0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10},
{0x3b, 0x3f, 0xd9, 0x2e, 0xb7, 0x2d, 0xad, 0x20, 0x33, 0x34, 0x49, 0xf8, 0xe8, 0x3c, 0xfb, 0x4a,
0xc8, 0xa6, 0x45, 0x37, 0xa0, 0xb3, 0xa9, 0x3f, 0xcd, 0xe3, 0xcd, 0xad, 0x9f, 0x1c, 0xe5, 0x8b,
0x26, 0x75, 0x1f, 0x67, 0xa3, 0xcb, 0xb1, 0x40, 0xb1, 0x80, 0x8c, 0xf1, 0x87, 0xa4, 0xf4, 0xdf,
0xc0, 0x4b, 0x05, 0x35, 0x7c, 0x5d, 0x1c, 0x0e, 0xea, 0xc4, 0xc6, 0x6f, 0x9f, 0xf7, 0xf2, 0xe6},
},
};
TEST(CFBTest, TestVectors) {
unsigned test_num = 0;
for (const auto &test : kCFBTestCases) {
test_num++;
SCOPED_TRACE(test_num);
const size_t input_len = sizeof(test.plaintext);
std::unique_ptr<uint8_t[]> out(new uint8_t[input_len]);
for (size_t stride = 1; stride <= input_len; stride++) {
bssl::ScopedEVP_CIPHER_CTX ctx;
ASSERT_TRUE(EVP_EncryptInit_ex(ctx.get(), EVP_aes_128_cfb128(), nullptr,
test.key, test.iv));
size_t done = 0;
while (done < input_len) {
size_t todo = stride;
if (todo > input_len - done) {
todo = input_len - done;
}
int out_bytes;
ASSERT_TRUE(EVP_EncryptUpdate(ctx.get(), out.get() + done, &out_bytes,
test.plaintext + done, todo));
ASSERT_EQ(static_cast<size_t>(out_bytes), todo);
done += todo;
}
EXPECT_EQ(Bytes(test.ciphertext), Bytes(out.get(), input_len));
}
bssl::ScopedEVP_CIPHER_CTX decrypt_ctx;
ASSERT_TRUE(EVP_DecryptInit_ex(decrypt_ctx.get(), EVP_aes_128_cfb128(),
nullptr, test.key, test.iv));
std::unique_ptr<uint8_t[]> plaintext(new uint8_t[input_len]);
int num_bytes;
ASSERT_TRUE(EVP_DecryptUpdate(decrypt_ctx.get(), plaintext.get(),
&num_bytes, out.get(), input_len));
EXPECT_EQ(static_cast<size_t>(num_bytes), input_len);
EXPECT_EQ(Bytes(test.plaintext), Bytes(plaintext.get(), input_len));
}
}

View File

@ -410,6 +410,9 @@ OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_cbc(void);
OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_192_ctr(void); 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_gcm(void);
// EVP_aes_128_cfb128 is only available in decrepit.
OPENSSL_EXPORT const EVP_CIPHER *EVP_aes_128_cfb128(void);
// Private functions. // Private functions.