From 8f42380a21e7732cc0931be1921e24d360285278 Mon Sep 17 00:00:00 2001 From: Pauli Date: Tue, 2 Mar 2021 22:44:25 +1000 Subject: prov: support params argument to RCx ciphers Reviewed-by: Shane Lontis (Merged from https://github.com/openssl/openssl/pull/14383) --- providers/implementations/ciphers/cipher_rc2.c | 32 +++++++++++++++++++--- providers/implementations/ciphers/cipher_rc4.c | 24 ++++++++++++++-- .../implementations/ciphers/cipher_rc4_hmac_md5.c | 25 +++++++++++++++-- providers/implementations/ciphers/cipher_rc5.c | 28 +++++++++++++++++-- providers/implementations/ciphers/ciphercommon.c | 6 ++++ .../implementations/ciphers/ciphercommon_ccm.c | 3 ++ .../implementations/ciphers/ciphercommon_gcm.c | 3 ++ 7 files changed, 111 insertions(+), 10 deletions(-) (limited to 'providers/implementations/ciphers') diff --git a/providers/implementations/ciphers/cipher_rc2.c b/providers/implementations/ciphers/cipher_rc2.c index d78b044247..f8a18462af 100644 --- a/providers/implementations/ciphers/cipher_rc2.c +++ b/providers/implementations/ciphers/cipher_rc2.c @@ -25,10 +25,13 @@ #define RC2_128_MAGIC 0x3a #define RC2_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH +static OSSL_FUNC_cipher_encrypt_init_fn rc2_einit; +static OSSL_FUNC_cipher_decrypt_init_fn rc2_dinit; static OSSL_FUNC_cipher_freectx_fn rc2_freectx; static OSSL_FUNC_cipher_dupctx_fn rc2_dupctx; static OSSL_FUNC_cipher_gettable_ctx_params_fn rc2_gettable_ctx_params; static OSSL_FUNC_cipher_settable_ctx_params_fn rc2_settable_ctx_params; +static OSSL_FUNC_cipher_set_ctx_params_fn rc2_set_ctx_params; static void rc2_freectx(void *vctx) { @@ -84,6 +87,24 @@ static int rc2_magic_to_keybits(int magic) return 0; } +static int rc2_einit(void *ctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return rc2_set_ctx_params(ctx, params); +} + +static int rc2_dinit(void *ctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return rc2_set_ctx_params(ctx, params); +} + static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[]) { PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx; @@ -138,11 +159,14 @@ static int rc2_get_ctx_params(void *vctx, OSSL_PARAM params[]) return 1; } -static int rc2_set_ctx_params(void *vctx, OSSL_PARAM params[]) +static int rc2_set_ctx_params(void *vctx, const OSSL_PARAM params[]) { PROV_RC2_CTX *ctx = (PROV_RC2_CTX *)vctx; const OSSL_PARAM *p; + if (params == NULL) + return 1; + if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params)) return 0; p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_RC2_KEYBITS); @@ -152,7 +176,7 @@ static int rc2_set_ctx_params(void *vctx, OSSL_PARAM params[]) return 0; } } - p = OSSL_PARAM_locate(params, OSSL_CIPHER_PARAM_ALG_ID); + p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_ALG_ID); if (p != NULL) { ASN1_TYPE *type = NULL; long num = 0; @@ -222,8 +246,8 @@ const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \ (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ - { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \ - { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \ + { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc2_einit }, \ + { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc2_dinit }, \ { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\ { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \ { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \ diff --git a/providers/implementations/ciphers/cipher_rc4.c b/providers/implementations/ciphers/cipher_rc4.c index 98937c0044..a548beafaf 100644 --- a/providers/implementations/ciphers/cipher_rc4.c +++ b/providers/implementations/ciphers/cipher_rc4.c @@ -21,6 +21,8 @@ #define RC4_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH +static OSSL_FUNC_cipher_encrypt_init_fn rc4_einit; +static OSSL_FUNC_cipher_decrypt_init_fn rc4_dinit; static OSSL_FUNC_cipher_freectx_fn rc4_freectx; static OSSL_FUNC_cipher_dupctx_fn rc4_dupctx; @@ -50,6 +52,24 @@ static void *rc4_dupctx(void *ctx) return ret; } +static int rc4_einit(void *ctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return ossl_cipher_var_keylen_set_ctx_params(ctx, params); +} + +static int rc4_dinit(void *ctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return ossl_cipher_var_keylen_set_ctx_params(ctx, params); +} + #define IMPLEMENT_cipher(alg, UCALG, flags, kbits, blkbits, ivbits, typ) \ static OSSL_FUNC_cipher_get_params_fn alg##_##kbits##_get_params; \ static int alg##_##kbits##_get_params(OSSL_PARAM params[]) \ @@ -75,8 +95,8 @@ const OSSL_DISPATCH ossl_##alg##kbits##_functions[] = { \ (void (*)(void)) alg##_##kbits##_newctx }, \ { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ - { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit }, \ - { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit }, \ + { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_einit }, \ + { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc4_dinit }, \ { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\ { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \ { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \ diff --git a/providers/implementations/ciphers/cipher_rc4_hmac_md5.c b/providers/implementations/ciphers/cipher_rc4_hmac_md5.c index 533820cd80..b915122a39 100644 --- a/providers/implementations/ciphers/cipher_rc4_hmac_md5.c +++ b/providers/implementations/ciphers/cipher_rc4_hmac_md5.c @@ -30,6 +30,8 @@ #define GET_HW(ctx) ((PROV_CIPHER_HW_RC4_HMAC_MD5 *)ctx->base.hw) +static OSSL_FUNC_cipher_encrypt_init_fn rc4_hmac_md5_einit; +static OSSL_FUNC_cipher_decrypt_init_fn rc4_hmac_md5_dinit; static OSSL_FUNC_cipher_newctx_fn rc4_hmac_md5_newctx; static OSSL_FUNC_cipher_freectx_fn rc4_hmac_md5_freectx; static OSSL_FUNC_cipher_get_ctx_params_fn rc4_hmac_md5_get_ctx_params; @@ -38,8 +40,6 @@ static OSSL_FUNC_cipher_set_ctx_params_fn rc4_hmac_md5_set_ctx_params; static OSSL_FUNC_cipher_settable_ctx_params_fn rc4_hmac_md5_settable_ctx_params; static OSSL_FUNC_cipher_get_params_fn rc4_hmac_md5_get_params; #define rc4_hmac_md5_gettable_params ossl_cipher_generic_gettable_params -#define rc4_hmac_md5_einit ossl_cipher_generic_einit -#define rc4_hmac_md5_dinit ossl_cipher_generic_dinit #define rc4_hmac_md5_update ossl_cipher_generic_stream_update #define rc4_hmac_md5_final ossl_cipher_generic_stream_final #define rc4_hmac_md5_cipher ossl_cipher_generic_cipher @@ -71,6 +71,24 @@ static void rc4_hmac_md5_freectx(void *vctx) OPENSSL_clear_free(ctx, sizeof(*ctx)); } +static int rc4_hmac_md5_einit(void *ctx, const unsigned char *key, + size_t keylen, const unsigned char *iv, + size_t ivlen, const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return rc4_hmac_md5_set_ctx_params(ctx, params); +} + +static int rc4_hmac_md5_dinit(void *ctx, const unsigned char *key, + size_t keylen, const unsigned char *iv, + size_t ivlen, const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return rc4_hmac_md5_set_ctx_params(ctx, params); +} + static const OSSL_PARAM rc4_hmac_md5_known_gettable_ctx_params[] = { OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL), OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_IVLEN, NULL), @@ -125,6 +143,9 @@ static int rc4_hmac_md5_set_ctx_params(void *vctx, const OSSL_PARAM params[]) const OSSL_PARAM *p; size_t sz; + if (params == NULL) + return 1; + p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); if (p != NULL) { if (!OSSL_PARAM_get_size_t(p, &sz)) { diff --git a/providers/implementations/ciphers/cipher_rc5.c b/providers/implementations/ciphers/cipher_rc5.c index 0d6f87ed63..5c7d2b1721 100644 --- a/providers/implementations/ciphers/cipher_rc5.c +++ b/providers/implementations/ciphers/cipher_rc5.c @@ -22,10 +22,13 @@ #define RC5_FLAGS PROV_CIPHER_FLAG_VARIABLE_LENGTH +static OSSL_FUNC_cipher_encrypt_init_fn rc5_einit; +static OSSL_FUNC_cipher_decrypt_init_fn rc5_dinit; static OSSL_FUNC_cipher_freectx_fn rc5_freectx; static OSSL_FUNC_cipher_dupctx_fn rc5_dupctx; OSSL_FUNC_cipher_gettable_ctx_params_fn rc5_gettable_ctx_params; OSSL_FUNC_cipher_settable_ctx_params_fn rc5_settable_ctx_params; +static OSSL_FUNC_cipher_set_ctx_params_fn rc5_set_ctx_params; static void rc5_freectx(void *vctx) { @@ -53,11 +56,32 @@ static void *rc5_dupctx(void *ctx) return ret; } +static int rc5_einit(void *ctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_einit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return rc5_set_ctx_params(ctx, params); +} + +static int rc5_dinit(void *ctx, const unsigned char *key, size_t keylen, + const unsigned char *iv, size_t ivlen, + const OSSL_PARAM params[]) +{ + if (!ossl_cipher_generic_dinit(ctx, key, keylen, iv, ivlen, NULL)) + return 0; + return rc5_set_ctx_params(ctx, params); +} + static int rc5_set_ctx_params(void *vctx, const OSSL_PARAM params[]) { PROV_RC5_CTX *ctx = (PROV_RC5_CTX *)vctx; const OSSL_PARAM *p; + if (params == NULL) + return 1; + if (!ossl_cipher_var_keylen_set_ctx_params(vctx, params)) return 0; @@ -134,8 +158,8 @@ const OSSL_DISPATCH ossl_##alg##kbits##lcmode##_functions[] = { \ (void (*)(void)) alg##_##kbits##_##lcmode##_newctx }, \ { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void)) alg##_freectx }, \ { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void)) alg##_dupctx }, \ - { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))ossl_cipher_generic_einit },\ - { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))ossl_cipher_generic_dinit },\ + { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc5_einit }, \ + { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))rc5_dinit }, \ { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))ossl_cipher_generic_##typ##_update },\ { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))ossl_cipher_generic_##typ##_final }, \ { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))ossl_cipher_generic_cipher }, \ diff --git a/providers/implementations/ciphers/ciphercommon.c b/providers/implementations/ciphers/ciphercommon.c index 3d2fb5b8f8..2054615838 100644 --- a/providers/implementations/ciphers/ciphercommon.c +++ b/providers/implementations/ciphers/ciphercommon.c @@ -107,6 +107,9 @@ int ossl_cipher_var_keylen_set_ctx_params(void *vctx, const OSSL_PARAM params[]) PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; const OSSL_PARAM *p; + if (params == NULL) + return 1; + if (!ossl_cipher_generic_set_ctx_params(vctx, params)) return 0; p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN); @@ -574,6 +577,9 @@ int ossl_cipher_generic_set_ctx_params(void *vctx, const OSSL_PARAM params[]) PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx; const OSSL_PARAM *p; + if (params == NULL) + return 1; + p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_PADDING); if (p != NULL) { unsigned int pad; diff --git a/providers/implementations/ciphers/ciphercommon_ccm.c b/providers/implementations/ciphers/ciphercommon_ccm.c index d14a7eb5e6..02618a784a 100644 --- a/providers/implementations/ciphers/ciphercommon_ccm.c +++ b/providers/implementations/ciphers/ciphercommon_ccm.c @@ -71,6 +71,9 @@ int ossl_ccm_set_ctx_params(void *vctx, const OSSL_PARAM params[]) const OSSL_PARAM *p; size_t sz; + if (params == NULL) + return 1; + p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG); if (p != NULL) { if (p->data_type != OSSL_PARAM_OCTET_STRING) { diff --git a/providers/implementations/ciphers/ciphercommon_gcm.c b/providers/implementations/ciphers/ciphercommon_gcm.c index c7b91e7bfa..7fc5bc7dec 100644 --- a/providers/implementations/ciphers/ciphercommon_gcm.c +++ b/providers/implementations/ciphers/ciphercommon_gcm.c @@ -223,6 +223,9 @@ int ossl_gcm_set_ctx_params(void *vctx, const OSSL_PARAM params[]) size_t sz; void *vp; + if (params == NULL) + return 1; + p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_AEAD_TAG); if (p != NULL) { vp = ctx->buf; -- cgit v1.2.3