1 /* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 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 16 #ifndef HKS_TYPES_H 17 #define HKS_TYPES_H 18 19 #include <stdint.h> 20 #include <stdlib.h> 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 #ifndef HKS_API_PUBLIC 27 #if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) 28 #ifdef HKS_DLL_EXPORT 29 #define HKS_DLL_API_PUBLIC __declspec(dllexport) 30 #else 31 #define HKS_DLL_API_PUBLIC __declspec(dllimport) 32 #endif 33 #else 34 #define HKS_DLL_API_PUBLIC __attribute__ ((visibility("default"))) 35 #endif 36 #else 37 #define HKS_DLL_API_PUBLIC __attribute__ ((visibility("default"))) 38 #endif 39 40 #define HKS_SDK_VERSION "1.0.0.10" 41 #define HKS_BOOL_FALSE 0 42 #define HKS_BOOL_TRUE 1 43 #define HKS_ALIAS_MAX_SIZE 64 44 #define HKS_SALT_MAX_SIZE 16 45 #define HKS_NONCE_MIN_SIZE 7 46 #define HKS_KEY_BYTES_CURVE25519 32 47 #define HKS_RANDOM_MAX_LEN 1024 48 #define HKS_MAX_KEY_LEN_128 128 49 #define HKS_MAX_KEY_LEN_192 192 50 #define HKS_MAX_KEY_LEN_256 256 51 #define HKS_KEY_DERIVE_LEN 32 52 #define HKS_BINARY_OF_DEC 10 53 #define HKS_BINARY_OF_HEX 16 54 #define HKS_HASH256_MIN_OUT_SIZE 32 55 #define HKS_HASH512_MIN_OUT_SIZE 64 56 #define HKS_DERIVED_KEY_MIN_OUT_SIZE 16 57 #define HKS_BITS_PER_BYTES 8 58 #define HKS_SIGNATURE_MIN_SIZE 64 59 #define HKS_RSA2048_SIGNATURE_SIZE 256 60 #define HKS_PUBLIC_BYTES_ED25519 32 61 #define HKS_PRIVATE_BYTES_ED25519 64 62 #define HKS_KEY_PAIR_CIPHER_ED25519 80 63 #define HKS_HEADER_HASH_SIZE 64 64 #define HKS_AUTH_ID_MAX_SIZE 64 65 #define HKS_KEY_LEN_RSA_KEYPAIR 2048 66 #define HKS_CHALLENGE_MIN_LEN 16 67 #define HKS_CHALLENGE_MAX_LEN 128 68 #define HKS_ENCRYPTED_KEY_TAG_LEN 16 69 #define HKS_ENCRYPTED_KEY_NONCE_LEN 16 70 #define HKS_ENCRYPTED_KEY_AAD_LEN 16 71 72 /* AES encrypt tag max length */ 73 #define HKS_ENCRYPT_MAX_TAG_SIZE ((uint8_t)32) 74 75 /* Data blob type and related macros */ 76 #define HKS_BLOB_TYPE_RAW ((uint8_t)0x00) 77 #define HKS_BLOB_TYPE_ALIAS ((uint8_t)0x01) 78 #define HKS_BLOB_TYPE_KEY ((uint8_t)0x02) 79 #define HKS_BLOB_TYPE_ENCRYPTED_KEY ((uint8_t)0x03) 80 #define HKS_BLOB_TYPE_MESSAGE ((uint8_t)0x04) 81 #define HKS_BLOB_TYPE_HASH ((uint8_t)0x05) 82 #define HKS_BLOB_TYPE_MAC ((uint8_t)0x06) 83 #define HKS_BLOB_TYPE_LABEL ((uint8_t)0x07) 84 #define HKS_BLOB_TYPE_SIGNATURE ((uint8_t)0x08) 85 #define HKS_BLOB_TYPE_IV ((uint8_t)0x09) 86 #define HKS_BLOB_TYPE_AAD ((uint8_t)0x0a) 87 #define HKS_BLOB_TYPE_SALT ((uint8_t)0x0b) 88 #define HKS_BLOB_TYPE_PLAIN_TEXT ((uint8_t)0x0c) 89 #define HKS_BLOB_TYPE_CIPHER_TEXT ((uint8_t)0x0d) 90 #define HKS_BLOB_TYPE_MATERIAL ((uint8_t)0x0e) 91 #define HKS_BLOB_TYPE_AUTH_ID ((uint8_t)0x10) 92 #define HKS_BLOB_TYPE_BUFFER ((uint8_t)0x12) 93 94 struct hks_blob { 95 uint8_t type; 96 uint8_t *data; 97 uint32_t size; 98 }; 99 100 /* HKS_ECC_CURVE_CURVE25519 */ 101 #define HKS_ECC_CURVE_CURVE25519 ((uint16_t)0x001d) 102 103 #define HKS_ECC_CURVE_ED25519 ((uint16_t)0x8001) 104 105 #define HKS_KEY_TYPE_RSA_PUBLIC_KEY ((uint32_t)0x60010000) 106 107 #define HKS_KEY_TYPE_RSA_KEYPAIR ((uint32_t)0x70010000) 108 109 #define HKS_KEY_TYPE_ECC_PUBLIC_KEY_BASE ((uint32_t)0x60030000) 110 111 #define HKS_KEY_TYPE_ECC_KEYPAIR_BASE ((uint32_t)0x70030000) 112 113 #define HKS_KEY_TYPE_ECC_CURVE_MASK ((uint32_t)0x0000ffff) 114 115 #define hks_key_type_ecc_public_key(curve) \ 116 (HKS_KEY_TYPE_ECC_PUBLIC_KEY_BASE | (curve)) 117 118 #define hks_key_type_ecc_key_pair(curve) \ 119 (HKS_KEY_TYPE_ECC_KEYPAIR_BASE | (curve)) 120 121 #define HKS_KEY_TYPE_ECC_PUBLIC_KEY_CURVE25519 \ 122 (hks_key_type_ecc_public_key(HKS_ECC_CURVE_CURVE25519)) 123 124 #define HKS_KEY_TYPE_ECC_KEYPAIR_CURVE25519 \ 125 (hks_key_type_ecc_key_pair(HKS_ECC_CURVE_CURVE25519)) 126 127 #define HKS_KEY_TYPE_EDDSA_PUBLIC_KEY_BASE ((uint32_t)0xe0010000) 128 129 #define HKS_KEY_TYPE_EDDSA_KEYPAIR_BASE ((uint32_t)0xf0010000) 130 131 #define hks_key_type_eddsa_public_key(curve) \ 132 (HKS_KEY_TYPE_EDDSA_PUBLIC_KEY_BASE | (curve)) 133 134 #define hks_key_type_eddsa_key_pair(curve) \ 135 (HKS_KEY_TYPE_EDDSA_KEYPAIR_BASE | (curve)) 136 137 #define HKS_KEY_TYPE_EDDSA_PUBLIC_KEY_ED25519 \ 138 (hks_key_type_eddsa_public_key(HKS_ECC_CURVE_CURVE25519)) 139 140 #define HKS_KEY_TYPE_EDDSA_KEYPAIR_ED25519 \ 141 (hks_key_type_eddsa_key_pair(HKS_ECC_CURVE_CURVE25519)) 142 143 144 #define HKS_KEY_TYPE_AES ((uint32_t)0x40000001) 145 146 #define HKS_KEY_TYPE_HMAC ((uint32_t)0x51000000) 147 148 #define HKS_KEY_TYPE_DERIVE ((uint32_t)0x52000000) 149 150 /* key usage */ 151 #define HKS_KEY_USAGE_EXPORT ((uint32_t)0x00000001) 152 153 #define HKS_KEY_USAGE_ENCRYPT ((uint32_t)0x00000100) 154 155 #define HKS_KEY_USAGE_DECRYPT ((uint32_t)0x00000200) 156 157 #define HKS_KEY_USAGE_SIGN ((uint32_t)0x00000400) 158 159 #define HKS_KEY_USAGE_VERIFY ((uint32_t)0x00000800) 160 161 #define HKS_KEY_USAGE_DERIVE ((uint32_t)0x00001000) 162 163 #ifdef _SUPPORT_HKS_TEE_ 164 #define HKS_KEY_USAGE_WRAP ((uint32_t)0x00010000) 165 #define HKS_KEY_USAGE_UNWRAP ((uint32_t)0x00020000) 166 #endif 167 /* algorithm padding */ 168 #define HKS_PADDING_NONE ((uint32_t)0x00000000) 169 #define HKS_PADDING_PKCS7 ((uint32_t)0x00000001) 170 #define HKS_PADDING_PSS ((uint32_t)0x00000002) 171 #define HKS_PADDING_OAEP ((uint32_t)0x00000003) 172 #define HKS_PADDING_PKCS1_5 ((uint32_t)0x00000004) 173 #define HKS_PADDING_PKCS2_1 ((uint32_t)0x00000005) 174 175 /* mode */ 176 #define HKS_MODE_CBC ((uint32_t)0x04600101) 177 178 #define HKS_MODE_GCM ((uint32_t)0x06001002) 179 180 /* hash algorithms */ 181 #define HKS_ALG_DIGEST_NONE ((uint32_t)0x00000000) 182 183 #define HKS_ALG_HASH_MASK ((uint32_t)0x000000ff) 184 185 #define HKS_ALG_HASH_SHA_1 ((uint32_t)0x01000005) 186 187 #define HKS_ALG_HASH_SHA_256 ((uint32_t)0x01000009) 188 189 #define HKS_ALG_HASH_SHA_384 ((uint32_t)0x0100000a) 190 191 #define HKS_ALG_HASH_SHA_512 ((uint32_t)0x0100000b) 192 193 /* mac algorithms */ 194 #define HKS_ALG_HMAC_BASE ((uint32_t)0x02800000) 195 196 #define hks_alg_hmac(hash_alg) \ 197 (HKS_ALG_HMAC_BASE | ((hash_alg) & HKS_ALG_HASH_MASK)) 198 199 /* AEAD algorithms */ 200 #define HKS_ALG_CCM ((uint32_t)0x06001001) 201 #define HKS_ALG_GCM ((uint32_t)0x06001002) 202 #define HKS_ALG_CBC ((uint32_t)0x06001003) 203 204 /* HKDF algorithms */ 205 #define HKS_ALG_HKDF_BASE ((uint32_t)0x30000100) 206 #define hks_alg_hkdf(hash_alg) \ 207 (HKS_ALG_HKDF_BASE | ((hash_alg) & HKS_ALG_HASH_MASK)) 208 209 /* Key agreement/derivation algorithm */ 210 #define HKS_ALG_SELECT_RAW ((uint32_t)0x31000001) 211 #define HKS_ALG_ECDH_BASE ((uint32_t)0x22200000) 212 #define HKS_ALG_KEY_DERIVATION_MASK ((uint32_t)0x010fffff) 213 #define hks_alg_ecdh(kdf_alg) (HKS_ALG_ECDH_BASE | ((kdf_alg) & HKS_ALG_KEY_DERIVATION_MASK)) 214 215 struct hks_key_param { 216 uint32_t key_type; /* algorithm */ 217 uint32_t key_len; 218 uint32_t key_usage; /* usage */ 219 uint32_t key_pad; /* Fill mode */ 220 uint32_t key_mode; /* Group mode */ 221 uint32_t key_role; /* role */ 222 uint16_t key_domain; 223 struct hks_blob key_auth_id; /* auth id */ 224 }; 225 226 struct hks_crypt_param { 227 struct hks_blob nonce; /* Nonce value or iv vector */ 228 struct hks_blob aad; 229 }; 230 231 /* 232 * log interface 233 * tag - module name, default "HKS" 234 */ 235 typedef void(*hks_log_func)(const char *tag, const char *func_name, 236 const char *format, ...); 237 238 struct hks_log_f_group { 239 hks_log_func log_info; 240 hks_log_func log_warn; 241 hks_log_func log_error; 242 hks_log_func log_debug; 243 }; 244 245 struct hks_encrypt_material { 246 struct hks_blob *cipher_text; 247 struct hks_blob *nonce_blob; /* Nonce value or iv vector */ 248 struct hks_blob *aad_blob; 249 struct hks_blob *plain_text; 250 struct hks_storage_key_info *key_info; 251 uint32_t sealing_alg; 252 }; 253 254 enum hks_pki_cmd_type { 255 CMD_PKI_PROVISION = 1, 256 CMD_PKI_VERIFY, 257 CMD_EFUSE_ROOT_SALT_WRITE, 258 CMD_EFUSE_ROOT_SALT_VERIFY, 259 }; 260 261 #define HKS_MAX_CERT_NUM 3 262 #define HKS_MIN_SIGNATURE_SIZE 256 263 #define HKS_GET_CAPABILITES_SIZE 512 264 265 struct hks_cert_chain { 266 uint32_t count; 267 struct hks_blob *cert; /* cert array list */ 268 }; 269 270 struct hks_usage_spec { 271 uint32_t mode; 272 uint32_t padding; 273 uint32_t digest; 274 void *param; /* extended param */ 275 }; 276 277 #ifdef __cplusplus 278 } 279 #endif 280 281 #endif /* HKS_TYPES_H */ 282