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 common def header. \n 16 * 17 * History: \n 18 * 2023-03-22, Create file. \n 19 */ 20 #ifndef CRYPTO_COMMON_DEF_H 21 #define CRYPTO_COMMON_DEF_H 22 23 #define MS_TO_US 1000 24 25 #define CRYPTO_BITS_IN_BYTE 8 26 27 #define CRYPTO_AES_IV_SIZE 16 28 #define CRYPTO_AES_IV_SIZE_IN_WORD 4 29 #define CRYPTO_WORD_WIDTH 4 30 #define CRYPTO_IV_LEN_IN_BYTES 16 31 32 #define CRYPTO_128_KEY_LEN 16 33 #define CRYPTO_192_KEY_LEN 24 34 #define CRYPTO_256_KEY_LEN 32 35 36 #define CRYPTO_AES_MAX_TAG_SIZE 16 37 #define CRYPTO_AES_CCM_N_LEN_IN_BYTES 16 38 39 #define CRYPTO_AES_BLOCK_SIZE_IN_BYTES 16 40 41 #define CRYPTO_BIT_3 3 42 #define CRYPTO_BIT_6 6 43 44 #define CRYPTO_AES_CCM_NQ_LEN 14 45 #define CRYPTO_SYMC_CCM_Q_LEN_2B 2 46 #define CRYPTO_SYMC_CCM_Q_LEN_3B 3 47 #define CRYPTO_SYMC_CCM_Q_LEN_4B 4 48 49 #define HASH_SHA1_BLOCK_SIZE 64 50 #define HASH_SHA224_BLOCK_SIZE 64 51 #define HASH_SHA256_BLOCK_SIZE 64 52 #define HASH_SHA384_BLOCK_SIZE 128 53 #define HASH_SHA512_BLOCK_SIZE 128 54 #define HASH_SM3_BLOCK_SIZE 64 55 56 #define HASH_SHA1_RESULT_SIZE 20 57 #define HASH_SHA224_RESULT_SIZE 28 58 #define HASH_SHA256_RESULT_SIZE 32 59 #define HASH_SHA384_RESULT_SIZE 48 60 #define HASH_SHA512_RESULT_SIZE 64 61 #define HASH_SM3_RESULT_SIZE 32 62 63 #define CRYPTO_GCM_SPECIAL_IV_BYTES 12 64 #define CRYPTO_AES_MAX_TAG_LEN 16 65 66 #define CRYPTO_CENC_IV_LEN 8 67 68 #define CRYPTO_TYPE_ENCRYPT 0x0 69 #define CRYPTO_TYPE_DECRYPT 0x1 70 #define CRYPTO_TYPE_DMA 0x2 71 72 #define CRYPTO_RSA_1024_LEN 128 73 #define CRYPTO_RSA_2048_LEN 256 74 #define CRYPTO_RSA_3072_LEN 384 75 #define CRYPTO_RSA_4096_LEN 512 76 77 /* Memory Limit. */ 78 #define CRYPTO_MAX_AAD_SIZE (4 * 1024) 79 #define CRYPTO_MAX_CRYPTO_SIZE (4 * 1024) 80 81 /* PBKDF2 Limits. */ 82 #define CRYPTO_PBKDF2_PASS_MAX_LENGTH 1024 83 #define CRYPTO_PBKDF2_SALT_MAX_LENGTH 1024 84 #define CRYPTO_PBKDF2_OUT_MAX_LENGTH 1024 85 #define CRYPTO_PBKDF2_MAX_COUNT 100000 86 87 /* Hash Limits. */ 88 #define CRYPTO_HASH_KEY_MAX_LENGTH 128 89 #define CRYPTO_HASH_MIN_LEN 32 90 #define CRYPTO_HASH_MAX_LEN 64 91 92 /* CENC Limits. */ 93 #define CRYPTO_SYMC_CENC_SUBSAMPLE_MAX_SIZE 200 94 95 /* SYMC Limits. */ 96 #define CRYPTO_SYMC_MULTI_PACK_MAX_SIZE 200 97 #define CRYPTO_SYMC_AAD_MAX_SIZE (4 * 1024) 98 99 /* PKE Limits. */ 100 #define CRYPTO_PKE_ECC_KEY_MIN_SIZE 32 101 #define CRYPTO_PKE_ECC_KEY_MAX_SIZE 72 102 #define CRYPTO_PKE_RSA_KEY_MIN_SIZE 256 103 #define CRYPTO_PKE_RSA_KEY_MAX_SIZE 512 104 105 /* TRNG Limits. */ 106 #define CRYPTO_MULTI_RANDOM_MAX_LENGTH 1024 107 108 /* HKDF Limits. */ 109 #define HKDF_EXPAND_INFO_MAX_LENGTH 1024 110 111 #define CRYPTO_PKE_MSG_MIN_SIZE 1 112 #define CRYPTO_PKE_MSG_MAX_SIZE (10 * 1024) 113 #define CRYPTO_PKE_SM2_ID_MIN_SIZE 1 114 #define CRYPTO_PKE_SM2_ID_MAX_SIZE 1024 115 #define CRYPTO_PKE_SM2_PLAIN_TEXT_MIN_SIZE 1 116 #define CRYPTO_PKE_SM2_PLAIN_TEXT_MAX_SIZE 1024 117 #define CRYPTO_PKE_RSA_LABLE_MAX_SIZE 1024 118 119 /* 120 * Handle Process 121 * Kapi Handle consists of the following parts: 122 *|<---- Reserved(15-bit) --->|<- Soft alg flag(1-bit) ->|<---- Module ID(8-bit) ---->|<---- Context Idx(8-bit)---->| 123 */ 124 #define crypto_set_soft_alg_flag(handle) ((handle) | 0x10000) 125 #define crypto_check_soft_alg_flag(handle) (((handle) >> 16) & 0x1) 126 #define crypto_get_soft_channel_id(handle) ((handle) & 0xff) 127 128 #define kapi_get_module_id(kapi_handle) (((kapi_handle) >> 8) & 0xff) 129 #define kapi_get_ctx_idx(kapi_handle) ((kapi_handle) & 0xff) 130 #define synthesize_kapi_handle(module_id, ctx_id) (((module_id) << 8) | (ctx_id)) 131 132 /* 133 *|<---- Reserved(8-bit) --->|<- keyslot_type(8-bit) ->|<---- Module ID(8-bit) ---->|<---- Context Idx(8-bit)---->| 134 * keyslot_type: 0 - MCIPHER, 1 - HMAC 135 */ 136 #define crypto_keyslot_compat_handle(keyslot_type, idx) \ 137 (((keyslot_type) << 16) | ((KAPI_KEYSLOT_MODULE_ID) << 8) | (idx)) 138 #define crypto_keyslot_compat_hmac_handle(idx) crypto_keyslot_compat_handle(1, idx) 139 #define crypto_keyslot_compat_mcipher_handle(idx) crypto_keyslot_compat_handle(0, idx) 140 #define crypto_keyslot_is_hmac(handle) ((((handle) >> 16) & 0xff) == 0x1) 141 #define crypto_keyslot_is_mcipher(handle) ((((handle) >> 16) & 0xff) == 0) 142 #define crypto_keyslot_get_idx(handle) ((handle) & 0xff) 143 #define crypto_keyslot_get_module_id(handle) (((handle) >> 8) & 0xff) 144 145 #endif 146