dh.h
Go to the documentation of this file.
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 
26 #ifndef WOLF_CRYPT_DH_H
27 #define WOLF_CRYPT_DH_H
28 
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>
40 
41 #ifdef __cplusplus
42  extern "C" {
43 #endif
44 
45 #ifdef WOLFSSL_ASYNC_CRYPT
46  #include <wolfssl/wolfcrypt/async.h>
47 #endif
48 typedef struct DhParams {
49  #ifdef HAVE_FFDHE_Q
50  const byte* q;
51  word32 q_len;
52  #endif /* HAVE_FFDHE_Q */
53  const byte* p;
54  word32 p_len;
55  const byte* g;
56  word32 g_len;
57 } DhParams;
58 
59 /* Diffie-Hellman Key */
60 struct DhKey {
61  mp_int p, g, q; /* group parameters */
62 #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
63  mp_int pub;
64  mp_int priv;
65 #endif
66  void* heap;
67 #ifdef WOLFSSL_ASYNC_CRYPT
68  WC_ASYNC_DEV asyncDev;
69 #endif
70 };
71 
72 #ifndef WC_DH_TYPE_DEFINED
73  typedef struct DhKey DhKey;
74  #define WC_DH_TYPE_DEFINED
75 #endif
76 
77 #ifdef HAVE_FFDHE_2048
78 WOLFSSL_API const DhParams* wc_Dh_ffdhe2048_Get(void);
79 #endif
80 #ifdef HAVE_FFDHE_3072
81 WOLFSSL_API const DhParams* wc_Dh_ffdhe3072_Get(void);
82 #endif
83 #ifdef HAVE_FFDHE_4096
84 WOLFSSL_API const DhParams* wc_Dh_ffdhe4096_Get(void);
85 #endif
86 #ifdef HAVE_FFDHE_6144
87 WOLFSSL_API const DhParams* wc_Dh_ffdhe6144_Get(void);
88 #endif
89 #ifdef HAVE_FFDHE_8192
90 WOLFSSL_API const DhParams* wc_Dh_ffdhe8192_Get(void);
91 #endif
92 
93 WOLFSSL_API int wc_InitDhKey(DhKey* key);
94 WOLFSSL_API int wc_InitDhKey_ex(DhKey* key, void* heap, int devId);
95 WOLFSSL_API int wc_FreeDhKey(DhKey* key);
96 
97 WOLFSSL_API int wc_DhGenerateKeyPair(DhKey* key, WC_RNG* rng, byte* priv,
98  word32* privSz, byte* pub, word32* pubSz);
99 WOLFSSL_API int wc_DhAgree(DhKey* key, byte* agree, word32* agreeSz,
100  const byte* priv, word32 privSz, const byte* otherPub,
101  word32 pubSz);
102 
103 WOLFSSL_API int wc_DhKeyDecode(const byte* input, word32* inOutIdx, DhKey* key,
104  word32);
105 WOLFSSL_API int wc_DhSetKey(DhKey* key, const byte* p, word32 pSz, const byte* g,
106  word32 gSz);
107 WOLFSSL_API int wc_DhSetKey_ex(DhKey* key, const byte* p, word32 pSz,
108  const byte* g, word32 gSz, const byte* q, word32 qSz);
109 #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
110 WOLFSSL_LOCAL int wc_DhSetFullKeys(DhKey* key,const byte* priv_key,word32 privSz,
111  const byte* pub_key, word32 pubSz);
112 #endif
113 WOLFSSL_API int wc_DhSetCheckKey(DhKey* key, const byte* p, word32 pSz,
114  const byte* g, word32 gSz, const byte* q, word32 qSz,
115  int trusted, WC_RNG* rng);
116 WOLFSSL_API int wc_DhParamsLoad(const byte* input, word32 inSz, byte* p,
117  word32* pInOutSz, byte* g, word32* gInOutSz);
118 WOLFSSL_API int wc_DhCheckPubKey(DhKey* key, const byte* pub, word32 pubSz);
119 WOLFSSL_API int wc_DhCheckPubKey_ex(DhKey* key, const byte* pub, word32 pubSz,
120  const byte* prime, word32 primeSz);
121 WOLFSSL_API int wc_DhCheckPubValue(const byte* prime, word32 primeSz,
122  const byte* pub, word32 pubSz);
123 WOLFSSL_API int wc_DhCheckPrivKey(DhKey* key, const byte* priv, word32 pubSz);
124 WOLFSSL_API int wc_DhCheckPrivKey_ex(DhKey* key, const byte* priv, word32 pubSz,
125  const byte* prime, word32 primeSz);
126 WOLFSSL_API int wc_DhCheckKeyPair(DhKey* key, const byte* pub, word32 pubSz,
127  const byte* priv, word32 privSz);
128 WOLFSSL_API int wc_DhGenerateParams(WC_RNG *rng, int modSz, DhKey *dh);
129 WOLFSSL_API int wc_DhExportParamsRaw(DhKey* dh, byte* p, word32* pSz,
130  byte* q, word32* qSz, byte* g, word32* gSz);
131 
132 
133 #ifdef __cplusplus
134  } /* extern "C" */
135 #endif
136 
137 #endif /* NO_DH */
138 #endif /* WOLF_CRYPT_DH_H */
139 
WOLFSSL_API int wc_DhKeyDecode(const byte *input, word32 *inOutIdx, DhKey *key, word32)
This function decodes a Diffie-Hellman key from the given input buffer containing the key in DER form...
Definition: asn.c:4393
WOLFSSL_API int wc_DhAgree(DhKey *key, byte *agree, word32 *agreeSz, const byte *priv, word32 privSz, const byte *otherPub, word32 pubSz)
This function generates an agreed upon secret key based on a local private key and a received public ...
Definition: dh.c:2045
WOLFSSL_API const DhParams * wc_Dh_ffdhe8192_Get(void)
This function returns ... and requires that HAVE_FFDHE_8192 be defined.
Definition: dh.c:911
WOLFSSL_API int wc_DhGenerateKeyPair(DhKey *key, WC_RNG *rng, byte *priv, word32 *privSz, byte *pub, word32 *pubSz)
This function generates a public/private key pair based on the Diffie-Hellman public parameters...
Definition: dh.c:1814
WOLFSSL_API int wc_DhSetKey(DhKey *key, const byte *p, word32 pSz, const byte *g, word32 gSz)
This function sets the key for a DhKey structure using the input private key parameters. Unlike wc_DhKeyDecode, this function does not require that the input key be formatted in DER format, and instead simply accepts the parsed input parameters p (prime) and g (base).
Definition: dh.c:2234
WOLFSSL_API const DhParams * wc_Dh_ffdhe6144_Get(void)
This function returns ... and requires that HAVE_FFDHE_6144 be defined.
Definition: dh.c:633
WOLFSSL_API void wc_FreeDhKey(DhKey *key)
This function frees a Diffie-Hellman key after it has been used to negotiate a secure secret key with...
Definition: dh.c:957
WOLFSSL_API int wc_DhCheckKeyPair(DhKey *key, const byte *pub, word32 pubSz, const byte *priv, word32 privSz)
Checks DH keys for pair-wise consistency per process in SP 800-56Ar3, section 5.6.2.1.4, method (b) for FFC.
Definition: dh.c:1704
WOLFSSL_API const DhParams * wc_Dh_ffdhe4096_Get(void)
This function returns ... and requires that HAVE_FFDHE_4096 be defined.
Definition: dh.c:419
Definition: integer.h:200
WOLFSSL_API int wc_DhCheckPubValue(const byte *prime, word32 primeSz, const byte *pub, word32 pubSz)
Definition: dh.c:1553
Definition: dh.h:60
WOLFSSL_API int wc_DhParamsLoad(const byte *input, word32 inSz, byte *p, word32 *pInOutSz, byte *g, word32 *gInOutSz)
This function loads the Diffie-Hellman parameters, p (prime) and g (base) out of the given input buff...
Definition: asn.c:4472
WOLFSSL_API int wc_InitDhKey(DhKey *key)
This function initializes a Diffie-Hellman key for use in negotiating a secure secret key with the Di...
Definition: dh.c:951
Definition: random.h:153
WOLFSSL_API const DhParams * wc_Dh_ffdhe3072_Get(void)
This function returns ... and requires that HAVE_FFDHE_3072 be defined.
Definition: dh.c:269
WOLFSSL_API int wc_DhCheckPrivKey(DhKey *key, const byte *priv, word32 pubSz)
Check DH private key for invalid numbers.
Definition: dh.c:1687
WOLFSSL_API const DhParams * wc_Dh_ffdhe2048_Get(void)
This function returns ... and requires that HAVE_FFDHE_2048 be defined.
Definition: dh.c:151