1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright (c) 2021 IBM Corporation
4 */
5
6 #include <linux/module.h>
7 #include <crypto/internal/akcipher.h>
8 #include <crypto/internal/ecc.h>
9 #include <crypto/akcipher.h>
10 #include <crypto/ecdh.h>
11 #include <linux/asn1_decoder.h>
12 #include <linux/scatterlist.h>
13
14 #include "ecdsasignature.asn1.h"
15
16 struct ecc_ctx {
17 unsigned int curve_id;
18 const struct ecc_curve *curve;
19
20 bool pub_key_set;
21 u64 x[ECC_MAX_DIGITS]; /* pub key x and y coordinates */
22 u64 y[ECC_MAX_DIGITS];
23 struct ecc_point pub_key;
24 };
25
26 struct ecdsa_signature_ctx {
27 const struct ecc_curve *curve;
28 u64 r[ECC_MAX_DIGITS];
29 u64 s[ECC_MAX_DIGITS];
30 };
31
32 /*
33 * Get the r and s components of a signature from the X509 certificate.
34 */
ecdsa_get_signature_rs(u64 * dest,size_t hdrlen,unsigned char tag,const void * value,size_t vlen,unsigned int ndigits)35 static int ecdsa_get_signature_rs(u64 *dest, size_t hdrlen, unsigned char tag,
36 const void *value, size_t vlen, unsigned int ndigits)
37 {
38 size_t bufsize = ndigits * sizeof(u64);
39 const char *d = value;
40
41 if (!value || !vlen || vlen > bufsize + 1)
42 return -EINVAL;
43
44 /*
45 * vlen may be 1 byte larger than bufsize due to a leading zero byte
46 * (necessary if the most significant bit of the integer is set).
47 */
48 if (vlen > bufsize) {
49 /* skip over leading zeros that make 'value' a positive int */
50 if (*d == 0) {
51 vlen -= 1;
52 d++;
53 } else {
54 return -EINVAL;
55 }
56 }
57
58 ecc_digits_from_bytes(d, vlen, dest, ndigits);
59
60 return 0;
61 }
62
ecdsa_get_signature_r(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)63 int ecdsa_get_signature_r(void *context, size_t hdrlen, unsigned char tag,
64 const void *value, size_t vlen)
65 {
66 struct ecdsa_signature_ctx *sig = context;
67
68 return ecdsa_get_signature_rs(sig->r, hdrlen, tag, value, vlen,
69 sig->curve->g.ndigits);
70 }
71
ecdsa_get_signature_s(void * context,size_t hdrlen,unsigned char tag,const void * value,size_t vlen)72 int ecdsa_get_signature_s(void *context, size_t hdrlen, unsigned char tag,
73 const void *value, size_t vlen)
74 {
75 struct ecdsa_signature_ctx *sig = context;
76
77 return ecdsa_get_signature_rs(sig->s, hdrlen, tag, value, vlen,
78 sig->curve->g.ndigits);
79 }
80
_ecdsa_verify(struct ecc_ctx * ctx,const u64 * hash,const u64 * r,const u64 * s)81 static int _ecdsa_verify(struct ecc_ctx *ctx, const u64 *hash, const u64 *r, const u64 *s)
82 {
83 const struct ecc_curve *curve = ctx->curve;
84 unsigned int ndigits = curve->g.ndigits;
85 u64 s1[ECC_MAX_DIGITS];
86 u64 u1[ECC_MAX_DIGITS];
87 u64 u2[ECC_MAX_DIGITS];
88 u64 x1[ECC_MAX_DIGITS];
89 u64 y1[ECC_MAX_DIGITS];
90 struct ecc_point res = ECC_POINT_INIT(x1, y1, ndigits);
91
92 /* 0 < r < n and 0 < s < n */
93 if (vli_is_zero(r, ndigits) || vli_cmp(r, curve->n, ndigits) >= 0 ||
94 vli_is_zero(s, ndigits) || vli_cmp(s, curve->n, ndigits) >= 0)
95 return -EBADMSG;
96
97 /* hash is given */
98 pr_devel("hash : %016llx %016llx ... %016llx\n",
99 hash[ndigits - 1], hash[ndigits - 2], hash[0]);
100
101 /* s1 = (s^-1) mod n */
102 vli_mod_inv(s1, s, curve->n, ndigits);
103 /* u1 = (hash * s1) mod n */
104 vli_mod_mult_slow(u1, hash, s1, curve->n, ndigits);
105 /* u2 = (r * s1) mod n */
106 vli_mod_mult_slow(u2, r, s1, curve->n, ndigits);
107 /* res = u1*G + u2 * pub_key */
108 ecc_point_mult_shamir(&res, u1, &curve->g, u2, &ctx->pub_key, curve);
109
110 /* res.x = res.x mod n (if res.x > order) */
111 if (unlikely(vli_cmp(res.x, curve->n, ndigits) == 1))
112 /* faster alternative for NIST p384, p256 & p192 */
113 vli_sub(res.x, res.x, curve->n, ndigits);
114
115 if (!vli_cmp(res.x, r, ndigits))
116 return 0;
117
118 return -EKEYREJECTED;
119 }
120
121 /*
122 * Verify an ECDSA signature.
123 */
ecdsa_verify(struct akcipher_request * req)124 static int ecdsa_verify(struct akcipher_request *req)
125 {
126 struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
127 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
128 size_t bufsize = ctx->curve->g.ndigits * sizeof(u64);
129 struct ecdsa_signature_ctx sig_ctx = {
130 .curve = ctx->curve,
131 };
132 u8 rawhash[ECC_MAX_BYTES];
133 u64 hash[ECC_MAX_DIGITS];
134 unsigned char *buffer;
135 ssize_t diff;
136 int ret;
137
138 if (unlikely(!ctx->pub_key_set))
139 return -EINVAL;
140
141 buffer = kmalloc(req->src_len + req->dst_len, GFP_KERNEL);
142 if (!buffer)
143 return -ENOMEM;
144
145 sg_pcopy_to_buffer(req->src,
146 sg_nents_for_len(req->src, req->src_len + req->dst_len),
147 buffer, req->src_len + req->dst_len, 0);
148
149 ret = asn1_ber_decoder(&ecdsasignature_decoder, &sig_ctx,
150 buffer, req->src_len);
151 if (ret < 0)
152 goto error;
153
154 /* if the hash is shorter then we will add leading zeros to fit to ndigits */
155 diff = bufsize - req->dst_len;
156 if (diff >= 0) {
157 if (diff)
158 memset(rawhash, 0, diff);
159 memcpy(&rawhash[diff], buffer + req->src_len, req->dst_len);
160 } else if (diff < 0) {
161 /* given hash is longer, we take the left-most bytes */
162 memcpy(&rawhash, buffer + req->src_len, bufsize);
163 }
164
165 ecc_swap_digits((u64 *)rawhash, hash, ctx->curve->g.ndigits);
166
167 ret = _ecdsa_verify(ctx, hash, sig_ctx.r, sig_ctx.s);
168
169 error:
170 kfree(buffer);
171
172 return ret;
173 }
174
ecdsa_ecc_ctx_init(struct ecc_ctx * ctx,unsigned int curve_id)175 static int ecdsa_ecc_ctx_init(struct ecc_ctx *ctx, unsigned int curve_id)
176 {
177 ctx->curve_id = curve_id;
178 ctx->curve = ecc_get_curve(curve_id);
179 if (!ctx->curve)
180 return -EINVAL;
181
182 return 0;
183 }
184
185
ecdsa_ecc_ctx_deinit(struct ecc_ctx * ctx)186 static void ecdsa_ecc_ctx_deinit(struct ecc_ctx *ctx)
187 {
188 ctx->pub_key_set = false;
189 }
190
ecdsa_ecc_ctx_reset(struct ecc_ctx * ctx)191 static int ecdsa_ecc_ctx_reset(struct ecc_ctx *ctx)
192 {
193 unsigned int curve_id = ctx->curve_id;
194 int ret;
195
196 ecdsa_ecc_ctx_deinit(ctx);
197 ret = ecdsa_ecc_ctx_init(ctx, curve_id);
198 if (ret == 0)
199 ctx->pub_key = ECC_POINT_INIT(ctx->x, ctx->y,
200 ctx->curve->g.ndigits);
201 return ret;
202 }
203
204 /*
205 * Set the public key given the raw uncompressed key data from an X509
206 * certificate. The key data contain the concatenated X and Y coordinates of
207 * the public key.
208 */
ecdsa_set_pub_key(struct crypto_akcipher * tfm,const void * key,unsigned int keylen)209 static int ecdsa_set_pub_key(struct crypto_akcipher *tfm, const void *key, unsigned int keylen)
210 {
211 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
212 unsigned int digitlen, ndigits;
213 const unsigned char *d = key;
214 int ret;
215
216 ret = ecdsa_ecc_ctx_reset(ctx);
217 if (ret < 0)
218 return ret;
219
220 if (keylen < 1 || (((keylen - 1) >> 1) % sizeof(u64)) != 0)
221 return -EINVAL;
222 /* we only accept uncompressed format indicated by '4' */
223 if (d[0] != 4)
224 return -EINVAL;
225
226 keylen--;
227 digitlen = keylen >> 1;
228
229 ndigits = DIV_ROUND_UP(digitlen, sizeof(u64));
230 if (ndigits != ctx->curve->g.ndigits)
231 return -EINVAL;
232
233 d++;
234
235 ecc_digits_from_bytes(d, digitlen, ctx->pub_key.x, ndigits);
236 ecc_digits_from_bytes(&d[digitlen], digitlen, ctx->pub_key.y, ndigits);
237
238 ret = ecc_is_pubkey_valid_full(ctx->curve, &ctx->pub_key);
239
240 ctx->pub_key_set = ret == 0;
241
242 return ret;
243 }
244
ecdsa_exit_tfm(struct crypto_akcipher * tfm)245 static void ecdsa_exit_tfm(struct crypto_akcipher *tfm)
246 {
247 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
248
249 ecdsa_ecc_ctx_deinit(ctx);
250 }
251
ecdsa_max_size(struct crypto_akcipher * tfm)252 static unsigned int ecdsa_max_size(struct crypto_akcipher *tfm)
253 {
254 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
255
256 return ctx->pub_key.ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
257 }
258
ecdsa_nist_p384_init_tfm(struct crypto_akcipher * tfm)259 static int ecdsa_nist_p384_init_tfm(struct crypto_akcipher *tfm)
260 {
261 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
262
263 return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P384);
264 }
265
266 static struct akcipher_alg ecdsa_nist_p384 = {
267 .verify = ecdsa_verify,
268 .set_pub_key = ecdsa_set_pub_key,
269 .max_size = ecdsa_max_size,
270 .init = ecdsa_nist_p384_init_tfm,
271 .exit = ecdsa_exit_tfm,
272 .base = {
273 .cra_name = "ecdsa-nist-p384",
274 .cra_driver_name = "ecdsa-nist-p384-generic",
275 .cra_priority = 100,
276 .cra_module = THIS_MODULE,
277 .cra_ctxsize = sizeof(struct ecc_ctx),
278 },
279 };
280
ecdsa_nist_p256_init_tfm(struct crypto_akcipher * tfm)281 static int ecdsa_nist_p256_init_tfm(struct crypto_akcipher *tfm)
282 {
283 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
284
285 return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P256);
286 }
287
288 static struct akcipher_alg ecdsa_nist_p256 = {
289 .verify = ecdsa_verify,
290 .set_pub_key = ecdsa_set_pub_key,
291 .max_size = ecdsa_max_size,
292 .init = ecdsa_nist_p256_init_tfm,
293 .exit = ecdsa_exit_tfm,
294 .base = {
295 .cra_name = "ecdsa-nist-p256",
296 .cra_driver_name = "ecdsa-nist-p256-generic",
297 .cra_priority = 100,
298 .cra_module = THIS_MODULE,
299 .cra_ctxsize = sizeof(struct ecc_ctx),
300 },
301 };
302
ecdsa_nist_p192_init_tfm(struct crypto_akcipher * tfm)303 static int ecdsa_nist_p192_init_tfm(struct crypto_akcipher *tfm)
304 {
305 struct ecc_ctx *ctx = akcipher_tfm_ctx(tfm);
306
307 return ecdsa_ecc_ctx_init(ctx, ECC_CURVE_NIST_P192);
308 }
309
310 static struct akcipher_alg ecdsa_nist_p192 = {
311 .verify = ecdsa_verify,
312 .set_pub_key = ecdsa_set_pub_key,
313 .max_size = ecdsa_max_size,
314 .init = ecdsa_nist_p192_init_tfm,
315 .exit = ecdsa_exit_tfm,
316 .base = {
317 .cra_name = "ecdsa-nist-p192",
318 .cra_driver_name = "ecdsa-nist-p192-generic",
319 .cra_priority = 100,
320 .cra_module = THIS_MODULE,
321 .cra_ctxsize = sizeof(struct ecc_ctx),
322 },
323 };
324 static bool ecdsa_nist_p192_registered;
325
ecdsa_init(void)326 static int __init ecdsa_init(void)
327 {
328 int ret;
329
330 /* NIST p192 may not be available in FIPS mode */
331 ret = crypto_register_akcipher(&ecdsa_nist_p192);
332 ecdsa_nist_p192_registered = ret == 0;
333
334 ret = crypto_register_akcipher(&ecdsa_nist_p256);
335 if (ret)
336 goto nist_p256_error;
337
338 ret = crypto_register_akcipher(&ecdsa_nist_p384);
339 if (ret)
340 goto nist_p384_error;
341
342 return 0;
343
344 nist_p384_error:
345 crypto_unregister_akcipher(&ecdsa_nist_p256);
346
347 nist_p256_error:
348 if (ecdsa_nist_p192_registered)
349 crypto_unregister_akcipher(&ecdsa_nist_p192);
350 return ret;
351 }
352
ecdsa_exit(void)353 static void __exit ecdsa_exit(void)
354 {
355 if (ecdsa_nist_p192_registered)
356 crypto_unregister_akcipher(&ecdsa_nist_p192);
357 crypto_unregister_akcipher(&ecdsa_nist_p256);
358 crypto_unregister_akcipher(&ecdsa_nist_p384);
359 }
360
361 subsys_initcall(ecdsa_init);
362 module_exit(ecdsa_exit);
363
364 MODULE_LICENSE("GPL");
365 MODULE_AUTHOR("Stefan Berger <stefanb@linux.ibm.com>");
366 MODULE_DESCRIPTION("ECDSA generic algorithm");
367 MODULE_ALIAS_CRYPTO("ecdsa-nist-p192");
368 MODULE_ALIAS_CRYPTO("ecdsa-nist-p256");
369 MODULE_ALIAS_CRYPTO("ecdsa-nist-p384");
370 MODULE_ALIAS_CRYPTO("ecdsa-generic");
371