• 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 huks
26    .generateKey(srcKeyAlies, 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 publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKey) {
38  await huks
39    .exportKey(srcKeyAlies, HuksOptions)
40    .then((data) => {
41      console.log(`test exportKey data: ${JSON.stringify(data)}`);
42      if (exportKey == 1) {
43        exportKeyFrist = data.outData;
44      } else {
45        exportKeySecond = data.outData;
46      }
47    })
48    .catch((err) => {
49      console.log('test exportKey err information: ' + JSON.stringify(err));
50      expect(null).assertFail();
51    });
52}
53
54async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
55  await huks
56    .init(srcKeyAlies, HuksOptions)
57    .then((data) => {
58      console.log(`test init data ${JSON.stringify(data)}`);
59      handle = data.handle;
60      expect(data.errorCode == 0).assertTrue();
61    })
62    .catch((err) => {
63      console.log('test init err information: ' + JSON.stringify(err));
64      expect(null).assertFail();
65    });
66}
67
68async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
69  let _inData = HuksOptions.inData;
70  if (exportKey == 1) {
71    HuksOptions.inData = exportKeySecond;
72  } else {
73    HuksOptions.inData = exportKeyFrist;
74  }
75  await huks
76    .update(handle, HuksOptions)
77    .then((data) => {
78      console.log(`test update data ${JSON.stringify(data)}`);
79      expect(data.errorCode == 0).assertTrue();
80    })
81    .catch((err) => {
82      console.log('test update err information: ' + JSON.stringify(err));
83      expect(null).assertFail();
84    });
85  HuksOptions.inData = _inData;
86}
87
88async function publicAgreeFinishAbortFunc(HuksOptionsFinish, thirdInderfaceName) {
89  if (thirdInderfaceName == 'finish') {
90    await huks
91      .finish(handle, HuksOptionsFinish)
92      .then((data) => {
93        console.log(`test finish data ${JSON.stringify(data)}`);
94        expect(data.errorCode == 0).assertTrue();
95      })
96      .catch((err) => {
97        console.log('test finish err information: ' + JSON.stringify(err));
98        expect(null).assertFail();
99      });
100  } else {
101    let HuksOptionsAbort = new Array({
102      tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
103      value: HksKeyStorageType.HKS_STORAGE_TEMP,
104    });
105    await huks
106      .abort(handle, HuksOptionsAbort)
107      .then((data) => {
108        console.log(`test abort data ${JSON.stringify(data)}`);
109        expect(data.errorCode == 0).assertTrue();
110      })
111      .catch((err) => {
112        console.log('test abort err information: ' + JSON.stringify(err));
113        expect(null).assertFail();
114      });
115  }
116}
117
118async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
119  await huks
120    .deleteKey(srcKeyAlies, HuksOptions)
121    .then((data) => {
122      console.log(`test deleteKey data ${JSON.stringify(data)}`);
123      expect(data.errorCode == 0).assertTrue();
124    })
125    .catch((err) => {
126      console.log('test deleteKey err information: ' + JSON.stringify(err));
127      expect(null).assertFail();
128    });
129}
130
131async function publicAgreeFunc(
132  srcKeyAliesFrist,
133  srcKeyAliesSecond,
134  HuksOptions,
135  HuksOptionsFinish,
136  thirdInderfaceName
137) {
138  try {
139    await publicAgreeGenFunc(srcKeyAliesFrist, HuksOptions);
140    await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions);
141    await publicAgreeExport1Func(srcKeyAliesFrist, HuksOptions, 1);
142    await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2);
143
144    if (HuksOptions.properties[0].value == HksKeyAlg.HKS_ALG_ECC) {
145      HuksOptions.properties.splice(0, 1, HuksAgreeECDH.HuksKeyAlgECDH);
146      HuksOptions.properties.splice(3, 1);
147      HuksOptions.properties.splice(4, 1);
148      HuksOptions.properties.splice(5, 1);
149    }
150
151    await publicAgreeInitFunc(srcKeyAliesFrist, HuksOptions);
152    await publicAgreeUpdateFunc(HuksOptions, 1);
153    await publicAgreeFinishAbortFunc(HuksOptionsFinish, thirdInderfaceName);
154
155    let tempHuksOptionsFinish = HuksOptionsFinish;
156    let HuksOptionsFinishSecond = tempHuksOptionsFinish;
157    HuksOptionsFinishSecond.properties.splice(6, 1, {
158      tag: HksTag.HKS_TAG_KEY_ALIAS,
159      value: stringToUint8Array(srcKeyAliesSecond + 'final'),
160    });
161
162    await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions);
163    await publicAgreeUpdateFunc(HuksOptions, 2);
164    await publicAgreeFinishAbortFunc(HuksOptionsFinishSecond, thirdInderfaceName);
165
166    await publicAgreeDeleteFunc(srcKeyAliesFrist, HuksOptions);
167    if (thirdInderfaceName == 'finish') {
168      await publicAgreeDeleteFunc(srcKeyAliesSecond + 'final', HuksOptions);
169    }
170    await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions);
171  } catch (e) {
172    expect(null).assertFail();
173  }
174}
175
176export { publicAgreeFunc };
177