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 */ 15import huks from '@ohos.security.huks'; 16import { HksTag, HksKeyStorageType, HksKeyAlg } from '../publicParam'; 17import { HuksAgreeECDH } from './publicAgreeParam.js'; 18import { stringToUint8Array } from '../publicFunc.js'; 19import { expect } from '@ohos/hypium'; 20 21let exportKeyFrist; 22let exportKeySecond; 23let handle; 24 25async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) { 26 await huks 27 .generateKey(srcKeyAlies, HuksOptions) 28 .then((data) => { 29 console.log(`test generateKey data: ${JSON.stringify(data)}`); 30 expect(data.errorCode == 0).assertTrue(); 31 }) 32 .catch((err) => { 33 console.log('test generateKey err information: ' + JSON.stringify(err)); 34 expect(null).assertFail(); 35 }); 36} 37 38async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKey) { 39 await huks 40 .exportKey(srcKeyAlies, HuksOptions) 41 .then((data) => { 42 console.log(`test exportKey data: ${JSON.stringify(data)}`); 43 if (exportKey == 1) { 44 exportKeyFrist = data.outData; 45 } else { 46 exportKeySecond = data.outData; 47 } 48 }) 49 .catch((err) => { 50 console.log('test exportKey err information: ' + JSON.stringify(err)); 51 expect(null).assertFail(); 52 }); 53} 54 55async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) { 56 await huks 57 .init(srcKeyAlies, HuksOptions) 58 .then((data) => { 59 console.log(`test init data ${JSON.stringify(data)}`); 60 handle = data.handle; 61 expect(data.errorCode == 0).assertTrue(); 62 }) 63 .catch((err) => { 64 console.log('test init err information: ' + JSON.stringify(err)); 65 expect(null).assertFail(); 66 }); 67} 68 69async function publicAgreeUpdateFunc(HuksOptions, exportKey) { 70 let _inData = HuksOptions.inData; 71 if (exportKey == 1) { 72 HuksOptions.inData = exportKeySecond; 73 } else { 74 HuksOptions.inData = exportKeyFrist; 75 } 76 await huks 77 .update(handle, HuksOptions) 78 .then((data) => { 79 console.log(`test update data ${JSON.stringify(data)}`); 80 expect(data.errorCode == 0).assertTrue(); 81 }) 82 .catch((err) => { 83 console.log('test update err information: ' + JSON.stringify(err)); 84 expect(null).assertFail(); 85 }); 86 HuksOptions.inData = _inData; 87} 88 89async function publicAgreeFinishAbortFunc(HuksOptionsFinish, thirdInderfaceName) { 90 if (thirdInderfaceName == 'finish') { 91 await huks 92 .finish(handle, HuksOptionsFinish) 93 .then((data) => { 94 console.log(`test finish data ${JSON.stringify(data)}`); 95 expect(data.errorCode == 0).assertTrue(); 96 }) 97 .catch((err) => { 98 console.log('test finish err information: ' + JSON.stringify(err)); 99 expect(null).assertFail(); 100 }); 101 } else { 102 let HuksOptionsAbort = new Array({ 103 tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG, 104 value: HksKeyStorageType.HKS_STORAGE_TEMP, 105 }); 106 await huks 107 .abort(handle, HuksOptionsAbort) 108 .then((data) => { 109 console.log(`test abort data ${JSON.stringify(data)}`); 110 expect(data.errorCode == 0).assertTrue(); 111 }) 112 .catch((err) => { 113 console.log('test abort err information: ' + JSON.stringify(err)); 114 expect(null).assertFail(); 115 }); 116 } 117} 118 119async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) { 120 await huks 121 .deleteKey(srcKeyAlies, HuksOptions) 122 .then((data) => { 123 console.log(`test deleteKey data ${JSON.stringify(data)}`); 124 expect(data.errorCode == 0).assertTrue(); 125 }) 126 .catch((err) => { 127 console.log('test deleteKey err information: ' + JSON.stringify(err)); 128 expect(null).assertFail(); 129 }); 130} 131 132async function publicAgreeFunc( 133 srcKeyAliesFirst, 134 srcKeyAliesSecond, 135 HuksOptions, 136 HuksOptionsFinish, 137 thirdInderfaceName, 138 isDeleteFinalKeys, 139) { 140 try { 141 await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions); 142 await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions); 143 await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1); 144 await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2); 145 146 if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_ECC) { 147 HuksOptions.properties.splice(0, 1, HuksAgreeECDH.HuksKeyAlgECDH); 148 HuksOptions.properties.splice(3, 1); 149 HuksOptions.properties.splice(4, 1); 150 HuksOptions.properties.splice(5, 1); 151 } 152 153 let HuksOptionsInit = JSON.parse(JSON.stringify(HuksOptions)); 154 HuksOptionsInit.properties.splice(2, 1, HuksOptionsFinish.properties[3]) 155 156 //1st Agree 157 HuksOptionsFinish.properties.splice(6, 1, { 158 tag: HksTag.HKS_TAG_KEY_ALIAS, 159 value: stringToUint8Array(srcKeyAliesFirst + 'final'), 160 }); 161 await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptionsInit); 162 await publicAgreeUpdateFunc(HuksOptions, 1); 163 await publicAgreeFinishAbortFunc(HuksOptionsFinish, thirdInderfaceName); 164 165 //2nd Agree 166 let tempHuksOptionsFinish = HuksOptionsFinish; 167 let HuksOptionsFinishSecond = tempHuksOptionsFinish; 168 HuksOptionsFinishSecond.properties.splice(6, 1, { 169 tag: HksTag.HKS_TAG_KEY_ALIAS, 170 value: stringToUint8Array(srcKeyAliesSecond + 'final'), 171 }); 172 await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptionsInit); 173 await publicAgreeUpdateFunc(HuksOptions, 2); 174 await publicAgreeFinishAbortFunc(HuksOptionsFinishSecond, thirdInderfaceName); 175 176 //Delete 177 await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions); 178 await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions); 179 if (thirdInderfaceName == 'finish' && isDeleteFinalKeys) { 180 await publicAgreeDeleteFunc(srcKeyAliesFirst + 'final', HuksOptions); 181 await publicAgreeDeleteFunc(srcKeyAliesSecond + 'final', HuksOptions); 182 } 183 } catch (e) { 184 expect(null).assertFail(); 185 } 186} 187 188export { publicAgreeFunc }; 189