提交 03509522 编写于 作者: H hu-jixiang1

release 3.1basic

Signed-off-by: Nhu-jixiang1 <hujixiang1@huawei.com>
Change-Id: I7ce4dc50e00e1b4da37ccddf8936fa00b6f75d2e
上级 0e98fad6
...@@ -20,7 +20,7 @@ group("security") { ...@@ -20,7 +20,7 @@ group("security") {
"access_token/AccessTokenTest_NoPermission_js:ActsAccessTokenJSApiTestNoPermission", "access_token/AccessTokenTest_NoPermission_js:ActsAccessTokenJSApiTestNoPermission",
"access_token/AccessTokenTest_Normal_js:ActsAccessTokenJSApiTestNormal", "access_token/AccessTokenTest_Normal_js:ActsAccessTokenJSApiTestNormal",
"huks_standard", "huks_standard",
"security_huks", "security_huks_basic",
] ]
} }
} }
{
"description": "Configuration for huksAgreeBasicCallback js api Tests",
"driver": {
"type": "JSUnitTest",
"test-timeout": "180000",
"package": "com.example.huksagreebasiccallback",
"shell-timeout": "60000"
},
"kits": [
{
"test-file-name": [
"ActsHuksAgreeBasicCallbackJSApiTest.hap"
],
"type": "AppInstallKit",
"cleanup-apps": true
}
]
}
/*
* 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 { describe, it, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData63 = Data.Date63KB
let srcData63Kb = stringToUint8Array(srcData63)
let HksKeyAlg = {
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_DH_KEY_SIZE_128: 128,
HKS_DH_KEY_SIZE_192: 192,
HKS_DH_KEY_SIZE_256: 256,
HKS_DH_KEY_SIZE_512: 512,
HKS_DH_KEY_SIZE_2048: 2048,
HKS_DH_KEY_SIZE_3072: 3072,
HKS_DH_KEY_SIZE_4096: 4096,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree002 = {
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_192,
},
HuksKeyAlgDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_DH,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
HuksKeyPurposeDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyDHSize2048: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_2048,
},
HuksKeyDHSize3072: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_3072,
},
HuksKeyDHSize4096: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_4096,
},
}
let HuksOptions63kb = {
properties: new Array(
HuksAgree002.HuksKeyAlgDH,
HuksAgree002.HuksKeyPurposeDH,
HuksAgree002.HuksKeyDHSize2048
),
inData: srcData63Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeDHCallbackJsunit', function () {
it('testAgreeDHAbort63KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeDHSize2048Abort63KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeDHSize2048Abort63KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree002.HuksKeySTORAGE,
HuksAgree002.HuksKeyISKEYALIAS,
HuksAgree002.HuksKeyALGORITHMAES,
HuksAgree002.HuksKeySIZE256,
HuksAgree002.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree002.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree002.HuksKeyPADDINGNONE,
HuksAgree002.HuksKeyBLOCKMODEECB
),
inData: srcData63Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions63kb,
huksOptionsFinish,
'abort'
)
done()
})
})
/*
* 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 { describe, it, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData65 = Data.Date65KB
let srcData65Kb = stringToUint8Array(srcData65)
let HksKeyAlg = {
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_DH_KEY_SIZE_128: 128,
HKS_DH_KEY_SIZE_192: 192,
HKS_DH_KEY_SIZE_256: 256,
HKS_DH_KEY_SIZE_512: 512,
HKS_DH_KEY_SIZE_2048: 2048,
HKS_DH_KEY_SIZE_3072: 3072,
HKS_DH_KEY_SIZE_4096: 4096,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree002 = {
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_192,
},
HuksKeyAlgDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_DH,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
HuksKeyPurposeDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyDHSize2048: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_2048,
},
HuksKeyDHSize3072: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_3072,
},
HuksKeyDHSize4096: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_4096,
},
}
let HuksOptions65kb = {
properties: new Array(
HuksAgree002.HuksKeyAlgDH,
HuksAgree002.HuksKeyPurposeDH,
HuksAgree002.HuksKeyDHSize2048
),
inData: srcData65Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeDHCallbackJsunit', function () {
it('testAgreeDHAbort65KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeDHSize2048Abort65KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeDHSize2048Abort65KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree002.HuksKeySTORAGE,
HuksAgree002.HuksKeyISKEYALIAS,
HuksAgree002.HuksKeyALGORITHMAES,
HuksAgree002.HuksKeySIZE256,
HuksAgree002.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree002.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree002.HuksKeyPADDINGNONE,
HuksAgree002.HuksKeyBLOCKMODEECB
),
inData: srcData65Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions65kb,
huksOptionsFinish,
'abort'
)
done()
})
})
/*
* 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 { describe, it, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData63 = Data.Date63KB
let srcData63Kb = stringToUint8Array(srcData63)
let HksKeyAlg = {
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_DH_KEY_SIZE_128: 128,
HKS_DH_KEY_SIZE_192: 192,
HKS_DH_KEY_SIZE_256: 256,
HKS_DH_KEY_SIZE_512: 512,
HKS_DH_KEY_SIZE_2048: 2048,
HKS_DH_KEY_SIZE_3072: 3072,
HKS_DH_KEY_SIZE_4096: 4096,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree002 = {
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_192,
},
HuksKeyAlgDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_DH,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
HuksKeyPurposeDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyDHSize2048: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_2048,
},
HuksKeyDHSize3072: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_3072,
},
HuksKeyDHSize4096: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_4096,
},
}
let HuksOptions63kb = {
properties: new Array(
HuksAgree002.HuksKeyAlgDH,
HuksAgree002.HuksKeyPurposeDH,
HuksAgree002.HuksKeyDHSize2048
),
inData: srcData63Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond + 'final', HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeDHCallbackJsunit', function () {
it('testAgreeDHFinish63KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeDHSize2048Finish63KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeDHSize2048Finish63KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree002.HuksKeySTORAGE,
HuksAgree002.HuksKeyISKEYALIAS,
HuksAgree002.HuksKeyALGORITHMAES,
HuksAgree002.HuksKeySIZE256,
HuksAgree002.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree002.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree002.HuksKeyPADDINGNONE,
HuksAgree002.HuksKeyBLOCKMODEECB
),
inData: srcData63Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions63kb,
huksOptionsFinish,
'finish'
)
done()
})
})
/*
* 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 { describe, it, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData65 = Data.Date65KB
let srcData65Kb = stringToUint8Array(srcData65)
let HksKeyAlg = {
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_DH_KEY_SIZE_128: 128,
HKS_DH_KEY_SIZE_192: 192,
HKS_DH_KEY_SIZE_256: 256,
HKS_DH_KEY_SIZE_512: 512,
HKS_DH_KEY_SIZE_2048: 2048,
HKS_DH_KEY_SIZE_3072: 3072,
HKS_DH_KEY_SIZE_4096: 4096,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree002 = {
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_192,
},
HuksKeyAlgDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_DH,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
HuksKeyPurposeDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyDHSize2048: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_2048,
},
HuksKeyDHSize3072: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_3072,
},
HuksKeyDHSize4096: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_DH_KEY_SIZE_4096,
},
}
let HuksOptions65kb = {
properties: new Array(
HuksAgree002.HuksKeyAlgDH,
HuksAgree002.HuksKeyPurposeDH,
HuksAgree002.HuksKeyDHSize2048
),
inData: srcData65Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond + 'final', HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeDHCallbackJsunit', function () {
it('testAgreeDHFinish65KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeDHSize2048Finish65KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeDHSize2048Finish65KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree002.HuksKeySTORAGE,
HuksAgree002.HuksKeyISKEYALIAS,
HuksAgree002.HuksKeyALGORITHMAES,
HuksAgree002.HuksKeySIZE256,
HuksAgree002.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree002.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree002.HuksKeyPADDINGNONE,
HuksAgree002.HuksKeyBLOCKMODEECB
),
inData: srcData65Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions65kb,
huksOptionsFinish,
'finish'
)
done()
})
})
/*
* 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 { describe, it, beforeEach, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData63 = Data.Date63KB
let srcData63Kb = stringToUint8Array(srcData63)
let HksKeyAlg = {
HKS_ALG_ECDH: 100,
HKS_ALG_ECC: 2,
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_ECC_KEY_SIZE_128: 128,
HKS_ECC_KEY_SIZE_192: 192,
HKS_ECC_KEY_SIZE_224: 224,
HKS_ECC_KEY_SIZE_256: 256,
HKS_ECC_KEY_SIZE_384: 384,
HKS_ECC_KEY_SIZE_521: 521,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree001 = {
HuksKeyAlgECDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECDH,
},
HuksKeyAlgECC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECC,
},
HuksKeyECCSize224: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_224,
},
HuksKeyECCSize256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeyECCSize384: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_384,
},
HuksKeyECCSize521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeyECCDIGEST: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyECCPADDING: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyECCBLOCKMODE: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_192,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeECDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
}
let HuksOptions63kb = {
properties: new Array(
HuksAgree001.HuksKeyAlgECC,
HuksAgree001.HuksKeyPurposeECDH,
HuksAgree001.HuksKeyECCSize224,
HuksAgree001.HuksKeyECCDIGEST,
HuksAgree001.HuksKeyECCPADDING,
HuksAgree001.HuksKeyECCBLOCKMODE
),
inData: srcData63Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
HuksOptions.properties.splice(0, 1, HuksAgree001.HuksKeyAlgECDH)
HuksOptions.properties.splice(3, 1)
HuksOptions.properties.splice(4, 1)
HuksOptions.properties.splice(5, 1)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeECDHCallbackJsunit', function () {
it('testAgreeECDHAbort63KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeECDHSize224Abort63KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeECDHSize224Abort63KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree001.HuksKeySTORAGE,
HuksAgree001.HuksKeyISKEYALIAS,
HuksAgree001.HuksKeyALGORITHMAES,
HuksAgree001.HuksKeySIZE256,
HuksAgree001.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree001.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree001.HuksKeyPADDINGNONE,
HuksAgree001.HuksKeyBLOCKMODEECB
),
inData: srcData63Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions63kb,
huksOptionsFinish,
'abort'
)
done()
})
})
/*
* 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 { describe, it, beforeEach, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData65 = Data.Date65KB
let srcData65Kb = stringToUint8Array(srcData65)
let HksKeyAlg = {
HKS_ALG_ECDH: 100,
HKS_ALG_ECC: 2,
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_ECC_KEY_SIZE_128: 128,
HKS_ECC_KEY_SIZE_192: 192,
HKS_ECC_KEY_SIZE_224: 224,
HKS_ECC_KEY_SIZE_256: 256,
HKS_ECC_KEY_SIZE_384: 384,
HKS_ECC_KEY_SIZE_521: 521,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree001 = {
HuksKeyAlgECDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECDH,
},
HuksKeyAlgECC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECC,
},
HuksKeyECCSize224: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_224,
},
HuksKeyECCSize256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeyECCSize384: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_384,
},
HuksKeyECCSize521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeyECCDIGEST: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyECCPADDING: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyECCBLOCKMODE: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_192,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeECDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
}
let HuksOptions65kb = {
properties: new Array(
HuksAgree001.HuksKeyAlgECC,
HuksAgree001.HuksKeyPurposeECDH,
HuksAgree001.HuksKeyECCSize224,
HuksAgree001.HuksKeyECCDIGEST,
HuksAgree001.HuksKeyECCPADDING,
HuksAgree001.HuksKeyECCBLOCKMODE
),
inData: srcData65Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
HuksOptions.properties.splice(0, 1, HuksAgree001.HuksKeyAlgECDH)
HuksOptions.properties.splice(3, 1)
HuksOptions.properties.splice(4, 1)
HuksOptions.properties.splice(5, 1)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeECDHCallbackJsunit', function () {
it('testAgreeECDHAbort65KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeECDHSize224Abort65KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeECDHSize224Abort65KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree001.HuksKeySTORAGE,
HuksAgree001.HuksKeyISKEYALIAS,
HuksAgree001.HuksKeyALGORITHMAES,
HuksAgree001.HuksKeySIZE256,
HuksAgree001.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree001.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree001.HuksKeyPADDINGNONE,
HuksAgree001.HuksKeyBLOCKMODEECB
),
inData: srcData65Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions65kb,
huksOptionsFinish,
'abort'
)
done()
})
})
/*
* 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 { describe, it, beforeEach, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData63 = Data.Date63KB
let srcData63Kb = stringToUint8Array(srcData63)
let HksKeyAlg = {
HKS_ALG_ECDH: 100,
HKS_ALG_ECC: 2,
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_ECC_KEY_SIZE_128: 128,
HKS_ECC_KEY_SIZE_192: 192,
HKS_ECC_KEY_SIZE_224: 224,
HKS_ECC_KEY_SIZE_256: 256,
HKS_ECC_KEY_SIZE_384: 384,
HKS_ECC_KEY_SIZE_521: 521,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree001 = {
HuksKeyAlgECDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECDH,
},
HuksKeyAlgECC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECC,
},
HuksKeyECCSize224: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_224,
},
HuksKeyECCSize256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeyECCSize384: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_384,
},
HuksKeyECCSize521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeyECCDIGEST: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyECCPADDING: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyECCBLOCKMODE: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_192,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeECDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
}
let HuksOptions63kb = {
properties: new Array(
HuksAgree001.HuksKeyAlgECC,
HuksAgree001.HuksKeyPurposeECDH,
HuksAgree001.HuksKeyECCSize224,
HuksAgree001.HuksKeyECCDIGEST,
HuksAgree001.HuksKeyECCPADDING,
HuksAgree001.HuksKeyECCBLOCKMODE
),
inData: srcData63Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
HuksOptions.properties.splice(0, 1, HuksAgree001.HuksKeyAlgECDH)
HuksOptions.properties.splice(3, 1)
HuksOptions.properties.splice(4, 1)
HuksOptions.properties.splice(5, 1)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond + 'final', HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeECDHCallbackJsunit', function () {
it('testAgreeECDHFinish63KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeECDHSize224Finish63KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeECDHSize224Finish63KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree001.HuksKeySTORAGE,
HuksAgree001.HuksKeyISKEYALIAS,
HuksAgree001.HuksKeyALGORITHMAES,
HuksAgree001.HuksKeySIZE256,
HuksAgree001.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree001.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree001.HuksKeyPADDINGNONE,
HuksAgree001.HuksKeyBLOCKMODEECB
),
inData: srcData63Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions63kb,
huksOptionsFinish,
'finish'
)
done()
})
})
/*
* 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 { describe, it, beforeEach, expect } from 'deccjsunit/index'
import huks from '@ohos.security.huks'
import Data from '../../../../../../../utils/data.json'
let exportKeyFrist
let exportKeySecond
let handle = {}
let handle1
let handle2
let finishDataFrist
let finishDataSecond
let srcData65 = Data.Date65KB
let srcData65Kb = stringToUint8Array(srcData65)
let HksKeyAlg = {
HKS_ALG_ECDH: 100,
HKS_ALG_ECC: 2,
HKS_ALG_DH: 103,
HKS_ALG_AES: 20,
HKS_ALG_HMAC: 50,
}
let HksKeyPurpose = {
HKS_KEY_PURPOSE_ENCRYPT: 1,
HKS_KEY_PURPOSE_DECRYPT: 2,
HKS_KEY_PURPOSE_AGREE: 256,
HKS_KEY_PURPOSE_DERIVE: 16,
HKS_KEY_PURPOSE_MAC: 128,
}
let HksKeySize = {
HKS_ECC_KEY_SIZE_128: 128,
HKS_ECC_KEY_SIZE_192: 192,
HKS_ECC_KEY_SIZE_224: 224,
HKS_ECC_KEY_SIZE_256: 256,
HKS_ECC_KEY_SIZE_384: 384,
HKS_ECC_KEY_SIZE_521: 521,
}
let HksKeyDigest = {
HKS_DIGEST_NONE: 0,
HKS_DIGEST_MD5: 1,
HKS_DIGEST_SHA1: 10,
HKS_DIGEST_SHA224: 11,
HKS_DIGEST_SHA256: 12,
HKS_DIGEST_SHA384: 13,
HKS_DIGEST_SHA512: 14,
}
let HksKeyPadding = {
HKS_PADDING_NONE: 0,
HKS_PADDING_OAEP: 1,
HKS_PADDING_PSS: 2,
HKS_PADDING_PKCS1_V1_5: 3,
HKS_PADDING_PKCS5: 4,
HKS_PADDING_PKCS7: 5,
}
let HksCipherMode = {
HKS_MODE_ECB: 1,
HKS_MODE_CBC: 2,
HKS_MODE_CTR: 3,
HKS_MODE_OFB: 4,
HKS_MODE_CCM: 31,
HKS_MODE_GCM: 32,
}
let HksTagType = {
HKS_TAG_TYPE_UINT: 2 << 28,
HKS_TAG_TYPE_BOOL: 4 << 28,
HKS_TAG_TYPE_BYTES: 5 << 28,
}
let HksKeyStorageType = {
HKS_STORAGE_TEMP: 0,
HKS_STORAGE_PERSISTENT: 1,
}
let HksTag = {
HKS_TAG_ALGORITHM: HksTagType.HKS_TAG_TYPE_UINT | 1,
HKS_TAG_PURPOSE: HksTagType.HKS_TAG_TYPE_UINT | 2,
HKS_TAG_KEY_SIZE: HksTagType.HKS_TAG_TYPE_UINT | 3,
HKS_TAG_KEY_STORAGE_FLAG: HksTagType.HKS_TAG_TYPE_UINT | 1002,
HKS_TAG_DIGEST: HksTagType.HKS_TAG_TYPE_UINT | 4,
HKS_TAG_PADDING: HksTagType.HKS_TAG_TYPE_UINT | 5,
HKS_TAG_BLOCK_MODE: HksTagType.HKS_TAG_TYPE_UINT | 6,
HKS_TAG_IS_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BOOL | 1001,
HKS_TAG_KEY_ALIAS: HksTagType.HKS_TAG_TYPE_BYTES | 23,
}
let HuksAgree001 = {
HuksKeyAlgECDH: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECDH,
},
HuksKeyAlgECC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_ECC,
},
HuksKeyECCSize224: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_224,
},
HuksKeyECCSize256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeyECCSize384: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_384,
},
HuksKeyECCSize521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeyECCDIGEST: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyECCPADDING: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyECCBLOCKMODE: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeySTORAGE: {
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_PERSISTENT,
},
HuksKeyISKEYALIAS: { tag: HksTag.HKS_TAG_IS_KEY_ALIAS, value: true },
HuksKeyDIGESTNONE: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_NONE,
},
HuksKeyDIGESTSHA1: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA1,
},
HuksKeyDIGESTSHA224: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA224,
},
HuksKeyDIGESTSHA256: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA256,
},
HuksKeyDIGESTSHA384: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA384,
},
HuksKeyDIGESTSHA512: {
tag: HksTag.HKS_TAG_DIGEST,
value: HksKeyDigest.HKS_DIGEST_SHA512,
},
HuksKeyPADDINGNONE: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_NONE,
},
HuksKeyPADDINGPKCS7: {
tag: HksTag.HKS_TAG_PADDING,
value: HksKeyPadding.HKS_PADDING_PKCS7,
},
HuksKeyBLOCKMODECBC: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CBC,
},
HuksKeyBLOCKMODECCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CCM,
},
HuksKeyBLOCKMODEECB: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_ECB,
},
HuksKeyBLOCKMODECTR: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_CTR,
},
HuksKeyBLOCKMODEGCM: {
tag: HksTag.HKS_TAG_BLOCK_MODE,
value: HksCipherMode.HKS_MODE_GCM,
},
HuksKeyALGORITHMAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyALGORITHMHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeySIZE521: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_521,
},
HuksKeySIZE256: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_256,
},
HuksKeySIZE128: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_128,
},
HuksKeySIZE192: {
tag: HksTag.HKS_TAG_KEY_SIZE,
value: HksKeySize.HKS_ECC_KEY_SIZE_192,
},
HuksKeyAlgAES: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_AES,
},
HuksKeyAlgHMAC: {
tag: HksTag.HKS_TAG_ALGORITHM,
value: HksKeyAlg.HKS_ALG_HMAC,
},
HuksKeyPurposeECDH: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_AGREE,
},
HuksKeyPurposeDERIVE: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_DERIVE,
},
HuksKeyPurposeMAC: {
tag: HksTag.HKS_TAG_PURPOSE,
value: HksKeyPurpose.HKS_KEY_PURPOSE_MAC,
},
HuksKeyPurposeENCRYPTDECRYPT: {
tag: HksTag.HKS_TAG_PURPOSE,
value:
HksKeyPurpose.HKS_KEY_PURPOSE_ENCRYPT |
HksKeyPurpose.HKS_KEY_PURPOSE_DECRYPT,
},
}
let HuksOptions65kb = {
properties: new Array(
HuksAgree001.HuksKeyAlgECC,
HuksAgree001.HuksKeyPurposeECDH,
HuksAgree001.HuksKeyECCSize224,
HuksAgree001.HuksKeyECCDIGEST,
HuksAgree001.HuksKeyECCPADDING,
HuksAgree001.HuksKeyECCBLOCKMODE
),
inData: srcData65Kb,
}
function stringToUint8Array(str) {
var arr = []
for (var i = 0, j = str.length; i < j; ++i) {
arr.push(str.charCodeAt(i))
}
var tmpUint8Array = new Uint8Array(arr)
return tmpUint8Array
}
function uint8ArrayToString(fileData) {
var dataString = ''
for (var i = 0; i < fileData.length; i++) {
dataString += String.fromCharCode(fileData[i])
}
return dataString
}
async function publicAgreeGenFunc(srcKeyAlies, HuksOptions) {
await generateKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function generateKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.generateKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test generateKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeExport1Func(srcKeyAlies, HuksOptions, exportKeys) {
await exportKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (exportKeys == 1) {
exportKeyFrist = data.outData
} else {
exportKeySecond = data.outData
}
})
.catch((err) => {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function exportKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.exportKey(srcKeyAlies, HuksOptions, function (err, data) {
console.log(`test exportKey data: ${JSON.stringify(data)}`)
if (err.code !== 0) {
console.log(
'test exportKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeInitFunc(srcKeyAlies, HuksOptions) {
await init(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test init data ${JSON.stringify(data)}`)
handle1 = data.handle1
handle2 = data.handle2
handle = {
handle1: handle1,
handle2: handle2,
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test init err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
}
function init(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.init(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log('test init err information: ' + JSON.stringify(err))
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeUpdateFunc(HuksOptions, exportKey) {
let _inData = HuksOptions.inData
if (exportKey == 1) {
HuksOptions.inData = exportKeySecond
} else {
HuksOptions.inData = exportKeyFrist
}
await update(handle, HuksOptions)
.then((data) => {
console.log(`test update data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log('test update err information: ' + JSON.stringify(err))
expect(null).assertFail()
})
HuksOptions.inData = _inData
}
function update(handle, HuksOptions) {
return new Promise((resolve, reject) => {
huks.update(handle, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test update err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
finishData
) {
if (thirdInderfaceName == 'finish') {
console.log(
`test befor finish huksOptionsFinish ${JSON.stringify(
huksOptionsFinish
)}`
)
await finish(handle, huksOptionsFinish)
.then((data) => {
console.log(`test finish data ${JSON.stringify(data)}`)
if (finishData == 1) {
finishDataFrist = data.outData
} else {
finishDataSecond = data.outData
}
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test finish err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
} else {
let huksOptionsAbort = new Array({
tag: HksTag.HKS_TAG_KEY_STORAGE_FLAG,
value: HksKeyStorageType.HKS_STORAGE_TEMP,
})
await abort(handle, huksOptionsAbort)
.then((data) => {
console.log(`test abort data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
}
function abort(handle, huksOptionsAbort) {
return new Promise((resolve, reject) => {
huks.abort(handle, huksOptionsAbort, function (err, data) {
if (err.code !== 0) {
console.log(
'test abort err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
function finish(handle, huksOptionsFinish) {
return new Promise((resolve, reject) => {
huks.finish(handle, huksOptionsFinish, function (err, data) {
if (err.code !== 0) {
console.log(
'test generateKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeDeleteFunc(srcKeyAlies, HuksOptions) {
await deleteKey(srcKeyAlies, HuksOptions)
.then((data) => {
console.log(`test deleteKey data ${JSON.stringify(data)}`)
expect(data.errorCode == 0).assertTrue()
})
.catch((err) => {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
expect(null).assertFail()
})
}
function deleteKey(srcKeyAlies, HuksOptions) {
return new Promise((resolve, reject) => {
huks.deleteKey(srcKeyAlies, HuksOptions, function (err, data) {
if (err.code !== 0) {
console.log(
'test deleteKey err information: ' + JSON.stringify(err)
)
reject(err)
} else {
resolve(data)
}
})
})
}
async function publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions,
huksOptionsFinish,
thirdInderfaceName
) {
try {
await publicAgreeGenFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeGenFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeExport1Func(srcKeyAliesFirst, HuksOptions, 1)
await publicAgreeExport1Func(srcKeyAliesSecond, HuksOptions, 2)
HuksOptions.properties.splice(0, 1, HuksAgree001.HuksKeyAlgECDH)
HuksOptions.properties.splice(3, 1)
HuksOptions.properties.splice(4, 1)
HuksOptions.properties.splice(5, 1)
await publicAgreeInitFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 1)
await publicAgreeFinishAbortFunc(
huksOptionsFinish,
thirdInderfaceName,
1
)
let _huksOptionsFinish = huksOptionsFinish
let huksOptionsFinishSecond = _huksOptionsFinish
huksOptionsFinishSecond.properties.splice(6, 1, {
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesSecond + 'final'),
})
await publicAgreeInitFunc(srcKeyAliesSecond, HuksOptions)
await publicAgreeUpdateFunc(HuksOptions, 2)
await publicAgreeFinishAbortFunc(
huksOptionsFinishSecond,
thirdInderfaceName,
2
)
await publicAgreeDeleteFunc(srcKeyAliesFirst, HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond + 'final', HuksOptions)
await publicAgreeDeleteFunc(srcKeyAliesSecond, HuksOptions)
} catch (e) {
expect(null).assertFail()
}
}
describe('SecurityHuksAgreeECDHCallbackJsunit', function () {
it('testAgreeECDHFinish65KB001', 0, async function (done) {
const srcKeyAliesFirst =
'testAgreeECDHSize224Finish65KBAgreeKeyAlias_01_001'
const srcKeyAliesSecond =
'testAgreeECDHSize224Finish65KBAgreeKeyAlias_02_001'
let huksOptionsFinish = {
properties: new Array(
HuksAgree001.HuksKeySTORAGE,
HuksAgree001.HuksKeyISKEYALIAS,
HuksAgree001.HuksKeyALGORITHMAES,
HuksAgree001.HuksKeySIZE256,
HuksAgree001.HuksKeyPurposeENCRYPTDECRYPT,
HuksAgree001.HuksKeyDIGESTNONE,
{
tag: HksTag.HKS_TAG_KEY_ALIAS,
value: stringToUint8Array(srcKeyAliesFirst),
},
HuksAgree001.HuksKeyPADDINGNONE,
HuksAgree001.HuksKeyBLOCKMODEECB
),
inData: srcData65Kb,
}
await publicAgreeFunc(
srcKeyAliesFirst,
srcKeyAliesSecond,
HuksOptions65kb,
huksOptionsFinish,
'finish'
)
done()
})
})
{
"description": "Configuration for huksAgreeBasicPromise js api Tests",
"driver": {
"type": "JSUnitTest",
"test-timeout": "180000",
"package": "com.example.huksagreebasicpromise",
"shell-timeout": "60000"
},
"kits": [
{
"test-file-name": [
"ActsHuksAgreeBasicPromiseJSApiTest.hap"
],
"type": "AppInstallKit",
"cleanup-apps": true
}
]
}
{
"description": "Configuration for huksCipherBasicCallback js api Tests",
"driver": {
"type": "JSUnitTest",
"test-timeout": "180000",
"package": "com.example.hukscipherbasiccallback",
"shell-timeout": "60000"
},
"kits": [
{
"test-file-name": [
"ActsHuksCipherBasicCallbackJSApiTest.hap"
],
"type": "AppInstallKit",
"cleanup-apps": true
}
]
}
{
"description": "Configuration for huksCipherBasicPromise js api Tests",
"driver": {
"type": "JSUnitTest",
"test-timeout": "180000",
"package": "com.example.hukscipherbasicpromise",
"shell-timeout": "60000"
},
"kits": [
{
"test-file-name": [
"ActsHuksCipherBasicPromiseJSApiTest.hap"
],
"type": "AppInstallKit",
"cleanup-apps": true
}
]
}
{
"description": "Configuration for huksDeriveBasicCallback js api Tests",
"driver": {
"type": "JSUnitTest",
"test-timeout": "180000",
"package": "com.example.huksderivebasiccallback",
"shell-timeout": "60000"
},
"kits": [
{
"test-file-name": [
"ActsHuksDeriveBasicCallbackJSApiTest.hap"
],
"type": "AppInstallKit",
"cleanup-apps": true
}
]
}
.container {
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
.title {
font-size: 40px;
color: #000000;
opacity: 0.9;
}
@media screen and (device-type: tablet) and (orientation: landscape) {
.title {
font-size: 100px;
}
}
@media screen and (device-type: wearable) {
.title {
font-size: 28px;
color: #FFFFFF;
}
}
@media screen and (device-type: tv) {
.container {
background-image: url("/common/images/Wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.title {
font-size: 100px;
color: #FFFFFF;
}
}
@media screen and (device-type: phone) and (orientation: landscape) {
.title {
font-size: 60px;
}
}
/*
* 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 { Core, ExpectExtend } from 'deccjsunit/index'
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
},
onShow() {
const core = Core.getInstance();
const expectExtend = new ExpectExtend({
'id': 'extend'
});
core.addService('expect', expectExtend);
core.init();
const configService = core.getDefaultService('config');
this.timeout = 1500000;
configService.setConfig(this)
console.log("test start")
require('../../../test/List.test')
core.execute()
},
onReady() {
},
}
/*
* 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.
*/
let isOpenSSL = false
export {isOpenSSL}
{
"description": "Configuration for huksDeriveBasicPromise js api Tests",
"driver": {
"type": "JSUnitTest",
"test-timeout": "180000",
"package": "com.example.huksderivebasicpromise",
"shell-timeout": "60000"
},
"kits": [
{
"test-file-name": [
"ActsHuksDeriveBasicPromiseJSApiTest.hap"
],
"type": "AppInstallKit",
"cleanup-apps": true
}
]
}
/*
* 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.
*/
export default {
onCreate() {
console.info('AceApplication onCreate');
},
onDestroy() {
console.info('AceApplication onDestroy');
}
};
.container {
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
height: 100%;
}
.title {
font-size: 40px;
color: #000000;
opacity: 0.9;
}
@media screen and (device-type: tablet) and (orientation: landscape) {
.title {
font-size: 100px;
}
}
@media screen and (device-type: wearable) {
.title {
font-size: 28px;
color: #FFFFFF;
}
}
@media screen and (device-type: tv) {
.container {
background-image: url("/common/images/Wallpaper.png");
background-size: cover;
background-repeat: no-repeat;
background-position: center;
}
.title {
font-size: 100px;
color: #FFFFFF;
}
}
@media screen and (device-type: phone) and (orientation: landscape) {
.title {
font-size: 60px;
}
}
/*
* 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 { Core, ExpectExtend } from 'deccjsunit/index'
export default {
data: {
title: ""
},
onInit() {
this.title = this.$t('strings.world');
},
onShow() {
const core = Core.getInstance();
const expectExtend = new ExpectExtend({
'id': 'extend'
});
core.addService('expect', expectExtend);
core.init();
const configService = core.getDefaultService('config');
this.timeout = 1500000;
configService.setConfig(this)
console.log("test start")
require('../../../test/List.test')
core.execute()
},
onReady() {
},
}
/*
* 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.
*/
let isOpenSSL = false
export { isOpenSSL }
{
"description": "Configuration for huksHmacBasicCallback js api Tests",
"driver": {
"type": "JSUnitTest",
"test-timeout": "180000",
"package": "com.example.huksHmacbasiccallback",
"shell-timeout": "60000"
},
"kits": [
{
"test-file-name": [
"ActsHuksHmacBasicCallbackJSApiTest.hap"
],
"type": "AppInstallKit",
"cleanup-apps": true
}
]
}
/*
* 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.
*/
export default {
onCreate() {
console.info('AceApplication onCreate');
},
onDestroy() {
console.info('AceApplication onDestroy');
}
};
/*
* 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.
*/
let isOpenSSL = false
export { isOpenSSL }
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
# limitations under the License. # limitations under the License.
import("//test/xts/tools/build/suite.gni") import("//test/xts/tools/build/suite.gni")
group("security_huks") { group("security_huks_basic") {
testonly = true testonly = true
if (is_standard_system) { if (is_standard_system) {
deps = [ deps = [
......
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册