ecdh_helper.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. static inline void swap_digits(u64 *in, u64 *out, unsigned int ndigits)
  27. {
  28. int i;
  29. for (i = 0; i < ndigits; i++)
  30. out[i] = __swab64(in[ndigits - 1 - i]);
  31. }
  32. /* compute_ecdh_secret() - function assumes that the private key was
  33. * already set.
  34. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  35. * @public_key: pair's ecc public key.
  36. * secret: memory where the ecdh computed shared secret will be saved.
  37. *
  38. * Return: zero on success; error code in case of error.
  39. */
  40. int compute_ecdh_secret(struct crypto_kpp *tfm, const u8 public_key[64],
  41. u8 secret[32])
  42. {
  43. DECLARE_CRYPTO_WAIT(result);
  44. struct kpp_request *req;
  45. u8 *tmp;
  46. struct scatterlist src, dst;
  47. int err;
  48. tmp = kmalloc(64, GFP_KERNEL);
  49. if (!tmp)
  50. return -ENOMEM;
  51. req = kpp_request_alloc(tfm, GFP_KERNEL);
  52. if (!req) {
  53. err = -ENOMEM;
  54. goto free_tmp;
  55. }
  56. swap_digits((u64 *)public_key, (u64 *)tmp, 4); /* x */
  57. swap_digits((u64 *)&public_key[32], (u64 *)&tmp[32], 4); /* y */
  58. sg_init_one(&src, tmp, 64);
  59. sg_init_one(&dst, secret, 32);
  60. kpp_request_set_input(req, &src, 64);
  61. kpp_request_set_output(req, &dst, 32);
  62. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  63. crypto_req_done, &result);
  64. err = crypto_kpp_compute_shared_secret(req);
  65. err = crypto_wait_req(err, &result);
  66. if (err < 0) {
  67. pr_err("alg: ecdh: compute shared secret failed. err %d\n",
  68. err);
  69. goto free_all;
  70. }
  71. swap_digits((u64 *)secret, (u64 *)tmp, 4);
  72. memcpy(secret, tmp, 32);
  73. free_all:
  74. kpp_request_free(req);
  75. free_tmp:
  76. kfree_sensitive(tmp);
  77. return err;
  78. }
  79. /* set_ecdh_privkey() - set or generate ecc private key.
  80. *
  81. * Function generates an ecc private key in the crypto subsystem when receiving
  82. * a NULL private key or sets the received key when not NULL.
  83. *
  84. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  85. * @private_key: user's ecc private key. When not NULL, the key is expected
  86. * in little endian format.
  87. *
  88. * Return: zero on success; error code in case of error.
  89. */
  90. int set_ecdh_privkey(struct crypto_kpp *tfm, const u8 private_key[32])
  91. {
  92. u8 *buf, *tmp = NULL;
  93. unsigned int buf_len;
  94. int err;
  95. struct ecdh p = {0};
  96. if (private_key) {
  97. tmp = kmalloc(32, GFP_KERNEL);
  98. if (!tmp)
  99. return -ENOMEM;
  100. swap_digits((u64 *)private_key, (u64 *)tmp, 4);
  101. p.key = tmp;
  102. p.key_size = 32;
  103. }
  104. buf_len = crypto_ecdh_key_len(&p);
  105. buf = kmalloc(buf_len, GFP_KERNEL);
  106. if (!buf) {
  107. err = -ENOMEM;
  108. goto free_tmp;
  109. }
  110. err = crypto_ecdh_encode_key(buf, buf_len, &p);
  111. if (err)
  112. goto free_all;
  113. err = crypto_kpp_set_secret(tfm, buf, buf_len);
  114. /* fall through */
  115. free_all:
  116. kfree_sensitive(buf);
  117. free_tmp:
  118. kfree_sensitive(tmp);
  119. return err;
  120. }
  121. /* generate_ecdh_public_key() - function assumes that the private key was
  122. * already set.
  123. *
  124. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  125. * @public_key: memory where the computed ecc public key will be saved.
  126. *
  127. * Return: zero on success; error code in case of error.
  128. */
  129. int generate_ecdh_public_key(struct crypto_kpp *tfm, u8 public_key[64])
  130. {
  131. DECLARE_CRYPTO_WAIT(result);
  132. struct kpp_request *req;
  133. u8 *tmp;
  134. struct scatterlist dst;
  135. int err;
  136. tmp = kmalloc(64, GFP_KERNEL);
  137. if (!tmp)
  138. return -ENOMEM;
  139. req = kpp_request_alloc(tfm, GFP_KERNEL);
  140. if (!req) {
  141. err = -ENOMEM;
  142. goto free_tmp;
  143. }
  144. sg_init_one(&dst, tmp, 64);
  145. kpp_request_set_input(req, NULL, 0);
  146. kpp_request_set_output(req, &dst, 64);
  147. kpp_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG,
  148. crypto_req_done, &result);
  149. err = crypto_kpp_generate_public_key(req);
  150. err = crypto_wait_req(err, &result);
  151. if (err < 0)
  152. goto free_all;
  153. /* The public key is handed back in little endian as expected by
  154. * the Security Manager Protocol.
  155. */
  156. swap_digits((u64 *)tmp, (u64 *)public_key, 4); /* x */
  157. swap_digits((u64 *)&tmp[32], (u64 *)&public_key[32], 4); /* y */
  158. free_all:
  159. kpp_request_free(req);
  160. free_tmp:
  161. kfree(tmp);
  162. return err;
  163. }
  164. /* generate_ecdh_keys() - generate ecc key pair.
  165. *
  166. * @tfm: KPP tfm handle allocated with crypto_alloc_kpp().
  167. * @public_key: memory where the computed ecc public key will be saved.
  168. *
  169. * Return: zero on success; error code in case of error.
  170. */
  171. int generate_ecdh_keys(struct crypto_kpp *tfm, u8 public_key[64])
  172. {
  173. int err;
  174. err = set_ecdh_privkey(tfm, NULL);
  175. if (err)
  176. return err;
  177. return generate_ecdh_public_key(tfm, public_key);
  178. }