1 /* 2 * Copyright (c) 2024 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 #ifndef CRYPTO_FFI_H 16 #define CRYPTO_FFI_H 17 18 #include "asy_key_generator_impl.h" 19 #include "asy_key_spec_generator_impl.h" 20 #include "cipher_impl.h" 21 #include "dh_key_util_impl.h" 22 #include "detailed_iv_params.h" 23 #include "detailed_gcm_params.h" 24 #include "detailed_ccm_params.h" 25 #include "detailed_dsa_key_params.h" 26 #include "detailed_ecc_key_params.h" 27 #include "detailed_rsa_key_params.h" 28 #include "detailed_alg_25519_key_params.h" 29 #include "detailed_dh_key_params.h" 30 #include "ecc_key_util_impl.h" 31 #include "kdf_impl.h" 32 #include "key_agreement_impl.h" 33 #include "key_pair_impl.h" 34 #include "mac_impl.h" 35 #include "md_impl.h" 36 #include "pri_key_impl.h" 37 #include "pub_key_impl.h" 38 #include "random_impl.h" 39 #include "securec.h" 40 #include "sign_impl.h" 41 #include "sm2_crypto_util_impl.h" 42 #include "sym_key_generator_impl.h" 43 #include "sym_key_impl.h" 44 #include "verify_impl.h" 45 46 extern "C" { 47 typedef struct { 48 HcfBlob iv; 49 HcfBlob add; 50 HcfBlob authTag; 51 } CParamsSpec; 52 53 // random 54 FFI_EXPORT int64_t FfiOHOSCreateRandom(int32_t* errCode); 55 FFI_EXPORT const char *FfiOHOSRandomGetAlgName(int64_t id, int32_t* errCode); 56 FFI_EXPORT HcfBlob FfiOHOSGenerateRandom(int64_t id, int32_t numBytes, int32_t* errCode); 57 FFI_EXPORT void FfiOHOSSetSeed(int64_t id, HcfBlob *seed, int32_t* errCode); 58 59 // md 60 FFI_EXPORT int64_t FfiOHOSCreateMd(char* algName, int32_t* errCode); 61 FFI_EXPORT int32_t FfiOHOSMdUpdate(int64_t id, HcfBlob *input); 62 FFI_EXPORT HcfBlob FfiOHOSDigest(int64_t id, int32_t* errCode); 63 FFI_EXPORT uint32_t FfiOHOSGetMdLength(int64_t id, int32_t* errCode); 64 65 // symkeygenerator 66 FFI_EXPORT int64_t FfiOHOSCreateSymKeyGenerator(char* algName, int32_t* errCode); 67 FFI_EXPORT const char *FfiOHOSSymKeyGeneratorGetAlgName(int64_t id, int32_t* errCode); 68 FFI_EXPORT int64_t FfiOHOSGenerateSymKey(int64_t id, int32_t* errCode); 69 FFI_EXPORT int64_t FfiOHOSConvertKey(int64_t id, HcfBlob *key, int32_t* errCode); 70 71 // symkey 72 FFI_EXPORT const char *FfiOHOSSymKeyGetAlgName(int64_t id, int32_t* errCode); 73 FFI_EXPORT const char *FfiOHOSSymKeyGetFormat(int64_t id, int32_t* errCode); 74 FFI_EXPORT int32_t FfiOHOSSymKeyGetEncoded(int64_t id, HcfBlob *returnBlob); 75 FFI_EXPORT void FfiOHOSClearMem(int64_t id); 76 FFI_EXPORT void* FfiOHOSSymKeyGetHcfKey(int64_t id); 77 78 // cipher 79 FFI_EXPORT int64_t FfiOHOSCreateCipher(char* transformation, int32_t* errCode); 80 FFI_EXPORT int32_t FfiOHOSCipherInitByIv(int64_t id, int32_t opMode, void* key, HcfBlob blob1); 81 FFI_EXPORT int32_t FfiOHOSCipherInitByGcm(int64_t id, int32_t opMode, void* key, CParamsSpec spec); 82 FFI_EXPORT int32_t FfiOHOSCipherInitByCcm(int64_t id, int32_t opMode, void* key, CParamsSpec spec); 83 FFI_EXPORT int32_t FfiOHOSCipherInitWithOutParams(int64_t id, int32_t opMode, void* key); 84 FFI_EXPORT int32_t FfiOHOSCipherUpdate(int64_t id, HcfBlob *input, HcfBlob *output); 85 FFI_EXPORT int32_t FfiOHOSCipherDoFinal(int64_t id, HcfBlob *input, HcfBlob *output); 86 FFI_EXPORT int32_t FfiOHOSSetCipherSpec(int64_t id, int32_t item, HcfBlob pSource); 87 FFI_EXPORT int32_t FfiOHOSGetCipherSpecString(int64_t id, int32_t item, char *returnString); 88 FFI_EXPORT int32_t FfiOHOSGetCipherSpecUint8Array(int64_t id, int32_t item, HcfBlob *returnUint8Array); 89 FFI_EXPORT const char *FfiOHOSCipherGetAlgName(int64_t id, int32_t* errCode); 90 91 // mac 92 FFI_EXPORT int64_t FFiOHOSCryptoMacConstructor(char* algName, int32_t* errCode); 93 FFI_EXPORT int32_t FfiOHOSCryptoMacInit(int64_t id, int64_t symKeyId); 94 FFI_EXPORT int32_t FfiOHOSCryptoMacUpdate(int64_t id, HcfBlob *input); 95 FFI_EXPORT HcfBlob FfiOHOSCryptoMacDoFinal(int64_t id, int32_t* errCode); 96 FFI_EXPORT uint32_t FfiOHOSCryptoGetMacLength(int64_t id); 97 98 // sign 99 FFI_EXPORT int64_t FFiOHOSCryptoSignConstructor(char* algName, int32_t* errCode); 100 FFI_EXPORT int32_t FFiOHOSSignInit(int64_t sid, int64_t pid); 101 FFI_EXPORT int32_t FFiOHOSSignUpdate(int64_t id, HcfBlob input); 102 FFI_EXPORT int32_t FFiOHOSSignSign(int64_t id, HcfBlob *input, HcfBlob *output); 103 FFI_EXPORT int32_t FFiOHOSSignSetSignSpecByNum(int64_t id, int32_t itemValue); 104 FFI_EXPORT int32_t FFiOHOSSignSetSignSpecByArr(int64_t id, HcfBlob itemValue); 105 FFI_EXPORT int32_t FFiOHOSSignGetSignSpecString(int64_t id, SignSpecItem item, char *itemValue); 106 FFI_EXPORT int32_t FFiOHOSSignGetSignSpecNum(int64_t id, SignSpecItem item, int32_t *itemValue); 107 108 // verify 109 FFI_EXPORT int64_t FFiOHOSVerifyConstructor(char* algName, int32_t* errCode); 110 FFI_EXPORT int32_t FFiOHOSVerifyInit(int64_t vid, int64_t pid); 111 FFI_EXPORT int32_t FFiOHOSVerifyUpdate(int64_t id, HcfBlob input); 112 FFI_EXPORT bool FFiOHOSVerifyVerify(int64_t id, HcfBlob *data, HcfBlob signatureData, int32_t* errCode); 113 FFI_EXPORT int32_t FFiOHOSVerifyRecover(int64_t id, HcfBlob input, HcfBlob *output); 114 FFI_EXPORT int32_t FFiOHOSVerifySetVerifySpecByNum(int64_t id, int32_t itemValue); 115 FFI_EXPORT int32_t FFiOHOSVerifySetVerifySpecByArr(int64_t id, HcfBlob itemValue); 116 FFI_EXPORT int32_t FFiOHOSVerifyGetVerifySpecString(int64_t id, SignSpecItem item, char *itemValue); 117 FFI_EXPORT int32_t FFiOHOSVerifyGetVerifySpecNum(int64_t id, SignSpecItem item, int32_t *itemValue); 118 119 // asykeygenerator 120 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorConstructor(char *algName, int32_t *errCode); 121 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorGenerateKeyPair(int64_t id, int32_t *errCode); 122 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorConvertKey(int64_t id, HcfBlob *pubKey, HcfBlob *priKey, int32_t *errCode); 123 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorConvertPemKey(int64_t id, char *pubKey, char *priKey, int32_t *errCode); 124 125 // asykeyspecgenerator 126 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByDsaCommonSpec(HcfDsaCommParamsSpec *spec, int32_t *errCode); 127 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByDsaPubKeySpec(HcfDsaPubKeyParamsSpec *spec, int32_t *errCode); 128 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByDsaKeyPairSpec(HcfDsaKeyPairParamsSpec *spec, int32_t *errCode); 129 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByEccCommonSpec(HcfEccCommParamsSpec *spec, int32_t *errCode); 130 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByEccPriKeySpec(HcfEccPriKeyParamsSpec *spec, int32_t *errCode); 131 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByEccPubKeySpec(HcfEccPubKeyParamsSpec *spec, int32_t *errCode); 132 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByEccKeyPairSpec(HcfEccKeyPairParamsSpec *spec, int32_t *errCode); 133 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByRsaPubKeySpec(HcfRsaPubKeyParamsSpec *spec, int32_t *errCode); 134 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByRsaKeyPairSpec(HcfRsaKeyPairParamsSpec *spec, int32_t *errCode); 135 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByAlg25519PriKeySpec(HcfAlg25519PriKeyParamsSpec *spec, int32_t *errCode); 136 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByAlg25519PubKeySpec(HcfAlg25519PubKeyParamsSpec *spec, int32_t *errCode); 137 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByAlg25519KeyPairSpec( 138 HcfAlg25519KeyPairParamsSpec *spec, int32_t *errCode); 139 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByDhPriKeySpec(HcfDhPriKeyParamsSpec *spec, int32_t *errCode); 140 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByDhPubKeySpec(HcfDhPubKeyParamsSpec *spec, int32_t *errCode); 141 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByDhKeyPairSpec(HcfDhKeyPairParamsSpec *spec, int32_t *errCode); 142 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorByDhCommonSpec(HcfDhCommParamsSpec *spec, int32_t *errCode); 143 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorBySpecGenerateKeyPair(int64_t id, int32_t *errCode); 144 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorBySpecGeneratePriKey(int64_t id, int32_t *errCode); 145 FFI_EXPORT int64_t FFiOHOSAsyKeyGeneratorBySpecGeneratePubKey(int64_t id, int32_t *errCode); 146 147 // prikey 148 FFI_EXPORT HcfBlob FFiOHOSPriKeyGetEncoded(int64_t id, int32_t *errCode); 149 FFI_EXPORT HcfBlob FFiOHOSPriKeyGetEncodedDer(int64_t id, char *format, int32_t *errCode); 150 FFI_EXPORT char *FFiOHOSPriKeyGetEncodedPem(int64_t id, char *format, int32_t *errCode); 151 FFI_EXPORT int32_t FFiOHOSPriKeyClearMem(int64_t id); 152 FFI_EXPORT int FFiOHOSPriKeyGetAsyKeySpecByNum(int64_t id, int32_t itemType, int32_t *errCode); 153 FFI_EXPORT char *FFiOHOSPriKeyGetAsyKeySpecByStr(int64_t id, int32_t itemType, int32_t *errCode); 154 FFI_EXPORT HcfBigInteger FFiOHOSPriKeyGetAsyKeySpecByBigInt(int64_t id, int32_t itemType, int32_t *errCode); 155 FFI_EXPORT const char *FfiOHOSPriKeyKeyGetFormat(int64_t id, int32_t* errCode); 156 157 // pubkey 158 FFI_EXPORT HcfBlob FFiOHOSPubKeyGetEncoded(int64_t id, int32_t *errCode); 159 FFI_EXPORT HcfBlob FFiOHOSPubKeyGetEncodedDer(int64_t id, char *format, int32_t *errCode); 160 FFI_EXPORT char *FFiOHOSPubKeyGetEncodedPem(int64_t id, char *format, int32_t *errCode); 161 FFI_EXPORT int FFiOHOSPubKeyGetAsyKeySpecByNum(int64_t id, int32_t itemType, int32_t *errCode); 162 FFI_EXPORT char *FFiOHOSPubKeyGetAsyKeySpecByStr(int64_t id, int32_t itemType, int32_t *errCode); 163 FFI_EXPORT HcfBigInteger FFiOHOSPubKeyGetAsyKeySpecByBigInt(int64_t id, int32_t itemType, int32_t *errCode); 164 FFI_EXPORT const char *FfiOHOSPubKeyKeyGetFormat(int64_t id, int32_t* errCode); 165 166 // keypair 167 FFI_EXPORT int64_t FFiOHOSKeyPairPubKey(int64_t id, int32_t *errCode); 168 FFI_EXPORT int64_t FFiOHOSKeyPairPriKey(int64_t id, int32_t *errCode); 169 170 // kdf 171 FFI_EXPORT int64_t FFiOHOSKdfConstructor(char *algName, int32_t *errCode); 172 FFI_EXPORT int32_t FFiOHOSKdfGenerateSecretByPB(int64_t id, HcfPBKDF2ParamsSpec *params); 173 FFI_EXPORT int32_t FFiOHOSKdfGenerateSecretByH(int64_t id, HcfHkdfParamsSpec *params); 174 175 // ecc_key_util 176 FFI_EXPORT HcfEccCommParamsSpec *FFiOHOSECCKeyUtilGenECCCommonParamsSpec(char *curveName, int32_t *errCode); 177 FFI_EXPORT HcfPoint FFiOHOSECCKeyUtilConvertPoint(char *curveName, HcfBlob encodedPoint, int32_t *errCode); 178 FFI_EXPORT HcfBlob FFiOHOSECCKeyUtilGetEncodedPoint( 179 char *curveName, HcfPoint point, char *format, int32_t *errCode); 180 181 // keyagreement 182 FFI_EXPORT int64_t FFiOHOSKeyAgreementConstructor(char *algName, int32_t *errCode); 183 FFI_EXPORT HcfBlob FFiOHOSKeyAgreementGenerateSecret(int64_t id, int64_t priId, int64_t pubId, int32_t *errCode); 184 185 // dh_key_util 186 FFI_EXPORT HcfDhCommParamsSpec *FFiOHOSDHKeyUtilGenDHCommonParamsSpec( 187 int32_t pLen, int32_t skLen, int32_t *errCode); 188 189 // sm2_crypto_util 190 FFI_EXPORT HcfBlob FFiOHOSSm2CryptoUtilGenCipherTextBySpec(Sm2CipherTextSpec spec, char *mode, int32_t *errCode); 191 FFI_EXPORT Sm2CipherTextSpec *FFiOHOSSm2CryptoUtilGetCipherTextSpec(HcfBlob input, char *mode, int32_t *errCode); 192 } 193 194 #endif 195