dh.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /* Diffie-Hellman Key Agreement Method [RFC2631]
  2. *
  3. * Copyright (c) 2016, Intel Corporation
  4. * Authors: Salvatore 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/dh.h>
  15. #include <linux/mpi.h>
  16. struct dh_ctx {
  17. MPI p; /* Value is guaranteed to be set. */
  18. MPI q; /* Value is optional. */
  19. MPI g; /* Value is guaranteed to be set. */
  20. MPI xa; /* Value is guaranteed to be set. */
  21. };
  22. static void dh_clear_ctx(struct dh_ctx *ctx)
  23. {
  24. mpi_free(ctx->p);
  25. mpi_free(ctx->q);
  26. mpi_free(ctx->g);
  27. mpi_free(ctx->xa);
  28. memset(ctx, 0, sizeof(*ctx));
  29. }
  30. /*
  31. * If base is g we compute the public key
  32. * ya = g^xa mod p; [RFC2631 sec 2.1.1]
  33. * else if base if the counterpart public key we compute the shared secret
  34. * ZZ = yb^xa mod p; [RFC2631 sec 2.1.1]
  35. */
  36. static int _compute_val(const struct dh_ctx *ctx, MPI base, MPI val)
  37. {
  38. /* val = base^xa mod p */
  39. return mpi_powm(val, base, ctx->xa, ctx->p);
  40. }
  41. static inline struct dh_ctx *dh_get_ctx(struct crypto_kpp *tfm)
  42. {
  43. return kpp_tfm_ctx(tfm);
  44. }
  45. static int dh_check_params_length(unsigned int p_len)
  46. {
  47. return (p_len < 1536) ? -EINVAL : 0;
  48. }
  49. static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
  50. {
  51. if (dh_check_params_length(params->p_size << 3))
  52. return -EINVAL;
  53. ctx->p = mpi_read_raw_data(params->p, params->p_size);
  54. if (!ctx->p)
  55. return -EINVAL;
  56. if (params->q && params->q_size) {
  57. ctx->q = mpi_read_raw_data(params->q, params->q_size);
  58. if (!ctx->q)
  59. return -EINVAL;
  60. }
  61. ctx->g = mpi_read_raw_data(params->g, params->g_size);
  62. if (!ctx->g)
  63. return -EINVAL;
  64. return 0;
  65. }
  66. static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
  67. unsigned int len)
  68. {
  69. struct dh_ctx *ctx = dh_get_ctx(tfm);
  70. struct dh params;
  71. /* Free the old MPI key if any */
  72. dh_clear_ctx(ctx);
  73. if (crypto_dh_decode_key(buf, len, &params) < 0)
  74. goto err_clear_ctx;
  75. if (dh_set_params(ctx, &params) < 0)
  76. goto err_clear_ctx;
  77. ctx->xa = mpi_read_raw_data(params.key, params.key_size);
  78. if (!ctx->xa)
  79. goto err_clear_ctx;
  80. return 0;
  81. err_clear_ctx:
  82. dh_clear_ctx(ctx);
  83. return -EINVAL;
  84. }
  85. /*
  86. * SP800-56A public key verification:
  87. *
  88. * * If Q is provided as part of the domain paramenters, a full validation
  89. * according to SP800-56A section 5.6.2.3.1 is performed.
  90. *
  91. * * If Q is not provided, a partial validation according to SP800-56A section
  92. * 5.6.2.3.2 is performed.
  93. */
  94. static int dh_is_pubkey_valid(struct dh_ctx *ctx, MPI y)
  95. {
  96. if (unlikely(!ctx->p))
  97. return -EINVAL;
  98. /*
  99. * Step 1: Verify that 2 <= y <= p - 2.
  100. *
  101. * The upper limit check is actually y < p instead of y < p - 1
  102. * as the mpi_sub_ui function is yet missing.
  103. */
  104. if (mpi_cmp_ui(y, 1) < 1 || mpi_cmp(y, ctx->p) >= 0)
  105. return -EINVAL;
  106. /* Step 2: Verify that 1 = y^q mod p */
  107. if (ctx->q) {
  108. MPI val = mpi_alloc(0);
  109. int ret;
  110. if (!val)
  111. return -ENOMEM;
  112. ret = mpi_powm(val, y, ctx->q, ctx->p);
  113. if (ret) {
  114. mpi_free(val);
  115. return ret;
  116. }
  117. ret = mpi_cmp_ui(val, 1);
  118. mpi_free(val);
  119. if (ret != 0)
  120. return -EINVAL;
  121. }
  122. return 0;
  123. }
  124. static int dh_compute_value(struct kpp_request *req)
  125. {
  126. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  127. struct dh_ctx *ctx = dh_get_ctx(tfm);
  128. MPI base, val = mpi_alloc(0);
  129. int ret = 0;
  130. int sign;
  131. if (!val)
  132. return -ENOMEM;
  133. if (unlikely(!ctx->xa)) {
  134. ret = -EINVAL;
  135. goto err_free_val;
  136. }
  137. if (req->src) {
  138. base = mpi_read_raw_from_sgl(req->src, req->src_len);
  139. if (!base) {
  140. ret = -EINVAL;
  141. goto err_free_val;
  142. }
  143. ret = dh_is_pubkey_valid(ctx, base);
  144. if (ret)
  145. goto err_free_base;
  146. } else {
  147. base = ctx->g;
  148. }
  149. ret = _compute_val(ctx, base, val);
  150. if (ret)
  151. goto err_free_base;
  152. ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign);
  153. if (ret)
  154. goto err_free_base;
  155. if (sign < 0)
  156. ret = -EBADMSG;
  157. err_free_base:
  158. if (req->src)
  159. mpi_free(base);
  160. err_free_val:
  161. mpi_free(val);
  162. return ret;
  163. }
  164. static unsigned int dh_max_size(struct crypto_kpp *tfm)
  165. {
  166. struct dh_ctx *ctx = dh_get_ctx(tfm);
  167. return mpi_get_size(ctx->p);
  168. }
  169. static void dh_exit_tfm(struct crypto_kpp *tfm)
  170. {
  171. struct dh_ctx *ctx = dh_get_ctx(tfm);
  172. dh_clear_ctx(ctx);
  173. }
  174. static struct kpp_alg dh = {
  175. .set_secret = dh_set_secret,
  176. .generate_public_key = dh_compute_value,
  177. .compute_shared_secret = dh_compute_value,
  178. .max_size = dh_max_size,
  179. .exit = dh_exit_tfm,
  180. .base = {
  181. .cra_name = "dh",
  182. .cra_driver_name = "dh-generic",
  183. .cra_priority = 100,
  184. .cra_module = THIS_MODULE,
  185. .cra_ctxsize = sizeof(struct dh_ctx),
  186. },
  187. };
  188. static int dh_init(void)
  189. {
  190. return crypto_register_kpp(&dh);
  191. }
  192. static void dh_exit(void)
  193. {
  194. crypto_unregister_kpp(&dh);
  195. }
  196. module_init(dh_init);
  197. module_exit(dh_exit);
  198. MODULE_ALIAS_CRYPTO("dh");
  199. MODULE_LICENSE("GPL");
  200. MODULE_DESCRIPTION("DH generic algorithm");