提交 58721f07 编写于 作者: Q Qwink2016

修改加解密算法库XTS测试套摘要算法部分的逻辑

Signed-off-by: NQwink2016 <huangkun45@huawei.com>
Change-Id: Ic5b335777ae5704dda6cdf47933475a4c85f33d2
上级 67fed82b
/* /*
* Copyright (C) 2022 Huawei Device Co., Ltd. * Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { expect } from "@ohos/hypium"; import { expect } from "@ohos/hypium";
import cryptoFramework from "@ohos.security.cryptoFramework"; import cryptoFramework from "@ohos.security.cryptoFramework";
import { stringTouInt8Array, uInt8ArrayToShowStr, uInt8ArrayToString, } from "../common/publicDoString"; import {
stringTouInt8Array,
async function generateSymKey(symKeyGenerator) { uInt8ArrayToShowStr,
return new Promise((resolve, reject) => { uInt8ArrayToString,
symKeyGenerator.generateSymKey((err, symKey) => { } from "../common/publicDoString";
if (err) {
console.error("[Callback]generateSymKey failed. error is " + err); async function generateSymKey(symKeyGenerator) {
reject(err); return new Promise((resolve, reject) => {
} else { symKeyGenerator.generateSymKey((err, symKey) => {
console.log("[Callback]generateSymKey success. symKey is " + symKey); if (err) {
console.warn("[Callback]key algName:" + symKey.algName); console.error("[Callback]generateSymKey failed. error is " + err);
console.warn("[Callback]key format:" + symKey.format); reject(err);
var encodeKey = symKey.getEncoded(); } else {
console.warn( console.log("[Callback]generateSymKey success. symKey is " + symKey);
"[Callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data) console.warn("[Callback]key algName:" + symKey.algName);
); console.warn("[Callback]key format:" + symKey.format);
resolve(symKey); var encodeKey = symKey.getEncoded();
} console.warn(
}); "[Callback]key getEncoded hex: " + uInt8ArrayToShowStr(encodeKey.data)
}); );
} resolve(symKey);
}
async function initMac(macGenerator, symKey) { });
return new Promise((resolve, reject) => { });
macGenerator.init(symKey, (err) => { }
if (err) {
console.error("[Callback]macGenerator init failed. error is " + err); async function initMac(macGenerator, symKey) {
reject(err); return new Promise((resolve, reject) => {
} else { macGenerator.init(symKey, (err) => {
console.log("[Callback]macGenerator init success!"); if (err) {
resolve("init success"); console.error("[Callback]macGenerator init failed. error is " + err);
} reject(err);
}); } else {
}); console.log("[Callback]macGenerator init success!");
} resolve("init success");
}
async function updateMac(macGenerator, dataBlob) { });
return new Promise((resolve, reject) => { });
macGenerator.update(dataBlob, (err) => { }
if (err) {
console.error("[Callback]macGenerator update failed. error is " + err); async function updateMac(macGenerator, dataBlob) {
reject(err); return new Promise((resolve, reject) => {
} else { macGenerator.update(dataBlob, (err) => {
console.log("[Callback]macGenerator update success!"); if (err) {
resolve("update success"); console.error("[Callback]macGenerator update failed. error is " + err);
} reject(err);
}); } else {
}); console.log("[Callback]macGenerator update success!");
} resolve("update success");
}
async function doFinalMac(macGenerator) { });
return new Promise((resolve, reject) => { });
macGenerator.doFinal((err, output) => { }
if (err) {
console.error("[Callback]macGenerator doFinal failed. error is " + err); async function doFinalMac(macGenerator) {
reject(err); return new Promise((resolve, reject) => {
} else { macGenerator.doFinal((err, output) => {
console.log( if (err) {
"[Callback]macGenerator doFinal success! output is: " + output console.error("[Callback]macGenerator doFinal failed. error is " + err);
); reject(err);
resolve(output); } else {
} console.log(
}); "[Callback]macGenerator doFinal success! output is: " + output
}); );
} resolve(output);
}
async function updateMd(mdGenerator, dataBlob) { });
return new Promise((resolve, reject) => { });
mdGenerator.update(dataBlob, (err) => { }
if (err) {
console.error("[Callback]mdGenerator update failed. error is " + err); async function updateMd(mdGenerator, dataBlob) {
reject(err); return new Promise((resolve, reject) => {
} else { mdGenerator.update(dataBlob, (err) => {
console.log("[Callback]mdGenerator update success!"); if (err) {
resolve("update success"); console.error("[Callback]mdGenerator update failed. error is " + err);
} reject(err);
}); } else {
}); console.log("[Callback]mdGenerator update success!");
} resolve("update success");
}
async function digestMd(mdGenerator) { });
return new Promise((resolve, reject) => { });
mdGenerator.digest((err, output) => { }
if (err) {
console.error("[Callback]mdGenerator digest failed. error is " + err); async function digestMd(mdGenerator) {
reject(err); return new Promise((resolve, reject) => {
} else { mdGenerator.digest((err, output) => {
console.log( if (err) {
"[Callback]mdGenerator digest success! output is: " + output console.error("[Callback]mdGenerator digest failed. error is " + err);
); reject(err);
resolve(output); } else {
} console.log(
}); "[Callback]mdGenerator digest success! output is: " + output
}); );
} resolve(output);
}
async function testMDDigestCallback(MDAlgoName) { });
var globalMd; });
var globalText = "my test data"; }
var inBlob = {
data: stringTouInt8Array(globalText), async function testMDDigestCallback(MDAlgoName) {
}; var globalMd;
var globalText = "my test data";
return new Promise((resolve, reject) => { var inBlob = {
globalMd = cryptoFramework.createMd(MDAlgoName); data: stringTouInt8Array(globalText),
expect(globalMd != null).assertTrue(); };
console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName); return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName);
updateMd(globalMd, inBlob) expect(globalMd != null).assertTrue();
.then((updateData) => { console.warn("md= " + globalMd);
expect(updateData === "update success").assertTrue(); console.warn("MD algName is: " + globalMd.algName);
return digestMd(globalMd);
}) updateMd(globalMd, inBlob)
.then((digestBlob) => { .then((updateData) => {
console.warn( expect(updateData === "update success").assertTrue();
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data) return digestMd(globalMd);
); })
let mdLen = globalMd.getMdLength(); .then((digestBlob) => {
console.warn("Md len: " + mdLen); console.warn(
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue(); "[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
resolve(); );
}) let mdLen = globalMd.getMdLength();
.catch((err) => { console.warn("Md len: " + mdLen);
console.error("testMDDigestCallback catch error: " + err); expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
reject(err); resolve();
}); })
}); .catch((err) => {
} console.error("testMDDigestCallback catch error: " + err);
reject(err);
async function testMDDigestCallbackLen(MDAlgoName, DatablobLen) { });
var globalMd; });
var i; }
var globalText;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz", n = t.length, s = ""; async function testMDDigestCallbackLen(MDAlgoName, DatablobLen) {
for (i = 0; i < DatablobLen; i++) { let globalText = "";
globalText += t.charAt(Math.floor(Math.random() * n)); let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz0123456789";
} for (let i = 0; i < DatablobLen; i++) {
console.warn("Datablob = " + globalText); globalText += t.charAt(Math.floor(Math.random() * t.length));
var ginBlob = { }
data: stringTouInt8Array(globalText), console.warn("Datablob = " + globalText);
}; var ginBlob = {
data: stringTouInt8Array(globalText),
return new Promise((resolve, reject) => { };
globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue(); try {
console.warn("md= " + globalMd); let mdObj = cryptoFramework.createMd(MDAlgoName);
console.warn("MD algName is: " + globalMd.algName); expect(mdObj != null).assertTrue();
let updateData = await updateMd(mdObj, ginBlob);
updateMd(globalMd, ginBlob) expect(updateData === "update success").assertTrue();
.then((updateData) => { let digestBlob = await digestMd(mdObj);
expect(updateData === "update success").assertTrue(); console.warn(
return digestMd(globalMd); "[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data)
}) );
.then((digestBlob) => { let mdLen = mdObj.getMdLength();
console.warn( console.log("mdLen is: " + mdLen);
"[callback]: digest result: " + uInt8ArrayToShowStr(digestBlob.data) expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue();
); } catch (err) {
let mdLen = globalMd.getMdLength(); console.error("testMDDigestCallback catch error: " + err);
console.warn("Md len: " + mdLen); throw err;
expect(digestBlob != null && mdLen != 0 && mdLen != null).assertTrue(); }
resolve(); }
})
.catch((err) => { async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) {
console.error("testMDDigestCallback catch error: " + err); var globalHMAC;
reject(err); var globalText = "my test data";
}); var globalsymKeyGenerator;
}); var ginBlob = {
} data: stringTouInt8Array(globalText),
};
async function testMDErrorAlgorithm(MDAlgoName) {
var globalMd; return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
return new Promise((resolve, reject) => { expect(globalHMAC != null).assertTrue();
console.warn("md= test begin"); console.warn("mac= " + globalHMAC);
try { console.warn("HMAC algName is: " + globalHMAC.algName);
globalMd = cryptoFramework.createMd(MDAlgoName); console.log("start to call createSymKeyGenerator()");
reject(); globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
} catch (error) { expect(globalsymKeyGenerator != null).assertTrue();
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(error.code == 801).assertTrue();
resolve(); generateSymKey(globalsymKeyGenerator)
} .then((symKey) => {
}); expect(symKey != null).assertTrue();
} return initMac(globalHMAC, symKey);
})
async function testMDErrorAlgorithmNull(MDAlgoName) { .then((initData) => {
var globalMd; expect(initData === "init success").assertTrue();
return updateMac(globalHMAC, ginBlob);
return new Promise((resolve, reject) => { })
try { .then((updateData) => {
globalMd = cryptoFramework.createMd(MDAlgoName); expect(updateData === "update success").assertTrue();
reject(); return doFinalMac(globalHMAC);
} catch (error) { })
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message); .then((macOutput) => {
expect(error.code == 401).assertTrue(); console.warn("HMAC result:" + macOutput.data);
resolve(); let macLen = globalHMAC.getMacLength();
} console.warn("MAC len:" + macLen);
}); expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
} resolve();
})
async function testHMACDigestCallback(HMACAlgoName, keyAlgoName) { .catch((err) => {
var globalHMAC; console.error("testHMACDigestCallback catch error: " + err);
var globalText = "my test data"; reject(err);
var globalsymKeyGenerator; });
var ginBlob = { });
data: stringTouInt8Array(globalText), }
};
export {
return new Promise((resolve, reject) => { testMDDigestCallback,
globalHMAC = cryptoFramework.createMac(HMACAlgoName); testHMACDigestCallback,
expect(globalHMAC != null).assertTrue(); testMDDigestCallbackLen,
console.warn("mac= " + globalHMAC); };
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
expect(globalsymKeyGenerator != null).assertTrue();
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
generateSymKey(globalsymKeyGenerator)
.then((symKey) => {
expect(symKey != null).assertTrue();
return initMac(globalHMAC, symKey);
})
.then((initData) => {
expect(initData === "init success").assertTrue();
return updateMac(globalHMAC, ginBlob);
})
.then((updateData) => {
expect(updateData === "update success").assertTrue();
return doFinalMac(globalHMAC);
})
.then((macOutput) => {
console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve();
})
.catch((err) => {
console.error("testHMACDigestCallback catch error: " + err);
reject(err);
});
});
}
export {
testMDDigestCallback,
testHMACDigestCallback,
testMDErrorAlgorithm,
testMDErrorAlgorithmNull,
testMDDigestCallbackLen
};
/* /*
* Copyright (C) 2022 Huawei Device Co., Ltd. * Copyright (C) 2022 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
import { expect } from "@ohos/hypium"; import { expect } from "@ohos/hypium";
import cryptoFramework from "@ohos.security.cryptoFramework"; import cryptoFramework from "@ohos.security.cryptoFramework";
import { import {
stringTouInt8Array, stringTouInt8Array,
uInt8ArrayToShowStr, uInt8ArrayToShowStr,
uInt8ArrayToString, uInt8ArrayToString,
} from "../common/publicDoString"; } from "../common/publicDoString";
async function testMDDigestPromise(MDAlgoName) { async function testMDDigestPromise(MDAlgoName) {
var globalMd; var globalMd;
var globalText = "my test data"; var globalText = "my test data";
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
globalMd = cryptoFramework.createMd(MDAlgoName); globalMd = cryptoFramework.createMd(MDAlgoName);
expect(globalMd != null).assertTrue(); expect(globalMd != null).assertTrue();
console.warn("md= " + globalMd); console.warn("md= " + globalMd);
console.warn("MD algName is: " + globalMd.algName); console.warn("MD algName is: " + globalMd.algName);
let inBlob = { let inBlob = {
data: stringTouInt8Array(globalText), data: stringTouInt8Array(globalText),
}; };
globalMd globalMd
.update(inBlob) .update(inBlob)
.then(() => { .then(() => {
console.warn("[Promise]: update finished"); console.warn("[Promise]: update finished");
let digestBlob = globalMd.digest(); let digestBlob = globalMd.digest();
return digestBlob; return digestBlob;
}) })
.then((mdOutput) => { .then((mdOutput) => {
console.warn( console.warn(
"[Promise]: digest result1: " + uInt8ArrayToShowStr(mdOutput.data) "[Promise]: digest result1: " + uInt8ArrayToShowStr(mdOutput.data)
); );
console.warn("[Promise]: digest result2: " + mdOutput.data); console.warn("[Promise]: digest result2: " + mdOutput.data);
let mdLen = globalMd.getMdLength(); let mdLen = globalMd.getMdLength();
console.warn("Md len: " + mdLen); console.warn("Md len: " + mdLen);
expect(mdOutput != null && mdLen != 0 && mdLen != null).assertTrue(); expect(mdOutput != null && mdLen != 0 && mdLen != null).assertTrue();
resolve(); resolve();
}) })
.catch((err) => { .catch((err) => {
console.error("[promise]catch err:" + err); console.error("[promise]catch err:" + err);
reject(err); reject(err);
}); });
}); });
} }
async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) { async function testHMACDigestPromise(HMACAlgoName, keyAlgoName) {
var globalHMAC; var globalHMAC;
var globalText = "my test data"; var globalText = "my test data";
var globalsymKeyGenerator; var globalsymKeyGenerator;
var inBlob = { var inBlob = {
data: stringTouInt8Array(globalText), data: stringTouInt8Array(globalText),
}; };
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName); globalHMAC = cryptoFramework.createMac(HMACAlgoName);
expect(globalHMAC != null).assertTrue(); expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC); console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName); console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()"); console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName); globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName); console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue(); expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok"); console.log("createSymKeyGenerator ok");
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName); console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
globalsymKeyGenerator globalsymKeyGenerator
.generateSymKey() .generateSymKey()
.then((key) => { .then((key) => {
expect(key != null).assertTrue(); expect(key != null).assertTrue();
console.warn("generateSymKey ok"); console.warn("generateSymKey ok");
console.warn("key algName:" + key.algName); console.warn("key algName:" + key.algName);
console.warn("key format:" + key.format); console.warn("key format:" + key.format);
var encodedKey = key.getEncoded(); var encodedKey = key.getEncoded();
console.warn( console.warn(
"key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data) "key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data)
); );
var promiseMacInit = globalHMAC.init(key); var promiseMacInit = globalHMAC.init(key);
return promiseMacInit; return promiseMacInit;
}) })
.then(() => { .then(() => {
var promiseMacUpdate = globalHMAC.update(inBlob); var promiseMacUpdate = globalHMAC.update(inBlob);
return promiseMacUpdate; return promiseMacUpdate;
}) })
.then(() => { .then(() => {
var promiseMacdoFinal = globalHMAC.doFinal(); var promiseMacdoFinal = globalHMAC.doFinal();
return promiseMacdoFinal; return promiseMacdoFinal;
}) })
.then((macOutput) => { .then((macOutput) => {
console.warn("HMAC result:" + macOutput.data); console.warn("HMAC result:" + macOutput.data);
let macLen = globalHMAC.getMacLength(); let macLen = globalHMAC.getMacLength();
console.warn("MAC len:" + macLen); console.warn("MAC len:" + macLen);
expect(macOutput != null && macLen != 0 && macLen != null).assertTrue(); expect(macOutput != null && macLen != 0 && macLen != null).assertTrue();
resolve(); resolve();
}) })
.catch((err) => { .catch((err) => {
console.error("[promise]catch err:" + err); console.error("[promise]catch err:" + err);
reject(err); reject(err);
}); });
}); });
} }
async function testHMACDigestPromiseDatablobLong(HMACAlgoName, keyAlgoName, DatablobLen) { export { testMDDigestPromise, testHMACDigestPromise };
var globalHMAC;
var globalsymKeyGenerator;
var i;
var globalText;
var t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz",n = t.length,s="";
for (i = 0; i < DatablobLen; i++){
globalText += t.charAt(Math.floor(Math.random() * n));
}
console.warn("Datablob = " + globalText);
var inBlob = {
data: stringTouInt8Array(globalText),
};
return new Promise((resolve, reject) => {
globalHMAC = cryptoFramework.createMac(HMACAlgoName);
resolve();
expect(globalHMAC != null).assertTrue();
console.warn("mac= " + globalHMAC);
console.warn("HMAC algName is: " + globalHMAC.algName);
console.log("start to call createSymKeyGenerator()");
globalsymKeyGenerator = cryptoFramework.createSymKeyGenerator(keyAlgoName);
console.warn("symKeyGenerator algName:" + globalsymKeyGenerator.algName);
expect(globalsymKeyGenerator != null).assertTrue();
console.log("createSymKeyGenerator ok");
globalsymKeyGenerator
.generateSymKey()
.then((key) => {
expect(key != null).assertTrue();
console.warn("generateSymKey ok");
console.warn("key algName:" + key.algName);
console.warn("key format:" + key.format);
var encodedKey = key.getEncoded();
console.warn(
"key getEncoded hex: " + uInt8ArrayToShowStr(encodedKey.data)
);
var promiseMacInit = globalHMAC.init(key);
return promiseMacInit;
})
.then(() => {
try {
var promiseMacUpdate = globalHMAC.update(inBlob);
console.log("promiseMacUpdate = " + promiseMacUpdate);
} catch (error) {
console.error("[Promise]: error code: " + error.code + ", message is: " + error.message);
}
})
.then(() => {
var promiseMacdoFinal = globalHMAC.doFinal();
console.log("promiseMacdoFinal = " + promiseMacdoFinal);
return promiseMacdoFinal;
})
.catch((err) => {
console.error("[promise]catch err:" + err);
reject(err);
});
});
}
export { testMDDigestPromise, testHMACDigestPromise, testHMACDigestPromiseDatablobLong};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册