dh.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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 (unlikely(!params->p || !params->g))
  50. return -EINVAL;
  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. ctx->g = mpi_read_raw_data(params->g, params->g_size);
  57. if (!ctx->g)
  58. return -EINVAL;
  59. return 0;
  60. }
  61. static int dh_set_secret(struct crypto_kpp *tfm, void *buf, unsigned int len)
  62. {
  63. struct dh_ctx *ctx = dh_get_ctx(tfm);
  64. struct dh params;
  65. /* Free the old MPI key if any */
  66. dh_clear_ctx(ctx);
  67. if (crypto_dh_decode_key(buf, len, &params) < 0)
  68. goto err_clear_ctx;
  69. if (dh_set_params(ctx, &params) < 0)
  70. goto err_clear_ctx;
  71. ctx->xa = mpi_read_raw_data(params.key, params.key_size);
  72. if (!ctx->xa)
  73. goto err_clear_ctx;
  74. return 0;
  75. err_clear_ctx:
  76. dh_clear_ctx(ctx);
  77. return -EINVAL;
  78. }
  79. static int dh_compute_value(struct kpp_request *req)
  80. {
  81. struct crypto_kpp *tfm = crypto_kpp_reqtfm(req);
  82. struct dh_ctx *ctx = dh_get_ctx(tfm);
  83. MPI base, val = mpi_alloc(0);
  84. int ret = 0;
  85. int sign;
  86. if (!val)
  87. return -ENOMEM;
  88. if (unlikely(!ctx->xa)) {
  89. ret = -EINVAL;
  90. goto err_free_val;
  91. }
  92. if (req->src) {
  93. base = mpi_read_raw_from_sgl(req->src, req->src_len);
  94. if (!base) {
  95. ret = EINVAL;
  96. goto err_free_val;
  97. }
  98. } else {
  99. base = ctx->g;
  100. }
  101. ret = _compute_val(ctx, base, val);
  102. if (ret)
  103. goto err_free_base;
  104. ret = mpi_write_to_sgl(val, req->dst, req->dst_len, &sign);
  105. if (ret)
  106. goto err_free_base;
  107. if (sign < 0)
  108. ret = -EBADMSG;
  109. err_free_base:
  110. if (req->src)
  111. mpi_free(base);
  112. err_free_val:
  113. mpi_free(val);
  114. return ret;
  115. }
  116. static int dh_max_size(struct crypto_kpp *tfm)
  117. {
  118. struct dh_ctx *ctx = dh_get_ctx(tfm);
  119. return mpi_get_size(ctx->p);
  120. }
  121. static void dh_exit_tfm(struct crypto_kpp *tfm)
  122. {
  123. struct dh_ctx *ctx = dh_get_ctx(tfm);
  124. dh_clear_ctx(ctx);
  125. }
  126. static struct kpp_alg dh = {
  127. .set_secret = dh_set_secret,
  128. .generate_public_key = dh_compute_value,
  129. .compute_shared_secret = dh_compute_value,
  130. .max_size = dh_max_size,
  131. .exit = dh_exit_tfm,
  132. .base = {
  133. .cra_name = "dh",
  134. .cra_driver_name = "dh-generic",
  135. .cra_priority = 100,
  136. .cra_module = THIS_MODULE,
  137. .cra_ctxsize = sizeof(struct dh_ctx),
  138. },
  139. };
  140. static int dh_init(void)
  141. {
  142. return crypto_register_kpp(&dh);
  143. }
  144. static void dh_exit(void)
  145. {
  146. crypto_unregister_kpp(&dh);
  147. }
  148. module_init(dh_init);
  149. module_exit(dh_exit);
  150. MODULE_ALIAS_CRYPTO("dh");
  151. MODULE_LICENSE("GPL");
  152. MODULE_DESCRIPTION("DH generic algorithm");