dh.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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;
  18. MPI g;
  19. MPI xa;
  20. };
  21. static void dh_clear_ctx(struct dh_ctx *ctx)
  22. {
  23. mpi_free(ctx->p);
  24. mpi_free(ctx->g);
  25. mpi_free(ctx->xa);
  26. memset(ctx, 0, sizeof(*ctx));
  27. }
  28. /*
  29. * If base is g we compute the public key
  30. * ya = g^xa mod p; [RFC2631 sec 2.1.1]
  31. * else if base if the counterpart public key we compute the shared secret
  32. * ZZ = yb^xa mod p; [RFC2631 sec 2.1.1]
  33. */
  34. static int _compute_val(const struct dh_ctx *ctx, MPI base, MPI val)
  35. {
  36. /* val = base^xa mod p */
  37. return mpi_powm(val, base, ctx->xa, ctx->p);
  38. }
  39. static inline struct dh_ctx *dh_get_ctx(struct crypto_kpp *tfm)
  40. {
  41. return kpp_tfm_ctx(tfm);
  42. }
  43. static int dh_check_params_length(unsigned int p_len)
  44. {
  45. return (p_len < 1536) ? -EINVAL : 0;
  46. }
  47. static int dh_set_params(struct dh_ctx *ctx, struct dh *params)
  48. {
  49. if (dh_check_params_length(params->p_size << 3))
  50. return -EINVAL;
  51. ctx->p = mpi_read_raw_data(params->p, params->p_size);
  52. if (!ctx->p)
  53. return -EINVAL;
  54. ctx->g = mpi_read_raw_data(params->g, params->g_size);
  55. if (!ctx->g)
  56. return -EINVAL;
  57. return 0;
  58. }
  59. static int dh_set_secret(struct crypto_kpp *tfm, const void *buf,
  60. unsigned int len)
  61. {
  62. struct dh_ctx *ctx = dh_get_ctx(tfm);
  63. struct dh params;
  64. /* Free the old MPI key if any */
  65. dh_clear_ctx(ctx);
  66. if (crypto_dh_decode_key(buf, len, &params) < 0)
  67. goto err_clear_ctx;
  68. if (dh_set_params(ctx, &params) < 0)
  69. goto err_clear_ctx;
  70. ctx->xa = mpi_read_raw_data(params.key, params.key_size);
  71. if (!ctx->xa)
  72. goto err_clear_ctx;
  73. return 0;
  74. err_clear_ctx:
  75. dh_clear_ctx(ctx);
  76. return -EINVAL;
  77. }
  78. static int dh_compute_value(struct kpp_request *req)
  79. {
  80. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  81. struct dh_ctx *ctx = dh_get_ctx(tfm);
  82. MPI base, val = mpi_alloc(0);
  83. int ret = 0;
  84. int sign;
  85. if (!val)
  86. return -ENOMEM;
  87. if (unlikely(!ctx->xa)) {
  88. ret = -EINVAL;
  89. goto err_free_val;
  90. }
  91. if (req->src) {
  92. base = mpi_read_raw_from_sgl(req->src, req->src_len);
  93. if (!base) {
  94. ret = -EINVAL;
  95. goto err_free_val;
  96. }
  97. } else {
  98. base = ctx->g;
  99. }
  100. ret = _compute_val(ctx, base, val);
  101. if (ret)
  102. goto err_free_base;
  103. ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign);
  104. if (ret)
  105. goto err_free_base;
  106. if (sign < 0)
  107. ret = -EBADMSG;
  108. err_free_base:
  109. if (req->src)
  110. mpi_free(base);
  111. err_free_val:
  112. mpi_free(val);
  113. return ret;
  114. }
  115. static unsigned int dh_max_size(struct crypto_kpp *tfm)
  116. {
  117. struct dh_ctx *ctx = dh_get_ctx(tfm);
  118. return mpi_get_size(ctx->p);
  119. }
  120. static void dh_exit_tfm(struct crypto_kpp *tfm)
  121. {
  122. struct dh_ctx *ctx = dh_get_ctx(tfm);
  123. dh_clear_ctx(ctx);
  124. }
  125. static struct kpp_alg dh = {
  126. .set_secret = dh_set_secret,
  127. .generate_public_key = dh_compute_value,
  128. .compute_shared_secret = dh_compute_value,
  129. .max_size = dh_max_size,
  130. .exit = dh_exit_tfm,
  131. .base = {
  132. .cra_name = "dh",
  133. .cra_driver_name = "dh-generic",
  134. .cra_priority = 100,
  135. .cra_module = THIS_MODULE,
  136. .cra_ctxsize = sizeof(struct dh_ctx),
  137. },
  138. };
  139. static int dh_init(void)
  140. {
  141. return crypto_register_kpp(&dh);
  142. }
  143. static void dh_exit(void)
  144. {
  145. crypto_unregister_kpp(&dh);
  146. }
  147. module_init(dh_init);
  148. module_exit(dh_exit);
  149. MODULE_ALIAS_CRYPTO("dh");
  150. MODULE_LICENSE("GPL");
  151. MODULE_DESCRIPTION("DH generic algorithm");