hks_xts_common.test.js 7.9 KB
Newer Older
H
huks  
hu-jixiang1 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import hks from '@ohos.security.huks'

H
hu-jixiang1 已提交
18 19
export const alias = 'alias';
export const aliasA = 'aliasA';
H
huks  
hu-jixiang1 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
export var aliasB = 'aliasB';
export var targetAlias = 'targetAlias';

export var useLib = 'openssl'; /** openssl or mbedtls */

export var successStr = 'SUCCESS';
export var failStr = 'FAIL';

export var loop = 200;
export var timer = 1000;

export var emptyOption = makeEmptyOption();
export var inDataOption = makeInDataOption();

export function makeGenerateKeyOption(alg, size, purpose, padding, mode, digest) {
  var properties = new Array();
  properties[0] = makeAlgTagProperty(alg);
  properties[1] = makeSizeProperty(size);
  properties[2] = makePurposeProperty(purpose);
H
hu-jixiang1 已提交
39
  if (purpose == (hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT)) {
H
huks  
hu-jixiang1 已提交
40 41
    properties[3] = makePaddingProperty(padding);
    properties[4] = makeModeProperty(mode);
H
hu-jixiang1 已提交
42
    if (alg == hks.HuksKeyAlg.HUKS_ALG_RSA) {
H
huks  
hu-jixiang1 已提交
43 44 45
      properties[5] = makeDigestProperty(digest);
      properties[6] = makeKeyGenerateType();
    }
H
hu-jixiang1 已提交
46
  } else if (purpose == (hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_SIGN | hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_VERIFY)) {
H
huks  
hu-jixiang1 已提交
47 48
    properties[3] = makePaddingProperty(padding);
    properties[4] = makeDigestProperty(digest);
H
hu-jixiang1 已提交
49
  } else if (purpose == hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_MAC) {
H
huks  
hu-jixiang1 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    properties[3] = makeDigestProperty(digest);
  } else {
    properties[3] = makeDigestProperty(digest);
  }
  var option = {
    properties: properties
  };
  return option;
};

export function makeEncryptAndDecryptOption(alg, purpose, padding, mode, size, digest, text) {
  var properties = new Array();
  properties[0] = makeAlgTagProperty(alg);
  properties[1] = makePurposeProperty(purpose);
  properties[2] = makePaddingProperty(padding);
  properties[3] = makeModeProperty(mode);
H
hu-jixiang1 已提交
66
  if (alg == hks.HuksKeyAlg.HUKS_ALG_AES) {
H
huks  
hu-jixiang1 已提交
67
    properties[4] = makeIV();
H
hu-jixiang1 已提交
68
    if (mode == hks.HuksCipherMode.HUKS_MODE_GCM) {
H
huks  
hu-jixiang1 已提交
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
      properties[5] = makeAAD();
      properties[6] = makeNonce();
    }
  } else {
    properties[4] = makeIsKeyAlias();
    properties[5] = makeSizeProperty(size);
    properties[6] = makeDigestProperty(digest);
  } 
  var option = {
    properties: properties,
    inData: text
  };
  return option;
};

export function makeSignAndVerifyOption(alg, size, purpose, padding, digest, text) {
  var properties = new Array();
  properties[0] = makeAlgTagProperty(alg);
  properties[1] = makeSizeProperty(size);
  properties[2] = makePurposeProperty(purpose);
  properties[3] = makePaddingProperty(padding);
  properties[4] = makeDigestProperty(digest);
  var option = {
    properties: properties,
    inData: text
  };
  return option;
};

export function makeRandomArr(size) {
  var arr = new Uint8Array(size);
  for (var i = 0; i < size; i++) {
    arr[i] = Math.floor(Math.random() * 10);
  }
  return arr;
};

