Fix the problem that can not get random k when do not set the value or callback function. There are three ways to use random k in sm2 sign, fix to support them all in v1 and v2: - If k is not set and cb has been set, use cb to generate random k. - If k is set or has been generated by cb, directly set k. - If k and cb are not set, hw driver should config to generate random k.
Signed-off-by: Zhiqi Song songzhiqi1@huawei.com --- v1/wd_ecc.c | 17 +++++++++-------- wd_ecc.c | 15 +++++++++------ 2 files changed, 18 insertions(+), 14 deletions(-)
diff --git a/v1/wd_ecc.c b/v1/wd_ecc.c index 36c507a..8486007 100644 --- a/v1/wd_ecc.c +++ b/v1/wd_ecc.c @@ -1803,11 +1803,6 @@ static int generate_random(struct wcrypto_ecc_ctx *ctx, struct wd_dtb *k) struct wcrypto_rand_mt *rand_mt = &ctx->setup.rand; int ret;
- if (!rand_mt->cb) { - WD_ERR("failed to get rand cb!\n"); - return -WD_EINVAL; - } - ret = rand_mt->cb(k->data, k->dsize, rand_mt->usr); if (unlikely(ret)) WD_ERR("failed to rand cb: ret = %d!\n", ret); @@ -1938,14 +1933,20 @@ static struct wcrypto_ecc_in *new_sign_in(struct wcrypto_ecc_ctx *ctx, return NULL;
sin = &ecc_in->param.sin; - if (!k) { + sin->k_set = 0; + sin->dgst_set = 0; + + /* + * If k is not set and cb has been set, use cb to generate random k. + * If k is set or has been generated by cb, directly set k. + * If k and cb are not set, hw driver should config to generate random k. + */ + if (!k && cx->setup.rand.cb) { ret = generate_random(cx, &sin->k); if (unlikely(ret)) goto release_in; }
- sin->k_set = 0; - sin->dgst_set = 0; if (k || cx->setup.rand.cb) sin->k_set = 1;
diff --git a/wd_ecc.c b/wd_ecc.c index 8c053fd..cf2cbae 100644 --- a/wd_ecc.c +++ b/wd_ecc.c @@ -1627,11 +1627,6 @@ static int generate_random(struct wd_ecc_sess *sess, struct wd_dtb *k) struct wd_rand_mt rand_t = sess->setup.rand; int ret;
- if (!rand_t.cb) { - WD_ERR("failed to get rand cb!\n"); - return -WD_EINVAL; - } - ret = rand_t.cb(k->data, k->dsize, rand_t.usr); if (ret) WD_ERR("failed to do rand cb, ret = %d!\n", ret); @@ -1761,7 +1756,15 @@ static struct wd_ecc_in *new_sign_in(struct wd_ecc_sess *sess, return NULL;
sin = &ecc_in->param.sin; - if (!k) { + sin->k_set = 0; + sin->dgst_set = 0; + + /* + * If k is not set and cb has been set, use cb to generate random k. + * If k is set or has been generated by cb, directly set k. + * If k and cb are not set, hw driver should config to generate random k. + */ + if (!k && sess_t->setup.rand.cb) { ret = generate_random(sess_t, &sin->k); if (ret) goto release_in;