1 /* 2 * Copyright (c) 2024 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 * Description: encrypt and decrypt data module 15 * Author: lijianzhao 16 * Create: 2022-01-19 17 */ 18 19 #ifndef ENCRYPT_DECRYPT_H 20 #define ENCRYPT_DECRYPT_H 21 22 #include <cstdint> 23 #include <string> 24 25 #include "openssl/hmac.h" 26 #include "openssl/err.h" 27 #include "openssl/rand.h" 28 #include "utils.h" 29 #include "singleton.h" 30 31 namespace OHOS { 32 namespace CastEngine { 33 namespace CastEngineService { 34 struct EncryptInfo { 35 PacketData aad; 36 PacketData tag; 37 ConstPacketData key; 38 ConstPacketData iv; 39 }; 40 41 class EncryptDecrypt final { 42 DECLARE_SINGLETON(EncryptDecrypt); 43 44 public: 45 static EncryptDecrypt &GetInstance(); 46 47 bool EncryptData(int algCode, const uint8_t *key, int keyLen, ConstPacketData inputData, PacketData &outputData); 48 bool DecryptData(int algCode, const uint8_t *key, int keyLen, ConstPacketData inputData, PacketData &outputData); 49 std::string GetEncryptInfo(); 50 int GetEncryptMatch(const std::string &encyptInfo); 51 int GetVersion(); 52 53 static const int AES_KEY_LEN_128 = 16; 54 static const unsigned int AES_IV_LEN = 16; 55 static const int AES_KEY_LEN = 16; 56 static const int AES_KEY_SIZE = 16; 57 static const int PC_ENCRYPT_LEN = 64; 58 59 static const int INVALID_CODE = -1; 60 static const int DEFAULT_CODE = 0; 61 static const int CTR_CODE = 1; 62 static const int GCM_CODE = 2; 63 64 static const std::string PC_ENCRYPT_ALG; 65 66 private: 67 enum ErrorCode : int { 68 SEC_COMMON_ERR_BASE = 0x66010000, 69 SEC_ERR_CREATECIPHER_FAIL = SEC_COMMON_ERR_BASE + 10, 70 SEC_ERR_ENCRYPTUPDATE_FAIL, 71 SEC_ERR_ENCRYPTFINAL_FAIL, 72 SEC_ERR_GCMGETTAG_FAIL, 73 SEC_ERR_INVALID_AAD, 74 SEC_ERR_INVALID_CID, 75 SEC_ERR_INVALID_DATA_LEN, 76 SEC_ERR_INVALID_EXTN, 77 SEC_ERR_INVALID_IV, 78 SEC_ERR_INVALID_IV_LEN, 79 SEC_ERR_INVALID_KEY, 80 SEC_ERR_INVALID_KEY_LEN, 81 SEC_ERR_INVALID_MAC, 82 SEC_ERR_INVALID_MODE, 83 SEC_ERR_INVALID_OID, 84 SEC_ERR_INVALID_PRINTABLE, 85 SEC_ERR_INVALID_SALT_LEN, 86 SEC_ERR_INVALID_SERIALNUMBER, 87 SEC_ERR_INVALID_VERSION, 88 SEC_ERR_NONCE_MISMATCH, 89 SEC_ERR_MALLOC_FAIL, 90 SEC_ERR_NULL_PTR, 91 SEC_ERR_INITLIB, 92 SEC_ERR_INITDEVICE, 93 SEC_ERR_MEMCPY_FAILED, 94 SEC_ERR_MEMSET_FAILED, 95 SEC_ERR_INVALID_PLAIN, 96 SEC_ERR_INVALID_CIPHERTEXT, 97 SEC_ERR_SETAAD_FAIL, 98 }; 99 100 static const int AES_GCM_MAX_IVLEN = 12; 101 static const int AES_GCM_SIV_TAG_LEN = 16; 102 static const int UNSIGNED_CHAR_MIN = 0; 103 static const int UNSIGNED_CHAR_MAX = 255; 104 105 static const int VERSION = 1; 106 107 void GetAESIv(uint8_t iv[], int ivLen); 108 int AES128Encry(ConstPacketData inputData, PacketData &outputData, ConstPacketData sessionKey, 109 ConstPacketData sessionIV); 110 int AES128Decrypt(ConstPacketData inputData, PacketData &outputData, ConstPacketData sessionKey, 111 ConstPacketData sessionIV); 112 int AES128GCMCheckEncryPara(ConstPacketData inputData, PacketData &outputData, EncryptInfo &encryInfo); 113 int AES128GCMCheckDecryptPara(ConstPacketData inputData, PacketData &outputData, EncryptInfo &encryInfo); 114 int EnctyptProcess(ConstPacketData inputData, PacketData &outputData, EncryptInfo &encryInfo, EVP_CIPHER_CTX *ctx); 115 int DecryptProcess(ConstPacketData inputData, PacketData &outputData, EncryptInfo &encryInfo, EVP_CIPHER_CTX *ctx); 116 int AES128GCMEncry(ConstPacketData inputData, PacketData &outputData, EncryptInfo &encryInfo); 117 int AES128GCMDecrypt(ConstPacketData inputData, PacketData &outputData, EncryptInfo &encryInfo); 118 }; 119 } // namespace CastEngineService 120 } // namespace CastEngine 121 } // namespace OHOS 122 123 #endif // ENCRYPT_DECRYPT_H 124