1 /* 2 * Copyright (c) 2021-2022 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 digest; 66 uint32_t purpose; 67 uint32_t pssSaltLenType; 68 /* 69 * Different algorithms correspond to different structures,for example: 70 * struct HksAeadParam for aead; 71 * struct HksCipherParam for cipher; 72 */ 73 void *algParam; 74 }; 75 76 struct KeyMaterialRsa { 77 enum HksKeyAlg keyAlg; 78 uint32_t keySize; 79 uint32_t nSize; 80 uint32_t eSize; 81 uint32_t dSize; 82 }; 83 84 struct KeyMaterialEcc { 85 enum HksKeyAlg keyAlg; 86 uint32_t keySize; 87 uint32_t xSize; 88 uint32_t ySize; 89 uint32_t zSize; 90 }; 91 92 struct KeyMaterialDsa { 93 enum HksKeyAlg keyAlg; 94 uint32_t keySize; 95 uint32_t xSize; 96 uint32_t ySize; 97 uint32_t pSize; 98 uint32_t qSize; 99 uint32_t gSize; 100 }; 101 102 struct KeyMaterialDh { 103 enum HksKeyAlg keyAlg; 104 uint32_t keySize; 105 uint32_t pubKeySize; 106 uint32_t priKeySize; 107 uint32_t reserved; 108 }; 109 110 struct KeyMaterial25519 { 111 enum HksKeyAlg keyAlg; 112 uint32_t keySize; 113 uint32_t pubKeySize; 114 uint32_t priKeySize; 115 uint32_t reserved; 116 }; 117 118 typedef int32_t (*GetMainKey)(const struct HksBlob *, struct HksBlob *); 119 120 typedef int32_t (*GenerateKey)(const struct HksKeySpec *, struct HksBlob *); 121 122 typedef int32_t (*PubKey)(const struct HksBlob *, struct HksBlob *); 123 124 typedef int32_t (*DeriveKey)(const struct HksBlob *, const struct HksKeySpec *, struct HksBlob *); 125 126 typedef int32_t (*FillRandom)(struct HksBlob *); 127 128 typedef int32_t (*AgreeKey)(const struct HksBlob *, const struct HksBlob *, const struct HksKeySpec *, 129 struct HksBlob *); 130 131 typedef int32_t (*Sign)(const struct HksBlob *, const struct HksUsageSpec *, const struct HksBlob *, 132 struct HksBlob *); 133 134 typedef int32_t (*Verify)(const struct HksBlob *, const struct HksUsageSpec *, const struct HksBlob *, 135 const struct HksBlob *); 136 137 typedef int32_t (*Hmac)(const struct HksBlob *, uint32_t, const struct HksBlob *, struct HksBlob *); 138 139 typedef int32_t (*HmacInit)(void **, const struct HksBlob *, uint32_t); 140 141 typedef int32_t (*HmacUpdate)(void *, const struct HksBlob *); 142 143 typedef int32_t (*HmacFinal)(void **, const struct HksBlob *, struct HksBlob *); 144 145 typedef int32_t (*Hash)(uint32_t, const struct HksBlob *, struct HksBlob *); 146 147 typedef int32_t (*HashInit)(void **, uint32_t); 148 149 typedef int32_t (*HashUpdate)(void *, const struct HksBlob *); 150 151 typedef int32_t (*HashFinal)(void **, const struct HksBlob *, struct HksBlob *); 152 153 typedef int32_t (*Encrypt)(const struct HksBlob *, const struct HksUsageSpec *, 154 const struct HksBlob *, struct HksBlob *, struct HksBlob *); 155 156 typedef int32_t (*EncryptInit)(void **, const struct HksBlob *, const struct HksUsageSpec *, const bool); 157 158 typedef int32_t (*EncryptUpdate)(void *, const struct HksBlob *, struct HksBlob *, const bool); 159 160 typedef int32_t (*EncryptFinal)(void **, const struct HksBlob *, struct HksBlob *, struct HksBlob *, const bool); 161 162 typedef int32_t (*Decrypt)(const struct HksBlob *, const struct HksUsageSpec *, 163 const struct HksBlob *, struct HksBlob *); 164 165 typedef int32_t (*DecryptInit)(void **, const struct HksBlob *, const struct HksUsageSpec *, const bool); 166 167 typedef int32_t (*DecryptUpdate)(void *, const struct HksBlob *, struct HksBlob *, const bool); 168 169 typedef int32_t (*DecryptFinal)(void **, const struct HksBlob *, struct HksBlob *, struct HksBlob *, const bool); 170 171 typedef int32_t (*BnExpMod)(struct HksBlob *, const struct HksBlob *, 172 const struct HksBlob *, const struct HksBlob *); 173 174 typedef void (*FreeCtx)(void **); 175 176 int32_t HksCryptoHalGetMainKey(const struct HksBlob *message, struct HksBlob *mainKey); 177 178 int32_t HksCryptoHalGenerateKey(const struct HksKeySpec *spec, struct HksBlob *key); 179 180 int32_t HksCryptoHalGetPubKey(const struct HksBlob *keyIn, struct HksBlob *keyOut); 181 182 int32_t HksCryptoHalDeriveKey(const struct HksBlob *mainKey, const struct HksKeySpec *derivationSpec, 183 struct HksBlob *derivedKey); 184 185 int32_t HksCryptoHalFillRandom(struct HksBlob *randomData); 186 187 int32_t HksCryptoHalFillPrivRandom(struct HksBlob *randomData); 188 189 int32_t HksCryptoHalAddEntropy(const struct HksBlob *entropy); 190 191 int32_t HksCryptoHalAgreeKey(const struct HksBlob *nativeKey, const struct HksBlob *pubKey, 192 const struct HksKeySpec *spec, struct HksBlob *sharedKey); 193 194 int32_t HksCryptoHalSign(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 195 const struct HksBlob *message, struct HksBlob *signature); 196 197 int32_t HksCryptoHalVerify(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 198 const struct HksBlob *message, const struct HksBlob *signature); 199 200 int32_t HksCryptoHalHmacInit(const struct HksBlob *key, uint32_t digestAlg, void **ctx); 201 202 int32_t HksCryptoHalHmacUpdate(const struct HksBlob *chunk, void *ctx); 203 204 int32_t HksCryptoHalHmacFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *mac); 205 206 void HksCryptoHalHmacFreeCtx(void **ctx); 207 208 int32_t HksCryptoHalHmac(const struct HksBlob *key, uint32_t digestAlg, const struct HksBlob *msg, 209 struct HksBlob *mac); 210 211 int32_t HksCryptoHalHashInit(uint32_t alg, void **ctx); 212 213 int32_t HksCryptoHalHashUpdate(const struct HksBlob *msg, void *ctx); 214 215 int32_t HksCryptoHalHashFinal(const struct HksBlob *msg, void **ctx, struct HksBlob *hash); 216 217 void HksCryptoHalHashFreeCtx(void **ctx); 218 219 int32_t HksCryptoHalHash(uint32_t alg, const struct HksBlob *msg, struct HksBlob *hash); 220 221 int32_t HksCryptoHalEncryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx); 222 223 int32_t HksCryptoHalEncryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, 224 const uint32_t algtype); 225 226 int32_t HksCryptoHalEncryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText, 227 struct HksBlob *tagAead, const uint32_t algtype); 228 229 void HksCryptoHalEncryptFreeCtx(void **ctx, const uint32_t algtype); 230 231 int32_t HksCryptoHalEncrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 232 const struct HksBlob *message, struct HksBlob *cipherText, struct HksBlob *tagAead); 233 234 int32_t HksCryptoHalDecryptInit(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, void **ctx); 235 236 int32_t HksCryptoHalDecryptUpdate(const struct HksBlob *message, void *ctx, struct HksBlob *out, 237 const uint32_t algtype); 238 239 int32_t HksCryptoHalDecryptFinal(const struct HksBlob *message, void **ctx, struct HksBlob *cipherText, 240 struct HksBlob *tagAead, const uint32_t algtype); 241 242 void HksCryptoHalDecryptFreeCtx(void **ctx, const uint32_t algtype); 243 244 int32_t HksCryptoHalDecrypt(const struct HksBlob *key, const struct HksUsageSpec *usageSpec, 245 const struct HksBlob *message, struct HksBlob *cipherText); 246 247 int32_t HksCryptoHalBnExpMod(struct HksBlob *x, const struct HksBlob *a, 248 const struct HksBlob *e, const struct HksBlob *n); 249 250 int32_t HksCryptoHalInit(void); 251 252 #ifdef __cplusplus 253 } 254 #endif 255 256 #endif /* HKS_CRYPTO_HAL_H */ 257