blob: 1f53f31e8e057015a6a9fa3bea200006685ed9e5 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* dh.h
2 *
3 * Copyright (C) 2006-2021 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/*!
23 \file wolfssl/wolfcrypt/dh.h
24*/
25
26#ifndef WOLF_CRYPT_DH_H
27#define WOLF_CRYPT_DH_H
28
29#include <wolfssl/wolfcrypt/types.h>
30
31#ifndef NO_DH
32
33#if defined(HAVE_FIPS) && \
34 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
35 #include <wolfssl/wolfcrypt/fips.h>
36#endif /* HAVE_FIPS_VERSION >= 2 */
37
38#include <wolfssl/wolfcrypt/integer.h>
39#include <wolfssl/wolfcrypt/random.h>
40
41#ifdef __cplusplus
42 extern "C" {
43#endif
44
45#ifdef WOLFSSL_ASYNC_CRYPT
46 #include <wolfssl/wolfcrypt/async.h>
47#endif
48
49typedef struct DhParams {
50#ifdef HAVE_FFDHE_Q
51 const byte* q;
52 word32 q_len;
53#endif /* HAVE_FFDHE_Q */
54 const byte* p;
55 word32 p_len;
56 const byte* g;
57 word32 g_len;
58} DhParams;
59
60/* Diffie-Hellman Key */
61struct DhKey {
62 mp_int p, g, q; /* group parameters */
63#ifdef WOLFSSL_DH_EXTRA
64 mp_int pub;
65 mp_int priv;
66#endif
67 void* heap;
68#ifdef WOLFSSL_ASYNC_CRYPT
69 WC_ASYNC_DEV asyncDev;
70#endif
71};
72
73#ifndef WC_DH_TYPE_DEFINED
74 typedef struct DhKey DhKey;
75 #define WC_DH_TYPE_DEFINED
76#endif
77
78#ifdef HAVE_FFDHE_2048
79WOLFSSL_API const DhParams* wc_Dh_ffdhe2048_Get(void);
80#endif
81#ifdef HAVE_FFDHE_3072
82WOLFSSL_API const DhParams* wc_Dh_ffdhe3072_Get(void);
83#endif
84#ifdef HAVE_FFDHE_4096
85WOLFSSL_API const DhParams* wc_Dh_ffdhe4096_Get(void);
86#endif
87#ifdef HAVE_FFDHE_6144
88WOLFSSL_API const DhParams* wc_Dh_ffdhe6144_Get(void);
89#endif
90#ifdef HAVE_FFDHE_8192
91WOLFSSL_API const DhParams* wc_Dh_ffdhe8192_Get(void);
92#endif
93
94WOLFSSL_API int wc_InitDhKey(DhKey* key);
95WOLFSSL_API int wc_InitDhKey_ex(DhKey* key, void* heap, int devId);
96WOLFSSL_API int wc_FreeDhKey(DhKey* key);
97
98WOLFSSL_API int wc_DhGenerateKeyPair(DhKey* key, WC_RNG* rng, byte* priv,
99 word32* privSz, byte* pub, word32* pubSz);
100WOLFSSL_API int wc_DhAgree(DhKey* key, byte* agree, word32* agreeSz,
101 const byte* priv, word32 privSz, const byte* otherPub,
102 word32 pubSz);
103
104WOLFSSL_API int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key,
105 word32); /* wc_DhKeyDecode is in asn.c */
106
107WOLFSSL_API int wc_DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g,
108 word32 gSz);
109WOLFSSL_API int wc_DhSetKey_ex(DhKey* key, const byte* p, word32 pSz,
110 const byte* g, word32 gSz, const byte* q, word32 qSz);
111
112#ifdef WOLFSSL_DH_EXTRA
113WOLFSSL_API int wc_DhPublicKeyDecode(const byte* input, word32* inOutIdx,
114 DhKey* key, word32 inSz);
115WOLFSSL_API int wc_DhImportKeyPair(DhKey* key, const byte* priv, word32 privSz,
116 const byte* pub, word32 pubSz);
117WOLFSSL_API int wc_DhExportKeyPair(DhKey* key, byte* priv, word32* pPrivSz,
118 byte* pub, word32* pPubSz);
119WOLFSSL_LOCAL int wc_DhKeyCopy(DhKey* src, DhKey* dst);
120#endif
121WOLFSSL_API int wc_DhSetCheckKey(DhKey* key, const byte* p, word32 pSz,
122 const byte* g, word32 gSz, const byte* q, word32 qSz,
123 int trusted, WC_RNG* rng);
124WOLFSSL_API int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p,
125 word32* pInOutSz, byte* g, word32* gInOutSz);
126WOLFSSL_API int wc_DhCheckPubKey(DhKey* key, const byte* pub, word32 pubSz);
127WOLFSSL_API int wc_DhCheckPubKey_ex(DhKey* key, const byte* pub, word32 pubSz,
128 const byte* prime, word32 primeSz);
129WOLFSSL_API int wc_DhCheckPubValue(const byte* prime, word32 primeSz,
130 const byte* pub, word32 pubSz);
131WOLFSSL_API int wc_DhCheckPrivKey(DhKey* key, const byte* priv, word32 pubSz);
132WOLFSSL_API int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 pubSz,
133 const byte* prime, word32 primeSz);
134WOLFSSL_API int wc_DhCheckKeyPair(DhKey* key, const byte* pub, word32 pubSz,
135 const byte* priv, word32 privSz);
136WOLFSSL_API int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh);
137WOLFSSL_API int wc_DhExportParamsRaw(DhKey* dh, byte* p, word32* pSz,
138 byte* q, word32* qSz, byte* g, word32* gSz);
139
140
141#ifdef __cplusplus
142 } /* extern "C" */
143#endif
144
145#endif /* NO_DH */
146#endif /* WOLF_CRYPT_DH_H */