1 | /* dh.h
|
---|
2 | *
|
---|
3 | * Copyright (C) 2006-2020 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 |
|
---|
49 | /* Optional support extended DH public / private keys */
|
---|
50 | #if !defined(WOLFSSL_DH_EXTRA) && (defined(WOLFSSL_QT) || \
|
---|
51 | defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
|
---|
52 | defined(WOLFSSL_STATIC_EPHEMERAL))
|
---|
53 | #define WOLFSSL_DH_EXTRA
|
---|
54 | #endif
|
---|
55 |
|
---|
56 | typedef struct DhParams {
|
---|
57 | #ifdef HAVE_FFDHE_Q
|
---|
58 | const byte* q;
|
---|
59 | word32 q_len;
|
---|
60 | #endif /* HAVE_FFDHE_Q */
|
---|
61 | const byte* p;
|
---|
62 | word32 p_len;
|
---|
63 | const byte* g;
|
---|
64 | word32 g_len;
|
---|
65 | } DhParams;
|
---|
66 |
|
---|
67 | /* Diffie-Hellman Key */
|
---|
68 | struct DhKey {
|
---|
69 | mp_int p, g, q; /* group parameters */
|
---|
70 | #ifdef WOLFSSL_DH_EXTRA
|
---|
71 | mp_int pub;
|
---|
72 | mp_int priv;
|
---|
73 | #endif
|
---|
74 | void* heap;
|
---|
75 | #ifdef WOLFSSL_ASYNC_CRYPT
|
---|
76 | WC_ASYNC_DEV asyncDev;
|
---|
77 | #endif
|
---|
78 | };
|
---|
79 |
|
---|
80 | #ifndef WC_DH_TYPE_DEFINED
|
---|
81 | typedef struct DhKey DhKey;
|
---|
82 | #define WC_DH_TYPE_DEFINED
|
---|
83 | #endif
|
---|
84 |
|
---|
85 | #ifdef HAVE_FFDHE_2048
|
---|
86 | WOLFSSL_API const DhParams* wc_Dh_ffdhe2048_Get(void);
|
---|
87 | #endif
|
---|
88 | #ifdef HAVE_FFDHE_3072
|
---|
89 | WOLFSSL_API const DhParams* wc_Dh_ffdhe3072_Get(void);
|
---|
90 | #endif
|
---|
91 | #ifdef HAVE_FFDHE_4096
|
---|
92 | WOLFSSL_API const DhParams* wc_Dh_ffdhe4096_Get(void);
|
---|
93 | #endif
|
---|
94 | #ifdef HAVE_FFDHE_6144
|
---|
95 | WOLFSSL_API const DhParams* wc_Dh_ffdhe6144_Get(void);
|
---|
96 | #endif
|
---|
97 | #ifdef HAVE_FFDHE_8192
|
---|
98 | WOLFSSL_API const DhParams* wc_Dh_ffdhe8192_Get(void);
|
---|
99 | #endif
|
---|
100 |
|
---|
101 | WOLFSSL_API int wc_InitDhKey(DhKey* key);
|
---|
102 | WOLFSSL_API int wc_InitDhKey_ex(DhKey* key, void* heap, int devId);
|
---|
103 | WOLFSSL_API int wc_FreeDhKey(DhKey* key);
|
---|
104 |
|
---|
105 | WOLFSSL_API int wc_DhGenerateKeyPair(DhKey* key, WC_RNG* rng, byte* priv,
|
---|
106 | word32* privSz, byte* pub, word32* pubSz);
|
---|
107 | WOLFSSL_API int wc_DhAgree(DhKey* key, byte* agree, word32* agreeSz,
|
---|
108 | const byte* priv, word32 privSz, const byte* otherPub,
|
---|
109 | word32 pubSz);
|
---|
110 |
|
---|
111 | WOLFSSL_API int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key,
|
---|
112 | word32); /* wc_DhKeyDecode is in asn.c */
|
---|
113 |
|
---|
114 | WOLFSSL_API int wc_DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g,
|
---|
115 | word32 gSz);
|
---|
116 | WOLFSSL_API int wc_DhSetKey_ex(DhKey* key, const byte* p, word32 pSz,
|
---|
117 | const byte* g, word32 gSz, const byte* q, word32 qSz);
|
---|
118 |
|
---|
119 | #ifdef WOLFSSL_DH_EXTRA
|
---|
120 | WOLFSSL_API int wc_DhImportKeyPair(DhKey* key, const byte* priv, word32 privSz,
|
---|
121 | const byte* pub, word32 pubSz);
|
---|
122 | WOLFSSL_API int wc_DhExportKeyPair(DhKey* key, byte* priv, word32* pPrivSz,
|
---|
123 | byte* pub, word32* pPubSz);
|
---|
124 | WOLFSSL_LOCAL int wc_DhKeyCopy(DhKey* src, DhKey* dst);
|
---|
125 | #endif
|
---|
126 | WOLFSSL_API int wc_DhSetCheckKey(DhKey* key, const byte* p, word32 pSz,
|
---|
127 | const byte* g, word32 gSz, const byte* q, word32 qSz,
|
---|
128 | int trusted, WC_RNG* rng);
|
---|
129 | WOLFSSL_API int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p,
|
---|
130 | word32* pInOutSz, byte* g, word32* gInOutSz);
|
---|
131 | WOLFSSL_API int wc_DhCheckPubKey(DhKey* key, const byte* pub, word32 pubSz);
|
---|
132 | WOLFSSL_API int wc_DhCheckPubKey_ex(DhKey* key, const byte* pub, word32 pubSz,
|
---|
133 | const byte* prime, word32 primeSz);
|
---|
134 | WOLFSSL_API int wc_DhCheckPubValue(const byte* prime, word32 primeSz,
|
---|
135 | const byte* pub, word32 pubSz);
|
---|
136 | WOLFSSL_API int wc_DhCheckPrivKey(DhKey* key, const byte* priv, word32 pubSz);
|
---|
137 | WOLFSSL_API int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 pubSz,
|
---|
138 | const byte* prime, word32 primeSz);
|
---|
139 | WOLFSSL_API int wc_DhCheckKeyPair(DhKey* key, const byte* pub, word32 pubSz,
|
---|
140 | const byte* priv, word32 privSz);
|
---|
141 | WOLFSSL_API int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh);
|
---|
142 | WOLFSSL_API int wc_DhExportParamsRaw(DhKey* dh, byte* p, word32* pSz,
|
---|
143 | byte* q, word32* qSz, byte* g, word32* gSz);
|
---|
144 |
|
---|
145 |
|
---|
146 | #ifdef __cplusplus
|
---|
147 | } /* extern "C" */
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | #endif /* NO_DH */
|
---|
151 | #endif /* WOLF_CRYPT_DH_H */
|
---|