• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 huks from '@ohos.security.huks';
16import { HksTag, HksKeyStorageType, HksKeyAlg } from '../publicParam';
17import { HuksAgreeECDH } from './publicAgreeParam.js';
18import { stringToUint8Array } from '../publicFunc.js';
19import { expect } from 'deccjsunit/index';
20let exportKeyFrist;
21let exportKeySecond;
22let handle;
23
24async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
25  await generateKey(srcKeyAlies, 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 publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
51  await exportKey(srcKeyAlies, HuksOptions)
52    .then((data) => {
53      console.log(`test exportKey data: ${JSON.stringify(data)}`);
54      if (exportKeys == 1) {
55        exportKeyFrist = data.outData;
56      } else {
57        exportKeySecond = data.outData;
58      }
59    })
60    .catch((err) => {
61      console.log('test exportKey err information: ' + JSON.stringify(err));
62      expect(null).assertFail();
63    });
64}
65
66function exportKey(srcKeyAlies, HuksOptions) {
67  return new Promise((resolve, reject) => {
68    huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
69      console.log(`test exportKey data: ${JSON.stringify(data)}`);
70      if (err.code !== 0) {
71        console.log('test exportKey err information: ' + JSON.stringify(err));
72        reject(err);
73      } else {
74        resolve(data);
75      }
76    });
77  });
78}
79
80async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
81  await init(srcKeyAlies, HuksOptions)
82    .then((data) => {
83      console.log(`test init data ${JSON.stringify(data)}`);
84      handle = data.handle;
85      expect(data.errorCode == 0).assertTrue();
86    })
87    .catch((err) => {
88      console.log('test init err information: ' + JSON.stringify(err));
89      expect(null).assertFail();
90    });
91}
92
93function init(srcKeyAlies, HuksOptions) {
94  return new Promise((resolve, reject) => {
95    huks.init(srcKeyAlies, HuksOptions, function (err, data) {
96      if (err.code !== 0) {
97        console.log('test init err information: ' + JSON.stringify(err));
98        reject(err);
99      } else {
100        resolve(data);
101      }
102    });
103  });
104}
105
106async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
107  let _inData = HuksOptions.inData;
108  if (exportKey == 1) {
109    HuksOptions.inData = exportKeySecond;
110  } else {
111    HuksOptions.inData = exportKeyFrist;
112  }
113  await update(handle, HuksOptions)
114    .then((data) => {
115      console.log(`test update data ${JSON.stringify(data)}`);
116      expect(data.errorCode == 0).assertTrue();
117    })
118    .catch((err) => {
119      console.log('test update err information: ' + JSON.stringify(err));
120      expect(null).assertFail();
121    });
122  HuksOptions.inData = _inData;
123}
124
125function update(handle, HuksOptions) {
126  return new Promise((resolve, reject) => {
127    huks.update(handle, HuksOptions, function (err, data) {
128      if (err.code !== 0) {
129        console.log('test update err information: ' + JSON.stringify(err));
130        reject(err);
131      } else {
132        resolve(data);
133      }
134    });
135  });
136}
137
138async function publicAgreeFinishAbortFunc(HuksOptionsFinish, thirdInderfaceName) {
139  if (thirdInderfaceName == 'finish') {
140    await finish(handle, HuksOptionsFinish)
141      .then((data) => {
142        console.log(`test finish data ${JSON.stringify(data)}`);
143        expect(data.errorCode == 0).assertTrue();
144      })
145      .catch((err) => {
146        console.log('test finish err information: ' + JSON.stringify(err));
147        expect(null).assertFail();
148      });
149  } else {
150    let HuksOptionsAbort = new Array({
151      tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
152      value: HksKeyStorageType.HKS_STORAGE_TEMP,
153    });
154    await abort(handle, HuksOptionsAbort)
155      .then((data) => {
156        console.log(`test abort data ${JSON.stringify(data)}`);
157        expect(data.errorCode == 0).assertTrue();
158      })
159      .catch((err) => {
160        console.log('test abort err information: ' + JSON.stringify(err));
161        expect(null).assertFail();
162      });
163  }
164}
165
166function abort(handle, HuksOptionsAbort) {
167  return new Promise((resolve, reject) => {
168    huks.abort(handle, HuksOptionsAbort, function (err, data) {
169      if (err.code !== 0) {
170        console.log('test abort err information: ' + JSON.stringify(err));
171        reject(err);
172      } else {
173        resolve(data);
174      }
175    });
176  });
177}
178
179function finish(handle, HuksOptionsFinish) {
180  return new Promise((resolve, reject) => {
181    huks.finish(handle, HuksOptionsFinish, function (err, data) {
182      if (err.code !== 0) {
183        console.log('test finish err information: ' + JSON.stringify(err));
184        reject(err);
185      } else {
186        resolve(data);
187      }
188    });
189  });
190}
191
192async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
193  await deleteKey(srcKeyAlies, HuksOptions)
194    .then((data) => {
195      console.log(`test deleteKey data ${JSON.stringify(data)}`);
196      expect(data.errorCode == 0).assertTrue();
197    })
198    .catch((err) => {
199      console.log('test deleteKey err information: ' + JSON.stringify(err));
200      expect(null).assertFail();
201    });
202}
203
204function deleteKey(srcKeyAlies, HuksOptions) {
205  return new Promise((resolve, reject) => {
206    huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
207      if (err.code !== 0) {
208        console.log('test deleteKey err information: ' + JSON.stringify(err));
209        reject(err);
210      } else {
211        resolve(data);
212      }
213    });
214  });
215}
216
217async function publicAgreeFunc(
218  srcKeyAliesFirst,
219  srcKeyAliesSecond,
220  HuksOptions,
221  HuksOptionsFinish,
222  thirdInderfaceName,
223  isDeleteFinalKeys,
224) {
225  try {
226    await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions);
227    await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions);
228    await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1);
229    await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2);
230
231    if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_ECC) {
232      HuksOptions.properties.splice(0, 1, HuksAgreeECDH.HuksKeyAlgECDH);
233      HuksOptions.properties.splice(3, 1);
234      HuksOptions.properties.splice(4, 1);
235      HuksOptions.properties.splice(5, 1);
236    }
237
238    let HuksOptionsInit = JSON.parse(JSON.stringify(HuksOptions));
239    HuksOptionsInit.properties.splice(2, 1, HuksOptionsFinish.properties[3])
240
241    //1st Agree
242    HuksOptionsFinish.properties.splice(6, 1, {
243      tag: HksTag.HKS_TAG_KEY_ALIAS,
244      value: stringToUint8Array(srcKeyAliesFirst + 'final'),
245    });
246    await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptionsInit);
247    await publicAgreeUpdateFunc(HuksOptions, 1);
248    await publicAgreeFinishAbortFunc(HuksOptionsFinish, thirdInderfaceName);
249
250    //2nd Agree
251    let tempHuksOptionsFinish = HuksOptionsFinish;
252    let HuksOptionsFinishSecond = tempHuksOptionsFinish;
253    HuksOptionsFinishSecond.properties.splice(6, 1, {
254      tag: HksTag.HKS_TAG_KEY_ALIAS,
255      value: stringToUint8Array(srcKeyAliesSecond + 'final'),
256    });
257    await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptionsInit);
258    await publicAgreeUpdateFunc(HuksOptions, 2);
259    await publicAgreeFinishAbortFunc(HuksOptionsFinishSecond, thirdInderfaceName);
260
261    //delete
262    await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions);
263    await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions);
264    if (thirdInderfaceName == 'finish' && isDeleteFinalKeys) {
265      await publicAgreeDeleteFunc(srcKeyAliesFirst + 'final', HuksOptions);
266      await publicAgreeDeleteFunc(srcKeyAliesSecond + 'final', HuksOptions);
267    }
268  } catch (e) {
269    expect(null).assertFail();
270  }
271}
272export { publicAgreeFunc };
273