dh_helper.c 3.6 KB

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