1*b077aed3SPierre Pronchery /* 2*b077aed3SPierre Pronchery * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. 3*b077aed3SPierre Pronchery * 4*b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use 5*b077aed3SPierre Pronchery * this file except in compliance with the License. You can obtain a copy 6*b077aed3SPierre Pronchery * in the file LICENSE in the source distribution or at 7*b077aed3SPierre Pronchery * https://www.openssl.org/source/license.html 8*b077aed3SPierre Pronchery */ 9*b077aed3SPierre Pronchery 10*b077aed3SPierre Pronchery /* 11*b077aed3SPierre Pronchery * ECDSA low level APIs are deprecated for public use, but still ok for 12*b077aed3SPierre Pronchery * internal use. 13*b077aed3SPierre Pronchery */ 14*b077aed3SPierre Pronchery #include "internal/deprecated.h" 15*b077aed3SPierre Pronchery 16*b077aed3SPierre Pronchery #include <string.h> /* memcpy */ 17*b077aed3SPierre Pronchery #include <openssl/crypto.h> 18*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h> 19*b077aed3SPierre Pronchery #include <openssl/core_names.h> 20*b077aed3SPierre Pronchery #include <openssl/dsa.h> 21*b077aed3SPierre Pronchery #include <openssl/params.h> 22*b077aed3SPierre Pronchery #include <openssl/evp.h> 23*b077aed3SPierre Pronchery #include <openssl/err.h> 24*b077aed3SPierre Pronchery #include <openssl/proverr.h> 25*b077aed3SPierre Pronchery #include "internal/nelem.h" 26*b077aed3SPierre Pronchery #include "internal/sizes.h" 27*b077aed3SPierre Pronchery #include "internal/cryptlib.h" 28*b077aed3SPierre Pronchery #include "prov/providercommon.h" 29*b077aed3SPierre Pronchery #include "prov/implementations.h" 30*b077aed3SPierre Pronchery #include "prov/provider_ctx.h" 31*b077aed3SPierre Pronchery #include "prov/securitycheck.h" 32*b077aed3SPierre Pronchery #include "crypto/ec.h" 33*b077aed3SPierre Pronchery #include "prov/der_ec.h" 34*b077aed3SPierre Pronchery 35*b077aed3SPierre Pronchery static OSSL_FUNC_signature_newctx_fn ecdsa_newctx; 36*b077aed3SPierre Pronchery static OSSL_FUNC_signature_sign_init_fn ecdsa_sign_init; 37*b077aed3SPierre Pronchery static OSSL_FUNC_signature_verify_init_fn ecdsa_verify_init; 38*b077aed3SPierre Pronchery static OSSL_FUNC_signature_sign_fn ecdsa_sign; 39*b077aed3SPierre Pronchery static OSSL_FUNC_signature_verify_fn ecdsa_verify; 40*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_init_fn ecdsa_digest_sign_init; 41*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_update_fn ecdsa_digest_signverify_update; 42*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_final_fn ecdsa_digest_sign_final; 43*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_init_fn ecdsa_digest_verify_init; 44*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_update_fn ecdsa_digest_signverify_update; 45*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_final_fn ecdsa_digest_verify_final; 46*b077aed3SPierre Pronchery static OSSL_FUNC_signature_freectx_fn ecdsa_freectx; 47*b077aed3SPierre Pronchery static OSSL_FUNC_signature_dupctx_fn ecdsa_dupctx; 48*b077aed3SPierre Pronchery static OSSL_FUNC_signature_get_ctx_params_fn ecdsa_get_ctx_params; 49*b077aed3SPierre Pronchery static OSSL_FUNC_signature_gettable_ctx_params_fn ecdsa_gettable_ctx_params; 50*b077aed3SPierre Pronchery static OSSL_FUNC_signature_set_ctx_params_fn ecdsa_set_ctx_params; 51*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn ecdsa_settable_ctx_params; 52*b077aed3SPierre Pronchery static OSSL_FUNC_signature_get_ctx_md_params_fn ecdsa_get_ctx_md_params; 53*b077aed3SPierre Pronchery static OSSL_FUNC_signature_gettable_ctx_md_params_fn ecdsa_gettable_ctx_md_params; 54*b077aed3SPierre Pronchery static OSSL_FUNC_signature_set_ctx_md_params_fn ecdsa_set_ctx_md_params; 55*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_md_params_fn ecdsa_settable_ctx_md_params; 56*b077aed3SPierre Pronchery 57*b077aed3SPierre Pronchery /* 58*b077aed3SPierre Pronchery * What's passed as an actual key is defined by the KEYMGMT interface. 59*b077aed3SPierre Pronchery * We happen to know that our KEYMGMT simply passes DSA structures, so 60*b077aed3SPierre Pronchery * we use that here too. 61*b077aed3SPierre Pronchery */ 62*b077aed3SPierre Pronchery 63*b077aed3SPierre Pronchery typedef struct { 64*b077aed3SPierre Pronchery OSSL_LIB_CTX *libctx; 65*b077aed3SPierre Pronchery char *propq; 66*b077aed3SPierre Pronchery EC_KEY *ec; 67*b077aed3SPierre Pronchery char mdname[OSSL_MAX_NAME_SIZE]; 68*b077aed3SPierre Pronchery 69*b077aed3SPierre Pronchery /* 70*b077aed3SPierre Pronchery * Flag to determine if the hash function can be changed (1) or not (0) 71*b077aed3SPierre Pronchery * Because it's dangerous to change during a DigestSign or DigestVerify 72*b077aed3SPierre Pronchery * operation, this flag is cleared by their Init function, and set again 73*b077aed3SPierre Pronchery * by their Final function. 74*b077aed3SPierre Pronchery */ 75*b077aed3SPierre Pronchery unsigned int flag_allow_md : 1; 76*b077aed3SPierre Pronchery 77*b077aed3SPierre Pronchery /* The Algorithm Identifier of the combined signature algorithm */ 78*b077aed3SPierre Pronchery unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE]; 79*b077aed3SPierre Pronchery unsigned char *aid; 80*b077aed3SPierre Pronchery size_t aid_len; 81*b077aed3SPierre Pronchery size_t mdsize; 82*b077aed3SPierre Pronchery int operation; 83*b077aed3SPierre Pronchery 84*b077aed3SPierre Pronchery EVP_MD *md; 85*b077aed3SPierre Pronchery EVP_MD_CTX *mdctx; 86*b077aed3SPierre Pronchery /* 87*b077aed3SPierre Pronchery * Internally used to cache the results of calling the EC group 88*b077aed3SPierre Pronchery * sign_setup() methods which are then passed to the sign operation. 89*b077aed3SPierre Pronchery * This is used by CAVS failure tests to terminate a loop if the signature 90*b077aed3SPierre Pronchery * is not valid. 91*b077aed3SPierre Pronchery * This could of also been done with a simple flag. 92*b077aed3SPierre Pronchery */ 93*b077aed3SPierre Pronchery BIGNUM *kinv; 94*b077aed3SPierre Pronchery BIGNUM *r; 95*b077aed3SPierre Pronchery #if !defined(OPENSSL_NO_ACVP_TESTS) 96*b077aed3SPierre Pronchery /* 97*b077aed3SPierre Pronchery * This indicates that KAT (CAVS) test is running. Externally an app will 98*b077aed3SPierre Pronchery * override the random callback such that the generated private key and k 99*b077aed3SPierre Pronchery * are known. 100*b077aed3SPierre Pronchery * Normal operation will loop to choose a new k if the signature is not 101*b077aed3SPierre Pronchery * valid - but for this mode of operation it forces a failure instead. 102*b077aed3SPierre Pronchery */ 103*b077aed3SPierre Pronchery unsigned int kattest; 104*b077aed3SPierre Pronchery #endif 105*b077aed3SPierre Pronchery } PROV_ECDSA_CTX; 106*b077aed3SPierre Pronchery 107*b077aed3SPierre Pronchery static void *ecdsa_newctx(void *provctx, const char *propq) 108*b077aed3SPierre Pronchery { 109*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx; 110*b077aed3SPierre Pronchery 111*b077aed3SPierre Pronchery if (!ossl_prov_is_running()) 112*b077aed3SPierre Pronchery return NULL; 113*b077aed3SPierre Pronchery 114*b077aed3SPierre Pronchery ctx = OPENSSL_zalloc(sizeof(PROV_ECDSA_CTX)); 115*b077aed3SPierre Pronchery if (ctx == NULL) 116*b077aed3SPierre Pronchery return NULL; 117*b077aed3SPierre Pronchery 118*b077aed3SPierre Pronchery ctx->flag_allow_md = 1; 119*b077aed3SPierre Pronchery ctx->libctx = PROV_LIBCTX_OF(provctx); 120*b077aed3SPierre Pronchery if (propq != NULL && (ctx->propq = OPENSSL_strdup(propq)) == NULL) { 121*b077aed3SPierre Pronchery OPENSSL_free(ctx); 122*b077aed3SPierre Pronchery ctx = NULL; 123*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); 124*b077aed3SPierre Pronchery } 125*b077aed3SPierre Pronchery return ctx; 126*b077aed3SPierre Pronchery } 127*b077aed3SPierre Pronchery 128*b077aed3SPierre Pronchery static int ecdsa_signverify_init(void *vctx, void *ec, 129*b077aed3SPierre Pronchery const OSSL_PARAM params[], int operation) 130*b077aed3SPierre Pronchery { 131*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 132*b077aed3SPierre Pronchery 133*b077aed3SPierre Pronchery if (!ossl_prov_is_running() 134*b077aed3SPierre Pronchery || ctx == NULL) 135*b077aed3SPierre Pronchery return 0; 136*b077aed3SPierre Pronchery 137*b077aed3SPierre Pronchery if (ec == NULL && ctx->ec == NULL) { 138*b077aed3SPierre Pronchery ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET); 139*b077aed3SPierre Pronchery return 0; 140*b077aed3SPierre Pronchery } 141*b077aed3SPierre Pronchery 142*b077aed3SPierre Pronchery if (ec != NULL) { 143*b077aed3SPierre Pronchery if (!ossl_ec_check_key(ctx->libctx, ec, operation == EVP_PKEY_OP_SIGN)) 144*b077aed3SPierre Pronchery return 0; 145*b077aed3SPierre Pronchery if (!EC_KEY_up_ref(ec)) 146*b077aed3SPierre Pronchery return 0; 147*b077aed3SPierre Pronchery EC_KEY_free(ctx->ec); 148*b077aed3SPierre Pronchery ctx->ec = ec; 149*b077aed3SPierre Pronchery } 150*b077aed3SPierre Pronchery 151*b077aed3SPierre Pronchery ctx->operation = operation; 152*b077aed3SPierre Pronchery 153*b077aed3SPierre Pronchery if (!ecdsa_set_ctx_params(ctx, params)) 154*b077aed3SPierre Pronchery return 0; 155*b077aed3SPierre Pronchery 156*b077aed3SPierre Pronchery return 1; 157*b077aed3SPierre Pronchery } 158*b077aed3SPierre Pronchery 159*b077aed3SPierre Pronchery static int ecdsa_sign_init(void *vctx, void *ec, const OSSL_PARAM params[]) 160*b077aed3SPierre Pronchery { 161*b077aed3SPierre Pronchery return ecdsa_signverify_init(vctx, ec, params, EVP_PKEY_OP_SIGN); 162*b077aed3SPierre Pronchery } 163*b077aed3SPierre Pronchery 164*b077aed3SPierre Pronchery static int ecdsa_verify_init(void *vctx, void *ec, const OSSL_PARAM params[]) 165*b077aed3SPierre Pronchery { 166*b077aed3SPierre Pronchery return ecdsa_signverify_init(vctx, ec, params, EVP_PKEY_OP_VERIFY); 167*b077aed3SPierre Pronchery } 168*b077aed3SPierre Pronchery 169*b077aed3SPierre Pronchery static int ecdsa_sign(void *vctx, unsigned char *sig, size_t *siglen, 170*b077aed3SPierre Pronchery size_t sigsize, const unsigned char *tbs, size_t tbslen) 171*b077aed3SPierre Pronchery { 172*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 173*b077aed3SPierre Pronchery int ret; 174*b077aed3SPierre Pronchery unsigned int sltmp; 175*b077aed3SPierre Pronchery size_t ecsize = ECDSA_size(ctx->ec); 176*b077aed3SPierre Pronchery 177*b077aed3SPierre Pronchery if (!ossl_prov_is_running()) 178*b077aed3SPierre Pronchery return 0; 179*b077aed3SPierre Pronchery 180*b077aed3SPierre Pronchery if (sig == NULL) { 181*b077aed3SPierre Pronchery *siglen = ecsize; 182*b077aed3SPierre Pronchery return 1; 183*b077aed3SPierre Pronchery } 184*b077aed3SPierre Pronchery 185*b077aed3SPierre Pronchery #if !defined(OPENSSL_NO_ACVP_TESTS) 186*b077aed3SPierre Pronchery if (ctx->kattest && !ECDSA_sign_setup(ctx->ec, NULL, &ctx->kinv, &ctx->r)) 187*b077aed3SPierre Pronchery return 0; 188*b077aed3SPierre Pronchery #endif 189*b077aed3SPierre Pronchery 190*b077aed3SPierre Pronchery if (sigsize < (size_t)ecsize) 191*b077aed3SPierre Pronchery return 0; 192*b077aed3SPierre Pronchery 193*b077aed3SPierre Pronchery if (ctx->mdsize != 0 && tbslen != ctx->mdsize) 194*b077aed3SPierre Pronchery return 0; 195*b077aed3SPierre Pronchery 196*b077aed3SPierre Pronchery ret = ECDSA_sign_ex(0, tbs, tbslen, sig, &sltmp, ctx->kinv, ctx->r, ctx->ec); 197*b077aed3SPierre Pronchery if (ret <= 0) 198*b077aed3SPierre Pronchery return 0; 199*b077aed3SPierre Pronchery 200*b077aed3SPierre Pronchery *siglen = sltmp; 201*b077aed3SPierre Pronchery return 1; 202*b077aed3SPierre Pronchery } 203*b077aed3SPierre Pronchery 204*b077aed3SPierre Pronchery static int ecdsa_verify(void *vctx, const unsigned char *sig, size_t siglen, 205*b077aed3SPierre Pronchery const unsigned char *tbs, size_t tbslen) 206*b077aed3SPierre Pronchery { 207*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 208*b077aed3SPierre Pronchery 209*b077aed3SPierre Pronchery if (!ossl_prov_is_running() || (ctx->mdsize != 0 && tbslen != ctx->mdsize)) 210*b077aed3SPierre Pronchery return 0; 211*b077aed3SPierre Pronchery 212*b077aed3SPierre Pronchery return ECDSA_verify(0, tbs, tbslen, sig, siglen, ctx->ec); 213*b077aed3SPierre Pronchery } 214*b077aed3SPierre Pronchery 215*b077aed3SPierre Pronchery static int ecdsa_setup_md(PROV_ECDSA_CTX *ctx, const char *mdname, 216*b077aed3SPierre Pronchery const char *mdprops) 217*b077aed3SPierre Pronchery { 218*b077aed3SPierre Pronchery EVP_MD *md = NULL; 219*b077aed3SPierre Pronchery size_t mdname_len; 220*b077aed3SPierre Pronchery int md_nid, sha1_allowed; 221*b077aed3SPierre Pronchery WPACKET pkt; 222*b077aed3SPierre Pronchery 223*b077aed3SPierre Pronchery if (mdname == NULL) 224*b077aed3SPierre Pronchery return 1; 225*b077aed3SPierre Pronchery 226*b077aed3SPierre Pronchery mdname_len = strlen(mdname); 227*b077aed3SPierre Pronchery if (mdname_len >= sizeof(ctx->mdname)) { 228*b077aed3SPierre Pronchery ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, 229*b077aed3SPierre Pronchery "%s exceeds name buffer length", mdname); 230*b077aed3SPierre Pronchery return 0; 231*b077aed3SPierre Pronchery } 232*b077aed3SPierre Pronchery if (mdprops == NULL) 233*b077aed3SPierre Pronchery mdprops = ctx->propq; 234*b077aed3SPierre Pronchery md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); 235*b077aed3SPierre Pronchery if (md == NULL) { 236*b077aed3SPierre Pronchery ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, 237*b077aed3SPierre Pronchery "%s could not be fetched", mdname); 238*b077aed3SPierre Pronchery return 0; 239*b077aed3SPierre Pronchery } 240*b077aed3SPierre Pronchery sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN); 241*b077aed3SPierre Pronchery md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md, 242*b077aed3SPierre Pronchery sha1_allowed); 243*b077aed3SPierre Pronchery if (md_nid < 0) { 244*b077aed3SPierre Pronchery ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, 245*b077aed3SPierre Pronchery "digest=%s", mdname); 246*b077aed3SPierre Pronchery EVP_MD_free(md); 247*b077aed3SPierre Pronchery return 0; 248*b077aed3SPierre Pronchery } 249*b077aed3SPierre Pronchery 250*b077aed3SPierre Pronchery if (!ctx->flag_allow_md) { 251*b077aed3SPierre Pronchery if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) { 252*b077aed3SPierre Pronchery ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, 253*b077aed3SPierre Pronchery "digest %s != %s", mdname, ctx->mdname); 254*b077aed3SPierre Pronchery EVP_MD_free(md); 255*b077aed3SPierre Pronchery return 0; 256*b077aed3SPierre Pronchery } 257*b077aed3SPierre Pronchery EVP_MD_free(md); 258*b077aed3SPierre Pronchery return 1; 259*b077aed3SPierre Pronchery } 260*b077aed3SPierre Pronchery 261*b077aed3SPierre Pronchery EVP_MD_CTX_free(ctx->mdctx); 262*b077aed3SPierre Pronchery EVP_MD_free(ctx->md); 263*b077aed3SPierre Pronchery 264*b077aed3SPierre Pronchery ctx->aid_len = 0; 265*b077aed3SPierre Pronchery if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) 266*b077aed3SPierre Pronchery && ossl_DER_w_algorithmIdentifier_ECDSA_with_MD(&pkt, -1, ctx->ec, 267*b077aed3SPierre Pronchery md_nid) 268*b077aed3SPierre Pronchery && WPACKET_finish(&pkt)) { 269*b077aed3SPierre Pronchery WPACKET_get_total_written(&pkt, &ctx->aid_len); 270*b077aed3SPierre Pronchery ctx->aid = WPACKET_get_curr(&pkt); 271*b077aed3SPierre Pronchery } 272*b077aed3SPierre Pronchery WPACKET_cleanup(&pkt); 273*b077aed3SPierre Pronchery ctx->mdctx = NULL; 274*b077aed3SPierre Pronchery ctx->md = md; 275*b077aed3SPierre Pronchery ctx->mdsize = EVP_MD_get_size(ctx->md); 276*b077aed3SPierre Pronchery OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); 277*b077aed3SPierre Pronchery 278*b077aed3SPierre Pronchery return 1; 279*b077aed3SPierre Pronchery } 280*b077aed3SPierre Pronchery 281*b077aed3SPierre Pronchery static int ecdsa_digest_signverify_init(void *vctx, const char *mdname, 282*b077aed3SPierre Pronchery void *ec, const OSSL_PARAM params[], 283*b077aed3SPierre Pronchery int operation) 284*b077aed3SPierre Pronchery { 285*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 286*b077aed3SPierre Pronchery 287*b077aed3SPierre Pronchery if (!ossl_prov_is_running()) 288*b077aed3SPierre Pronchery return 0; 289*b077aed3SPierre Pronchery 290*b077aed3SPierre Pronchery if (!ecdsa_signverify_init(vctx, ec, params, operation) 291*b077aed3SPierre Pronchery || !ecdsa_setup_md(ctx, mdname, NULL)) 292*b077aed3SPierre Pronchery return 0; 293*b077aed3SPierre Pronchery 294*b077aed3SPierre Pronchery ctx->flag_allow_md = 0; 295*b077aed3SPierre Pronchery 296*b077aed3SPierre Pronchery if (ctx->mdctx == NULL) { 297*b077aed3SPierre Pronchery ctx->mdctx = EVP_MD_CTX_new(); 298*b077aed3SPierre Pronchery if (ctx->mdctx == NULL) 299*b077aed3SPierre Pronchery goto error; 300*b077aed3SPierre Pronchery } 301*b077aed3SPierre Pronchery 302*b077aed3SPierre Pronchery if (!EVP_DigestInit_ex2(ctx->mdctx, ctx->md, params)) 303*b077aed3SPierre Pronchery goto error; 304*b077aed3SPierre Pronchery return 1; 305*b077aed3SPierre Pronchery error: 306*b077aed3SPierre Pronchery EVP_MD_CTX_free(ctx->mdctx); 307*b077aed3SPierre Pronchery ctx->mdctx = NULL; 308*b077aed3SPierre Pronchery return 0; 309*b077aed3SPierre Pronchery } 310*b077aed3SPierre Pronchery 311*b077aed3SPierre Pronchery static int ecdsa_digest_sign_init(void *vctx, const char *mdname, void *ec, 312*b077aed3SPierre Pronchery const OSSL_PARAM params[]) 313*b077aed3SPierre Pronchery { 314*b077aed3SPierre Pronchery return ecdsa_digest_signverify_init(vctx, mdname, ec, params, 315*b077aed3SPierre Pronchery EVP_PKEY_OP_SIGN); 316*b077aed3SPierre Pronchery } 317*b077aed3SPierre Pronchery 318*b077aed3SPierre Pronchery static int ecdsa_digest_verify_init(void *vctx, const char *mdname, void *ec, 319*b077aed3SPierre Pronchery const OSSL_PARAM params[]) 320*b077aed3SPierre Pronchery { 321*b077aed3SPierre Pronchery return ecdsa_digest_signverify_init(vctx, mdname, ec, params, 322*b077aed3SPierre Pronchery EVP_PKEY_OP_VERIFY); 323*b077aed3SPierre Pronchery } 324*b077aed3SPierre Pronchery 325*b077aed3SPierre Pronchery int ecdsa_digest_signverify_update(void *vctx, const unsigned char *data, 326*b077aed3SPierre Pronchery size_t datalen) 327*b077aed3SPierre Pronchery { 328*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 329*b077aed3SPierre Pronchery 330*b077aed3SPierre Pronchery if (ctx == NULL || ctx->mdctx == NULL) 331*b077aed3SPierre Pronchery return 0; 332*b077aed3SPierre Pronchery 333*b077aed3SPierre Pronchery return EVP_DigestUpdate(ctx->mdctx, data, datalen); 334*b077aed3SPierre Pronchery } 335*b077aed3SPierre Pronchery 336*b077aed3SPierre Pronchery int ecdsa_digest_sign_final(void *vctx, unsigned char *sig, size_t *siglen, 337*b077aed3SPierre Pronchery size_t sigsize) 338*b077aed3SPierre Pronchery { 339*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 340*b077aed3SPierre Pronchery unsigned char digest[EVP_MAX_MD_SIZE]; 341*b077aed3SPierre Pronchery unsigned int dlen = 0; 342*b077aed3SPierre Pronchery 343*b077aed3SPierre Pronchery if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) 344*b077aed3SPierre Pronchery return 0; 345*b077aed3SPierre Pronchery 346*b077aed3SPierre Pronchery /* 347*b077aed3SPierre Pronchery * If sig is NULL then we're just finding out the sig size. Other fields 348*b077aed3SPierre Pronchery * are ignored. Defer to ecdsa_sign. 349*b077aed3SPierre Pronchery */ 350*b077aed3SPierre Pronchery if (sig != NULL 351*b077aed3SPierre Pronchery && !EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) 352*b077aed3SPierre Pronchery return 0; 353*b077aed3SPierre Pronchery ctx->flag_allow_md = 1; 354*b077aed3SPierre Pronchery return ecdsa_sign(vctx, sig, siglen, sigsize, digest, (size_t)dlen); 355*b077aed3SPierre Pronchery } 356*b077aed3SPierre Pronchery 357*b077aed3SPierre Pronchery int ecdsa_digest_verify_final(void *vctx, const unsigned char *sig, 358*b077aed3SPierre Pronchery size_t siglen) 359*b077aed3SPierre Pronchery { 360*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 361*b077aed3SPierre Pronchery unsigned char digest[EVP_MAX_MD_SIZE]; 362*b077aed3SPierre Pronchery unsigned int dlen = 0; 363*b077aed3SPierre Pronchery 364*b077aed3SPierre Pronchery if (!ossl_prov_is_running() || ctx == NULL || ctx->mdctx == NULL) 365*b077aed3SPierre Pronchery return 0; 366*b077aed3SPierre Pronchery 367*b077aed3SPierre Pronchery if (!EVP_DigestFinal_ex(ctx->mdctx, digest, &dlen)) 368*b077aed3SPierre Pronchery return 0; 369*b077aed3SPierre Pronchery ctx->flag_allow_md = 1; 370*b077aed3SPierre Pronchery return ecdsa_verify(ctx, sig, siglen, digest, (size_t)dlen); 371*b077aed3SPierre Pronchery } 372*b077aed3SPierre Pronchery 373*b077aed3SPierre Pronchery static void ecdsa_freectx(void *vctx) 374*b077aed3SPierre Pronchery { 375*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 376*b077aed3SPierre Pronchery 377*b077aed3SPierre Pronchery OPENSSL_free(ctx->propq); 378*b077aed3SPierre Pronchery EVP_MD_CTX_free(ctx->mdctx); 379*b077aed3SPierre Pronchery EVP_MD_free(ctx->md); 380*b077aed3SPierre Pronchery ctx->propq = NULL; 381*b077aed3SPierre Pronchery ctx->mdctx = NULL; 382*b077aed3SPierre Pronchery ctx->md = NULL; 383*b077aed3SPierre Pronchery ctx->mdsize = 0; 384*b077aed3SPierre Pronchery EC_KEY_free(ctx->ec); 385*b077aed3SPierre Pronchery BN_clear_free(ctx->kinv); 386*b077aed3SPierre Pronchery BN_clear_free(ctx->r); 387*b077aed3SPierre Pronchery OPENSSL_free(ctx); 388*b077aed3SPierre Pronchery } 389*b077aed3SPierre Pronchery 390*b077aed3SPierre Pronchery static void *ecdsa_dupctx(void *vctx) 391*b077aed3SPierre Pronchery { 392*b077aed3SPierre Pronchery PROV_ECDSA_CTX *srcctx = (PROV_ECDSA_CTX *)vctx; 393*b077aed3SPierre Pronchery PROV_ECDSA_CTX *dstctx; 394*b077aed3SPierre Pronchery 395*b077aed3SPierre Pronchery if (!ossl_prov_is_running()) 396*b077aed3SPierre Pronchery return NULL; 397*b077aed3SPierre Pronchery 398*b077aed3SPierre Pronchery dstctx = OPENSSL_zalloc(sizeof(*srcctx)); 399*b077aed3SPierre Pronchery if (dstctx == NULL) 400*b077aed3SPierre Pronchery return NULL; 401*b077aed3SPierre Pronchery 402*b077aed3SPierre Pronchery *dstctx = *srcctx; 403*b077aed3SPierre Pronchery dstctx->ec = NULL; 404*b077aed3SPierre Pronchery dstctx->md = NULL; 405*b077aed3SPierre Pronchery dstctx->mdctx = NULL; 406*b077aed3SPierre Pronchery dstctx->propq = NULL; 407*b077aed3SPierre Pronchery 408*b077aed3SPierre Pronchery if (srcctx->ec != NULL && !EC_KEY_up_ref(srcctx->ec)) 409*b077aed3SPierre Pronchery goto err; 410*b077aed3SPierre Pronchery /* Test KATS should not need to be supported */ 411*b077aed3SPierre Pronchery if (srcctx->kinv != NULL || srcctx->r != NULL) 412*b077aed3SPierre Pronchery goto err; 413*b077aed3SPierre Pronchery dstctx->ec = srcctx->ec; 414*b077aed3SPierre Pronchery 415*b077aed3SPierre Pronchery if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) 416*b077aed3SPierre Pronchery goto err; 417*b077aed3SPierre Pronchery dstctx->md = srcctx->md; 418*b077aed3SPierre Pronchery 419*b077aed3SPierre Pronchery if (srcctx->mdctx != NULL) { 420*b077aed3SPierre Pronchery dstctx->mdctx = EVP_MD_CTX_new(); 421*b077aed3SPierre Pronchery if (dstctx->mdctx == NULL 422*b077aed3SPierre Pronchery || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) 423*b077aed3SPierre Pronchery goto err; 424*b077aed3SPierre Pronchery } 425*b077aed3SPierre Pronchery 426*b077aed3SPierre Pronchery if (srcctx->propq != NULL) { 427*b077aed3SPierre Pronchery dstctx->propq = OPENSSL_strdup(srcctx->propq); 428*b077aed3SPierre Pronchery if (dstctx->propq == NULL) 429*b077aed3SPierre Pronchery goto err; 430*b077aed3SPierre Pronchery } 431*b077aed3SPierre Pronchery 432*b077aed3SPierre Pronchery return dstctx; 433*b077aed3SPierre Pronchery err: 434*b077aed3SPierre Pronchery ecdsa_freectx(dstctx); 435*b077aed3SPierre Pronchery return NULL; 436*b077aed3SPierre Pronchery } 437*b077aed3SPierre Pronchery 438*b077aed3SPierre Pronchery static int ecdsa_get_ctx_params(void *vctx, OSSL_PARAM *params) 439*b077aed3SPierre Pronchery { 440*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 441*b077aed3SPierre Pronchery OSSL_PARAM *p; 442*b077aed3SPierre Pronchery 443*b077aed3SPierre Pronchery if (ctx == NULL) 444*b077aed3SPierre Pronchery return 0; 445*b077aed3SPierre Pronchery 446*b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); 447*b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_octet_string(p, ctx->aid, ctx->aid_len)) 448*b077aed3SPierre Pronchery return 0; 449*b077aed3SPierre Pronchery 450*b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); 451*b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_size_t(p, ctx->mdsize)) 452*b077aed3SPierre Pronchery return 0; 453*b077aed3SPierre Pronchery 454*b077aed3SPierre Pronchery p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); 455*b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_set_utf8_string(p, ctx->md == NULL 456*b077aed3SPierre Pronchery ? ctx->mdname 457*b077aed3SPierre Pronchery : EVP_MD_get0_name(ctx->md))) 458*b077aed3SPierre Pronchery return 0; 459*b077aed3SPierre Pronchery 460*b077aed3SPierre Pronchery return 1; 461*b077aed3SPierre Pronchery } 462*b077aed3SPierre Pronchery 463*b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = { 464*b077aed3SPierre Pronchery OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), 465*b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), 466*b077aed3SPierre Pronchery OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), 467*b077aed3SPierre Pronchery OSSL_PARAM_END 468*b077aed3SPierre Pronchery }; 469*b077aed3SPierre Pronchery 470*b077aed3SPierre Pronchery static const OSSL_PARAM *ecdsa_gettable_ctx_params(ossl_unused void *vctx, 471*b077aed3SPierre Pronchery ossl_unused void *provctx) 472*b077aed3SPierre Pronchery { 473*b077aed3SPierre Pronchery return known_gettable_ctx_params; 474*b077aed3SPierre Pronchery } 475*b077aed3SPierre Pronchery 476*b077aed3SPierre Pronchery static int ecdsa_set_ctx_params(void *vctx, const OSSL_PARAM params[]) 477*b077aed3SPierre Pronchery { 478*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 479*b077aed3SPierre Pronchery const OSSL_PARAM *p; 480*b077aed3SPierre Pronchery size_t mdsize = 0; 481*b077aed3SPierre Pronchery 482*b077aed3SPierre Pronchery if (ctx == NULL) 483*b077aed3SPierre Pronchery return 0; 484*b077aed3SPierre Pronchery if (params == NULL) 485*b077aed3SPierre Pronchery return 1; 486*b077aed3SPierre Pronchery 487*b077aed3SPierre Pronchery #if !defined(OPENSSL_NO_ACVP_TESTS) 488*b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_KAT); 489*b077aed3SPierre Pronchery if (p != NULL && !OSSL_PARAM_get_uint(p, &ctx->kattest)) 490*b077aed3SPierre Pronchery return 0; 491*b077aed3SPierre Pronchery #endif 492*b077aed3SPierre Pronchery 493*b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); 494*b077aed3SPierre Pronchery if (p != NULL) { 495*b077aed3SPierre Pronchery char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; 496*b077aed3SPierre Pronchery char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; 497*b077aed3SPierre Pronchery const OSSL_PARAM *propsp = 498*b077aed3SPierre Pronchery OSSL_PARAM_locate_const(params, 499*b077aed3SPierre Pronchery OSSL_SIGNATURE_PARAM_PROPERTIES); 500*b077aed3SPierre Pronchery 501*b077aed3SPierre Pronchery if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) 502*b077aed3SPierre Pronchery return 0; 503*b077aed3SPierre Pronchery if (propsp != NULL 504*b077aed3SPierre Pronchery && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) 505*b077aed3SPierre Pronchery return 0; 506*b077aed3SPierre Pronchery if (!ecdsa_setup_md(ctx, mdname, mdprops)) 507*b077aed3SPierre Pronchery return 0; 508*b077aed3SPierre Pronchery } 509*b077aed3SPierre Pronchery 510*b077aed3SPierre Pronchery p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST_SIZE); 511*b077aed3SPierre Pronchery if (p != NULL) { 512*b077aed3SPierre Pronchery if (!OSSL_PARAM_get_size_t(p, &mdsize) 513*b077aed3SPierre Pronchery || (!ctx->flag_allow_md && mdsize != ctx->mdsize)) 514*b077aed3SPierre Pronchery return 0; 515*b077aed3SPierre Pronchery ctx->mdsize = mdsize; 516*b077aed3SPierre Pronchery } 517*b077aed3SPierre Pronchery 518*b077aed3SPierre Pronchery return 1; 519*b077aed3SPierre Pronchery } 520*b077aed3SPierre Pronchery 521*b077aed3SPierre Pronchery static const OSSL_PARAM settable_ctx_params[] = { 522*b077aed3SPierre Pronchery OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), 523*b077aed3SPierre Pronchery OSSL_PARAM_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, NULL), 524*b077aed3SPierre Pronchery OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), 525*b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), 526*b077aed3SPierre Pronchery OSSL_PARAM_END 527*b077aed3SPierre Pronchery }; 528*b077aed3SPierre Pronchery 529*b077aed3SPierre Pronchery static const OSSL_PARAM settable_ctx_params_no_digest[] = { 530*b077aed3SPierre Pronchery OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_KAT, NULL), 531*b077aed3SPierre Pronchery OSSL_PARAM_END 532*b077aed3SPierre Pronchery }; 533*b077aed3SPierre Pronchery 534*b077aed3SPierre Pronchery static const OSSL_PARAM *ecdsa_settable_ctx_params(void *vctx, 535*b077aed3SPierre Pronchery ossl_unused void *provctx) 536*b077aed3SPierre Pronchery { 537*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 538*b077aed3SPierre Pronchery 539*b077aed3SPierre Pronchery if (ctx != NULL && !ctx->flag_allow_md) 540*b077aed3SPierre Pronchery return settable_ctx_params_no_digest; 541*b077aed3SPierre Pronchery return settable_ctx_params; 542*b077aed3SPierre Pronchery } 543*b077aed3SPierre Pronchery 544*b077aed3SPierre Pronchery static int ecdsa_get_ctx_md_params(void *vctx, OSSL_PARAM *params) 545*b077aed3SPierre Pronchery { 546*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 547*b077aed3SPierre Pronchery 548*b077aed3SPierre Pronchery if (ctx->mdctx == NULL) 549*b077aed3SPierre Pronchery return 0; 550*b077aed3SPierre Pronchery 551*b077aed3SPierre Pronchery return EVP_MD_CTX_get_params(ctx->mdctx, params); 552*b077aed3SPierre Pronchery } 553*b077aed3SPierre Pronchery 554*b077aed3SPierre Pronchery static const OSSL_PARAM *ecdsa_gettable_ctx_md_params(void *vctx) 555*b077aed3SPierre Pronchery { 556*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 557*b077aed3SPierre Pronchery 558*b077aed3SPierre Pronchery if (ctx->md == NULL) 559*b077aed3SPierre Pronchery return 0; 560*b077aed3SPierre Pronchery 561*b077aed3SPierre Pronchery return EVP_MD_gettable_ctx_params(ctx->md); 562*b077aed3SPierre Pronchery } 563*b077aed3SPierre Pronchery 564*b077aed3SPierre Pronchery static int ecdsa_set_ctx_md_params(void *vctx, const OSSL_PARAM params[]) 565*b077aed3SPierre Pronchery { 566*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 567*b077aed3SPierre Pronchery 568*b077aed3SPierre Pronchery if (ctx->mdctx == NULL) 569*b077aed3SPierre Pronchery return 0; 570*b077aed3SPierre Pronchery 571*b077aed3SPierre Pronchery return EVP_MD_CTX_set_params(ctx->mdctx, params); 572*b077aed3SPierre Pronchery } 573*b077aed3SPierre Pronchery 574*b077aed3SPierre Pronchery static const OSSL_PARAM *ecdsa_settable_ctx_md_params(void *vctx) 575*b077aed3SPierre Pronchery { 576*b077aed3SPierre Pronchery PROV_ECDSA_CTX *ctx = (PROV_ECDSA_CTX *)vctx; 577*b077aed3SPierre Pronchery 578*b077aed3SPierre Pronchery if (ctx->md == NULL) 579*b077aed3SPierre Pronchery return 0; 580*b077aed3SPierre Pronchery 581*b077aed3SPierre Pronchery return EVP_MD_settable_ctx_params(ctx->md); 582*b077aed3SPierre Pronchery } 583*b077aed3SPierre Pronchery 584*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_ecdsa_signature_functions[] = { 585*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))ecdsa_newctx }, 586*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))ecdsa_sign_init }, 587*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))ecdsa_sign }, 588*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))ecdsa_verify_init }, 589*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))ecdsa_verify }, 590*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, 591*b077aed3SPierre Pronchery (void (*)(void))ecdsa_digest_sign_init }, 592*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, 593*b077aed3SPierre Pronchery (void (*)(void))ecdsa_digest_signverify_update }, 594*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, 595*b077aed3SPierre Pronchery (void (*)(void))ecdsa_digest_sign_final }, 596*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, 597*b077aed3SPierre Pronchery (void (*)(void))ecdsa_digest_verify_init }, 598*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, 599*b077aed3SPierre Pronchery (void (*)(void))ecdsa_digest_signverify_update }, 600*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, 601*b077aed3SPierre Pronchery (void (*)(void))ecdsa_digest_verify_final }, 602*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))ecdsa_freectx }, 603*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))ecdsa_dupctx }, 604*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))ecdsa_get_ctx_params }, 605*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, 606*b077aed3SPierre Pronchery (void (*)(void))ecdsa_gettable_ctx_params }, 607*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))ecdsa_set_ctx_params }, 608*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, 609*b077aed3SPierre Pronchery (void (*)(void))ecdsa_settable_ctx_params }, 610*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, 611*b077aed3SPierre Pronchery (void (*)(void))ecdsa_get_ctx_md_params }, 612*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, 613*b077aed3SPierre Pronchery (void (*)(void))ecdsa_gettable_ctx_md_params }, 614*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, 615*b077aed3SPierre Pronchery (void (*)(void))ecdsa_set_ctx_md_params }, 616*b077aed3SPierre Pronchery { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, 617*b077aed3SPierre Pronchery (void (*)(void))ecdsa_settable_ctx_md_params }, 618*b077aed3SPierre Pronchery { 0, NULL } 619*b077aed3SPierre Pronchery }; 620