This package includes the SharkSSL lite library and header files.
Dependents: WebSocket-Client-Example SharkMQ-LED-Demo
SharkSslCrypto.h
00001 /* 00002 * ____ _________ __ _ 00003 * / __ \___ ____ _/ /_ __(_)___ ___ ___ / / ____ ____ _(_)____ 00004 * / /_/ / _ \/ __ `/ / / / / / __ `__ \/ _ \/ / / __ \/ __ `/ / ___/ 00005 * / _, _/ __/ /_/ / / / / / / / / / / / __/ /___/ /_/ / /_/ / / /__ 00006 * /_/ |_|\___/\__,_/_/ /_/ /_/_/ /_/ /_/\___/_____/\____/\__, /_/\___/ 00007 * /____/ 00008 * 00009 * SharkSSL Embedded SSL/TLS Stack 00010 **************************************************************************** 00011 * PROGRAM MODULE 00012 * 00013 * $Id: SharkSslCrypto.h 3686 2015-04-27 06:35:45Z gianluca $ 00014 * 00015 * COPYRIGHT: Real Time Logic LLC, 2010 - 2013 00016 * 00017 * This software is copyrighted by and is the sole property of Real 00018 * Time Logic LLC. All rights, title, ownership, or other interests in 00019 * the software remain the property of Real Time Logic LLC. This 00020 * software may only be used in accordance with the terms and 00021 * conditions stipulated in the corresponding license agreement under 00022 * which the software has been supplied. Any unauthorized use, 00023 * duplication, transmission, distribution, or disclosure of this 00024 * software is expressly forbidden. 00025 * 00026 * This Copyright notice may not be removed or modified without prior 00027 * written consent of Real Time Logic LLC. 00028 * 00029 * Real Time Logic LLC. reserves the right to modify this software 00030 * without notice. 00031 * 00032 * http://www.realtimelogic.com 00033 * http://www.sharkssl.com 00034 **************************************************************************** 00035 * 00036 */ 00037 #ifndef _SharkSslCrypto_h 00038 #define _SharkSslCrypto_h 00039 00040 #define SHARKSSL_LIB 1 00041 #include "SharkSSL.h" 00042 00043 00044 #define SHARKSSL_MD5_HASH_LEN 16 00045 #define SHARKSSL_SHA1_HASH_LEN 20 00046 #define SHARKSSL_SHA256_HASH_LEN 32 00047 #define SHARKSSL_SHA384_HASH_LEN 48 00048 #define SHARKSSL_SHA512_HASH_LEN 64 00049 #define SHARKSSL_POLY1305_HASH_LEN 16 00050 00051 00052 #if SHARKSSL_USE_MD5 00053 /** MD5 00054 \ingroup RayCryptoMD5 00055 */ 00056 typedef struct SharkSslMd5Ctx 00057 { 00058 U32 total[2]; 00059 U32 state[4]; 00060 U8 buffer[64]; 00061 } SharkSslMd5Ctx; 00062 #endif 00063 00064 00065 #if SHARKSSL_USE_SHA1 00066 /** SHA1 00067 \ingroup RayCryptoSHA1 00068 */ 00069 typedef struct SharkSslSha1Ctx 00070 { 00071 U32 total[2]; 00072 U32 state[5]; 00073 U8 buffer[64]; 00074 } SharkSslSha1Ctx; 00075 #endif 00076 00077 00078 #if SHARKSSL_USE_SHA_256 00079 /** SHA256 00080 \ingroup RayCryptoSHA256 00081 */ 00082 typedef struct SharkSslSha256Ctx 00083 { 00084 U32 total[2]; 00085 U32 state[8]; 00086 U8 buffer[64]; 00087 } SharkSslSha256Ctx; 00088 #endif 00089 00090 00091 #if (SHARKSSL_USE_SHA_384 || SHARKSSL_USE_SHA_512) 00092 /** SHA384 00093 \ingroup RayCryptoSHA384 00094 */ 00095 typedef struct SharkSslSha384Ctx 00096 { 00097 U32 total[4]; 00098 U64 state[8]; 00099 U8 buffer[128]; 00100 } SharkSslSha384Ctx; 00101 #endif 00102 00103 00104 #if SHARKSSL_USE_SHA_512 00105 /** SHA512 00106 \ingroup RayCryptoSHA512 00107 */ 00108 typedef struct SharkSslSha384Ctx SharkSslSha512Ctx; 00109 #endif 00110 00111 00112 #if SHARKSSL_USE_POLY1305 00113 /** POLY1305 00114 \ingroup RayCryptoPOLY1305 00115 */ 00116 typedef struct SharkSslPoly1305Ctx 00117 { 00118 U32 r[5]; 00119 U32 key[4]; 00120 U32 nonce[4]; 00121 U8 buffer[16]; 00122 U8 flag, blen; 00123 } SharkSslPoly1305Ctx; 00124 #endif 00125 00126 00127 #if SHARKSSL_USE_CHACHA20 00128 /** CHACHA20 00129 \ingroup RayCryptoCHACHA20 00130 */ 00131 typedef struct 00132 { 00133 U32 state[16]; 00134 } SharkSslChaChaCtx; 00135 #endif 00136 00137 00138 #if SHARKSSL_USE_ARC4 00139 /** ARC4 00140 \ingroup RayCryptoARC4 00141 */ 00142 typedef struct SharkSslArc4Ctx 00143 { 00144 U8 x; 00145 U8 y; 00146 U8 b[256]; 00147 } SharkSslArc4Ctx; 00148 #endif 00149 00150 00151 #if (SHARKSSL_USE_DES || SHARKSSL_USE_3DES) 00152 /** DES 00153 \ingroup RayCryptoDES 00154 */ 00155 typedef struct SharkSslDesCtx 00156 { 00157 #if SHARKSSL_NOPACK 00158 U32 key[96]; /* Max size == 3DES */ 00159 U8 tdea; 00160 #else 00161 00162 #if (SHARKSSL_USE_CAU || SHARKSSL_USE_MMCAU) 00163 #if SHARKSSL_USE_3DES 00164 U32 key[6]; 00165 #else 00166 U32 key[2]; 00167 #endif 00168 00169 #else 00170 00171 #if SHARKSSL_USE_3DES 00172 U32 key[96]; 00173 #else 00174 U32 key[32]; 00175 #endif 00176 #endif 00177 00178 #if (SHARKSSL_USE_DES && SHARKSSL_USE_3DES) 00179 U8 tdea; 00180 #endif 00181 00182 #endif 00183 } SharkSslDesCtx; 00184 00185 00186 /* Encrypt/decrypt type */ 00187 typedef enum 00188 { 00189 SharkSslDesCtx_Decrypt, /*!< Decrypt */ 00190 SharkSslDesCtx_Encrypt /*!< Encrypt */ 00191 } SharkSslDesCtx_Type; 00192 #endif 00193 00194 00195 #if (SHARKSSL_USE_AES_256 || SHARKSSL_USE_AES_192 || SHARKSSL_USE_AES_128) 00196 /** AES 00197 \ingroup RayCryptoAES 00198 */ 00199 typedef struct SharkSslAesCtx 00200 { 00201 #if (SHARKSSL_USE_AES_256 || SHARKSSL_NOPACK) 00202 U32 key[60]; 00203 #elif SHARKSSL_USE_AES_192 00204 U32 key[52]; 00205 #else 00206 U32 key[44]; 00207 #endif 00208 U16 nr; 00209 } SharkSslAesCtx; 00210 00211 typedef enum 00212 { 00213 SharkSslAesCtx_Decrypt, 00214 SharkSslAesCtx_Encrypt 00215 } SharkSslAesCtx_Type; 00216 00217 #if SHARKSSL_ENABLE_AES_GCM 00218 /** AesGcmCtx 00219 \ingroup RayCryptoAesGcm 00220 */ 00221 typedef struct SharkSslAesGcmCtx 00222 { 00223 SharkSslAesCtx super; 00224 U8 M0[16][16]; 00225 } SharkSslAesGcmCtx; 00226 #endif 00227 00228 #if SHARKSSL_ENABLE_AES_CCM 00229 /** AesCcmCtx 00230 \ingroup RayCryptoAesCcm 00231 */ 00232 typedef struct SharkSslAesCcmCtx 00233 { 00234 SharkSslAesCtx super; 00235 U8 tagLen; 00236 } SharkSslAesCcmCtx; 00237 #endif 00238 #endif /* SHARKSSL_USE_AES_256 || SHARKSSL_USE_AES_192 || SHARKSSL_USE_AES_128 */ 00239 00240 00241 #ifdef __cplusplus 00242 extern "C" { 00243 #endif 00244 00245 /* SharkSslCrypto.c */ 00246 SHARKSSL_API int sharkssl_entropy(U32); 00247 SHARKSSL_API int sharkssl_rng(U8*, U16); 00248 SHARKSSL_API int sharkssl_kmemcmp(const void *a, const void *b, U32 n); 00249 00250 00251 #if SHARKSSL_USE_MD5 00252 /** Initialize 00253 \ingroup RayCryptoMD5 00254 00255 \param ctx Uninitialized data of size sizeof(SharkSslMd5Ctx). 00256 */ 00257 SHARKSSL_API void SharkSslMd5Ctx_constructor(SharkSslMd5Ctx* ctx); 00258 00259 /** append 00260 \ingroup RayCryptoMD5 00261 */ 00262 SHARKSSL_API void SharkSslMd5Ctx_append(SharkSslMd5Ctx* ctx, const U8* data, U32 len); 00263 00264 /** finish 00265 \ingroup RayCryptoMD5 00266 */ 00267 SHARKSSL_API void SharkSslMd5Ctx_finish(SharkSslMd5Ctx* ctx, U8 digest[SHARKSSL_MD5_HASH_LEN]); 00268 00269 /** md5 00270 \ingroup RayCryptoMD5 00271 */ 00272 SHARKSSL_API int sharkssl_md5(const U8*, U16, U8*); 00273 #endif 00274 00275 #if SHARKSSL_USE_SHA1 00276 /** Initialize 00277 \ingroup RayCryptoSHA1 00278 00279 \param ctx Uninitialized data of size sizeof(SharkSslSha1Ctx). 00280 */ 00281 SHARKSSL_API void SharkSslSha1Ctx_constructor(SharkSslSha1Ctx* ctx); 00282 00283 /** append 00284 \ingroup RayCryptoSHA1 00285 */ 00286 SHARKSSL_API void SharkSslSha1Ctx_append(SharkSslSha1Ctx* ctx, const U8* data, U32 len); 00287 00288 /** finish 00289 \ingroup RayCryptoSHA1 00290 */ 00291 SHARKSSL_API void SharkSslSha1Ctx_finish(SharkSslSha1Ctx*, U8 digest[SHARKSSL_SHA1_HASH_LEN]); 00292 00293 /** sha1 00294 \ingroup RayCryptoSHA1 00295 */ 00296 SHARKSSL_API int sharkssl_sha1(const U8*, U16, U8*); 00297 #endif 00298 00299 #if SHARKSSL_USE_SHA_256 00300 /** Initialize 00301 \ingroup RayCryptoSHA256 00302 00303 \param ctx Uninitialized data of size sizeof(SharkSslSha256Ctx). 00304 */ 00305 SHARKSSL_API void SharkSslSha256Ctx_constructor(SharkSslSha256Ctx* ctx); 00306 00307 /** append 00308 \ingroup RayCryptoSHA256 00309 */ 00310 SHARKSSL_API void SharkSslSha256Ctx_append(SharkSslSha256Ctx*, const U8* data, U32 len); 00311 00312 /** finish 00313 \ingroup RayCryptoSHA256 00314 */ 00315 SHARKSSL_API void SharkSslSha256Ctx_finish(SharkSslSha256Ctx*, U8 digest[SHARKSSL_SHA256_HASH_LEN]); 00316 00317 /** sha256 00318 \ingroup RayCryptoSHA256 00319 */ 00320 SHARKSSL_API int sharkssl_sha256(const U8*, U16, U8*); 00321 #endif 00322 00323 #if SHARKSSL_USE_SHA_384 00324 /** Initialize 00325 \ingroup RayCryptoSHA384 00326 00327 \param ctx Uninitialized data of size sizeof(SharkSslSha384Ctx). 00328 */ 00329 SHARKSSL_API void SharkSslSha384Ctx_constructor(SharkSslSha384Ctx* ctx); 00330 00331 /** append 00332 \ingroup RayCryptoSHA384 00333 */ 00334 SHARKSSL_API void SharkSslSha384Ctx_append(SharkSslSha384Ctx*, const U8* data, U32 len); 00335 00336 /** finish 00337 \ingroup RayCryptoSHA384 00338 */ 00339 SHARKSSL_API void SharkSslSha384Ctx_finish(SharkSslSha384Ctx*, U8 digest[SHARKSSL_SHA384_HASH_LEN]); 00340 00341 /** sha384 00342 \ingroup RayCryptoSHA384 00343 */ 00344 SHARKSSL_API int sharkssl_sha384(const U8*, U16, U8*); 00345 #endif 00346 00347 #if SHARKSSL_USE_SHA_512 00348 /** Initialize 00349 \ingroup RayCryptoSHA512 00350 00351 \param ctx Uninitialized data of size sizeof(SharkSslSha512Ctx). 00352 */ 00353 SHARKSSL_API void SharkSslSha512Ctx_constructor(SharkSslSha512Ctx* ctx); 00354 00355 /** append 00356 \ingroup RayCryptoSHA512 00357 */ 00358 #define SharkSslSha512Ctx_append(ctx, d, l) \ 00359 SharkSslSha384Ctx_append((SharkSslSha384Ctx*)ctx, d, l) 00360 00361 /** finish 00362 \ingroup RayCryptoSHA512 00363 */ 00364 SHARKSSL_API void SharkSslSha512Ctx_finish(SharkSslSha512Ctx*, U8 digest[SHARKSSL_SHA512_HASH_LEN]); 00365 00366 /** sha512 00367 \ingroup RayCryptoSHA512 00368 */ 00369 SHARKSSL_API int sharkssl_sha512(const U8*, U16, U8*); 00370 #endif 00371 00372 #if SHARKSSL_USE_POLY1305 00373 /** Initialize 00374 \ingroup RayCryptoPOLY1305 00375 00376 \param ctx Uninitialized data of size sizeof(SharkSslPoly1305Ctx). 00377 \param key 16-byte key + 16-byte nonce 00378 */ 00379 SHARKSSL_API void SharkSslPoly1305Ctx_constructor(SharkSslPoly1305Ctx *ctx, const U8 key[32]); 00380 00381 #define SharkSslPoly1305Ctx_destructor(o) memset(o, 0, sizeof(SharkSslPoly1305Ctx)) 00382 00383 /** append 00384 \ingroup RayCryptoPOLY1305 00385 */ 00386 SHARKSSL_API void SharkSslPoly1305Ctx_append(SharkSslPoly1305Ctx *ctx, const U8 *in, U32 len); 00387 00388 /** finish 00389 \ingroup RayCryptoPOLY1305 00390 */ 00391 SHARKSSL_API void SharkSslPoly1305Ctx_finish(SharkSslPoly1305Ctx *ctx, U8 digest[SHARKSSL_POLY1305_HASH_LEN]); 00392 00393 /** poly1305 00394 \ingroup RayCryptoPOLY1305 00395 */ 00396 SHARKSSL_API int sharkssl_poly1305(const U8 *data, U16 len, U8 *digest, const U8 key[32]); 00397 #endif 00398 00399 #if SHARKSSL_USE_CHACHA20 00400 /** Initialize 00401 \ingroup RayCryptoCHACHA20 00402 00403 \param ctx Uninitialized data of size sizeof(SharkSslChaChaCtx) 00404 \param key 16- or 32-byte key 00405 \param keyLen the key length in bytes (either 16 or 32) 00406 */ 00407 SHARKSSL_API void SharkSslChaChaCtx_constructor(SharkSslChaChaCtx *ctx, 00408 const U8 *key, U8 keyLen); 00409 00410 #define SharkSslChaChaCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslChaChaCtx)) 00411 00412 /** Initialize 00413 \ingroup RayCryptoCHACHA20 00414 00415 \param ctx Initialized data of size sizeof(SharkSslChaChaCtx) 00416 \param IV 8-byte initialization vector 00417 */ 00418 SHARKSSL_API void SharkSslChaChaCtx_setIV(SharkSslChaChaCtx *ctx, const U8 IV[8]); 00419 00420 /** Encrypt or decrypt 00421 \ingroup RayCryptoCHACHA20 00422 */ 00423 SHARKSSL_API void SharkSslChaChaCtx_crypt( 00424 SharkSslChaChaCtx *ctx, const U8 *input, U8 *output, U32 len); 00425 #endif 00426 00427 #if SHARKSSL_USE_ARC4 00428 /** Initialize 00429 \ingroup RayCryptoARC4 00430 00431 */ 00432 SHARKSSL_API void SharkSslArc4Ctx_constructor(SharkSslArc4Ctx *ctx, 00433 const U8 *key, U8 keyLen); 00434 00435 #define SharkSslArc4Ctx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslArc4Ctx)) 00436 00437 /** Encrypt or decrypt 00438 \ingroup RayCryptoARC4 00439 */ 00440 SHARKSSL_API void SharkSslArc4Ctx_crypt( 00441 SharkSslArc4Ctx *ctx, U8 *input, U8 *output, U16 len); 00442 #endif 00443 00444 #if (SHARKSSL_USE_DES || SHARKSSL_USE_3DES) 00445 /** Initialize 00446 \ingroup RayCryptoDES 00447 00448 \param ctx Uninitialized data of size sizeof(SharkSslDesCtx) 00449 \param type encrypt/decrypt 00450 \param key the encryption/decryption key 00451 \param keyLen 'key' length 00452 */ 00453 SHARKSSL_API void SharkSslDesCtx_constructor(SharkSslDesCtx *ctx, 00454 SharkSslDesCtx_Type type, 00455 const U8 *key, U8 keyLen); 00456 #define SharkSslDesCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslDesCtx)) 00457 00458 /** Encrypt 00459 \ingroup RayCryptoDES 00460 */ 00461 SHARKSSL_API void SharkSslDesCtx_encrypt( 00462 SharkSslDesCtx *ctx, U8 input[8], U8 output[8]); 00463 #if (SHARKSSL_USE_CAU || SHARKSSL_USE_MMCAU) 00464 00465 /** Decrypt 00466 \ingroup RayCryptoDES 00467 */ 00468 SHARKSSL_API void SharkSslDesCtx_decrypt( 00469 SharkSslDesCtx *ctx, U8 input[8], U8 output[8]); 00470 #else 00471 #define SharkSslDesCtx_decrypt(ctx,i,o) SharkSslDesCtx_encrypt((ctx),(i),(o)) 00472 #endif 00473 00474 /** Encrypt 00475 \ingroup RayCryptoDES 00476 */ 00477 SHARKSSL_API void SharkSslDesCtx_cbc_encrypt(SharkSslDesCtx *ctx, U8 vect[8], 00478 U8 *input, U8 *output, U16 len); 00479 00480 /** Decrypt 00481 \ingroup RayCryptoDES 00482 */ 00483 SHARKSSL_API void SharkSslDesCtx_cbc_decrypt(SharkSslDesCtx *ctx, U8 vect[8], 00484 U8 *input, U8 *output, U16 len); 00485 #endif 00486 00487 #if (SHARKSSL_USE_AES_256 || SHARKSSL_USE_AES_192 || SHARKSSL_USE_AES_128) 00488 /** Initialize 00489 \ingroup RayCryptoAES 00490 00491 \param ctx Uninitialized data of size sizeof(SharkSslAesCtx) 00492 \param type encrypt/decrypt 00493 \param key the encryption/decryption key 00494 \param keyLen 'key' length 00495 */ 00496 SHARKSSL_API void SharkSslAesCtx_constructor(SharkSslAesCtx *ctx, 00497 SharkSslAesCtx_Type type, 00498 const U8 *key, U8 keyLen); 00499 #define SharkSslAesCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslAesCtx)) 00500 00501 #if (!SHARKSSL_DISABLE_AES_ECB_DECRYPT) 00502 /** Decrypt 00503 \ingroup RayCryptoAES 00504 */ 00505 SHARKSSL_API void SharkSslAesCtx_decrypt(SharkSslAesCtx *ctx, U8 input[16], U8 output[16]); 00506 #endif 00507 00508 /** Encrypt 00509 \ingroup RayCryptoAES 00510 */ 00511 SHARKSSL_API void SharkSslAesCtx_encrypt(SharkSslAesCtx *ctx, U8 input[16], U8 output[16]); 00512 00513 #if (!SHARKSSL_DISABLE_AES_CBC_MODE) 00514 /** Encrypt 00515 \ingroup RayCryptoAES 00516 */ 00517 SHARKSSL_API void SharkSslAesCtx_cbc_encrypt(SharkSslAesCtx *ctx, U8 vect[16], 00518 U8 *input, U8 *output, U16 len); 00519 00520 /** Decrypt 00521 \ingroup RayCryptoAES 00522 */ 00523 SHARKSSL_API void SharkSslAesCtx_cbc_decrypt(SharkSslAesCtx *ctx, U8 vect[16], 00524 U8 *input, U8 *output, U16 len); 00525 #endif 00526 #if SHARKSSL_ENABLE_AES_CTR_MODE 00527 00528 /** 00529 \ingroup RayCryptoAES 00530 */ 00531 SHARKSSL_API void SharkSslAesCtx_ctr_mode(SharkSslAesCtx *ctx, U8 ctr[16], 00532 U8 *input, U8 *output, U16 len); 00533 #endif 00534 #if SHARKSSL_ENABLE_AES_GCM 00535 /** Initialize 00536 \ingroup RayCryptoAesGcm 00537 00538 \param ctx Uninitialized data of size sizeof(SharkSslAesGcmCtx). 00539 \param key the encryption/decryption key 00540 \param keyLen 'key' length 00541 */ 00542 SHARKSSL_API void SharkSslAesGcmCtx_constructor(SharkSslAesGcmCtx *ctx, 00543 const U8 *key, U8 keyLen); 00544 00545 #define SharkSslAesGcmCtx_destructor(ctx) \ 00546 memset(ctx, 0, sizeof(SharkSslAesGcmCtx)) 00547 00548 /** Encrypt 00549 \ingroup RayCryptoAesGcm 00550 */ 00551 SHARKSSL_API int SharkSslAesGcmCtx_encrypt(SharkSslAesGcmCtx *ctx, 00552 const U8 vect[12], U8 tagout[16], 00553 const U8 *auth, U16 authlen, 00554 U8 *input, U8 *output, U16 len); 00555 00556 00557 /** Decrypt 00558 \ingroup RayCryptoAesGcm 00559 */ 00560 SHARKSSL_API int SharkSslAesGcmCtx_decrypt(SharkSslAesGcmCtx *ctx, 00561 const U8 vect[12], U8 tagin[16], 00562 const U8 *auth, U16 authlen, 00563 U8 *input, U8 *output, U16 len); 00564 #endif 00565 #if SHARKSSL_ENABLE_AES_CCM 00566 /** Initialize 00567 \ingroup RayCryptoAesCcm 00568 00569 \param ctx Uninitialized data of size sizeof(SharkSslAesCcmCtx). 00570 */ 00571 SHARKSSL_API void SharkSslAesCcmCtx_constructor(SharkSslAesCcmCtx *ctx, 00572 const U8 *key, U8 keyLen, U8 tagLen); 00573 00574 #define SharkSslAesCcmCtx_destructor(ctx) memset(ctx, 0, sizeof(SharkSslAesCcmCtx)) 00575 00576 /** Encrypt 00577 \ingroup RayCryptoAesCcm 00578 */ 00579 SHARKSSL_API int SharkSslAesCcmCtx_encrypt(SharkSslAesCcmCtx *ctx, 00580 const U8 vect[12], U8 *tagout, 00581 const U8 *auth, U16 authlen, 00582 U8 *input, U8 *output, U16 len); 00583 00584 00585 /** Decrypt 00586 \ingroup RayCryptoAesCcm 00587 */ 00588 SHARKSSL_API int SharkSslAesCcmCtx_decrypt(SharkSslAesCcmCtx *ctx, 00589 const U8 vect[12], U8 *tagin, 00590 const U8 *auth, U16 authlen, 00591 U8 *input, U8 *output, U16 len); 00592 #endif 00593 #endif 00594 00595 #ifdef __cplusplus 00596 } 00597 #endif 00598 00599 00600 #endif /* _SharkSslCrypto_h */
Generated on Wed Jul 13 2022 10:54:53 by 1.7.2