From bfdca9a4856e31d06c71582ad42ca0b9a458204e Mon Sep 17 00:00:00 2001 From: qiaozzzh Date: Wed, 28 Sep 2022 12:18:13 +0800 Subject: [PATCH] =?UTF-8?q?ActsCryptoFrameworkJSNormalTest=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E5=A5=97=E8=A1=A5=E5=85=85=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: qiaozzzh Change-Id: Ifc38aac03ad9c6096798cd7cc655dc9aa628979d --- .../src/main/js/MainAbility/app.js | 4 +- .../src/main/js/test/List.test.js | 2 + .../js/test/SymmetricCryptography.test.js | 60 ++++ .../symmetric/publicSymmetricCallback.js | 318 ++++++++++++++++++ .../utils/symmetric/publicSymmetricPromise.js | 312 +++++++++++++++++ 5 files changed, 694 insertions(+), 2 deletions(-) create mode 100644 security/cryptoFramework/src/main/js/test/SymmetricCryptography.test.js create mode 100644 security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricCallback.js create mode 100644 security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricPromise.js diff --git a/security/cryptoFramework/src/main/js/MainAbility/app.js b/security/cryptoFramework/src/main/js/MainAbility/app.js index 830070d19..0b43af8d7 100644 --- a/security/cryptoFramework/src/main/js/MainAbility/app.js +++ b/security/cryptoFramework/src/main/js/MainAbility/app.js @@ -15,9 +15,9 @@ export default { onCreate() { - console.info('AceApplication onCreate'); + console.info('SecurityApplication onCreate'); }, onDestroy() { - console.info('AceApplication onDestroy'); + console.info('SecurityApplication onDestroy'); } }; diff --git a/security/cryptoFramework/src/main/js/test/List.test.js b/security/cryptoFramework/src/main/js/test/List.test.js index e2f65333e..a90fbe39b 100644 --- a/security/cryptoFramework/src/main/js/test/List.test.js +++ b/security/cryptoFramework/src/main/js/test/List.test.js @@ -16,10 +16,12 @@ import SecurityRandomJsunit from "./SecurityRandom.test.js"; import DigestAlgorithmJsunit from "./DigestAlgorithm.test.js"; import CertificateJsunit from "./Certificate.test.js"; +import SymmetricCryptographyJsunit from "./SymmetricCryptography.test.js"; export default function testsuite() { CertificateJsunit(); DigestAlgorithmJsunit(); + SymmetricCryptographyJsunit(); SecurityRandomJsunit(); } \ No newline at end of file diff --git a/security/cryptoFramework/src/main/js/test/SymmetricCryptography.test.js b/security/cryptoFramework/src/main/js/test/SymmetricCryptography.test.js new file mode 100644 index 000000000..cf3d4481f --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/SymmetricCryptography.test.js @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2022 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, beforeAll, afterEach, it, expect } from "@ohos/hypium"; +import { + testEncryptionDecryptionPromise, + testEncryptionDecryptionConvertKeyPromise, + testClearMemPromise +} from "./utils/symmetric/publicSymmetricPromise"; +import { testEncryptionDecryptionCallback, testClearMemCallback } from "./utils/symmetric/publicSymmetricCallback"; + +export default function SymmetricCryptographyJsunit() { + describe("SymmetricCryptographyJsunit", function () { + console.info("##########start SymmetricCryptographyJsunit##########"); + beforeAll(function () { + }); + afterEach(function () { + }); + + /** + * @tc.number Security_crypto_framework_Symmetric_Encryption_AES_3700 + * @tc.name Test clearMem interface + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_Symmetric_Encryption_AES_3700", + 0, + async function (done) { + await testClearMemPromise("AES256"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_Symmetric_Encryption_AES_0100 + * @tc.name Test AES128|GCM|PKCS7 normal encryption and decryption + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_Symmetric_Encryption_AES_0100", + 0, + async function (done) { + testEncryptionDecryptionPromise("AES128", "AES128|GCM|PKCS7", "genGcmParamsSpec"); + done(); + } + ); + }); +} diff --git a/security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricCallback.js b/security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricCallback.js new file mode 100644 index 000000000..82ab231f6 --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricCallback.js @@ -0,0 +1,318 @@ +/* + * Copyright (C) 2022 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 { expect } from "@ohos/hypium"; +import cryptoFramework from "@ohos.security.cryptoFramework"; +import { + stringTouInt8Array, + uInt8ArrayToShowStr, + uInt8ArrayToString, + genGcmParamsSpec, + genIvParamsSpec, + genCcmParamsSpec +} from "../common/publicDoString"; + + +function testEncryptionDecryptionCallback(symAlgoName, cipherAlgoName, paramType) { + var globalCipher; + var globalParams; + var globalKey; + var globalCipherText; + var globalText = "aaa this is test! this is test! bbb"; + + // createSymKeyGenerator + var symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgoName); + expect(symKeyGenerator != null).assertTrue(); + console.info("[callback]createGenerator ok"); + console.warn("[callback]symKeyGenerator algName: " + symKeyGenerator.algName); + + //createCipher + globalCipher = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipher != null).assertTrue(); + console.info("[callback]createCipher ok"); + console.warn("[callback]cipher algName:" + globalCipher.algName); + + if (paramType == "genIvParamsSpec") { + if (symAlgoName.includes("AES")) { + globalParams = genIvParamsSpec(16); + } else if (symAlgoName.includes("3DES")) { + globalParams = genIvParamsSpec(8); + } else { + console.error("[callback]Gen params error"); + throw "[callback]Gen params error"; + } + } else if (paramType == "genGcmParamsSpec") { + globalParams = genGcmParamsSpec(); + } else if (paramType == "genCcmParamsSpec") { + globalParams = genCcmParamsSpec(); + } else if (paramType == "null") { + globalParams = null; + } else { + console.error("[callback]globalParams not match"); + expect(null).assertFail(); + } + + symKeyGenerator.generateSymKey((err, key) => { + if (err) { + console.error("[callback]generateSymKey err, error code: " + err.code); + expect(null).assertFail(); + } + expect(key != null).assertTrue(); + console.info("[callback]generaeSymkey ok"); + console.warn("[callback]key algName:" + key.algName); + console.warn("[callback]key format:" + key.format); + var encodeKey = key.getEncoded(); + console.warn("[callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)); + globalKey = key; + //encrypt init + console.info("[callback]start init ==================[encrypt]"); + var mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; + console.warn("[callback]mode:" + mode); + globalCipher.init(mode, key, globalParams, (err,) => { + if (err) { + console.error("[callback]init error! error code: " + err.code); + expect(null).assertFail(); + } + console.info("[callback]init ok"); + console.info("[callback]start update ==================[encrypt]"); + var plaintext = { data: stringTouInt8Array(globalText) }; + console.warn("[callback]plaintext hex: " + uInt8ArrayToShowStr(plaintext.data)); + globalCipher.update(plaintext, (err, updateOutput) => { + if (err) { + console.error("[callback]update error! error code: " + err.code); + expect(null).assertFail(); + } + console.info("[callback]update ok"); + console.warn("[callback]encrypt update out hex:" + uInt8ArrayToShowStr(updateOutput.data)); + globalCipherText = updateOutput; + expect(globalCipherText != null).assertTrue(); + //encrypt doFinall + console.info("[callback]start doFinall ==================[encrypt]"); + globalCipher.doFinal(null, (err, finalOutput) => { + if (err.code != 0) { + console.error("[callback]doFinall err: ", err.code); + expect(null).assertFail(); + return; + } + console.info("[callback]doFinal ok"); + if (finalOutput == null) { + } else { + if (paramType == "genGcmParamsSpec" || paramType == "genCcmParamsSpec") { + console.warn("encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalParams.authTag = finalOutput; + } else { + console.warn("encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalCipherText = Array.from(globalCipherText.data); + finalOutput = Array.from(finalOutput.data); + globalCipherText = globalCipherText.concat(finalOutput); + globalCipherText = new Uint8Array(globalCipherText); + globalCipherText = { data: globalCipherText }; + } + } + //decrypt init + console.info("[callback]start init ==================[decrypt]"); + var mode = cryptoFramework.CryptoMode.DECRYPT_MODE; + console.warn("[callback]mode: " + mode); + globalCipher.init(mode, globalKey, globalParams, (err,) => { + console.info("[callback]init ok"); + //decrypt update + console.info("[callback]start update ==================[decrypt]"); + globalCipher.update(globalCipherText, (err, updateOutput) => { + console.info("[callback]update ok"); + console.warn("[callback]decrypt update out: " + uInt8ArrayToString(updateOutput.data)); + expect(uInt8ArrayToString(updateOutput.data) == globalText).assertTrue(); + //decrypt doFinal + console.info("[callback]start doFinal ==================[decrypt]"); + globalCipher.doFinal(null, (err, finalOutput) => { + if (err) { + console.error("[callback]decrypt doFinal failed! error code is: " + err.code); + expect(null).assertFail(); + } + console.info("[callback]doFinal ok"); + if (finalOutput == null) { + console.error("[callback]decrypt doFinal out is null"); + } else { + console.log("[callback]decrypt doFinal out hex: " + uInt8ArrayToString(finalOutput.data)); + } + }) + }) + }) + }) + }) + }) + }) +} + +function testEncryptionDecryptionConvertKeyCallback(symAlgoName, cipherAlgoName, paramType) { + var globalCipher; + var globalParams; + var globalKey; + var globalCipherText; + var globalText = "aaa this is test! this is test! bbb"; + + // createSymKeyGenerator + var symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgoName); + expect(symKeyGenerator != null).assertTrue(); + console.info("[callback]createGenerator ok"); + console.warn("[callback]symKeyGenerator algName: " + symKeyGenerator.algName); + + //createCipher + globalCipher = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipher != null).assertTrue(); + console.info("[callback]createCipher ok"); + console.warn("[callback]cipher algName:" + globalCipher.algName); + + if (paramType == "genIvParamsSpec") { + if (symAlgoName.includes("AES")) { + globalParams = genIvParamsSpec(16); + } else if (symAlgoName.includes("3DES")) { + globalParams = genIvParamsSpec(8); + } else { + console.error("[callback]Gen params error"); + throw "[callback]Gen params error"; + } + } else if (paramType == "genGcmParamsSpec") { + globalParams = genGcmParamsSpec(); + } else if (paramType == "genCcmParamsSpec") { + globalParams = genCcmParamsSpec(); + } else if (paramType == "null") { + globalParams = null; + } else { + console.error("[callback]globalParams not match"); + expect(null).assertFail(); + } + + symKeyGenerator.generateSymKey((err, key) => { + if (err) { + console.error("[callback]generateSymKey err, error code: " + err.code); + expect(null).assertFail(); + } + expect(key != null).assertTrue(); + console.info("[callback]generaeSymkey ok"); + console.warn("[callback]key algName:" + key.algName); + console.warn("[callback]key format:" + key.format); + var encodeKey = key.getEncoded(); + console.warn("[callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)); + symKeyGenerator.convertKey(encodeKey, (err0, convertKey)=>{ + if (err0) { + console.error("[callback]generateSymKey err, error code: " + err0.code); + expect(null).assertFail(); + } else { + globalKey = convertKey; + } + }); + //encrypt init + console.info("[callback]start init ==================[encrypt]"); + var mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; + console.warn("[callback]mode:" + mode); + globalCipher.init(mode, key, globalParams, (err,) => { + if (err) { + console.error("[callback]init error! error code: " + err.code); + expect(null).assertFail(); + } + console.info("[callback]init ok"); + console.info("[callback]start update ==================[encrypt]"); + var plaintext = { data: stringTouInt8Array(globalText) }; + console.warn("[callback]plaintext hex: " + uInt8ArrayToShowStr(plaintext.data)); + globalCipher.update(plaintext, (err, updateOutput) => { + if (err) { + console.error("[callback]update error! error code: " + err.code); + expect(null).assertFail(); + } + console.info("[callback]update ok"); + console.warn("[callback]encrypt update out hex:" + uInt8ArrayToShowStr(updateOutput.data)); + globalCipherText = updateOutput; + expect(globalCipherText != null).assertTrue(); + //encrypt doFinall + console.info("[callback]start doFinall ==================[encrypt]"); + globalCipher.doFinal(null, (err, finalOutput) => { + if (err.code != 0) { + console.error("[callback]doFinall err: ", err.code); + expect(null).assertFail(); + return; + } + console.info("[callback]doFinal ok"); + if (finalOutput == null) { + } else { + if (paramType == "genGcmParamsSpec" || paramType == "genCcmParamsSpec") { + console.warn("encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalParams.authTag = finalOutput; + } else { + console.warn("encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalCipherText = Array.from(globalCipherText.data); + finalOutput = Array.from(finalOutput.data); + globalCipherText = globalCipherText.concat(finalOutput); + globalCipherText = new Uint8Array(globalCipherText); + globalCipherText = { data: globalCipherText }; + } + } + //decrypt init + console.info("[callback]start init ==================[decrypt]"); + var mode = cryptoFramework.CryptoMode.DECRYPT_MODE; + console.warn("[callback]mode: " + mode); + globalCipher.init(mode, globalKey, globalParams, (err,) => { + console.info("[callback]init ok"); + //decrypt update + console.info("[callback]start update ==================[decrypt]"); + globalCipher.update(globalCipherText, (err, updateOutput) => { + console.info("[callback]update ok"); + console.warn("[callback]decrypt update out: " + uInt8ArrayToString(updateOutput.data)); + expect(uInt8ArrayToString(updateOutput.data) == globalText).assertTrue(); + //decrypt doFinal + console.info("[callback]start doFinal ==================[decrypt]"); + globalCipher.doFinal(null, (err, finalOutput) => { + if (err) { + console.error("[callback]decrypt doFinal failed! error code is: " + err.code); + expect(null).assertFail(); + } + console.info("[callback]doFinal ok"); + if (finalOutput == null) { + console.error("[callback]decrypt doFinal out is null"); + } else { + console.log("[callback]decrypt doFinal out hex: " + uInt8ArrayToString(finalOutput.data)); + } + }) + }) + }) + }) + }) + }) + }) +} + +function testClearMemCallback(symAlgoName) { + var globalSymKeyGenerator; + // createSymKeyGenerator + globalSymKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgoName); + expect(globalSymKeyGenerator != null).assertTrue(); + console.info("[callback]createGenerator ok"); + console.warn("[callback]symKeyGenerator algName: " + globalSymKeyGenerator.algName); + + globalSymKeyGenerator.generateSymKey((err, symKey) => { + if (err) { + console.error("catch err is: " + err); + expect(err == null).assertFail(); + } + console.info("[callback]generaeSymkey ok"); + console.warn("[callback]key algName:" + symKey.algName); + console.warn("[callback]key format:" + symKey.format); + var encodeKey = symKey.getEncoded(); + console.warn("[callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)); + let ret = symKey.clearMem(); + console.log("ret = ", ret); + expect(ret == null).assertTrue() + }) +} + +export { testEncryptionDecryptionCallback, testEncryptionDecryptionConvertKeyCallback, testClearMemCallback }; diff --git a/security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricPromise.js b/security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricPromise.js new file mode 100644 index 000000000..338c459ee --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/utils/symmetric/publicSymmetricPromise.js @@ -0,0 +1,312 @@ +/* + * Copyright (C) 2022 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 { expect } from "@ohos/hypium"; +import cryptoFramework from "@ohos.security.cryptoFramework"; +import { + stringTouInt8Array, + uInt8ArrayToShowStr, + uInt8ArrayToString, + genGcmParamsSpec, + genIvParamsSpec, + genCcmParamsSpec +} from "../common/publicDoString"; + +function testEncryptionDecryptionPromise(symAlgoName, cipherAlgoName, paramType) { + var globalCipher; + var globalParams; + var globalKey; + var globalCipherText; + var globalText = "aaa this is test! this is test! bbb"; + + return new Promise((resolve, reject) => { + setTimeout(() => { + console.error(""); + resolve("") + }, 50); + }).then(() => { + // createSymKeyGenerator + var symKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgoName); + expect(symKeyGenerator != null).assertTrue(); + console.info("[promise]createGenerator ok"); + console.warn("[promise]symKeyGenerator algName: " + symKeyGenerator.algName); + + //createCipher + globalCipher = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipher != null).assertTrue(); + console.info("[promise]createCipher ok"); + console.warn("[promise]cipher algName:" + globalCipher.algName); + + if (paramType == "genIvParamsSpec") { + if (symAlgoName.includes("AES")) { + globalParams = genIvParamsSpec(16); + } else if (symAlgoName.includes("3DES")) { + globalParams = genIvParamsSpec(8); + } else { + console.error("[promise]Gen params error"); + throw "[promise]Gen params error"; + } + } else if (paramType == "genGcmParamsSpec") { + globalParams = genGcmParamsSpec(); + } else if (paramType == "genCcmParamsSpec") { + globalParams = genCcmParamsSpec(); + } else if (paramType == "null") { + globalParams = null; + } else { + console.error("[promise]globalParams not match"); + throw "[promise]globalParams not match"; + } + + var promiseKey = symKeyGenerator.generateSymKey(); + expect(promiseKey != null).assertTrue(); + return promiseKey; + }).then(key => { + console.info("[promise]generaeSymkey ok"); + console.warn("[promise]key algName:" + key.algName); + console.warn("[promise]key format:" + key.format); + var encodeKey = key.getEncoded(); + console.warn("[promise]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)); + globalKey = key; + return key; + }).then(key => { + //encrypt init + console.info("[promise]start init ==================[encrypt]"); + var mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; + console.warn("[promise]mode:" + mode); + var promiseInit = globalCipher.init(mode, key, globalParams); + console.info("[promise]init ok"); + return promiseInit; + }).then(() => { + //encrypt update + console.info("[promise]start update ==================[encrypt]"); + var plaintext = { data: stringTouInt8Array(globalText) }; + console.warn("[promise]plaintext hex: " + uInt8ArrayToShowStr(plaintext.data)); + var promiseUpdate = globalCipher.update(plaintext); + console.info("[promise]update ok"); + return promiseUpdate; + }).then((updateOutput) => { + //encrypt doFinall + console.info("[promise]start doFinall ==================[encrypt]"); + console.warn("[promise]encrypt update out hex:" + uInt8ArrayToShowStr(updateOutput.data)); + globalCipherText = updateOutput; + expect(globalCipherText != null).assertTrue(); + var promiseFinal = globalCipher.doFinal(null); + console.info("[promise]doFinal ok"); + return promiseFinal; + }).then(finalOutput => { + if (finalOutput == null) { + ; + } else { + if (paramType == "genGcmParamsSpec" || paramType == "genCcmParamsSpec") { + console.warn("[promise]encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalParams.authTag = finalOutput; + } else { + console.warn("[promise]encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalCipherText = Array.from(globalCipherText.data); + finalOutput = Array.from(finalOutput.data); + globalCipherText = globalCipherText.concat(finalOutput); + globalCipherText = new Uint8Array(globalCipherText); + globalCipherText = { data: globalCipherText }; + } + } + return; + }).then(() => { + //decrypt init + console.info("[promise]start init ==================[decrypt]"); + var mode = cryptoFramework.CryptoMode.DECRYPT_MODE; + console.warn("[promise]mode: " + mode); + var promiseInit = globalCipher.init(mode, globalKey, globalParams); + console.info("[promise]init ok"); + return promiseInit; + }).then(() => { + //decrypt update + console.info("[promise]start update ==================[decrypt]"); + var promiseUpdate = globalCipher.update(globalCipherText); + console.info("[promise]update ok"); + return promiseUpdate; + }).then(updateOutput => { + //decrypt doFinal + console.info("[promise]start doFinal ==================[decrypt]"); + console.warn("[promise]decrypt update out: " + uInt8ArrayToString(updateOutput.data)); + expect(uInt8ArrayToString(updateOutput.data) == globalText).assertTrue(); + var promiseFinal = globalCipher.doFinal(null); + console.info("[promise]doFinal ok"); + return promiseFinal; + }).then(finalOutput => { + if (finalOutput == null) { + console.error("[promise]decrypt doFinal out is null"); + } else { + console.log("[promise]decrypt doFinal out hex: " + uInt8ArrayToShowStr(finalOutput.data)); + } + }).catch(err => { + console.error("[promise]catch err:" + err); + expect(null).assertFail(); + }) +} + +function testEncryptionDecryptionConvertKeyPromise(symAlgoName, cipherAlgoName, paramType) { + var globalCipher; + var globalParams; + var globalKey; + var globalCipherText; + var globalSymKeyGenerator; + var globalText = "aaa this is test! this is test! bbb"; + + return new Promise((resolve, reject) => { + setTimeout(() => { + console.error(""); + resolve("") + }, 50); + }).then(() => { + // createSymKeyGenerator + globalSymKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgoName); + expect(globalSymKeyGenerator != null).assertTrue(); + console.info("[promise]createGenerator ok"); + console.warn("[promise]symKeyGenerator algName: " + globalSymKeyGenerator.algName); + + //createCipher + globalCipher = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipher != null).assertTrue(); + console.info("[promise]createCipher ok"); + console.warn("[promise]cipher algName:" + globalCipher.algName); + + if (paramType == "genIvParamsSpec") { + if (symAlgoName.includes("AES")) { + globalParams = genIvParamsSpec(16); + } else if (symAlgoName.includes("3DES")) { + globalParams = genIvParamsSpec(8); + } else { + console.error("[promise]Gen params error"); + throw "[promise]Gen params error"; + } + } else if (paramType == "genGcmParamsSpec") { + globalParams = genGcmParamsSpec(); + } else if (paramType == "genCcmParamsSpec") { + globalParams = genCcmParamsSpec(); + } else if (paramType == "null") { + globalParams = null; + } else { + console.error("[promise]globalParams not match"); + throw "[promise]globalParams not match"; + } + + var promiseKey = globalSymKeyGenerator.generateSymKey(); + return promiseKey; + }).then(key => { + var encodeKey = key.getEncoded(); + console.warn("[promise]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)); + var convertKey = globalSymKeyGenerator.convertKey(encodeKey); + return convertKey; + }).then(key => { + //encrypt init + console.warn("[promise]convertkey getEncoded hex: " + uInt8ArrayToShowStr(key.getEncoded().data)); + globalKey = key; + console.info("[promise]start init ==================[encrypt]"); + var mode = cryptoFramework.CryptoMode.ENCRYPT_MODE; + console.warn("[promise]mode:" + mode); + var promiseInit = globalCipher.init(mode, key, globalParams); + console.info("[promise]init ok"); + return promiseInit; + }).then(() => { + //encrypt update + console.info("[promise]start update ==================[encrypt]"); + var plaintext = { data: stringTouInt8Array(globalText) }; + console.warn("[promise]plaintext hex: " + uInt8ArrayToShowStr(plaintext.data)); + var promiseUpdate = globalCipher.update(plaintext); + console.info("[promise]update ok"); + return promiseUpdate; + }).then((updateOutput) => { + //encrypt doFinall + console.info("[promise]start doFinall ==================[encrypt]"); + console.warn("[promise]encrypt update out hex:" + uInt8ArrayToShowStr(updateOutput.data)); + globalCipherText = updateOutput; + expect(globalCipherText != null).assertTrue(); + var promiseFinal = globalCipher.doFinal(null); + console.info("[promise]doFinal ok"); + return promiseFinal; + }).then(finalOutput => { + if (finalOutput == null) { + ; + } else { + if (paramType == "genGcmParamsSpec" || paramType == "genCcmParamsSpec") { + console.warn("[promise]encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalParams.authTag = finalOutput; + } else { + console.warn("[promise]encrypt authTag(finalOutput) hex: " + uInt8ArrayToShowStr(finalOutput.data)); + globalCipherText = Array.from(globalCipherText.data); + finalOutput = Array.from(finalOutput.data); + globalCipherText = globalCipherText.concat(finalOutput); + globalCipherText = new Uint8Array(globalCipherText); + globalCipherText = { data: globalCipherText }; + } + } + return; + }).then(() => { + //decrypt init + console.info("[promise]start init ==================[decrypt]"); + var mode = cryptoFramework.CryptoMode.DECRYPT_MODE; + console.warn("[promise]mode: " + mode); + var promiseInit = globalCipher.init(mode, globalKey, globalParams); + console.info("[promise]init ok"); + return promiseInit; + }).then(() => { + //decrypt update + console.info("[promise]start update ==================[decrypt]"); + var promiseUpdate = globalCipher.update(globalCipherText); + console.info("[promise]update ok"); + return promiseUpdate; + }).then(updateOutput => { + //decrypt doFinal + console.info("[promise]start doFinal ==================[decrypt]"); + console.warn("[promise]decrypt update out: " + uInt8ArrayToString(updateOutput.data)); + expect(uInt8ArrayToString(updateOutput.data) == globalText).assertTrue(); + var promiseFinal = globalCipher.doFinal(null); + console.info("[promise]doFinal ok"); + return promiseFinal; + }).then(finalOutput => { + if (finalOutput == null) { + console.error("[promise]decrypt doFinal out is null"); + } else { + console.log("[promise]decrypt doFinal out hex: " + uInt8ArrayToShowStr(finalOutput.data)); + } + }).catch(err => { + console.error("[promise]catch err:" + err); + expect(null).assertFail(); + }) +} + +function testClearMemPromise(symAlgoName) { + var globalSymKeyGenerator; + // createSymKeyGenerator + globalSymKeyGenerator = cryptoFramework.createSymKeyGenerator(symAlgoName); + expect(globalSymKeyGenerator != null).assertTrue(); + console.info("[promise]createGenerator ok"); + console.warn("[promise]symKeyGenerator algName: " + globalSymKeyGenerator.algName); + + globalSymKeyGenerator.generateSymKey().then(symKey => { + console.info("[promise]generaeSymkey ok"); + console.warn("[promise]key algName:" + symKey.algName); + console.warn("[promise]key format:" + symKey.format); + var encodeKey = symKey.getEncoded(); + console.warn("[promise]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)); + let ret = symKey.clearMem(); + console.log("ret = ", ret); + expect(ret == null).assertTrue() + }).catch(err => { + console.error("catch err is: " + err); + expect(err == null).assertFail(); + }) +} + +export { testEncryptionDecryptionPromise, testEncryptionDecryptionConvertKeyPromise, testClearMemPromise }; -- GitLab