Convert all {NAME}err() in providers/ to their corresponding ERR_raise() call
[openssl.git] / providers / implementations / rands / drbg_ctr.c
index 14f8b9fbc84933bbe7ebac004e46b88eeae184d0..6f9dc658d7308639aeda309b98026c9003e8da14 100644 (file)
 #include "prov/providercommonerr.h"
 #include "drbg_local.h"
 
-static OSSL_OP_rand_newctx_fn drbg_ctr_new_wrapper;
-static OSSL_OP_rand_freectx_fn drbg_ctr_free;
-static OSSL_OP_rand_instantiate_fn drbg_ctr_instantiate_wrapper;
-static OSSL_OP_rand_uninstantiate_fn drbg_ctr_uninstantiate_wrapper;
-static OSSL_OP_rand_generate_fn drbg_ctr_generate_wrapper;
-static OSSL_OP_rand_reseed_fn drbg_ctr_reseed_wrapper;
-static OSSL_OP_rand_settable_ctx_params_fn drbg_ctr_settable_ctx_params;
-static OSSL_OP_rand_set_ctx_params_fn drbg_ctr_set_ctx_params;
-static OSSL_OP_rand_gettable_ctx_params_fn drbg_ctr_gettable_ctx_params;
-static OSSL_OP_rand_get_ctx_params_fn drbg_ctr_get_ctx_params;
-static OSSL_OP_rand_verify_zeroization_fn drbg_ctr_verify_zeroization;
+static OSSL_FUNC_rand_newctx_fn drbg_ctr_new_wrapper;
+static OSSL_FUNC_rand_freectx_fn drbg_ctr_free;
+static OSSL_FUNC_rand_instantiate_fn drbg_ctr_instantiate_wrapper;
+static OSSL_FUNC_rand_uninstantiate_fn drbg_ctr_uninstantiate_wrapper;
+static OSSL_FUNC_rand_generate_fn drbg_ctr_generate_wrapper;
+static OSSL_FUNC_rand_reseed_fn drbg_ctr_reseed_wrapper;
+static OSSL_FUNC_rand_settable_ctx_params_fn drbg_ctr_settable_ctx_params;
+static OSSL_FUNC_rand_set_ctx_params_fn drbg_ctr_set_ctx_params;
+static OSSL_FUNC_rand_gettable_ctx_params_fn drbg_ctr_gettable_ctx_params;
+static OSSL_FUNC_rand_get_ctx_params_fn drbg_ctr_get_ctx_params;
+static OSSL_FUNC_rand_verify_zeroization_fn drbg_ctr_verify_zeroization;
 
 /*
  * The state of a DRBG AES-CTR.
@@ -330,8 +330,8 @@ static int drbg_ctr_instantiate_wrapper(void *vdrbg, unsigned int strength,
 {
     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
 
-    return PROV_DRBG_instantiate(drbg, strength, prediction_resistance,
-                                 pstr, pstr_len);
+    return ossl_prov_drbg_instantiate(drbg, strength, prediction_resistance,
+                                      pstr, pstr_len);
 }
 
 static int drbg_ctr_reseed(PROV_DRBG *drbg,
@@ -355,8 +355,8 @@ static int drbg_ctr_reseed_wrapper(void *vdrbg, int prediction_resistance,
 {
     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
 
-    return PROV_DRBG_reseed(drbg, prediction_resistance, ent, ent_len,
-                            adin, adin_len);
+    return ossl_prov_drbg_reseed(drbg, prediction_resistance, ent, ent_len,
+                                 adin, adin_len);
 }
 
 static void ctr96_inc(unsigned char *counter)
@@ -452,8 +452,8 @@ static int drbg_ctr_generate_wrapper
 {
     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
 
-    return PROV_DRBG_generate(drbg, out, outlen, strength,
-                              prediction_resistance, adin, adin_len);
+    return ossl_prov_drbg_generate(drbg, out, outlen, strength,
+                                   prediction_resistance, adin, adin_len);
 }
 
 static int drbg_ctr_uninstantiate(PROV_DRBG *drbg)
@@ -465,7 +465,7 @@ static int drbg_ctr_uninstantiate(PROV_DRBG *drbg)
     OPENSSL_cleanse(ctr->bltmp, sizeof(ctr->bltmp));
     OPENSSL_cleanse(ctr->KX, sizeof(ctr->KX));
     ctr->bltmp_pos = 0;
-    return PROV_DRBG_uninstantiate(drbg);
+    return ossl_prov_drbg_uninstantiate(drbg);
 }
 
 static int drbg_ctr_uninstantiate_wrapper(void *vdrbg)
@@ -494,7 +494,7 @@ static int drbg_ctr_init_lengths(PROV_DRBG *drbg)
 
 #ifdef FIPS_MODULE
     if (!ctr->use_df) {
-        PROVerr(0, RAND_R_DERIVATION_FUNCTION_MANDATORY_FOR_FIPS);
+        ERR_raise(ERR_LIB_PROV, RAND_R_DERIVATION_FUNCTION_MANDATORY_FOR_FIPS);
         ctr->use_df = 1;
         res = 0;
     }
@@ -530,9 +530,13 @@ static int drbg_ctr_init_lengths(PROV_DRBG *drbg)
 static int drbg_ctr_init(PROV_DRBG *drbg)
 {
     PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
-    const size_t keylen = EVP_CIPHER_key_length(ctr->cipher_ctr);
+    size_t keylen;
 
-    ctr->keylen = keylen;
+    if (ctr->cipher_ctr == NULL) {
+        ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CIPHER);
+        return 0;
+    }
+    ctr->keylen = keylen = EVP_CIPHER_key_length(ctr->cipher_ctr);
     if (ctr->ctx_ecb == NULL)
         ctr->ctx_ecb = EVP_CIPHER_CTX_new();
     if (ctr->ctx_ctr == NULL)
@@ -542,39 +546,37 @@ static int drbg_ctr_init(PROV_DRBG *drbg)
         goto err;
     }
 
-    if (ctr->cipher_ctr != NULL) {
-        if (!EVP_CipherInit_ex(ctr->ctx_ecb,
-                               ctr->cipher_ecb, NULL, NULL, NULL, 1)
-            || !EVP_CipherInit_ex(ctr->ctx_ctr,
-                                  ctr->cipher_ctr, NULL, NULL, NULL, 1)) {
-            ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_INITIALISE_CIPHERS);
-            goto err;
-        }
+    if (!EVP_CipherInit_ex(ctr->ctx_ecb,
+                           ctr->cipher_ecb, NULL, NULL, NULL, 1)
+        || !EVP_CipherInit_ex(ctr->ctx_ctr,
+                              ctr->cipher_ctr, NULL, NULL, NULL, 1)) {
+        ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_INITIALISE_CIPHERS);
+        goto err;
+    }
 
-        drbg->strength = keylen * 8;
-        drbg->seedlen = keylen + 16;
+    drbg->strength = keylen * 8;
+    drbg->seedlen = keylen + 16;
 
-        if (ctr->use_df) {
-            /* df initialisation */
-            static const unsigned char df_key[32] = {
-                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
-                0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
-                0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
-                0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
-            };
-
-            if (ctr->ctx_df == NULL)
-                ctr->ctx_df = EVP_CIPHER_CTX_new();
-            if (ctr->ctx_df == NULL) {
-                ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
-                goto err;
-            }
-            /* Set key schedule for df_key */
-            if (!EVP_CipherInit_ex(ctr->ctx_df,
-                                   ctr->cipher_ecb, NULL, df_key, NULL, 1)) {
-                ERR_raise(ERR_LIB_PROV, PROV_R_DERIVATION_FUNCTION_INIT_FAILED);
-                goto err;
-            }
+    if (ctr->use_df) {
+        /* df initialisation */
+        static const unsigned char df_key[32] = {
+            0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
+            0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
+            0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
+            0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
+        };
+
+        if (ctr->ctx_df == NULL)
+            ctr->ctx_df = EVP_CIPHER_CTX_new();
+        if (ctr->ctx_df == NULL) {
+            ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
+            goto err;
+        }
+        /* Set key schedule for df_key */
+        if (!EVP_CipherInit_ex(ctr->ctx_df,
+                               ctr->cipher_ecb, NULL, df_key, NULL, 1)) {
+            ERR_raise(ERR_LIB_PROV, PROV_R_DERIVATION_FUNCTION_INIT_FAILED);
+            goto err;
         }
     }
     return drbg_ctr_init_lengths(drbg);