export function makePlainTextSize(size,padding,digest){
  var plainTextSize = 0;
H
hu-jixiang1 已提交
108 109
  if (padding == hks.HuksKeyPadding.HUKS_PADDING_OAEP) {
    if (digest == hks.HuksKeyDigest.HUKS_DIGEST_SHA224 && size == hks.HuksKeySize.HUKS_RSA_KEY_SIZE_512) {
H
huks  
hu-jixiang1 已提交
110 111 112 113
      plainTextSize = 4;
    } else {
      plainTextSize = 16;
    }
H
hu-jixiang1 已提交
114
  } else if (padding == hks.HuksKeyPadding.HUKS_PADDING_PKCS1_V1_5) {
H
huks  
hu-jixiang1 已提交
115 116 117
    plainTextSize = 8;
  } else {
    switch (size) {
H
hu-jixiang1 已提交
118
      case hks.HuksKeySize.HUKS_RSA_KEY_SIZE_512:
H
huks  
hu-jixiang1 已提交
119 120
        plainTextSize = 64;
        break;
H
hu-jixiang1 已提交
121
      case hks.HuksKeySize.HUKS_RSA_KEY_SIZE_768:
H
huks  
hu-jixiang1 已提交
122 123
        plainTextSize = 96;
        break;
H
hu-jixiang1 已提交
124
      case hks.HuksKeySize.HUKS_RSA_KEY_SIZE_1024:
H
huks  
hu-jixiang1 已提交
125 126
        plainTextSize = 128;
        break;
H
hu-jixiang1 已提交
127
      case hks.HuksKeySize.HUKS_RSA_KEY_SIZE_2048:
H
huks  
hu-jixiang1 已提交
128 129
        plainTextSize = 256;
        break;
H
hu-jixiang1 已提交
130
      case hks.HuksKeySize.HUKS_RSA_KEY_SIZE_3072:
H
huks  
hu-jixiang1 已提交
131 132
        plainTextSize = 384;
        break;
H
hu-jixiang1 已提交
133
      case hks.HuksKeySize.HUKS_RSA_KEY_SIZE_4096:
H
huks  
hu-jixiang1 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        plainTextSize = 512;
        break;
      default:
        plainTextSize = 512;
    }
  }
  return plainTextSize;
};

export function makeImportOption(alg, size, purpose, padding, mode, digest, publicKey) {
  var properties = new Array();
  properties[0] = makeAlgTagProperty(alg);
  properties[1] = makeSizeProperty(size);
  properties[2] = makePurposeProperty(purpose);
  properties[3] = makePaddingProperty(padding);
H
hu-jixiang1 已提交
149 150
  if (alg == hks.HuksKeyAlg.HUKS_ALG_RSA || alg == hks.HuksKeyAlg.HUKS_ALG_DSA || alg == hks.HuksKeyAlg.HUKS_ALG_ECC) {
    if (purpose == (hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_ENCRYPT | hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_DECRYPT)) {
H
huks  
hu-jixiang1 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
      properties[4] = makeModeProperty(mode);
      properties[5] = makeDigestProperty(digest);
      properties[6] = makeKeyGenerateType();
    } else {
      properties[4] = makeDigestProperty(digest);
    }
  }
  var options = {
    properties: properties,
    inData: publicKey
  };
  return options;
};


function makeAlgTagProperty(alg) {
  var property = {
H
hu-jixiang1 已提交
168
    tag: hks.HuksTag.HUKS_TAG_ALGORITHM,
H
huks  
hu-jixiang1 已提交
169 170 171 172 173 174 175
    value: alg
  }
  return property;
};

function makeSizeProperty(size) {
  var property = {
H
hu-jixiang1 已提交
176
    tag: hks.HuksTag.HUKS_TAG_KEY_SIZE,
H
huks  
hu-jixiang1 已提交
177 178 179 180 181 182 183
    value: size
  };
  return property;
};

function makePurposeProperty(purpose) {
  var property = {
H
hu-jixiang1 已提交
184
    tag: hks.HuksTag.HUKS_TAG_PURPOSE,
H
huks  
hu-jixiang1 已提交
185 186 187 188 189 190 191
    value: purpose
  };
  return property;
};

function makePaddingProperty(padding) {
  var property = {
H
hu-jixiang1 已提交
192
    tag: hks.HuksTag.HUKS_TAG_PADDING,
H
huks  
hu-jixiang1 已提交
193 194 195 196 197 198 199
    value: padding
  };
  return property;
};

