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 4818 2021-07-07 09:45:38Z gianluca $
14  *
15  * COPYRIGHT: Real Time Logic LLC, 2010 - 2019
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
67 
70 typedef 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
80 
83 typedef 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
93 
96 typedef 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)
106 
109 typedef 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
119 
122 typedef 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)
127 
130 typedef 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
170 
173 typedef struct SharkSslPoly1305Ctx
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
185 
188 typedef struct
189 {
190  U32 state[16];
192 #endif
193 
194 
195 #if (SHARKSSL_USE_AES_256 || SHARKSSL_USE_AES_192 || SHARKSSL_USE_AES_128)
196 
199 typedef 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 
211 typedef enum
212 {
213  SharkSslAesCtx_Decrypt,
214  SharkSslAesCtx_Encrypt
215 } SharkSslAesCtx_Type;
216 
217 #if SHARKSSL_ENABLE_AES_GCM
218 
221 typedef struct SharkSslAesGcmCtx
222 {
223  SharkSslAesCtx super;
224  U8 M0[16][16];
226 #endif
227 
228 #if SHARKSSL_ENABLE_AES_CCM
229 
232 typedef 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
242 extern "C" {
243 #endif
244 
245 /* SharkSslCrypto.c */
246 SHARKSSL_API int sharkssl_entropy(U32);
247 SHARKSSL_API int sharkssl_rng(U8*, U16);
248 SHARKSSL_API int sharkssl_kmemcmp(const void *a, const void *b, U32 n);
249 
250 
251 #if SHARKSSL_USE_MD5
252 
257 SHARKSSL_API void SharkSslMd5Ctx_constructor(SharkSslMd5Ctx* ctx);
258 
262 SHARKSSL_API void SharkSslMd5Ctx_append(SharkSslMd5Ctx* ctx, const U8* data, U32 len);
263 
267 SHARKSSL_API void SharkSslMd5Ctx_finish(SharkSslMd5Ctx* ctx, U8 digest[SHARKSSL_MD5_HASH_LEN]);
268 
272 SHARKSSL_API int sharkssl_md5(const U8*, U16, U8*);
273 #endif
274 
275 #if SHARKSSL_USE_SHA1
276 
281 SHARKSSL_API void SharkSslSha1Ctx_constructor(SharkSslSha1Ctx* ctx);
282 
286 SHARKSSL_API void SharkSslSha1Ctx_append(SharkSslSha1Ctx* ctx, const U8* data, U32 len);
287 
291 SHARKSSL_API void SharkSslSha1Ctx_finish(SharkSslSha1Ctx*, U8 digest[SHARKSSL_SHA1_HASH_LEN]);
292 
296 SHARKSSL_API int sharkssl_sha1(const U8*, U16, U8*);
297 #endif
298 
299 #if SHARKSSL_USE_SHA_256
300 
306 
310 SHARKSSL_API void SharkSslSha256Ctx_append(SharkSslSha256Ctx*, const U8* data, U32 len);
311 
315 SHARKSSL_API void SharkSslSha256Ctx_finish(SharkSslSha256Ctx*, U8 digest[SHARKSSL_SHA256_HASH_LEN]);
316 
320 SHARKSSL_API int sharkssl_sha256(const U8*, U16, U8*);
321 #endif
322 
323 #if SHARKSSL_USE_SHA_384
324 
330 
334 SHARKSSL_API void SharkSslSha384Ctx_append(SharkSslSha384Ctx*, const U8* data, U32 len);
335 
339 SHARKSSL_API void SharkSslSha384Ctx_finish(SharkSslSha384Ctx*, U8 digest[SHARKSSL_SHA384_HASH_LEN]);
340 
344 SHARKSSL_API int sharkssl_sha384(const U8*, U16, U8*);
345 #endif
346 
347 #if SHARKSSL_USE_SHA_512
348 
354 
358 #define SharkSslSha512Ctx_append(ctx, d, l) \
359  SharkSslSha384Ctx_append((SharkSslSha384Ctx*)ctx, d, l)
360 
364 SHARKSSL_API void SharkSslSha512Ctx_finish(SharkSslSha512Ctx*, U8 digest[SHARKSSL_SHA512_HASH_LEN]);
365 
369 SHARKSSL_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)
373 
382 SHARKSSL_API void SharkSslHMACCtx_constructor(SharkSslHMACCtx* ctx, U8 hashID, const U8* key, U16 keyLen);
383 
387 SHARKSSL_API void SharkSslHMACCtx_append(SharkSslHMACCtx*, const U8* data, U32 len);
388 
392 SHARKSSL_API void SharkSslHMACCtx_finish(SharkSslHMACCtx*, U8 *HMAC);
393 
394 #define SharkSslHMACCtx_destructor(o) memset(o, 0, sizeof(SharkSslHMACCtx))
395 
407 SHARKSSL_API int sharkssl_HMAC(const U8 hashID, const U8 *data, U16 len, const U8 *key, U16 keyLen, U8 *digest);
408 #endif
409 
410 U16 sharkssl_getHashLen(U8 hashID);
411 int sharkssl_hash(U8 *digest, U8 *data, U16 len, U8 hashID);
412 
413 
414 #if SHARKSSL_USE_POLY1305
415 
421 SHARKSSL_API void SharkSslPoly1305Ctx_constructor(SharkSslPoly1305Ctx *ctx, const U8 key[32]);
422 
423 #define SharkSslPoly1305Ctx_destructor(o) memset(o, 0, sizeof(SharkSslPoly1305Ctx))
424 
428 SHARKSSL_API void SharkSslPoly1305Ctx_append(SharkSslPoly1305Ctx *ctx, const U8 *in, U32 len);
429 
433 SHARKSSL_API void SharkSslPoly1305Ctx_finish(SharkSslPoly1305Ctx *ctx, U8 digest[SHARKSSL_POLY1305_HASH_LEN]);
434 
438 SHARKSSL_API int sharkssl_poly1305(const U8 *data, U16 len, U8 *digest, const U8 key[32]);
439 #endif
440 
441 #if SHARKSSL_USE_CHACHA20
442 
450  const U8 *key, U8 keyLen);
451 
452 #define SharkSslChaChaCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslChaChaCtx))
453 
460 SHARKSSL_API void SharkSslChaChaCtx_setIV(SharkSslChaChaCtx *ctx, const U8 IV[12]);
461 
465 SHARKSSL_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)
471 
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)
485 
488 SHARKSSL_API void SharkSslAesCtx_decrypt(SharkSslAesCtx *ctx, const U8 input[16], U8 output[16]);
489 #endif
490 
494 SHARKSSL_API void SharkSslAesCtx_encrypt(SharkSslAesCtx *ctx, U8 input[16], U8 output[16]);
495 
496 #if SHARKSSL_ENABLE_AES_CBC
497 
500 SHARKSSL_API void SharkSslAesCtx_cbc_encrypt(SharkSslAesCtx *ctx, U8 vect[16],
501  const U8 *input, U8 *output, U16 len);
502 
506 SHARKSSL_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 
514 SHARKSSL_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
518 
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
586 
594 SHARKSSL_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 
602 SHARKSSL_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 
611 SHARKSSL_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 */
SharkSslSha384Ctx_append
SHARKSSL_API void SharkSslSha384Ctx_append(SharkSslSha384Ctx *, const U8 *data, U32 len)
append
SharkSslSha384Ctx_constructor
SHARKSSL_API void SharkSslSha384Ctx_constructor(SharkSslSha384Ctx *ctx)
Initialize.
SharkSslAesGcmCtx
AesGcmCtx.
Definition: SharkSslCrypto.h:222
SharkSslAesCtx_decrypt
SHARKSSL_API void SharkSslAesCtx_decrypt(SharkSslAesCtx *ctx, const U8 input[16], U8 output[16])
Decrypt.
SharkSslChaChaCtx_setIV
SHARKSSL_API void SharkSslChaChaCtx_setIV(SharkSslChaChaCtx *ctx, const U8 IV[12])
Initialize.
SharkSslAesCtx
struct SharkSslAesCtx SharkSslAesCtx
AES.
SharkSslSha384Ctx_finish
SHARKSSL_API void SharkSslSha384Ctx_finish(SharkSslSha384Ctx *, U8 digest[SHARKSSL_SHA384_HASH_LEN])
finish
SharkSslPoly1305Ctx
struct SharkSslPoly1305Ctx SharkSslPoly1305Ctx
POLY1305.
SharkSslPoly1305Ctx_finish
SHARKSSL_API void SharkSslPoly1305Ctx_finish(SharkSslPoly1305Ctx *ctx, U8 digest[SHARKSSL_POLY1305_HASH_LEN])
finish
SharkSslSha384Ctx
struct SharkSslSha384Ctx SharkSslSha384Ctx
SHA384.
SharkSslAesGcmCtx_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.
SharkSslSha256Ctx
SHA256.
Definition: SharkSslCrypto.h:97
SharkSslHMACCtx_append
SHARKSSL_API void SharkSslHMACCtx_append(SharkSslHMACCtx *, const U8 *data, U32 len)
append
SharkSslAesCtx_constructor
SHARKSSL_API void SharkSslAesCtx_constructor(SharkSslAesCtx *ctx, SharkSslAesCtx_Type type, const U8 *key, U8 keyLen)
Initialize.
SharkSslAesCtx_encrypt
SHARKSSL_API void SharkSslAesCtx_encrypt(SharkSslAesCtx *ctx, U8 input[16], U8 output[16])
Encrypt.
SharkSslPoly1305Ctx_constructor
SHARKSSL_API void SharkSslPoly1305Ctx_constructor(SharkSslPoly1305Ctx *ctx, const U8 key[32])
Initialize.
SharkSslHMACCtx
HMAC.
Definition: SharkSslCrypto.h:131
SharkSslHMACCtx
struct SharkSslHMACCtx SharkSslHMACCtx
HMAC.
SharkSslPoly1305Ctx_append
SHARKSSL_API void SharkSslPoly1305Ctx_append(SharkSslPoly1305Ctx *ctx, const U8 *in, U32 len)
append
SharkSslPoly1305Ctx
POLY1305.
Definition: SharkSslCrypto.h:174
sharkssl_HMAC
SHARKSSL_API int sharkssl_HMAC(const U8 hashID, const U8 *data, U16 len, const U8 *key, U16 keyLen, U8 *digest)
HMAC.
SharkSslAesGcmCtx_constructor
SHARKSSL_API void SharkSslAesGcmCtx_constructor(SharkSslAesGcmCtx *ctx, const U8 *key, U8 keyLen)
Initialize.
sharkssl_sha512
SHARKSSL_API int sharkssl_sha512(const U8 *, U16, U8 *)
sha512
SharkSslAesCtx
AES.
Definition: SharkSslCrypto.h:200
sharkssl_sha384
SHARKSSL_API int sharkssl_sha384(const U8 *, U16, U8 *)
sha384
SharkSslChaChaCtx_constructor
SHARKSSL_API void SharkSslChaChaCtx_constructor(SharkSslChaChaCtx *ctx, const U8 *key, U8 keyLen)
Initialize.
SharkSslChaChaCtx
CHACHA20.
Definition: SharkSslCrypto.h:189
sharkssl_poly1305
SHARKSSL_API int sharkssl_poly1305(const U8 *data, U16 len, U8 *digest, const U8 key[32])
poly1305
SharkSslHMACCtx_constructor
SHARKSSL_API void SharkSslHMACCtx_constructor(SharkSslHMACCtx *ctx, U8 hashID, const U8 *key, U16 keyLen)
Initialize.
SharkSslAesGcmCtx_decrypt
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.
SharkSslHMACCtx_finish
SHARKSSL_API void SharkSslHMACCtx_finish(SharkSslHMACCtx *, U8 *HMAC)
finish
SharkSslSha256Ctx_finish
SHARKSSL_API void SharkSslSha256Ctx_finish(SharkSslSha256Ctx *, U8 digest[SHARKSSL_SHA256_HASH_LEN])
finish
sharkssl_sha256
SHARKSSL_API int sharkssl_sha256(const U8 *, U16, U8 *)
sha256
SharkSslSha512Ctx_constructor
SHARKSSL_API void SharkSslSha512Ctx_constructor(SharkSslSha512Ctx *ctx)
Initialize.
SharkSslSha512Ctx_finish
SHARKSSL_API void SharkSslSha512Ctx_finish(SharkSslSha512Ctx *, U8 digest[SHARKSSL_SHA512_HASH_LEN])
finish
SharkSslSha256Ctx_constructor
SHARKSSL_API void SharkSslSha256Ctx_constructor(SharkSslSha256Ctx *ctx)
Initialize.
SharkSslAesGcmCtx
struct SharkSslAesGcmCtx SharkSslAesGcmCtx
AesGcmCtx.
SharkSslSha384Ctx
SHA384.
Definition: SharkSslCrypto.h:110
SharkSslSha256Ctx
struct SharkSslSha256Ctx SharkSslSha256Ctx
SHA256.
SharkSslSha256Ctx_append
SHARKSSL_API void SharkSslSha256Ctx_append(SharkSslSha256Ctx *, const U8 *data, U32 len)
append
SharkSslChaChaCtx_crypt
SHARKSSL_API void SharkSslChaChaCtx_crypt(SharkSslChaChaCtx *ctx, const U8 *input, U8 *output, U32 len)
Encrypt or decrypt.