/*
* Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
*
- * Licensed under the OpenSSL license (the "License"). You may not use
+ * 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
*/
+/*
+ * ECDSA low level APIs are deprecated for public use, but still ok for
+ * internal use.
+ */
+#include "internal/deprecated.h"
+
#include "internal/cryptlib.h"
#include <openssl/asn1t.h>
#include <openssl/ec.h>
#include <openssl/evp.h>
-#include "internal/evp_int.h"
-#include "internal/sm2.h"
-#include "internal/sm2err.h"
+#include "crypto/evp.h"
+#include "crypto/sm2.h"
+#include "crypto/sm2err.h"
/* EC pkey context structure */
typedef struct {
- /* Key and paramgen group */
- EC_GROUP *gen_group;
/* message digest */
const EVP_MD *md;
- /* Distinguishing Identifier, ISO/IEC 15946-3 */
+ /* Distinguishing Identifier, ISO/IEC 15946-3, FIPS 196 */
uint8_t *id;
size_t id_len;
/* id_set indicates if the 'id' field is set (1) or not (0) */
SM2_PKEY_CTX *smctx = ctx->data;
if (smctx != NULL) {
- EC_GROUP_free(smctx->gen_group);
OPENSSL_free(smctx->id);
OPENSSL_free(smctx);
ctx->data = NULL;
}
}
-static int pkey_sm2_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
+static int pkey_sm2_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
{
SM2_PKEY_CTX *dctx, *sctx;
return 0;
sctx = src->data;
dctx = dst->data;
- if (sctx->gen_group != NULL) {
- dctx->gen_group = EC_GROUP_dup(sctx->gen_group);
- if (dctx->gen_group == NULL) {
- pkey_sm2_cleanup(dst);
- return 0;
- }
- }
if (sctx->id != NULL) {
dctx->id = OPENSSL_malloc(sctx->id_len);
if (dctx->id == NULL) {
static int pkey_sm2_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
{
SM2_PKEY_CTX *smctx = ctx->data;
- EC_GROUP *group;
uint8_t *tmp_id;
switch (type) {
case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID:
- group = EC_GROUP_new_by_curve_name(p1);
- if (group == NULL) {
+ /*
+ * This control could be removed, which would signal it being
+ * unsupported. However, that means that when the caller uses
+ * the correct curve, it may interpret the unsupported signal
+ * as an error, so it's better to accept the control, check the
+ * value and return a corresponding value.
+ */
+ if (p1 != NID_sm2) {
SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_INVALID_CURVE);
return 0;
}
- EC_GROUP_free(smctx->gen_group);
- smctx->gen_group = group;
- return 1;
-
- case EVP_PKEY_CTRL_EC_PARAM_ENC:
- if (smctx->gen_group == NULL) {
- SM2err(SM2_F_PKEY_SM2_CTRL, SM2_R_NO_PARAMETERS_SET);
- return 0;
- }
- EC_GROUP_set_asn1_flag(smctx->gen_group, p1);
return 1;
case EVP_PKEY_CTRL_MD:
*(size_t *)p2 = smctx->id_len;
return 1;
+ case EVP_PKEY_CTRL_DIGESTINIT:
+ /* nothing to be inited, this is to suppress the error... */
+ return 1;
+
default:
return -2;
}
static int pkey_sm2_ctrl_str(EVP_PKEY_CTX *ctx,
const char *type, const char *value)
{
+ uint8_t *hex_id;
+ long hex_len = 0;
+ int ret = 0;
+
if (strcmp(type, "ec_paramgen_curve") == 0) {
int nid = NID_undef;
else
return -2;
return EVP_PKEY_CTX_set_ec_param_enc(ctx, param_enc);
+ } else if (strcmp(type, "distid") == 0) {
+ return pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID,
+ (int)strlen(value), (void *)value);
+ } else if (strcmp(type, "hexdistid") == 0) {
+ hex_id = OPENSSL_hexstr2buf((const char *)value, &hex_len);
+ if (hex_id == NULL) {
+ SM2err(SM2_F_PKEY_SM2_CTRL_STR, ERR_R_PASSED_INVALID_ARGUMENT);
+ return 0;
+ }
+ ret = pkey_sm2_ctrl(ctx, EVP_PKEY_CTRL_SET1_ID, (int)hex_len,
+ (void *)hex_id);
+ OPENSSL_free(hex_id);
+ return ret;
}
return -2;
SM2_PKEY_CTX *smctx = ctx->data;
EC_KEY *ec = ctx->pkey->pkey.ec;
const EVP_MD *md = EVP_MD_CTX_md(mctx);
+ int mdlen = EVP_MD_size(md);
if (!smctx->id_set) {
/*
return 0;
}
+ if (mdlen < 0) {
+ SM2err(SM2_F_PKEY_SM2_DIGEST_CUSTOM, SM2_R_INVALID_DIGEST);
+ return 0;
+ }
+
/* get hashed prefix 'z' of tbs message */
if (!sm2_compute_z_digest(z, md, smctx->id, smctx->id_len, ec))
return 0;
- return EVP_DigestUpdate(mctx, z, EVP_MD_size(md));
+ return EVP_DigestUpdate(mctx, z, (size_t)mdlen);
}
-const EVP_PKEY_METHOD sm2_pkey_meth = {
+static int pkey_sm2_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
+{
+ EC_KEY *ec = NULL;
+ int ret;
+
+ ec = EC_KEY_new_by_curve_name(NID_sm2);
+ if (ec == NULL)
+ return 0;
+ if (!ossl_assert(ret = EVP_PKEY_assign_EC_KEY(pkey, ec)))
+ EC_KEY_free(ec);
+ return ret;
+}
+
+static int pkey_sm2_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
+{
+ EC_KEY *ec = NULL;
+
+ ec = EC_KEY_new_by_curve_name(NID_sm2);
+ if (ec == NULL)
+ return 0;
+ if (!ossl_assert(EVP_PKEY_assign_EC_KEY(pkey, ec))) {
+ EC_KEY_free(ec);
+ return 0;
+ }
+ /* Note: if error is returned, we count on caller to free pkey->pkey.ec */
+ if (ctx->pkey != NULL
+ && !EVP_PKEY_copy_parameters(pkey, ctx->pkey))
+ return 0;
+
+ return EC_KEY_generate_key(ec);
+}
+
+static const EVP_PKEY_METHOD sm2_pkey_meth = {
EVP_PKEY_SM2,
0,
pkey_sm2_init,
pkey_sm2_cleanup,
0,
- 0,
+ pkey_sm2_paramgen,
0,
- 0,
+ pkey_sm2_keygen,
0,
pkey_sm2_sign,
pkey_sm2_digest_custom
};
+
+const EVP_PKEY_METHOD *sm2_pkey_method(void)
+{
+ return &sm2_pkey_meth;
+}