function makeModeProperty(mode) {
  var property = {
H
hu-jixiang1 已提交
200
    tag: hks.HuksTag.HUKS_TAG_BLOCK_MODE,
H
huks  
hu-jixiang1 已提交
201 202 203 204 205 206 207
    value: mode
  };
  return property;
};

function makeDigestProperty(digest) {
  var property = {
H
hu-jixiang1 已提交
208
    tag: hks.HuksTag.HUKS_TAG_DIGEST,
H
huks  
hu-jixiang1 已提交
209 210 211 212 213 214 215
    value: digest
  };
  return property;
};

function makeKeyGenerateType() {
  var property = {
H
hu-jixiang1 已提交
216 217
    tag: hks.HuksTag.HUKS_TAG_KEY_GENERATE_TYPE,
    value: hks.HuksKeyGenerateType.HUKS_KEY_GENERATE_TYPE_DEFAULT
H
huks  
hu-jixiang1 已提交
218 219 220 221 222 223
  };
  return property;
};

function makeIV() {
  var property = {
H
hu-jixiang1 已提交
224
    tag: hks.HuksTag.HUKS_TAG_IV,
H
huks  
hu-jixiang1 已提交
225 226 227 228 229 230 231
    value: new Uint8Array(16)
  };
  return property;
};

function makeAAD() {
  var property = {
H
hu-jixiang1 已提交
232
    tag: hks.HuksTag.HUKS_TAG_ASSOCIATED_DATA,
H
huks  
hu-jixiang1 已提交
233 234 235 236 237 238 239
    value: new Uint8Array(16)
  };
  return property;
};

function makeNonce() {
  var property = {
H
hu-jixiang1 已提交
240
    tag: hks.HuksTag.HUKS_TAG_NONCE,
H
huks  
hu-jixiang1 已提交
241 242 243 244 245 246 247
    value: new Uint8Array(16)
  };
  return property;
};

function makeIsKeyAlias() {
  var property = {
H
hu-jixiang1 已提交
248
    tag: hks.HuksTag.HUKS_TAG_IS_KEY_ALIAS,
H
huks  
hu-jixiang1 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    value: true
  };
  return property;
};

function makeEmptyOption() {
  var emptyOption = {
    properties: []
  };
  return emptyOption;
};

function makeInDataOption() {
  var option = {
    properties: [],
    inData: new Uint8Array(16)
  };
  return option;
};

export function makeMacOption(plaintText) {
  var properties = new Array();
  properties[0] = {
H
hu-jixiang1 已提交
272 273
    tag: hks.HuksTag.HUKS_TAG_ALGORITHM,
    value: hks.HuksKeyAlg.HUKS_ALG_HMAC
H
huks  
hu-jixiang1 已提交
274 275
  };
  properties[1] = {
H
hu-jixiang1 已提交
276 277
    tag: hks.HuksTag.HUKS_TAG_PURPOSE,
    value: hks.HuksKeyPurpose.HUKS_KEY_PURPOSE_MAC
H
huks  
hu-jixiang1 已提交
278 279
  };
  properties[2] = {
H
hu-jixiang1 已提交
280 281
    tag: hks.HuksTag.HUKS_TAG_DIGEST,
    value: hks.HuksKeyDigest.HUKS_DIGEST_SHA1
H
huks  
hu-jixiang1 已提交
282 283 284 285 286 287 288 289 290 291 292
  };
  var options = {
    properties: properties,
    inData: plaintText
  };
  return options;
};

export function makeAgreeOptions(publicKey) {
  var properties = new Array();
  properties[0] = {
H
hu-jixiang1 已提交
293 294
    tag: hks.HuksTag.HUKS_TAG_ALGORITHM,
    value: hks.HuksKeyAlg.HUKS_ALG_ECDH
H
huks  
hu-jixiang1 已提交
295 296
  };
  properties[1] = {
H
hu-jixiang1 已提交
297 298
    tag: hks.HuksTag.HUKS_TAG_KEY_SIZE,
    value: hks.HuksKeySize.HUKS_ECC_KEY_SIZE_224
H
huks  
hu-jixiang1 已提交
299 300 301 302 303 304 305
  };
  var options = {
    properties: properties,
    inData: publicKey
  };
  return options;
};