• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2021 Huawei Technologies Co., Ltd
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef MINDSPORE_CORE_UTILS_CRYPTO_H
18 #define MINDSPORE_CORE_UTILS_CRYPTO_H
19 
20 #include <string>
21 #include <memory>
22 #include "mindapi/base/macros.h"
23 
24 typedef unsigned char Byte;
25 namespace mindspore {
26 constexpr size_t MAX_BLOCK_SIZE = 64 * 1024 * 1024;  // Maximum ciphertext segment, units is Byte
27 constexpr size_t RESERVED_BYTE_PER_BLOCK = 50;       // Reserved byte per block to save addition info
28 constexpr size_t DECRYPT_BLOCK_BUF_SIZE = MAX_BLOCK_SIZE + RESERVED_BYTE_PER_BLOCK;  // maximum length of decrypt block
29 constexpr unsigned int GCM_MAGIC_NUM = 0x7F3A5ED8;                                   // Magic number
30 constexpr unsigned int CBC_MAGIC_NUM = 0x7F3A5ED9;                                   // Magic number
31 constexpr unsigned int SM4_CBC_MAGIC_NUM = 0x7F3A5EDA;                               // Magic number
32 constexpr size_t Byte16 = 16;
33 
34 MS_CORE_API std::unique_ptr<Byte[]> Encrypt(size_t *encrypt_len, const Byte *plain_data, size_t plain_len,
35                                             const Byte *key, size_t key_len, const std::string &enc_mode);
36 MS_CORE_API std::unique_ptr<Byte[]> Decrypt(size_t *decrypt_len, const std::string &encrypt_data_path, const Byte *key,
37                                             size_t key_len, const std::string &dec_mode);
38 MS_CORE_API std::unique_ptr<Byte[]> Decrypt(size_t *decrypt_len, const Byte *model_data, size_t data_size,
39                                             const Byte *key, size_t key_len, const std::string &dec_mode);
40 MS_CORE_API bool IsCipherFile(const std::string &file_path);
41 MS_CORE_API bool IsCipherFile(const Byte *model_data);
42 }  // namespace mindspore
43 #endif
44