dh_helper.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2016, Intel Corporation
  4. * Authors: Salvatore Benedetto <salvatore.benedetto@intel.com>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/export.h>
  8. #include <linux/err.h>
  9. #include <linux/string.h>
  10. #include <crypto/dh.h>
  11. #include <crypto/kpp.h>
  12. #define DH_KPP_SECRET_MIN_SIZE (sizeof(struct kpp_secret) + 4 * sizeof(int))
  13. static inline u8 *dh_pack_data(u8 *dst, u8 *end, const void *src, size_t size)
  14. {
  15. if (!dst || size > end - dst)
  16. return NULL;
  17. memcpy(dst, src, size);
  18. return dst + size;
  19. }
  20. static inline const u8 *dh_unpack_data(void *dst, const void *src, size_t size)
  21. {
  22. memcpy(dst, src, size);
  23. return src + size;
  24. }
  25. static inline unsigned int dh_data_size(const struct dh *p)
  26. {
  27. return p->key_size + p->p_size + p->q_size + p->g_size;
  28. }
  29. unsigned int crypto_dh_key_len(const struct dh *p)
  30. {
  31. return DH_KPP_SECRET_MIN_SIZE + dh_data_size(p);
  32. }
  33. EXPORT_SYMBOL_GPL(crypto_dh_key_len);
  34. int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params)
  35. {
  36. u8 *ptr = buf;
  37. u8 * const end = ptr + len;
  38. struct kpp_secret secret = {
  39. .type = CRYPTO_KPP_SECRET_TYPE_DH,
  40. .len = len
  41. };
  42. if (unlikely(!len))
  43. return -EINVAL;
  44. ptr = dh_pack_data(ptr, end, &secret, sizeof(secret));
  45. ptr = dh_pack_data(ptr, end, &params->key_size,
  46. sizeof(params->key_size));
  47. ptr = dh_pack_data(ptr, end, &params->p_size, sizeof(params->p_size));
  48. ptr = dh_pack_data(ptr, end, &params->q_size, sizeof(params->q_size));
  49. ptr = dh_pack_data(ptr, end, &params->g_size, sizeof(params->g_size));
  50. ptr = dh_pack_data(ptr, end, params->key, params->key_size);
  51. ptr = dh_pack_data(ptr, end, params->p, params->p_size);
  52. ptr = dh_pack_data(ptr, end, params->q, params->q_size);
  53. ptr = dh_pack_data(ptr, end, params->g, params->g_size);
  54. if (ptr != end)
  55. return -EINVAL;
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(crypto_dh_encode_key);
  59. int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
  60. {
  61. const u8 *ptr = buf;
  62. struct kpp_secret secret;
  63. if (unlikely(!buf || len < DH_KPP_SECRET_MIN_SIZE))
  64. return -EINVAL;
  65. ptr = dh_unpack_data(&secret, ptr, sizeof(secret));
  66. if (secret.type != CRYPTO_KPP_SECRET_TYPE_DH)
  67. return -EINVAL;
  68. ptr = dh_unpack_data(&params->key_size, ptr, sizeof(params->key_size));
  69. ptr = dh_unpack_data(&params->p_size, ptr, sizeof(params->p_size));
  70. ptr = dh_unpack_data(&params->q_size, ptr, sizeof(params->q_size));
  71. ptr = dh_unpack_data(&params->g_size, ptr, sizeof(params->g_size));
  72. if (secret.len != crypto_dh_key_len(params))
  73. return -EINVAL;
  74. /*
  75. * Don't permit the buffer for 'key' or 'g' to be larger than 'p', since
  76. * some drivers assume otherwise.
  77. */
  78. if (params->key_size > params->p_size ||
  79. params->g_size > params->p_size || params->q_size > params->p_size)
  80. return -EINVAL;
  81. /* Don't allocate memory. Set pointers to data within
  82. * the given buffer
  83. */
  84. params->key = (void *)ptr;
  85. params->p = (void *)(ptr + params->key_size);
  86. params->q = (void *)(ptr + params->key_size + params->p_size);
  87. params->g = (void *)(ptr + params->key_size + params->p_size +
  88. params->q_size);
  89. /*
  90. * Don't permit 'p' to be 0. It's not a prime number, and it's subject
  91. * to corner cases such as 'mod 0' being undefined or
  92. * crypto_kpp_maxsize() returning 0.
  93. */
  94. if (memchr_inv(params->p, 0, params->p_size) == NULL)
  95. return -EINVAL;
  96. /* It is permissible to not provide Q. */
  97. if (params->q_size == 0)
  98. params->q = NULL;
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(crypto_dh_decode_key);