Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #ifndef TSD_PACKAGE_VERIFY_H
11 : #define TSD_PACKAGE_VERIFY_H
12 :
13 : #include <string>
14 : #include "tsd/status.h"
15 :
16 : namespace tsd {
17 : constexpr uint32_t RESERVED0_LEN = 20U;
18 : constexpr uint32_t USERDEFINEDDATA_LEN = 32U;
19 : constexpr uint32_t UCLHASH_LEN = 32U;
20 : constexpr uint32_t ROOTPUBKEYE_LEN = 512U;
21 : constexpr uint32_t ROOTPUBKEY_LEN = 512U;
22 : constexpr uint32_t CODEDERIVESALT_LEN = 32U;
23 : constexpr uint32_t CODEENCRYPTIV_LEN = 16U;
24 : constexpr uint32_t CODEENCRYPTTAG_LEN = 16U;
25 : constexpr uint32_t CODEENCRYPTADD_LEN = 16U;
26 : constexpr uint32_t RESERVED1_LEN = 88U;
27 : constexpr uint32_t RESERVED2_LEN = 20U;
28 : constexpr uint32_t HEADHASH_LEN = 32U;
29 : constexpr uint32_t AISDK_CMSFLAG_LEN = 16U;
30 : constexpr uint32_t AISDK_CODENVCNT_LEN = 8U;
31 : constexpr uint32_t AISDK_CODETAG_LEN = 16U;
32 : constexpr uint32_t ENCHASH_LEN = 32U;
33 : struct SeImageHead {
34 : uint32_t preamble;
35 : uint8_t reserved0[RESERVED0_LEN];
36 : uint32_t headLen;
37 : uint32_t userLen;
38 : uint8_t userDefineData[USERDEFINEDDATA_LEN];
39 : uint8_t ucLHash[UCLHASH_LEN];
40 : uint32_t subkeyCertOffset;
41 : uint32_t subkeyCertLen;
42 : uint32_t rootHashAlo;
43 : uint32_t codeSignAlgo;
44 : uint32_t rootPubkeyLen;
45 : uint8_t rootPubkey[ROOTPUBKEY_LEN];
46 : uint8_t rootPubkeyE[ROOTPUBKEYE_LEN];
47 : uint32_t codeOffset;
48 : uint32_t uwLCodeLen;
49 : uint32_t signOffset;
50 : uint32_t signLen;
51 : uint32_t codeEncryptFlag;
52 : uint32_t codeEncryptAlgo;
53 : uint8_t codeDeriveSalt[CODEDERIVESALT_LEN];
54 : uint32_t kmIretationCnt;
55 : uint8_t codeEncryptIv[CODEENCRYPTIV_LEN];
56 : uint8_t codeEncryptTag[CODEENCRYPTTAG_LEN];
57 : uint8_t codeEncryptAdd[CODEENCRYPTADD_LEN];
58 : uint8_t reserved1[RESERVED1_LEN];
59 : uint32_t h2cEnable;
60 : uint32_t h2cCertLen;
61 : uint32_t h2cCertOffset;
62 : uint32_t rootPubkeyInfo;
63 : uint8_t reserved2[RESERVED2_LEN];
64 : uint32_t headMagic;
65 : uint8_t headHash[HEADHASH_LEN];
66 : uint8_t aisdkCmsFlag[AISDK_CMSFLAG_LEN];
67 : uint8_t aisdkCodeNVCNT[AISDK_CODENVCNT_LEN];
68 : uint8_t aisdkCodeTag[AISDK_CODETAG_LEN];
69 : uint8_t encHash[ENCHASH_LEN];
70 : uint32_t certType;
71 : };
72 : class PackageVerify {
73 : public:
74 31 : PackageVerify(const std::string& pkgPath) : pkgPath_(pkgPath){};
75 31 : ~PackageVerify() = default;
76 :
77 : TSD_StatusT VerifyPackage() const;
78 :
79 : private:
80 : PackageVerify(PackageVerify const&) = delete;
81 : PackageVerify& operator=(PackageVerify const&) = delete;
82 : PackageVerify(PackageVerify&&) = delete;
83 : PackageVerify& operator=(PackageVerify&&) = delete;
84 :
85 : TSD_StatusT IsPackageValid() const;
86 : TSD_StatusT ChangePackageMode() const;
87 : bool IsPackageNeedCmsVerify() const;
88 : bool IsSupportCmsVerify() const;
89 : bool IsCmsVerifyPackage() const;
90 : TSD_StatusT VerifyPackageByCms() const;
91 : TSD_StatusT VerifyPackageByDrv() const;
92 : uint32_t GetVerifyDeviceId() const;
93 : TSD_StatusT GetPkgCodeLen(const std::string& srcPath, uint32_t& mixCodeLen) const;
94 : TSD_StatusT ProcessSendStepVerify(const std::string& srcPath, const uint32_t codeLen) const;
95 : TSD_StatusT ReWriteAicpuPackage(const uint8_t* const buf, const uint32_t len, const std::string& srcPath) const;
96 :
97 : std::string pkgPath_;
98 : };
99 :
100 : } // namespace tsd
101 :
102 : #endif // TSD_PACKAGE_VERIFY_H
|