1 /* 2 * Copyright (c) 2020-2021 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 __BASE_H__ 17 #define __BASE_H__ 18 19 #include <stdint.h> 20 21 #ifdef _HC_DEBUG_ 22 #include <assert.h> 23 24 /* for memory leak check, only used for input params checking */ 25 #define check_ptr_return_val(d_ptr, d_val) \ 26 do { \ 27 assert((d_ptr) != NULL); \ 28 (void)(d_val); \ 29 } while (0) 30 31 #define check_ptr_return(d_ptr) assert((d_ptr) != NULL) 32 33 #define check_num_return_val(d_num, d_val) \ 34 do { \ 35 assert((d_num) != 0); \ 36 (void)(d_val); \ 37 } while (0) 38 39 #else /* _HC_DEBUG_ */ 40 /* only used for input params checking */ 41 #define check_ptr_return_val(d_ptr, d_val) \ 42 do { \ 43 if ((d_ptr) == NULL) { \ 44 return (d_val); \ 45 } \ 46 } while (0) 47 48 #define check_ptr_return(d_ptr) \ 49 do { \ 50 if ((d_ptr) == NULL) { \ 51 return; \ 52 } \ 53 } while (0) 54 55 #define check_num_return_val(d_num, d_val) \ 56 do { \ 57 if ((d_num) == 0) { \ 58 return (d_val); \ 59 } \ 60 } while (0) 61 62 #endif /* _HC_DEBUG_ */ 63 64 #define HC_RAMDOM_MAX_LEN 32 65 #define HC_BASE_LEN 32 66 #define HC_EXPONENT_LEN 32 67 #define HC_RAMDOM_VALUE_MAX_LEN 1024 68 #define HC_HMAC_LEN 32 69 #define HC_HKDF_LEN 512 70 #define HC_HKDF_SECRET_LEN 32 71 72 #define HC_STS_SHARED_SECRET_LENGTH 32 73 #define HC_SERVICE_ID_BUFF_LEN 64 74 75 #if !defined(_SCANTY_MEMORY_) 76 #define HC_BIG_PRIME_MAX_LEN 384 77 #else /* _SCANTY_MEMORY_ */ 78 #define HC_BIG_PRIME_MAX_LEN 256 79 #endif /* _SCANTY_MEMORY_ */ 80 81 #define HC_BIG_PRIME_MAX_LEN_384 384 82 #define HC_BIG_PRIME_MAX_LEN_256 256 83 84 /* SHA256 */ 85 #define HC_SHA256_LEN 32 86 #define HC_SHA512_LEN 64 87 88 /* KEY */ 89 #define HC_LT_PUBLIC_KEY_LEN 32 90 #define HC_LT_PRIVATE_KEY_LEN 64 91 #define HC_ST_PUBLIC_KEY_LEN 32 92 #define HC_ST_PRIVATE_KEY_LEN 32 93 94 #define HC_SIGNATURE_LEN 64 95 #define HC_KEY_ALIAS_MAX_LEN 64 96 97 /* AES */ 98 #define HC_AES_ADD_LEN 64 99 100 /* nonce */ 101 #define HC_AES_GCM_NONCE_LEN 12 102 103 #define BYTE_TO_HEX_OPER_LENGTH 2 104 #define VERSION_LENGTH 30 105 106 #define HC_MAX_KEY_TYPE_NUM 7 107 #define HC_MAX_PAIR_TYPE_NUM 2 108 #define HC_KEY_TYPE_PAIR_LEN 2 109 110 #define AUTH_FORM 0 111 112 struct var_buffer { 113 uint32_t length; 114 uint8_t data[0]; 115 }; 116 117 struct big_num { 118 uint32_t length; 119 uint8_t big_num[HC_BIG_PRIME_MAX_LEN]; 120 }; 121 122 struct ltsk { 123 uint32_t length; 124 uint8_t ltsk[HC_LT_PRIVATE_KEY_LEN]; 125 }; 126 127 struct ltpk { 128 uint32_t length; 129 uint8_t ltpk[HC_LT_PUBLIC_KEY_LEN]; 130 }; 131 132 /* temp public key */ 133 struct stpk { 134 uint32_t length; 135 uint8_t stpk[HC_ST_PUBLIC_KEY_LEN]; 136 }; 137 138 /* temp private key */ 139 struct stsk { 140 uint32_t length; 141 uint8_t stsk[HC_ST_PRIVATE_KEY_LEN]; 142 }; 143 144 /* long store key pair */ 145 struct lt_key_pair { 146 struct ltpk lt_public_key; 147 struct ltsk lt_private_key; 148 }; 149 150 /* temp key pair */ 151 struct st_key_pair { 152 struct stpk st_public_key; 153 struct stsk st_private_key; 154 }; 155 156 struct signature { 157 uint32_t length; 158 uint8_t signature[HC_SIGNATURE_LEN]; 159 }; 160 161 struct sts_shared_secret { 162 uint32_t length; 163 uint8_t sts_shared_secret[HC_STS_SHARED_SECRET_LENGTH]; 164 }; 165 166 struct service_id { 167 uint32_t length; 168 uint8_t service_id[HC_SERVICE_ID_BUFF_LEN]; 169 }; 170 171 struct sha256_value { 172 uint32_t length; 173 uint8_t sha256_value[HC_SHA256_LEN]; 174 }; 175 176 struct sha512_value { 177 uint32_t length; 178 uint8_t sha512_value[HC_SHA512_LEN]; 179 }; 180 181 struct aes_aad { 182 uint32_t length; 183 uint8_t aad[HC_AES_ADD_LEN]; 184 }; 185 186 struct random_value { 187 uint32_t length; 188 uint8_t random_value[HC_RAMDOM_MAX_LEN]; 189 }; 190 191 struct base { 192 uint32_t length; 193 uint8_t base[HC_BASE_LEN]; 194 }; 195 196 struct exponent { 197 uint32_t length; 198 uint8_t exp[HC_EXPONENT_LEN]; 199 }; 200 201 struct rand_value { 202 uint32_t length; 203 uint8_t ramdom_value[HC_RAMDOM_VALUE_MAX_LEN]; 204 }; 205 206 struct hmac { 207 uint32_t length; 208 uint8_t hmac[HC_HMAC_LEN]; 209 }; 210 211 struct hkdf { 212 uint32_t length; 213 uint8_t hkdf[HC_HKDF_LEN]; 214 }; 215 216 struct message { 217 uint16_t msg_code; 218 uint16_t rsv; 219 void *payload; 220 }; 221 222 struct inform_message_data { 223 uint16_t error_code; 224 }; 225 226 enum message_code { 227 INVALID_MESSAGE = 0, 228 229 PAKE_REQUEST = 0x0001, 230 PAKE_CLIENT_CONFIRM = 0x0002, 231 EXCHANGE_REQUEST = 0x0003, 232 AUTH_START_REQUEST = 0x0011, 233 AUTH_ACK_REQUEST = 0x0012, 234 ADD_AUTHINFO_REQUEST = 0x0023, 235 REMOVE_AUTHINFO_REQUEST = 0x0033, 236 SEC_CLONE_START_REQUEST = 0x0041, 237 SEC_CLONE_ACK_REQUEST = 0x0042, 238 239 PAKE_RESPONSE = 0x8001, 240 PAKE_SERVER_CONFIRM_RESPONSE = 0x8002, 241 EXCHANGE_RESPONSE = 0x8003, 242 AUTH_START_RESPONSE = 0x8011, 243 AUTH_ACK_RESPONSE = 0x8012, 244 ADD_AUTHINFO_RESPONSE = 0x8023, 245 REMOVE_AUTHINFO_RESPONSE = 0x8033, 246 SEC_CLONE_START_RESPONSE = 0x8041, 247 SEC_CLONE_ACK_RESPONSE = 0x8042, 248 249 INFORM_MESSAGE = 0x8080, 250 }; 251 252 enum json_object_data_type { 253 JSON_STRING_DATA = 0, 254 JSON_OBJECT_DATA = 1 255 }; 256 257 enum large_prime_number_type { 258 NUM_LEN_384 = 0, /* big prime number type is 384 length */ 259 NUM_LEN_256 /* big prime number type is 256 length */ 260 }; 261 262 #endif /* __BASE_H__ */ 263