ecdh.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 3;
  30. case ECC_CURVE_NIST_P256: return 4;
  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. params.key_size > sizeof(ctx->private_key))
  42. return -EINVAL;
  43. ndigits = ecdh_supported_curve(params.curve_id);
  44. if (!ndigits)
  45. return -EINVAL;
  46. ctx->curve_id = params.curve_id;
  47. ctx->ndigits = ndigits;
  48. if (!params.key || !params.key_size)
  49. return ecc_gen_privkey(ctx->curve_id, ctx->ndigits,
  50. ctx->private_key);
  51. memcpy(ctx->private_key, params.key, params.key_size);
  52. if (ecc_is_key_valid(ctx->curve_id, ctx->ndigits,
  53. ctx->private_key, params.key_size) < 0) {
  54. memzero_explicit(ctx->private_key, params.key_size);
  55. return -EINVAL;
  56. }
  57. return 0;
  58. }
  59. static int ecdh_compute_value(struct kpp_request *req)
  60. {
  61. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  62. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  63. u64 *public_key;
  64. u64 *shared_secret = NULL;
  65. void *buf;
  66. size_t copied, nbytes, public_key_sz;
  67. int ret = -ENOMEM;
  68. nbytes = ctx->ndigits << ECC_DIGITS_TO_BYTES_SHIFT;
  69. /* Public part is a point thus it has both coordinates */
  70. public_key_sz = 2 * nbytes;
  71. public_key = kmalloc(public_key_sz, GFP_KERNEL);
  72. if (!public_key)
  73. return -ENOMEM;
  74. if (req->src) {
  75. shared_secret = kmalloc(nbytes, GFP_KERNEL);
  76. if (!shared_secret)
  77. goto free_pubkey;
  78. copied = sg_copy_to_buffer(req->src, 1, public_key,
  79. public_key_sz);
  80. if (copied != public_key_sz) {
  81. ret = -EINVAL;
  82. goto free_all;
  83. }
  84. ret = crypto_ecdh_shared_secret(ctx->curve_id, ctx->ndigits,
  85. ctx->private_key, public_key,
  86. shared_secret);
  87. buf = shared_secret;
  88. } else {
  89. ret = ecc_make_pub_key(ctx->curve_id, ctx->ndigits,
  90. ctx->private_key, public_key);
  91. buf = public_key;
  92. nbytes = public_key_sz;
  93. }
  94. if (ret < 0)
  95. goto free_all;
  96. copied = sg_copy_from_buffer(req->dst, 1, buf, nbytes);
  97. if (copied != nbytes)
  98. ret = -EINVAL;
  99. /* fall through */
  100. free_all:
  101. kzfree(shared_secret);
  102. free_pubkey:
  103. kfree(public_key);
  104. return ret;
  105. }
  106. static unsigned int ecdh_max_size(struct crypto_kpp *tfm)
  107. {
  108. struct ecdh_ctx *ctx = ecdh_get_ctx(tfm);
  109. /* Public key is made of two coordinates, add one to the left shift */
  110. return ctx->ndigits << (ECC_DIGITS_TO_BYTES_SHIFT + 1);
  111. }
  112. static void no_exit_tfm(struct crypto_kpp *tfm)
  113. {
  114. return;
  115. }
  116. static struct kpp_alg ecdh = {
  117. .set_secret = ecdh_set_secret,
  118. .generate_public_key = ecdh_compute_value,
  119. .compute_shared_secret = ecdh_compute_value,
  120. .max_size = ecdh_max_size,
  121. .exit = no_exit_tfm,
  122. .base = {
  123. .cra_name = "ecdh",
  124. .cra_driver_name = "ecdh-generic",
  125. .cra_priority = 100,
  126. .cra_module = THIS_MODULE,
  127. .cra_ctxsize = sizeof(struct ecdh_ctx),
  128. },
  129. };
  130. static int ecdh_init(void)
  131. {
  132. return crypto_register_kpp(&ecdh);
  133. }
  134. static void ecdh_exit(void)
  135. {
  136. crypto_unregister_kpp(&ecdh);
  137. }
  138. module_init(ecdh_init);
  139. module_exit(ecdh_exit);
  140. MODULE_ALIAS_CRYPTO("ecdh");
  141. MODULE_LICENSE("GPL");
  142. MODULE_DESCRIPTION("ECDH generic algorithm");