1 /* 2 * Copyright (C) 2022 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 #ifdef FEATURE_ENCRYPTION_SUPPORT 16 #ifndef OHOS_WIFI_CONFIG_HKS_H 17 #define OHOS_WIFI_CONFIG_HKS_H 18 #include <string> 19 #include <vector> 20 #include "hks_api.h" 21 #include "hks_type.h" 22 #include "hks_param.h" 23 24 namespace OHOS { 25 namespace Wifi { 26 constexpr uint32_t AES_COMMON_SIZE = 256; 27 constexpr uint32_t AAD_SIZE = 16; 28 constexpr uint32_t NONCE_SIZE = 16; 29 30 const uint8_t AAD[AAD_SIZE] = {0}; 31 32 class EncryptedData final { 33 public: 34 std::string encryptedPassword = ""; 35 std::string IV = ""; EncryptedData(const std::string password,const std::string inputIV)36 EncryptedData(const std::string password, const std::string inputIV) 37 { 38 encryptedPassword = password; 39 IV = inputIV; 40 } EncryptedData()41 EncryptedData() {} ~EncryptedData()42 ~EncryptedData() {} 43 }; 44 45 class WifiEncryptionInfo { 46 public: 47 std::string fileName; 48 static constexpr char WIFI_ENCRY_KEY[] = "WifiEncryHksAes"; 49 struct HksBlob keyAlias; SetFile(const std::string file)50 void SetFile(const std::string file) 51 { 52 fileName = WIFI_ENCRY_KEY + file; 53 keyAlias = { fileName.length(), (uint8_t *)&fileName[0] }; 54 } WifiEncryptionInfo(const std::string file)55 explicit WifiEncryptionInfo(const std::string file) 56 { 57 SetFile(file); 58 } WifiEncryptionInfo()59 WifiEncryptionInfo() {} ~WifiEncryptionInfo()60 ~WifiEncryptionInfo() {} 61 }; 62 63 /** 64 * @Description Set up Huks service 65 */ 66 int32_t SetUpHks(); 67 68 /** 69 * @Description Generate new or get existed GCM-AES key based on input encryptionInfo and genParamSet 70 * @param wifiEncryptionInfo - keyAlias info 71 * @param genParamSet - generate params 72 * @return HKS_SUCCESS - find key, others - find key failed 73 */ 74 int32_t GetKey(const WifiEncryptionInfo &wifiEncryptionInfo, const struct HksParamSet *genParamSet); 75 76 /** 77 * @Description Encrypt inputString using GCM-AES based on input encryptionInfo 78 * @param wifiEncryptionInfo - keyAlias info 79 * @param inputString - plaint string that needs to be encrypted 80 * @param encryptedData - encrypted result with encrypted string and IV value 81 * @return HKS_SUCCESS - encryption success, others - encryption failed 82 */ 83 int32_t WifiEncryption(const WifiEncryptionInfo &wifiEncryptionInfo, const std::string &inputString, 84 EncryptedData &encryptedData); 85 86 87 /** 88 * @Description Decrypt encryptedData using GCM-AES based on input encryptionInfo 89 * @param wifiEncryptionInfo - keyAlias info 90 * @param encryptedData - encrypted result with encrypted string and IV value 91 * @param decryptedData - string after decryption 92 * @return HKS_SUCCESS - decryption success, others - decryption failed 93 */ 94 int32_t WifiDecryption(const WifiEncryptionInfo &wifiEncryptionInfo, const EncryptedData &encryptedData, 95 std::string &decryptedData); 96 } 97 } 98 #endif 99 #endif