1 /* 2 * Copyright (c) 2021-2023 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_CRYPTO_HAL_H 17 #define HKS_CRYPTO_HAL_H 18 19 #include "hks_type.h" 20 21 #ifdef __cplusplus 22 extern "C" { 23 #endif 24 25 enum HksKeyAlgMode { 26 HKS_ALGORITHM_RSA_MODE_CRT = 1, 27 HKS_ALGORITHM_RSA_MODE_NO_CRT = 2, 28 HKS_ALGORITHM_EC_MODE_ECDH = 3, 29 HKS_ALGORITHM_ED_MODE_SIG_VERIFY = 4, 30 HKS_ALGORITHM_ED_MODE_VERIFY = 5, 31 HKS_ALGORITHM_X25519_MODE = 6, 32 }; 33 34 struct HksKeySpec { 35 uint32_t algType; 36 uint32_t keyLen; 37 void *algParam; /* for example : struct HksKeyDerivationParam */ 38 }; 39 40 struct HksKeyDerivationParam { 41 struct HksBlob salt; 42 struct HksBlob info; 43 uint32_t iterations; 44 uint32_t digestAlg; 45 }; 46 47 struct HksAeadParam { 48 struct HksBlob nonce; 49 struct HksBlob aad; 50 union { 51 struct HksBlob tagDec; 52 uint32_t tagLenEnc; 53 }; 54 uint32_t payloadLen; 55 }; 56 57 struct HksCipherParam { 58 struct HksBlob iv; 59 }; 60 61 struct HksUsageSpec { 62 uint32_t algType; 63 uint32_t mode; 64 uint32_t padding; 65 uint32_t mgfDigest; 66 uint32_t digest; 67 uint32_t purpose; 68 uint32_t pssSaltLenType; 69 /* 70 * Different algorithms correspond to different structures,for example: 71 * struct HksAeadParam for aead; 72 * struct HksCipherParam for cipher; 73 */ 74 void *algParam; 75 }; 76 77 struct KeyMaterialRsa { 78 enum HksKeyAlg keyAlg; 79 uint32_t keySize; 80 uint32_t nSize; 81 uint32_t eSize; 82 uint32_t dSize; 83 }; 84 85 struct KeyMaterialEcc { 86 enum HksKeyAlg keyAlg; 87 uint32_t keySize; 88 uint32_t xSize; 89 uint32_t ySize; 90 uint32_t zSize; 91 }; 92 93 struct KeyMaterialDsa { 94 enum HksKeyAlg keyAlg; 95 uint32_t keySize; 96 uint32_t xSize; 97 uint32_t ySize; 98 uint32_t pSize; 99 uint32_t qSize; 100 uint32_t gSize; 101 }; 102 103 struct KeyMaterialDh { 104 enum HksKeyAlg keyAlg; 105 uint32_t keySize; 106 uint32_t pubKeySize; 107 uint32_t priKeySize; 108 uint32_t reserved; 109 }; 110 111 struct KeyMaterial25519 { 112 enum HksKeyAlg keyAlg; 113 uint32_t keySize; 114 uint32_t pubKeySize; 115 uint32_t priKeySize; 116 uint32_t reserved; 117 }; 118 119 typedef int32_t (*GetMainKey)(const struct HksBlob *, struct HksBlob *); 120 121 typedef int32_t (*GenerateKey)(const struct HksKeySpec *, struct HksBlob *); 122 123 typedef int32_t (*PubKey)(const struct HksBlob *, struct HksBlob *); 124 125 typedef int32_t (*DeriveKey)(const struct HksBlob *, const struct HksKeySpec *, struct HksBlob *); 126 127 typedef int32_t (*FillRandom)(struct HksBlob *); 128 129 typedef int32_t (*AgreeKey)(const struct HksBlob *, const struct HksBlob *, const struct HksKeySpec *, 130 struct HksBlob *); 131 132 typedef int32_t (*Sign)(const struct HksBlob *, const struct HksUsageSpec *, const struct HksBlob *, 133 struct HksBlob *); 134 135 typedef int32_t (*Verify)(const struct HksBlob *, const struct HksUsageSpec *, const struct HksBlob *, 136 const struct HksBlob *); 137 138 typedef int32_t (*Hmac)(const struct HksBlob *, uint32_t, const struct HksBlob *, struct HksBlob *); 139 140 typedef int32_t (*HmacInit)(void **, const struct HksBlob *, uint32_t); 141 142 typedef int32_t (*HmacUpdate)(void *, const struct HksBlob *); 143 144 typedef int32_t (*HmacFinal)(void **, const struct HksBlob *, struct HksBlob *); 145 146 typedef int32_t (*Hash)(uint32_t, const struct HksBlob *, struct HksBlob *); 147 148 typedef int32_t (*HashInit)(void **, uint32_t); 149 150 typedef int32_t (*HashUpdate)(void *, const struct HksBlob *); 151 152 typedef int32_t (*HashFinal)(void **, const struct HksBlob *, struct HksBlob *); 153 154 typedef int32_t (*Encrypt)(const struct HksBlob *, const struct HksUsageSpec *, 155 const struct HksBlob *, struct HksBlob *, struct HksBlob *); 156 157 typedef int32_t (*EncryptInit)(void **, const struct HksBlob *, const struct HksUsageSpec *, const bool); 158 159 typedef int32_t (*EncryptUpdate)(void *, const struct HksBlob *, struct HksBlob *, const bool); 160 161 typedef int32_t (*EncryptFinal)(void **, const struct HksBlob *, struct HksBlob *, struct HksBlob *, const bool); 162 163 typedef int32_t (*Decrypt)(const struct HksBlob *, const struct HksUsageSpec *, 164 const struct HksBlob *, struct HksBlob *); 165 166 typedef int32_t (*DecryptInit)(void **, const struct HksBlob *, const struct HksUsageSpec *, const bool); 167 168 typedef int32_t (*DecryptUpdate)(void *, const struct HksBlob *, struct HksBlob *, const bool); 169 170 typedef int32_t (*DecryptFinal)(void **, const struct HksBlob *, struct HksBlob *, struct HksBlob *, const bool); 171 172 typedef int32_t (*BnExpMod)(struct HksBlob *, const struct HksBlob *, 173 const struct HksBlob *, const struct HksBlob *); 174 175 typedef void (*FreeCtx)(void **); 176 177 int32_t HksCryptoHalGetMainKey(const struct HksBlob *message, struct HksBlob *mainKey); 178 179 int32_t HksCryptoHalGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key); 180 181 int32_t HksCryptoHalGetPubKey(const struct HksBlob *keyIn, struct HksBlob *keyOut); 182 183 int32_t HksCryptoHalDeriveKey(const struct HksBlob *mainKey, const struct HksKeySpec *derivationSpec, 184 struct HksBlob *derivedKey); 185 186 int32_t HksCryptoHalFillRandom(struct HksBlob *randomData); 187 188 int32_t HksCryptoHalFillPrivRandom(struct HksBlob *randomData); 189 190 int32_t HksCryptoHalAddEntropy(const struct HksBlob *entropy); 191 192 int32_t HksCryptoHalAgreeKey(const struct HksBlob *nativeKey, const struct HksBlob *pubKey, 193 const struct HksKeySpec *spec, struct HksBlob *sharedKey); 194 195 int32_t HksCryptoHalSign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 196 const struct HksBlob *message, struct HksBlob *signature); 197 198 int32_t HksCryptoHalVerify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 199 const struct HksBlob *message, const struct HksBlob *signature); 200 201 int32_t HksCryptoHalHmacInit(const struct HksBlob *key, uint32_t digestAlg, void **ctx); 202 203 int32_t HksCryptoHalHmacUpdate(const struct HksBlob *chunk, void *ctx); 204 205 int32_t HksCryptoHalHmacFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *mac); 206 207 void HksCryptoHalHmacFreeCtx(void **ctx); 208 209 int32_t HksCryptoHalHmac(const struct HksBlob *key, uint32_t digestAlg, const struct HksBlob *msg, 210 struct HksBlob *mac); 211 212 int32_t HksCryptoHalHashInit(uint32_t alg, void **ctx); 213 214 int32_t HksCryptoHalHashUpdate(const struct HksBlob *msg, void *ctx); 215 216 int32_t HksCryptoHalHashFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *hash); 217 218 void HksCryptoHalHashFreeCtx(void **ctx); 219 220 int32_t HksCryptoHalHash(uint32_t alg, const struct HksBlob *msg, struct HksBlob *hash); 221 222 int32_t HksCryptoHalEncryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx); 223 224 int32_t HksCryptoHalEncryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, 225 const uint32_t algtype); 226 227 int32_t HksCryptoHalEncryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText, 228 struct HksBlob *tagAead, const uint32_t algtype); 229 230 void HksCryptoHalEncryptFreeCtx(void **ctx, const uint32_t algtype); 231 232 int32_t HksCryptoHalEncrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 233 const struct HksBlob *message, struct HksBlob *cipherText, struct HksBlob *tagAead); 234 235 int32_t HksCryptoHalDecryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx); 236 237 int32_t HksCryptoHalDecryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, 238 const uint32_t algtype); 239 240 int32_t HksCryptoHalDecryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText, 241 struct HksBlob *tagAead, const uint32_t algtype); 242 243 void HksCryptoHalDecryptFreeCtx(void **ctx, const uint32_t algtype); 244 245 int32_t HksCryptoHalDecrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 246 const struct HksBlob *message, struct HksBlob *cipherText); 247 248 int32_t HksCryptoHalBnExpMod(struct HksBlob *x, const struct HksBlob *a, 249 const struct HksBlob *e, const struct HksBlob *n); 250 251 int32_t HksCryptoHalInit(void); 252 253 #ifdef __cplusplus 254 } 255 #endif 256 257 #endif /* HKS_CRYPTO_HAL_H */ 258