dh.c 3.7 KB

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