ecdh.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* ECDH key-agreement protocol
  3. *
  4. * Copyright (c) 2016, Intel Corporation
  5. * Authors: Salvator Benedetto <[email protected]>
  6. */
  7. #include <linux/module.h>
  8. #include <crypto/internal/ecc.h>
  9. #include <crypto/internal/kpp.h>
  10. #include <crypto/kpp.h>
  11. #include <crypto/ecdh.h>
  12. #include <linux/scatterlist.h>
  13. struct ecdh_ctx {
  14. unsigned int curve_id;
  15. unsigned int ndigits;
  16. u64 private_key[ECC_MAX_DIGITS];
  17. };
  18. static inline struct ecdh_ctx *ecdh_get_ctx(struct crypto_kpp *tfm)
  19. {
  20. return kpp_tfm_ctx(tfm);
  21. }
  22. static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
  23. unsigned int len)
  24. {
  25. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  26. struct ecdh params;
  27. if (crypto_ecdh_decode_key(buf, len, &params) < 0 ||
  28. params.key_size > sizeof(u64) * ctx->ndigits)
  29. return -EINVAL;
  30. if (!params.key || !params.key_size)
  31. return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
  32. ctx->private_key);
  33. memcpy(ctx->private_key, params.key, params.key_size);
  34. if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
  35. ctx->private_key, params.key_size) < 0) {
  36. memzero_explicit(ctx->private_key, params.key_size);
  37. return -EINVAL;
  38. }
  39. return 0;
  40. }
  41. static int ecdh_compute_value(struct kpp_request *req)
  42. {
  43. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  44. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  45. u64 *public_key;
  46. u64 *shared_secret = NULL;
  47. void *buf;
  48. size_t copied, nbytes, public_key_sz;
  49. int ret = -ENOMEM;
  50. nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  51. /* Public part is a point thus it has both coordinates */
  52. public_key_sz = 2 * nbytes;
  53. public_key = kmalloc(public_key_sz, GFP_KERNEL);
  54. if (!public_key)
  55. return -ENOMEM;
  56. if (req->src) {
  57. shared_secret = kmalloc(nbytes, GFP_KERNEL);
  58. if (!shared_secret)
  59. goto free_pubkey;
  60. /* from here on it's invalid parameters */
  61. ret = -EINVAL;
  62. /* must have exactly two points to be on the curve */
  63. if (public_key_sz != req->src_len)
  64. goto free_all;
  65. copied = sg_copy_to_buffer(req->src,
  66. sg_nents_for_len(req->src,
  67. public_key_sz),
  68. public_key, public_key_sz);
  69. if (copied != public_key_sz)
  70. goto free_all;
  71. ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
  72. ctx->private_key, public_key,
  73. shared_secret);
  74. buf = shared_secret;
  75. } else {
  76. ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
  77. ctx->private_key, public_key);
  78. buf = public_key;
  79. nbytes = public_key_sz;
  80. }
  81. if (ret < 0)
  82. goto free_all;
  83. /* might want less than we've got */
  84. nbytes = min_t(size_t, nbytes, req->dst_len);
  85. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
  86. nbytes),
  87. buf, nbytes);
  88. if (copied != nbytes)
  89. ret = -EINVAL;
  90. /* fall through */
  91. free_all:
  92. kfree_sensitive(shared_secret);
  93. free_pubkey:
  94. kfree(public_key);
  95. return ret;
  96. }
  97. static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
  98. {
  99. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  100. /* Public key is made of two coordinates, add one to the left shift */
  101. return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
  102. }
  103. static int ecdh_nist_p192_init_tfm(struct crypto_kpp *tfm)
  104. {
  105. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  106. ctx->curve_id = ECC_CURVE_NIST_P192;
  107. ctx->ndigits = ECC_CURVE_NIST_P192_DIGITS;
  108. return 0;
  109. }
  110. static struct kpp_alg ecdh_nist_p192 = {
  111. .set_secret = ecdh_set_secret,
  112. .generate_public_key = ecdh_compute_value,
  113. .compute_shared_secret = ecdh_compute_value,
  114. .max_size = ecdh_max_size,
  115. .init = ecdh_nist_p192_init_tfm,
  116. .base = {
  117. .cra_name = "ecdh-nist-p192",
  118. .cra_driver_name = "ecdh-nist-p192-generic",
  119. .cra_priority = 100,
  120. .cra_module = THIS_MODULE,
  121. .cra_ctxsize = sizeof(struct ecdh_ctx),
  122. },
  123. };
  124. static int ecdh_nist_p256_init_tfm(struct crypto_kpp *tfm)
  125. {
  126. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  127. ctx->curve_id = ECC_CURVE_NIST_P256;
  128. ctx->ndigits = ECC_CURVE_NIST_P256_DIGITS;
  129. return 0;
  130. }
  131. static struct kpp_alg ecdh_nist_p256 = {
  132. .set_secret = ecdh_set_secret,
  133. .generate_public_key = ecdh_compute_value,
  134. .compute_shared_secret = ecdh_compute_value,
  135. .max_size = ecdh_max_size,
  136. .init = ecdh_nist_p256_init_tfm,
  137. .base = {
  138. .cra_name = "ecdh-nist-p256",
  139. .cra_driver_name = "ecdh-nist-p256-generic",
  140. .cra_priority = 100,
  141. .cra_module = THIS_MODULE,
  142. .cra_ctxsize = sizeof(struct ecdh_ctx),
  143. },
  144. };
  145. static int ecdh_nist_p384_init_tfm(struct crypto_kpp *tfm)
  146. {
  147. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  148. ctx->curve_id = ECC_CURVE_NIST_P384;
  149. ctx->ndigits = ECC_CURVE_NIST_P384_DIGITS;
  150. return 0;
  151. }
  152. static struct kpp_alg ecdh_nist_p384 = {
  153. .set_secret = ecdh_set_secret,
  154. .generate_public_key = ecdh_compute_value,
  155. .compute_shared_secret = ecdh_compute_value,
  156. .max_size = ecdh_max_size,
  157. .init = ecdh_nist_p384_init_tfm,
  158. .base = {
  159. .cra_name = "ecdh-nist-p384",
  160. .cra_driver_name = "ecdh-nist-p384-generic",
  161. .cra_priority = 100,
  162. .cra_module = THIS_MODULE,
  163. .cra_ctxsize = sizeof(struct ecdh_ctx),
  164. },
  165. };
  166. static bool ecdh_nist_p192_registered;
  167. static int __init ecdh_init(void)
  168. {
  169. int ret;
  170. /* NIST p192 will fail to register in FIPS mode */
  171. ret = crypto_register_kpp(&ecdh_nist_p192);
  172. ecdh_nist_p192_registered = ret == 0;
  173. ret = crypto_register_kpp(&ecdh_nist_p256);
  174. if (ret)
  175. goto nist_p256_error;
  176. ret = crypto_register_kpp(&ecdh_nist_p384);
  177. if (ret)
  178. goto nist_p384_error;
  179. return 0;
  180. nist_p384_error:
  181. crypto_unregister_kpp(&ecdh_nist_p256);
  182. nist_p256_error:
  183. if (ecdh_nist_p192_registered)
  184. crypto_unregister_kpp(&ecdh_nist_p192);
  185. return ret;
  186. }
  187. static void __exit ecdh_exit(void)
  188. {
  189. if (ecdh_nist_p192_registered)
  190. crypto_unregister_kpp(&ecdh_nist_p192);
  191. crypto_unregister_kpp(&ecdh_nist_p256);
  192. crypto_unregister_kpp(&ecdh_nist_p384);
  193. }
  194. subsys_initcall(ecdh_init);
  195. module_exit(ecdh_exit);
  196. MODULE_ALIAS_CRYPTO("ecdh");
  197. MODULE_LICENSE("GPL");
  198. MODULE_DESCRIPTION("ECDH generic algorithm");