1 /* 2 * Copyright (C) 2022 Huawei Technologies Co., Ltd. 3 * Licensed under the Mulan PSL v2. 4 * You can use this software according to the terms and conditions of the Mulan PSL v2. 5 * You may obtain a copy of Mulan PSL v2 at: 6 * http://license.coscl.org.cn/MulanPSL2 7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR 8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR 9 * PURPOSE. 10 * See the Mulan PSL v2 for more details. 11 */ 12 13 #ifndef __CRYPTO_INNER_DEFINES_H_ 14 #define __CRYPTO_INNER_DEFINES_H_ 15 16 #include <tee_crypto_api.h> 17 #define SIP_HASH_OUTPUT_LEN 8 18 #define MD5_OUTPUT_LEN 16 19 #define SHA1_OUTPUT_LEN 20 20 #define SHA224_OUTPUT_LEN 28 21 #define SHA256_OUTPUT_LEN 32 22 #define SHA384_OUTPUT_LEN 48 23 #define SHA512_OUTPUT_LEN 64 24 #define DIGEST_NO_ALLOC_CTX 0 25 #define DIGEST_ALLOC_CTX 1 26 #define PKCS5_PADDING_LEN 16 27 #define LISENCE_KEY 0 28 #define SESSION_KEY 1 29 #define MAX_MALLOC_LEN (500 * 1024 + 1) 30 #define MAX_DX_ASYMMETRIC_KEY_SIZE 3072 31 #define BIT_TO_BYTE 8 32 #define AES_IV_LEN 16 33 #define DES_IV_LEN 8 34 #define DES_BLOCK_SIZE 8 35 #define ECC_MAX_KEY_IN_BYTE 66 36 #define ECC_SPECIAL_KEY_LEN_IN_BYTE 66 37 #define ECC_SPECIAL_KEY_LEN_IN_BITS 521 38 #define BYTE_TO_WORD 4 39 #define AES_KEY_SIZE_128 16 40 #define AES_KEY_SIZE_192 24 41 #define AES_KEY_SIZE_256 32 42 #define AES_KEY_SIZE_512 64 43 #define DES_KEY_64 8 44 #define DES_KEY_128 16 45 #define DES_KEY_192 24 46 #define AES_MAC_LEN 16 47 #define DES_CMAC_LEN 8 48 #define RSA_V1_5_PADDING 11 49 #define DH_ATTRIBUTE_TOTAL 6 50 #define PBKDF2_ATTRIBUTE_TOTAL 3 51 #define CTX_OFF_SET 256 52 #define RSA_KEY_MIN 2048 53 #define RSA_KEY_MAX 4096 54 #define RSA_KEY_BLOCK 128 55 #define ECDSA_KEY_224 224 56 #define ECDSA_KEY_256 256 57 #define ECDSA_KEY_320 320 58 #define ECDSA_KEY_384 384 59 #define ECDSA_KEY_521 521 60 #define MALLOC_MAX_KEY_SIZE 1024 61 #define DH_MIN_KEY_SIZE 32 62 #define DH_MAX_KEY_SIZE 256 63 #define MAX_IV_LEN 32 64 #define RSA_MIN_KEY_SIZE 256 65 #define ECDH_MIN_KEY_SIZE 28 66 #define ECDSA_MIN_KEY_SIZE 28 67 #define PBKDF2_MIN_KEY_SIZE 14 68 #define PBKDF2_MAX_KEY_SIZE 1024 69 #define HMAC_MIN_KEY 64 70 #define HMAC_MAX_KEY (1024 * 8) 71 #define HMAC_SM3_MIN_KEY 80 72 #define HMAC_SM3_MAX_KEY 1024 73 #define SM4_KEY_SIZE 128 74 #define AES_BLOCK_SIZE 16 75 #define DH_BLOCK_SIZE 8 76 #define MAX_HMAC_LEN 64 77 #define RSA_PKCS1_PADDING_LEN 2 78 #define RSA_KEY_PAIR_ATTRIBUTE_COUNT 8 79 #define RSA_PRIV_KEY_ATTRIBUTE_COUNT 3 80 #define RSA_KEY_PAIR_ATTRIBUTE_COUNT_NO_CRT 3 81 #define BIT_NUMBER_SEVEN 7 82 #define BIT_TO_BYTE_MOVE_THREE 3 83 #define EC_KEY_FIX_BUFFER_LEN 66 84 #define RSA_PUB_KEY_ATTRIBUTE_COUNT 2 85 #define RSA_CRT_KEY_ATTRIBUTE_COUNT 5 86 #define RSA_CRT_KEY_BASE_INDEX 3 87 #define UINT32_SIZE 4 88 #define DH_MAX_KEY_SIZE 256 89 #define SM2_GROUP_NOSTANDARD 0x12 90 #define SM2_GROUP_NOSTANDARD_USER 2 91 #define MAX_EXTRA_PARAM_COUNT 10 92 #define MAX_KDF_PARAM_COUNT 9 93 #define UINT32_SHIFT_MAX 4 94 #define MAX_ATTR_LEN 8 95 #define AES_GCM_AAD_LEN 16 96 #define RSA_FACTOR_P_INDEX 0 97 #define RSA_FACTOR_Q_INDEX 1 98 #define RSA_CRT_DMP1 2 99 #define RSA_CRT_DMQ1 3 100 #define RSA_CRT_IQMP 4 101 #define MAX_MODE_NUM 2 102 #define ELEM_NUM(array) (sizeof(array) / sizeof((array)[0])) 103 104 struct min_size_of_algorithm { 105 uint32_t algorithm; 106 size_t output_lower_limit; 107 }; 108 109 const static struct min_size_of_algorithm g_output_lower_limit[] = { 110 { TEE_ALG_MD5, MD5_OUTPUT_LEN }, 111 { TEE_ALG_SHA1, SHA1_OUTPUT_LEN }, 112 { TEE_ALG_SHA224, SHA224_OUTPUT_LEN }, 113 { TEE_ALG_SHA256, SHA256_OUTPUT_LEN }, 114 { TEE_ALG_SHA384, SHA384_OUTPUT_LEN }, 115 { TEE_ALG_SHA512, SHA512_OUTPUT_LEN }, 116 { TEE_ALG_SM3, SHA256_OUTPUT_LEN }, 117 { TEE_ALG_HMAC_MD5, MD5_OUTPUT_LEN }, 118 { TEE_ALG_HMAC_SHA1, SHA1_OUTPUT_LEN }, 119 { TEE_ALG_HMAC_SHA224, SHA224_OUTPUT_LEN }, 120 { TEE_ALG_HMAC_SHA256, SHA256_OUTPUT_LEN }, 121 { TEE_ALG_HMAC_SHA384, SHA384_OUTPUT_LEN }, 122 { TEE_ALG_HMAC_SHA512, SHA512_OUTPUT_LEN }, 123 { TEE_ALG_HMAC_SM3, SHA256_OUTPUT_LEN }, 124 { TEE_ALG_AES_CMAC, AES_MAC_LEN }, 125 { TEE_ALG_AES_CBC_MAC_NOPAD, AES_MAC_LEN }, 126 { TEE_ALG_DES_CBC_MAC_NOPAD, DES_CMAC_LEN }, 127 { TEE_ALG_DES3_CBC_MAC_NOPAD, DES_CMAC_LEN }, 128 { TEE_ALG_SIP_HASH, SIP_HASH_OUTPUT_LEN }, 129 }; 130 131 typedef struct { 132 uint32_t crypto_flag; 133 uint32_t digestalloc_flag; 134 uint32_t cipher_update_len; 135 } crypto_hal_info; 136 137 TEE_Result change_hal_ret_to_gp(int32_t error); 138 uint32_t get_ecc_domain(uint32_t curve); 139 uint32_t get_sm2_domain(uint32_t curve); 140 TEE_Result get_tee_curve_by_keysize(uint32_t keySize, uint32_t *curve); 141 int32_t get_attr_index_by_id(uint32_t id, const TEE_Attribute *attrs, uint32_t attrCount); 142 TEE_Result crypto_lock_operation(TEE_OperationHandle operation); 143 void crypto_unlock_operation(TEE_OperationHandle operation); 144 TEE_Result crypto_lock_two_operation(TEE_OperationHandle op1, TEE_OperationHandle op2); 145 void crypyo_unlock_two_operation(TEE_OperationHandle op1, TEE_OperationHandle op2); 146 void free_operation_ctx(TEE_OperationHandle operation); 147 void fill_src_dest_param(operation_src_dest *src_dest_param, void *src_data_value, size_t src_len_value, 148 void *dest_data_value, size_t *dest_len_value); 149 #endif 150