diff --git a/security/cryptoFramework/src/main/js/MainAbility/app.js b/security/cryptoFramework/src/main/js/MainAbility/app.js index 830070d196d86b127cea947d168bfd116f446205..0b43af8d7ed4d2b992ea2e28725574bb007efe49 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/AsymmetricCryptography.test.js b/security/cryptoFramework/src/main/js/test/AsymmetricCryptography.test.js new file mode 100644 index 0000000000000000000000000000000000000000..7512d8bd9a77cc23b368f2080a07f89821c8f779 --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/AsymmetricCryptography.test.js @@ -0,0 +1,146 @@ +/* + * 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 { + testAsyKeyEncryptDecryptPromise, + testAsyKeySignVerifyPromise, + testAsyKeyECDHPromise, + testAsyKeyConvertKeyPromise, + testAsyKeyPriKeyClearPromise, +} from "./utils/asymmetric/publicAsymmetricPromise"; + + +export default function AsymmetricCryptographyJsunit() { + describe("AsymmetricCryptographyJsunit", function () { + console.info("##########start AsymmetricCryptographyJsunit##########"); + beforeAll(function () { + }); + afterEach(function () { + }); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_0100 + * @tc.name Test RSA1024|PRIMES_2 normal encryption and decryption + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_0100", + 0, + async function (done) { + testAsyKeyEncryptDecryptPromise("RSA1024|PRIMES_2", "RSA1024|PKCS1|SHA256"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_0200 + * @tc.name Test RSA2048|PRIMES_2 normal encryption and decryption + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_0200", + 0, + async function (done) { + testAsyKeyEncryptDecryptPromise("RSA2048|PRIMES_2", "RSA2048|PKCS1|MD5"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_RSA_0100 + * @tc.name Test RSA1024|PKCS1|MD5 normal sign and verify + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_RSA_0100", + 0, + async function (done) { + testAsyKeySignVerifyPromise("RSA1024|PRIMES_2", "RSA1024|PKCS1|MD5"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_RSA_0200 + * @tc.name Test RSA2048|PKCS1|SHA1 normal sign and verify + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_RSA_0200", + 0, + async function (done) { + testAsyKeySignVerifyPromise("RSA2048|PRIMES_2", "RSA2048|PKCS1|SHA1"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_ECDH_0100 + * @tc.name Test ECC224 SignVerify ECDH + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_ECDH_0100", + 0, + async function (done) { + testAsyKeyECDHPromise("ECC224"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_SignVerify_ECDH_0200 + * @tc.name Test ECC256 SignVerify ECDH + * @tc.desc Use the Promise Style of Interface + */ + it( + "Security_crypto_framework_ASymmetric_SignVerify_ECDH_0200", + 0, + async function (done) { + testAsyKeyECDHPromise("ECC256"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3300 + * @tc.name Test convertKey + * @tc.desc Use RSA3072|PRIMES_2 algorithm + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_3300", + 0, + async function (done) { + testAsyKeyConvertKeyPromise("RSA3072|PRIMES_2"); + done(); + } + ); + + /** + * @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3100 + * @tc.name Test convertKey + * @tc.desc Use RSA1024|PRIMES_2 algorithm + */ + it( + "Security_crypto_framework_ASymmetric_Encryption_RSA_3100", + 0, + async function (done) { + testAsyKeyPriKeyClearPromise("RSA1024|PRIMES_2"); + done(); + } + ); + }); +} diff --git a/security/cryptoFramework/src/main/js/test/List.test.js b/security/cryptoFramework/src/main/js/test/List.test.js index e2f65333e52a74ca69c3106edcbc354cfb14eacb..089124d8ecd430832ebe12690b25d320a1d56ffc 100644 --- a/security/cryptoFramework/src/main/js/test/List.test.js +++ b/security/cryptoFramework/src/main/js/test/List.test.js @@ -16,10 +16,14 @@ import SecurityRandomJsunit from "./SecurityRandom.test.js"; import DigestAlgorithmJsunit from "./DigestAlgorithm.test.js"; import CertificateJsunit from "./Certificate.test.js"; +import SymmetricCryptographyJsunit from "./SymmetricCryptography.test.js"; +import AsymmetricCryptographyJsunit from "./AsymmetricCryptography.test.js"; export default function testsuite() { CertificateJsunit(); DigestAlgorithmJsunit(); + SymmetricCryptographyJsunit(); + AsymmetricCryptographyJsunit(); 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 0000000000000000000000000000000000000000..cf3d4481f143fdb4ddd9960b7faeda12d4efdb44 --- /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/asymmetric/publicAsymmetricCallback.js b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricCallback.js new file mode 100644 index 0000000000000000000000000000000000000000..c3f7caa946d6cd00f6af751b2354f357099dab22 --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricCallback.js @@ -0,0 +1,235 @@ +/* + * 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 } from "../common/publicDoString"; + +async function testAsyKeyEncryptDecryptCallback(asyAlgoName, cipherAlgoName) { + var globalPubKey; + var globalPriKey; + var globalCipherEncrypt; + var globalCipherDecrypt; + var globalText = "This is a cipher test"; + var globalInput = { data: stringTouInt8Array(globalText) }; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + globalCipherEncrypt = cryptoFramework.createCipher(cipherAlgoName); + globalCipherDecrypt = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipherEncrypt != null).assertTrue(); + expect(globalCipherDecrypt != null).assertTrue(); + + rsaGenerator.generateKeyPair((err, keyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + expect(keyPair != null).assertTrue(); + globalPubKey = keyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = keyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + console.info("[Callback]:start init =====================[encrypt]"); + globalCipherEncrypt.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, globalPubKey, null, (err0,) => { + if (err0) { + expect(err0 == null).assertFail(); + } + console.warn("plaintext hex: " + uInt8ArrayToShowStr(globalInput.data)); + console.info("[Callback]:start doFinal =====================[encrypt]"); + globalCipherEncrypt.doFinal(globalInput, (err1, cipherFinalEncrypt) => { + if (err1) { + expect(err1 == null).assertFail(); + } + expect(cipherFinalEncrypt != null).assertTrue(); + console.log("cipherOutput: " + uInt8ArrayToShowStr(cipherFinalEncrypt.data)); + expect(globalCipherDecrypt != null).assertTrue(); + console.log("[Callback]:start init =====================[decrypt]"); + globalCipherDecrypt.init(cryptoFramework.CryptoMode.DECRYPT_MODE, globalPriKey, null, (err,) => { + console.log("[Callback]:start doFinal =====================[decrypt]"); + globalCipherDecrypt.doFinal(cipherFinalEncrypt, (err2, cipherFinalDecrypt) => { + if (err2) { + expect(err2 == null).assertFail(); + } + expect(cipherFinalDecrypt != null).assertTrue(); + if (cipherFinalDecrypt == null) { + console.warn("decrypt doFinal out is null"); + } else { + console.warn("decrypt doFinal output hex:" + uInt8ArrayToShowStr(cipherFinalDecrypt.data)); + console.warn("decrypt doFinal output :" + uInt8ArrayToString(cipherFinalDecrypt.data)); + } + let decryptData = uInt8ArrayToString(cipherFinalDecrypt.data); + expect(decryptData == globalText).assertTrue(); + }); + }) + }); + }); + }); +} + +async function testAsyKeySignVerifyCallback(asyAlgoName, signAlgoName) { + var globalPubKey; + var globalPriKey; + var globalSigner; + var globalVerify; + var globalText = "This is a sign test"; + var globalInput = { data: stringTouInt8Array(globalText) }; + var globalSignBlob; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + globalSigner = cryptoFramework.createSign(signAlgoName); + globalVerify = cryptoFramework.createVerify(signAlgoName); + expect(globalSigner != null).assertTrue(); + expect(globalVerify != null).assertTrue(); + + rsaGenerator.generateKeyPair((err, keyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + expect(keyPair != null).assertTrue(); + globalPubKey = keyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = keyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + globalSigner.init(globalPriKey, (err0,) => { + if (err0) { + expect(err0 == null).assertFail(); + } + console.warn("plaintext hex: " + uInt8ArrayToShowStr(globalInput.data)); + globalSigner.update(globalInput, (err1,) => { + if (err1) { + expect(err1 == null).assertFail(); + } + }); + globalSigner.sign(globalInput, (err2, signData) => { + if (err2) { + expect(err2 == null).assertFail(); + } + globalSignBlob = signData; + console.log("signOutput: " + uInt8ArrayToShowStr(signData.data)); + globalVerify.init(globalPubKey, null, (err3,) => { + if (err3) { + expect(err3 == null).assertFail(); + } + globalVerify.update(globalInput, (err4,) => { + if (err4) { + expect(err4 == null).assertFail(); + } + }); + globalVerify.verify(globalInput,globalSignBlob,(err5, finalStatus) => { + if (err5) { + expect(err5 == null).assertFail(); + } + expect(finalStatus).assertTrue(); + }); + }); + }); + }); + }); +} + +async function testAsyKeyECDHCallback(ECDHAlgoName) { + var globalPubKey; + var globalPriKey; + var globalECDHData; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(ECDHAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair((err, rsaKeyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + globalECDHData = cryptoFramework.createKeyAgreement(ECDHAlgoName); + expect(globalECDHData != null).assertTrue(); + console.log("ECDH algoname is: ", globalECDHData.algName); + globalECDHData.generateSecret(globalPriKey, globalPubKey, (err1, result) => { + if (err1) { + expect(err1 == null).assertFail(); + } + console.warn("result data is " + uInt8ArrayToShowStr(result.data)); + expect(result != null).assertTrue(); + }); + }); +} + +async function testAsyKeyConvertKeyCallback(asyAlgoName) { + var globalPubKey; + var globalPriKey; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Callback]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair((err, rsaKeyPair)=> { + if (err) { + expect(err == null).assertFail(); + } + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + globalPriKey.format); + rsaGenerator.convertKey(encodedPubKey, encodedPriKey, (err1, convertKeyPair)=>{ + if (err1) { + expect(err1 == null).assertFail(); + } + expect(convertKeyPair != null).assertTrue(); + let encodedConvertPubKey = convertKeyPair.pubKey.getEncoded(); + console.warn("[Callback]: pubKey.getAlgorithm= " + convertKeyPair.pubKey.algName); + console.warn("[Callback]: pubKey.getEncoded= " + encodedConvertPubKey.data); + console.warn("[Callback]: pubKey.getFormat= " + convertKeyPair.pubKey.format); + let encodedConvertPriKey = convertKeyPair.priKey.getEncoded(); + console.warn("[Callback]: priKey.getAlgorithm= " + convertKeyPair.priKey.algName); + console.warn("[Callback]: priKey.getEncoded= " + encodedConvertPriKey.data); + console.warn("[Callback]: priKey.getFormat= " + convertKeyPair.priKey.format); + }); + }); +} + +export { + testAsyKeyEncryptDecryptCallback, + testAsyKeySignVerifyCallback, + testAsyKeyECDHCallback, + testAsyKeyConvertKeyCallback, +}; \ No newline at end of file diff --git a/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricPromise.js b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricPromise.js new file mode 100644 index 0000000000000000000000000000000000000000..af6d019a6815ba93b097384995b8fe92f1152391 --- /dev/null +++ b/security/cryptoFramework/src/main/js/test/utils/asymmetric/publicAsymmetricPromise.js @@ -0,0 +1,243 @@ +/* + * 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 } from "../common/publicDoString"; + +async function testAsyKeyEncryptDecryptPromise(asyAlgoName, cipherAlgoName) { + var globalPubKey; + var globalPriKey; + var globalCipherEncrypt; + var globalCipherDecrypt; + var globalCipherBlob; + var globalText = "This is a cipher test"; + var input = { data: stringTouInt8Array(globalText) }; + + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + globalCipherEncrypt = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipherEncrypt != null).assertTrue(); + let keyGenPromise = rsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(keyGenPromise != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + console.info("[Promise]:start init =====================[encrypt]"); + return globalCipherEncrypt.init(cryptoFramework.CryptoMode.ENCRYPT_MODE, globalPubKey, null); + }).then(() => { + console.warn("plaintext hex: " + uInt8ArrayToShowStr(input.data)); + console.info("[Promise]:start doFinal =====================[encrypt]"); + return globalCipherEncrypt.doFinal(input); + }).then(dataBlob => { + expect(dataBlob != null).assertTrue(); + globalCipherBlob = dataBlob; + console.log("cipherOutput: " + uInt8ArrayToShowStr(dataBlob.data)); + globalCipherDecrypt = cryptoFramework.createCipher(cipherAlgoName); + expect(globalCipherDecrypt != null).assertTrue(); + console.log("[Promise]:start init =====================[decrypt]"); + return globalCipherDecrypt.init(cryptoFramework.CryptoMode.DECRYPT_MODE, globalPriKey, null); + }).then(() => { + console.log("[Promise]:start doFinal =====================[decrypt]"); + let promiseFinal = globalCipherDecrypt.doFinal(globalCipherBlob); + return promiseFinal; + }).then(finalOutput => { + expect(finalOutput != null).assertTrue(); + if (finalOutput == null) { + console.warn("decrypt doFinal out is null"); + } else { + console.warn("decrypt doFinal output hex:" + uInt8ArrayToShowStr(finalOutput.data)); + console.warn("decrypt doFinal output :" + uInt8ArrayToString(finalOutput.data)); + } + let decryptData = uInt8ArrayToString(finalOutput.data); + expect(decryptData == globalText).assertTrue(); + }).catch(err => { + console.error("err: " + err.code); + expect(err == null).assertFail(); + }) +} + +async function testAsyKeySignVerifyPromise(asyAlgoName, signAlgoName) { + var globalPubKey; + var globalPriKey; + var globalSigner; + var globalVerify; + var globalSignBlob; + var globalText1 = "This is a sign test1"; + var input1 = { data: stringTouInt8Array(globalText1) }; + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + globalSigner = cryptoFramework.createSign(signAlgoName); + expect(globalSigner != null).assertTrue(); + console.log("sign algoname is: ", globalSigner.algName); + return globalSigner.init(globalPriKey); + }).then(() => { + console.warn("plaintext1 hex: " + uInt8ArrayToShowStr(input1.data)); + return globalSigner.update(input1); + }).then(() => { + return globalSigner.sign(input1); + }).then(promiseSign => { + console.log("promiseSign success: ", promiseSign); + globalSignBlob = promiseSign; + }).then(() => { + globalVerify = cryptoFramework.createVerify(signAlgoName); + expect(globalVerify != null).assertTrue(); + console.log("verify algname is: ", globalVerify.algName); + return globalVerify.init(globalPubKey); + }).then(() => { + console.warn("plaintext1 hex: " + uInt8ArrayToShowStr(input1.data)); + return globalVerify.update(input1); + }).then(() => { + console.log("111111111"); + return globalVerify.verify(input1, globalSignBlob); + }).then((status) => { + console.log("22222222222"); + expect(status).assertTrue(); + }).catch(err => { + console.error("err: " + err.code); + expect(err == null).assertFail(); + }) +} + +async function testAsyKeyECDHPromise(ECDHAlgoName) { + var globalPubKey; + var globalPriKey; + var globalECDHData; + + try { + let rsaGenerator = cryptoFramework.createAsyKeyGenerator(ECDHAlgoName); + console.warn("[Promise]: algName = " + rsaGenerator.algName); + expect(rsaGenerator != null).assertTrue(); + rsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + globalECDHData = cryptoFramework.createKeyAgreement(ECDHAlgoName); + expect(globalECDHData != null).assertTrue(); + console.warn("ECDH algoname is: " + globalECDHData.algName); + let result = globalECDHData.generateSecret(globalPriKey, globalPubKey); + return result; + }).then(secretResult => { + console.warn("result data is " + uInt8ArrayToShowStr(secretResult.data)); + expect(secretResult != null).assertTrue(); + }) + } catch (err) { + console.error("catch err: " + err.code); + expect(err == null).assertFail(); + } +} + +async function testAsyKeyConvertKeyPromise(asyAlgoName) { + var globalPubKey; + var globalPriKey; + + try { + var globalRsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + globalRsaGenerator.algName); + expect(globalRsaGenerator != null).assertTrue(); + globalRsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + return globalRsaGenerator.convertKey(encodedPubKey, encodedPriKey); + }).then(convertKeyPair => { + expect(convertKeyPair != null).assertTrue(); + let encodedConvertPubKey = convertKeyPair.pubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + convertKeyPair.pubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedConvertPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + convertKeyPair.pubKey.format); + let encodedConvertPriKey = convertKeyPair.priKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + convertKeyPair.priKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedConvertPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + convertKeyPair.priKey.format); + }) + } catch (err) { + console.error("err: " + err.code); + expect(err == null).assertFail(); + } +} + +async function testAsyKeyPriKeyClearPromise(asyAlgoName) { + var globalPubKey; + var globalPriKey; + + var globalRsaGenerator = cryptoFramework.createAsyKeyGenerator(asyAlgoName); + console.warn("[Promise]: algName = " + globalRsaGenerator.algName); + expect(globalRsaGenerator != null).assertTrue(); + globalRsaGenerator.generateKeyPair().then(rsaKeyPair => { + expect(rsaKeyPair != null).assertTrue(); + globalPubKey = rsaKeyPair.pubKey; + let encodedPubKey = globalPubKey.getEncoded(); + console.warn("[Promise]: pubKey.getAlgorithm= " + globalPubKey.algName); + console.warn("[Promise]: pubKey.getEncoded= " + encodedPubKey.data); + console.warn("[Promise]: pubKey.getFormat= " + globalPubKey.format); + globalPriKey = rsaKeyPair.priKey; + let encodedPriKey = globalPriKey.getEncoded(); + console.warn("[Promise]: priKey.getAlgorithm= " + globalPriKey.algName); + console.warn("[Promise]: priKey.getEncoded= " + encodedPriKey.data); + console.warn("[Promise]: priKey.getFormat= " + globalPriKey.format); + let result = globalPriKey.clearMem(); + console.log("result is: " + result); + expect(result == undefined).assertTrue(); + }).catch(err => { + console.error("err: " + err.code); + expect(err == null).assertFail(); + }) +} + +export { + testAsyKeyEncryptDecryptPromise, + testAsyKeySignVerifyPromise, + testAsyKeyECDHPromise, + testAsyKeyConvertKeyPromise, + testAsyKeyPriKeyClearPromise, +}; \ No newline at end of file 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 0000000000000000000000000000000000000000..82ab231f6393536f5b8b03498a84ad5593ed02f2 --- /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 0000000000000000000000000000000000000000..338c459ee41c62846884acdfbf10e65c8b018f50 --- /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 };