未验证 提交 dd14b5d7 编写于 作者: O openharmony_ci 提交者: Gitee

!6616 【OpenHarmony 3.2.9.1】【安全子系统】新增非对称加解密用例

Merge pull request !6616 from qiaozzzh/1121_1
......@@ -4,6 +4,7 @@
"type": "OHJSUnitTest",
"test-timeout": "900000",
"shell-timeout": "900000",
"testcase-timeout": "180000",
"bundle-name": "com.openharmony.cryptoFramework1",
"package-name": "com.openharmony.cryptoFramework1"
},
......
......@@ -391,6 +391,17 @@ async function convertKeyEncryptAndDecryptProcess(asyAlgoName) {
});
}
async function createAsyKeyAgreementFail(ECDHAlgoName) {
return new Promise((resolve, reject) => {
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
resolve(globalECDHData);
expect(globalECDHData == "TypeError: Cannot read property algName of undefined").assertTrue();
if (globalECDHData != "TypeError: Cannot read property algName of undefined"){
reject();
}
});
}
async function keyAgreementProcess(ECDHAlgoName) {
var globalPubKey;
var globalPriKey;
......@@ -420,9 +431,328 @@ async function keyAgreementProcess(ECDHAlgoName) {
});
}
async function keyAgreementProcessFail(ECDHAlgoName, ECDHAlgoName1) {
var globalPubKey;
var globalPriKey;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(ECDHAlgoName);
expect(rsaGenerator != null).assertTrue();
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName1);
expect(globalECDHData != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return generateAsySecret(globalECDHData, globalPriKey, globalPubKey);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
expect(result != null).assertTrue();
reject();
})
.catch((err) => {
console.error("[Callback] keyAgreementProcess catch err:" + err);
resolve(err);
});
});
}
async function keyAgreementProcessParameterException(ECDHAlgoName) {
var globalPubKey;
var globalPriKey;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(ECDHAlgoName);
expect(rsaGenerator != null).assertTrue();
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
expect(globalECDHData != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return generateAsySecret(globalECDHData, null, null);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
expect(result != null).assertTrue();
})
.catch((err) => {
expect(err == "Error: [PriKey]: param unwarp error.").assertTrue();
});
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return generateAsySecret(globalECDHData, globalPubKey, null);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
expect(result != null).assertTrue();
})
.catch((err) => {
expect(err == "Error: [PubKey]: param unwarp error.").assertTrue();
});
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return generateAsySecret(globalECDHData, null, globalPriKey);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
expect(result != null).assertTrue();
reject();
})
.catch((err) => {
expect(err == "Error: [PriKey]: param unwarp error.").assertTrue();
resolve(err);
});
});
}
async function createAsyKeyGeneratorFail(asyAlgoName) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
resolve(rsaGenerator);
expect(rsaGenerator == "TypeError: Cannot read property algName of null").assertTrue();
if (rsaGenerator != "TypeError: Cannot read property algName of null") {
reject();
}
});
}
async function encryptAndDecryptNormalProcessSuperdata(asyAlgoName, cipherAlgoName, globalTextLen) {
var globalCipherText;
var globalPubKey;
var globalPriKey;
var globalText;
var i;
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz",n = t.length,s="";
for (i = 0; i < globalTextLen; i++) {
globalText += t.charAt(Math.floor(Math.random() * n));
}
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorEncrypt != null).assertTrue();
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorDecrypt != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalPubKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalCipherText = finalOutput;
console.log(
"cipherOutput: " + uInt8ArrayToShowStr(globalCipherText.data)
);
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalPriKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
if (finalOutput == null) {
console.error("[Callback]decrypt doFinal out is null");
} else {
console.log(
"[Callback]decrypt doFinal out hex: " +
uInt8ArrayToShowStr(finalOutput.data)
);
}
let decryptData = uInt8ArrayToString(finalOutput.data);
expect(decryptData == globalText).assertTrue();
resolve();
})
.catch((err) => {
console.error(
"[Callback] encryptAndDecryptNormalProcess catch err:" + err
);
reject(err);
});
});
}
async function signAndVerifyNormalProcessSuperdata(asyAlgoName, signVerifyAlgoName, globalTextLen) {
var globalSignBlob;
var globalPubKey;
var globalPriKey;
var globalText;
var i;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz",n = t.length,s="";
for (i = 0; i < globalTextLen; i++) {
globalText += t.charAt(Math.floor(Math.random() * n));
}
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName);
expect(signGenerator != null).assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
expect(verifyGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initSign(signGenerator, globalPriKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateSign(signGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalSignBlob = finalOutput;
console.log("signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data));
return initVerify(verifyGenerator, globalPubKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateVerify(verifyGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((finalStatus) => {
expect(finalStatus).assertTrue();
resolve();
})
.catch((err) => {
console.error("[Callback] signAndVerifyNormalProcess catch err:" + err);
reject(err);
});
});
}
async function encryptAndDecryptNormalProcessNull(asyAlgoName, cipherAlgoName) {
var globalCipherText;
var globalPubKey;
var globalPriKey;
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorEncrypt != null).assertTrue();
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
expect(cipherGeneratorDecrypt != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalPubKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, null);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalCipherText = finalOutput;
console.log(
"cipherOutput: " + uInt8ArrayToShowStr(globalCipherText.data)
);
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalPriKey,
null
);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
if (finalOutput == null) {
console.error("[Callback]decrypt doFinal out is null");
} else {
console.log(
"[Callback]decrypt doFinal out hex: " +
uInt8ArrayToShowStr(finalOutput.data)
);
}
let decryptData = uInt8ArrayToString(finalOutput.data);
expect(decryptData == globalText).assertTrue();
reject();
})
.catch((err) => {
console.error(
"[Callback] encryptAndDecryptNormalProcess catch err:" + err
);
resolve(err);
});
});
}
export {
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
convertKeyEncryptAndDecryptProcess,
keyAgreementProcess,
createAsyKeyAgreementFail,
keyAgreementProcessFail,
keyAgreementProcessParameterException,
createAsyKeyGeneratorFail,
encryptAndDecryptNormalProcessSuperdata,
signAndVerifyNormalProcessSuperdata,
encryptAndDecryptNormalProcessNull,
};
......@@ -351,6 +351,74 @@ async function signAndVerifyNormalProcess(asyAlgoName, signVerifyAlgoName) {
});
}
async function signAndVerifyNormalProcessDataException(asyAlgoName, signVerifyAlgoName, dataType) {
var globalPubKey;
var globalPriKey;
var globalSignBlob;
var globalText = "This is a sign test";
var input;
if (dataType == "null") {
input = null;
} else {
input = { data: stringTouInt8Array(globalText) };
};
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName);
expect(signGenerator != null).assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
expect(verifyGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
expect(rsaKeyPair != null).assertTrue();
globalPubKey = rsaKeyPair.pubKey;
globalPriKey = rsaKeyPair.priKey;
return initSign(signGenerator, globalPriKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateSign(signGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
expect(finalOutput != null).assertTrue();
globalSignBlob = finalOutput;
console.log("signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data));
return initVerify(verifyGenerator, globalPubKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateVerify(verifyGenerator, input);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((finalStatus) => {
expect(finalStatus).assertTrue();
if (dataType == "null") {
reject();
} else {
resolve();
}
})
.catch((err) => {
console.error("[promise] signAndVerifyNormalProcess catch err:" + err);
if (dataType =="null") {
resolve(err);
} else {
reject(err);
}
});
});
}
async function convertKeyEncryptAndDecryptProcess(asyAlgoName) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
......@@ -445,10 +513,84 @@ async function AsyPriKeyClearProcess(asyAlgoName) {
});
}
async function createAsyKeyGeneratorFail(asyAlgoName) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
resolve(rsaGenerator);
expect(rsaGenerator == "TypeError: Cannot read property algName of null").assertTrue();
if (rsaGenerator != "TypeError: Cannot read property algName of null") {
reject();
}
});
}
async function createAsySignFail(
asyAlgoName,
signVerifyAlgoName,
signVerifyAlgoName1,
signVerifyAlgoName2,
signVerifyAlgoName3
) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var rsaKeyPair = generateAsyKeyPair(rsaGenerator);
expect(rsaKeyPair != null).assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName);
expect(signGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName1);
expect(signGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName2);
expect(signGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
var signGenerator = createAsySign(signVerifyAlgoName3);
resolve(signGenerator);
expect(signGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
if (signGenerator != "TypeError: Cannot read property algName of undefined") {
reject();
}
});
}
async function createAsyVerifyFail(
asyAlgoName,
signVerifyAlgoName,
signVerifyAlgoName1,
signVerifyAlgoName2,
signVerifyAlgoName3
) {
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
var rsaKeyPair = generateAsyKeyPair(rsaGenerator);
expect(rsaKeyPair != null).assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
expect(verifyGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName1);
expect(verifyGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName2);
expect(verifyGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
var verifyGenerator = createAsyVerify(signVerifyAlgoName3);
resolve(verifyGenerator);
expect(verifyGenerator == "TypeError: Cannot read property algName of undefined").assertTrue();
if (verifyGenerator != "TypeError: Cannot read property algName of undefined") {
reject();
}
});
}
export {
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
convertKeyEncryptAndDecryptProcess,
keyAgreementProcess,
AsyPriKeyClearProcess,
signAndVerifyNormalProcessDataException,
createAsyKeyGeneratorFail,
createAsySignFail,
createAsyVerifyFail,
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册