@@ -629,14 +631,29 @@ static void drbg_ctr_free(void *vdrbg)
 static int drbg_ctr_get_ctx_params(void *vdrbg, OSSL_PARAM params[])
 {
     PROV_DRBG *drbg = (PROV_DRBG *)vdrbg;
+    PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)drbg->data;
+    OSSL_PARAM *p;
+
+    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_USE_DF);
+    if (p != NULL && !OSSL_PARAM_set_int(p, ctr->use_df))
+        return 0;
+
+    p = OSSL_PARAM_locate(params, OSSL_DRBG_PARAM_CIPHER);
+    if (p != NULL) {
+        if (ctr->cipher_ctr == NULL
+            || !OSSL_PARAM_set_utf8_string(p, EVP_CIPHER_name(ctr->cipher_ctr)))
+            return 0;
+    }
 
     return drbg_get_ctx_params(drbg, params);
 }
 
-static const OSSL_PARAM *drbg_ctr_gettable_ctx_params(void)
+static const OSSL_PARAM *drbg_ctr_gettable_ctx_params(ossl_unused void *provctx)
 {
     static const OSSL_PARAM known_gettable_ctx_params[] = {
-        OSSL_PARAM_DRBG_GETABLE_CTX_COMMON,
+        OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_CIPHER, NULL, 0),
+        OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),
+        OSSL_PARAM_DRBG_GETTABLE_CTX_COMMON,
         OSSL_PARAM_END
     };
     return known_gettable_ctx_params;
