• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 PKG_ALGORITHM_DIGEST_H
16 #define PKG_ALGORITHM_DIGEST_H
17 
18 #include "openssl/sha.h"
19 #include "pkg_utils.h"
20 
21 namespace Hpackage {
22 constexpr uint32_t DIGEST_CRC_LEN = 4;
23 constexpr uint32_t DIGEST_SHA256_LEN = 32;
24 constexpr uint32_t DIGEST_SHA384_LEN = 64;
25 constexpr uint32_t SIGN_SHA256_LEN = 256;
26 constexpr uint32_t SIGN_SHA384_LEN = 384;
27 constexpr uint32_t SIGN_TOTAL_LEN = 384 + 256;
28 
29 class DigestAlgorithm {
30 public:
31     using DigestAlgorithmPtr = std::shared_ptr<DigestAlgorithm>;
32 
DigestAlgorithm()33     DigestAlgorithm() {}
34 
~DigestAlgorithm()35     virtual ~DigestAlgorithm() {}
36 
Init()37     virtual int32_t Init()
38     {
39         return 0;
40     };
41 
Update(const PkgBuffer & buffer,size_t size)42     virtual int32_t Update(const PkgBuffer &buffer, size_t size)
43     {
44         UNUSED(buffer);
45         return PKG_SUCCESS;
46     }
47 
Final(PkgBuffer & buffer)48     virtual int32_t Final(PkgBuffer &buffer)
49     {
50         (void)buffer;
51         return PKG_SUCCESS;
52     }
53 
Calculate(PkgBuffer & result,const PkgBuffer & buffer,size_t size)54     virtual int32_t Calculate(PkgBuffer &result, const PkgBuffer &buffer, size_t size)
55     {
56         UNUSED(result);
57         UNUSED(buffer);
58         return PKG_SUCCESS;
59     }
60 
61     static size_t GetDigestLen(int8_t digestMethod);
62     static size_t GetSignatureLen(int8_t digestMethod);
63     static uint8_t GetDigestMethod(const std::string version);
64 };
65 
66 class Crc32Algorithm : public DigestAlgorithm {
67 public:
Crc32Algorithm()68     Crc32Algorithm() {}
69 
~Crc32Algorithm()70     ~Crc32Algorithm() override {}
71 
72     int32_t Init() override;
73 
74     int32_t Update(const PkgBuffer &buffer, size_t size) override;
75 
76     int32_t Final(PkgBuffer &result) override;
77 
78     int32_t Calculate(PkgBuffer &result, const PkgBuffer &buffer, size_t size) override;
79 
80 private:
81     uint32_t crc32_ { 0 };
82 };
83 
84 class Sha256Algorithm : public DigestAlgorithm {
85 public:
Sha256Algorithm()86     Sha256Algorithm() {}
87 
~Sha256Algorithm()88     ~Sha256Algorithm() override {}
89 
90     int32_t Init() override;
91 
92     int32_t Update(const PkgBuffer &buffer, size_t size) override;
93 
94     int32_t Final(PkgBuffer &result) override;
95 
96     int32_t Calculate(PkgBuffer &result, const PkgBuffer &buffer, size_t size) override;
97 
98 private:
99     SHA256_CTX sha256Ctx_ {};
100 };
101 
102 class Sha384Algorithm : public DigestAlgorithm {
103 public:
Sha384Algorithm()104     Sha384Algorithm() {}
105 
~Sha384Algorithm()106     ~Sha384Algorithm() override {}
107 
108     int32_t Init() override;
109 
110     int32_t Update(const PkgBuffer &buffer, size_t size) override;
111 
112     int32_t Final(PkgBuffer &result) override;
113 
114     int32_t Calculate(PkgBuffer &result, const PkgBuffer &buffer, size_t size) override;
115 
116 private:
117     SHA512_CTX shaCtx_ {};
118 };
119 } // namespace Hpackage
120 #endif
121