diff options
author | Shane Lontis <shane.lontis@oracle.com> | 2020-04-09 12:47:46 +1000 |
---|---|---|
committer | Shane Lontis <shane.lontis@oracle.com> | 2020-04-09 12:47:46 +1000 |
commit | f5056577ba08b3694aab2722eae1c97bf00acc80 (patch) | |
tree | 00492b31c1e6b69164d5a3be50557bac18cdcd2e /providers | |
parent | cc45a884bd499e8b84de0c0133746591c3712f4c (diff) |
Move legacy ciphers into the legacy provider
DES, idea, seed, rc2, rc4, rc5, cast and blowfish have been moved out of the default provider.
Code shared between desx and tdes has been moved into a seperate file (cipher_tdes_common.c).
3 test recipes failed due to using app/openssl calls that used legacy ciphers.
These calls have been updated to supply both the default and legacy providers.
Fixed openssl app '-provider' memory leak
Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11419)
Diffstat (limited to 'providers')
-rw-r--r-- | providers/build.info | 2 | ||||
-rw-r--r-- | providers/defltprov.c | 54 | ||||
-rw-r--r-- | providers/implementations/ciphers/build.info | 20 | ||||
-rw-r--r-- | providers/implementations/ciphers/cipher_tdes.c | 93 | ||||
-rw-r--r-- | providers/implementations/ciphers/cipher_tdes_common.c | 110 | ||||
-rw-r--r-- | providers/legacyprov.c | 75 |
6 files changed, 189 insertions, 165 deletions
diff --git a/providers/build.info b/providers/build.info index a58c8eae75..54c3381bca 100644 --- a/providers/build.info +++ b/providers/build.info @@ -47,7 +47,7 @@ $COMMON_INCLUDES=../crypto ../include implementations/include common/include INCLUDE[$LIBCOMMON]=$COMMON_INCLUDES INCLUDE[$LIBIMPLEMENTATIONS]=.. $COMMON_INCLUDES -INCLUDE[$LIBLEGACY]=$COMMON_INCLUDES +INCLUDE[$LIBLEGACY]=.. $COMMON_INCLUDES INCLUDE[$LIBNONFIPS]=$COMMON_INCLUDES INCLUDE[$LIBFIPS]=.. $COMMON_INCLUDES DEFINE[$LIBFIPS]=FIPS_MODE diff --git a/providers/defltprov.c b/providers/defltprov.c index 2ec229e16b..f93bd31d57 100644 --- a/providers/defltprov.c +++ b/providers/defltprov.c @@ -254,43 +254,12 @@ static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = { ALG("DES-EDE3-CFB", tdes_ede3_cfb_functions), ALG("DES-EDE3-CFB8", tdes_ede3_cfb8_functions), ALG("DES-EDE3-CFB1", tdes_ede3_cfb1_functions), + ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", tdes_wrap_cbc_functions), ALG("DES-EDE-ECB:DES-EDE", tdes_ede2_ecb_functions), ALG("DES-EDE-CBC", tdes_ede2_cbc_functions), ALG("DES-EDE-OFB", tdes_ede2_ofb_functions), ALG("DES-EDE-CFB", tdes_ede2_cfb_functions), - ALG("DESX-CBC:DESX", tdes_desx_cbc_functions), - ALG("DES3-WRAP:id-smime-alg-CMS3DESwrap", tdes_wrap_cbc_functions), - ALG("DES-ECB", des_ecb_functions), - ALG("DES-CBC:DES", des_cbc_functions), - ALG("DES-OFB", des_ofb64_functions), - ALG("DES-CFB", des_cfb64_functions), - ALG("DES-CFB1", des_cfb1_functions), - ALG("DES-CFB8", des_cfb8_functions), #endif /* OPENSSL_NO_DES */ -#ifndef OPENSSL_NO_BF - ALG("BF-ECB", blowfish128ecb_functions), - ALG("BF-CBC:BF:BLOWFISH", blowfish128cbc_functions), - ALG("BF-OFB", blowfish64ofb64_functions), - ALG("BF-CFB", blowfish64cfb64_functions), -#endif /* OPENSSL_NO_BF */ -#ifndef OPENSSL_NO_IDEA - ALG("IDEA-ECB", idea128ecb_functions), - ALG("IDEA-CBC:IDEA", idea128cbc_functions), - ALG("IDEA-OFB:IDEA-OFB64", idea128ofb64_functions), - ALG("IDEA-CFB:IDEA-CFB64", idea128cfb64_functions), -#endif /* OPENSSL_NO_IDEA */ -#ifndef OPENSSL_NO_CAST - ALG("CAST5-ECB", cast5128ecb_functions), - ALG("CAST5-CBC:CAST-CBC:CAST", cast5128cbc_functions), - ALG("CAST5-OFB", cast564ofb64_functions), - ALG("CAST5-CFB", cast564cfb64_functions), -#endif /* OPENSSL_NO_CAST */ -#ifndef OPENSSL_NO_SEED - ALG("SEED-ECB", seed128ecb_functions), - ALG("SEED-CBC:SEED", seed128cbc_functions), - ALG("SEED-OFB:SEED-OFB128", seed128ofb128_functions), - ALG("SEED-CFB:SEED-CFB128", seed128cfb128_functions), -#endif /* OPENSSL_NO_SEED */ #ifndef OPENSSL_NO_SM4 ALG("SM4-ECB", sm4128ecb_functions), ALG("SM4-CBC:SM4", sm4128cbc_functions), @@ -298,27 +267,6 @@ static const OSSL_ALGORITHM_CAPABLE deflt_ciphers[] = { ALG("SM4-OFB:SM4-OFB128", sm4128ofb128_functions), ALG("SM4-CFB:SM4-CFB128", sm4128cfb128_functions), #endif /* OPENSSL_NO_SM4 */ -#ifndef OPENSSL_NO_RC4 - ALG("RC4", rc4128_functions), - ALG("RC4-40", rc440_functions), -# ifndef OPENSSL_NO_MD5 - ALG("RC4-HMAC-MD5", rc4_hmac_md5_functions), -# endif /* OPENSSL_NO_MD5 */ -#endif /* OPENSSL_NO_RC4 */ -#ifndef OPENSSL_NO_RC5 - ALG("RC5-ECB", rc5128ecb_functions), - ALG("RC5-CBC", rc5128cbc_functions), - ALG("RC5-OFB", rc5128ofb64_functions), - ALG("RC5-CFB", rc5128cfb64_functions), -#endif /* OPENSSL_NO_RC5 */ -#ifndef OPENSSL_NO_RC2 - ALG("RC2-ECB", rc2128ecb_functions), - ALG("RC2-CBC", rc2128cbc_functions), - ALG("RC2-40-CBC", rc240cbc_functions), - ALG("RC2-64-CBC", rc264cbc_functions), - ALG("RC2-CFB", rc2128cfb128_functions), - ALG("RC2-OFB", rc2128ofb128_functions), -#endif /* OPENSSL_NO_RC2 */ #ifndef OPENSSL_NO_CHACHA ALG("ChaCha20", chacha20_functions), # ifndef OPENSSL_NO_POLY1305 diff --git a/providers/implementations/ciphers/build.info b/providers/implementations/ciphers/build.info index c45ea00f16..c97008c330 100644 --- a/providers/implementations/ciphers/build.info +++ b/providers/implementations/ciphers/build.info @@ -11,17 +11,17 @@ $NULL_GOAL=../../libimplementations.a $AES_GOAL=../../libimplementations.a $TDES_1_GOAL=../../libimplementations.a $TDES_2_GOAL=../../libimplementations.a -$DES_GOAL=../../libimplementations.a $ARIA_GOAL=../../libimplementations.a $CAMELLIA_GOAL=../../libimplementations.a -$BLOWFISH_GOAL=../../libimplementations.a -$IDEA_GOAL=../../libimplementations.a -$CAST5_GOAL=../../libimplementations.a -$SEED_GOAL=../../libimplementations.a +$DES_GOAL=../../liblegacy.a +$BLOWFISH_GOAL=../../liblegacy.a +$IDEA_GOAL=../../liblegacy.a +$CAST5_GOAL=../../liblegacy.a +$RC2_GOAL=../../liblegacy.a +$RC4_GOAL=../../liblegacy.a +$RC5_GOAL=../../liblegacy.a +$SEED_GOAL=../../liblegacy.a $SM4_GOAL=../../libimplementations.a -$RC4_GOAL=../../libimplementations.a -$RC5_GOAL=../../libimplementations.a -$RC2_GOAL=../../libimplementations.a $CHACHA_GOAL=../../libimplementations.a $CHACHAPOLY_GOAL=../../libimplementations.a $SIV_GOAL=../../libimplementations.a @@ -33,7 +33,7 @@ SOURCE[$COMMON_GOAL]=\ ciphercommon_ccm.c ciphercommon_ccm_hw.c IF[{- !$disabled{des} -}] - SOURCE[$TDES_1_GOAL]=cipher_tdes.c cipher_tdes_hw.c + SOURCE[$TDES_1_GOAL]=cipher_tdes.c cipher_tdes_common.c cipher_tdes_hw.c ENDIF SOURCE[$NULL_GOAL]=\ @@ -63,7 +63,7 @@ IF[{- !$disabled{des} -}] cipher_tdes_default.c cipher_tdes_default_hw.c \ cipher_tdes_wrap.c cipher_tdes_wrap_hw.c SOURCE[$DES_GOAL]=\ - cipher_desx.c cipher_desx_hw.c \ + cipher_desx.c cipher_desx_hw.c cipher_tdes_common.c\ cipher_des.c cipher_des_hw.c ENDIF diff --git a/providers/implementations/ciphers/cipher_tdes.c b/providers/implementations/ciphers/cipher_tdes.c index ea0c987a80..82af8bdc16 100644 --- a/providers/implementations/ciphers/cipher_tdes.c +++ b/providers/implementations/ciphers/cipher_tdes.c @@ -1,5 +1,5 @@ /* - * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the Apache License 2.0 (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -19,101 +19,10 @@ #include "prov/implementations.h" #include "prov/providercommonerr.h" -void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits, - size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw) -{ - PROV_TDES_CTX *tctx = OPENSSL_zalloc(sizeof(*tctx)); - - if (tctx != NULL) - cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags, hw, - provctx); - return tctx; -} - -void tdes_freectx(void *vctx) -{ - PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx; - - OPENSSL_clear_free(ctx, sizeof(*ctx)); -} - -static int tdes_init(void *vctx, const unsigned char *key, size_t keylen, - const unsigned char *iv, size_t ivlen, int enc) -{ - PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; - - ctx->enc = enc; - - if (iv != NULL) { - if (!cipher_generic_initiv(ctx, iv, ivlen)) - return 0; - } - - if (key != NULL) { - if (keylen != ctx->keylen) { - ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN); - return 0; - } - return ctx->hw->init(ctx, key, ctx->keylen); - } - return 1; -} - -int tdes_einit(void *vctx, const unsigned char *key, size_t keylen, - const unsigned char *iv, size_t ivlen) -{ - return tdes_init(vctx, key, keylen, iv, ivlen, 1); -} - -int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen, - const unsigned char *iv, size_t ivlen) -{ - return tdes_init(vctx, key, keylen, iv, ivlen, 0); -} - -static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr) -{ - - DES_cblock *deskey = ptr; - size_t kl = ctx->keylen; - - if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0) - return 0; - DES_set_odd_parity(deskey); - if (kl >= 16) - DES_set_odd_parity(deskey + 1); - if (kl >= 24) { - DES_set_odd_parity(deskey + 2); - return 1; - } - return 0; -} - -CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes) - OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0), -CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes) - -int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[]) -{ - PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; - OSSL_PARAM *p; - - if (!cipher_generic_get_ctx_params(vctx, params)) - return 0; - - p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY); - if (p != NULL && !tdes_generatekey(ctx, p->data)) { - ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); - return 0; - } - return 1; -} - /* * TODO(3.0) - ECB mode does not use an IV - but existing test code is setting * an IV. Fixing this could potentially make applications break. */ - /* tdes_ede3_ecb_functions */ IMPLEMENT_tdes_cipher(ede3, EDE3, ecb, ECB, TDES_FLAGS, 64*3, 64, 64, block); /* tdes_ede3_cbc_functions */ diff --git a/providers/implementations/ciphers/cipher_tdes_common.c b/providers/implementations/ciphers/cipher_tdes_common.c new file mode 100644 index 0000000000..36a8962e03 --- /dev/null +++ b/providers/implementations/ciphers/cipher_tdes_common.c @@ -0,0 +1,110 @@ +/* + * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +/* + * DES low level APIs are deprecated for public use, but still ok for internal + * use. + */ +#include "internal/deprecated.h" + +#include "prov/ciphercommon.h" +#include "cipher_tdes.h" +#include <openssl/rand.h> +#include "prov/implementations.h" +#include "prov/providercommonerr.h" + +void *tdes_newctx(void *provctx, int mode, size_t kbits, size_t blkbits, + size_t ivbits, uint64_t flags, const PROV_CIPHER_HW *hw) +{ + PROV_TDES_CTX *tctx = OPENSSL_zalloc(sizeof(*tctx)); + + if (tctx != NULL) + cipher_generic_initkey(tctx, kbits, blkbits, ivbits, mode, flags, hw, + provctx); + return tctx; +} + +void tdes_freectx(void *vctx) +{ + PROV_TDES_CTX *ctx = (PROV_TDES_CTX *)vctx; + + OPENSSL_clear_free(ctx, sizeof(*ctx)); +} + +static int tdes_init(void *vctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen, int enc) +{ + PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; + + ctx->enc = enc; + + if (iv != NULL) { + if (!cipher_generic_initiv(ctx, iv, ivlen)) + return 0; + } + + if (key != NULL) { + if (keylen != ctx->keylen) { + ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEYLEN); + return 0; + } + return ctx->hw->init(ctx, key, ctx->keylen); + } + return 1; +} + +int tdes_einit(void *vctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen) +{ + return tdes_init(vctx, key, keylen, iv, ivlen, 1); +} + +int tdes_dinit(void *vctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen) +{ + return tdes_init(vctx, key, keylen, iv, ivlen, 0); +} + +CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_START(tdes) + OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_RANDOM_KEY, NULL, 0), +CIPHER_DEFAULT_GETTABLE_CTX_PARAMS_END(tdes) + +static int tdes_generatekey(PROV_CIPHER_CTX *ctx, void *ptr) +{ + + DES_cblock *deskey = ptr; + size_t kl = ctx->keylen; + + if (kl == 0 || RAND_priv_bytes_ex(ctx->libctx, ptr, kl) <= 0) + return 0; + DES_set_odd_parity(deskey); + if (kl >= 16) + DES_set_odd_parity(deskey + 1); + if (kl >= 24) { + DES_set_odd_parity(deskey + 2); + return 1; + } + return 0; +} + +int tdes_get_ctx_params(void *vctx, OSSL_PARAM params[]) +{ + PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; + OSSL_PARAM *p; + + if (!cipher_generic_get_ctx_params(vctx, params)) + return 0; + + p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_RANDOM_KEY); + if (p != NULL && !tdes_generatekey(ctx, p->data)) { + ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GENERATE_KEY); + return 0; + } + return 1; +} diff --git a/providers/legacyprov.c b/providers/legacyprov.c index 3494a8801c..a439cfd32f 100644 --- a/providers/legacyprov.c +++ b/providers/legacyprov.c @@ -15,6 +15,8 @@ #include <openssl/params.h> #include "prov/implementations.h" +#define ALG(NAMES, FUNC) { NAMES, "provider=legacy", FUNC } + #ifdef STATIC_LEGACY OSSL_provider_init_fn ossl_legacy_provider_init; # define OSSL_provider_init ossl_legacy_provider_init @@ -56,25 +58,78 @@ static int legacy_get_params(const OSSL_PROVIDER *prov, OSSL_PARAM params[]) static const OSSL_ALGORITHM legacy_digests[] = { #ifndef OPENSSL_NO_MD2 - { "MD2", "provider=legacy", md2_functions }, + ALG("MD2", md2_functions), #endif - #ifndef OPENSSL_NO_MD4 - { "MD4", "provider=legacy", md4_functions }, + ALG("MD4", md4_functions), #endif - #ifndef OPENSSL_NO_MDC2 - { "MDC2", "provider=legacy", mdc2_functions }, + ALG("MDC2", mdc2_functions), #endif /* OPENSSL_NO_MDC2 */ - #ifndef OPENSSL_NO_WHIRLPOOL - { "WHIRLPOOL", "provider=legacy", wp_functions }, + ALG("WHIRLPOOL", wp_functions), #endif /* OPENSSL_NO_WHIRLPOOL */ - #ifndef OPENSSL_NO_RMD160 - { "RIPEMD-160:RIPEMD160:RIPEMD:RMD160", "provider=legacy", ripemd160_functions }, + ALG("RIPEMD-160:RIPEMD160:RIPEMD:RMD160", ripemd160_functions), #endif /* OPENSSL_NO_RMD160 */ + { NULL, NULL, NULL } +}; +static const OSSL_ALGORITHM legacy_ciphers[] = { +#ifndef OPENSSL_NO_CAST + ALG("CAST5-ECB", cast5128ecb_functions), + ALG("CAST5-CBC:CAST-CBC:CAST", cast5128cbc_functions), + ALG("CAST5-OFB", cast564ofb64_functions), + ALG("CAST5-CFB", cast564cfb64_functions), +#endif /* OPENSSL_NO_CAST */ +#ifndef OPENSSL_NO_BF + ALG("BF-ECB", blowfish128ecb_functions), + ALG("BF-CBC:BF:BLOWFISH", blowfish128cbc_functions), + ALG("BF-OFB", blowfish64ofb64_functions), + ALG("BF-CFB", blowfish64cfb64_functions), +#endif /* OPENSSL_NO_BF */ +#ifndef OPENSSL_NO_IDEA + ALG("IDEA-ECB", idea128ecb_functions), + ALG("IDEA-CBC:IDEA", idea128cbc_functions), + ALG("IDEA-OFB:IDEA-OFB64", idea128ofb64_functions), + ALG("IDEA-CFB:IDEA-CFB64", idea128cfb64_functions), +#endif /* OPENSSL_NO_IDEA */ +#ifndef OPENSSL_NO_SEED + ALG("SEED-ECB", seed128ecb_functions), + ALG("SEED-CBC:SEED", seed128cbc_functions), + ALG("SEED-OFB:SEED-OFB128", seed128ofb128_functions), + ALG("SEED-CFB:SEED-CFB128", seed128cfb128_functions), +#endif /* OPENSSL_NO_SEED */ +#ifndef OPENSSL_NO_RC2 + ALG("RC2-ECB", rc2128ecb_functions), + ALG("RC2-CBC", rc2128cbc_functions), + ALG("RC2-40-CBC", rc240cbc_functions), + ALG("RC2-64-CBC", rc264cbc_functions), + ALG("RC2-CFB", rc2128cfb128_functions), + ALG("RC2-OFB", rc2128ofb128_functions), +#endif /* OPENSSL_NO_RC2 */ +#ifndef OPENSSL_NO_RC4 + ALG("RC4", rc4128_functions), + ALG("RC4-40", rc440_functions), +# ifndef OPENSSL_NO_MD5 + ALG("RC4-HMAC-MD5", rc4_hmac_md5_functions), +# endif /* OPENSSL_NO_MD5 */ +#endif /* OPENSSL_NO_RC4 */ +#ifndef OPENSSL_NO_RC5 + ALG("RC5-ECB", rc5128ecb_functions), + ALG("RC5-CBC", rc5128cbc_functions), + ALG("RC5-OFB", rc5128ofb64_functions), + ALG("RC5-CFB", rc5128cfb64_functions), +#endif /* OPENSSL_NO_RC5 */ +#ifndef OPENSSL_NO_DES + ALG("DESX-CBC:DESX", tdes_desx_cbc_functions), + ALG("DES-ECB", des_ecb_functions), + ALG("DES-CBC:DES", des_cbc_functions), + ALG("DES-OFB", des_ofb64_functions), + ALG("DES-CFB", des_cfb64_functions), + ALG("DES-CFB1", des_cfb1_functions), + ALG("DES-CFB8", des_cfb8_functions), +#endif /* OPENSSL_NO_DES */ { NULL, NULL, NULL } }; @@ -86,6 +141,8 @@ static const OSSL_ALGORITHM *legacy_query(OSSL_PROVIDER *prov, switch (operation_id) { case OSSL_OP_DIGEST: return legacy_digests; + case OSSL_OP_CIPHER: + return legacy_ciphers; } return NULL; } |