prov: upport modified gettable/settable ctx calls for ciphers
[openssl.git] / providers / implementations / ciphers / cipher_rc4_hmac_md5.c
index 3b50517c9e363bd4edbc2b69871023aa7574e658..533820cd80d091954d5e3565b3b2b401c12934c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2019-2021 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
  */
 #include "internal/deprecated.h"
 
+#include <openssl/proverr.h>
 #include "cipher_rc4_hmac_md5.h"
 #include "prov/implementations.h"
-#include "prov/providercommonerr.h"
+#include "prov/providercommon.h"
 
-/* TODO(3.0) Figure out what flags are required */
-#define RC4_HMAC_MD5_FLAGS (EVP_CIPH_STREAM_CIPHER | EVP_CIPH_VARIABLE_LENGTH  \
-                            | EVP_CIPH_FLAG_AEAD_CIPHER)
+#define RC4_HMAC_MD5_FLAGS (PROV_CIPHER_FLAG_VARIABLE_LENGTH                   \
+                            | PROV_CIPHER_FLAG_AEAD)
 
 #define RC4_HMAC_MD5_KEY_BITS (16 * 8)
 #define RC4_HMAC_MD5_BLOCK_BITS (1 * 8)
 
 #define GET_HW(ctx) ((PROV_CIPHER_HW_RC4_HMAC_MD5 *)ctx->base.hw)
 
-static OSSL_OP_cipher_newctx_fn rc4_hmac_md5_newctx;
-static OSSL_OP_cipher_freectx_fn rc4_hmac_md5_freectx;
-static OSSL_OP_cipher_get_ctx_params_fn rc4_hmac_md5_get_ctx_params;
-static OSSL_OP_cipher_gettable_ctx_params_fn rc4_hmac_md5_gettable_ctx_params;
-static OSSL_OP_cipher_set_ctx_params_fn rc4_hmac_md5_set_ctx_params;
-static OSSL_OP_cipher_settable_ctx_params_fn rc4_hmac_md5_settable_ctx_params;
-static OSSL_OP_cipher_get_params_fn rc4_hmac_md5_get_params;
-#define rc4_hmac_md5_gettable_params cipher_generic_gettable_params
-#define rc4_hmac_md5_einit cipher_generic_einit
-#define rc4_hmac_md5_dinit cipher_generic_dinit
-#define rc4_hmac_md5_update cipher_generic_stream_update
-#define rc4_hmac_md5_final cipher_generic_stream_final
-#define rc4_hmac_md5_cipher cipher_generic_cipher
+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;
+static OSSL_FUNC_cipher_gettable_ctx_params_fn rc4_hmac_md5_gettable_ctx_params;
+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
 
 static void *rc4_hmac_md5_newctx(void *provctx)
 {
-    PROV_RC4_HMAC_MD5_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
+    PROV_RC4_HMAC_MD5_CTX *ctx;
 
+    if (!ossl_prov_is_running())
+        return NULL;
+
+    ctx = OPENSSL_zalloc(sizeof(*ctx));
     if (ctx != NULL)
-        cipher_generic_initkey(ctx, RC4_HMAC_MD5_KEY_BITS,
-                               RC4_HMAC_MD5_BLOCK_BITS,
-                               RC4_HMAC_MD5_IV_BITS,
-                               RC4_HMAC_MD5_MODE, RC4_HMAC_MD5_FLAGS,
-                               PROV_CIPHER_HW_rc4_hmac_md5(RC4_HMAC_MD5_KEY_BITS),
-                               NULL);
+        ossl_cipher_generic_initkey(ctx, RC4_HMAC_MD5_KEY_BITS,
+                                    RC4_HMAC_MD5_BLOCK_BITS,
+                                    RC4_HMAC_MD5_IV_BITS,
+                                    RC4_HMAC_MD5_MODE, RC4_HMAC_MD5_FLAGS,
+                                    ossl_prov_cipher_hw_rc4_hmac_md5(
+                                        RC4_HMAC_MD5_KEY_BITS
+                                    ), NULL);
      return ctx;
 }
 
@@ -62,6 +67,7 @@ static void rc4_hmac_md5_freectx(void *vctx)
 {
     PROV_RC4_HMAC_MD5_CTX *ctx = (PROV_RC4_HMAC_MD5_CTX *)vctx;
 
+    ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
     OPENSSL_clear_free(ctx,  sizeof(*ctx));
 }
 
@@ -71,7 +77,8 @@ static const OSSL_PARAM rc4_hmac_md5_known_gettable_ctx_params[] = {
     OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD_PAD, NULL),
     OSSL_PARAM_END
 };
-const OSSL_PARAM *rc4_hmac_md5_gettable_ctx_params(void)
+const OSSL_PARAM *rc4_hmac_md5_gettable_ctx_params(ossl_unused void *cctx,
+                                                   ossl_unused void *provctx)
 {
     return rc4_hmac_md5_known_gettable_ctx_params;
 }
@@ -106,7 +113,8 @@ static const OSSL_PARAM rc4_hmac_md5_known_settable_ctx_params[] = {
     OSSL_PARAM_octet_string(OSSL_CIPHER_PARAM_AEAD_TLS1_AAD, NULL, 0),
     OSSL_PARAM_END
 };
-const OSSL_PARAM *rc4_hmac_md5_settable_ctx_params(void)
+const OSSL_PARAM *rc4_hmac_md5_settable_ctx_params(ossl_unused void *cctx,
+                                                   ossl_unused void *provctx)
 {
     return rc4_hmac_md5_known_settable_ctx_params;
 }
@@ -162,19 +170,27 @@ static int rc4_hmac_md5_set_ctx_params(void *vctx, const OSSL_PARAM params[])
         }
         GET_HW(ctx)->init_mackey(&ctx->base, p->data, p->data_size);
     }
+    p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_TLS_VERSION);
+    if (p != NULL) {
+        if (!OSSL_PARAM_get_uint(p, &ctx->base.tlsversion)) {
+            ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
+            return 0;
+        }
+    }
+
     return 1;
 }
 
 static int rc4_hmac_md5_get_params(OSSL_PARAM params[])
 {
-    return cipher_generic_get_params(params, RC4_HMAC_MD5_MODE,
-                                     RC4_HMAC_MD5_FLAGS,
-                                     RC4_HMAC_MD5_KEY_BITS,
-                                     RC4_HMAC_MD5_BLOCK_BITS,
-                                     RC4_HMAC_MD5_IV_BITS);
+    return ossl_cipher_generic_get_params(params, RC4_HMAC_MD5_MODE,
+                                          RC4_HMAC_MD5_FLAGS,
+                                          RC4_HMAC_MD5_KEY_BITS,
+                                          RC4_HMAC_MD5_BLOCK_BITS,
+                                          RC4_HMAC_MD5_IV_BITS);
 }
 
-const OSSL_DISPATCH rc4_hmac_md5_functions[] = {
+const OSSL_DISPATCH ossl_rc4_hmac_ossl_md5_functions[] = {
     { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))rc4_hmac_md5_newctx },
     { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))rc4_hmac_md5_freectx },
     { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))rc4_hmac_md5_einit },