1 /* 2 * This file is part of the openHiTLS project. 3 * 4 * openHiTLS is licensed under the Mulan PSL v2. 5 * You can use this software according to the terms and conditions of the Mulan PSL v2. 6 * You may obtain a copy of Mulan PSL v2 at: 7 * 8 * http://license.coscl.org.cn/MulanPSL2 9 * 10 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13 * See the Mulan PSL v2 for more details. 14 */ 15 16 #ifndef CRYPT_SHA3_H 17 #define CRYPT_SHA3_H 18 19 #include "hitls_build.h" 20 #ifdef HITLS_CRYPTO_SHA3 21 22 #include <stdint.h> 23 #include <stdlib.h> 24 #include "crypt_types.h" 25 #include "bsl_params.h" 26 27 #ifdef __cplusplus 28 extern "C" { 29 #endif /* __cpluscplus */ 30 31 32 /** @defgroup LLF SHA3 Low level function */ 33 34 /* SHA3-224 */ 35 #define CRYPT_SHA3_224_BLOCKSIZE 144 // ((1600 - 224 * 2) / 8) 36 #define CRYPT_SHA3_224_DIGESTSIZE 28 37 38 /* SHA3-256 */ 39 #define CRYPT_SHA3_256_BLOCKSIZE 136 // ((1600 - 256 * 2) / 8) 40 #define CRYPT_SHA3_256_DIGESTSIZE 32 41 42 /* SHA3-384 */ 43 #define CRYPT_SHA3_384_BLOCKSIZE 104 // ((1600 - 384 * 2) / 8) 44 #define CRYPT_SHA3_384_DIGESTSIZE 48 45 46 /* SHA3-512 */ 47 #define CRYPT_SHA3_512_BLOCKSIZE 72 // ((1600 - 512 * 2) / 8) 48 #define CRYPT_SHA3_512_DIGESTSIZE 64 49 50 /* SHAKE128 */ 51 #define CRYPT_SHAKE128_BLOCKSIZE 168 // ((1600 - 128 * 2) / 8) 52 #define CRYPT_SHAKE128_DIGESTSIZE 0 53 54 /* SHAKE256 */ 55 #define CRYPT_SHAKE256_BLOCKSIZE 136 // ((1600 - 256 * 2) / 8) 56 #define CRYPT_SHAKE256_DIGESTSIZE 0 57 58 typedef struct CryptSha3Ctx CRYPT_SHA3_Ctx; 59 60 typedef CRYPT_SHA3_Ctx CRYPT_SHA3_224_Ctx; 61 62 typedef CRYPT_SHA3_Ctx CRYPT_SHA3_256_Ctx; 63 64 typedef CRYPT_SHA3_Ctx CRYPT_SHA3_384_Ctx; 65 66 typedef CRYPT_SHA3_Ctx CRYPT_SHA3_512_Ctx; 67 68 typedef CRYPT_SHA3_Ctx CRYPT_SHAKE128_Ctx; 69 70 typedef CRYPT_SHA3_Ctx CRYPT_SHAKE256_Ctx; 71 // new context 72 CRYPT_SHA3_224_Ctx *CRYPT_SHA3_224_NewCtx(void); 73 CRYPT_SHA3_256_Ctx *CRYPT_SHA3_256_NewCtx(void); 74 CRYPT_SHA3_384_Ctx *CRYPT_SHA3_384_NewCtx(void); 75 CRYPT_SHA3_512_Ctx *CRYPT_SHA3_512_NewCtx(void); 76 CRYPT_SHAKE128_Ctx *CRYPT_SHAKE128_NewCtx(void); 77 CRYPT_SHAKE256_Ctx *CRYPT_SHAKE256_NewCtx(void); 78 79 // free context 80 void CRYPT_SHA3_224_FreeCtx(CRYPT_SHA3_224_Ctx* ctx); 81 void CRYPT_SHA3_256_FreeCtx(CRYPT_SHA3_256_Ctx* ctx); 82 void CRYPT_SHA3_384_FreeCtx(CRYPT_SHA3_384_Ctx* ctx); 83 void CRYPT_SHA3_512_FreeCtx(CRYPT_SHA3_512_Ctx* ctx); 84 void CRYPT_SHAKE128_FreeCtx(CRYPT_SHAKE128_Ctx* ctx); 85 void CRYPT_SHAKE256_FreeCtx(CRYPT_SHAKE256_Ctx* ctx); 86 87 // free context 88 89 // Initialize the context 90 int32_t CRYPT_SHA3_224_Init(CRYPT_SHA3_224_Ctx *ctx, BSL_Param *param); 91 92 int32_t CRYPT_SHA3_256_Init(CRYPT_SHA3_256_Ctx *ctx, BSL_Param *param); 93 94 int32_t CRYPT_SHA3_384_Init(CRYPT_SHA3_384_Ctx *ctx, BSL_Param *param); 95 96 int32_t CRYPT_SHA3_512_Init(CRYPT_SHA3_512_Ctx *ctx, BSL_Param *param); 97 int32_t CRYPT_SHAKE128_Init(CRYPT_SHAKE128_Ctx *ctx, BSL_Param *param); 98 int32_t CRYPT_SHAKE256_Init(CRYPT_SHAKE256_Ctx *ctx, BSL_Param *param); 99 100 // Data update API 101 int32_t CRYPT_SHA3_224_Update(CRYPT_SHA3_224_Ctx *ctx, const uint8_t *in, uint32_t len); 102 103 int32_t CRYPT_SHA3_256_Update(CRYPT_SHA3_256_Ctx *ctx, const uint8_t *in, uint32_t len); 104 105 int32_t CRYPT_SHA3_384_Update(CRYPT_SHA3_384_Ctx *ctx, const uint8_t *in, uint32_t len); 106 107 int32_t CRYPT_SHA3_512_Update(CRYPT_SHA3_512_Ctx *ctx, const uint8_t *in, uint32_t len); 108 int32_t CRYPT_SHAKE128_Update(CRYPT_SHAKE128_Ctx *ctx, const uint8_t *in, uint32_t len); 109 int32_t CRYPT_SHAKE256_Update(CRYPT_SHAKE256_Ctx *ctx, const uint8_t *in, uint32_t len); 110 111 // Padding and output the digest value 112 int32_t CRYPT_SHA3_224_Final(CRYPT_SHA3_224_Ctx *ctx, uint8_t *out, uint32_t *len); 113 114 int32_t CRYPT_SHA3_256_Final(CRYPT_SHA3_256_Ctx *ctx, uint8_t *out, uint32_t *len); 115 116 int32_t CRYPT_SHA3_384_Final(CRYPT_SHA3_384_Ctx *ctx, uint8_t *out, uint32_t *len); 117 118 int32_t CRYPT_SHA3_512_Final(CRYPT_SHA3_512_Ctx *ctx, uint8_t *out, uint32_t *len); 119 int32_t CRYPT_SHAKE128_Final(CRYPT_SHAKE128_Ctx *ctx, uint8_t *out, uint32_t *len); 120 int32_t CRYPT_SHAKE256_Final(CRYPT_SHAKE256_Ctx *ctx, uint8_t *out, uint32_t *len); 121 122 int32_t CRYPT_SHAKE128_Squeeze(CRYPT_SHAKE128_Ctx *ctx, uint8_t *out, uint32_t len); 123 int32_t CRYPT_SHAKE256_Squeeze(CRYPT_SHAKE256_Ctx *ctx, uint8_t *out, uint32_t len); 124 125 // Clear the context 126 void CRYPT_SHA3_224_Deinit(CRYPT_SHA3_224_Ctx *ctx); 127 128 void CRYPT_SHA3_256_Deinit(CRYPT_SHA3_256_Ctx *ctx); 129 130 void CRYPT_SHA3_384_Deinit(CRYPT_SHA3_384_Ctx *ctx); 131 132 void CRYPT_SHA3_512_Deinit(CRYPT_SHA3_512_Ctx *ctx); 133 void CRYPT_SHAKE128_Deinit(CRYPT_SHAKE128_Ctx *ctx); 134 void CRYPT_SHAKE256_Deinit(CRYPT_SHAKE256_Ctx *ctx); 135 136 // Copy the context 137 int32_t CRYPT_SHA3_224_CopyCtx(CRYPT_SHA3_224_Ctx *dst, const CRYPT_SHA3_224_Ctx *src); 138 int32_t CRYPT_SHA3_256_CopyCtx(CRYPT_SHA3_256_Ctx *dst, const CRYPT_SHA3_256_Ctx *src); 139 int32_t CRYPT_SHA3_384_CopyCtx(CRYPT_SHA3_384_Ctx *dst, const CRYPT_SHA3_384_Ctx *src); 140 int32_t CRYPT_SHA3_512_CopyCtx(CRYPT_SHA3_512_Ctx *dst, const CRYPT_SHA3_512_Ctx *src); 141 int32_t CRYPT_SHAKE128_CopyCtx(CRYPT_SHA3_384_Ctx *dst, const CRYPT_SHA3_384_Ctx *src); 142 int32_t CRYPT_SHAKE256_CopyCtx(CRYPT_SHA3_512_Ctx *dst, const CRYPT_SHA3_512_Ctx *src); 143 144 // Dup the context 145 CRYPT_SHA3_224_Ctx *CRYPT_SHA3_224_DupCtx(const CRYPT_SHA3_224_Ctx *src); 146 CRYPT_SHA3_256_Ctx *CRYPT_SHA3_256_DupCtx(const CRYPT_SHA3_256_Ctx *src); 147 CRYPT_SHA3_384_Ctx *CRYPT_SHA3_384_DupCtx(const CRYPT_SHA3_384_Ctx *src); 148 CRYPT_SHA3_512_Ctx *CRYPT_SHA3_512_DupCtx(const CRYPT_SHA3_512_Ctx *src); 149 CRYPT_SHA3_384_Ctx *CRYPT_SHAKE128_DupCtx(const CRYPT_SHA3_384_Ctx *src); 150 CRYPT_SHA3_512_Ctx *CRYPT_SHAKE256_DupCtx(const CRYPT_SHA3_512_Ctx *src); 151 #ifdef __cplusplus 152 } 153 #endif 154 155 #endif // HITLS_CRYPTO_SHA3 156 157 #endif // CRYPT_SHA3_H 158