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 huks 26 .generateKey(keyAlias, HuksOptions) 27 .then((data) => { 28 console.log(`test generateKey data: ${JSON.stringify(data)}`); 29 expect(data.errorCode == 0).assertTrue(); 30 }) 31 .catch((err) => { 32 console.log('test generateKey err information: ' + JSON.stringify(err)); 33 expect(null).assertFail(); 34 }); 35} 36 37async function publicImportKey(keyAlias, HuksOptions) { 38 let _InData = HuksOptions.inData; 39 HuksOptions.inData = exportKey; 40 await huks 41 .importKey(keyAlias, HuksOptions) 42 .then((data) => { 43 console.log(`test ImportKey data: ${JSON.stringify(data)}`); 44 }) 45 .catch((err) => { 46 console.log('test exportKey err information: ' + JSON.stringify(err)); 47 expect(null).assertFail(); 48 }); 49 HuksOptions.inData = _InData; 50} 51 52async function publicExportKey(keyAlias, HuksOptions) { 53 await huks 54 .exportKey(keyAlias, HuksOptions) 55 .then((data) => { 56 console.log(`test exportKey data: ${JSON.stringify(data)}`); 57 exportKey = data.outData; 58 }) 59 .catch((err) => { 60 console.log('test exportKey err information: ' + JSON.stringify(err)); 61 expect(null).assertFail(); 62 }); 63} 64 65async function publicInitFunc(keyAlias, HuksOptions) { 66 await huks 67 .init(keyAlias, HuksOptions) 68 .then((data) => { 69 console.log(`test init data: ${JSON.stringify(data)}`); 70 handle = data.handle; 71 expect(data.errorCode == 0).assertTrue(); 72 }) 73 .catch((err) => { 74 console.log('test init err information: ' + JSON.stringify(err)); 75 expect(null).assertFail(); 76 }); 77} 78 79async function publicUpdateFunc(HuksOptions) { 80 const maxUpdateSize = 64; 81 const inData = HuksOptions.inData; 82 const lastInDataPosition = inData.length - 1; 83 let inDataSegSize = maxUpdateSize; 84 let inDataSegPosition = 0; 85 let isFinished = false; 86 let outData = []; 87 88 while (inDataSegPosition <= lastInDataPosition) { 89 HuksOptions.inData = new Uint8Array( 90 Array.from(inData).slice(inDataSegPosition, inDataSegPosition + inDataSegSize) 91 ); 92 console.error(`enter promise doUpdate`); 93 await huks.update(handle, HuksOptions) 94 .then((data) => { 95 console.log(`test update data ${JSON.stringify(data)}`); 96 outData = outData.concat(Array.from(data.outData)); 97 expect(data.errorCode == 0).assertTrue(); 98 }) 99 .catch((err) => { 100 console.log('test update err information: ' + err); 101 expect(null).assertFail(); 102 }); 103 if (inDataSegPosition + maxUpdateSize > lastInDataPosition) { 104 isFinished = true; 105 inDataSegSize = lastInDataPosition - inDataSegPosition + 1; 106 console.error(`enter promise doUpdate`); 107 break; 108 } 109 if ((!isFinished) && (inDataSegPosition + maxUpdateSize > lastInDataPosition)) { 110 console.log(`update size invalid isFinished = ${isFinished}`); 111 console.log(`inDataSegPosition = ${inDataSegPosition}`); 112 console.log(`lastInDataPosition = ${lastInDataPosition}`); 113 expect(null).assertFail(); 114 return; 115 } 116 inDataSegPosition += maxUpdateSize; 117 } 118} 119 120async function publicFinishFunc(HuksOptions) { 121 await huks 122 .finish(handle, HuksOptions) 123 .then((data) => { 124 console.log(`test finish data: ${JSON.stringify(data)}`); 125 finishOutData = data.outData; 126 expect(data.errorCode == 0).assertTrue(); 127 }) 128 .catch((err) => { 129 console.log('test finish err information: ' + JSON.stringify(err)); 130 expect(null).assertFail(); 131 }); 132} 133 134async function publicAbortFucn(HuksOptions) { 135 await huks 136 .abort(handle, HuksOptions) 137 .then((data) => { 138 console.log(`test abort data: ${JSON.stringify(data)}`); 139 expect(data.errorCode == 0).assertTrue(); 140 }) 141 .catch((err) => { 142 console.log('test abort err information: ' + JSON.stringify(err)); 143 expect(null).assertFail(); 144 }); 145} 146 147async function publicDeleteKeyFunc(KeyAlias, HuksOptions) { 148 await huks 149 .deleteKey(KeyAlias, HuksOptions) 150 .then((data) => { 151 console.log(`test deleteKey data: ${JSON.stringify(data)}`); 152 expect(data.errorCode == 0).assertTrue(); 153 }) 154 .catch((err) => { 155 console.log('test deleteKey err information: ' + JSON.stringify(err)); 156 expect(null).assertFail(); 157 }); 158} 159 160async function publicSignVerifyFunc(srcKeyAlies, HuksOptions, thirdInderfaceName, isSING, rawData) { 161 try { 162 let keyAlias = srcKeyAlies; 163 let purposeSignVerigy = HuksSignVerifyDSA.HuksKeyRSAPurposeSINGVERIFY; 164 let purposeSign = HuksSignVerifyDSA.HuksKeyDSAPurposeSIGN; 165 let PurposeVerify = HuksSignVerifyDSA.HuksKeyDSAPurposeVERIFY; 166 if (isSING) { 167 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 168 if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_DSA) { 169 HuksOptions.properties.splice(2, 0, HuksSignVerifyDSA.HuksKeySIZE1024); 170 } 171 await publicGenerateKeyFunc(keyAlias, HuksOptions); 172 HuksOptions.properties.splice(1, 1, purposeSign); 173 if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_DSA) { 174 HuksOptions.properties.splice(2, 1); 175 } 176 } else { 177 keyAlias = srcKeyAlies + 'New'; 178 finishOutData = HuksOptions.inData; 179 await publicImportKey(keyAlias, HuksOptions); 180 } 181 await publicInitFunc(keyAlias, HuksOptions); 182 HuksOptions.inData = rawData; 183 await publicUpdateFunc(HuksOptions); 184 if (thirdInderfaceName == 'finish') { 185 if (isSING) { 186 HuksOptions.inData = new Uint8Array(new Array()); 187 await publicFinishFunc(HuksOptions); 188 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 189 await publicExportKey(keyAlias, HuksOptions); 190 } else { 191 HuksOptions.inData = finishOutData; 192 await publicFinishFunc(HuksOptions); 193 } 194 } else { 195 await publicAbortFucn(HuksOptions); 196 } 197 if (isSING && thirdInderfaceName == 'abort') { 198 HuksOptions.properties.splice(1, 1, purposeSignVerigy); 199 } else if (!isSING) { 200 HuksOptions.properties.splice(1, 1, PurposeVerify); 201 } 202 await publicDeleteKeyFunc(keyAlias, HuksOptions); 203 return finishOutData; 204 } catch (e) { 205 expect(null).assertFail(); 206 } 207} 208 209export { publicSignVerifyFunc }; 210