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; 20 21async function publicDeriveGenFunc(srcKeyAlies, HuksOptions) { 22 await 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 33function generateKey(srcKeyAlies, HuksOptions) { 34 return new Promise((resolve, reject) => { 35 huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) { 36 console.log(`test generateKey data: ${JSON.stringify(data)}`); 37 if (err.code !== 0) { 38 console.log('test generateKey err information: ' + JSON.stringify(err)); 39 reject(err); 40 } else { 41 resolve(data); 42 } 43 }); 44 }); 45} 46 47async function publicDeriveInitFunc(srcKeyAlies, HuksOptions) { 48 await init(srcKeyAlies, HuksOptions) 49 .then((data) => { 50 console.log(`test init data ${JSON.stringify(data)}`); 51 handle = data.handle; 52 expect(data.errorCode == 0).assertTrue(); 53 }) 54 .catch((err) => { 55 console.log('test init err information: ' + JSON.stringify(err)); 56 expect(null).assertFail(); 57 }); 58 return HuksOptions; 59} 60 61function init(srcKeyAlies, HuksOptions) { 62 return new Promise((resolve, reject) => { 63 huks.init(srcKeyAlies, HuksOptions, function (err, data) { 64 if (err.code !== 0) { 65 console.log('test init err information: ' + JSON.stringify(err)); 66 reject(err); 67 } else { 68 resolve(data); 69 } 70 }); 71 }); 72} 73 74async function publicDeriveUpdateFunc(HuksOptions) { 75 await update(handle, HuksOptions) 76 .then((data) => { 77 console.log(`test update data ${JSON.stringify(data)}`); 78 expect(data.errorCode == 0).assertTrue(); 79 }) 80 .catch((err) => { 81 console.log('test update err information: ' + JSON.stringify(err)); 82 expect(null).assertFail(); 83 }); 84} 85 86function update(handle, HuksOptions) { 87 return new Promise((resolve, reject) => { 88 huks.update(handle, HuksOptions, function (err, data) { 89 if (err.code !== 0) { 90 console.log('test update err information: ' + JSON.stringify(err)); 91 reject(err); 92 } else { 93 resolve(data); 94 } 95 }); 96 }); 97} 98 99async function publicDeriveFinishAbortFunc(huksOptionsFinish, thirdInderfaceName) { 100 if (thirdInderfaceName == 'finish') { 101 await finish(handle, huksOptionsFinish) 102 .then((data) => { 103 console.log(`test finish data ${JSON.stringify(data)}`); 104 expect(data.errorCode == 0).assertTrue(); 105 }) 106 .catch((err) => { 107 console.log('test finish err information: ' + JSON.stringify(err)); 108 expect(null).assertFail(); 109 }); 110 } else { 111 let huksOptionsAbort = new Array({ 112 tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG, 113 value: HksKeyStorageType.HKS_STORAGE_TEMP, 114 }); 115 await abort(handle, huksOptionsAbort) 116 .then((data) => { 117 console.log(`test abort data ${JSON.stringify(data)}`); 118 expect(data.errorCode == 0).assertTrue(); 119 }) 120 .catch((err) => { 121 console.log('test abort err information: ' + JSON.stringify(err)); 122 expect(null).assertFail(); 123 }); 124 } 125} 126 127function abort(handle, huksOptionsAbort) { 128 return new Promise((resolve, reject) => { 129 huks.abort(handle, huksOptionsAbort, function (err, data) { 130 if (err.code !== 0) { 131 console.log('test abort err information: ' + JSON.stringify(err)); 132 reject(err); 133 } else { 134 resolve(data); 135 } 136 }); 137 }); 138} 139 140function finish(handle, huksOptionsFinish) { 141 return new Promise((resolve, reject) => { 142 huks.finish(handle, huksOptionsFinish, function (err, data) { 143 if (err.code !== 0) { 144 console.log('test generateKey err information: ' + JSON.stringify(err)); 145 reject(err); 146 } else { 147 resolve(data); 148 } 149 }); 150 }); 151} 152 153async function publicDeriveDeleteFunc(srcKeyAlies, HuksOptions) { 154 await deleteKey(srcKeyAlies, HuksOptions) 155 .then((data) => { 156 console.log(`test deleteKey data ${JSON.stringify(data)}`); 157 expect(data.errorCode == 0).assertTrue(); 158 }) 159 .catch((err) => { 160 console.log('test deleteKey err information: ' + JSON.stringify(err)); 161 expect(null).assertFail(); 162 }); 163} 164 165function deleteKey(srcKeyAlies, HuksOptions) { 166 return new Promise((resolve, reject) => { 167 huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) { 168 if (err.code !== 0) { 169 console.log('test deleteKey err information: ' + JSON.stringify(err)); 170 reject(err); 171 } else { 172 resolve(data); 173 } 174 }); 175 }); 176} 177 178async function publicDeriveFunc(srcKeyAlies, HuksOptions, huksOptionsFinish, thirdInderfaceName) { 179 try { 180 await publicDeriveGenFunc(srcKeyAlies, HuksOptions); 181 HuksOptions.properties.splice(0, 1, paramPublic.HuksDeriveHKDF.HuksKeyAlgHKDF); 182 HuksOptions.properties.splice(3, 1, paramPublic.HuksDeriveHKDF.HuksKeyDERIVEKEYSIZE); 183 184 await publicDeriveInitFunc(srcKeyAlies, HuksOptions); 185 await publicDeriveUpdateFunc(HuksOptions); 186 await publicDeriveFinishAbortFunc(huksOptionsFinish, thirdInderfaceName); 187 188 HuksOptions.properties.splice(0, 1, paramPublic.HuksDeriveHKDF.HuksKeyAlgAES); 189 HuksOptions.properties.splice(3, 1, paramPublic.HuksDeriveHKDF.HuksKeyHKDFSize128); 190 await publicDeriveDeleteFunc(srcKeyAlies, HuksOptions); 191 } catch (e) { 192 expect(null).assertFail(); 193 } 194} 195 196export { publicDeriveFunc }; 197