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 PACKAGE_UPDATER_H 16 #define PACKAGE_UPDATER_H 17 18 #include <cstdlib> 19 #include <iostream> 20 #include <functional> 21 #include <memory> 22 #include <cstdio> 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 constexpr uint32_t DIGEST_MAX_LEN = 32; 29 constexpr int8_t MID_COMPRESS_LEVEL = 2; 30 constexpr uint8_t PKG_SUPPORT_L1 = 0x01; 31 32 enum PkgPackType { 33 PKG_PACK_TYPE_NONE = 0, 34 PKG_PACK_TYPE_UPGRADE, // upgrade package 35 PKG_PACK_TYPE_ZIP, // zip package 36 PKG_PACK_TYPE_LZ4, // lz4 package 37 PKG_PACK_TYPE_GZIP // gzip package 38 }; 39 40 /** 41 * Supported compression methods 42 */ 43 enum PkgCompressMethod { 44 PKG_COMPRESS_METHOD_NONE = 0, // no compression, for upgrade 45 PKG_COMPRESS_METHOD_ZIP, // standard deflate 46 PKG_COMPRESS_METHOD_LZ4, // lz4 47 PKG_COMPRESS_METHOD_LZ4_BLOCK, // lz4 block 48 PKG_COMPRESS_METHOD_GZIP // gzip 49 }; 50 51 /** 52 * Digest algorithm 53 */ 54 enum PkgDigestMethod { 55 PKG_DIGEST_TYPE_NONE = 0, 56 PKG_DIGEST_TYPE_CRC = 1, // crc for zip 57 PKG_DIGEST_TYPE_SHA256, 58 PKG_DIGEST_TYPE_SHA384, 59 PKG_DIGEST_TYPE_MAX 60 }; 61 62 /** 63 * signature algorithm 64 */ 65 enum PkgSignMethod { 66 PKG_SIGN_METHOD_NONE = 0, 67 PKG_SIGN_METHOD_RSA = 1, 68 PKG_SIGN_METHOD_ECDSA = 2, 69 PKG_SIGN_METHOD_MAX 70 }; 71 72 /** 73 * C function used by Python to create an update package 74 */ 75 struct UpgradePkgInfoExt { 76 uint8_t digestMethod = PKG_DIGEST_TYPE_SHA256; 77 uint8_t signMethod = PKG_SIGN_METHOD_RSA; 78 uint8_t pkgType = 0; 79 uint32_t entryCount = 0; 80 uint32_t updateFileVersion = 0; 81 char *productUpdateId; 82 char *softwareVersion; 83 char *date; 84 char *time; 85 char *descriptPackageId = nullptr; 86 }; 87 88 struct ComponentInfoExt { 89 uint8_t digest[DIGEST_MAX_LEN]; 90 char *filePath; 91 char *componentAddr; 92 char *version; 93 uint32_t size = 0; 94 uint32_t id; 95 uint32_t originalSize; 96 uint8_t resType; 97 uint8_t type; 98 uint8_t flags; 99 }; 100 101 /** 102 * create upgrade package 103 * 104 * @param pkgInfo package information 105 * @param comp component information list 106 * @param path package file output path 107 * @param keyPath path of the key used for signature 108 * @return update package creation result 109 */ 110 int32_t CreatePackage(const UpgradePkgInfoExt *pkgInfo, ComponentInfoExt comp[], 111 const char *path, const char *keyPath); 112 113 /** 114 * Signature verification of upgrade package 115 * 116 * @param packagePath path of the update package 117 * @param keyPath path of the key used for verification 118 * @param version file versioin 119 * @param digest digest value 120 * @param size digest value size 121 * @return verification result 122 */ 123 int32_t VerifyPackage(const char *packagePath, const char *keyPath, 124 const char *version, const uint8_t *digest, size_t size); 125 126 /** 127 * 对升级包进行签名验证 128 * 129 * @param packagePath 包文件的文件名 130 * @param keyPath 运来校验的key的文件名 131 * @param cb 校验进度回调函数 132 * @return 校验结果 133 */ 134 int32_t VerifyPackageWithCallback(const std::string &packagePath, const std::string &keyPath, 135 std::function<void(int32_t result, uint32_t percent)> cb); 136 137 /** 138 * Extra dir in package 139 * 140 * @param packagePath path of the update package 141 * @param keyPath path of the key used for verification 142 * @param dir dir in package, empty means extra all files 143 * @param outPath package file output path 144 * @return Extra package creation result 145 */ 146 int32_t ExtraPackageDir(const char *packagePath, const char *keyPath, const char *dir, 147 const char *outPath); 148 149 /** 150 * Extra file in package 151 * 152 * @param packagePath path of the update package 153 * @param keyPath path of the key used for verification 154 * @param file file in package 155 * @param outPath package file output path 156 * @return Extra package creation result 157 */ 158 int32_t ExtraPackageFile(const char *packagePath, const char *keyPath, const char *file, 159 const char *outPath); 160 161 #ifdef __cplusplus 162 } 163 #endif 164 #endif // PACKAGE_UPDATER_H 165