dh_helper.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 Licence
  7. * as published by the Free Software Foundation; either version
  8. * 2 of the Licence, 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) + 3 * sizeof(int))
  17. static inline u8 *dh_pack_data(void *dst, const void *src, size_t size)
  18. {
  19. memcpy(dst, src, size);
  20. return dst + size;
  21. }
  22. static inline const u8 *dh_unpack_data(void *dst, const void *src, size_t size)
  23. {
  24. memcpy(dst, src, size);
  25. return src + size;
  26. }
  27. static inline int dh_data_size(const struct dh *p)
  28. {
  29. return p->key_size + p->p_size + p->g_size;
  30. }
  31. int crypto_dh_key_len(const struct dh *p)
  32. {
  33. return DH_KPP_SECRET_MIN_SIZE + dh_data_size(p);
  34. }
  35. EXPORT_SYMBOL_GPL(crypto_dh_key_len);
  36. int crypto_dh_encode_key(char *buf, unsigned int len, const struct dh *params)
  37. {
  38. u8 *ptr = buf;
  39. struct kpp_secret secret = {
  40. .type = CRYPTO_KPP_SECRET_TYPE_DH,
  41. .len = len
  42. };
  43. if (unlikely(!buf))
  44. return -EINVAL;
  45. if (len != crypto_dh_key_len(params))
  46. return -EINVAL;
  47. ptr = dh_pack_data(ptr, &secret, sizeof(secret));
  48. ptr = dh_pack_data(ptr, &params->key_size, sizeof(params->key_size));
  49. ptr = dh_pack_data(ptr, &params->p_size, sizeof(params->p_size));
  50. ptr = dh_pack_data(ptr, &params->g_size, sizeof(params->g_size));
  51. ptr = dh_pack_data(ptr, params->key, params->key_size);
  52. ptr = dh_pack_data(ptr, params->p, params->p_size);
  53. dh_pack_data(ptr, params->g, params->g_size);
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(crypto_dh_encode_key);
  57. int crypto_dh_decode_key(const char *buf, unsigned int len, struct dh *params)
  58. {
  59. const u8 *ptr = buf;
  60. struct kpp_secret secret;
  61. if (unlikely(!buf || len < DH_KPP_SECRET_MIN_SIZE))
  62. return -EINVAL;
  63. ptr = dh_unpack_data(&secret, ptr, sizeof(secret));
  64. if (secret.type != CRYPTO_KPP_SECRET_TYPE_DH)
  65. return -EINVAL;
  66. ptr = dh_unpack_data(&params->key_size, ptr, sizeof(params->key_size));
  67. ptr = dh_unpack_data(&params->p_size, ptr, sizeof(params->p_size));
  68. ptr = dh_unpack_data(&params->g_size, ptr, sizeof(params->g_size));
  69. if (secret.len != crypto_dh_key_len(params))
  70. return -EINVAL;
  71. /*
  72. * Don't permit the buffer for 'key' or 'g' to be larger than 'p', since
  73. * some drivers assume otherwise.
  74. */
  75. if (params->key_size > params->p_size ||
  76. params->g_size > params->p_size)
  77. return -EINVAL;
  78. /* Don't allocate memory. Set pointers to data within
  79. * the given buffer
  80. */
  81. params->key = (void *)ptr;
  82. params->p = (void *)(ptr + params->key_size);
  83. params->g = (void *)(ptr + params->key_size + params->p_size);
  84. /*
  85. * Don't permit 'p' to be 0. It's not a prime number, and it's subject
  86. * to corner cases such as 'mod 0' being undefined or
  87. * crypto_kpp_maxsize() returning 0.
  88. */
  89. if (memchr_inv(params->p, 0, params->p_size) == NULL)
  90. return -EINVAL;
  91. return 0;
  92. }
  93. EXPORT_SYMBOL_GPL(crypto_dh_decode_key);