1 /* 2 * Copyright (C) 2022-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 16 #ifndef HCF_OPENSSL_COMMON_H 17 #define HCF_OPENSSL_COMMON_H 18 19 #include <stdint.h> 20 #include <stdbool.h> 21 #include <openssl/bn.h> 22 #include <openssl/ec.h> 23 #include <openssl/evp.h> 24 #include <openssl/rsa.h> 25 26 #include "big_integer.h" 27 #include "params_parser.h" 28 #include "result.h" 29 #include "utils.h" 30 #include "algorithm_parameter.h" 31 32 #define HCF_OPENSSL_SUCCESS 1 /* openssl return 1: success */ 33 #define HCF_BITS_PER_BYTE 8 34 35 typedef enum { 36 UNINITIALIZED = 0, 37 INITIALIZED = 1, 38 READY = 2, 39 } CryptoStatus; 40 41 #ifdef __cplusplus 42 extern "C" { 43 #endif 44 45 HcfResult GetCurveNameByCurveId(int32_t curveId, char **curveName); 46 HcfResult GetNidByCurveNameValue(int32_t curveNameValue, int32_t *nid); 47 HcfResult GetGroupNameByNid(int32_t nid, char **groupName); 48 HcfResult GetFormatTypeByFormatValue(int32_t formatValue, int32_t *formatType); 49 HcfResult GetAlgNameByBits(int32_t keyLen, char **algName); 50 HcfResult GetOpensslCurveId(int32_t keyLen, int32_t *returnCurveId); 51 HcfResult GetOpensslDigestAlg(uint32_t alg, EVP_MD **digestAlg); 52 void HcfPrintOpensslError(void); 53 54 HcfResult GetOpensslPadding(int32_t padding, int32_t *opensslPadding); 55 56 bool IsBigEndian(void); 57 58 HcfResult BigIntegerToBigNum(const HcfBigInteger *src, BIGNUM **dest); 59 60 HcfResult BigNumToBigInteger(const BIGNUM *src, HcfBigInteger *dest); 61 62 HcfResult BigNumToBigIntegerSecp256k1(const BIGNUM *src, HcfBigInteger *dest); 63 64 HcfResult GetRsaSpecStringMd(const HcfAlgParaValue md, char **returnString); 65 66 HcfResult GetRsaSpecStringMGF(char **returnString); 67 68 HcfResult GetSm2SpecStringSm3(char **returnString); 69 70 HcfResult KeyDerive(EVP_PKEY *priKey, EVP_PKEY *pubKey, HcfBlob *returnSecret); 71 72 HcfResult GetKeyEncodedPem(EVP_PKEY *pkey, const char *outPutStruct, int selection, char **returnString); 73 74 HcfResult ConvertPubPemStrToKey(EVP_PKEY **pkey, const char *keyType, int selection, const char *keyStr); 75 76 HcfResult ConvertPriPemStrToKey(const char *keyStr, EVP_PKEY **pkey, const char *keyType); 77 78 #ifdef __cplusplus 79 } 80 #endif 81 82 #endif 83