Skip to content

Commit 91f1ab7

Browse files
committed
provider: Allow ECDSA deterministic signatures, but use fallback
Tolerate the use of deterministic signatures (OSSL_SIGNATURE_PARAM_NONCE_TYPE set to a non-zero value), but use the fallback provider for calculating a deterministic signature. Signed-off-by: Ingo Franzki <ifranzki@linux.ibm.com>
1 parent e843783 commit 91f1ab7

File tree

2 files changed

+43
-12
lines changed

2 files changed

+43
-12
lines changed

src/provider/ec_signature.c

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@ static void ibmca_signature_ec_free_cb(struct ibmca_op_ctx *ctx)
109109
if (ctx->ec.signature.md_ctx != NULL)
110110
EVP_MD_CTX_free(ctx->ec.signature.md_ctx);
111111
ctx->ec.signature.md_ctx = NULL;
112+
113+
ctx->ec.signature.nonce_type = 0;
112114
}
113115

114116
static int ibmca_signature_ec_dup_cb(const struct ibmca_op_ctx *ctx,
@@ -142,6 +144,8 @@ static int ibmca_signature_ec_dup_cb(const struct ibmca_op_ctx *ctx,
142144
}
143145
}
144146

147+
new_ctx->ec.signature.nonce_type = ctx->ec.signature.nonce_type;
148+
145149
return 1;
146150
}
147151

@@ -268,6 +272,10 @@ static int ibmca_signature_ec_sign_fallback(struct ibmca_op_ctx *ctx,
268272
{
269273
EVP_PKEY *pkey = NULL;
270274
EVP_PKEY_CTX *pctx = NULL;
275+
#ifdef OSSL_SIGNATURE_PARAM_NONCE_TYPE
276+
OSSL_PARAM params[3];
277+
const char *md_name;
278+
#endif
271279
int rc = 0;
272280

273281
ibmca_debug_op_ctx(ctx, "ctx: %p key: %p tbslen: %lu sig: %p siglen: %lu",
@@ -296,6 +304,35 @@ static int ibmca_signature_ec_sign_fallback(struct ibmca_op_ctx *ctx,
296304
goto out;
297305
}
298306

307+
#ifdef OSSL_SIGNATURE_PARAM_NONCE_TYPE
308+
ibmca_debug_op_ctx(ctx, "nonce_type: %u", ctx->ec.signature.nonce_type);
309+
310+
if (ctx->ec.signature.nonce_type != 0) {
311+
md_name = EVP_MD_get0_name(ctx->ec.signature.md);
312+
if (md_name == NULL) {
313+
put_error_op_ctx(ctx, IBMCA_ERR_INVALID_PARAM,
314+
"Digest must be set when using deterministic "
315+
"signatures");
316+
goto out;
317+
}
318+
319+
ibmca_debug_op_ctx(ctx, "md_name: %s", md_name);
320+
321+
params[0] = OSSL_PARAM_construct_utf8_string(
322+
OSSL_SIGNATURE_PARAM_DIGEST,
323+
(char *)md_name, strlen(md_name));
324+
params[1] = OSSL_PARAM_construct_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE,
325+
&ctx->ec.signature.nonce_type);
326+
params[2] = OSSL_PARAM_construct_end();
327+
328+
if (EVP_PKEY_CTX_set_params(pctx, params) != 1) {
329+
put_error_op_ctx(ctx, IBMCA_ERR_INTERNAL_ERROR,
330+
"EVP_PKEY_CTX_set_params failed");
331+
goto out;
332+
}
333+
}
334+
#endif
335+
299336
if (EVP_PKEY_sign(pctx, sig, siglen, tbs, tbslen) != 1) {
300337
put_error_op_ctx(ctx, IBMCA_ERR_INTERNAL_ERROR,
301338
"EVP_PKEY_sign failed");
@@ -365,7 +402,7 @@ static int ibmca_signature_ec_sign(void *vctx,
365402
goto out;
366403
}
367404

368-
if (ctx->key->ec.fallback.d != NULL) {
405+
if (ctx->key->ec.fallback.d != NULL || ctx->ec.signature.nonce_type != 0) {
369406
rc = ibmca_signature_ec_sign_fallback(ctx, sig, siglen, tbs, tbslen);
370407
if (rc != 1) {
371408
ibmca_debug_op_ctx(ctx,
@@ -701,7 +738,8 @@ static int ibmca_signature_ec_get_ctx_params(void *vctx,
701738
#ifdef OSSL_SIGNATURE_PARAM_NONCE_TYPE
702739
/* OSSL_SIGNATURE_PARAM_NONCE_TYPE */
703740
rc = ibmca_param_build_set_uint(ctx->provctx, NULL, params,
704-
OSSL_SIGNATURE_PARAM_NONCE_TYPE, 0);
741+
OSSL_SIGNATURE_PARAM_NONCE_TYPE,
742+
ctx->ec.signature.nonce_type);
705743
if (rc == 0)
706744
return 0;
707745
#endif
@@ -716,9 +754,6 @@ static int ibmca_signature_ec_set_ctx_params(void *vctx,
716754
const OSSL_PARAM *p;
717755
const char *name, *props = NULL;
718756
size_t md_size;
719-
#ifdef OSSL_SIGNATURE_PARAM_NONCE_TYPE
720-
unsigned int nonce_type;
721-
#endif
722757
int rc;
723758

724759
if (ctx == NULL)
@@ -760,15 +795,10 @@ static int ibmca_signature_ec_set_ctx_params(void *vctx,
760795
#ifdef OSSL_SIGNATURE_PARAM_NONCE_TYPE
761796
/* OSSL_SIGNATURE_PARAM_NONCE_TYPE */
762797
rc = ibmca_param_get_uint(ctx->provctx, params,
763-
OSSL_SIGNATURE_PARAM_NONCE_TYPE, &nonce_type);
798+
OSSL_SIGNATURE_PARAM_NONCE_TYPE,
799+
&ctx->ec.signature.nonce_type);
764800
if (rc == 0)
765801
return 0;
766-
/* Only allow nonce_type = 0 = random K */
767-
if (nonce_type != 0) {
768-
put_error_op_ctx(ctx, IBMCA_ERR_INVALID_PARAM,
769-
"Deterministic signature is not supported");
770-
return 0;
771-
}
772802
#endif
773803

774804
return 1;

src/provider/p_ibmca.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ struct ibmca_op_ctx {
239239
bool set_md_allowed;
240240
size_t md_size;
241241
EVP_MD_CTX *md_ctx;
242+
unsigned int nonce_type;
242243
} signature; /* For operation EVP_PKEY_OP_SIGN/VERIFY */
243244
struct {
244245
struct ibmca_key *peer_key;

0 commit comments

Comments
 (0)