Fix up path generation to use OPENSSL_MODULES
[openssl.git] / providers / implementations / asymciphers / sm2_enc.c
index b1f62b03c77ced592f032ff1f2aa62d9620fed73..a9d652be307ee737a670579d9035fe9bf9447d53 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2020 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2020-2023 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
@@ -15,8 +15,8 @@
 #include <openssl/core_names.h>
 #include <openssl/params.h>
 #include <openssl/err.h>
-#include <crypto/sm2.h>
-#include "prov/providercommonerr.h"
+#include <openssl/proverr.h>
+#include "crypto/sm2.h"
 #include "prov/provider_ctx.h"
 #include "prov/implementations.h"
 #include "prov/provider_util.h"
@@ -56,7 +56,7 @@ static void *sm2_newctx(void *provctx)
     return psm2ctx;
 }
 
-static int sm2_init(void *vpsm2ctx, void *vkey)
+static int sm2_init(void *vpsm2ctx, void *vkey, const OSSL_PARAM params[])
 {
     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
 
@@ -65,7 +65,7 @@ static int sm2_init(void *vpsm2ctx, void *vkey)
     EC_KEY_free(psm2ctx->key);
     psm2ctx->key = vkey;
 
-    return 1;
+    return sm2_set_ctx_params(psm2ctx, params);
 }
 
 static const EVP_MD *sm2_get_md(PROV_SM2_CTX *psm2ctx)
@@ -89,14 +89,14 @@ static int sm2_asym_encrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,
         return 0;
 
     if (out == NULL) {
-        if (!sm2_ciphertext_size(psm2ctx->key, md, inlen, outlen)) {
+        if (!ossl_sm2_ciphertext_size(psm2ctx->key, md, inlen, outlen)) {
             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY);
             return 0;
         }
         return 1;
     }
 
-    return sm2_encrypt(psm2ctx->key, md, in, inlen, out, outlen);
+    return ossl_sm2_encrypt(psm2ctx->key, md, in, inlen, out, outlen);
 }
 
 static int sm2_asym_decrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,
@@ -110,12 +110,12 @@ static int sm2_asym_decrypt(void *vpsm2ctx, unsigned char *out, size_t *outlen,
         return 0;
 
     if (out == NULL) {
-        if (!sm2_plaintext_size(psm2ctx->key, md, inlen, outlen))
+        if (!ossl_sm2_plaintext_size(in, inlen, outlen))
             return 0;
         return 1;
     }
 
-    return sm2_decrypt(psm2ctx->key, md, in, inlen, out, outlen);
+    return ossl_sm2_decrypt(psm2ctx->key, md, in, inlen, out, outlen);
 }
 
 static void sm2_freectx(void *vpsm2ctx)
@@ -138,6 +138,8 @@ static void *sm2_dupctx(void *vpsm2ctx)
         return NULL;
 
     *dstctx = *srcctx;
+    memset(&dstctx->md, 0, sizeof(dstctx->md));
+
     if (dstctx->key != NULL && !EC_KEY_up_ref(dstctx->key)) {
         OPENSSL_free(dstctx);
         return NULL;
@@ -156,7 +158,7 @@ static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
     OSSL_PARAM *p;
 
-    if (vpsm2ctx == NULL || params == NULL)
+    if (vpsm2ctx == NULL)
         return 0;
 
     p = OSSL_PARAM_locate(params, OSSL_ASYM_CIPHER_PARAM_DIGEST);
@@ -164,7 +166,7 @@ static int sm2_get_ctx_params(void *vpsm2ctx, OSSL_PARAM *params)
         const EVP_MD *md = ossl_prov_digest_md(&psm2ctx->md);
 
         if (!OSSL_PARAM_set_utf8_string(p, md == NULL ? ""
-                                                      : EVP_MD_name(md)))
+                                                      : EVP_MD_get0_name(md)))
             return 0;
     }
 
@@ -176,7 +178,8 @@ static const OSSL_PARAM known_gettable_ctx_params[] = {
     OSSL_PARAM_END
 };
 
-static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *provctx)
+static const OSSL_PARAM *sm2_gettable_ctx_params(ossl_unused void *vpsm2ctx,
+                                                 ossl_unused void *provctx)
 {
     return known_gettable_ctx_params;
 }
@@ -185,8 +188,10 @@ static int sm2_set_ctx_params(void *vpsm2ctx, const OSSL_PARAM params[])
 {
     PROV_SM2_CTX *psm2ctx = (PROV_SM2_CTX *)vpsm2ctx;
 
-    if (psm2ctx == NULL || params == NULL)
+    if (psm2ctx == NULL)
         return 0;
+    if (params == NULL)
+        return 1;
 
     if (!ossl_prov_digest_load_from_params(&psm2ctx->md, params,
                                            psm2ctx->libctx))
@@ -202,12 +207,13 @@ static const OSSL_PARAM known_settable_ctx_params[] = {
     OSSL_PARAM_END
 };
 
-static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *provctx)
+static const OSSL_PARAM *sm2_settable_ctx_params(ossl_unused void *vpsm2ctx,
+                                                 ossl_unused void *provctx)
 {
     return known_settable_ctx_params;
 }
 
-const OSSL_DISPATCH sm2_asym_cipher_functions[] = {
+const OSSL_DISPATCH ossl_sm2_asym_cipher_functions[] = {
     { OSSL_FUNC_ASYM_CIPHER_NEWCTX, (void (*)(void))sm2_newctx },
     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT_INIT, (void (*)(void))sm2_init },
     { OSSL_FUNC_ASYM_CIPHER_ENCRYPT, (void (*)(void))sm2_asym_encrypt },
@@ -223,5 +229,5 @@ const OSSL_DISPATCH sm2_asym_cipher_functions[] = {
       (void (*)(void))sm2_set_ctx_params },
     { OSSL_FUNC_ASYM_CIPHER_SETTABLE_CTX_PARAMS,
       (void (*)(void))sm2_settable_ctx_params },
-    { 0, NULL }
+    OSSL_DISPATCH_END
 };