• 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 #include "pkg_algo_digest.h"
16 #include "openssl/sha.h"
17 #include "pkg_algorithm.h"
18 #include "pkg_utils.h"
19 #include "zlib.h"
20 
21 namespace Hpackage {
GetDigestLen(int8_t digestMethod)22 size_t DigestAlgorithm::GetDigestLen(int8_t digestMethod)
23 {
24     static size_t digestLens[PKG_DIGEST_TYPE_MAX] = {0, DIGEST_CRC_LEN, DIGEST_SHA256_LEN, DIGEST_SHA256_LEN};
25     if (digestMethod < PKG_DIGEST_TYPE_MAX) {
26         return digestLens[digestMethod];
27     }
28     return 0;
29 }
30 
GetSignatureLen(int8_t digestMethod)31 size_t DigestAlgorithm::GetSignatureLen(int8_t digestMethod)
32 {
33     static size_t signatureLens[PKG_DIGEST_TYPE_MAX] = {0, 0, SIGN_SHA256_LEN, SIGN_SHA384_LEN};
34     if (digestMethod < PKG_DIGEST_TYPE_MAX) {
35         return signatureLens[digestMethod];
36     }
37     return SIGN_SHA256_LEN;
38 }
39 
GetDigestMethod(const std::string version)40 uint8_t DigestAlgorithm::GetDigestMethod(const std::string version)
41 {
42     return PKG_DIGEST_TYPE_SHA256;
43 }
44 
Init()45 int32_t Crc32Algorithm::Init()
46 {
47     crc32_ = 0;
48     return PKG_SUCCESS;
49 }
50 
Update(const PkgBuffer & buffer,size_t size)51 int32_t Crc32Algorithm::Update(const PkgBuffer &buffer, size_t size)
52 {
53     if (buffer.buffer == nullptr) {
54         PKG_LOGE("Param null!");
55         return PKG_INVALID_PARAM;
56     }
57     crc32_ = crc32(crc32_, buffer.buffer, size);
58     return PKG_SUCCESS;
59 }
60 
Final(PkgBuffer & result)61 int32_t Crc32Algorithm::Final(PkgBuffer &result)
62 {
63     if (result.buffer == nullptr) {
64         PKG_LOGE("Param null!");
65         return PKG_INVALID_PARAM;
66     }
67     if (result.length != sizeof(uint32_t)) {
68         PKG_LOGE("Invalid size %zu", result.length);
69         return PKG_INVALID_PARAM;
70     }
71     WriteLE32(result.buffer, crc32_);
72     return PKG_SUCCESS;
73 }
74 
Calculate(PkgBuffer & result,const PkgBuffer & buffer,size_t size)75 int32_t Crc32Algorithm::Calculate(PkgBuffer &result, const PkgBuffer &buffer, size_t size)
76 {
77     if (result.buffer == nullptr || buffer.buffer == nullptr) {
78         PKG_LOGE("Param null!");
79         return PKG_INVALID_PARAM;
80     }
81     if (result.length != sizeof(uint32_t)) {
82         PKG_LOGE("Invalid size %zu", result.length);
83         return PKG_INVALID_PARAM;
84     }
85     auto crc = reinterpret_cast<uint32_t *>(result.buffer);
86     // Generate CRC32 of file.
87     *crc = crc32(*crc, buffer.buffer, size);
88     return PKG_SUCCESS;
89 }
90 
Init()91 int32_t Sha256Algorithm::Init()
92 {
93     SHA256_Init(&sha256Ctx_);
94     return PKG_SUCCESS;
95 }
96 
Update(const PkgBuffer & buffer,size_t size)97 int32_t Sha256Algorithm::Update(const PkgBuffer &buffer, size_t size)
98 {
99     if (buffer.buffer == nullptr) {
100         PKG_LOGE("Param null!");
101         return PKG_INVALID_PARAM;
102     }
103     SHA256_Update(&sha256Ctx_, buffer.buffer, size);
104     return PKG_SUCCESS;
105 }
106 
Final(PkgBuffer & result)107 int32_t Sha256Algorithm::Final(PkgBuffer &result)
108 {
109     if (result.buffer == nullptr || result.length != DIGEST_SHA256_LEN) {
110         PKG_LOGE("Param context null!");
111         return PKG_INVALID_PARAM;
112     }
113     SHA256_Final(result.buffer, &sha256Ctx_);
114     return PKG_SUCCESS;
115 }
116 
Calculate(PkgBuffer & result,const PkgBuffer & buffer,size_t size)117 int32_t Sha256Algorithm::Calculate(PkgBuffer &result, const PkgBuffer &buffer, size_t size)
118 {
119     if (result.buffer == nullptr || result.length != DIGEST_SHA256_LEN) {
120         PKG_LOGE("Param context null!");
121         return PKG_INVALID_PARAM;
122     }
123     if (buffer.buffer == nullptr) {
124         PKG_LOGE("Param null!");
125         return PKG_INVALID_PARAM;
126     }
127     SHA256_Init(&sha256Ctx_);
128     SHA256_Update(&sha256Ctx_, buffer.buffer, size);
129     SHA256_Final(result.buffer, &sha256Ctx_);
130     return PKG_SUCCESS;
131 }
132 
Init()133 int32_t Sha384Algorithm::Init()
134 {
135     SHA512_Init(&shaCtx_);
136     return PKG_SUCCESS;
137 }
138 
Update(const PkgBuffer & buffer,size_t size)139 int32_t Sha384Algorithm::Update(const PkgBuffer &buffer, size_t size)
140 {
141     if (buffer.buffer == nullptr) {
142         PKG_LOGE("Param null!");
143         return PKG_INVALID_PARAM;
144     }
145     SHA384_Update(&shaCtx_, buffer.buffer, size);
146     return PKG_SUCCESS;
147 }
148 
Final(PkgBuffer & result)149 int32_t Sha384Algorithm::Final(PkgBuffer &result)
150 {
151     if (result.buffer == nullptr || result.length != DIGEST_SHA384_LEN) {
152         PKG_LOGE("Param context null!");
153         return PKG_INVALID_PARAM;
154     }
155     SHA384_Final(result.buffer, &shaCtx_);
156     return PKG_SUCCESS;
157 }
158 
Calculate(PkgBuffer & result,const PkgBuffer & buffer,size_t size)159 int32_t Sha384Algorithm::Calculate(PkgBuffer &result, const PkgBuffer &buffer, size_t size)
160 {
161     if (result.buffer == nullptr || result.length != DIGEST_SHA384_LEN) {
162         PKG_LOGE("Param context null!");
163         return PKG_INVALID_PARAM;
164     }
165     if (buffer.buffer == nullptr) {
166         PKG_LOGE("Param null!");
167         return PKG_INVALID_PARAM;
168     }
169     SHA512_Init(&shaCtx_);
170     SHA384_Update(&shaCtx_, buffer.buffer, size);
171     SHA384_Final(result.buffer, &shaCtx_);
172     return PKG_SUCCESS;
173 }
174 
GetDigestAlgorithm(uint8_t type)175 DigestAlgorithm::DigestAlgorithmPtr PkgAlgorithmFactory::GetDigestAlgorithm(uint8_t type)
176 {
177     switch (type) {
178         case PKG_DIGEST_TYPE_CRC:
179             return std::make_shared<Crc32Algorithm>();
180         case PKG_DIGEST_TYPE_SHA256:
181         case PKG_DIGEST_TYPE_SHA384:
182             return std::make_shared<Sha256Algorithm>();
183         default:
184             return std::make_shared<DigestAlgorithm>();
185     }
186     return nullptr;
187 }
188 } // namespace Hpackage
189