ecdh.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /* ECDH key-agreement protocol
  2. *
  3. * Copyright (c) 2016, Intel Corporation
  4. * Authors: Salvator Benedetto <salvatore.benedetto@intel.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <crypto/internal/kpp.h>
  13. #include <crypto/kpp.h>
  14. #include <crypto/ecdh.h>
  15. #include <linux/scatterlist.h>
  16. #include "ecc.h"
  17. struct ecdh_ctx {
  18. unsigned int curve_id;
  19. unsigned int ndigits;
  20. u64 private_key[ECC_MAX_DIGITS];
  21. };
  22. static inline struct ecdh_ctx *ecdh_get_ctx(struct crypto_kpp *tfm)
  23. {
  24. return kpp_tfm_ctx(tfm);
  25. }
  26. static unsigned int ecdh_supported_curve(unsigned int curve_id)
  27. {
  28. switch (curve_id) {
  29. case ECC_CURVE_NIST_P192: return ECC_CURVE_NIST_P192_DIGITS;
  30. case ECC_CURVE_NIST_P256: return ECC_CURVE_NIST_P256_DIGITS;
  31. default: return 0;
  32. }
  33. }
  34. static int ecdh_set_secret(struct crypto_kpp *tfm, const void *buf,
  35. unsigned int len)
  36. {
  37. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  38. struct ecdh params;
  39. unsigned int ndigits;
  40. if (crypto_ecdh_decode_key(buf, len, &params) < 0)
  41. return -EINVAL;
  42. ndigits = ecdh_supported_curve(params.curve_id);
  43. if (!ndigits)
  44. return -EINVAL;
  45. ctx->curve_id = params.curve_id;
  46. ctx->ndigits = ndigits;
  47. if (!params.key || !params.key_size)
  48. return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
  49. ctx->private_key);
  50. if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
  51. (const u64 *)params.key, params.key_size) < 0)
  52. return -EINVAL;
  53. memcpy(ctx->private_key, params.key, params.key_size);
  54. return 0;
  55. }
  56. static int ecdh_compute_value(struct kpp_request *req)
  57. {
  58. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  59. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  60. u64 *public_key;
  61. u64 *shared_secret = NULL;
  62. void *buf;
  63. size_t copied, nbytes, public_key_sz;
  64. int ret = -ENOMEM;
  65. nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  66. /* Public part is a point thus it has both coordinates */
  67. public_key_sz = 2 * nbytes;
  68. public_key = kmalloc(public_key_sz, GFP_KERNEL);
  69. if (!public_key)
  70. return -ENOMEM;
  71. if (req->src) {
  72. shared_secret = kmalloc(nbytes, GFP_KERNEL);
  73. if (!shared_secret)
  74. goto free_pubkey;
  75. /* from here on it's invalid parameters */
  76. ret = -EINVAL;
  77. /* must have exactly two points to be on the curve */
  78. if (public_key_sz != req->src_len)
  79. goto free_all;
  80. copied = sg_copy_to_buffer(req->src,
  81. sg_nents_for_len(req->src,
  82. public_key_sz),
  83. public_key, public_key_sz);
  84. if (copied != public_key_sz)
  85. goto free_all;
  86. ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
  87. ctx->private_key, public_key,
  88. shared_secret);
  89. buf = shared_secret;
  90. } else {
  91. ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
  92. ctx->private_key, public_key);
  93. buf = public_key;
  94. nbytes = public_key_sz;
  95. }
  96. if (ret < 0)
  97. goto free_all;
  98. /* might want less than we've got */
  99. nbytes = min_t(size_t, nbytes, req->dst_len);
  100. copied = sg_copy_from_buffer(req->dst, sg_nents_for_len(req->dst,
  101. nbytes),
  102. buf, nbytes);
  103. if (copied != nbytes)
  104. ret = -EINVAL;
  105. /* fall through */
  106. free_all:
  107. kzfree(shared_secret);
  108. free_pubkey:
  109. kfree(public_key);
  110. return ret;
  111. }
  112. static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
  113. {
  114. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  115. /* Public key is made of two coordinates, add one to the left shift */
  116. return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
  117. }
  118. static struct kpp_alg ecdh = {
  119. .set_secret = ecdh_set_secret,
  120. .generate_public_key = ecdh_compute_value,
  121. .compute_shared_secret = ecdh_compute_value,
  122. .max_size = ecdh_max_size,
  123. .base = {
  124. .cra_name = "ecdh",
  125. .cra_driver_name = "ecdh-generic",
  126. .cra_priority = 100,
  127. .cra_module = THIS_MODULE,
  128. .cra_ctxsize = sizeof(struct ecdh_ctx),
  129. },
  130. };
  131. static int ecdh_init(void)
  132. {
  133. return crypto_register_kpp(&ecdh);
  134. }
  135. static void ecdh_exit(void)
  136. {
  137. crypto_unregister_kpp(&ecdh);
  138. }
  139. module_init(ecdh_init);
  140. module_exit(ecdh_exit);
  141. MODULE_ALIAS_CRYPTO("ecdh");
  142. MODULE_LICENSE("GPL");
  143. MODULE_DESCRIPTION("ECDH generic algorithm");