1 /* 2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the License); you may 5 * not use this file except in compliance with the License. 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 */ 9 10 11 12 #ifndef GMSSL_SHA3_H 13 #define GMSSL_SHA3_H 14 15 16 #include <string.h> 17 #include <stdint.h> 18 #include <sys/types.h> 19 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 26 #define SHA3_KECCAK_P_SIZE (1600/8) 27 28 #define SHA3_224_DIGEST_SIZE (224/8) 29 #define SHA3_256_DIGEST_SIZE (256/8) 30 #define SHA3_384_DIGEST_SIZE (384/8) 31 #define SHA3_512_DIGEST_SIZE (512/8) 32 33 #define SHA3_224_CAPACITY (SHA3_224_DIGEST_SIZE * 2) 34 #define SHA3_256_CAPACITY (SHA3_256_DIGEST_SIZE * 2) 35 #define SHA3_384_CAPACITY (SHA3_384_DIGEST_SIZE * 2) 36 #define SHA3_512_CAPACITY (SHA3_512_DIGEST_SIZE * 2) 37 38 #define SHA3_224_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 144 39 #define SHA3_256_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 136 40 #define SHA3_384_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 104 41 #define SHA3_512_BLOCK_SIZE (SHA3_KECCAK_P_SIZE - SHA3_224_CAPACITY) // 72 42 43 44 typedef struct { 45 uint64_t A[5][5]; 46 uint8_t buf[SHA3_224_BLOCK_SIZE]; 47 int num; 48 } SHA3_224_CTX; 49 50 void sha3_224_init(SHA3_224_CTX *ctx); 51 void sha3_224_update(SHA3_224_CTX *ctx, const uint8_t *data, size_t datalen); 52 void sha3_224_finish(SHA3_224_CTX *ctx, uint8_t dgst[SHA3_224_DIGEST_SIZE]); 53 54 typedef struct { 55 uint64_t A[5][5]; 56 uint8_t buf[SHA3_256_BLOCK_SIZE]; 57 int num; 58 } SHA3_256_CTX; 59 60 void sha3_256_init(SHA3_256_CTX *ctx); 61 void sha3_256_update(SHA3_256_CTX *ctx, const uint8_t *data, size_t datalen); 62 void sha3_256_finish(SHA3_256_CTX *ctx, uint8_t dgst[SHA3_256_DIGEST_SIZE]); 63 64 typedef struct { 65 uint64_t A[5][5]; 66 uint8_t buf[SHA3_384_BLOCK_SIZE]; 67 int num; 68 } SHA3_384_CTX; 69 70 void sha3_384_init(SHA3_384_CTX *ctx); 71 void sha3_384_update(SHA3_384_CTX *ctx, const uint8_t *data, size_t datalen); 72 void sha3_384_finish(SHA3_384_CTX *ctx, uint8_t dgst[SHA3_384_DIGEST_SIZE]); 73 74 typedef struct { 75 uint64_t A[5][5]; 76 uint8_t buf[SHA3_512_BLOCK_SIZE]; 77 int num; 78 } SHA3_512_CTX; 79 80 void sha3_512_init(SHA3_512_CTX *ctx); 81 void sha3_512_update(SHA3_512_CTX *ctx, const uint8_t *data, size_t datalen); 82 void sha3_512_finish(SHA3_512_CTX *ctx, uint8_t dgst[SHA3_512_DIGEST_SIZE]); 83 84 void sha3_shake128(const uint8_t *in, size_t *inlen, size_t outlen, uint8_t *out); 85 void sha3_shake256(const uint8_t *in, size_t *inlen, size_t outlen, uint8_t *out); 86 void sha3_keccak_p(uint8_t state[SHA3_KECCAK_P_SIZE]); 87 88 89 #ifdef __cplusplus 90 } 91 #endif 92 #endif 93