ecdh_helper.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. if (private_key) {
  113. tmp = kmalloc(32, GFP_KERNEL);
  114. if (!tmp)
  115. return -ENOMEM;
  116. swap_digits((u64 *)private_key, (u64 *)tmp, 4);
  117. p.key = tmp;
  118. p.key_size = 32;
  119. }
  120. buf_len = crypto_ecdh_key_len(&p);
  121. buf = kmalloc(buf_len, GFP_KERNEL);
  122. if (!buf) {
  123. err = -ENOMEM;
  124. goto free_tmp;
  125. }
  126. err = crypto_ecdh_encode_key(buf, buf_len, &p);
  127. if (err)
  128. goto free_all;
  129. err = crypto_kpp_set_secret(tfm, buf, buf_len);
  130. /* fall through */
  131. free_all:
  132. kfree_sensitive(buf);
  133. free_tmp:
  134. kfree_sensitive(tmp);
  135. return err;
  136. }
  137. /* generate_ecdh_public_key() - function assumes that the private key was
  138. * already set.
  139. *
  140. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  141. * @public_key: memory where the computed ecc public key will be saved.
  142. *
  143. * Return: zero on success; error code in case of error.
  144. */
  145. int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
  146. {
  147. struct kpp_request *req;
  148. u8 *tmp;
  149. struct ecdh_completion result;
  150. struct scatterlist dst;
  151. int err;
  152. tmp = kmalloc(64, GFP_KERNEL);
  153. if (!tmp)
  154. return -ENOMEM;
  155. req = kpp_request_alloc(tfm, GFP_KERNEL);
  156. if (!req) {
  157. err = -ENOMEM;
  158. goto free_tmp;
  159. }
  160. init_completion(&result.completion);
  161. sg_init_one(&dst, tmp, 64);
  162. kpp_request_set_input(req, NULL, 0);
  163. kpp_request_set_output(req, &dst, 64);
  164. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  165. ecdh_complete, &result);
  166. err = crypto_kpp_generate_public_key(req);
  167. if (err == -EINPROGRESS) {
  168. wait_for_completion(&result.completion);
  169. err = result.err;
  170. }
  171. if (err < 0)
  172. goto free_all;
  173. /* The public key is handed back in little endian as expected by
  174. * the Security Manager Protocol.
  175. */
  176. swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
  177. swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
  178. free_all:
  179. kpp_request_free(req);
  180. free_tmp:
  181. kfree(tmp);
  182. return err;
  183. }
  184. /* generate_ecdh_keys() - generate ecc key pair.
  185. *
  186. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  187. * @public_key: memory where the computed ecc public key will be saved.
  188. *
  189. * Return: zero on success; error code in case of error.
  190. */
  191. int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
  192. {
  193. int err;
  194. err = set_ecdh_privkey(tfm, NULL);
  195. if (err)
  196. return err;
  197. return generate_ecdh_public_key(tfm, public_key);
  198. }