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 
31 #define HCF_OPENSSL_SUCCESS 1     /* openssl return 1: success */
32 #define HCF_BITS_PER_BYTE 8
33 
34 typedef enum {
35     UNINITIALIZED = 0,
36     INITIALIZED = 1,
37     READY = 2,
38 } CryptoStatus;
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43 
44 HcfResult GetCurveNameByCurveId(int32_t curveId, char **curveName);
45 HcfResult GetNidByCurveNameValue(int32_t curveNameValue, int32_t *nid);
46 HcfResult GetGroupNameByNid(int32_t nid, char **groupName);
47 HcfResult GetFormatTypeByFormatValue(int32_t formatValue, int32_t *formatType);
48 HcfResult GetAlgNameByBits(int32_t keyLen, char **algName);
49 HcfResult GetOpensslCurveId(int32_t keyLen, int32_t *returnCurveId);
50 HcfResult GetOpensslDigestAlg(uint32_t alg, EVP_MD **digestAlg);
51 void HcfPrintOpensslError(void);
52 
53 HcfResult GetOpensslPadding(int32_t padding, int32_t *opensslPadding);
54 
55 bool IsBigEndian(void);
56 
57 HcfResult BigIntegerToBigNum(const HcfBigInteger *src, BIGNUM **dest);
58 
59 HcfResult BigNumToBigInteger(const BIGNUM *src, HcfBigInteger *dest);
60 
61 HcfResult BigNumToBigIntegerSecp256k1(const BIGNUM *src, HcfBigInteger *dest);
62 
63 HcfResult GetRsaSpecStringMd(const HcfAlgParaValue md, char **returnString);
64 
65 HcfResult GetRsaSpecStringMGF(char **returnString);
66 
67 HcfResult GetSm2SpecStringSm3(char **returnString);
68 
69 HcfResult KeyDerive(EVP_PKEY *priKey, EVP_PKEY *pubKey, HcfBlob *returnSecret);
70 
71 HcfResult GetKeyEncodedPem(EVP_PKEY *pkey, const char *outPutStruct, int selection, char **returnString);
72 
73 #ifdef __cplusplus
74 }
75 #endif
76 
77 #endif
78