Linux Audio

Check our new training course

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