dh.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /* dh.h
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. /*!
  22. \file wolfssl/wolfcrypt/dh.h
  23. */
  24. #ifndef WOLF_CRYPT_DH_H
  25. #define WOLF_CRYPT_DH_H
  26. #include <wolfssl/wolfcrypt/types.h>
  27. #ifndef NO_DH
  28. #if defined(HAVE_FIPS) && \
  29. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  30. #include <wolfssl/wolfcrypt/fips.h>
  31. #endif /* HAVE_FIPS_VERSION >= 2 */
  32. #include <wolfssl/wolfcrypt/wolfmath.h>
  33. #include <wolfssl/wolfcrypt/random.h>
  34. #ifdef WOLFSSL_KCAPI_DH
  35. #include <wolfssl/wolfcrypt/port/kcapi/kcapi_dh.h>
  36. #endif
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #ifdef WOLFSSL_ASYNC_CRYPT
  41. #include <wolfssl/wolfcrypt/async.h>
  42. #endif
  43. typedef struct DhParams {
  44. #ifdef HAVE_FFDHE_Q
  45. const byte* q;
  46. word32 q_len;
  47. #endif /* HAVE_FFDHE_Q */
  48. const byte* p;
  49. word32 p_len;
  50. const byte* g;
  51. word32 g_len;
  52. } DhParams;
  53. /* Diffie-Hellman Key */
  54. struct DhKey {
  55. mp_int p, g, q; /* group parameters */
  56. #ifdef WOLFSSL_DH_EXTRA
  57. mp_int pub;
  58. mp_int priv;
  59. #endif
  60. void* heap;
  61. #ifdef WOLFSSL_ASYNC_CRYPT
  62. WC_ASYNC_DEV asyncDev;
  63. #endif
  64. int trustedGroup;
  65. #ifdef WOLFSSL_KCAPI_DH
  66. struct kcapi_handle* handle;
  67. #endif
  68. };
  69. #ifndef WC_DH_TYPE_DEFINED
  70. typedef struct DhKey DhKey;
  71. #define WC_DH_TYPE_DEFINED
  72. #endif
  73. enum {
  74. WC_FFDHE_2048 = 256,
  75. WC_FFDHE_3072 = 257,
  76. WC_FFDHE_4096 = 258,
  77. WC_FFDHE_6144 = 259,
  78. WC_FFDHE_8192 = 260
  79. };
  80. /* DH Private Key size up to 8192 bit */
  81. #ifndef WC_DH_PRIV_MAX_SZ
  82. #define WC_DH_PRIV_MAX_SZ 52
  83. #endif
  84. #ifndef DH_MAX_SIZE
  85. #ifdef USE_FAST_MATH
  86. /* FP implementation support numbers up to FP_MAX_BITS / 2 bits. */
  87. #define DH_MAX_SIZE (FP_MAX_BITS / 2)
  88. #if defined(WOLFSSL_MYSQL_COMPATIBLE) && DH_MAX_SIZE < 8192
  89. #error "MySQL needs FP_MAX_BITS at least at 16384"
  90. #endif
  91. #elif defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  92. /* SP implementation supports numbers of SP_INT_BITS bits. */
  93. #define DH_MAX_SIZE (((SP_INT_BITS + 7) / 8) * 8)
  94. #if defined(WOLFSSL_MYSQL_COMPATIBLE) && DH_MAX_SIZE < 8192
  95. #error "MySQL needs SP_INT_BITS at least at 8192"
  96. #endif
  97. #else
  98. #ifdef WOLFSSL_MYSQL_COMPATIBLE
  99. /* Integer maths is dynamic but we only go up to 8192 bits. */
  100. #define DH_MAX_SIZE 8192
  101. #else
  102. /* Integer maths is dynamic but we only go up to 4096 bits. */
  103. #define DH_MAX_SIZE 4096
  104. #endif
  105. #endif
  106. #endif
  107. #ifdef HAVE_PUBLIC_FFDHE
  108. #ifdef HAVE_FFDHE_2048
  109. WOLFSSL_API const DhParams* wc_Dh_ffdhe2048_Get(void);
  110. #endif
  111. #ifdef HAVE_FFDHE_3072
  112. WOLFSSL_API const DhParams* wc_Dh_ffdhe3072_Get(void);
  113. #endif
  114. #ifdef HAVE_FFDHE_4096
  115. WOLFSSL_API const DhParams* wc_Dh_ffdhe4096_Get(void);
  116. #endif
  117. #ifdef HAVE_FFDHE_6144
  118. WOLFSSL_API const DhParams* wc_Dh_ffdhe6144_Get(void);
  119. #endif
  120. #ifdef HAVE_FFDHE_8192
  121. WOLFSSL_API const DhParams* wc_Dh_ffdhe8192_Get(void);
  122. #endif
  123. #endif
  124. WOLFSSL_API int wc_InitDhKey(DhKey* key);
  125. WOLFSSL_API int wc_InitDhKey_ex(DhKey* key, void* heap, int devId);
  126. WOLFSSL_API int wc_FreeDhKey(DhKey* key);
  127. WOLFSSL_API int wc_DhGenerateKeyPair(DhKey* key, WC_RNG* rng, byte* priv,
  128. word32* privSz, byte* pub, word32* pubSz);
  129. WOLFSSL_API int wc_DhAgree(DhKey* key, byte* agree, word32* agreeSz,
  130. const byte* priv, word32 privSz, const byte* otherPub,
  131. word32 pubSz);
  132. WOLFSSL_API int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key,
  133. word32 inSz); /* wc_DhKeyDecode is in asn.c */
  134. WOLFSSL_API int wc_DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g,
  135. word32 gSz);
  136. WOLFSSL_API int wc_DhSetKey_ex(DhKey* key, const byte* p, word32 pSz,
  137. const byte* g, word32 gSz, const byte* q, word32 qSz);
  138. WOLFSSL_API int wc_DhSetNamedKey(DhKey* key, int name);
  139. WOLFSSL_API int wc_DhGetNamedKeyParamSize(int name,
  140. word32* p, word32* g, word32* q);
  141. WOLFSSL_API word32 wc_DhGetNamedKeyMinSize(int name);
  142. WOLFSSL_API int wc_DhCmpNamedKey(int name, int noQ,
  143. const byte* p, word32 pSz,
  144. const byte* g, word32 gSz,
  145. const byte* q, word32 qSz);
  146. WOLFSSL_API int wc_DhCopyNamedKey(int name,
  147. byte* p, word32* pSz, byte* g, word32* gSz, byte* q, word32* qSz);
  148. #ifdef WOLFSSL_DH_EXTRA
  149. WOLFSSL_API int wc_DhImportKeyPair(DhKey* key, const byte* priv, word32 privSz,
  150. const byte* pub, word32 pubSz);
  151. WOLFSSL_API int wc_DhExportKeyPair(DhKey* key, byte* priv, word32* pPrivSz,
  152. byte* pub, word32* pPubSz);
  153. WOLFSSL_LOCAL int wc_DhKeyCopy(DhKey* src, DhKey* dst);
  154. #endif
  155. WOLFSSL_API int wc_DhSetCheckKey(DhKey* key, const byte* p, word32 pSz,
  156. const byte* g, word32 gSz, const byte* q, word32 qSz,
  157. int trusted, WC_RNG* rng);
  158. WOLFSSL_API int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p,
  159. word32* pInOutSz, byte* g, word32* gInOutSz);
  160. WOLFSSL_API int wc_DhCheckPubKey(DhKey* key, const byte* pub, word32 pubSz);
  161. WOLFSSL_API int wc_DhCheckPubKey_ex(DhKey* key, const byte* pub, word32 pubSz,
  162. const byte* prime, word32 primeSz);
  163. WOLFSSL_API int wc_DhCheckPubValue(const byte* prime, word32 primeSz,
  164. const byte* pub, word32 pubSz);
  165. WOLFSSL_API int wc_DhCheckPrivKey(DhKey* key, const byte* priv, word32 pubSz);
  166. WOLFSSL_API int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 pubSz,
  167. const byte* prime, word32 primeSz);
  168. WOLFSSL_API int wc_DhCheckKeyPair(DhKey* key, const byte* pub, word32 pubSz,
  169. const byte* priv, word32 privSz);
  170. WOLFSSL_API int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh);
  171. WOLFSSL_API int wc_DhExportParamsRaw(DhKey* dh, byte* p, word32* pSz,
  172. byte* q, word32* qSz, byte* g, word32* gSz);
  173. #ifdef __cplusplus
  174. } /* extern "C" */
  175. #endif
  176. #endif /* NO_DH */
  177. #endif /* WOLF_CRYPT_DH_H */