cryptocb.h
1 /* cryptocb.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 3 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, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef _WOLF_CRYPTO_CB_H_
22 #define _WOLF_CRYPTO_CB_H_
23 
25 
26 #ifdef __cplusplus
27  extern "C" {
28 #endif
29 
30 /* Defines the Crypto Callback interface version, for compatibility */
31 /* Increment this when Crypto Callback interface changes are made */
32 #define CRYPTO_CB_VER 2
33 
34 
35 #ifdef WOLF_CRYPTO_CB
36 
37 #ifndef NO_RSA
38  #include <wolfssl/wolfcrypt/rsa.h>
39 #endif
40 #ifdef HAVE_ECC
41  #include <wolfssl/wolfcrypt/ecc.h>
42 #endif
43 #ifndef NO_AES
44  #include <wolfssl/wolfcrypt/aes.h>
45 #endif
46 #ifndef NO_SHA
47  #include <wolfssl/wolfcrypt/sha.h>
48 #endif
49 #ifndef NO_SHA256
51 #endif
52 #ifndef NO_HMAC
53  #include <wolfssl/wolfcrypt/hmac.h>
54 #endif
55 #ifndef WC_NO_RNG
57 #endif
58 #ifndef NO_DES3
59  #include <wolfssl/wolfcrypt/des3.h>
60 #endif
61 
62 
63 /* Crypto Information Structure for callbacks */
64 typedef struct wc_CryptoInfo {
65  int algo_type; /* enum wc_AlgoType */
66 #if !defined(NO_RSA) || defined(HAVE_ECC)
67  struct {
68  int type; /* enum wc_PkType */
69  union {
70  #ifndef NO_RSA
71  struct {
72  const byte* in;
73  word32 inLen;
74  byte* out;
75  word32* outLen;
76  int type;
77  RsaKey* key;
78  WC_RNG* rng;
79  } rsa;
80  #ifdef WOLFSSL_KEY_GEN
81  struct {
82  RsaKey* key;
83  int size;
84  long e;
85  WC_RNG* rng;
86  } rsakg;
87  #endif
88  #endif
89  #ifdef HAVE_ECC
90  struct {
91  WC_RNG* rng;
92  int size;
93  ecc_key* key;
94  int curveId;
95  } eckg;
96  struct {
97  ecc_key* private_key;
98  ecc_key* public_key;
99  byte* out;
100  word32* outlen;
101  } ecdh;
102  struct {
103  const byte* in;
104  word32 inlen;
105  byte* out;
106  word32* outlen;
107  WC_RNG* rng;
108  ecc_key* key;
109  } eccsign;
110  struct {
111  const byte* sig;
112  word32 siglen;
113  const byte* hash;
114  word32 hashlen;
115  int* res;
116  ecc_key* key;
117  } eccverify;
118  #endif
119  };
120  } pk;
121 #endif /* !NO_RSA || HAVE_ECC */
122 #if !defined(NO_AES) || !defined(NO_DES3)
123  struct {
124  int type; /* enum wc_CipherType */
125  int enc;
126  union {
127  #ifdef HAVE_AESGCM
128  struct {
129  Aes* aes;
130  byte* out;
131  const byte* in;
132  word32 sz;
133  const byte* iv;
134  word32 ivSz;
135  byte* authTag;
136  word32 authTagSz;
137  const byte* authIn;
138  word32 authInSz;
139  } aesgcm_enc;
140  struct {
141  Aes* aes;
142  byte* out;
143  const byte* in;
144  word32 sz;
145  const byte* iv;
146  word32 ivSz;
147  const byte* authTag;
148  word32 authTagSz;
149  const byte* authIn;
150  word32 authInSz;
151  } aesgcm_dec;
152  #endif /* HAVE_AESGCM */
153  #ifdef HAVE_AES_CBC
154  struct {
155  Aes* aes;
156  byte* out;
157  const byte* in;
158  word32 sz;
159  } aescbc;
160  #endif /* HAVE_AES_CBC */
161  #ifndef NO_DES3
162  struct {
163  Des3* des;
164  byte* out;
165  const byte* in;
166  word32 sz;
167  } des3;
168  #endif
169  };
170  } cipher;
171 #endif /* !NO_AES || !NO_DES3 */
172 #if !defined(NO_SHA) || !defined(NO_SHA256)
173  struct {
174  int type; /* enum wc_HashType */
175  const byte* in;
176  word32 inSz;
177  byte* digest;
178  union {
179  #ifndef NO_SHA
180  wc_Sha* sha1;
181  #endif
182  #ifndef NO_SHA256
183  wc_Sha256* sha256;
184  #endif
185  };
186  } hash;
187 #endif /* !NO_SHA || !NO_SHA256 */
188 #ifndef NO_HMAC
189  struct {
190  int macType; /* enum wc_HashType */
191  const byte* in;
192  word32 inSz;
193  byte* digest;
194  Hmac* hmac;
195  } hmac;
196 #endif
197 #ifndef WC_NO_RNG
198  struct {
199  WC_RNG* rng;
200  byte* out;
201  word32 sz;
202  } rng;
203  struct {
204  OS_Seed* os;
205  byte* seed;
206  word32 sz;
207  } seed;
208 #endif
209 } wc_CryptoInfo;
210 
211 
212 typedef int (*CryptoDevCallbackFunc)(int devId, wc_CryptoInfo* info, void* ctx);
213 
214 WOLFSSL_LOCAL void wc_CryptoCb_Init(void);
215 
216 WOLFSSL_API int wc_CryptoCb_RegisterDevice(int devId, CryptoDevCallbackFunc cb, void* ctx);
217 WOLFSSL_API void wc_CryptoCb_UnRegisterDevice(int devId);
218 
219 /* old function names */
220 #define wc_CryptoDev_RegisterDevice wc_CryptoCb_RegisterDevice
221 #define wc_CryptoDev_UnRegisterDevice wc_CryptoCb_UnRegisterDevice
222 
223 
224 #ifndef NO_RSA
225 WOLFSSL_LOCAL int wc_CryptoCb_Rsa(const byte* in, word32 inLen, byte* out,
226  word32* outLen, int type, RsaKey* key, WC_RNG* rng);
227 
228 #ifdef WOLFSSL_KEY_GEN
229 WOLFSSL_LOCAL int wc_CryptoCb_MakeRsaKey(RsaKey* key, int size, long e,
230  WC_RNG* rng);
231 #endif /* WOLFSSL_KEY_GEN */
232 #endif /* !NO_RSA */
233 
234 #ifdef HAVE_ECC
235 WOLFSSL_LOCAL int wc_CryptoCb_MakeEccKey(WC_RNG* rng, int keySize,
236  ecc_key* key, int curveId);
237 
238 WOLFSSL_LOCAL int wc_CryptoCb_Ecdh(ecc_key* private_key, ecc_key* public_key,
239  byte* out, word32* outlen);
240 
241 WOLFSSL_LOCAL int wc_CryptoCb_EccSign(const byte* in, word32 inlen, byte* out,
242  word32 *outlen, WC_RNG* rng, ecc_key* key);
243 
244 WOLFSSL_LOCAL int wc_CryptoCb_EccVerify(const byte* sig, word32 siglen,
245  const byte* hash, word32 hashlen, int* res, ecc_key* key);
246 #endif /* HAVE_ECC */
247 
248 #ifndef NO_AES
249 #ifdef HAVE_AESGCM
250 WOLFSSL_LOCAL int wc_CryptoCb_AesGcmEncrypt(Aes* aes, byte* out,
251  const byte* in, word32 sz, const byte* iv, word32 ivSz,
252  byte* authTag, word32 authTagSz, const byte* authIn, word32 authInSz);
253 
254 WOLFSSL_LOCAL int wc_CryptoCb_AesGcmDecrypt(Aes* aes, byte* out,
255  const byte* in, word32 sz, const byte* iv, word32 ivSz,
256  const byte* authTag, word32 authTagSz,
257  const byte* authIn, word32 authInSz);
258 #endif /* HAVE_AESGCM */
259 #ifdef HAVE_AES_CBC
260 WOLFSSL_LOCAL int wc_CryptoCb_AesCbcEncrypt(Aes* aes, byte* out,
261  const byte* in, word32 sz);
262 WOLFSSL_LOCAL int wc_CryptoCb_AesCbcDecrypt(Aes* aes, byte* out,
263  const byte* in, word32 sz);
264 #endif /* HAVE_AES_CBC */
265 #endif /* !NO_AES */
266 
267 #ifndef NO_DES3
268 WOLFSSL_LOCAL int wc_CryptoCb_Des3Encrypt(Des3* des3, byte* out,
269  const byte* in, word32 sz);
270 WOLFSSL_LOCAL int wc_CryptoCb_Des3Decrypt(Des3* des3, byte* out,
271  const byte* in, word32 sz);
272 #endif /* !NO_DES3 */
273 
274 #ifndef NO_SHA
275 WOLFSSL_LOCAL int wc_CryptoCb_ShaHash(wc_Sha* sha, const byte* in,
276  word32 inSz, byte* digest);
277 #endif /* !NO_SHA */
278 
279 #ifndef NO_SHA256
280 WOLFSSL_LOCAL int wc_CryptoCb_Sha256Hash(wc_Sha256* sha256, const byte* in,
281  word32 inSz, byte* digest);
282 #endif /* !NO_SHA256 */
283 #ifndef NO_HMAC
284 WOLFSSL_LOCAL int wc_CryptoCb_Hmac(Hmac* hmac, int macType, const byte* in,
285  word32 inSz, byte* digest);
286 #endif /* !NO_HMAC */
287 
288 #ifndef WC_NO_RNG
289 WOLFSSL_LOCAL int wc_CryptoCb_RandomBlock(WC_RNG* rng, byte* out, word32 sz);
290 WOLFSSL_LOCAL int wc_CryptoCb_RandomSeed(OS_Seed* os, byte* seed, word32 sz);
291 #endif
292 
293 #endif /* WOLF_CRYPTO_CB */
294 
295 #ifdef __cplusplus
296  } /* extern "C" */
297 #endif
298 
299 #endif /* _WOLF_CRYPTO_CB_H_ */
Definition: random.h:135
Definition: sha256.h:132
Definition: aes.h:149
Definition: hmac.h:137
Definition: ecc.h:357
Definition: random.h:153
Definition: wolfcaam_sha.h:68
Definition: des3.h:98
Definition: user_rsa.h:60