ecdh_helper.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * ECDH helper functions - KPP wrappings
  3. *
  4. * Copyright (C) 2017 Intel Corporation
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation;
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  11. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  12. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  13. * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  14. * CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. *
  19. * ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  20. * COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  21. * SOFTWARE IS DISCLAIMED.
  22. */
  23. #include "ecdh_helper.h"
  24. #include <linux/scatterlist.h>
  25. #include <crypto/ecdh.h>
  26. struct ecdh_completion {
  27. struct completion completion;
  28. int err;
  29. };
  30. static void ecdh_complete(struct crypto_async_request *req, int err)
  31. {
  32. struct ecdh_completion *res = req->data;
  33. if (err == -EINPROGRESS)
  34. return;
  35. res->err = err;
  36. complete(&res->completion);
  37. }
  38. static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
  39. {
  40. int i;
  41. for (i = 0; i < ndigits; i++)
  42. out[i] = __swab64(in[ndigits - 1 - i]);
  43. }
  44. /* compute_ecdh_secret() - function assumes that the private key was
  45. * already set.
  46. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  47. * @public_key: pair's ecc public key.
  48. * secret: memory where the ecdh computed shared secret will be saved.
  49. *
  50. * Return: zero on success; error code in case of error.
  51. */
  52. int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
  53. u8 secret[32])
  54. {
  55. struct kpp_request *req;
  56. u8 *tmp;
  57. struct ecdh_completion result;
  58. struct scatterlist src, dst;
  59. int err;
  60. tmp = kmalloc(64, GFP_KERNEL);
  61. if (!tmp)
  62. return -ENOMEM;
  63. req = kpp_request_alloc(tfm, GFP_KERNEL);
  64. if (!req) {
  65. err = -ENOMEM;
  66. goto free_tmp;
  67. }
  68. init_completion(&result.completion);
  69. swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
  70. swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
  71. sg_init_one(&src, tmp, 64);
  72. sg_init_one(&dst, secret, 32);
  73. kpp_request_set_input(req, &src, 64);
  74. kpp_request_set_output(req, &dst, 32);
  75. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  76. ecdh_complete, &result);
  77. err = crypto_kpp_compute_shared_secret(req);
  78. if (err == -EINPROGRESS) {
  79. wait_for_completion(&result.completion);
  80. err = result.err;
  81. }
  82. if (err < 0) {
  83. pr_err("alg: ecdh: compute shared secret failed. err %d\n",
  84. err);
  85. goto free_all;
  86. }
  87. swap_digits((u64 *)secret, (u64 *)tmp, 4);
  88. memcpy(secret, tmp, 32);
  89. free_all:
  90. kpp_request_free(req);
  91. free_tmp:
  92. kfree_sensitive(tmp);
  93. return err;
  94. }
  95. /* set_ecdh_privkey() - set or generate ecc private key.
  96. *
  97. * Function generates an ecc private key in the crypto subsystem when receiving
  98. * a NULL private key or sets the received key when not NULL.
  99. *
  100. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  101. * @private_key: user's ecc private key. When not NULL, the key is expected
  102. * in little endian format.
  103. *
  104. * Return: zero on success; error code in case of error.
  105. */
  106. int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
  107. {
  108. u8 *buf, *tmp = NULL;
  109. unsigned int buf_len;
  110. int err;
  111. struct ecdh p = {0};
  112. p.curve_id = ECC_CURVE_NIST_P256;
  113. if (private_key) {
  114. tmp = kmalloc(32, GFP_KERNEL);
  115. if (!tmp)
  116. return -ENOMEM;
  117. swap_digits((u64 *)private_key, (u64 *)tmp, 4);
  118. p.key = tmp;
  119. p.key_size = 32;
  120. }
  121. buf_len = crypto_ecdh_key_len(&p);
  122. buf = kmalloc(buf_len, GFP_KERNEL);
  123. if (!buf) {
  124. err = -ENOMEM;
  125. goto free_tmp;
  126. }
  127. err = crypto_ecdh_encode_key(buf, buf_len, &p);
  128. if (err)
  129. goto free_all;
  130. err = crypto_kpp_set_secret(tfm, buf, buf_len);
  131. /* fall through */
  132. free_all:
  133. kfree_sensitive(buf);
  134. free_tmp:
  135. kfree_sensitive(tmp);
  136. return err;
  137. }
  138. /* generate_ecdh_public_key() - function assumes that the private key was
  139. * already set.
  140. *
  141. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  142. * @public_key: memory where the computed ecc public key will be saved.
  143. *
  144. * Return: zero on success; error code in case of error.
  145. */
  146. int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
  147. {
  148. struct kpp_request *req;
  149. u8 *tmp;
  150. struct ecdh_completion result;
  151. struct scatterlist dst;
  152. int err;
  153. tmp = kmalloc(64, GFP_KERNEL);
  154. if (!tmp)
  155. return -ENOMEM;
  156. req = kpp_request_alloc(tfm, GFP_KERNEL);
  157. if (!req) {
  158. err = -ENOMEM;
  159. goto free_tmp;
  160. }
  161. init_completion(&result.completion);
  162. sg_init_one(&dst, tmp, 64);
  163. kpp_request_set_input(req, NULL, 0);
  164. kpp_request_set_output(req, &dst, 64);
  165. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  166. ecdh_complete, &result);
  167. err = crypto_kpp_generate_public_key(req);
  168. if (err == -EINPROGRESS) {
  169. wait_for_completion(&result.completion);
  170. err = result.err;
  171. }
  172. if (err < 0)
  173. goto free_all;
  174. /* The public key is handed back in little endian as expected by
  175. * the Security Manager Protocol.
  176. */
  177. swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
  178. swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
  179. free_all:
  180. kpp_request_free(req);
  181. free_tmp:
  182. kfree(tmp);
  183. return err;
  184. }
  185. /* generate_ecdh_keys() - generate ecc key pair.
  186. *
  187. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  188. * @public_key: memory where the computed ecc public key will be saved.
  189. *
  190. * Return: zero on success; error code in case of error.
  191. */
  192. int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
  193. {
  194. int err;
  195. err = set_ecdh_privkey(tfm, NULL);
  196. if (err)
  197. return err;
  198. return generate_ecdh_public_key(tfm, public_key);
  199. }