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 * as paramPublic from './publicDeriveParam.js'; 18import { HksTag, HksKeyStorageType } from '../publicParam'; 19let handle; 20async function publicDeriveGenFunc(srcKeyAlies, HuksOptions) { 21 await huks 22 .generateKey(srcKeyAlies, HuksOptions) 23 .then((data) => { 24 console.log(`test generateKey data: ${JSON.stringify(data)}`); 25 expect(data.errorCode == 0).assertTrue(); 26 }) 27 .catch((err) => { 28 console.log('test generateKey err information: ' + JSON.stringify(err)); 29 expect(null).assertFail(); 30 }); 31} 32 33async function publicDeriveInitFunc(srcKeyAlies, HuksOptions) { 34 await huks 35 .init(srcKeyAlies, HuksOptions) 36 .then((data) => { 37 console.log(`test init data ${JSON.stringify(data)}`); 38 handle = data.handle; 39 expect(data.errorCode == 0).assertTrue(); 40 }) 41 .catch((err) => { 42 console.log('test init err information: ' + JSON.stringify(err)); 43 expect(null).assertFail(); 44 }); 45} 46 47async function publicDeriveUpdateFunc(HuksOptions) { 48 await huks 49 .update(handle, HuksOptions) 50 .then((data) => { 51 console.log(`test update data ${JSON.stringify(data)}`); 52 expect(data.errorCode == 0).assertTrue(); 53 }) 54 .catch((err) => { 55 console.log('test update err information: ' + JSON.stringify(err)); 56 expect(null).assertFail(); 57 }); 58} 59 60async function publicDeriveFinishAbortFunc(huksOptionsFinish, thirdInderfaceName) { 61 if (thirdInderfaceName == 'finish') { 62 await huks 63 .finish(handle, huksOptionsFinish) 64 .then((data) => { 65 console.log(`test finish data ${JSON.stringify(data)}`); 66 expect(data.errorCode == 0).assertTrue(); 67 }) 68 .catch((err) => { 69 console.log('test finish err information: ' + JSON.stringify(err)); 70 expect(null).assertFail(); 71 }); 72 } else { 73 let huksOptionsAbort = new Array({ 74 tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG, 75 value: HksKeyStorageType.HKS_STORAGE_TEMP, 76 }); 77 await huks 78 .abort(handle, huksOptionsAbort) 79 .then((data) => { 80 console.log(`test abort data ${JSON.stringify(data)}`); 81 expect(data.errorCode == 0).assertTrue(); 82 }) 83 .catch((err) => { 84 console.log('test abort err information: ' + JSON.stringify(err)); 85 expect(null).assertFail(); 86 }); 87 } 88} 89 90async function publicDeriveDeleteFunc(srcKeyAlies, HuksOptions) { 91 await huks 92 .deleteKey(srcKeyAlies, HuksOptions) 93 .then((data) => { 94 console.log(`test deleteKey data ${JSON.stringify(data)}`); 95 expect(data.errorCode == 0).assertTrue(); 96 }) 97 .catch((err) => { 98 console.log('test deleteKey err information: ' + JSON.stringify(err)); 99 expect(null).assertFail(); 100 }); 101} 102 103async function publicDeriveFunc(srcKeyAlies, HuksOptions, huksOptionsFinish, thirdInderfaceName) { 104 try { 105 await publicDeriveGenFunc(srcKeyAlies, HuksOptions); 106 HuksOptions.properties.splice(0, 1, paramPublic.HuksDeriveHKDF.HuksKeyAlgHKDF); 107 HuksOptions.properties.splice(3, 1, paramPublic.HuksDeriveHKDF.HuksKeyDERIVEKEYSIZE); 108 109 await publicDeriveInitFunc(srcKeyAlies, HuksOptions); 110 await publicDeriveUpdateFunc(HuksOptions); 111 await publicDeriveFinishAbortFunc(huksOptionsFinish, thirdInderfaceName); 112 113 HuksOptions.properties.splice(0, 1, paramPublic.HuksDeriveHKDF.HuksKeyAlgAES); 114 HuksOptions.properties.splice(3, 1, paramPublic.HuksDeriveHKDF.HuksKeyHKDFSize128); 115 await publicDeriveDeleteFunc(srcKeyAlies, HuksOptions); 116 } catch (e) { 117 expect(null).assertFail(); 118 } 119} 120 121export { publicDeriveFunc }; 122