mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-13 20:34:02 +08:00
193
fastdeploy/encryption/util/src/crypto/aes_gcm.cc
Executable file
193
fastdeploy/encryption/util/src/crypto/aes_gcm.cc
Executable file
@@ -0,0 +1,193 @@
|
||||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include "fastdeploy/encryption/util/include/crypto/aes_gcm.h"
|
||||
namespace fastdeploy {
|
||||
namespace util {
|
||||
namespace crypto {
|
||||
|
||||
int AesGcm::aes_gcm_key(const unsigned char* key, const unsigned char* iv,
|
||||
EVP_CIPHER_CTX* e_ctx, EVP_CIPHER_CTX* d_ctx) {
|
||||
int ret = 0;
|
||||
if (e_ctx != NULL) {
|
||||
ret = EVP_EncryptInit_ex(e_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
|
||||
if (ret != 1) {
|
||||
return -1;
|
||||
}
|
||||
ret = EVP_CIPHER_CTX_ctrl(e_ctx, EVP_CTRL_GCM_SET_IVLEN, AES_GCM_IV_LENGTH,
|
||||
NULL);
|
||||
if (ret != 1) {
|
||||
return -2;
|
||||
}
|
||||
ret = EVP_EncryptInit_ex(e_ctx, NULL, NULL, key, iv);
|
||||
if (ret != 1) {
|
||||
return -3;
|
||||
}
|
||||
}
|
||||
// initial decrypt ctx
|
||||
if (d_ctx != NULL) {
|
||||
ret = EVP_DecryptInit_ex(d_ctx, EVP_aes_256_gcm(), NULL, NULL, NULL);
|
||||
if (!ret) {
|
||||
return -1;
|
||||
}
|
||||
ret = EVP_CIPHER_CTX_ctrl(d_ctx, EVP_CTRL_GCM_SET_IVLEN, AES_GCM_IV_LENGTH,
|
||||
NULL);
|
||||
if (!ret) {
|
||||
return -2;
|
||||
}
|
||||
ret = EVP_DecryptInit_ex(d_ctx, NULL, NULL, key, iv);
|
||||
if (!ret) {
|
||||
return -3;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int AesGcm::aes_gcm_key(const std::string& key_hex, const std::string& iv_hex,
|
||||
EVP_CIPHER_CTX* e_ctx, EVP_CIPHER_CTX* d_ctx) {
|
||||
// check key_hex and iv_hex length
|
||||
if (key_hex.length() != AES_GCM_KEY_LENGTH * 2 ||
|
||||
iv_hex.length() != AES_GCM_IV_LENGTH * 2) {
|
||||
return -4;
|
||||
}
|
||||
|
||||
unsigned char key[AES_GCM_KEY_LENGTH];
|
||||
unsigned char iv[AES_GCM_IV_LENGTH];
|
||||
|
||||
int ret = Basic::hex_to_byte(key_hex, key);
|
||||
if (ret < 0) {
|
||||
return -5;
|
||||
}
|
||||
ret = Basic::hex_to_byte(iv_hex, iv);
|
||||
if (ret < 0) {
|
||||
return -5;
|
||||
}
|
||||
return aes_gcm_key(key, iv, e_ctx, d_ctx);
|
||||
}
|
||||
|
||||
int AesGcm::encrypt_aes_gcm(const unsigned char* plaintext, const int& len,
|
||||
const unsigned char* key, const unsigned char* iv,
|
||||
unsigned char* ciphertext, int& out_len) {
|
||||
EVP_CIPHER_CTX* ctx = NULL;
|
||||
int ret = 0;
|
||||
int update_len = 0;
|
||||
int ciphertext_len = 0;
|
||||
unsigned char tag_char[AES_GCM_TAG_LENGTH];
|
||||
|
||||
if (!(ctx = EVP_CIPHER_CTX_new())) {
|
||||
return -1;
|
||||
}
|
||||
// initial context
|
||||
ret = aes_gcm_key(key, iv, ctx, NULL);
|
||||
if (ret) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return -1;
|
||||
}
|
||||
// encryption
|
||||
ret = EVP_EncryptUpdate(ctx, ciphertext, &update_len, plaintext, len);
|
||||
if (ret != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return -2;
|
||||
}
|
||||
ciphertext_len = update_len;
|
||||
|
||||
ret = EVP_EncryptFinal_ex(ctx, ciphertext + ciphertext_len, &update_len);
|
||||
if (1 != ret) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return -3;
|
||||
}
|
||||
ciphertext_len += update_len;
|
||||
|
||||
// Get the tags for authentication
|
||||
ret = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_GET_TAG, AES_GCM_TAG_LENGTH,
|
||||
tag_char);
|
||||
if (1 != ret) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return -4;
|
||||
}
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
// append the tags to the end of encryption text
|
||||
for (int i = 0; i < AES_GCM_TAG_LENGTH; ++i) {
|
||||
ciphertext[ciphertext_len + i] = tag_char[i];
|
||||
}
|
||||
out_len = ciphertext_len + AES_GCM_TAG_LENGTH;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int AesGcm::decrypt_aes_gcm(const unsigned char* ciphertext, const int& len,
|
||||
const unsigned char* key, const unsigned char* iv,
|
||||
unsigned char* plaintext, int& out_len) {
|
||||
EVP_CIPHER_CTX* ctx = NULL;
|
||||
int ret = 0;
|
||||
int update_len = 0;
|
||||
int cipher_len = 0;
|
||||
int plaintext_len = 0;
|
||||
unsigned char tag_char[AES_GCM_TAG_LENGTH];
|
||||
|
||||
// get the tag at the end of ciphertext
|
||||
for (int i = 0; i < AES_GCM_TAG_LENGTH; ++i) {
|
||||
tag_char[i] = ciphertext[len - AES_GCM_TAG_LENGTH + i];
|
||||
}
|
||||
cipher_len = len - AES_GCM_TAG_LENGTH;
|
||||
|
||||
// initial aes context
|
||||
if (!(ctx = EVP_CIPHER_CTX_new())) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
ret = aes_gcm_key(key, iv, NULL, ctx);
|
||||
if (ret) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return -1;
|
||||
}
|
||||
|
||||
// decryption
|
||||
ret = EVP_DecryptUpdate(ctx, plaintext, &update_len, ciphertext, cipher_len);
|
||||
if (ret != 1) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return -2;
|
||||
}
|
||||
plaintext_len = update_len;
|
||||
|
||||
// check if the tag is equal to the decrption tag
|
||||
ret = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_TAG, AES_GCM_TAG_LENGTH,
|
||||
tag_char);
|
||||
if (!ret) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
// decrption failed
|
||||
return -3;
|
||||
}
|
||||
|
||||
ret = EVP_DecryptFinal_ex(ctx, plaintext + update_len, &update_len);
|
||||
if (ret <= 0) {
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
return -4;
|
||||
}
|
||||
|
||||
plaintext_len += update_len;
|
||||
|
||||
EVP_CIPHER_CTX_free(ctx);
|
||||
|
||||
out_len = plaintext_len;
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace util
|
||||
} // namespace fastdeploy
|
133
fastdeploy/encryption/util/src/crypto/base64.cc
Executable file
133
fastdeploy/encryption/util/src/crypto/base64.cc
Executable file
@@ -0,0 +1,133 @@
|
||||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "fastdeploy/encryption/util/include/crypto/base64.h"
|
||||
|
||||
|
||||
namespace fastdeploy {
|
||||
namespace baidu {
|
||||
namespace base {
|
||||
namespace base64 {
|
||||
using std::string;
|
||||
namespace {
|
||||
const string base64_chars = // NOLINT
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
inline bool is_base64(unsigned char c) {
|
||||
return isalnum(c) || (c == '+') || (c == '/');
|
||||
}
|
||||
|
||||
inline size_t encode_len(size_t input_len) { return (input_len + 2) / 3 * 4; }
|
||||
|
||||
void encode_char_array(unsigned char *encode_block,
|
||||
const unsigned char *decode_block) {
|
||||
encode_block[0] = (decode_block[0] & 0xfc) >> 2;
|
||||
encode_block[1] =
|
||||
((decode_block[0] & 0x03) << 4) + ((decode_block[1] & 0xf0) >> 4);
|
||||
encode_block[2] =
|
||||
((decode_block[1] & 0x0f) << 2) + ((decode_block[2] & 0xc0) >> 6);
|
||||
encode_block[3] = decode_block[2] & 0x3f;
|
||||
}
|
||||
|
||||
void decode_char_array(unsigned char *encode_block,
|
||||
unsigned char *decode_block) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
encode_block[i] = base64_chars.find(encode_block[i]);
|
||||
}
|
||||
decode_block[0] = (encode_block[0] << 2) + ((encode_block[1] & 0x30) >> 4);
|
||||
decode_block[1] =
|
||||
((encode_block[1] & 0xf) << 4) + ((encode_block[2] & 0x3c) >> 2);
|
||||
decode_block[2] = ((encode_block[2] & 0x3) << 6) + encode_block[3];
|
||||
}
|
||||
} // namespace
|
||||
|
||||
string base64_encode(const string &input) {
|
||||
string output;
|
||||
size_t i = 0;
|
||||
unsigned char decode_block[3];
|
||||
unsigned char encode_block[4];
|
||||
|
||||
for (string::size_type len = 0; len != input.size(); ++len) {
|
||||
decode_block[i++] = input[len];
|
||||
if (i == 3) {
|
||||
encode_char_array(encode_block, decode_block);
|
||||
for (i = 0; i < 4; ++i) {
|
||||
output += base64_chars[encode_block[i]];
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
for (size_t j = i; j < 3; ++j) {
|
||||
decode_block[j] = '\0';
|
||||
}
|
||||
|
||||
encode_char_array(encode_block, decode_block);
|
||||
|
||||
for (size_t j = 0; j < i + 1; ++j) {
|
||||
output += base64_chars[encode_block[j]];
|
||||
}
|
||||
|
||||
while (i++ < 3) {
|
||||
output += '=';
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
string base64_decode(const string &encoded_string) {
|
||||
int in_len = encoded_string.size();
|
||||
int i = 0;
|
||||
int len = 0;
|
||||
unsigned char encode_block[4];
|
||||
unsigned char decode_block[3];
|
||||
string output;
|
||||
|
||||
while (in_len-- && (encoded_string[len] != '=') &&
|
||||
is_base64(encoded_string[len])) {
|
||||
encode_block[i++] = encoded_string[len];
|
||||
len++;
|
||||
if (i == 4) {
|
||||
decode_char_array(encode_block, decode_block);
|
||||
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
output += decode_block[j];
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
for (int j = i; j < 4; ++j) {
|
||||
encode_block[j] = 0;
|
||||
}
|
||||
|
||||
decode_char_array(encode_block, decode_block);
|
||||
|
||||
for (int j = 0; j < i - 1; ++j) {
|
||||
output += decode_block[j];
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
} // namespace base64
|
||||
} // namespace base
|
||||
} // namespace baidu
|
||||
} // namespace fastdeploy
|
81
fastdeploy/encryption/util/src/crypto/basic.cc
Executable file
81
fastdeploy/encryption/util/src/crypto/basic.cc
Executable file
@@ -0,0 +1,81 @@
|
||||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include "fastdeploy/encryption/util/include/crypto/basic.h"
|
||||
namespace fastdeploy {
|
||||
namespace util {
|
||||
namespace crypto {
|
||||
|
||||
int Basic::byte_to_hex(const unsigned char* in_byte, int len,
|
||||
std::string& out_hex) {
|
||||
std::ostringstream oss;
|
||||
oss << std::hex << std::setfill('0');
|
||||
for (int i = 0; i < len; ++i) {
|
||||
oss << std::setw(2) << int(in_byte[i]);
|
||||
}
|
||||
out_hex = oss.str();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Basic::hex_to_byte(const std::string& in_hex, unsigned char* out_byte) {
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
int len = in_hex.length() / 2;
|
||||
const unsigned char* hex;
|
||||
if (in_hex.length() % 2 != 0 || out_byte == NULL) {
|
||||
return -1;
|
||||
}
|
||||
hex = (unsigned char*)in_hex.c_str();
|
||||
|
||||
for (; j < len; i += 2, ++j) {
|
||||
unsigned char high = hex[i];
|
||||
unsigned char low = hex[i + 1];
|
||||
if (high >= '0' && high <= '9') {
|
||||
high = high - '0';
|
||||
} else if (high >= 'A' && high <= 'F') {
|
||||
high = high - 'A' + 10;
|
||||
} else if (high >= 'a' && high <= 'f') {
|
||||
high = high - 'a' + 10;
|
||||
} else {
|
||||
return -2;
|
||||
}
|
||||
|
||||
if (low >= '0' && low <= '9') {
|
||||
low = low - '0';
|
||||
} else if (low >= 'A' && low <= 'F') {
|
||||
low = low - 'A' + 10;
|
||||
} else if (low >= 'a' && low <= 'f') {
|
||||
low = low - 'a' + 10;
|
||||
} else {
|
||||
return -2;
|
||||
}
|
||||
out_byte[j] = high << 4 | low;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Basic::random(unsigned char* random, int len) {
|
||||
std::random_device rd;
|
||||
int i = 0;
|
||||
if (len <= 0 || random == NULL) {
|
||||
return -1;
|
||||
}
|
||||
for (; i < len; ++i) {
|
||||
random[i] = rd() % 256;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace util
|
||||
} // namespace fastdeploy
|
85
fastdeploy/encryption/util/src/crypto/sha256_utils.cc
Executable file
85
fastdeploy/encryption/util/src/crypto/sha256_utils.cc
Executable file
@@ -0,0 +1,85 @@
|
||||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <stdio.h>
|
||||
#include <openssl/sha.h>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
|
||||
#include "fastdeploy/encryption/util/include/crypto/sha256_utils.h"
|
||||
namespace fastdeploy {
|
||||
namespace util {
|
||||
namespace crypto {
|
||||
|
||||
void SHA256Utils::sha256(const void* data, size_t len, unsigned char* md) {
|
||||
SHA256_CTX sha_ctx = {};
|
||||
SHA256_Init(&sha_ctx);
|
||||
SHA256_Update(&sha_ctx, data, len);
|
||||
SHA256_Final(md, &sha_ctx);
|
||||
}
|
||||
std::vector<unsigned char> SHA256Utils::sha256(const void* data, size_t len) {
|
||||
std::vector<unsigned char> md(32);
|
||||
sha256(data, len, &md[0]);
|
||||
return md;
|
||||
}
|
||||
std::vector<unsigned char> SHA256Utils::sha256(
|
||||
const std::vector<unsigned char>& data) {
|
||||
return sha256(&data[0], data.size());
|
||||
}
|
||||
std::string SHA256Utils::sha256_string(const void* data, size_t len) {
|
||||
std::vector<unsigned char> md = sha256(data, len);
|
||||
std::ostringstream oss;
|
||||
oss << std::hex << std::setfill('0');
|
||||
for (unsigned char c : md) {
|
||||
oss << std::setw(2) << int(c);
|
||||
}
|
||||
return oss.str();
|
||||
}
|
||||
std::string SHA256Utils::sha256_string(const std::vector<unsigned char>& data) {
|
||||
return sha256_string(&data[0], data.size());
|
||||
}
|
||||
std::string SHA256Utils::sha256_string(const std::string& string) {
|
||||
return sha256_string(string.c_str(), string.size());
|
||||
}
|
||||
std::string SHA256Utils::sha256_file(const std::string& path) {
|
||||
FILE* file = fopen(path.c_str(), "rb");
|
||||
if (!file) {
|
||||
return "";
|
||||
}
|
||||
unsigned char hash[SHA256_DIGEST_LENGTH];
|
||||
SHA256_CTX sha_ctx = {};
|
||||
SHA256_Init(&sha_ctx);
|
||||
const int size = 32768;
|
||||
void* buffer = malloc(size);
|
||||
if (!buffer) {
|
||||
fclose(file);
|
||||
return "";
|
||||
}
|
||||
int read = 0;
|
||||
while ((read = fread(buffer, 1, size, file))) {
|
||||
SHA256_Update(&sha_ctx, buffer, read);
|
||||
}
|
||||
SHA256_Final(hash, &sha_ctx);
|
||||
std::ostringstream oss;
|
||||
oss << std::hex << std::setfill('0');
|
||||
for (unsigned char c : hash) {
|
||||
oss << std::setw(2) << int(c);
|
||||
}
|
||||
fclose(file);
|
||||
free(buffer);
|
||||
return oss.str();
|
||||
}
|
||||
|
||||
} // namespace crypto
|
||||
} // namespace util
|
||||
} // namespace fastdeploy
|
225
fastdeploy/encryption/util/src/io_utils.cc
Executable file
225
fastdeploy/encryption/util/src/io_utils.cc
Executable file
@@ -0,0 +1,225 @@
|
||||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifdef LINUX
|
||||
#include <unistd.h>
|
||||
#include <dirent.h>
|
||||
#endif
|
||||
#ifdef WIN32
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <iostream>
|
||||
|
||||
#include "fastdeploy/encryption/util/include/io_utils.h"
|
||||
#include "fastdeploy/encryption/include/model_code.h"
|
||||
#include "fastdeploy/encryption/util/include/log.h"
|
||||
namespace fastdeploy {
|
||||
namespace ioutil {
|
||||
|
||||
int read_file(const char* file_path, unsigned char** dataptr, size_t* sizeptr) {
|
||||
FILE* fp = NULL;
|
||||
fp = fopen(file_path, "rb");
|
||||
if (fp == NULL) {
|
||||
LOGD("[M]open file(%s) failed", file_path);
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
*sizeptr = ftell(fp);
|
||||
*dataptr = (unsigned char*)malloc(sizeof(unsigned char) * (*sizeptr));
|
||||
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
fread(*dataptr, 1, *sizeptr, fp);
|
||||
fclose(fp);
|
||||
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
int read_with_pos_and_length(const char* file_path, unsigned char* dataptr,
|
||||
size_t pos, size_t length) {
|
||||
if (dataptr == NULL) {
|
||||
LOGD("Read file pos dataptr = NULL");
|
||||
return CODE_READ_FILE_PTR_IS_NULL;
|
||||
}
|
||||
|
||||
FILE* fp = NULL;
|
||||
if ((fp = fopen(file_path, "rb")) == NULL) {
|
||||
LOGD("[M]open file(%s) failed", file_path);
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
|
||||
fseek(fp, pos, SEEK_SET);
|
||||
fread(dataptr, 1, length, fp);
|
||||
fclose(fp);
|
||||
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
int read_with_pos(const char* file_path, size_t pos, unsigned char** dataptr,
|
||||
size_t* sizeptr) {
|
||||
FILE* fp = NULL;
|
||||
if ((fp = fopen(file_path, "rb")) == NULL) {
|
||||
LOGD("[M]open file(%s) failed when read_with_pos", file_path);
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size_t filesize = ftell(fp);
|
||||
|
||||
*sizeptr = filesize - pos;
|
||||
*dataptr = (unsigned char*)malloc(sizeof(unsigned char) * (filesize - pos));
|
||||
fseek(fp, pos, SEEK_SET);
|
||||
fread(*dataptr, 1, filesize - pos, fp);
|
||||
fclose(fp);
|
||||
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
int write_file(const char* file_path, const unsigned char* dataptr,
|
||||
size_t sizeptr) {
|
||||
FILE* fp = NULL;
|
||||
if ((fp = fopen(file_path, "wb")) == NULL) {
|
||||
LOGD("[M]open file(%s) failed", file_path);
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
|
||||
fwrite(dataptr, 1, sizeptr, fp);
|
||||
|
||||
fclose(fp);
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
int append_file(const char* file_path, const unsigned char* data, size_t len) {
|
||||
FILE* fp = fopen(file_path, "ab+");
|
||||
if (fp == NULL) {
|
||||
LOGD("[M]open file(%s) failed when append_file", file_path);
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
fwrite(data, sizeof(char), len, fp);
|
||||
fclose(fp);
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
size_t read_file_size(const char* file_path) {
|
||||
FILE* fp = NULL;
|
||||
fp = fopen(file_path, "rb");
|
||||
if (fp == NULL) {
|
||||
LOGD("[M]open file(%s) failed when read_file_size", file_path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
fseek(fp, 0, SEEK_END);
|
||||
size_t filesize = ftell(fp);
|
||||
fclose(fp);
|
||||
|
||||
return filesize;
|
||||
}
|
||||
|
||||
int read_file_to_file(const char* src_path, const char* dst_path) {
|
||||
FILE* infp = NULL;
|
||||
if ((infp = fopen(src_path, "rb")) == NULL) {
|
||||
LOGD("[M]read src file failed when read_file_to_file");
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
|
||||
fseek(infp, 0, SEEK_END);
|
||||
size_t insize = ftell(infp);
|
||||
char* content = reinterpret_cast<char*>(malloc(sizeof(char) * insize));
|
||||
|
||||
fseek(infp, 0, SEEK_SET);
|
||||
fread(content, 1, insize, infp);
|
||||
fclose(infp);
|
||||
|
||||
FILE* outfp = NULL;
|
||||
if ((outfp = fopen(dst_path, "wb")) == NULL) {
|
||||
LOGD("[M]open dst file failed when read_file_to_file");
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
fwrite(content, 1, insize, outfp);
|
||||
fclose(outfp);
|
||||
free(content);
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
int read_dir_files(const char* dir_path,
|
||||
std::vector<std::string>& files) { // NOLINT
|
||||
#ifdef LINUX
|
||||
struct dirent* ptr;
|
||||
DIR* dir = NULL;
|
||||
dir = opendir(dir_path);
|
||||
if (dir == NULL) {
|
||||
return -1; // CODE_NOT_EXIST_DIR
|
||||
}
|
||||
while ((ptr = readdir(dir)) != NULL) {
|
||||
if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
|
||||
files.push_back(ptr->d_name);
|
||||
}
|
||||
}
|
||||
closedir(dir);
|
||||
#endif
|
||||
#ifdef WIN32
|
||||
intptr_t handle;
|
||||
struct _finddata_t fileinfo;
|
||||
|
||||
std::string tmp_dir(dir_path);
|
||||
std::string::size_type idx = tmp_dir.rfind("\\*");
|
||||
if (idx == std::string::npos || idx != tmp_dir.length() - 1) {
|
||||
tmp_dir.append("\\*");
|
||||
}
|
||||
|
||||
handle = _findfirst(tmp_dir.c_str(), &fileinfo);
|
||||
if (handle == -1) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
do {
|
||||
std::cout << "File name = " << fileinfo.name << std::endl;
|
||||
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
|
||||
files.push_back(fileinfo.name);
|
||||
}
|
||||
} while (!_findnext(handle, &fileinfo));
|
||||
|
||||
std::cout << files.size() << std::endl;
|
||||
for (size_t i = 0; i < files.size(); i++) {
|
||||
std::cout << files[i] << std::endl;
|
||||
}
|
||||
|
||||
_findclose(handle);
|
||||
#endif
|
||||
return files.size();
|
||||
}
|
||||
|
||||
int dir_exist_or_mkdir(const char* dir) {
|
||||
#ifdef WIN32
|
||||
if (CreateDirectory(dir, NULL)) {
|
||||
// return CODE_OK;
|
||||
} else {
|
||||
return CODE_MKDIR_FAILED;
|
||||
}
|
||||
#endif
|
||||
#ifdef LINUX
|
||||
if (access(dir, 0) != 0) {
|
||||
mkdir(dir, S_IRWXU | S_IRWXG | S_IRWXO);
|
||||
}
|
||||
#endif
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
} // namespace ioutil
|
||||
} // namespace fastdeploy
|
144
fastdeploy/encryption/util/src/system_utils.cc
Executable file
144
fastdeploy/encryption/util/src/system_utils.cc
Executable file
@@ -0,0 +1,144 @@
|
||||
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
#include <sys/timeb.h>
|
||||
#include <string.h>
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
|
||||
#include "fastdeploy/encryption/include/model_code.h"
|
||||
#include "fastdeploy/encryption/util/include/system_utils.h"
|
||||
#include "fastdeploy/encryption/util/include/crypto/basic.h"
|
||||
#include "fastdeploy/encryption/util/include/crypto/sha256_utils.h"
|
||||
#include "fastdeploy/encryption/util/include/io_utils.h"
|
||||
#include "fastdeploy/encryption/util/include/log.h"
|
||||
#include "fastdeploy/encryption/util/include/constant/constant_model.h"
|
||||
|
||||
namespace fastdeploy {
|
||||
namespace util {
|
||||
|
||||
int SystemUtils::intN(int n) { return rand() % n; }
|
||||
|
||||
std::string SystemUtils::random_key_iv(int len) {
|
||||
unsigned char* tmp = (unsigned char*)malloc(sizeof(unsigned char) * len);
|
||||
int ret = util::crypto::Basic::random(tmp, len);
|
||||
std::string tmp_str(reinterpret_cast<const char*>(tmp), len);
|
||||
free(tmp);
|
||||
return tmp_str;
|
||||
}
|
||||
|
||||
std::string SystemUtils::random_str(int len) {
|
||||
unsigned char* tmp = (unsigned char*)malloc(sizeof(unsigned char) * len);
|
||||
int ret = util::crypto::Basic::random(tmp, len);
|
||||
std::string tmp_str(reinterpret_cast<const char*>(tmp), len);
|
||||
free(tmp);
|
||||
return tmp_str;
|
||||
}
|
||||
|
||||
int SystemUtils::check_key_match(const char* key, const char* filepath) {
|
||||
std::string aes_key_iv(key);
|
||||
std::string sha256_aes_key_iv =
|
||||
util::crypto::SHA256Utils::sha256_string(aes_key_iv);
|
||||
|
||||
unsigned char* data_pos = (unsigned char*)malloc(sizeof(unsigned char) * 64);
|
||||
int ret = ioutil::read_with_pos_and_length(
|
||||
filepath, data_pos, constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN,
|
||||
64);
|
||||
if (ret != CODE_OK) {
|
||||
LOGD("[M]read file failed when check key");
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string check_str(reinterpret_cast<char*>(data_pos), 64);
|
||||
if (strcmp(sha256_aes_key_iv.c_str(), check_str.c_str()) != 0) {
|
||||
return CODE_KEY_NOT_MATCH;
|
||||
}
|
||||
free(data_pos);
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
int SystemUtils::check_key_match(const std::string& key,
|
||||
std::istream& cipher_stream) {
|
||||
cipher_stream.seekg(0, std::ios::beg);
|
||||
std::string sha256_aes_key_iv = util::crypto::SHA256Utils::sha256_string(key);
|
||||
int check_len = 64;
|
||||
|
||||
std::string data_pos_str;
|
||||
cipher_stream.seekg(constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN);
|
||||
std::copy_n(std::istreambuf_iterator<char>(cipher_stream), check_len,
|
||||
std::back_inserter(data_pos_str));
|
||||
if (data_pos_str.size() != check_len) {
|
||||
LOGD("[M]read file failed when check key");
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
if (data_pos_str == sha256_aes_key_iv) {
|
||||
return CODE_OK;
|
||||
}
|
||||
|
||||
return CODE_KEY_NOT_MATCH;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param filepath
|
||||
* @return 0 - file encrypted 1 - file unencrypted
|
||||
*/
|
||||
int SystemUtils::check_file_encrypted(const char* filepath) {
|
||||
size_t read_len = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN;
|
||||
unsigned char* data_pos =
|
||||
(unsigned char*)malloc(sizeof(unsigned char) * read_len);
|
||||
if (ioutil::read_with_pos_and_length(filepath, data_pos, 0, read_len) !=
|
||||
CODE_OK) {
|
||||
LOGD("check file failed when read %s(file)", filepath);
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
|
||||
std::string tag(constant::MAGIC_NUMBER);
|
||||
tag.append(constant::VERSION);
|
||||
std::string check_str(reinterpret_cast<char*>(data_pos), read_len);
|
||||
int ret_cmp = strcmp(tag.c_str(), check_str.c_str()) == 0 ? 0 : 1;
|
||||
free(data_pos);
|
||||
return ret_cmp;
|
||||
}
|
||||
|
||||
int SystemUtils::check_file_encrypted(std::istream& cipher_stream) {
|
||||
cipher_stream.seekg(0, std::ios::beg);
|
||||
size_t read_len = constant::MAGIC_NUMBER_LEN + constant::VERSION_LEN;
|
||||
std::string data_pos_str;
|
||||
std::copy_n(std::istreambuf_iterator<char>(cipher_stream), read_len,
|
||||
std::back_inserter(data_pos_str));
|
||||
if (data_pos_str.size() != read_len) {
|
||||
LOGD("check file failed when read cipher stream");
|
||||
return CODE_OPEN_FAILED;
|
||||
}
|
||||
|
||||
std::string tag(constant::MAGIC_NUMBER);
|
||||
tag.append(constant::VERSION);
|
||||
if (data_pos_str == tag) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
int SystemUtils::check_pattern_exist(const std::vector<std::string>& vecs,
|
||||
const std::string& pattern) {
|
||||
if (std::find(vecs.begin(), vecs.end(), pattern) == vecs.end()) {
|
||||
return -1; // not exist
|
||||
} else {
|
||||
return 0; // exist
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace util
|
||||
} // namespace fastdeploy
|
Reference in New Issue
Block a user