1 /*
2  * Copyright (c) 2024-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 SIGNATRUETOOLS_DIGEST_UTILS_H
16 #define SIGNATRUETOOLS_DIGEST_UTILS_H
17 #include <string>
18 #include <vector>
19 
20 #include "byte_buffer.h"
21 #include "openssl/evp.h"
22 #include "openssl/ossl_typ.h"
23 #include "digest_parameter.h"
24 #include "pkcs7_context.h"
25 #include "signature_info.h"
26 #include "export_define.h"
27 #include "openssl/pkcs7.h"
28 #include "openssl/safestack.h"
29 
30 namespace OHOS {
31 namespace SignatureTools {
32 
33 enum SignatureAlgorithm {
34     ALGORITHM_SHA256_WITH_ECDSA = 0x00000201,
35     ALGORITHM_SHA384_WITH_ECDSA,
36     ALGORITHM_SHA512_WITH_ECDSA,
37     ALGORITHM_SHA256_WITH_DSA = 0x00000301,
38     ALGORITHM_SHA384_WITH_DSA,
39     ALGORITHM_SHA512_WITH_DSA,
40 };
41 
42 class DigestCommon {
43 public:
44     DigestCommon() = delete;
45 
46     static int32_t GetDigest(const ByteBuffer& chunk, const std::vector<OptionalBlock>& optionalBlocks,
47                              const DigestParameter& digestParameter, unsigned char(&out)[EVP_MAX_MD_SIZE]);
48     static bool DigestInit(const DigestParameter& digestParameter);
49     static bool DigestUpdate(const DigestParameter& digestParameter,
50                              const unsigned char content[], int32_t len);
51     static int32_t GetDigest(const DigestParameter& digestParameter, unsigned char(&out)[EVP_MAX_MD_SIZE]);
52     static int32_t GetDigestAlgorithmOutputSizeBytes(int32_t nId);
53     DLL_EXPORT static int32_t GetDigestAlgorithmId(int32_t signAlgorithm);
54     static std::string GetDigestAlgorithmString(int32_t signAlgorithm);
55     static void GetOpensslErrorMessage();
56 
57 private:
58     static bool CheckDigestParameter(const DigestParameter& digestParameter);
59 
60     static const int32_t OPENSSL_ERR_MESSAGE_MAX_LEN;
61 };
62 } // namespace SignatureTools
63 } // namespace OHOS
64 #endif // SIGNATRUETOOLS_VERIFY_OPENSSL_UTILS_H
65