ecdh.c 5.8 KB

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