From d82a7b24be3bc4e14817d383b71b62000bb469cf Mon Sep 17 00:00:00 2001 From: Matt Braithwaite Date: Wed, 19 Aug 2015 14:25:32 -0700 Subject: [PATCH] Re-add |EVP_des_ede|, which is ECB. (I couldn't find an authoritative source of test data, including in OpenSSL's source, so I used OpenSSL's implementation to produce the test ciphertext.) This benefits globalplatform. Change-Id: Ifb79e77afb7efed1c329126a1a459bbf7ce6ca00 Reviewed-on: https://boringssl-review.googlesource.com/5725 Reviewed-by: Adam Langley --- crypto/cipher/cipher_test.cc | 2 ++ crypto/cipher/e_des.c | 26 ++++++++++++++++++++++++++ crypto/cipher/test/cipher_test.txt | 8 ++++++++ include/openssl/cipher.h | 1 + 4 files changed, 37 insertions(+) diff --git a/crypto/cipher/cipher_test.cc b/crypto/cipher/cipher_test.cc index 8917c1e8f..5f04178fc 100644 --- a/crypto/cipher/cipher_test.cc +++ b/crypto/cipher/cipher_test.cc @@ -71,6 +71,8 @@ static const EVP_CIPHER *GetCipher(const std::string &name) { return EVP_des_cbc(); } else if (name == "DES-ECB") { return EVP_des_ecb(); + } else if (name == "DES-EDE") { + return EVP_des_ede(); } else if (name == "DES-EDE-CBC") { return EVP_des_ede_cbc(); } else if (name == "DES-EDE3-CBC") { diff --git a/crypto/cipher/e_des.c b/crypto/cipher/e_des.c index d8207ec63..b1d312c3d 100644 --- a/crypto/cipher/e_des.c +++ b/crypto/cipher/e_des.c @@ -179,3 +179,29 @@ static const EVP_CIPHER des_ede_cbc = { NULL /* cleanup */, NULL /* ctrl */, }; const EVP_CIPHER *EVP_des_ede_cbc(void) { return &des_ede_cbc; } + + +static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, + const uint8_t *in, size_t in_len) { + if (in_len < ctx->cipher->block_size) { + return 1; + } + in_len -= ctx->cipher->block_size; + + DES_EDE_KEY *dat = (DES_EDE_KEY *) ctx->cipher_data; + size_t i; + for (i = 0; i <= in_len; i += ctx->cipher->block_size) { + DES_ecb3_encrypt((DES_cblock *) (in + i), (DES_cblock *) (out + i), + &dat->ks.ks[0], &dat->ks.ks[1], &dat->ks.ks[2], + ctx->encrypt); + } + return 1; +} + +static const EVP_CIPHER des_ede_ecb = { + NID_des_ede_cbc, 8 /* block_size */, 16 /* key_size */, + 0 /* iv_len */, sizeof(DES_EDE_KEY), EVP_CIPH_ECB_MODE, + NULL /* app_data */, des_ede_init_key , des_ede_ecb_cipher, + NULL /* cleanup */, NULL /* ctrl */, }; + +const EVP_CIPHER *EVP_des_ede(void) { return &des_ede_ecb; } diff --git a/crypto/cipher/test/cipher_test.txt b/crypto/cipher/test/cipher_test.txt index dade4317f..21fffdb15 100644 --- a/crypto/cipher/test/cipher_test.txt +++ b/crypto/cipher/test/cipher_test.txt @@ -46,6 +46,14 @@ Plaintext = 37363534333231204E6F77206973207468652074696D6520666F722000000000 Ciphertext = 7948C0DA4FE91CD815DCA96DBC9B60A857EB954F4DEB08EB98722642AE69257B +# DES EDE tests +Cipher = DES-EDE +Key = 0123456789abcdeff1e0d3c2b5a49786 +IV = fedcba9876543210 +Plaintext = 37363534333231204E6F77206973207468652074696D6520666F722000000000 +Ciphertext = 22E889402E28422F8167AD279D90A566DA75B734E12C671FC2669AECB3E4FE8F + + # AES 128 ECB tests (from FIPS-197 test vectors, encrypt) Cipher = AES-128-ECB Key = 000102030405060708090A0B0C0D0E0F diff --git a/include/openssl/cipher.h b/include/openssl/cipher.h index 7c38a204f..a4e79ddd5 100644 --- a/include/openssl/cipher.h +++ b/include/openssl/cipher.h @@ -76,6 +76,7 @@ OPENSSL_EXPORT const EVP_CIPHER *EVP_rc4(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_des_cbc(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ecb(void); +OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede_cbc(void); OPENSSL_EXPORT const EVP_CIPHER *EVP_des_ede3_cbc(void);