1/* 2 * Copyright (C) 2021 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 { expect } from 'deccjsunit/index'; 16import huks from '@ohos.security.huks'; 17import { HksKeyAlg } from '../publicParam.js'; 18import { HuksSignVerifyDSA } from './publicSignverifyParam.js'; 19import { stringToUint8Array, uint8ArrayToString } from '../publicFunc.js'; 20let finishOutData; 21let exportKey; 22let handle; 23 24async function publicGenerateKeyFunc(keyAlias, HuksOptions) { 25 await generateKey(keyAlias, HuksOptions) 26 .then((data) => { 27 console.log(`test generateKey data: ${JSON.stringify(data)}`); 28 expect(data.errorCode == 0).assertTrue(); 29 }) 30 .catch((err) => { 31 console.log('test generateKey err information: ' + JSON.stringify(err)); 32 expect(null).assertFail(); 33 }); 34} 35 36function generateKey(srcKeyAlies, HuksOptions) { 37 return new Promise((resolve, reject) => { 38 huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) { 39 console.log(`test generateKey data: ${JSON.stringify(data)}`); 40 if (err.code !== 0) { 41 console.log('test generateKey err information: ' + JSON.stringify(err)); 42 reject(err); 43 } else { 44 resolve(data); 45 } 46 }); 47 }); 48} 49 50async function publicImportKey(keyAlias, HuksOptions) { 51 let _InData = HuksOptions.inData; 52 HuksOptions.inData = exportKey; 53 await importKey(keyAlias, HuksOptions) 54 .then((data) => { 55 console.log(`test ImportKey data: ${JSON.stringify(data)}`); 56 }) 57 .catch((err) => { 58 console.log('test ImportKey err information: ' + JSON.stringify(err)); 59 expect(null).assertFail(); 60 }); 61 HuksOptions.inData = _InData; 62} 63 64function importKey(srcKeyAlies, HuksOptions) { 65 return new Promise((resolve, reject) => { 66 huks.importKey(srcKeyAlies, HuksOptions, function (err, data) { 67 console.log(`test importKey data: ${JSON.stringify(data)}`); 68 if (err.code !== 0) { 69 console.log('test importKey err information: ' + JSON.stringify(err)); 70 reject(err); 71 } else { 72 resolve(data); 73 } 74 }); 75 }); 76} 77 78async function publicExportKey(keyAlias, HuksOptions) { 79 await exportkey(keyAlias, HuksOptions) 80 .then((data) => { 81 console.log(`test exportKey data: ${JSON.stringify(data)}`); 82 exportKey = data.outData; 83 }) 84 .catch((err) => { 85 console.log('test exportKey err information: ' + JSON.stringify(err)); 86 expect(null).assertFail(); 87 }); 88} 89 90function exportkey(srcKeyAlies, HuksOptions) { 91 return new Promise((resolve, reject) => { 92 huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) { 93 console.log(`test exportKey data: ${JSON.stringify(data)}`); 94 if (err.code !== 0) { 95 console.log('test exportKey err information: ' + JSON.stringify(err)); 96 reject(err); 97 } else { 98 resolve(data); 99 } 100 }); 101 }); 102} 103 104async function publicInitFunc(keyAlias, HuksOptions) { 105 await init(keyAlias, HuksOptions) 106 .then((data) => { 107 console.log(`test init data: ${JSON.stringify(data)}`); 108 handle = data.handle; 109 expect(data.errorCode == 0).assertTrue(); 110 }) 111 .catch((err) => { 112 console.log('test init err information: ' + JSON.stringify(err)); 113 expect(null).assertFail(); 114 }); 115} 116 117function init(srcKeyAlies, HuksOptions) { 118 return new Promise((resolve, reject) => { 119 huks.init(srcKeyAlies, HuksOptions, function (err, data) { 120 if (err.code !== 0) { 121 console.log('test init err information: ' + JSON.stringify(err)); 122 reject(err); 123 } else { 124 resolve(data); 125 } 126 }); 127 }); 128} 129 130async function publicUpdateFunc(HuksOptions) { 131 let dateSize = 64; 132 let tempHuksOptionsInData = HuksOptions.inData; 133 let inDataArray = HuksOptions.inData; 134 if (uint8ArrayToString(inDataArray).length < dateSize) { 135 await update(handle, HuksOptions); 136 HuksOptions.inData = tempHuksOptionsInData; 137 } else { 138 let count = Math.floor(uint8ArrayToString(inDataArray).length / dateSize); 139 let remainder = uint8ArrayToString(inDataArray).length % dateSize; 140 for (let i = 0; i < count; i++) { 141 HuksOptions.inData = stringToUint8Array( 142 uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * i, dateSize * (i + 1)) 143 ); 144 await update(handle, HuksOptions); 145 HuksOptions.inData = tempHuksOptionsInData; 146 } 147 if (remainder !== 0) { 148 HuksOptions.inData = stringToUint8Array( 149 uint8ArrayToString(tempHuksOptionsInData).slice(dateSize * count, uint8ArrayToString(inDataArray).length) 150 ); 151 await update(handle, HuksOptions); 152 HuksOptions.inData = tempHuksOptionsInData; 153 } 154 } 155} 156 157async function update(handle, HuksOptions) { 158 await updateCallback(handle, HuksOptions) 159 .then(async (data) => { 160 console.log(`test update data ${JSON.stringify(data)}`); 161 expect(data.errorCode == 0).assertTrue(); 162 }) 163 .catch((err) => { 164 console.log('test update err information: ' + err); 165 expect(null).assertFail(); 166 }); 167} 168 169function updateCallback(handle, HuksOptions) { 170 return new Promise((resolve, reject) => { 171 huks.update(handle, HuksOptions, function (err, data) { 172 if (err.code !== 0) { 173 console.log('test update err information: ' + JSON.stringify(err)); 174 reject(err); 175 } else { 176 resolve(data); 177 } 178 }); 179 }); 180} 181 182async function publicFinishFunc(HuksOptions) { 183 await finish(handle, HuksOptions) 184 .then((data) => { 185 console.log(`test finish data: ${JSON.stringify(data)}`); 186 finishOutData = data.outData; 187 expect(data.errorCode == 0).assertTrue(); 188 }) 189 .catch((err) => { 190 console.log('test finish err information: ' + JSON.stringify(err)); 191 expect(null).assertFail(); 192 }); 193} 194 195function finish(handle, HuksOptionsFinish) { 196 return new Promise((resolve, reject) => { 197 huks.finish(handle, HuksOptionsFinish, function (err, data) { 198 if (err.code !== 0) { 199 console.log('test generateKey err information: ' + JSON.stringify(err)); 200 reject(err); 201 } else { 202 resolve(data); 203 } 204 }); 205 }); 206} 207 208async function publicAbortFucn(HuksOptions) { 209 await abort(handle, HuksOptions) 210 .then((data) => { 211 console.log(`test abort data: ${JSON.stringify(data)}`); 212 expect(data.errorCode == 0).assertTrue(); 213 }) 214 .catch((err) => { 215 console.log('test abort err information: ' + JSON.stringify(err)); 216 expect(null).assertFail(); 217 }); 218} 219 220function abort(handle, HuksOptionsAbort) { 221 return new Promise((resolve, reject) => { 222 huks.abort(handle, HuksOptionsAbort, function (err, data) { 223 if (err.code !== 0) { 224 console.log('test abort err information: ' + JSON.stringify(err)); 225 reject(err); 226 } else { 227 resolve(data); 228 } 229 }); 230 }); 231} 232 233async function publicDeleteKeyFunc(KeyAlias, HuksOptions) { 234 await deleteKey(KeyAlias, HuksOptions) 235 .then((data) => { 236 console.log(`test deleteKey data: ${JSON.stringify(data)}`); 237 expect(data.errorCode == 0).assertTrue(); 238 }) 239 .catch((err) => { 240 console.log('test deleteKey err information: ' + JSON.stringify(err)); 241 expect(null).assertFail(); 242 }); 243} 244 245function deleteKey(srcKeyAlies, HuksOptions) { 246 return new Promise((resolve, reject) => { 247 huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) { 248 if (err.code !== 0) { 249 console.log('test deleteKey err information: ' + JSON.stringify(err)); 250 reject(err); 251 } else { 252 resolve(data); 253 } 254 }); 255 }); 256} 257 258async function publicSignVerifyFunc(srcKeyAlies, HuksOptions, thirdInderfaceName, isSING, rawData) { 259 try { 260 let keyAlias = srcKeyAlies; 261 let purposeSignVerigy = HuksSignVerifyDSA.HuksKeyRSAPurposeSINGVERIFY; 262 let purposeSign = HuksSignVerifyDSA.HuksKeyDSAPurposeSIGN; 263 let PurposeVerify = HuksSignVerifyDSA.HuksKeyDSAPurposeVERIFY; 264 if (isSING) { 265 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 266 if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_DSA) { 267 HuksOptions.properties.splice(2, 0, HuksSignVerifyDSA.HuksKeySIZE1024); 268 } 269 await publicGenerateKeyFunc(keyAlias, HuksOptions); 270 HuksOptions.properties.splice(1, 1, purposeSign); 271 if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_DSA) { 272 HuksOptions.properties.splice(2, 1); 273 } 274 } else { 275 keyAlias = srcKeyAlies + 'New'; 276 finishOutData = HuksOptions.inData; 277 await publicImportKey(keyAlias, HuksOptions); 278 } 279 await publicInitFunc(keyAlias, HuksOptions); 280 HuksOptions.inData = rawData; 281 await publicUpdateFunc(HuksOptions); 282 if (thirdInderfaceName == 'finish') { 283 if (isSING) { 284 HuksOptions.inData = new Uint8Array(new Array()); 285 await publicFinishFunc(HuksOptions); 286 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 287 await publicExportKey(keyAlias, HuksOptions); 288 } else { 289 HuksOptions.inData = finishOutData; 290 await publicFinishFunc(HuksOptions); 291 } 292 } else { 293 await publicAbortFucn(HuksOptions); 294 } 295 if (isSING && thirdInderfaceName == 'abort') { 296 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 297 } else if (!isSING) { 298 HuksOptions.properties.splice(1, 1, PurposeVerify); 299 } 300 await publicDeleteKeyFunc(keyAlias, HuksOptions); 301 return finishOutData; 302 } catch (e) { 303 expect(null).assertFail(); 304 } 305} 306 307export { publicSignVerifyFunc }; 308