Lines Matching +full:y +full:- +full:rc

1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SM2 asymmetric public-key algorithm
4 * as specified by OSCCA GM/T 0003.1-2012 -- 0003.5-2012 SM2 and
5 * described at https://tools.ietf.org/html/draft-shen-sm2-ecdsa-02
26 unsigned int fips:1; /* True if this is a FIPS140-2 approved curve */
41 * Curves (a,b) has ((A-2)/4,B^-1).
67 MPI x, y; in sm2_ec_ctx_init() local
68 int rc = -EINVAL; in sm2_ec_ctx_init() local
70 p = mpi_scanval(ecp->p); in sm2_ec_ctx_init()
71 a = mpi_scanval(ecp->a); in sm2_ec_ctx_init()
72 b = mpi_scanval(ecp->b); in sm2_ec_ctx_init()
76 x = mpi_scanval(ecp->g_x); in sm2_ec_ctx_init()
77 y = mpi_scanval(ecp->g_y); in sm2_ec_ctx_init()
78 if (!x || !y) in sm2_ec_ctx_init()
81 rc = -ENOMEM; in sm2_ec_ctx_init()
83 ec->Q = mpi_point_new(0); in sm2_ec_ctx_init()
84 if (!ec->Q) in sm2_ec_ctx_init()
88 ec->G = mpi_point_new(0); in sm2_ec_ctx_init()
89 if (!ec->G) { in sm2_ec_ctx_init()
90 mpi_point_release(ec->Q); in sm2_ec_ctx_init()
94 mpi_set(ec->G->x, x); in sm2_ec_ctx_init()
95 mpi_set(ec->G->y, y); in sm2_ec_ctx_init()
96 mpi_set_ui(ec->G->z, 1); in sm2_ec_ctx_init()
98 rc = -EINVAL; in sm2_ec_ctx_init()
99 ec->n = mpi_scanval(ecp->n); in sm2_ec_ctx_init()
100 if (!ec->n) { in sm2_ec_ctx_init()
101 mpi_point_release(ec->Q); in sm2_ec_ctx_init()
102 mpi_point_release(ec->G); in sm2_ec_ctx_init()
106 ec->h = ecp->h; in sm2_ec_ctx_init()
107 ec->name = ecp->desc; in sm2_ec_ctx_init()
108 mpi_ec_init(ec, ecp->model, ecp->dialect, 0, p, a, b); in sm2_ec_ctx_init()
110 rc = 0; in sm2_ec_ctx_init()
114 mpi_free(y); in sm2_ec_ctx_init()
120 return rc; in sm2_ec_ctx_init()
135 int rc; in sm2_ecc_os2ec() local
138 MPI x, y; in sm2_ecc_os2ec() local
143 return -ENOMEM; in sm2_ecc_os2ec()
145 rc = mpi_print(GCRYMPI_FMT_USG, buf, n, &n, value); in sm2_ecc_os2ec()
146 if (rc) in sm2_ecc_os2ec()
149 rc = -EINVAL; in sm2_ecc_os2ec()
150 if (n < 1 || ((n - 1) % 2)) in sm2_ecc_os2ec()
156 rc = -ENOMEM; in sm2_ecc_os2ec()
157 n = (n - 1) / 2; in sm2_ecc_os2ec()
161 y = mpi_read_raw_data(buf + 1 + n, n); in sm2_ecc_os2ec()
162 if (!y) in sm2_ecc_os2ec()
166 mpi_normalize(y); in sm2_ecc_os2ec()
167 mpi_set(result->x, x); in sm2_ecc_os2ec()
168 mpi_set(result->y, y); in sm2_ecc_os2ec()
169 mpi_set_ui(result->z, 1); in sm2_ecc_os2ec()
171 rc = 0; in sm2_ecc_os2ec()
173 mpi_free(y); in sm2_ecc_os2ec()
178 return rc; in sm2_ecc_os2ec()
192 return -EINVAL; in sm2_get_signature_r()
194 sig->sig_r = mpi_read_raw_data(value, vlen); in sm2_get_signature_r()
195 if (!sig->sig_r) in sm2_get_signature_r()
196 return -ENOMEM; in sm2_get_signature_r()
207 return -EINVAL; in sm2_get_signature_s()
209 sig->sig_s = mpi_read_raw_data(value, vlen); in sm2_get_signature_s()
210 if (!sig->sig_s) in sm2_get_signature_s()
211 return -ENOMEM; in sm2_get_signature_s()
225 return -EINVAL; in sm2_z_digest_update()
229 sm3_update(sctx, zero, pbytes - inlen); in sm2_z_digest_update()
233 sm3_update(sctx, in + inlen - pbytes, pbytes); in sm2_z_digest_update()
245 MPI x, y; in sm2_z_digest_update_point() local
246 int ret = -EINVAL; in sm2_z_digest_update_point()
249 y = mpi_new(0); in sm2_z_digest_update_point()
251 if (!mpi_ec_get_affine(x, y, point, ec) && in sm2_z_digest_update_point()
253 !sm2_z_digest_update(sctx, y, pbytes)) in sm2_z_digest_update_point()
257 mpi_free(y); in sm2_z_digest_update_point()
271 if (id_len > (USHRT_MAX / 8) || !ec->Q) in sm2_compute_z_digest()
272 return -EINVAL; in sm2_compute_z_digest()
278 pbytes = MPI_NBYTES(ec->p); in sm2_compute_z_digest()
285 if (sm2_z_digest_update(&sctx, ec->a, pbytes) || in sm2_compute_z_digest()
286 sm2_z_digest_update(&sctx, ec->b, pbytes) || in sm2_compute_z_digest()
287 sm2_z_digest_update_point(&sctx, ec->G, ec, pbytes) || in sm2_compute_z_digest()
288 sm2_z_digest_update_point(&sctx, ec->Q, ec, pbytes)) in sm2_compute_z_digest()
289 return -EINVAL; in sm2_compute_z_digest()
298 int rc = -EINVAL; in _sm2_verify() local
309 /* r, s in [1, n-1] */ in _sm2_verify()
310 if (mpi_cmp_ui(sig_r, 1) < 0 || mpi_cmp(sig_r, ec->n) > 0 || in _sm2_verify()
311 mpi_cmp_ui(sig_s, 1) < 0 || mpi_cmp(sig_s, ec->n) > 0) { in _sm2_verify()
316 mpi_addm(t, sig_r, sig_s, ec->n); in _sm2_verify()
321 rc = -EBADMSG; in _sm2_verify()
322 mpi_ec_mul_point(&sG, sig_s, ec->G, ec); in _sm2_verify()
323 mpi_ec_mul_point(&tP, t, ec->Q, ec); in _sm2_verify()
329 mpi_addm(t, hash, x1, ec->n); in _sm2_verify()
332 rc = -EKEYREJECTED; in _sm2_verify()
336 rc = 0; in _sm2_verify()
345 return rc; in _sm2_verify()
357 if (unlikely(!ec->Q)) in sm2_verify()
358 return -EINVAL; in sm2_verify()
360 buffer = kmalloc(req->src_len + req->dst_len, GFP_KERNEL); in sm2_verify()
362 return -ENOMEM; in sm2_verify()
364 sg_pcopy_to_buffer(req->src, in sm2_verify()
365 sg_nents_for_len(req->src, req->src_len + req->dst_len), in sm2_verify()
366 buffer, req->src_len + req->dst_len, 0); in sm2_verify()
371 buffer, req->src_len); in sm2_verify()
375 ret = -ENOMEM; in sm2_verify()
376 hash = mpi_read_raw_data(buffer + req->src_len, req->dst_len); in sm2_verify()
395 int rc; in sm2_set_pub_key() local
400 return -ENOMEM; in sm2_set_pub_key()
403 rc = sm2_ecc_os2ec(ec->Q, a); in sm2_set_pub_key()
406 return rc; in sm2_set_pub_key()
437 .cra_driver_name = "sm2-generic",
460 MODULE_ALIAS_CRYPTO("sm2-generic");