1 /** 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * 15 * Description: crypto hash struct header. \n 16 * 17 * History: \n 18 * 2023-03-22, Create file. \n 19 */ 20 #ifndef CRYPTO_HASH_STRUCT_H 21 #define CRYPTO_HASH_STRUCT_H 22 23 #include "crypto_common_struct.h" 24 25 /* 26 * Component of crypto_hash_type. 27 * is_hmac(4 bits) | alg(4 bits) | mode(4 bits) | max_message_length(4 bits) | block_size(4 bits) | result_size(12 bits) 28 * is_hmac: h'0 - hash 29 * h'1 - hmac 30 * alg: h'0 - sha1 31 * h'1 - sha2 32 * h'2 - sm3 33 * mode: h'0 - 224 34 * h'1 - 256 35 * h'2 - 384 36 * h'3 - 512 37 * max_message_length: 38 * h'6 - 2**6 -> 64, 最大消息 2**64 Bits. For SHA256. 39 * h'7 - 2**7 -> 128, 最大消息 2**128 Bits. For SHA384/SHA512. 40 * block_size: 41 * h'9 - 2**9 -> 512, Block Size 为 512 Bits. For SHA256. 42 * h'a - 2**10 -> 1024, Block Size 为 1024 Bits. For SHA384/SHA512. 43 * result_size: 44 * h'100 - 256, Result Size 为 256 Bits. For SHA256. 45 * h'180 - 384, Result Size 为 384 Bits. For SHA384. 46 * h'200 - 512, Result Size 为 512 Bits. For SHA512. 47 */ 48 #define compat_hash_type(is_hmac, alg, mode, max_message_length, block_size, result_size) \ 49 ((((is_hmac) & 0xF) << 28) | (((alg) & 0xF) << 24) | (((mode) & 0xF) << 20) | \ 50 (((max_message_length) & 0xF) << 16) | (((block_size) & 0xF) << 12) | ((result_size) & 0xFFF)) 51 52 #define CRYPTO_HASH_TYPE 0 53 #define CRYPTO_HMAC_TYPE 1 54 #define CRYPTO_IS_HMAC_MASK 0xF0000000 55 #define CRYPTO_IS_HMAC_SHIFT 28 56 57 #define CRYPTO_HASH_ALG_SHA1 0 58 #define CRYPTO_HASH_ALG_SHA2 1 59 #define CRYPTO_HASH_ALG_SM3 2 60 #define CRYPTO_HASH_ALG_MASK 0x0F000000 61 #define CRYPTO_HASH_ALG_SHIFT 24 62 63 #define CRYPTO_HASH_MODE_224 0 64 #define CRYPTO_HASH_MODE_256 1 65 #define CRYPTO_HASH_MODE_384 2 66 #define CRYPTO_HASH_MODE_512 3 67 #define CRYPTO_HASH_MODE_UNDEF 0xf 68 #define CRYPTO_HASH_MODE_MASK 0x00F00000 69 #define CRYPTO_HASH_MODE_SHIFT 20 70 71 #define CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT 0x6 72 #define CRYPTO_HASH_MAX_MESSAGE_LEN_128BIT 0x7 73 #define CRYPTO_HASH_MAX_MESSAGE_LEN_MASK 0x000F0000 74 #define CRYPTO_HASH_MAX_MESSAGE_LEN_SHIFT 16 75 76 #define CRYPTO_HASH_BLOCK_SIZE_512BIT 0x9 77 #define CRYPTO_HASH_BLOCK_SIZE_1024BIT 0xa 78 #define CRYPTO_HASH_BLOCK_SIZE_MASK 0x0000F000 79 #define CRYPTO_HASH_BLOCK_SIZE_SHIFT 12 80 81 #define CRYPTO_HASH_RESULT_SIZE_160BIT 0xa0 82 #define CRYPTO_HASH_RESULT_SIZE_224BIT 0xe0 83 #define CRYPTO_HASH_RESULT_SIZE_256BIT 0x100 84 #define CRYPTO_HASH_RESULT_SIZE_384BIT 0x180 85 #define CRYPTO_HASH_RESULT_SIZE_512BIT 0x200 86 #define CRYPTO_HASH_RESULT_SIZE_MASK 0x00000FFF 87 #define CRYPTO_HASH_RESULT_SIZE_SHIFT 0 88 89 #define CRYPTO_HASH_BLOCK_SIZE_20BYTE 0x14 90 #define CRYPTO_HASH_BLOCK_SIZE_32BYTE 0x20 91 #define CRYPTO_HASH_BLOCK_SIZE_64BYTE 0x40 92 93 #define crypto_hash_get_attr(value, mask, shift) (((td_u32)(value) & (td_u32)(mask)) >> (shift)) 94 #define crypto_hash_macth(value, mask, target, shift) (crypto_hash_get_attr(value, mask, shift) == (target)) 95 96 #define crypto_hash_get_alg(hash_type) \ 97 crypto_hash_get_attr(hash_type, CRYPTO_HASH_ALG_MASK, CRYPTO_HASH_ALG_SHIFT) 98 #define crypto_hash_get_mode(hash_type) \ 99 crypto_hash_get_attr(hash_type, CRYPTO_HASH_MODE_MASK, CRYPTO_HASH_MODE_SHIFT) 100 #define crypto_hash_is_hmac(hash_type) \ 101 crypto_hash_macth(hash_type, CRYPTO_IS_HMAC_MASK, CRYPTO_HMAC_TYPE, CRYPTO_IS_HMAC_SHIFT) 102 #define crypto_hash_get_message_len(hash_type) \ 103 (1 << crypto_hash_get_attr(hash_type, CRYPTO_HASH_MAX_MESSAGE_LEN_MASK, CRYPTO_HASH_MAX_MESSAGE_LEN_SHIFT)) 104 #define crypto_hash_get_block_size(hash_type) \ 105 (1 << crypto_hash_get_attr(hash_type, CRYPTO_HASH_BLOCK_SIZE_MASK, CRYPTO_HASH_BLOCK_SIZE_SHIFT)) 106 #define crypto_hash_get_result_size(hash_type) \ 107 crypto_hash_get_attr(hash_type, CRYPTO_HASH_RESULT_SIZE_MASK, CRYPTO_HASH_RESULT_SIZE_SHIFT) 108 109 typedef enum { 110 /* Hash. */ 111 CRYPTO_HASH_TYPE_SHA1 = compat_hash_type( 112 CRYPTO_HASH_TYPE, CRYPTO_HASH_ALG_SHA1, CRYPTO_HASH_MODE_UNDEF, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 113 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_160BIT 114 ), 115 CRYPTO_HASH_TYPE_SHA224 = compat_hash_type( 116 CRYPTO_HASH_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_224, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 117 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_224BIT 118 ), 119 CRYPTO_HASH_TYPE_SHA256 = compat_hash_type( 120 CRYPTO_HASH_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_256, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 121 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_256BIT 122 ), 123 CRYPTO_HASH_TYPE_SHA384 = compat_hash_type( 124 CRYPTO_HASH_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_384, CRYPTO_HASH_MAX_MESSAGE_LEN_128BIT, 125 CRYPTO_HASH_BLOCK_SIZE_1024BIT, CRYPTO_HASH_RESULT_SIZE_384BIT 126 ), 127 CRYPTO_HASH_TYPE_SHA512 = compat_hash_type( 128 CRYPTO_HASH_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_512, CRYPTO_HASH_MAX_MESSAGE_LEN_128BIT, 129 CRYPTO_HASH_BLOCK_SIZE_1024BIT, CRYPTO_HASH_RESULT_SIZE_512BIT 130 ), 131 CRYPTO_HASH_TYPE_SM3 = compat_hash_type( 132 CRYPTO_HASH_TYPE, CRYPTO_HASH_ALG_SM3, CRYPTO_HASH_MODE_256, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 133 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_256BIT 134 ), 135 136 /* HMAC. */ 137 CRYPTO_HASH_TYPE_HMAC_SHA1 = compat_hash_type( 138 CRYPTO_HMAC_TYPE, CRYPTO_HASH_ALG_SHA1, CRYPTO_HASH_MODE_UNDEF, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 139 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_160BIT 140 ), 141 CRYPTO_HASH_TYPE_HMAC_SHA224 = compat_hash_type( 142 CRYPTO_HMAC_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_224, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 143 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_224BIT 144 ), 145 CRYPTO_HASH_TYPE_HMAC_SHA256 = compat_hash_type( 146 CRYPTO_HMAC_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_256, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 147 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_256BIT 148 ), 149 CRYPTO_HASH_TYPE_HMAC_SHA384 = compat_hash_type( 150 CRYPTO_HMAC_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_384, CRYPTO_HASH_MAX_MESSAGE_LEN_128BIT, 151 CRYPTO_HASH_BLOCK_SIZE_1024BIT, CRYPTO_HASH_RESULT_SIZE_384BIT 152 ), 153 CRYPTO_HASH_TYPE_HMAC_SHA512 = compat_hash_type( 154 CRYPTO_HMAC_TYPE, CRYPTO_HASH_ALG_SHA2, CRYPTO_HASH_MODE_512, CRYPTO_HASH_MAX_MESSAGE_LEN_128BIT, 155 CRYPTO_HASH_BLOCK_SIZE_1024BIT, CRYPTO_HASH_RESULT_SIZE_512BIT 156 ), 157 CRYPTO_HASH_TYPE_HMAC_SM3 = compat_hash_type( 158 CRYPTO_HMAC_TYPE, CRYPTO_HASH_ALG_SM3, CRYPTO_HASH_MODE_256, CRYPTO_HASH_MAX_MESSAGE_LEN_64BIT, 159 CRYPTO_HASH_BLOCK_SIZE_512BIT, CRYPTO_HASH_RESULT_SIZE_256BIT 160 ), 161 162 CRYPTO_HASH_TYPE_INVALID = 0xffffffff, 163 } crypto_hash_type; 164 165 typedef struct { 166 td_u8 *key; 167 td_u32 key_len; 168 td_handle drv_keyslot_handle; 169 crypto_hash_type hash_type; 170 td_bool is_keyslot; 171 td_bool is_long_term; 172 } crypto_hash_attr; 173 174 /* Structure for HASH. */ 175 #define CRYPTO_HASH_RESULT_SIZE_MAX 64 // for SHA-512 176 #define CRYPTO_HASH_RESULT_SIZE_MAX_IN_WORD 16 // for SHA-512 177 #define CRYPTO_HASH_BLOCK_SIZE_MAX 128 // for SHA-512 178 179 typedef struct { 180 td_u32 length[2]; 181 td_u32 state[CRYPTO_HASH_RESULT_SIZE_MAX_IN_WORD]; 182 td_u32 tail_len; 183 crypto_hash_type hash_type; 184 #if defined(CRYPTO_SOFT_HMAC_SUPPORT) 185 td_u8 o_key_pad[CRYPTO_HASH_BLOCK_SIZE_MAX]; 186 td_u8 i_key_pad[CRYPTO_HASH_BLOCK_SIZE_MAX]; 187 #endif 188 td_u8 tail[CRYPTO_HASH_BLOCK_SIZE_MAX]; 189 } crypto_hash_clone_ctx; 190 191 typedef struct { 192 crypto_hash_type hmac_type; 193 td_u8 *salt; 194 td_u32 salt_length; 195 td_u8 *ikm; 196 td_u32 ikm_length; 197 } crypto_hkdf_extract_t; 198 199 typedef struct { 200 crypto_hash_type hmac_type; 201 td_u8 *prk; 202 td_u32 prk_length; 203 td_u8 *info; 204 td_u32 info_length; 205 } crypto_hkdf_expand_t; 206 207 typedef struct { 208 crypto_hash_type hmac_type; 209 td_u8 *salt; 210 td_u32 salt_length; 211 td_u8 *ikm; 212 td_u32 ikm_length; 213 td_u8 *info; 214 td_u32 info_length; 215 } crypto_hkdf_t; 216 217 #endif 218