SharkSSL™ Embedded SSL/TLS Stack
SharkSslCrypto.h
1/*
2 * ____ _________ __ _
3 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____
4 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/
5 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__
6 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/
7 * /____/
8 *
9 * SharkSSL Embedded SSL/TLS Stack
10 ****************************************************************************
11 * PROGRAM MODULE
12 *
13 * $Id: SharkSslCrypto.h 5149 2022-05-14 21:56:07Z gianluca $
14 *
15 * COPYRIGHT: Real Time Logic LLC, 2010 - 2022
16 *
17 * This software is copyrighted by and is the sole property of Real
18 * Time Logic LLC. All rights, title, ownership, or other interests in
19 * the software remain the property of Real Time Logic LLC. This
20 * software may only be used in accordance with the terms and
21 * conditions stipulated in the corresponding license agreement under
22 * which the software has been supplied. Any unauthorized use,
23 * duplication, transmission, distribution, or disclosure of this
24 * software is expressly forbidden.
25 *
26 * This Copyright notice may not be removed or modified without prior
27 * written consent of Real Time Logic LLC.
28 *
29 * Real Time Logic LLC. reserves the right to modify this software
30 * without notice.
31 *
32 * http://www.realtimelogic.com
33 * http://www.sharkssl.com
34 ****************************************************************************
35 *
36 */
37#ifndef _SharkSslCrypto_h
38#define _SharkSslCrypto_h
39
40#define SHARKSSL_LIB 1
41#include "SharkSSL.h"
42
43
44#define SHARKSSL_MD5_HASH_LEN 16
45#define SHARKSSL_SHA1_HASH_LEN 20
46#define SHARKSSL_SHA256_HASH_LEN 32
47#define SHARKSSL_SHA384_HASH_LEN 48
48#define SHARKSSL_SHA512_HASH_LEN 64
49#define SHARKSSL_POLY1305_HASH_LEN 16
50
51#define SHARKSSL_MD5_BLOCK_LEN 64
52#define SHARKSSL_SHA1_BLOCK_LEN 64
53#define SHARKSSL_SHA256_BLOCK_LEN 64
54#define SHARKSSL_SHA384_BLOCK_LEN 128
55#define SHARKSSL_SHA512_BLOCK_LEN 128
56
59#define SHARKSSL_HASHID_MD5 0x01
60#define SHARKSSL_HASHID_SHA1 0x02
61#define SHARKSSL_HASHID_SHA256 0x04
62#define SHARKSSL_HASHID_SHA384 0x05
63#define SHARKSSL_HASHID_SHA512 0x06
64
65
66#if SHARKSSL_USE_MD5
70typedef struct SharkSslMd5Ctx
71{
72 U32 total[2];
73 U32 state[4];
74 U8 buffer[64];
75} SharkSslMd5Ctx;
76#endif
77
78
79#if SHARKSSL_USE_SHA1
83typedef struct SharkSslSha1Ctx
84{
85 U32 total[2];
86 U32 state[5];
87 U8 buffer[64];
88} SharkSslSha1Ctx;
89#endif
90
91
92#if SHARKSSL_USE_SHA_256
96typedef struct SharkSslSha256Ctx
97{
98 U32 total[2];
99 U32 state[8];
100 U8 buffer[64];
102#endif
103
104
105#if (SHARKSSL_USE_SHA_384 || SHARKSSL_USE_SHA_512)
109typedef struct SharkSslSha384Ctx
110{
111 U32 total[4];
112 U64 state[8];
113 U8 buffer[128];
115#endif
116
117
118#if SHARKSSL_USE_SHA_512
122typedef struct SharkSslSha384Ctx SharkSslSha512Ctx;
123#endif
124
125
126#if (SHARKSSL_USE_SHA_512 || SHARKSSL_USE_SHA_384 || SHARKSSL_USE_SHA_256 || SHARKSSL_USE_SHA1 || SHARKSSL_USE_MD5)
130typedef struct SharkSslHMACCtx
131{
132 union
133 {
134 #if SHARKSSL_USE_MD5
135 SharkSslMd5Ctx md5Ctx;
136 #endif
137 #if SHARKSSL_USE_SHA1
138 SharkSslSha1Ctx sha1Ctx;
139 #endif
140 #if SHARKSSL_USE_SHA_256
141 SharkSslSha256Ctx sha256Ctx;
142 #endif
143 #if SHARKSSL_USE_SHA_384
144 SharkSslSha384Ctx sha384Ctx;
145 #endif
146 #if SHARKSSL_USE_SHA_512
147 SharkSslSha512Ctx sha512Ctx;
148 #endif
149 } hashCtx;
150
151 /* descending block length */
152 #if SHARKSSL_USE_SHA_512
153 U8 key[SHARKSSL_SHA512_BLOCK_LEN];
154 #elif SHARKSSL_USE_SHA_384
155 U8 key[SHARKSSL_SHA384_BLOCK_LEN];
156 #elif SHARKSSL_USE_SHA_256
157 U8 key[SHARKSSL_SHA256_BLOCK_LEN];
158 #elif SHARKSSL_USE_SHA1
159 U8 key[SHARKSSL_SHA1_BLOCK_LEN];
160 #elif SHARKSSL_USE_MD5
161 U8 key[SHARKSSL_MD5_BLOCK_LEN];
162 #endif
163
164 U8 hashID;
166#endif
167
168
169#if SHARKSSL_USE_POLY1305
174{
175 U32 r[5];
176 U32 key[4];
177 U32 nonce[4];
178 U8 buffer[16];
179 U8 flag, blen;
181#endif
182
183
184#if SHARKSSL_USE_CHACHA20
188typedef struct
189{
190 U32 state[16];
192#endif
193
194
195#if (SHARKSSL_USE_AES_256 || SHARKSSL_USE_AES_192 || SHARKSSL_USE_AES_128)
199typedef struct SharkSslAesCtx
200{
201 #if (SHARKSSL_USE_AES_256 || SHARKSSL_NOPACK)
202 U32 key[60];
203 #elif SHARKSSL_USE_AES_192
204 U32 key[52];
205 #else
206 U32 key[44];
207 #endif
208 U16 nr;
210
211typedef enum
212{
213 SharkSslAesCtx_Decrypt,
214 SharkSslAesCtx_Encrypt
215} SharkSslAesCtx_Type;
216
217#if SHARKSSL_ENABLE_AES_GCM
221typedef struct SharkSslAesGcmCtx
222{
223 SharkSslAesCtx super;
224 U8 M0[16][16];
226#endif
227
228#if SHARKSSL_ENABLE_AES_CCM
232typedef struct SharkSslAesCcmCtx
233{
234 SharkSslAesCtx super;
235 U8 tagLen;
236} SharkSslAesCcmCtx;
237#endif
238#endif /* SHARKSSL_USE_AES_256 || SHARKSSL_USE_AES_192 || SHARKSSL_USE_AES_128 */
239
240
241#ifdef __cplusplus
242extern "C" {
243#endif
244
245/* SharkSslCrypto.c */
246SHARKSSL_API int sharkssl_entropy(U32);
247SHARKSSL_API int sharkssl_rng(U8*, U16);
248SHARKSSL_API int sharkssl_kmemcmp(const void *a, const void *b, U32 n);
249
250
251#if SHARKSSL_USE_MD5
257SHARKSSL_API void SharkSslMd5Ctx_constructor(SharkSslMd5Ctx* ctx);
258
262SHARKSSL_API void SharkSslMd5Ctx_append(SharkSslMd5Ctx* ctx, const U8* data, U32 len);
263
267SHARKSSL_API void SharkSslMd5Ctx_finish(SharkSslMd5Ctx* ctx, U8 digest[SHARKSSL_MD5_HASH_LEN]);
268
272SHARKSSL_API int sharkssl_md5(const U8*, U16, U8*);
273#endif
274
275#if SHARKSSL_USE_SHA1
281SHARKSSL_API void SharkSslSha1Ctx_constructor(SharkSslSha1Ctx* ctx);
282
286SHARKSSL_API void SharkSslSha1Ctx_append(SharkSslSha1Ctx* ctx, const U8* data, U32 len);
287
291SHARKSSL_API void SharkSslSha1Ctx_finish(SharkSslSha1Ctx*, U8 digest[SHARKSSL_SHA1_HASH_LEN]);
292
296SHARKSSL_API int sharkssl_sha1(const U8*, U16, U8*);
297#endif
298
299#if SHARKSSL_USE_SHA_256
306
310SHARKSSL_API void SharkSslSha256Ctx_append(SharkSslSha256Ctx*, const U8* data, U32 len);
311
315SHARKSSL_API void SharkSslSha256Ctx_finish(SharkSslSha256Ctx*, U8 digest[SHARKSSL_SHA256_HASH_LEN]);
316
320SHARKSSL_API int sharkssl_sha256(const U8*, U16, U8*);
321#endif
322
323#if SHARKSSL_USE_SHA_384
330
334SHARKSSL_API void SharkSslSha384Ctx_append(SharkSslSha384Ctx*, const U8* data, U32 len);
335
339SHARKSSL_API void SharkSslSha384Ctx_finish(SharkSslSha384Ctx*, U8 digest[SHARKSSL_SHA384_HASH_LEN]);
340
344SHARKSSL_API int sharkssl_sha384(const U8*, U16, U8*);
345#endif
346
347#if SHARKSSL_USE_SHA_512
353SHARKSSL_API void SharkSslSha512Ctx_constructor(SharkSslSha512Ctx* ctx);
354
358#define SharkSslSha512Ctx_append(ctx, d, l) \
359 SharkSslSha384Ctx_append((SharkSslSha384Ctx*)ctx, d, l)
360
364SHARKSSL_API void SharkSslSha512Ctx_finish(SharkSslSha512Ctx*, U8 digest[SHARKSSL_SHA512_HASH_LEN]);
365
369SHARKSSL_API int sharkssl_sha512(const U8*, U16, U8*);
370#endif
371
372#if (SHARKSSL_USE_SHA_512 || SHARKSSL_USE_SHA_384 || SHARKSSL_USE_SHA_256 || SHARKSSL_USE_SHA1 || SHARKSSL_USE_MD5)
382SHARKSSL_API void SharkSslHMACCtx_constructor(SharkSslHMACCtx* ctx, U8 hashID, const U8* key, U16 keyLen);
383
387SHARKSSL_API void SharkSslHMACCtx_append(SharkSslHMACCtx*, const U8* data, U32 len);
388
392SHARKSSL_API void SharkSslHMACCtx_finish(SharkSslHMACCtx*, U8 *HMAC);
393
394#define SharkSslHMACCtx_destructor(o) memset(o, 0, sizeof(SharkSslHMACCtx))
395
407SHARKSSL_API int sharkssl_HMAC(const U8 hashID, const U8 *data, U16 len, const U8 *key, U16 keyLen, U8 *digest);
408#endif
409
410U16 sharkssl_getHashLen(U8 hashID);
411int sharkssl_hash(U8 *digest, U8 *data, U16 len, U8 hashID);
412
413
414#if SHARKSSL_USE_POLY1305
421SHARKSSL_API void SharkSslPoly1305Ctx_constructor(SharkSslPoly1305Ctx *ctx, const U8 key[32]);
422
423#define SharkSslPoly1305Ctx_destructor(o) memset(o, 0, sizeof(SharkSslPoly1305Ctx))
424
428SHARKSSL_API void SharkSslPoly1305Ctx_append(SharkSslPoly1305Ctx *ctx, const U8 *in, U32 len);
429
433SHARKSSL_API void SharkSslPoly1305Ctx_finish(SharkSslPoly1305Ctx *ctx, U8 digest[SHARKSSL_POLY1305_HASH_LEN]);
434
438SHARKSSL_API int sharkssl_poly1305(const U8 *data, U16 len, U8 *digest, const U8 key[32]);
439#endif
440
441#if SHARKSSL_USE_CHACHA20
450 const U8 *key, U8 keyLen);
451
452#define SharkSslChaChaCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslChaChaCtx))
453
460SHARKSSL_API void SharkSslChaChaCtx_setIV(SharkSslChaChaCtx *ctx, const U8 IV[12]);
461
465SHARKSSL_API void SharkSslChaChaCtx_crypt(
466 SharkSslChaChaCtx *ctx, const U8 *input, U8 *output, U32 len);
467#endif
468
469
470#if (SHARKSSL_USE_AES_256 || SHARKSSL_USE_AES_192 || SHARKSSL_USE_AES_128)
480 SharkSslAesCtx_Type type,
481 const U8 *key, U8 keyLen);
482#define SharkSslAesCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslAesCtx))
483
484#if (!SHARKSSL_DISABLE_AES_ECB_DECRYPT)
488SHARKSSL_API void SharkSslAesCtx_decrypt(SharkSslAesCtx *ctx, const U8 input[16], U8 output[16]);
489#endif
490
494SHARKSSL_API void SharkSslAesCtx_encrypt(SharkSslAesCtx *ctx, U8 input[16], U8 output[16]);
495
496#if SHARKSSL_ENABLE_AES_CBC
500SHARKSSL_API void SharkSslAesCtx_cbc_encrypt(SharkSslAesCtx *ctx, U8 vect[16],
501 const U8 *input, U8 *output, U16 len);
502
506SHARKSSL_API void SharkSslAesCtx_cbc_decrypt(SharkSslAesCtx *ctx, U8 vect[16],
507 const U8 *input, U8 *output, U16 len);
508#endif
509#if SHARKSSL_ENABLE_AES_CTR_MODE
510
514SHARKSSL_API void SharkSslAesCtx_ctr_mode(SharkSslAesCtx *ctx, U8 ctr[16],
515 const U8 *input, U8 *output, U16 len);
516#endif
517#if SHARKSSL_ENABLE_AES_GCM
526 const U8 *key, U8 keyLen);
527
528#define SharkSslAesGcmCtx_destructor(ctx) \
529 memset(ctx, 0, sizeof(SharkSslAesGcmCtx))
530
561 const U8 vect[12], U8 tagout[16],
562 const U8 *auth, U16 authlen,
563 const U8 *input, U8 *output, U16 len);
564
565
581 const U8 vect[12], U8 tagin[16],
582 const U8 *auth, U16 authlen,
583 U8 *input, U8 *output, U16 len);
584#endif
585#if SHARKSSL_ENABLE_AES_CCM
594SHARKSSL_API void SharkSslAesCcmCtx_constructor(SharkSslAesCcmCtx *ctx,
595 const U8 *key, U8 keyLen, U8 tagLen);
596
597#define SharkSslAesCcmCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslAesCcmCtx))
598
602SHARKSSL_API int SharkSslAesCcmCtx_encrypt(SharkSslAesCcmCtx *ctx,
603 const U8 vect[12], U8 *tagout,
604 const U8 *auth, U16 authlen,
605 const U8 *input, U8 *output, U16 len);
606
607
611SHARKSSL_API int SharkSslAesCcmCtx_decrypt(SharkSslAesCcmCtx *ctx,
612 const U8 vect[12], U8 *tagin,
613 const U8 *auth, U16 authlen,
614 const U8 *input, U8 *output, U16 len);
615#endif
616#endif
617
618#ifdef __cplusplus
619}
620#endif
621
622
623#endif /* _SharkSslCrypto_h */
struct SharkSslAesCtx SharkSslAesCtx
AES.
SHARKSSL_API void SharkSslAesCtx_constructor(SharkSslAesCtx *ctx, SharkSslAesCtx_Type type, const U8 *key, U8 keyLen)
Initialize.
SHARKSSL_API void SharkSslAesCtx_decrypt(SharkSslAesCtx *ctx, const U8 input[16], U8 output[16])
Decrypt.
SHARKSSL_API void SharkSslAesCtx_encrypt(SharkSslAesCtx *ctx, U8 input[16], U8 output[16])
Encrypt.
SHARKSSL_API int SharkSslAesGcmCtx_encrypt(SharkSslAesGcmCtx *ctx, const U8 vect[12], U8 tagout[16], const U8 *auth, U16 authlen, const U8 *input, U8 *output, U16 len)
Encrypt data or a chunk of a large data set.
SHARKSSL_API void SharkSslAesGcmCtx_constructor(SharkSslAesGcmCtx *ctx, const U8 *key, U8 keyLen)
Initialize.
SHARKSSL_API int SharkSslAesGcmCtx_decrypt(SharkSslAesGcmCtx *ctx, const U8 vect[12], U8 tagin[16], const U8 *auth, U16 authlen, U8 *input, U8 *output, U16 len)
Decrypt data or a chunk of a large data set.
struct SharkSslAesGcmCtx SharkSslAesGcmCtx
AesGcmCtx.
SHARKSSL_API void SharkSslChaChaCtx_crypt(SharkSslChaChaCtx *ctx, const U8 *input, U8 *output, U32 len)
Encrypt or decrypt.
SHARKSSL_API void SharkSslChaChaCtx_constructor(SharkSslChaChaCtx *ctx, const U8 *key, U8 keyLen)
Initialize.
SHARKSSL_API void SharkSslChaChaCtx_setIV(SharkSslChaChaCtx *ctx, const U8 IV[12])
Initialize.
struct SharkSslHMACCtx SharkSslHMACCtx
HMAC.
SHARKSSL_API void SharkSslHMACCtx_append(SharkSslHMACCtx *, const U8 *data, U32 len)
append
SHARKSSL_API void SharkSslHMACCtx_finish(SharkSslHMACCtx *, U8 *HMAC)
finish
SHARKSSL_API void SharkSslHMACCtx_constructor(SharkSslHMACCtx *ctx, U8 hashID, const U8 *key, U16 keyLen)
Initialize.
SHARKSSL_API int sharkssl_HMAC(const U8 hashID, const U8 *data, U16 len, const U8 *key, U16 keyLen, U8 *digest)
HMAC.
SHARKSSL_API void SharkSslPoly1305Ctx_finish(SharkSslPoly1305Ctx *ctx, U8 digest[SHARKSSL_POLY1305_HASH_LEN])
finish
SHARKSSL_API void SharkSslPoly1305Ctx_constructor(SharkSslPoly1305Ctx *ctx, const U8 key[32])
Initialize.
struct SharkSslPoly1305Ctx SharkSslPoly1305Ctx
POLY1305.
SHARKSSL_API void SharkSslPoly1305Ctx_append(SharkSslPoly1305Ctx *ctx, const U8 *in, U32 len)
append
SHARKSSL_API int sharkssl_poly1305(const U8 *data, U16 len, U8 *digest, const U8 key[32])
poly1305
SHARKSSL_API int sharkssl_sha256(const U8 *, U16, U8 *)
sha256
SHARKSSL_API void SharkSslSha256Ctx_constructor(SharkSslSha256Ctx *ctx)
Initialize.
SHARKSSL_API void SharkSslSha256Ctx_finish(SharkSslSha256Ctx *, U8 digest[SHARKSSL_SHA256_HASH_LEN])
finish
struct SharkSslSha256Ctx SharkSslSha256Ctx
SHA256.
SHARKSSL_API void SharkSslSha256Ctx_append(SharkSslSha256Ctx *, const U8 *data, U32 len)
append
SHARKSSL_API void SharkSslSha384Ctx_append(SharkSslSha384Ctx *, const U8 *data, U32 len)
append
SHARKSSL_API void SharkSslSha384Ctx_constructor(SharkSslSha384Ctx *ctx)
Initialize.
struct SharkSslSha384Ctx SharkSslSha384Ctx
SHA384.
SHARKSSL_API void SharkSslSha384Ctx_finish(SharkSslSha384Ctx *, U8 digest[SHARKSSL_SHA384_HASH_LEN])
finish
SHARKSSL_API int sharkssl_sha384(const U8 *, U16, U8 *)
sha384
AES.
Definition: SharkSslCrypto.h:200
AesGcmCtx.
Definition: SharkSslCrypto.h:222
CHACHA20.
Definition: SharkSslCrypto.h:189
HMAC.
Definition: SharkSslCrypto.h:131
POLY1305.
Definition: SharkSslCrypto.h:174
SHA256.
Definition: SharkSslCrypto.h:97
SHA384.
Definition: SharkSslCrypto.h:110