1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2021 IBM Corporation
6 #include <linux/module.h>
7 #include <crypto/internal/ecc.h>
8 #include <crypto/internal/sig.h>
9 #include <crypto/ecdh.h>
10 #include <crypto/sha2.h>
11 #include <crypto/sig.h>
14 unsigned int curve_id;
15 const struct ecc_curve *curve;
18 u64 x[ECC_MAX_DIGITS]; /* pub key x and y coordinates */
19 u64 y[ECC_MAX_DIGITS];
20 struct ecc_point pub_key;
23 static int _ecdsa_verify(struct ecc_ctx *ctx, const u64 *hash, const u64 *r, const u64 *s)
25 const struct ecc_curve *curve = ctx->curve;
26 unsigned int ndigits = curve->g.ndigits;
27 u64 s1[ECC_MAX_DIGITS];
28 u64 u1[ECC_MAX_DIGITS];
29 u64 u2[ECC_MAX_DIGITS];
30 u64 x1[ECC_MAX_DIGITS];
31 u64 y1[ECC_MAX_DIGITS];
32 struct ecc_point res = ECC_POINT_INIT(x1, y1, ndigits);
34 /* 0 < r < n and 0 < s < n */
35 if (vli_is_zero(r, ndigits) || vli_cmp(r, curve->n, ndigits) >= 0 ||
36 vli_is_zero(s, ndigits) || vli_cmp(s, curve->n, ndigits) >= 0)
40 pr_devel("hash : %016llx %016llx ... %016llx\n",
41 hash[ndigits - 1], hash[ndigits - 2], hash[0]);
43 /* s1 = (s^-1) mod n */
44 vli_mod_inv(s1, s, curve->n, ndigits);
45 /* u1 = (hash * s1) mod n */
46 vli_mod_mult_slow(u1, hash, s1, curve->n, ndigits);
47 /* u2 = (r * s1) mod n */
48 vli_mod_mult_slow(u2, r, s1, curve->n, ndigits);
49 /* res = u1*G + u2 * pub_key */
50 ecc_point_mult_shamir(&res, u1, &curve->g, u2, &ctx->pub_key, curve);
52 /* res.x = res.x mod n (if res.x > order) */
53 if (unlikely(vli_cmp(res.x, curve->n, ndigits) == 1))
54 /* faster alternative for NIST p521, p384, p256 & p192 */
55 vli_sub(res.x, res.x, curve->n, ndigits);
57 if (!vli_cmp(res.x, r, ndigits))
64 * Verify an ECDSA signature.
66 static int ecdsa_verify(struct crypto_sig *tfm,
67 const void *src, unsigned int slen,
68 const void *digest, unsigned int dlen)
70 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
71 size_t bufsize = ctx->curve->g.ndigits * sizeof(u64);
72 const struct ecdsa_raw_sig *sig = src;
73 u64 hash[ECC_MAX_DIGITS];
75 if (unlikely(!ctx->pub_key_set))
78 if (slen != sizeof(*sig))
84 ecc_digits_from_bytes(digest, bufsize, hash, ctx->curve->g.ndigits);
86 return _ecdsa_verify(ctx, hash, sig->r, sig->s);
89 static int ecdsa_ecc_ctx_init(struct ecc_ctx *ctx, unsigned int curve_id)
91 ctx->curve_id = curve_id;
92 ctx->curve = ecc_get_curve(curve_id);
100 static void ecdsa_ecc_ctx_deinit(struct ecc_ctx *ctx)
102 ctx->pub_key_set = false;
105 static int ecdsa_ecc_ctx_reset(struct ecc_ctx *ctx)
107 unsigned int curve_id = ctx->curve_id;
110 ecdsa_ecc_ctx_deinit(ctx);
111 ret = ecdsa_ecc_ctx_init(ctx, curve_id);
113 ctx->pub_key = ECC_POINT_INIT(ctx->x, ctx->y,
114 ctx->curve->g.ndigits);
119 * Set the public ECC key as defined by RFC5480 section 2.2 "Subject Public
120 * Key". Only the uncompressed format is supported.
122 static int ecdsa_set_pub_key(struct crypto_sig *tfm, const void *key,
125 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
126 unsigned int digitlen, ndigits;
127 const unsigned char *d = key;
130 ret = ecdsa_ecc_ctx_reset(ctx);
134 if (keylen < 1 || ((keylen - 1) & 1) != 0)
136 /* we only accept uncompressed format indicated by '4' */
141 digitlen = keylen >> 1;
143 ndigits = DIV_ROUND_UP(digitlen, sizeof(u64));
144 if (ndigits != ctx->curve->g.ndigits)
149 ecc_digits_from_bytes(d, digitlen, ctx->pub_key.x, ndigits);
150 ecc_digits_from_bytes(&d[digitlen], digitlen, ctx->pub_key.y, ndigits);
152 ret = ecc_is_pubkey_valid_full(ctx->curve, &ctx->pub_key);
154 ctx->pub_key_set = ret == 0;
159 static void ecdsa_exit_tfm(struct crypto_sig *tfm)
161 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
163 ecdsa_ecc_ctx_deinit(ctx);
166 static unsigned int ecdsa_key_size(struct crypto_sig *tfm)
168 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
170 return DIV_ROUND_UP(ctx->curve->nbits, 8);
173 static unsigned int ecdsa_digest_size(struct crypto_sig *tfm)
176 * ECDSA key sizes are much smaller than RSA, and thus could
177 * operate on (hashed) inputs that are larger than the key size.
178 * E.g. SHA384-hashed input used with secp256r1 based keys.
179 * Return the largest supported hash size (SHA512).
181 return SHA512_DIGEST_SIZE;
184 static int ecdsa_nist_p521_init_tfm(struct crypto_sig *tfm)
186 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
188 return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P521);
191 static struct sig_alg ecdsa_nist_p521 = {
192 .verify = ecdsa_verify,
193 .set_pub_key = ecdsa_set_pub_key,
194 .key_size = ecdsa_key_size,
195 .digest_size = ecdsa_digest_size,
196 .init = ecdsa_nist_p521_init_tfm,
197 .exit = ecdsa_exit_tfm,
199 .cra_name = "ecdsa-nist-p521",
200 .cra_driver_name = "ecdsa-nist-p521-generic",
202 .cra_module = THIS_MODULE,
203 .cra_ctxsize = sizeof(struct ecc_ctx),
207 static int ecdsa_nist_p384_init_tfm(struct crypto_sig *tfm)
209 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
211 return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P384);
214 static struct sig_alg ecdsa_nist_p384 = {
215 .verify = ecdsa_verify,
216 .set_pub_key = ecdsa_set_pub_key,
217 .key_size = ecdsa_key_size,
218 .digest_size = ecdsa_digest_size,
219 .init = ecdsa_nist_p384_init_tfm,
220 .exit = ecdsa_exit_tfm,
222 .cra_name = "ecdsa-nist-p384",
223 .cra_driver_name = "ecdsa-nist-p384-generic",
225 .cra_module = THIS_MODULE,
226 .cra_ctxsize = sizeof(struct ecc_ctx),
230 static int ecdsa_nist_p256_init_tfm(struct crypto_sig *tfm)
232 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
234 return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P256);
237 static struct sig_alg ecdsa_nist_p256 = {
238 .verify = ecdsa_verify,
239 .set_pub_key = ecdsa_set_pub_key,
240 .key_size = ecdsa_key_size,
241 .digest_size = ecdsa_digest_size,
242 .init = ecdsa_nist_p256_init_tfm,
243 .exit = ecdsa_exit_tfm,
245 .cra_name = "ecdsa-nist-p256",
246 .cra_driver_name = "ecdsa-nist-p256-generic",
248 .cra_module = THIS_MODULE,
249 .cra_ctxsize = sizeof(struct ecc_ctx),
253 static int ecdsa_nist_p192_init_tfm(struct crypto_sig *tfm)
255 struct ecc_ctx *ctx = crypto_sig_ctx(tfm);
257 return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P192);
260 static struct sig_alg ecdsa_nist_p192 = {
261 .verify = ecdsa_verify,
262 .set_pub_key = ecdsa_set_pub_key,
263 .key_size = ecdsa_key_size,
264 .digest_size = ecdsa_digest_size,
265 .init = ecdsa_nist_p192_init_tfm,
266 .exit = ecdsa_exit_tfm,
268 .cra_name = "ecdsa-nist-p192",
269 .cra_driver_name = "ecdsa-nist-p192-generic",
271 .cra_module = THIS_MODULE,
272 .cra_ctxsize = sizeof(struct ecc_ctx),
275 static bool ecdsa_nist_p192_registered;
277 static int __init ecdsa_init(void)
281 /* NIST p192 may not be available in FIPS mode */
282 ret = crypto_register_sig(&ecdsa_nist_p192);
283 ecdsa_nist_p192_registered = ret == 0;
285 ret = crypto_register_sig(&ecdsa_nist_p256);
287 goto nist_p256_error;
289 ret = crypto_register_sig(&ecdsa_nist_p384);
291 goto nist_p384_error;
293 ret = crypto_register_sig(&ecdsa_nist_p521);
295 goto nist_p521_error;
297 ret = crypto_register_template(&ecdsa_x962_tmpl);
299 goto x962_tmpl_error;
301 ret = crypto_register_template(&ecdsa_p1363_tmpl);
303 goto p1363_tmpl_error;
308 crypto_unregister_template(&ecdsa_x962_tmpl);
311 crypto_unregister_sig(&ecdsa_nist_p521);
314 crypto_unregister_sig(&ecdsa_nist_p384);
317 crypto_unregister_sig(&ecdsa_nist_p256);
320 if (ecdsa_nist_p192_registered)
321 crypto_unregister_sig(&ecdsa_nist_p192);
325 static void __exit ecdsa_exit(void)
327 crypto_unregister_template(&ecdsa_x962_tmpl);
328 crypto_unregister_template(&ecdsa_p1363_tmpl);
330 if (ecdsa_nist_p192_registered)
331 crypto_unregister_sig(&ecdsa_nist_p192);
332 crypto_unregister_sig(&ecdsa_nist_p256);
333 crypto_unregister_sig(&ecdsa_nist_p384);
334 crypto_unregister_sig(&ecdsa_nist_p521);
337 subsys_initcall(ecdsa_init);
338 module_exit(ecdsa_exit);
340 MODULE_LICENSE("GPL");
342 MODULE_DESCRIPTION("ECDSA generic algorithm");
343 MODULE_ALIAS_CRYPTO("ecdsa-nist-p192");
344 MODULE_ALIAS_CRYPTO("ecdsa-nist-p256");
345 MODULE_ALIAS_CRYPTO("ecdsa-nist-p384");
346 MODULE_ALIAS_CRYPTO("ecdsa-nist-p521");
347 MODULE_ALIAS_CRYPTO("ecdsa-generic");