ecdsa.c 9.4 KB

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