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