@@ -646,7 +663,7 @@ static int drbg_ctr_set_ctx_params(void *vctx, const OSSL_PARAM params[])
 {
     PROV_DRBG *ctx = (PROV_DRBG *)vctx;
     PROV_DRBG_CTR *ctr = (PROV_DRBG_CTR *)ctx->data;
-    OPENSSL_CTX *libctx = PROV_LIBRARY_CONTEXT_OF(ctx->provctx);
+    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
     const OSSL_PARAM *p;
     char *ecb;
     const char *propquery = NULL;
@@ -699,7 +716,7 @@ static int drbg_ctr_set_ctx_params(void *vctx, const OSSL_PARAM params[])
     return drbg_set_ctx_params(ctx, params);
 }
 
-static const OSSL_PARAM *drbg_ctr_settable_ctx_params(void)
+static const OSSL_PARAM *drbg_ctr_settable_ctx_params(ossl_unused void *provctx)
 {
     static const OSSL_PARAM known_settable_ctx_params[] = {
         OSSL_PARAM_utf8_string(OSSL_DRBG_PARAM_PROPERTIES, NULL, 0),
@@ -712,13 +729,13 @@ static const OSSL_PARAM *drbg_ctr_settable_ctx_params(void)
          */
         OSSL_PARAM_int(OSSL_DRBG_PARAM_USE_DF, NULL),
 #endif
-        OSSL_PARAM_DRBG_SETABLE_CTX_COMMON,
+        OSSL_PARAM_DRBG_SETTABLE_CTX_COMMON,
         OSSL_PARAM_END
     };
     return known_settable_ctx_params;
 }
 
-const OSSL_DISPATCH drbg_ctr_functions[] = {
+const OSSL_DISPATCH ossl_drbg_ctr_functions[] = {
     { OSSL_FUNC_RAND_NEWCTX, (void(*)(void))drbg_ctr_new_wrapper },
     { OSSL_FUNC_RAND_FREECTX, (void(*)(void))drbg_ctr_free },
     { OSSL_FUNC_RAND_INSTANTIATE,
@@ -736,7 +753,6 @@ const OSSL_DISPATCH drbg_ctr_functions[] = {
     { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
       (void(*)(void))drbg_ctr_gettable_ctx_params },
     { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))drbg_ctr_get_ctx_params },
-    { OSSL_FUNC_RAND_SET_CALLBACKS, (void(*)(void))drbg_set_callbacks },
     { OSSL_FUNC_RAND_VERIFY_ZEROIZATION,
       (void(*)(void))drbg_ctr_verify_zeroization },
     { 0, NULL }