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 const maxUpdateSize = 64; 132 const inData = HuksOptions.inData; 133 const lastInDataPosition = inData.length - 1; 134 let inDataSegSize = maxUpdateSize; 135 let inDataSegPosition = 0; 136 let isFinished = false; 137 let outData = []; 138 139 while (inDataSegPosition <= lastInDataPosition) { 140 HuksOptions.inData = new Uint8Array( 141 Array.from(inData).slice(inDataSegPosition, inDataSegPosition + inDataSegSize) 142 ); 143 console.error(`enter promise doUpdate`); 144 await updateCallback(handle, HuksOptions).then(async (data) => { 145 console.log(`test update data ${JSON.stringify(data)}`); 146 outData = outData.concat(Array.from(data.outData)); 147 expect(data.errorCode == 0).assertTrue(); 148 }) 149 .catch((err) => { 150 console.log('test update err information: ' + err); 151 expect(null).assertFail(); 152 }); 153 if (inDataSegPosition + maxUpdateSize > lastInDataPosition) { 154 isFinished = true; 155 inDataSegSize = lastInDataPosition - inDataSegPosition + 1; 156 console.error(`enter promise doUpdate`); 157 break; 158 } 159 if ((!isFinished) && (inDataSegPosition + maxUpdateSize > lastInDataPosition)) { 160 console.log(`update size invalid isFinished = ${isFinished}`); 161 console.log(`inDataSegPosition = ${inDataSegPosition}`); 162 console.log(`lastInDataPosition = ${lastInDataPosition}`); 163 expect(null).assertFail(); 164 return; 165 } 166 inDataSegPosition += maxUpdateSize; 167 } 168} 169 170function updateCallback(handle, HuksOptions) { 171 return new Promise((resolve, reject) => { 172 huks.update(handle, HuksOptions, function (err, data) { 173 if (err.code !== 0) { 174 console.log('test update err information: ' + JSON.stringify(err)); 175 reject(err); 176 } else { 177 resolve(data); 178 } 179 }); 180 }); 181} 182 183async function publicFinishFunc(HuksOptions) { 184 await finish(handle, HuksOptions) 185 .then((data) => { 186 console.log(`test finish data: ${JSON.stringify(data)}`); 187 finishOutData = data.outData; 188 expect(data.errorCode == 0).assertTrue(); 189 }) 190 .catch((err) => { 191 console.log('test finish err information: ' + JSON.stringify(err)); 192 expect(null).assertFail(); 193 }); 194} 195 196function finish(handle, HuksOptionsFinish) { 197 return new Promise((resolve, reject) => { 198 huks.finish(handle, HuksOptionsFinish, function (err, data) { 199 if (err.code !== 0) { 200 console.log('test generateKey err information: ' + JSON.stringify(err)); 201 reject(err); 202 } else { 203 resolve(data); 204 } 205 }); 206 }); 207} 208 209async function publicAbortFucn(HuksOptions) { 210 await abort(handle, HuksOptions) 211 .then((data) => { 212 console.log(`test abort data: ${JSON.stringify(data)}`); 213 expect(data.errorCode == 0).assertTrue(); 214 }) 215 .catch((err) => { 216 console.log('test abort err information: ' + JSON.stringify(err)); 217 expect(null).assertFail(); 218 }); 219} 220 221function abort(handle, HuksOptionsAbort) { 222 return new Promise((resolve, reject) => { 223 huks.abort(handle, HuksOptionsAbort, function (err, data) { 224 if (err.code !== 0) { 225 console.log('test abort err information: ' + JSON.stringify(err)); 226 reject(err); 227 } else { 228 resolve(data); 229 } 230 }); 231 }); 232} 233 234async function publicDeleteKeyFunc(KeyAlias, HuksOptions) { 235 await deleteKey(KeyAlias, HuksOptions) 236 .then((data) => { 237 console.log(`test deleteKey data: ${JSON.stringify(data)}`); 238 expect(data.errorCode == 0).assertTrue(); 239 }) 240 .catch((err) => { 241 console.log('test deleteKey err information: ' + JSON.stringify(err)); 242 expect(null).assertFail(); 243 }); 244} 245 246function deleteKey(srcKeyAlies, HuksOptions) { 247 return new Promise((resolve, reject) => { 248 huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) { 249 if (err.code !== 0) { 250 console.log('test deleteKey err information: ' + JSON.stringify(err)); 251 reject(err); 252 } else { 253 resolve(data); 254 } 255 }); 256 }); 257} 258 259async function publicSignVerifyFunc(srcKeyAlies, HuksOptions, thirdInderfaceName, isSING, rawData) { 260 try { 261 let keyAlias = srcKeyAlies; 262 let purposeSignVerigy = HuksSignVerifyDSA.HuksKeyRSAPurposeSINGVERIFY; 263 let purposeSign = HuksSignVerifyDSA.HuksKeyDSAPurposeSIGN; 264 let PurposeVerify = HuksSignVerifyDSA.HuksKeyDSAPurposeVERIFY; 265 if (isSING) { 266 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 267 if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_DSA) { 268 HuksOptions.properties.splice(2, 0, HuksSignVerifyDSA.HuksKeySIZE1024); 269 } 270 await publicGenerateKeyFunc(keyAlias, HuksOptions); 271 HuksOptions.properties.splice(1, 1, purposeSign); 272 if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_DSA) { 273 HuksOptions.properties.splice(2, 1); 274 } 275 } else { 276 keyAlias = srcKeyAlies + 'New'; 277 finishOutData = HuksOptions.inData; 278 await publicImportKey(keyAlias, HuksOptions); 279 } 280 await publicInitFunc(keyAlias, HuksOptions); 281 HuksOptions.inData = rawData; 282 await publicUpdateFunc(HuksOptions); 283 if (thirdInderfaceName == 'finish') { 284 if (isSING) { 285 HuksOptions.inData = new Uint8Array(new Array()); 286 await publicFinishFunc(HuksOptions); 287 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 288 await publicExportKey(keyAlias, HuksOptions); 289 } else { 290 HuksOptions.inData = finishOutData; 291 await publicFinishFunc(HuksOptions); 292 } 293 } else { 294 await publicAbortFucn(HuksOptions); 295 } 296 if (isSING && thirdInderfaceName == 'abort') { 297 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 298 } else if (!isSING) { 299 HuksOptions.properties.splice(1, 1, PurposeVerify); 300 } 301 await publicDeleteKeyFunc(keyAlias, HuksOptions); 302 return finishOutData; 303 } catch (e) { 304 expect(null).assertFail(); 305 } 306} 307 308export { publicSignVerifyFunc }; 309