提交 88812bb7 编写于 作者: Q qiaozzzh

XTS configuration

Signed-off-by: Nqiaozzzh <qiaozhang@huawei.com>
Change-Id: Ib34ecf79c0e160817a4805d80b19659f95788b13
上级 b59548bd
......@@ -18,587 +18,248 @@ import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import * as asyCallback from "./utils/asysmmetricspec/publicAsymmetricSpecCallback";
import * as asyPromise from "./utils/asysmmetricspec/publicAsymmetricSpecPromise";
import * as common from "./utils/common/publicDoSpec";
import * as callback from "./utils/asymmetric/publicAsymmetricCallback";
import * as promise from "./utils/asymmetric/publicAsymmetricPromise";
function genRsa2048KeyPairSpec() {
return common.genRsa2048KeyPairSpec();
}
function genDsa2048CommonSpec() {
return common.genDsa2048CommonSpecBigE();
}
function genRsa2048CommonSpec() {
return common.genRsa2048CommonSpec();
}
function genRsa2048PubKetSpec() {
return common.genRsa2048PubKetSpec();
return common.genDsa2048CommonSpecBigE();
}
function genDsa2048KeyPairSpec() {
return common.genDsa2048KeyPairSpecBigE();
return common.genDsa2048KeyPairSpecBigE();
}
function genDsa2048PubKeySpec() {
return common.genDsa2048PubKeySpecBigE();
return common.genDsa2048PubKeySpecBigE();
}
function genEccKeyTypeSpec(keyType) {
return common.genEccKeySpec(keyType);
}
function genEccCommonSpecErr() {
let fieldFp = {
fieldType: "Fp",
p: BigInt("0xffffffffffffffffffffffffffffffff000000000000000000000001"),
};
let G = {
x: BigInt("0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"),
y: BigInt("0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"),
};
let eccCommonSpec = {
algName: "ECC",
specType: 5,
field: fieldFp,
a: BigInt("0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe"),
b: BigInt("0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4"),
g: G,
n: BigInt("0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d"),
h: 1,
};
return eccCommonSpec;
}
function genDsaKeyPairSpecErr() {
let dsaCommonSpec = common.genDsa2048CommonSpecBigE();
let dsaKeyPairSpec = {
algName: "DSA",
specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC,
params: dsaCommonSpec,
sk: "111112222111",
pk: BigInt(
"0x178fa8118492ec8347c76ab092af5a2037a36479d2d03dcde061888821cc745dce4c5147f0c55c4c827aaf72adb9" +
"e053f278b7f0b5487f8a3a18d19f8b7da547b795ab98f87b7450568e57f0eef5b7baab8586f92bef4156a0a49fb73800460aa6f1f" +
"c1fd84e85449243215d6eccc2cb26310d21c4bd8d24bcd91819d7dcf1e7935048032cae2ee749885f9357279936b420abfca72bf2" +
"d998d7d4349d9650589aea54f3eef56314ee85837476e15295c3f7eb04047ba7281bccea4a4e84dad89c79d89b66892fcfacd779f" +
"9a9d8451378b90014c97e22518667b09f261123c838d7701d158e4d4f959740a1c27e011872f410e68d52167ff2c9f8338b33b7ce"
),
};
return dsaKeyPairSpec;
}
function genRsaKeyPairSpecErr() {
let nIn = BigInt(
"0x9260d0750ae117eee55c3f3deaba74917521a262ee76007cdf8a56755ad73a1598a1408410a01434c3f5bc54a88" +
"b57fa19fc4328daea0750a4c44e88cff3b2382621b80f670464433e4336e6d003e8cd65bff211da144b88291c2259a00a72b711c116ef7" +
"686e8fee34e4d933c868187bdc26f7be071493c86f7a5941c3510806ad67b0f94d88f5cf5c02a092821d8626e8932b65c5bd8c92049c21" +
"0932b7afa7ac59c0e886ae5c1edb00d8ce2c57633db26bd6639bff73cee82be9275c402b4cf2a4388da8cf8c64eefe1c5a0f5ab8057c39" +
"fa5c0589c3e253f0960332300f94bea44877b588e1edbde97cf2360727a09b775262d7ee552b3319b9266f05a25"
);
let rsaCommSpec = {
n: nIn,
algName: "RSA",
specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC,
};
let rsaKeyPairSpec = {
params: rsaCommSpec,
sk: "11121",
pk: "11121",
algName: "RSA",
specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC,
};
return rsaKeyPairSpec;
return common.genEccKeySpec(keyType);
}
export default function AsyKeyGeneratorBySpecJsunit() {
describe("AsyKeyGeneratorBySpecJsunit", function () {
console.info("##########start AsyKeyGeneratorBySpecJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0100
* @tc.name DSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genDsa2048CommonSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0100",
0,
async function (done) {
asyCallback
.generateByCommonSpec(genDsa2048CommonSpec(), "DSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1900 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0200
* @tc.name DSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genDsa2048PubKeySpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0200",
0,
async function (done) {
asyPromise
.generateByPubKeySpec(genDsa2048PubKeySpec(), "DSA")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0200 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0300
* @tc.name DSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genDsa2048KeyPairSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0300",
0,
async function (done) {
asyPromise
.generateByKeyPairSpec(genDsa2048KeyPairSpec(), "DSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GenerateBySpec_Func_0300 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0400
* @tc.name DSA pubKey generate prikey failed
* @tc.desc the asyKeySpec is genDsa2048PubKeySpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0400",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genDsa2048PubKeySpec()
describe("AsyKeyGeneratorBySpecJsunit", function () {
console.info("##########start AsyKeyGeneratorBySpecJsunit##########");
beforeAll(function () {
});
afterEach(function () {
});
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0100
* @tc.name DSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genDsa2048CommonSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0100",
0,
async function (done) {
asyCallback
.generateByCommonSpec(genDsa2048CommonSpec(), "DSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1900 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
asyKeyPairSpec
.generatePriKey()
.then((priKey) => {
console.log("priKey" + priKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed Security_CryptoFramework_GeneratorBySpec_Func_0400" +
" failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0500
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccCommonSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0500",
0,
async function (done) {
asyCallback
.generateByCommonSpec(
genEccKeyTypeSpec(
cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC
),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0500 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0600
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccPriKeySpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0600",
0,
async function (done) {
asyCallback
.generateByPriKeySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PRIVATE_KEY_SPEC),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0600 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0700
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccPubKeySpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0700",
0,
async function (done) {
asyPromise
.generateByPubKeySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0700 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0800
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccKeyPairSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0800",
0,
async function (done) {
asyPromise
.generateByKeyPairSpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0800 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0900
* @tc.name ECC PriKey generation Pubkey
* @tc.desc the asyKeySpec is eccPrikeySpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0900",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PRIVATE_KEY_SPEC)
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0200
* @tc.name DSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genDsa2048PubKeySpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0200",
0,
async function (done) {
asyPromise
.generateByPubKeySpec(genDsa2048PubKeySpec(), "DSA")
.then((data) => {
expect(data == null).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0200 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
asyKeyPairSpec
.generatePubKey()
.then((pubKey) => {
console.log("pubKey" + pubKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed Security_CryptoFramework_GeneratorBySpec_Func_0900" +
" failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1000
* @tc.name ECC PubKey generation Prikey
* @tc.desc the asyKeySpec is eccPubkeySpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1000",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC)
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0300
* @tc.name DSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genDsa2048KeyPairSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0300",
0,
async function (done) {
asyPromise
.generateByKeyPairSpec(genDsa2048KeyPairSpec(), "DSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GenerateBySpec_Func_0300 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
asyKeyPairSpec
.generatePriKey()
.then((priKey) => {
console.log("priKey" + priKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed Security_CryptoFramework_GeneratorBySpec_Func_1000" +
" failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1100
* @tc.name RSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genRsa2048CommonSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1100",
0,
async function (done) {
asyCallback
.generateByCommonSpec(genRsa2048CommonSpec(), "RSA")
.then((result) => {
console.log("result :" + result);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1100 catch err " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1200
* @tc.name RSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genRsa2048PubKetSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1200",
0,
async function (done) {
asyPromise
.generateByPubKeySpec(genRsa2048PubKetSpec(), "RSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1200 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1300
* @tc.name RSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genRsa2048KeyPairSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1300",
0,
async function (done) {
asyPromise
.generateByKeyPairSpec(genRsa2048KeyPairSpec(), "RSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1300 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1400
* @tc.name RSA pubKey generation prikey failed
* @tc.desc the asyKeySpec is genRsa2048PubKetSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1400",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genRsa2048PubKetSpec()
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0400
* @tc.name DSA pubKey generate prikey failed
* @tc.desc the asyKeySpec is genDsa2048PubKeySpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0400",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genDsa2048PubKeySpec()
);
asyKeyPairSpec
.generatePriKey()
.then((priKey) => {
console.log("priKey" + priKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed Security_CryptoFramework_GeneratorBySpec_Func_0400" +
" failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
asyKeyPairSpec
.generatePriKey()
.then((priKey) => {
console.log("priKey" + priKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed generateSpecAsyKeyPair failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1500
* @tc.name create key parser failed
* @tc.desc Pass in an exception parameter error EccCommonStruct and call it as a async
* @tc.desc Pass in an exception parameter error RsaKeyPairStruct and call it as a async
* @tc.desc Pass in an exception parameter error DsaKeyPairStruct and call it as a async
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1500",
0,
async function (done) {
try {
cryptoFramework.createAsyKeyGeneratorBySpec(genEccCommonSpecErr());
expect(null).assertFail();
} catch (err) {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1500 catch err " +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createAsyKeyGeneratorBySpec(genRsaKeyPairSpecErr());
expect(null).assertFail();
} catch (err) {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1500 catch err " +
err
);
expect(err.code == 401).assertTrue();
}
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0500
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccCommonSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0500",
0,
async function (done) {
asyCallback
.generateByCommonSpec(
genEccKeyTypeSpec(
cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC
),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0500 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
try {
cryptoFramework.createAsyKeyGeneratorBySpec(genDsaKeyPairSpecErr());
expect(null).assertFail();
} catch (err) {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1500 catch err " +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0600
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccPriKeySpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0600",
0,
async function (done) {
asyCallback
.generateByPriKeySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PRIVATE_KEY_SPEC),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0600 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1600
* @tc.name relatively pubkey and prikey
* @tc.desc the asyKeySpec is genDsaKeyPairSpecErr, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1600",
0,
async function (done) {
promise
.keyGenerationBySpecProcess(genDsa2048KeyPairSpec())
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1600 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0700
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccPubKeySpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0700",
0,
async function (done) {
asyPromise
.generateByPubKeySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0700 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1700
* @tc.name relatively pubkey and prikey
* @tc.desc the asyKeySpec is "DSA", Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1700",
0,
async function (done) {
callback
.keyGenerationBySpecProcess("DSA2048")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1700 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
});
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0800
* @tc.name ECC Key generation scenario by spec
* @tc.desc the asyKeySpec is eccKeyPairSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0800",
0,
async function (done) {
asyPromise
.generateByKeyPairSpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC),
"ECC"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_0800 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
});
}
/*
* Copyright (C) 2022-2023 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 cryptoFramework from "@ohos.security.cryptoFramework";
import { describe, beforeAll, afterEach, it, expect } from "@ohos/hypium";
import * as asyCallback from "./utils/asysmmetricspec/publicAsymmetricSpecCallback";
import * as asyPromise from "./utils/asysmmetricspec/publicAsymmetricSpecPromise";
import * as common from "./utils/common/publicDoSpec";
import * as callback from "./utils/asymmetric/publicAsymmetricCallback";
import * as promise from "./utils/asymmetric/publicAsymmetricPromise";
function genRsa2048KeyPairSpec() {
return common.genRsa2048KeyPairSpec();
}
function genRsa2048CommonSpec() {
return common.genRsa2048CommonSpec();
}
function genRsa2048PubKetSpec() {
return common.genRsa2048PubKetSpec();
}
function genDsa2048KeyPairSpec() {
return common.genDsa2048KeyPairSpecBigE();
}
function genEccKeyTypeSpec(keyType) {
return common.genEccKeySpec(keyType);
}
function genEccCommonSpecErr() {
let fieldFp = {
fieldType: "Fp",
p: BigInt("0xffffffffffffffffffffffffffffffff000000000000000000000001"),
};
let G = {
x: BigInt("0xb70e0cbd6bb4bf7f321390b94a03c1d356c21122343280d6115c1d21"),
y: BigInt("0xbd376388b5f723fb4c22dfe6cd4375a05a07476444d5819985007e34"),
};
let eccCommonSpec = {
algName: "ECC",
specType: 5,
field: fieldFp,
a: BigInt("0xfffffffffffffffffffffffffffffffefffffffffffffffffffffffe"),
b: BigInt("0xb4050a850c04b3abf54132565044b0b7d7bfd8ba270b39432355ffb4"),
g: G,
n: BigInt("0xffffffffffffffffffffffffffff16a2e0b8f03e13dd29455c5c2a3d"),
h: 1,
};
return eccCommonSpec;
}
function genDsaKeyPairSpecErr() {
let dsaCommonSpec = common.genDsa2048CommonSpecBigE();
let dsaKeyPairSpec = {
algName: "DSA",
specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC,
params: dsaCommonSpec,
sk: "111112222111",
pk: BigInt(
"0x178fa8118492ec8347c76ab092af5a2037a36479d2d03dcde061888821cc745dce4c5147f0c55c4c827aaf72adb9" +
"e053f278b7f0b5487f8a3a18d19f8b7da547b795ab98f87b7450568e57f0eef5b7baab8586f92bef4156a0a49fb73800460aa6f1f" +
"c1fd84e85449243215d6eccc2cb26310d21c4bd8d24bcd91819d7dcf1e7935048032cae2ee749885f9357279936b420abfca72bf2" +
"d998d7d4349d9650589aea54f3eef56314ee85837476e15295c3f7eb04047ba7281bccea4a4e84dad89c79d89b66892fcfacd779f" +
"9a9d8451378b90014c97e22518667b09f261123c838d7701d158e4d4f959740a1c27e011872f410e68d52167ff2c9f8338b33b7ce"
),
};
return dsaKeyPairSpec;
}
function genRsaKeyPairSpecErr() {
let nIn = BigInt(
"0x9260d0750ae117eee55c3f3deaba74917521a262ee76007cdf8a56755ad73a1598a1408410a01434c3f5bc54a88" +
"b57fa19fc4328daea0750a4c44e88cff3b2382621b80f670464433e4336e6d003e8cd65bff211da144b88291c2259a00a72b711c116ef7" +
"686e8fee34e4d933c868187bdc26f7be071493c86f7a5941c3510806ad67b0f94d88f5cf5c02a092821d8626e8932b65c5bd8c92049c21" +
"0932b7afa7ac59c0e886ae5c1edb00d8ce2c57633db26bd6639bff73cee82be9275c402b4cf2a4388da8cf8c64eefe1c5a0f5ab8057c39" +
"fa5c0589c3e253f0960332300f94bea44877b588e1edbde97cf2360727a09b775262d7ee552b3319b9266f05a25"
);
let rsaCommSpec = {
n: nIn,
algName: "RSA",
specType: cryptoFramework.AsyKeySpecType.COMMON_PARAMS_SPEC,
};
let rsaKeyPairSpec = {
params: rsaCommSpec,
sk: "11121",
pk: "11121",
algName: "RSA",
specType: cryptoFramework.AsyKeySpecType.KEY_PAIR_SPEC,
};
return rsaKeyPairSpec;
}
export default function AsyKeyGeneratorBySpecTwoJsunit() {
describe("AsyKeyGeneratorBySpecTwoJsunit", function () {
console.info("##########start AsyKeyGeneratorBySpecTwoJsunit##########");
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_0900
* @tc.name ECC PriKey generation Pubkey
* @tc.desc the asyKeySpec is eccPrikeySpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_0900",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PRIVATE_KEY_SPEC)
);
asyKeyPairSpec
.generatePubKey()
.then((pubKey) => {
console.log("pubKey" + pubKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed Security_CryptoFramework_GeneratorBySpec_Func_0900" +
" failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1000
* @tc.name ECC PubKey generation Prikey
* @tc.desc the asyKeySpec is eccPubkeySpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1000",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genEccKeyTypeSpec(cryptoFramework.AsyKeySpecType.PUBLIC_KEY_SPEC)
);
asyKeyPairSpec
.generatePriKey()
.then((priKey) => {
console.log("priKey" + priKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed Security_CryptoFramework_GeneratorBySpec_Func_1000" +
" failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1100
* @tc.name RSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genRsa2048CommonSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1100",
0,
async function (done) {
asyCallback
.generateByCommonSpec(genRsa2048CommonSpec(), "RSA")
.then((result) => {
console.log("result :" + result);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1100 catch err " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1200
* @tc.name RSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genRsa2048PubKetSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1200",
0,
async function (done) {
asyPromise
.generateByPubKeySpec(genRsa2048PubKetSpec(), "RSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1200 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1300
* @tc.name RSA Key generation scenario by spec
* @tc.desc the asyKeySpec is genRsa2048KeyPairSpec, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1300",
0,
async function (done) {
asyPromise
.generateByKeyPairSpec(genRsa2048KeyPairSpec(), "RSA")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1300 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1400
* @tc.name RSA pubKey generation prikey failed
* @tc.desc the asyKeySpec is genRsa2048PubKetSpec, Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1400",
0,
async function (done) {
let asyKeyPairSpec = cryptoFramework.createAsyKeyGeneratorBySpec(
genRsa2048PubKetSpec()
);
asyKeyPairSpec
.generatePriKey()
.then((priKey) => {
console.log("priKey" + priKey);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"generatePubTopriSpecFailed generateSpecAsyKeyPair failed. error is " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1500
* @tc.name create key parser failed
* @tc.desc Pass in an exception parameter error EccCommonStruct and call it as a async
* @tc.desc Pass in an exception parameter error RsaKeyPairStruct and call it as a async
* @tc.desc Pass in an exception parameter error DsaKeyPairStruct and call it as a async
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1500",
0,
async function (done) {
try {
cryptoFramework.createAsyKeyGeneratorBySpec(genEccCommonSpecErr());
expect(null).assertFail();
} catch (err) {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1500 catch err " +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createAsyKeyGeneratorBySpec(genRsaKeyPairSpecErr());
expect(null).assertFail();
} catch (err) {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1500 catch err " +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createAsyKeyGeneratorBySpec(genDsaKeyPairSpecErr());
expect(null).assertFail();
} catch (err) {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1500 catch err " +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1600
* @tc.name relatively pubkey and prikey
* @tc.desc the asyKeySpec is genDsaKeyPairSpecErr, Use the promise Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1600",
0,
async function (done) {
promise
.keyGenerationBySpecProcess(genDsa2048KeyPairSpec())
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1600 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_GeneratorBySpec_Func_1700
* @tc.name relatively pubkey and prikey
* @tc.desc the asyKeySpec is "DSA", Use the callback Style of Interface
*/
it(
"Security_CryptoFramework_GeneratorBySpec_Func_1700",
0,
async function (done) {
callback
.keyGenerationBySpecProcess("DSA2048")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_GeneratorBySpec_Func_1700 catch err " +
err
);
expect(null).assertFail();
});
done();
}
);
});
}
......@@ -13,22 +13,26 @@
* limitations under the License.
*/
import SecurityRandomSyncJsunit from "./SecurityRandomSync.test";
import SecuritySignatureVerificationJsunit from "./SecuritySignatureVerification.test";
import SecurityKeyAgreementJsunit from "./SecurityKeyAgreement.test";
import AsyKeyGeneratorBySpecJsunit from "./AsyKeyGeneratorBySpec.test";
import AsyGetKeySpecBySpecJsunit from "./AsyGetKeySpecBySpec.test";
import SecurityCipherJsunit from "./SecurityCipher.test";
import SecuritySignatureVerificationSM2Jsunit from "./SecuritySignatureVerificationSM2.test";
import SecurityCipherSM2SM4Jsunit from "./SecurityCipherSM2SM4.test";
import DigestAlgorithmSm3Jsunit from "./DigestAlgorithmSm3.test";
import SecurityRandomSyncJsunit from './SecurityRandomSync.test';
import SecuritySignatureVerificationTwoJsunit from './SecuritySignatureVerificationTwo.test';
import SecuritySignatureVerificationJsunit from './SecuritySignatureVerification.test';
import SecurityKeyAgreementJsunit from './SecurityKeyAgreement.test';
import AsyKeyGeneratorBySpecJsunit from './AsyKeyGeneratorBySpec.test';
import AsyKeyGeneratorBySpecTwoJsunit from './AsyKeyGeneratorBySpecTwo.test';
import AsyGetKeySpecBySpecJsunit from './AsyGetKeySpecBySpec.test';
import SecurityCipherJsunit from './SecurityCipher.test';
import SecuritySignatureVerificationSM2Jsunit from './SecuritySignatureVerificationSM2.test';
import SecurityCipherSM2SM4Jsunit from './SecurityCipherSM2SM4.test';
import DigestAlgorithmSm3Jsunit from './DigestAlgorithmSm3.test';
export default function testsuite() {
SecurityRandomSyncJsunit();
SecuritySignatureVerificationJsunit();
SecuritySignatureVerificationTwoJsunit();
SecurityKeyAgreementJsunit();
AsyGetKeySpecBySpecJsunit();
AsyKeyGeneratorBySpecJsunit();
AsyKeyGeneratorBySpecTwoJsunit();
SecurityCipherJsunit();
SecuritySignatureVerificationSM2Jsunit();
SecurityCipherSM2SM4Jsunit();
......
......@@ -20,1676 +20,583 @@ import * as asyCallback from "./utils/asymmetric/publicAsymmetricCallback";
import cryptoFramework from "@ohos.security.cryptoFramework";
export default function SecuritySignatureVerificationJsunit() {
describe("SecuritySignatureVerificationJsunit", function () {
console.log(
"##########start SecuritySignatureVerificationJsunit##########"
);
beforeAll(function () {});
afterEach(function () {});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0100
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the RSAKeyPar structure with a signature verification type of "RSA512|PKCS1|MD5"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0100",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA512|PKCS1|MD5"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0200
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA768|PSS|SHA1|MGF1_SHA1", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0200",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA768|PSS|SHA1|MGF1_SHA1",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0300
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA1024|PSS|SHA224|MGF1_SHA224", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0300",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PSS|SHA224|MGF1_SHA224",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0400
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA2048|PSS|SHA256|MGF1_SHA256", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0400",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA2048|PSS|SHA256|MGF1_SHA256",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0500
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA3072|PSS|SHA384|MGF1_SHA384", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0500",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA3072|PSS|SHA384|MGF1_SHA384",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0600
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA4096|PSS|SHA512|MGF1_SHA512", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0600",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA4096|PSS|SHA512|MGF1_SHA512",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0700
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA8192|PSS|SHA512|MGF1_SHA512", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0700",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA8192|PSS|SHA512|MGF1_SHA512",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0800
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the RSAKeyPar structure with a signature verification type of "RSA|PKCS1|MD5"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0800",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PKCS1|MD5"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0900
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA256|MGF1_SHA256", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0900",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA256|MGF1_SHA256",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1000
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA384|MGF1_SHA384", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1000",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA384|MGF1_SHA384",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1100
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA512|MGF1_SHA512", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1100",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA512|MGF1_SHA512",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"asyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1200
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA224|MGF1_SHA224", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1200",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA224|MGF1_SHA224",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1300
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC256|SHA256"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1300",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC256|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1400
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPair structure with a signature verification type of "ECC|SHA224"(No length)
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1400",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1500
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC224|SHA256"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1500",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC224|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1600
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC384|SHA224"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1600",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC384|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1700
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC521|SHA256"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1700",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC521|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1800
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA|SHA1"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1800",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA|SHA1"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1900
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA|SHA224"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1900",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2000
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA1024|SHA224"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2000",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA1024|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2100
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA2048|SHA256"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2100",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA2048|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2200
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA3072|SHA384"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2200",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA3072|SHA384"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2300
* @tc.name Scenario testing of signature verification and key generation and conversion combination
* @tc.desc Pass in key generation parameter "DSA1024|PRIMES_2",
* signature verification parameter "DSA1024|SHA1",
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2300",
0,
async function (done) {
await asyPromise
.signAndVerifyNormalProcess("DSA1024", "DSA1024|SHA1")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2400
* @tc.name Scenario testing of signature verification and key generation and conversion combination
* @tc.desc Pass in key generation parameter "DSA2048|PRIMES_2",
* signature verification parameter "DSA2048|SHA256",
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2400",
0,
async function (done) {
await asyCallback
.signAndVerifyNormalProcess("DSA2048", "DSA2048|SHA256")
.then((result) => {
expect(result == true).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2500
* @tc.name Scenario testing of signature verification and key generation and conversion combination
* @tc.desc Pass in key generation parameter "DSA3072|PRIMES_2",
* signature verification parameter "DSA3072|SHA384",
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2500",
0,
async function (done) {
await asyPromise
.signAndVerifyNormalProcess("DSA3072", "DSA3072|SHA384")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyNormalProcess catch error: " + err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2600
* @tc.name Exception in creating signature object
* @tc.desc Pass in an exception parameter "RSA10000|PKCS1|SHA224" and call it as a synchronously
* @tc.desc Pass in an exception parameter null and call it as a synchronously
* @tc.desc Pass in an exception parameter "RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5" and
* call it as a synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2600",
0,
async function (done) {
try {
cryptoFramework.createSign("RSA10000|PKCS1|SHA224");
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_2600 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign(null);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_2600 2 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign(
"RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5"
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_2600 3 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2700
* @tc.name Exception while initializing rsa signature object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter rsa and call them as async
* @tc.desc Pass in the public key generated by the exception parameter dsa and call it as a async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2700",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genRsa2048KeyPairSpec()
describe("SecuritySignatureVerificationJsunit", function () {
console.log(
"##########start SecuritySignatureVerificationJsunit##########"
);
let signGenerator = cryptoFramework.createSign("RSA1024|PKCS1|SHA224");
let signKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
signKeyPair = keyPair;
resolve();
}
});
beforeAll(function () {
});
try {
signGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
signGenerator.init(signKeyPair.priKey, signKeyPair.priKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
let dsaGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genDsa2048KeyPairSpecBigE()
);
let dsaKeyPair;
await new Promise((resolve, reject) => {
dsaGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
dsaKeyPair = keyPair;
resolve();
}
});
afterEach(function () {
});
try {
await signGenerator.init(dsaKeyPair.pubKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
signGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2800
* @tc.name Exception in adding signature data to rsa objects
* @tc.desc Call it as a promise without passing in parameters
* @tc.desc Pass in two exception parameters "This is a sign test" and "This is a sign test"
* and call them as promise
* @tc.desc Pass in an exception parameter null and call it as a promise
* @tc.desc Pass in an exception parameter of "This is a sign test" multiplied by 1000 times and call it promise
* @tc.desc The signature object was not initialized, and the data to be signed "This is a sign test"
* was passed in as a promise to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2800",
0,
async function (done) {
await asyPromise
.updateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
0
)
.then((data) => {
console.log("data 1 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise updateAbnormalParameterProcess 1 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
await asyPromise
.updateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
1
)
.then((data) => {
console.log("data 2 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise updateAbnormalParameterProcess 2 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
await asyPromise
.updateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
2
)
.then((data) => {
console.log("data 3 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise updateAbnormalParameterProcess 3 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
await asyPromise
.updateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
3
)
.then((data) => {
console.log("data 4 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise updateAbnormalParameterProcess 4 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
await asyPromise
.updateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
4
)
.then((data) => {
console.log("data 5 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise updateAbnormalParameterProcess 5 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2900
* @tc.name Signature data anomaly
* @tc.desc Pass in an exception parameter "This is a sign test" and "This is a sign test"
* call it in the form of a callback
* @tc.descPass in an exception parameter null and call it as a callback
* @tc.desc Pass in an exception parameter "" and call it as a callback
* @tc.desc The signature object was not initialized, and the data to be signed 'This is a sign test'
* was passed in as a callback to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2900",
0,
async function (done) {
await asyCallback
.signAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
0
)
.then((data) => {
console.log("data 1 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback signAbnormalParameterProcess 1 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
await asyCallback
.signAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
1
)
.then((data) => {
console.log("data 2 : " + data);
expect(data != null).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAbnormalParameterProcess 2 catch error: " + err
);
expect(err.code == undefined).assertTrue();
});
await asyCallback
.signAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
2
)
.then((data) => {
console.log("data 3 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback signAbnormalParameterProcess 3 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
await asyCallback
.signAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
3
)
.then((data) => {
console.log("data 4 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback signAbnormalParameterProcess 4 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3000
* @tc.name Exception in signature setting parameter mode
* @tc.desc Pass in exception parameter mode 1024, parameter 2, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter 4294967296, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, parameter "string",
* and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3000",
0,
async function (done) {
let signGenerator = cryptoFramework.createSign(
"RSA1024|PSS|SHA224|MGF1_SHA224"
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0100
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the RSAKeyPar structure with a signature verification type of "RSA512|PKCS1|MD5"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0100",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA512|PKCS1|MD5"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
try {
signGenerator.setSignSpec(1024, 2);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
signGenerator.setSignSpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
4294967296
);
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 2 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
signGenerator.setSignSpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
"string"
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 5 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3100
* @tc.name Exception in signature getting parameter mode
* @tc.desc Pass in exception parameter mode 10000, and call it synchronously
* @tc.desc Pass in exception parameter mode 6, and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3100",
0,
async function (done) {
let signGenerator = cryptoFramework.createSign(
"RSA1024|PSS|SHA224|MGF1_SHA224"
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0200
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA768|PSS|SHA1|MGF1_SHA1", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0200",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA768|PSS|SHA1|MGF1_SHA1",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
try {
signGenerator.getSignSpec(10000);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
signGenerator.getSignSpec(6);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 4 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3200
* @tc.name Abnormal situation of creating verification objects
* @tc.desc Pass in an exception parameter "RSA10000|SHA224" and call it as a synchronously
* @tc.desc Pass in an exception parameter null and call it as a synchronously
* @tc.desc Pass in an exception parameter "RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5" and
* call it as a synchronously
* @tc.desc Pass in an exception parameter "DSA512|PKCS1|MD5" and call it as a synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3200",
0,
async function (done) {
try {
cryptoFramework.createVerify("RSA10000|SHA224");
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 1 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify(null);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 2 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify(
"RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5"
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 3 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("DSA512|PKCS1|MD5");
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 5 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3300
* @tc.name Abnormal situation of initialization verification object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter rsa and call them as async
* @tc.desc Pass in the public key generated by the exception parameter dsa and call it as a async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3300",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genRsa2048KeyPairSpec()
);
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA224"
);
let verifyKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
verifyKeyPair = keyPair;
resolve();
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0300
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA1024|PSS|SHA224|MGF1_SHA224", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0300",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PSS|SHA224|MGF1_SHA224",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
});
});
try {
verifyGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.init(verifyKeyPair.pubKey, verifyKeyPair.pubKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
let dsaGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genDsa2048KeyPairSpecBigE()
);
let dsaKeyPair;
await new Promise((resolve, reject) => {
dsaGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
dsaKeyPair = keyPair;
resolve();
}
});
});
try {
await verifyGenerator.init(dsaKeyPair.pubKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3400
* @tc.name Abnormal situation of additional signature verification data
* @tc.desc Call it as a promise without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a promise
* @tc.desc Pass in an exception parameter "" and call it as a promise
* @tc.desc Pass in two exception parameters "This is a sign test" and "This is a sign test"
* and call them as promise
* @tc.desc The verify object was not initialized, and the data to be signed "This is a sign test"
* was passed in as a promise to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3400",
0,
async function (done) {
await asyPromise
.verifyUpdateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
0
)
.then((data) => {
console.log("data 1 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise verifyUpdateAbnormalParameterProcess 1 catch error: " +
err
);
expect(err.code == 401).assertTrue();
});
await asyPromise
.verifyUpdateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
1
)
.then((data) => {
console.log("data 2 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise verifyUpdateAbnormalParameterProcess 2 catch error: " +
err
);
expect(err.code == 401).assertTrue();
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0400
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA2048|PSS|SHA256|MGF1_SHA256", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0400",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA2048|PSS|SHA256|MGF1_SHA256",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
await asyPromise
.verifyUpdateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
2
)
.then((data) => {
console.log("data 3 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise verifyUpdateAbnormalParameterProcess 3 catch error: " +
err
);
expect(err.code == 401).assertTrue();
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0500
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA3072|PSS|SHA384|MGF1_SHA384", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0500",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA3072|PSS|SHA384|MGF1_SHA384",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
await asyPromise
.verifyUpdateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
3
)
.then((data) => {
console.log("data 4 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise verifyUpdateAbnormalParameterProcess 4 catch error: " +
err
);
expect(err.code == 401).assertTrue();
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0600
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA4096|PSS|SHA512|MGF1_SHA512", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0600",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA4096|PSS|SHA512|MGF1_SHA512",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
await asyPromise
.verifyUpdateAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
4
)
.then((data) => {
console.log("data 5 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyPromise verifyUpdateAbnormalParameterProcess 5 catch error: " +
err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0700
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA8192|PSS|SHA512|MGF1_SHA512", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0700",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA8192|PSS|SHA512|MGF1_SHA512",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3500
* @tc.name Abnormal situation of visa verification signature data
* @tc.desc Call it as a callback without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a callback
* @tc.desc Pass in an exception parameter "" and call it as a callback
* @tc.desc Pass in an exception parameter "This is a sign test" and "This is a sign test"
* call it in the form of a callback
* @tc.desc The verify object was not initialized, and the data to be signed 'This is a sign test'
* was passed in as a callback to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3500",
0,
async function (done) {
await asyCallback
.verifyAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
0
)
.then((data) => {
console.log("data 1 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback verifyAbnormalParameterProcess 1 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0800
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the RSAKeyPar structure with a signature verification type of "RSA|PKCS1|MD5"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0800",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PKCS1|MD5"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
await asyCallback
.verifyAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
1
)
.then((data) => {
console.log("data 2 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback verifyAbnormalParameterProcess 2 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_0900
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA256|MGF1_SHA256", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_0900",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA256|MGF1_SHA256",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
await asyCallback
.verifyAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
2
)
.then((data) => {
console.log("data 3 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback verifyAbnormalParameterProcess 3 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1000
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA384|MGF1_SHA384", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1000",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA384|MGF1_SHA384",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
await asyCallback
.verifyAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
3
)
.then((data) => {
console.log("data 4 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback verifyAbnormalParameterProcess 4 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1100
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec before init Sign/Verify object
* @tc.desc Use a callback style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA512|MGF1_SHA512", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1100",
0,
async function (done) {
await asyCallback
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA512|MGF1_SHA512",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"asyCallback signAndVerifySetAndGetSpecProcess catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
await asyCallback
.verifyAbnormalParameterProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA1024|PKCS1|SHA224",
4
)
.then((data) => {
console.log("data 4 : " + data);
expect(null).assertFail();
})
.catch((err) => {
console.error(
"AsyCallback verifyAbnormalParameterProcess 4 catch error: " + err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1200
* @tc.name Use createAsyKeyGeneratorBySpec to Create RSA asyKeySpec
* and Signature verification combination scenario testing
* and set Sign/Verify spec after init Sign/Verify object
* @tc.desc Use a promise style interface to pass in the RSAKeyPar structure with signature verification type
* "RSA|PSS|SHA224|MGF1_SHA224", parameter setting mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
* and parameter 32
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1200",
0,
async function (done) {
await asyPromise
.signAndVerifySetAndGetSpecProcess(
asyCommon.genRsa2048KeyPairSpec(),
"RSA|PSS|SHA224|MGF1_SHA224",
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
32
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifySetAndGetSpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3600
* @tc.name Abnormal situation of parameter mode for signature verification setting
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter 0, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter -1, and call it synchronously
* @tc.desc Pass in exception parameter mode 1024, parameter 2, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter 65535, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, parameter "string",
* and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3600",
0,
async function (done) {
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA224"
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1300
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC256|SHA256"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1300",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC256|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
0
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
-1
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(1024, 2);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
65535
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
"string"
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1400
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPair structure with a signature verification type of "ECC|SHA224"(No length)
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1400",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3700
* @tc.name Abnormal situation of parameter mode for signature verification getting
* @tc.desc Pass in exception parameter mode 10000, and call it synchronously
* @tc.desc Call it as a synchronously without passing in parameters
* @tc.desc Pass in exception parameter mode 6, and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3700",
0,
async function (done) {
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA224"
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1500
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC224|SHA256"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1500",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC224|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
try {
verifyGenerator.getVerifySpec(10000);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3700 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1600
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC384|SHA224"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1600",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC384|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
try {
verifyGenerator.getVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3700 2 err" +
err
);
expect(err.code == 401).assertTrue();
}
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1700
* @tc.name Use createAsyKeyGeneratorBySpec to Create ECC asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the ECCKeyPar structure with a signature verification type of "ECC521|SHA256"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1700",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genEccCommonSpec(),
"ECC521|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
try {
verifyGenerator.getVerifySpec(6);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3700 4 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1800
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA|SHA1"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1800",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA|SHA1"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3800
* @tc.name Signature verification and corresponding algorithm for obtaining signatures
* @tc.desc Pass in algorithm name "RSA1024|PKCS1|SHA512" and call it using the promise method
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3800",
0,
async function (done) {
try {
let signGenerator = cryptoFramework.createSign(
"RSA1024|PKCS1|SHA512"
);
expect(signGenerator.algName == "RSA1024|PKCS1|SHA512").assertTrue();
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA512"
);
expect(
verifyGenerator.algName == "RSA1024|PKCS1|SHA512"
).assertTrue();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3800 catch err: " +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
}
);
});
}
......@@ -13,714 +13,617 @@
* limitations under the License.
*/
import {
describe,
beforeAll,
afterEach,
it,
expect,
afterAll,
} from "@ohos/hypium";
import { describe, beforeAll, afterEach, it, expect, afterAll, } from "@ohos/hypium";
import * as asyPromise from "./utils/asymmetric/publicAsymmetricPromise";
import * as asyCallback from "./utils/asymmetric/publicAsymmetricCallback";
import * as asyCommon from "./utils/asymmetric/publicAsymmetricCommon";
import cryptoFramework from "@ohos.security.cryptoFramework";
import {
stringTouInt8Array,
uInt8ArrayToShowStr,
} from "./utils/common/publicDoString";
import { stringTouInt8Array, uInt8ArrayToShowStr, } from "./utils/common/publicDoString";
export default function SecuritySignatureVerificationSM2Jsunit() {
describe("SecuritySignatureVerificationSM2Jsunit", function () {
console.log(
"##########start SecuritySignatureVerificationSM2Jsunit##########"
);
var asyKeyPair;
beforeAll(async function () {
async function genAsyKeyPair() {
try {
let asyGenerator = asyCommon.createAsyKeyGenerator("SM2_256");
let aysKeyPair = await asyGenerator.generateKeyPair();
return aysKeyPair;
} catch (err) {
return err;
}
}
asyKeyPair = await genAsyKeyPair();
});
afterAll(function () {});
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0100
* @tc.name Signature and verification scenario testing
* @tc.desc the asyAlgoName is "SM2_256|SM3", Use the Callback Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0100",
0,
async function (done) {
await asyCallback
.signAndVerifyNormalProcess("SM2_256", "SM2_256|SM3")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0200
* @tc.name Create signature compatibility test
* @tc.desc the asyAlgoName is "RSA|SM3", Use the async Style of Interface
* @tc.desc the asyAlgoName is "ECC|SM3", Use the async Style of Interface
* @tc.desc the asyAlgoName is "DSA|SM3", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0200",
0,
async function (done) {
try {
await cryptoFramework.createSign("RSA|SM3");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 1 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("ECC|SM3");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 2 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("DSA|SM3");
} catch (err) {
console.error("signGenerator 3 error:" + err);
expect(err.code == undefined).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0300
* @tc.name Create signature compatibility test
* @tc.desc the asyAlgoName is "SM2_256|NoHash", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|MD5", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA1", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA224", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA384", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA512", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0300",
0,
async function (done) {
try {
cryptoFramework.createSign("SM2_256|NoHash");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|MD5");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|SHA256");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|SHA1");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|SHA224");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("SM2_256|SHA384");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("SM2_256|SHA512");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0400
* @tc.name Create verification compatibility test
* @tc.desc the asyAlgoName is "SM2_256|NoHash", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|MD5", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA1", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA224", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA384", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA512", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0400",
0,
async function (done) {
try {
cryptoFramework.createVerify("SM2_256|NoHash");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|MD5");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA256");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA1");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA224");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA384");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA512");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0500
* @tc.name Create Key Generator Test
* @tc.desc the asyAlgoName is "SM2_2567", Use the async Style of Interface
* @tc.desc the asyAlgoName is null, Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0500",
0,
async function (done) {
try {
cryptoFramework.createAsyKeyGenerator("SM2_2567");
expect(null).assertFail();
} catch (err) {
console.error("createAsyKeyGenerator 1 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createAsyKeyGenerator(null);
expect(null).assertFail();
} catch (err) {
console.error("createAsyKeyGenerator 2 error:" + err);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0600
* @tc.name Create signature failed
* @tc.desc the asyAlgoName is "SM2_256|SHA257", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|NULL", Use the async Style of Interface
* @tc.desc the asyAlgoName is "NULL|SHA1", Use the async Style of Interface
* @tc.desc the asyAlgoName is "ECC|SHA", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0600",
0,
async function (done) {
try {
cryptoFramework.createSign("SM2_256|SHA257");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 1 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|NULL");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 2 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("NULL|SHA1");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 3 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("ECC|SHA");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 4 error:" + err);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0700
* @tc.name Exception while initializing rsa signature object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter SM2_256 and call them as async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0700",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256");
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
let signKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
signKeyPair = keyPair;
resolve();
describe("SecuritySignatureVerificationSM2Jsunit", function () {
console.log(
"##########start SecuritySignatureVerificationSM2Jsunit##########"
);
var asyKeyPair;
beforeAll(async function () {
async function genAsyKeyPair() {
try {
let asyGenerator = asyCommon.createAsyKeyGenerator("SM2_256");
let aysKeyPair = await asyGenerator.generateKeyPair();
return aysKeyPair;
} catch (err) {
return err;
}
}
});
});
try {
await signGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.init(signKeyPair.priKey, signKeyPair.priKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0800
* @tc.name Exception in adding signature data to rsa objects
* @tc.desc The signature object was not initialized, and the data to be signed "This is a sign test"
* was passed in as a promise to be called
* @tc.desc Call it as a promise without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a promise
* @tc.desc Pass in two exception parameters of "This is a sign test" and "This is a sign test"
* and call it promise
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0800",
0,
async function (done) {
let globalText = "This is a sign test";
let input = { data: stringTouInt8Array(globalText) };
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
try {
await signGenerator.update(input);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
await signGenerator.init(asyKeyPair.priKey);
try {
await signGenerator.update(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.update(input, input);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0900
* @tc.name Signature data anomaly
* @tc.descPass in an exception parameter null and call it as a callback
* @tc.desc Pass in an exception parameter "" and call it as a callback
* @tc.desc Pass in two exception parameters "This is a sign test" and "This is a sign test"
* and call it as a callback
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0900",
0,
async function (done) {
let globalText = "This is a sign test";
let input = { data: stringTouInt8Array(globalText) };
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
await signGenerator.init(asyKeyPair.priKey);
try {
await signGenerator.sign(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.sign("");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.sign(input, input);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1000
* @tc.name Create verification failed
* @tc.desc the asyAlgoName is "SM2_2567|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "NULL|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA122", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|NULL", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1000",
0,
async function (done) {
try {
cryptoFramework.createVerify("SM2_2567|SHA256");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 1 error:" + err.code);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("NULL|SHA256");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 2 error:" + err.code);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA122");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 3 error:" + err.code);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|NULL");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 4 error:" + err.code);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1100
* @tc.name Abnormal situation of initialization verification object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter SM2_256 and call them as async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1100",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256");
let verifyGenerator = cryptoFramework.createVerify("SM2_256|SM3");
let verifyKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
verifyKeyPair = keyPair;
resolve();
}
});
asyKeyPair = await genAsyKeyPair();
});
afterAll(function () {
});
try {
await verifyGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await verifyGenerator.init(
verifyKeyPair.pubKey,
verifyKeyPair.pubKey
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await verifyGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1200
* @tc.name Abnormal situation of additional signature verification data
* @tc.desc Call it as a promise without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a promise
* @tc.desc Pass in two exception parameters "This is a sign test" and "This is a sign test"
* and call them as promise
* @tc.desc The verify object was not initialized, and the data to be signed "This is a sign test"
* was passed in as a promise to be called
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1200",
0,
async function (done) {
await asyPromise
.verifyUpdateAbnormalParameterSM2Process("SM2_256", "SM2_256|SM3", 0)
.then((data) => {
expect(null).assertFail();
})
.catch((err) => {
expect(err.code == 401).assertTrue();
});
await asyPromise
.verifyUpdateAbnormalParameterSM2Process("SM2_256", "SM2_256|SM3", 1)
.then((data) => {
expect(null).assertFail();
})
.catch((err) => {
expect(err.code == 401).assertTrue();
});
await asyPromise
.verifyUpdateAbnormalParameterSM2Process("SM2_256", "SM2_256|SM3", 3)
.then((data) => {
expect(null).assertFail();
})
.catch((err) => {
expect(err.code == 401).assertTrue();
});
await asyPromise
.verifyUpdateAbnormalParameterSM2Process("SM2_256", "SM2_256|SM3", 4)
.then((data) => {
expect(null).assertFail();
})
.catch((err) => {
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1300
* @tc.name Abnormal situation of visa verification signature data
* @tc.desc Call it as a callback without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a callback
* @tc.desc Pass in an exception parameter "This is a sign test" and "This is a sign test"
* call it in the form of a callback
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1300",
0,
async function (done) {
await asyCallback
.verifyAbnormalParameterSM2Process("SM2_256", "SM2_256|SM3", 0)
.then((data) => {
expect(null).assertFail();
})
.catch((err) => {
expect(err.code == 401).assertTrue();
});
await asyCallback
.verifyAbnormalParameterSM2Process("SM2_256", "SM2_256|SM3", 1)
.then((data) => {
expect(null).assertFail();
})
.catch((err) => {
expect(err.code == 401).assertTrue();
});
await asyCallback
.verifyAbnormalParameterSM2Process("SM2_256", "SM2_256|SM3", 3)
.then((data) => {
expect(null).assertFail();
})
.catch((err) => {
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1400
* @tc.name Test signature verification and pass in ultra long parameters
* @tc.desc the asyAlgoName is "SM2_256|SM3", Use the async Style of Interface
* @tc.desc the asyAlgoName is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz" * 350,
* Use the Promise Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1400",
0,
async function (done) {
var globalSignBlob;
var globalText;
try {
let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz";
for (let i = 0; i < 350; i++) {
globalText += t.charAt(Math.floor(Math.random() * t.length));
}
let input = { data: stringTouInt8Array(globalText) };
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256");
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
let verifyGenerator = cryptoFramework.createVerify("SM2_256|SM3");
let rsaKeyPair = await rsaGenerator.generateKeyPair();
await signGenerator.init(rsaKeyPair.priKey);
await signGenerator.update(input);
globalSignBlob = await signGenerator.sign(input);
await verifyGenerator.init(rsaKeyPair.pubKey);
await verifyGenerator.update(input);
let finalStatus = await verifyGenerator.verify(input, globalSignBlob);
expect(finalStatus).assertTrue();
} catch (err) {
expect(null).assertFail();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1500
* @tc.name Key negotiation test
* @tc.desc the asyAlgoName is "SM2_256", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1500",
0,
async function (done) {
try {
cryptoFramework.createAsyKeyGenerator("SM2_256");
cryptoFramework.createKeyAgreement("SM2_256");
expect(null).assertFail();
} catch (err) {
console.error("createAsyKeyAgreement error:" + err.code);
expect(err.code == 801).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1600
* @tc.name Signature verification and corresponding algorithm for obtaining signatures
* @tc.desc Pass in algorithm name "SM2_256|SM3" and call it using the async method
* @tc.desc Pass in algorithm name "SM2_256|SM3" and call it using the async method
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1600",
0,
async function (done) {
try {
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
expect(signGenerator.algName == "SM2_256|SM3").assertTrue();
let verifyGenerator = cryptoFramework.createVerify("SM2_256|SM3");
expect(verifyGenerator.algName == "SM2_256|SM3").assertTrue();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1600 catch err: " +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
});
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0100
* @tc.name Signature and verification scenario testing
* @tc.desc the asyAlgoName is "SM2_256|SM3", Use the Callback Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0100",
0,
async function (done) {
await asyCallback
.signAndVerifyNormalProcess("SM2_256", "SM2_256|SM3")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0100 catch error: " +
err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0200
* @tc.name Create signature compatibility test
* @tc.desc the asyAlgoName is "RSA|SM3", Use the async Style of Interface
* @tc.desc the asyAlgoName is "ECC|SM3", Use the async Style of Interface
* @tc.desc the asyAlgoName is "DSA|SM3", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0200",
0,
async function (done) {
try {
await cryptoFramework.createSign("RSA|SM3");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 1 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("ECC|SM3");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 2 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("DSA|SM3");
} catch (err) {
console.error("signGenerator 3 error:" + err);
expect(err.code == undefined).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0300
* @tc.name Create signature compatibility test
* @tc.desc the asyAlgoName is "SM2_256|NoHash", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|MD5", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA1", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA224", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA384", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA512", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0300",
0,
async function (done) {
try {
cryptoFramework.createSign("SM2_256|NoHash");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|MD5");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|SHA256");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|SHA1");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|SHA224");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("SM2_256|SHA384");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await cryptoFramework.createSign("SM2_256|SHA512");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0400
* @tc.name Create verification compatibility test
* @tc.desc the asyAlgoName is "SM2_256|NoHash", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|MD5", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA1", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA224", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA384", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA512", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0400",
0,
async function (done) {
try {
cryptoFramework.createVerify("SM2_256|NoHash");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|MD5");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA256");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA1");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA224");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA384");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA512");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0500
* @tc.name Create Key Generator Test
* @tc.desc the asyAlgoName is "SM2_2567", Use the async Style of Interface
* @tc.desc the asyAlgoName is null, Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0500",
0,
async function (done) {
try {
cryptoFramework.createAsyKeyGenerator("SM2_2567");
expect(null).assertFail();
} catch (err) {
console.error("createAsyKeyGenerator 1 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createAsyKeyGenerator(null);
expect(null).assertFail();
} catch (err) {
console.error("createAsyKeyGenerator 2 error:" + err);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0600
* @tc.name Create signature failed
* @tc.desc the asyAlgoName is "SM2_256|SHA257", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|NULL", Use the async Style of Interface
* @tc.desc the asyAlgoName is "NULL|SHA1", Use the async Style of Interface
* @tc.desc the asyAlgoName is "ECC|SHA", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0600",
0,
async function (done) {
try {
cryptoFramework.createSign("SM2_256|SHA257");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 1 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("SM2_256|NULL");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 2 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("NULL|SHA1");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 3 error:" + err);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign("ECC|SHA");
expect(null).assertFail();
} catch (err) {
console.error("signGenerator 4 error:" + err);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0700
* @tc.name Exception while initializing rsa signature object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter SM2_256 and call them as async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0700",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256");
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
let signKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
signKeyPair = keyPair;
resolve();
}
});
});
try {
await signGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.init(signKeyPair.priKey, signKeyPair.priKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0800
* @tc.name Exception in adding signature data to rsa objects
* @tc.desc The signature object was not initialized, and the data to be signed "This is a sign test"
* was passed in as a promise to be called
* @tc.desc Call it as a promise without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a promise
* @tc.desc Pass in two exception parameters of "This is a sign test" and "This is a sign test"
* and call it promise
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0800",
0,
async function (done) {
let globalText = "This is a sign test";
let input = {
data: stringTouInt8Array(globalText)
};
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
try {
await signGenerator.update(input);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
await signGenerator.init(asyKeyPair.priKey);
try {
await signGenerator.update(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.update(input, input);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_0900
* @tc.name Signature data anomaly
* @tc.descPass in an exception parameter null and call it as a callback
* @tc.desc Pass in an exception parameter "" and call it as a callback
* @tc.desc Pass in two exception parameters "This is a sign test" and "This is a sign test"
* and call it as a callback
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_0900",
0,
async function (done) {
let globalText = "This is a sign test";
let input = {
data: stringTouInt8Array(globalText)
};
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
await signGenerator.init(asyKeyPair.priKey);
try {
await signGenerator.sign(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.sign("");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await signGenerator.sign(input, input);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1000
* @tc.name Create verification failed
* @tc.desc the asyAlgoName is "SM2_2567|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "NULL|SHA256", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|SHA122", Use the async Style of Interface
* @tc.desc the asyAlgoName is "SM2_256|NULL", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1000",
0,
async function (done) {
try {
cryptoFramework.createVerify("SM2_2567|SHA256");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 1 error:" + err.code);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("NULL|SHA256");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 2 error:" + err.code);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|SHA122");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 3 error:" + err.code);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("SM2_256|NULL");
expect(null).assertFail();
} catch (err) {
console.error("verifyGenerator 4 error:" + err.code);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1100
* @tc.name Abnormal situation of initialization verification object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter SM2_256 and call them as async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1100",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256");
let verifyGenerator = cryptoFramework.createVerify("SM2_256|SM3");
let verifyKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
verifyKeyPair = keyPair;
resolve();
}
});
});
try {
await verifyGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await verifyGenerator.init(
verifyKeyPair.pubKey,
verifyKeyPair.pubKey
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
await verifyGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1400
* @tc.name Test signature verification and pass in ultra long parameters
* @tc.desc the asyAlgoName is "SM2_256|SM3", Use the async Style of Interface
* @tc.desc the asyAlgoName is "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz" * 350,
* Use the Promise Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1400",
0,
async function (done) {
var globalSignBlob;
var globalText;
try {
let t = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefhijklmnopqrstuvwxyz";
for (let i = 0; i < 350; i++) {
globalText += t.charAt(Math.floor(Math.random() * t.length));
}
let input = {
data: stringTouInt8Array(globalText)
};
let rsaGenerator = cryptoFramework.createAsyKeyGenerator("SM2_256");
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
let verifyGenerator = cryptoFramework.createVerify("SM2_256|SM3");
let rsaKeyPair = await rsaGenerator.generateKeyPair();
await signGenerator.init(rsaKeyPair.priKey);
await signGenerator.update(input);
globalSignBlob = await signGenerator.sign(input);
await verifyGenerator.init(rsaKeyPair.pubKey);
await verifyGenerator.update(input);
let finalStatus = await verifyGenerator.verify(input, globalSignBlob);
expect(finalStatus).assertTrue();
} catch (err) {
expect(null).assertFail();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1500
* @tc.name Key negotiation test
* @tc.desc the asyAlgoName is "SM2_256", Use the async Style of Interface
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1500",
0,
async function (done) {
try {
cryptoFramework.createAsyKeyGenerator("SM2_256");
cryptoFramework.createKeyAgreement("SM2_256");
expect(null).assertFail();
} catch (err) {
console.error("createAsyKeyAgreement error:" + err.code);
expect(err.code == 801).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerificationSM2_Func_1600
* @tc.name Signature verification and corresponding algorithm for obtaining signatures
* @tc.desc Pass in algorithm name "SM2_256|SM3" and call it using the async method
* @tc.desc Pass in algorithm name "SM2_256|SM3" and call it using the async method
*/
it(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1600",
0,
async function (done) {
try {
let signGenerator = cryptoFramework.createSign("SM2_256|SM3");
expect(signGenerator.algName == "SM2_256|SM3").assertTrue();
let verifyGenerator = cryptoFramework.createVerify("SM2_256|SM3");
expect(verifyGenerator.algName == "SM2_256|SM3").assertTrue();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerificationSM2_Func_1600 catch err: " +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
});
}
/*
* Copyright (C) 2022-2023 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 * as asyPromise from "./utils/asymmetric/publicAsymmetricPromise";
import * as asyCommon from "./utils/common/publicDoSpec";
import * as asyCallback from "./utils/asymmetric/publicAsymmetricCallback";
import cryptoFramework from "@ohos.security.cryptoFramework";
export default function SecuritySignatureVerificationTwoJsunit() {
describe("SecuritySignatureVerificationTwoJsunit", function () {
console.log(
"##########start SecuritySignatureVerificationJsunit##########"
);
beforeAll(function () {
});
afterEach(function () {
});
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_1900
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA|SHA224"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_1900",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2000
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA1024|SHA224"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2000",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA1024|SHA224"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2100
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA2048|SHA256"
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2100",
0,
async function (done) {
await asyPromise
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA2048|SHA256"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2200
* @tc.name Use createAsyKeyGenerator to Create DSA asyKeySpec
* and Signature verification combination scenario testing
* @tc.desc Pass in the DSAKeyPar structure with a signature verification type of "DSA3072|SHA384"
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2200",
0,
async function (done) {
await asyCallback
.signAndVerifyBySpecProcess(
asyCommon.genDsa2048CommonSpecBigE(),
"DSA3072|SHA384"
)
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2300
* @tc.name Scenario testing of signature verification and key generation and conversion combination
* @tc.desc Pass in key generation parameter "DSA1024|PRIMES_2",
* signature verification parameter "DSA1024|SHA1",
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2300",
0,
async function (done) {
await asyPromise
.signAndVerifyNormalProcess("DSA1024", "DSA1024|SHA1")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AyPromise signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2400
* @tc.name Scenario testing of signature verification and key generation and conversion combination
* @tc.desc Pass in key generation parameter "DSA2048|PRIMES_2",
* signature verification parameter "DSA2048|SHA256",
* and use a callback style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2400",
0,
async function (done) {
await asyCallback
.signAndVerifyNormalProcess("DSA2048", "DSA2048|SHA256")
.then((result) => {
expect(result == true).assertTrue();
})
.catch((err) => {
console.error(
"AsyCallback signAndVerifyBySpecProcess catch error: " + err
);
expect(null).assertFail();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2500
* @tc.name Scenario testing of signature verification and key generation and conversion combination
* @tc.desc Pass in key generation parameter "DSA3072|PRIMES_2",
* signature verification parameter "DSA3072|SHA384",
* and use a promise style interface
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2500",
0,
async function (done) {
await asyPromise
.signAndVerifyNormalProcess("DSA3072", "DSA3072|SHA384")
.then((result) => {
expect(result).assertTrue();
})
.catch((err) => {
console.error(
"AsyPromise signAndVerifyNormalProcess catch error: " + err
);
expect(err.code == 401).assertTrue();
});
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2600
* @tc.name Exception in creating signature object
* @tc.desc Pass in an exception parameter "RSA10000|PKCS1|SHA224" and call it as a synchronously
* @tc.desc Pass in an exception parameter null and call it as a synchronously
* @tc.desc Pass in an exception parameter "RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5" and
* call it as a synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2600",
0,
async function (done) {
try {
cryptoFramework.createSign("RSA10000|PKCS1|SHA224");
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_2600 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign(null);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_2600 2 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createSign(
"RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5"
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_2600 3 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2700
* @tc.name Exception while initializing rsa signature object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter rsa and call them as async
* @tc.desc Pass in the public key generated by the exception parameter dsa and call it as a async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2700",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genRsa2048KeyPairSpec()
);
let signGenerator = cryptoFramework.createSign("RSA1024|PKCS1|SHA224");
let signKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
signKeyPair = keyPair;
resolve();
}
});
});
try {
signGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
signGenerator.init(signKeyPair.priKey, signKeyPair.priKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
let dsaGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genDsa2048KeyPairSpecBigE()
);
let dsaKeyPair;
await new Promise((resolve, reject) => {
dsaGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
dsaKeyPair = keyPair;
resolve();
}
});
});
try {
await signGenerator.init(dsaKeyPair.pubKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
signGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2800
* @tc.name Exception in adding signature data to rsa objects
* @tc.desc Call it as a promise without passing in parameters
* @tc.desc Pass in two exception parameters "This is a sign test" and "This is a sign test"
* and call them as promise
* @tc.desc Pass in an exception parameter null and call it as a promise
* @tc.desc Pass in an exception parameter of "This is a sign test" multiplied by 1000 times and call it promise
* @tc.desc The signature object was not initialized, and the data to be signed "This is a sign test"
* was passed in as a promise to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2800",
0,
async function (done) {
try {
await asyPromise.updateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 1);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyPromise.updateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 2);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyPromise.updateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 3);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyPromise.updateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 4);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_2900
* @tc.name Signature data anomaly
* @tc.desc Pass in an exception parameter "This is a sign test" and "This is a sign test"
* call it in the form of a callback
* @tc.descPass in an exception parameter null and call it as a callback
* @tc.desc Pass in an exception parameter "" and call it as a callback
* @tc.desc The signature object was not initialized, and the data to be signed 'This is a sign test'
* was passed in as a callback to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_2900",
0,
async function (done) {
try {
await asyCallback.signAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 1);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyCallback.signAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 2);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyCallback.signAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 3);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3000
* @tc.name Exception in signature setting parameter mode
* @tc.desc Pass in exception parameter mode 1024, parameter 2, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter 4294967296, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, parameter "string",
* and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3000",
0,
async function (done) {
let signGenerator = cryptoFramework.createSign(
"RSA1024|PSS|SHA224|MGF1_SHA224"
);
try {
signGenerator.setSignSpec(1024, 2);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
signGenerator.setSignSpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
4294967296
);
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 2 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
signGenerator.setSignSpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
"string"
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 5 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_crypto_framework_ASymmetric_Encryption_RSA_3100
* @tc.name Exception in signature getting parameter mode
* @tc.desc Pass in exception parameter mode 10000, and call it synchronously
* @tc.desc Pass in exception parameter mode 6, and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3100",
0,
async function (done) {
let signGenerator = cryptoFramework.createSign(
"RSA1024|PSS|SHA224|MGF1_SHA224"
);
try {
signGenerator.getSignSpec(10000);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
signGenerator.getSignSpec(6);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3100 4 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3200
* @tc.name Abnormal situation of creating verification objects
* @tc.desc Pass in an exception parameter "RSA10000|SHA224" and call it as a synchronously
* @tc.desc Pass in an exception parameter null and call it as a synchronously
* @tc.desc Pass in an exception parameter "RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5" and
* call it as a synchronously
* @tc.desc Pass in an exception parameter "DSA512|PKCS1|MD5" and call it as a synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3200",
0,
async function (done) {
try {
cryptoFramework.createVerify("RSA10000|SHA224");
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 1 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify(null);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 2 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify(
"RSA1024PKCS1MD5RSA1024PKCS1MD5RSA1024PKCS1MD5"
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 3 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
cryptoFramework.createVerify("DSA512|PKCS1|MD5");
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3200 5 catch err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3300
* @tc.name Abnormal situation of initialization verification object
* @tc.desc Pass in an exception parameter null and call it as a async
* @tc.desc Pass in the two public keys generated by the exception parameter rsa and call them as async
* @tc.desc Pass in the public key generated by the exception parameter dsa and call it as a async
* @tc.desc Pass in an exception parameter "sroundpriKey" and call it as a async
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3300",
0,
async function (done) {
let specGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genRsa2048KeyPairSpec()
);
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA224"
);
let verifyKeyPair;
await new Promise((resolve, reject) => {
specGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
verifyKeyPair = keyPair;
resolve();
}
});
});
try {
verifyGenerator.init(null);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.init(verifyKeyPair.pubKey, verifyKeyPair.pubKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
let dsaGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(
asyCommon.genDsa2048KeyPairSpecBigE()
);
let dsaKeyPair;
await new Promise((resolve, reject) => {
dsaGenerator.generateKeyPair((err, keyPair) => {
if (err) {
reject(err);
} else {
dsaKeyPair = keyPair;
resolve();
}
});
});
try {
await verifyGenerator.init(dsaKeyPair.pubKey);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.init("sroundpriKey");
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3400
* @tc.name Abnormal situation of additional signature verification data
* @tc.desc Call it as a promise without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a promise
* @tc.desc Pass in an exception parameter "" and call it as a promise
* @tc.desc Pass in two exception parameters "This is a sign test" and "This is a sign test"
* and call them as promise
* @tc.desc The verify object was not initialized, and the data to be signed "This is a sign test"
* was passed in as a promise to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3400",
0,
async function (done) {
try {
await asyPromise.verifyUpdateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 1);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyPromise.verifyUpdateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 2);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyPromise.verifyUpdateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 3);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyPromise.verifyUpdateAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 4);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3500
* @tc.name Abnormal situation of visa verification signature data
* @tc.desc Call it as a callback without passing in parameters
* @tc.desc Pass in an exception parameter null and call it as a callback
* @tc.desc Pass in an exception parameter "" and call it as a callback
* @tc.desc Pass in an exception parameter "This is a sign test" and "This is a sign test"
* call it in the form of a callback
* @tc.desc The verify object was not initialized, and the data to be signed 'This is a sign test'
* was passed in as a callback to be called
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3500",
0,
async function (done) {
try {
let status = await asyCallback.verifyAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 1);
expect(status).assertFalse();
} catch (err) {
expect(null).assertFail();
}
try {
await asyCallback.verifyAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 2);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
await asyCallback.verifyAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 3);
expect(null).assertFail();
} catch (err) {
expect(err.code).assertEqual(401);
}
try {
let status = await asyCallback.verifyAbnormalParameterProcess(asyCommon.genRsa2048KeyPairSpec(), "RSA1024|PKCS1|SHA224", 4);
expect(status).assertFalse();
} catch (err) {
expect(null).assertFail();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3600
* @tc.name Abnormal situation of parameter mode for signature verification setting
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter 0, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter -1, and call it synchronously
* @tc.desc Pass in exception parameter mode 1024, parameter 2, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
* parameter 65535, and call it synchronously
* @tc.desc Pass in exception parameter mode cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM, parameter "string",
* and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3600",
0,
async function (done) {
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA224"
);
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
0
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
-1
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(1024, 2);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
65535
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.setVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM,
"string"
);
expect(null).assertFail();
} catch (err) {
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3700
* @tc.name Abnormal situation of parameter mode for signature verification getting
* @tc.desc Pass in exception parameter mode 10000, and call it synchronously
* @tc.desc Call it as a synchronously without passing in parameters
* @tc.desc Pass in exception parameter mode 6, and call it synchronously
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3700",
0,
async function (done) {
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA224"
);
try {
verifyGenerator.getVerifySpec(10000);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3700 1 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.getVerifySpec(
cryptoFramework.SignSpecItem.PSS_SALT_LEN_NUM
);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3700 2 err" +
err
);
expect(err.code == 401).assertTrue();
}
try {
verifyGenerator.getVerifySpec(6);
expect(null).assertFail();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3700 4 err" +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
/**
* @tc.number Security_CryptoFramework_SignatureVerification_Func_3800
* @tc.name Signature verification and corresponding algorithm for obtaining signatures
* @tc.desc Pass in algorithm name "RSA1024|PKCS1|SHA512" and call it using the promise method
*/
it(
"Security_CryptoFramework_SignatureVerification_Func_3800",
0,
async function (done) {
try {
let signGenerator = cryptoFramework.createSign(
"RSA1024|PKCS1|SHA512"
);
expect(signGenerator.algName == "RSA1024|PKCS1|SHA512").assertTrue();
let verifyGenerator = cryptoFramework.createVerify(
"RSA1024|PKCS1|SHA512"
);
expect(
verifyGenerator.algName == "RSA1024|PKCS1|SHA512"
).assertTrue();
} catch (err) {
console.log(
"Security_CryptoFramework_SignatureVerification_Func_3800 catch err: " +
err
);
expect(err.code == 401).assertTrue();
}
done();
}
);
}
);
}
......@@ -16,863 +16,704 @@
import { expect } from "@ohos/hypium";
import * as asyCommon from "../common/publicDoSpec";
import cryptoFramework from "@ohos.security.cryptoFramework";
import { stringTouInt8Array, uInt8ArrayToShowStr, uInt8ArrayToString, } from "../common/publicDoString";
import {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "../common/publicDoString";
import {
createAsyKeyGenerator,
createAsyKeyGeneratorBySpec,
createAsyCipher,
createAsySign,
createAsyVerify,
createAsyKeyGenerator,
createAsyKeyGeneratorBySpec,
createAsyCipher,
createAsySign,
createAsyVerify,
} from "./publicAsymmetricCommon";
async function generateAsyKeyPair(rsaGenerator) {
var pubKey;
var priKey;
return new Promise((resolve, reject) => {
rsaGenerator.generateKeyPair((err, rsaKeyPair) => {
if (err) {
console.error("[Callback]generateSymKey failed. error is " + err);
reject(err);
} else {
pubKey = rsaKeyPair.pubKey;
let encodedPubKey = pubKey.getEncoded();
console.log("[Callback]: pubKey.getAlgorithm= " + pubKey.algName);
console.log("[Callback]: pubKey.getEncoded= " + encodedPubKey.data);
console.log("[Callback]: pubKey.getFormat= " + pubKey.format);
priKey = rsaKeyPair.priKey;
let encodedPriKey = priKey.getEncoded();
console.log("[Callback]: priKey.getAlgorithm= " + priKey.algName);
console.log("[Callback]: priKey.getEncoded= " + encodedPriKey.data);
console.log("[Callback]: priKey.getFormat= " + priKey.format);
resolve(rsaKeyPair);
}
var pubKey;
var priKey;
return new Promise((resolve, reject) => {
rsaGenerator.generateKeyPair((err, rsaKeyPair) => {
if (err) {
console.error("[Callback]generateSymKey failed. error is " + err);
reject(err);
} else {
pubKey = rsaKeyPair.pubKey;
let encodedPubKey = pubKey.getEncoded();
console.log("[Callback]: pubKey.getAlgorithm= " + pubKey.algName);
console.log("[Callback]: pubKey.getEncoded= " + encodedPubKey.data);
console.log("[Callback]: pubKey.getFormat= " + pubKey.format);
priKey = rsaKeyPair.priKey;
let encodedPriKey = priKey.getEncoded();
console.log("[Callback]: priKey.getAlgorithm= " + priKey.algName);
console.log("[Callback]: priKey.getEncoded= " + encodedPriKey.data);
console.log("[Callback]: priKey.getFormat= " + priKey.format);
resolve(rsaKeyPair);
}
});
});
});
}
async function convertAsyKey(rsaGenerator, pubKeyDataBlob, priKeyDataBlob) {
return new Promise((resolve, reject) => {
rsaGenerator.convertKey(
pubKeyDataBlob,
priKeyDataBlob,
(err, convertKeyPair) => {
if (err) {
console.error("[Callback]convertKey failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]convertKey success. convertKeyPair is " + convertKeyPair
);
resolve(convertKeyPair);
}
}
);
});
return new Promise((resolve, reject) => {
rsaGenerator.convertKey(
pubKeyDataBlob,
priKeyDataBlob,
(err, convertKeyPair) => {
if (err) {
console.error("[Callback]convertKey failed. error is " + err);
reject(err);
} else {
console.log(
"[Callback]convertKey success. convertKeyPair is " + convertKeyPair
);
resolve(convertKeyPair);
}
}
);
});
}
async function initCipher(cipherGenerator, mode, key, params) {
return new Promise((resolve, reject) => {
cipherGenerator.init(mode, key, params, (err) => {
if (err) {
console.error(
"[Callback]cipherGenerator init failed. error is " +
err +
"mode is " +
mode
);
reject(err);
} else {
console.log(
"[Callback]cipherGenerator init success! mode is : " + mode
);
resolve("init success");
}
return new Promise((resolve, reject) => {
cipherGenerator.init(mode, key, params, (err) => {
if (err) {
console.error(
"[Callback]cipherGenerator init failed. error is " +
err +
"mode is " +
mode
);
reject(err);
} else {
console.log(
"[Callback]cipherGenerator init success! mode is : " + mode
);
resolve("init success");
}
});
});
});
}
async function doFinalCipher(cipherGenerator, mode, dataBlob) {
return new Promise((resolve, reject) => {
cipherGenerator.doFinal(dataBlob, (err, finalData) => {
if (err) {
console.error(
"[Callback]cipherGenerator doFinal failed. error is " +
err +
"mode is " +
mode
);
reject(err);
} else {
console.log(
"[Callback]cipherGenerator doFinal success! mode is : " + mode
);
resolve(finalData);
}
return new Promise((resolve, reject) => {
cipherGenerator.doFinal(dataBlob, (err, finalData) => {
if (err) {
console.error(
"[Callback]cipherGenerator doFinal failed. error is " +
err +
"mode is " +
mode
);
reject(err);
} else {
console.log(
"[Callback]cipherGenerator doFinal success! mode is : " + mode
);
resolve(finalData);
}
});
});
});
}
async function initSign(signGenerator, priKey) {
return new Promise((resolve, reject) => {
signGenerator.init(priKey, (err) => {
if (err) {
console.error("[Callback]signGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator init success!");
resolve("init success");
}
return new Promise((resolve, reject) => {
signGenerator.init(priKey, (err) => {
if (err) {
console.error("[Callback]signGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator init success!");
resolve("init success");
}
});
});
});
}
async function updateSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]signGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator update success!");
resolve("update success");
}
return new Promise((resolve, reject) => {
signGenerator.update(dataBlob, (err) => {
if (err) {
console.error("[Callback]signGenerator update failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator update success!");
resolve("update success");
}
});
});
});
}
async function signForSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator.sign(dataBlob, (err, signOutput) => {
if (err) {
console.error("[Callback]signGenerator sign failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator sign success!");
resolve(signOutput);
}
});
});
}
async function signForSignFailed(signGenerator, dataBlob, itemType) {
return new Promise((resolve, reject) => {
switch (itemType) {
case 0:
signGenerator.sign((err, signOutput) => {
if (err) {
reject(err);
} else {
resolve(signOutput);
}
});
break;
case 1:
signGenerator.sign(null, (err, signOutput) => {
if (err) {
reject(err);
} else {
resolve(signOutput);
}
});
break;
case 2:
signGenerator.sign("", (err, signOutput) => {
if (err) {
reject(err);
} else {
resolve(signOutput);
}
});
break;
case 3:
signGenerator.sign(dataBlob, dataBlob, (err, signOutput) => {
if (err) {
reject(err);
} else {
resolve(signOutput);
}
return new Promise((resolve, reject) => {
signGenerator.sign(dataBlob, (err, signOutput) => {
if (err) {
console.error("[Callback]signGenerator sign failed. error is " + err);
reject(err);
} else {
console.log("[Callback]signGenerator sign success!");
resolve(signOutput);
}
});
break;
default:
reject("err");
break;
}
});
});
}
async function initVerify(verifyGenerator, pubKey) {
return new Promise((resolve, reject) => {
verifyGenerator.init(pubKey, (err) => {
if (err) {
console.error("[Callback]verifyGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]verifyGenerator init success!");
resolve("init success");
}
return new Promise((resolve, reject) => {
verifyGenerator.init(pubKey, (err) => {
if (err) {
console.error("[Callback]verifyGenerator init failed. error is " + err);
reject(err);
} else {
console.log("[Callback]verifyGenerator init success!");
resolve("init success");
}
});
});
});
}
async function updateVerify(verifyGenerator, dataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator.update(dataBlob, (err) => {
if (err) {
console.error(
"[Callback]verifyGenerator update failed. error is " + err
);
reject(err);
} else {
console.log("[Callback]verifyGenerator update success!");
resolve("update success");
}
return new Promise((resolve, reject) => {
verifyGenerator.update(dataBlob, (err) => {
if (err) {
console.error(
"[Callback]verifyGenerator update failed. error is " + err
);
reject(err);
} else {
console.log("[Callback]verifyGenerator update success!");
resolve("update success");
}
});
});
});
}
async function verifyForVerify(verifyGenerator, dataBlob, signDataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator.verify(dataBlob, signDataBlob, (err, verifyOutput) => {
if (err) {
console.error("[Callback]signGenerator sign failed. error is " + err);
reject(err);
} else {
console.error(
"[Callback]signGenerator sign success is " + verifyOutput
);
resolve(verifyOutput);
}
});
});
}
async function verifyForVerifyFailed(
verifyGenerator,
dataBlob,
signDataBlob,
verifyType
) {
return new Promise((resolve, reject) => {
switch (verifyType) {
case 0:
verifyGenerator.verify(dataBlob, (err, verifyOutput) => {
if (err) {
reject(err);
} else {
resolve(verifyOutput);
}
});
break;
case 1:
verifyGenerator.verify(dataBlob, null, (err, verifyOutput) => {
if (err) {
reject(err);
} else {
resolve(verifyOutput);
}
});
break;
case 2:
verifyGenerator.verify(dataBlob, "", (err, verifyOutput) => {
if (err) {
reject(err);
} else {
resolve(verifyOutput);
}
});
break;
case 3:
verifyGenerator.verify(
dataBlob,
signDataBlob,
signDataBlob,
(err, verifyOutput) => {
return new Promise((resolve, reject) => {
verifyGenerator.verify(dataBlob, signDataBlob, (err, verifyOutput) => {
if (err) {
reject(err);
console.error("[Callback]signGenerator sign failed. error is " + err);
reject(err);
} else {
resolve(verifyOutput);
console.error(
"[Callback]signGenerator sign success is " + verifyOutput
);
resolve(verifyOutput);
}
}
);
break;
case 4:
verifyGenerator.verify(dataBlob, signDataBlob, (err, verifyOutput) => {
if (err) {
reject(err);
} else {
resolve(verifyOutput);
}
});
break;
default:
reject("err");
}
});
});
}
async function encryptAndDecryptNormalProcess(asyAlgoName, cipherAlgoName) {
var globalCipherText;
var globalRsaKeyPair;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalRsaKeyPair.pubKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptNormalProcess encryptMode initOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalRsaKeyPair.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptNormalProcess decryptMode initOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
}
resolve(false);
})
.catch((err) => {
console.error(
"[Callback] encryptAndDecryptNormalProcess catch err:" + err
);
reject(err);
});
});
var globalCipherText;
var globalRsaKeyPair;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalRsaKeyPair.pubKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptNormalProcess encryptMode initOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalRsaKeyPair.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptNormalProcess decryptMode initOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
}
resolve(false);
})
.catch((err) => {
console.error(
"[Callback] encryptAndDecryptNormalProcess catch err:" + err
);
reject(err);
});
});
}
async function signAndVerifyNormalProcess(asyAlgoName, signVerifyAlgoName) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var signGenerator = createAsySign(signVerifyAlgoName);
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess initVerifyOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess updateVerifyOut:" +
updateVerifyOut
);
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((verifyOutput) => {
resolve(verifyOutput);
})
.catch((err) => {
reject(err);
});
});
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var signGenerator = createAsySign(signVerifyAlgoName);
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess initVerifyOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[Callback] signAndVerifyNormalProcess updateVerifyOut:" +
updateVerifyOut
);
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((verifyOutput) => {
resolve(verifyOutput);
})
.catch((err) => {
reject(err);
});
});
}
async function signAndVerifySetAndGetSpecProcess(
asyKeySpec,
signVerifyAlgoName,
itemType,
itemValue
asyKeySpec,
signVerifyAlgoName,
itemType,
itemValue
) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
signGenerator.setSignSpec(itemType, itemValue);
verifyGenerator.setVerifySpec(itemType, itemValue);
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess initSignOut:" +
initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess updateSignOut:" +
updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess initVerifyOut:" +
initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess updateVerifyOut:" +
updateVerifyOut
);
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((verifyOutput) => {
let signSpec = signGenerator.getSignSpec(itemType);
let verifySpec = verifyGenerator.getVerifySpec(itemType);
if (signSpec != itemValue || verifySpec != itemValue) {
resolve(false);
}
resolve(verifyOutput);
})
.catch((err) => {
console.error(
"[Callback] signAndVerifySetAndGetSpecProcess catch err:" + err
);
reject(err);
});
});
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
signGenerator.setSignSpec(itemType, itemValue);
verifyGenerator.setVerifySpec(itemType, itemValue);
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess initSignOut:" +
initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess updateSignOut:" +
updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess initVerifyOut:" +
initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[Callback] signAndVerifySetAndGetSpecProcess updateVerifyOut:" +
updateVerifyOut
);
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((verifyOutput) => {
let signSpec = signGenerator.getSignSpec(itemType);
let verifySpec = verifyGenerator.getVerifySpec(itemType);
if (signSpec != itemValue || verifySpec != itemValue) {
resolve(false);
}
resolve(verifyOutput);
})
.catch((err) => {
console.error(
"[Callback] signAndVerifySetAndGetSpecProcess catch err:" + err
);
reject(err);
});
});
}
async function encryptAndDecryptBySpecProcess(asyKeyPairSpec, cipherAlgoName) {
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
let cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDncrypt = createAsyCipher(cipherAlgoName);
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptBySpecProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDncrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptBySpecProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDncrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[Callback] encryptAndDecryptBySpecProcess catch err:" + err
);
reject(err);
});
});
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
let cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDncrypt = createAsyCipher(cipherAlgoName);
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptBySpecProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDncrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptAndDecryptBySpecProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDncrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[Callback] encryptAndDecryptBySpecProcess catch err:" + err
);
reject(err);
});
});
}
async function signAbnormalParameterProcess(
asyKeySpec,
signVerifyAlgoName,
signType
) {
var globalRsaKeyPair;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
if (signType == 3) {
resolve(signForSignFailed(signGenerator, input, signType));
}
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Callback] signAbnormalParameterProcess initSignOut:" + initSignOut
);
resolve(signForSignFailed(signGenerator, input, signType));
})
.catch((err) => {
reject(err);
});
});
}
async function verifyAbnormalParameterProcess(
asyKeySpec,
verifyAlgoName,
verifyType
asyKeySpec,
signVerifyAlgoName,
signType
) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let verifyGenerator = createAsyVerify(verifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
if (verifyType == 4) {
resolve(verifyForVerify(verifyGenerator, input, globalSignBlob));
let globalText = "This is a sign test";
let input = { data: stringTouInt8Array(globalText) };
try {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let keyPair = await specGenerator.generateKeyPair();
switch (signType) {
case 1:
await initSign(signGenerator, keyPair.priKey);
await updateSign(signGenerator, input);
await signForSign(signGenerator, "");
break;
case 2:
await initSign(signGenerator, keyPair.priKey);
await updateSign(signGenerator, input);
await signForSign(signGenerator, 111);
break;
default:
await signForSign(signGenerator, input);
break;
}
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Callback] verifyAbnormalParameterProcess initVerifyOut:" +
initVerifyOut
);
resolve(
verifyForVerifyFailed(
verifyGenerator,
input,
globalSignBlob,
verifyType
)
);
})
.catch((err) => {
reject(err);
});
});
} catch (err) {
console.error("[Callback] signAbnormalParameterProcess catch err: " + err);
throw err;
}
}
async function encryptSetAndGetSpecInitProcess(asyKeyPairSpec, cipherAlgoName) {
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
var pSource = new Uint8Array([1, 2, 3, 4]);
return new Promise((resolve, reject) => {
let cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
cipherGeneratorEncrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
let retP = cipherGeneratorEncrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
if (retP.toString() != pSource.toString()) {
console.log("error init pSource" + retP);
} else {
console.log("pSource changed ==" + retP);
}
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[Callback] encryptSetAndGetSpecInitProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
cipherGeneratorDecrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
cipherGeneratorDecrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptSetAndGetSpecInitProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
var pSource = new Uint8Array([1, 2, 3, 4]);
return new Promise((resolve, reject) => {
let cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
cipherGeneratorEncrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
let retP = cipherGeneratorEncrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
if (retP.toString() != pSource.toString()) {
console.log("error init pSource" + retP);
} else {
resolve(false);
console.log("pSource changed ==" + retP);
}
})
.catch((err) => {
reject(err);
});
});
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[Callback] encryptSetAndGetSpecInitProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
cipherGeneratorDecrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
cipherGeneratorDecrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[Callback] encryptSetAndGetSpecInitProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
reject(err);
});
});
}
async function signAndVerifyBySpecProcess(asyKeySpec, signVerifyAlgoName) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
console.log(
"[callback]: verifyGenerator.algName : " + verifyGenerator.algName
);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Callback] signAndVerifyBySpecProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[Callback] signAndVerifyBySpecProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
console.log(
"[callback] signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data)
);
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
console.log(
"[Callback] signAndVerifyBySpecProcess initVerifyOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[Callback] signAndVerifyBySpecProcess updateVerifyOut:" +
updateVerifyOut
);
let result = verifyForVerify(verifyGenerator, input, globalSignBlob);
console.log("[callback] verifyForVerify result: " + result);
resolve(result);
})
.catch((err) => {
reject(err);
});
});
"[callback]: verifyGenerator.algName : " + verifyGenerator.algName
);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Callback] signAndVerifyBySpecProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[Callback] signAndVerifyBySpecProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
console.log(
"[callback] signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data)
);
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Callback] signAndVerifyBySpecProcess initVerifyOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[Callback] signAndVerifyBySpecProcess updateVerifyOut:" +
updateVerifyOut
);
let result = verifyForVerify(verifyGenerator, input, globalSignBlob);
console.log("[callback] verifyForVerify result: " + result);
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
async function keyGenerationBySpecProcess(asyAlgoName) {
var sk;
var pk;
let generatorBySpec;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
return convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
})
.then((convertKeyPair) => {
let dsap = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_P_BN
);
let dsaq = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_Q_BN
);
let dsag = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_G_BN
);
sk = convertKeyPair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
pk = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
console.log("keyGenerationBySpecProcess sk " + sk);
console.log("keyGenerationBySpecProcess pk " + pk);
let commKeySpec = asyCommon.genDsaKeyPairSpec(dsap, dsaq, dsag, sk, pk);
generatorBySpec = createAsyKeyGeneratorBySpec(commKeySpec);
return generateAsyKeyPair(generatorBySpec);
})
.then((specKeyPair) => {
let skSpec = specKeyPair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
let pkSpec = specKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
console.log("keyGenerationBySpecProcess skSpec " + skSpec);
console.log("keyGenerationBySpecProcess pkSpec " + pkSpec);
if (sk == skSpec && pk == pkSpec) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
reject(err);
});
});
var sk;
var pk;
let generatorBySpec;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
expect(rsaGenerator != null).assertTrue();
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
return convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
})
.then((convertKeyPair) => {
let dsap = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_P_BN
);
let dsaq = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_Q_BN
);
let dsag = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_G_BN
);
sk = convertKeyPair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
pk = convertKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
console.log("keyGenerationBySpecProcess sk " + sk);
console.log("keyGenerationBySpecProcess pk " + pk);
let commKeySpec = asyCommon.genDsaKeyPairSpec(dsap, dsaq, dsag, sk, pk);
generatorBySpec = createAsyKeyGeneratorBySpec(commKeySpec);
return generateAsyKeyPair(generatorBySpec);
})
.then((specKeyPair) => {
let skSpec = specKeyPair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
let pkSpec = specKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
console.log("keyGenerationBySpecProcess skSpec " + skSpec);
console.log("keyGenerationBySpecProcess pkSpec " + pkSpec);
if (sk == skSpec && pk == pkSpec) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
reject(err);
});
});
}
async function verifyAbnormalParameterSM2Process(
asyKeySpec,
verifyAlgoName,
verifyType
async function verifyAbnormalParameterProcess(
asyKeySpec,
signVerifyAlgoName,
verifyType
) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGenerator(asyKeySpec);
let verifyGenerator = createAsyVerify(verifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Callback] verifyAbnormalParameterProcess initVerifyOut:" +
initVerifyOut
);
resolve(
verifyForVerifyFailed(
verifyGenerator,
input,
globalSignBlob,
verifyType
)
);
})
.catch((err) => {
reject(err);
});
});
let globalText = "This is a sign test";
let input = { data: stringTouInt8Array(globalText) };
try {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPair = await specGenerator.generateKeyPair();
await initSign(signGenerator, keyPair.priKey);
await updateSign(signGenerator, input);
let signBlob = await signForSign(signGenerator, input);
switch (verifyType) {
case 1:
await initVerify(verifyGenerator, keyPair.pubKey);
await updateVerify(verifyGenerator, input);
return await verifyForVerify(verifyGenerator, null, signBlob);
case 2:
await initVerify(verifyGenerator, keyPair.pubKey);
await updateVerify(verifyGenerator, input);
return await verifyForVerify(verifyGenerator, '', signBlob);
case 3:
await initVerify(verifyGenerator, keyPair.pubKey);
await updateVerify(verifyGenerator, input);
return await verifyForVerify(verifyGenerator, input, null);
default:
return await verifyForVerify(verifyGenerator, input, signBlob);
}
} catch (err) {
console.error("[Callback] verifyUpdateAbnormalParameterProcess catch err: " + err);
throw err;
}
}
export {
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
encryptSetAndGetSpecInitProcess,
encryptAndDecryptBySpecProcess,
signAndVerifyBySpecProcess,
keyGenerationBySpecProcess,
signAndVerifySetAndGetSpecProcess,
signAbnormalParameterProcess,
verifyAbnormalParameterProcess,
verifyAbnormalParameterSM2Process,
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
encryptSetAndGetSpecInitProcess,
encryptAndDecryptBySpecProcess,
signAndVerifyBySpecProcess,
keyGenerationBySpecProcess,
signAndVerifySetAndGetSpecProcess,
signAbnormalParameterProcess,
verifyAbnormalParameterProcess
};
......@@ -14,1016 +14,819 @@
*/
import cryptoFramework from "@ohos.security.cryptoFramework";
import { stringTouInt8Array, uInt8ArrayToShowStr, uInt8ArrayToString, } from "../common/publicDoString";
import {
stringTouInt8Array,
uInt8ArrayToShowStr,
uInt8ArrayToString,
} from "../common/publicDoString";
import {
createAsyKeyGenerator,
createAsyKeyGeneratorBySpec,
createAsyCipher,
createAsySign,
createAsyVerify,
createAsyKeyAgreement,
createAsyKeyGenerator,
createAsyKeyGeneratorBySpec,
createAsyCipher,
createAsySign,
createAsyVerify,
createAsyKeyAgreement,
} from "./publicAsymmetricCommon";
async function generateAsyKeyPair(rsaGenerator) {
var pubKey;
var priKey;
return new Promise((resolve, reject) => {
rsaGenerator
.generateKeyPair()
.then((rsaKeyPair) => {
pubKey = rsaKeyPair.pubKey;
let encodedPubKey = pubKey.getEncoded();
console.log("encodedPubKey: " + encodedPubKey);
priKey = rsaKeyPair.priKey;
let encodedPriKey = priKey.getEncoded();
console.log("encodedPriKey: " + encodedPriKey);
resolve(rsaKeyPair);
})
.catch((err) => {
console.error("[Promise]generateSymKey failed. error is " + err);
reject(err);
});
});
var pubKey;
var priKey;
return new Promise((resolve, reject) => {
rsaGenerator
.generateKeyPair()
.then((rsaKeyPair) => {
pubKey = rsaKeyPair.pubKey;
let encodedPubKey = pubKey.getEncoded();
console.log("encodedPubKey: " + encodedPubKey);
priKey = rsaKeyPair.priKey;
let encodedPriKey = priKey.getEncoded();
console.log("encodedPriKey: " + encodedPriKey);
resolve(rsaKeyPair);
})
.catch((err) => {
console.error("[Promise]generateSymKey failed. error is " + err);
reject(err);
});
});
}
async function convertAsyKey(rsaGenerator, pubKeyDataBlob, priKeyDataBlob) {
return new Promise((resolve, reject) => {
rsaGenerator
.convertKey(pubKeyDataBlob, priKeyDataBlob)
.then((convertKeyPair) => {
console.log(
"[Promise]convertKey success. convertKeyPair is " + convertKeyPair
);
resolve(convertKeyPair);
})
.catch((err) => {
console.error("[Promise]convertKey failed. error is " + err);
reject(err);
});
});
return new Promise((resolve, reject) => {
rsaGenerator
.convertKey(pubKeyDataBlob, priKeyDataBlob)
.then((convertKeyPair) => {
console.log(
"[Promise]convertKey success. convertKeyPair is " + convertKeyPair
);
resolve(convertKeyPair);
})
.catch((err) => {
console.error("[Promise]convertKey failed. error is " + err);
reject(err);
});
});
}
async function initCipher(cipherGenerator, mode, key, params) {
return new Promise((resolve, reject) => {
cipherGenerator
.init(mode, key, params)
.then(() => {
console.log("[Promise]cipherGenerator init success! mode is : " + mode);
resolve("init success");
})
.catch((err) => {
console.error(
"[Promise]cipherGenerator init failed. error is " +
err +
"mode is " +
mode
);
reject(err);
});
});
return new Promise((resolve, reject) => {
cipherGenerator
.init(mode, key, params)
.then(() => {
console.log("[Promise]cipherGenerator init success! mode is : " + mode);
resolve("init success");
})
.catch((err) => {
console.error(
"[Promise]cipherGenerator init failed. error is " +
err +
"mode is " +
mode
);
reject(err);
});
});
}
async function doFinalCipher(cipherGenerator, mode, dataBlob) {
return new Promise((resolve, reject) => {
cipherGenerator
.doFinal(dataBlob)
.then((finalData) => {
console.log(
"[Promise]cipherGenerator doFinal success! mode is : " + mode
);
console.log(
"[Promise]cipherGenerator doFinal success! dataBlob is : " + dataBlob
);
resolve(finalData);
})
.catch((err) => {
console.log(
"[Promise]cipherGenerator doFinal faailed dataBlob is : " + dataBlob
);
console.error(
"[Promise]cipherGenerator doFinal failed. error is " +
err +
"mode is " +
mode
);
reject(err);
});
});
return new Promise((resolve, reject) => {
cipherGenerator
.doFinal(dataBlob)
.then((finalData) => {
console.log(
"[Promise]cipherGenerator doFinal success! mode is : " + mode
);
console.log(
"[Promise]cipherGenerator doFinal success! dataBlob is : " + dataBlob
);
resolve(finalData);
})
.catch((err) => {
console.log(
"[Promise]cipherGenerator doFinal faailed dataBlob is : " + dataBlob
);
console.error(
"[Promise]cipherGenerator doFinal failed. error is " +
err +
"mode is " +
mode
);
reject(err);
});
});
}
async function initSign(signGenerator, priKey) {
return new Promise((resolve, reject) => {
signGenerator
.init(priKey)
.then(() => {
console.log("[Promise]signGenerator init success!");
resolve("init success");
})
.catch((err) => {
console.error("[Promise]signGenerator init failed. error is " + err);
reject(err);
});
});
return new Promise((resolve, reject) => {
signGenerator
.init(priKey)
.then(() => {
console.log("[Promise]signGenerator init success!");
resolve("init success");
})
.catch((err) => {
console.error("[Promise]signGenerator init failed. error is " + err);
reject(err);
});
});
}
async function updateSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator
.update(dataBlob)
.then(() => {
console.log("[Promise]signGenerator update success!");
resolve("update success");
})
.catch((err) => {
console.error("[Promise]signGenerator update failed. error is " + err);
reject(err);
});
});
}
async function updateSignFailed(signGenerator, dataBlob, itemType) {
return new Promise((resolve, reject) => {
switch (itemType) {
case 0:
signGenerator
.update()
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
break;
case 1:
signGenerator
.update(null)
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
break;
case 2:
signGenerator
.update("")
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
break;
case 3:
let data = dataBlob;
for (let i = 1; i < 5000; i++) {
data = data + data;
}
signGenerator
.update(data)
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
break;
default:
return new Promise((resolve, reject) => {
signGenerator
.update(dataBlob)
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
break;
}
});
.update(dataBlob)
.then(() => {
console.log("[Promise]signGenerator update success!");
resolve("update success");
})
.catch((err) => {
console.error("[Promise]signGenerator update failed. error is " + err);
reject(err);
});
});
}
async function signForSign(signGenerator, dataBlob) {
return new Promise((resolve, reject) => {
signGenerator
.sign(dataBlob)
.then((signOutput) => {
console.log("[Promise]signGenerator sign success!");
resolve(signOutput);
})
.catch((err) => {
console.error("[Promise]signGenerator sign failed. error is " + err);
reject(err);
});
});
return new Promise((resolve, reject) => {
signGenerator
.sign(dataBlob)
.then((signOutput) => {
console.log("[Promise]signGenerator sign success!");
resolve(signOutput);
})
.catch((err) => {
console.error("[Promise]signGenerator sign failed. error is " + err);
reject(err);
});
});
}
async function initVerify(verifyGenerator, pubKey) {
return new Promise((resolve, reject) => {
verifyGenerator
.init(pubKey)
.then(() => {
console.log("[Promise]verifyGenerator init success!");
resolve("init success");
})
.catch((err) => {
console.error("[Promise]verifyGenerator init failed. error is " + err);
reject(err);
});
});
return new Promise((resolve, reject) => {
verifyGenerator
.init(pubKey)
.then(() => {
console.log("[Promise]verifyGenerator init success!");
resolve("init success");
})
.catch((err) => {
console.error("[Promise]verifyGenerator init failed. error is " + err);
reject(err);
});
});
}
async function updateVerify(verifyGenerator, dataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator
.update(dataBlob)
.then(() => {
console.log("[Promise]verifyGenerator update success!");
resolve("update success");
})
.catch((err) => {
console.error(
"[Promise]verifyGenerator update failed. error is " + err
);
reject(err);
});
});
}
async function updateVerifyFailed(verifyGenerator, dataBlob, itemType) {
return new Promise((resolve, reject) => {
if (itemType == 0) {
verifyGenerator
.update()
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
} else if (itemType == 1) {
verifyGenerator
.update(null)
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
} else if (itemType == 2) {
verifyGenerator
.update("")
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
} else if (itemType == 3) {
verifyGenerator
.update(dataBlob, dataBlob)
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
} else {
verifyGenerator
.update(dataBlob)
.then(() => {
resolve("update success");
})
.catch((err) => {
reject(err);
});
}
});
return new Promise((resolve, reject) => {
verifyGenerator
.update(dataBlob)
.then(() => {
console.log("[Promise]verifyGenerator update success!");
resolve("update success");
})
.catch((err) => {
console.error(
"[Promise]verifyGenerator update failed. error is " + err
);
reject(err);
});
});
}
async function verifyForVerify(verifyGenerator, dataBlob, signDataBlob) {
return new Promise((resolve, reject) => {
verifyGenerator
.verify(dataBlob, signDataBlob)
.then((verifyOutput) => {
console.log("[Promise]signGenerator sign success!");
resolve(verifyOutput);
})
.catch((err) => {
console.error("[Promise]signGenerator sign failed. error is " + err);
reject(err);
});
});
return new Promise((resolve, reject) => {
verifyGenerator
.verify(dataBlob, signDataBlob)
.then((verifyOutput) => {
console.log("[Promise]signGenerator sign success!");
resolve(verifyOutput);
})
.catch((err) => {
console.error("[Promise]signGenerator sign failed. error is " + err);
reject(err);
});
});
}
async function generateAsySecret(generator, priKey, pubKey) {
return new Promise((resolve, reject) => {
generator
.generateSecret(priKey, pubKey)
.then((output) => {
console.log("[Promise]generateSecret success!");
resolve(output);
})
.catch((err) => {
console.error("[Promise]generateSecret failed. error is " + err);
reject(err);
});
});
return new Promise((resolve, reject) => {
generator
.generateSecret(priKey, pubKey)
.then((output) => {
console.log("[Promise]generateSecret success!");
resolve(output);
})
.catch((err) => {
console.error("[Promise]generateSecret failed. error is " + err);
reject(err);
});
});
}
async function encryptAndDecryptNormalProcess(asyAlgoName, cipherAlgoName) {
var globalCipherText;
var globalRsaKeyPair;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalRsaKeyPair.pubKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptNormalProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalRsaKeyPair.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptNormalProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] encryptAndDecryptNormalProcess catch err:" + err
);
reject(err);
});
});
var globalCipherText;
var globalRsaKeyPair;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
var cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return initCipher(
cipherGeneratorEncrypt,
encryptMode,
globalRsaKeyPair.pubKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptNormalProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
globalRsaKeyPair.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptNormalProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] encryptAndDecryptNormalProcess catch err:" + err
);
reject(err);
});
});
}
async function signAndVerifyNormalProcess(asyAlgoName, signVerifyAlgoName) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var signGenerator = createAsySign(signVerifyAlgoName);
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] signAndVerifyNormalProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[promise] signAndVerifyNormalProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
console.log("signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data));
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[promise] signAndVerifyNormalProcess updateSignOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[promise] signAndVerifyNormalProcess updateVerifyOut:" +
updateVerifyOut
);
let result = verifyForVerify(verifyGenerator, input, globalSignBlob);
resolve(result);
})
.catch((err) => {
console.error("[promise] signAndVerifyNormalProcess catch err:" + err);
reject(err);
});
});
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(asyAlgoName);
var signGenerator = createAsySign(signVerifyAlgoName);
var verifyGenerator = createAsyVerify(signVerifyAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
let encodedPubKey = rsaKeyPair.pubKey.getEncoded();
let encodedPriKey = rsaKeyPair.priKey.getEncoded();
convertAsyKey(rsaGenerator, encodedPubKey, encodedPriKey);
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] signAndVerifyNormalProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[promise] signAndVerifyNormalProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
console.log("signOutput: " + uInt8ArrayToShowStr(globalSignBlob.data));
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[promise] signAndVerifyNormalProcess updateSignOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[promise] signAndVerifyNormalProcess updateVerifyOut:" +
updateVerifyOut
);
let result = verifyForVerify(verifyGenerator, input, globalSignBlob);
resolve(result);
})
.catch((err) => {
console.error("[promise] signAndVerifyNormalProcess catch err:" + err);
reject(err);
});
});
}
async function keyAgreementProcess(ECDHAlgoName) {
var globalKeyPair;
var globalKeyPair;
return new Promise((resolve, reject) => {
var eccGenerator = createAsyKeyGenerator("ECC521");
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
if (globalECDHData.code != null) {
reject(globalECDHData);
}
generateAsyKeyPair(eccGenerator)
.then((keyPair) => {
globalKeyPair = keyPair;
return generateAsySecret(
globalECDHData,
globalKeyPair.priKey,
globalKeyPair.pubKey
);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
if (result != null) {
resolve(true);
} else {
resolve(false);
return new Promise((resolve, reject) => {
var eccGenerator = createAsyKeyGenerator("ECC521");
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
if (globalECDHData.code != null) {
reject(globalECDHData);
}
})
.catch((err) => {
console.error("[promise] keyAgreementProcess catch err.code:" + err);
reject(err);
});
});
generateAsyKeyPair(eccGenerator)
.then((keyPair) => {
globalKeyPair = keyPair;
return generateAsySecret(
globalECDHData,
globalKeyPair.priKey,
globalKeyPair.pubKey
);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
if (result != null) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error("[promise] keyAgreementProcess catch err.code:" + err);
reject(err);
});
});
}
async function keyAgreementGetAlgNameProcess(ECDHAlgoName) {
var globalRsaKeyPair;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(ECDHAlgoName);
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return generateAsySecret(
globalECDHData,
globalRsaKeyPair.priKey,
globalRsaKeyPair.pubKey
);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
console.log("[Promise]: globalECDHData.algName 1: " + ECDHAlgoName);
console.log(
"[Promise]: globalECDHData.algName 2: " + globalECDHData.algName
);
resolve();
})
.catch((err) => {
console.error("[promise] keyAgreementProcess catch err:" + err);
reject(err);
});
});
var globalRsaKeyPair;
return new Promise((resolve, reject) => {
var rsaGenerator = createAsyKeyGenerator(ECDHAlgoName);
var globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
generateAsyKeyPair(rsaGenerator)
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return generateAsySecret(
globalECDHData,
globalRsaKeyPair.priKey,
globalRsaKeyPair.pubKey
);
})
.then((result) => {
console.warn("result data is " + uInt8ArrayToShowStr(result.data));
console.log("[Promise]: globalECDHData.algName 1: " + ECDHAlgoName);
console.log(
"[Promise]: globalECDHData.algName 2: " + globalECDHData.algName
);
resolve();
})
.catch((err) => {
console.error("[promise] keyAgreementProcess catch err:" + err);
reject(err);
});
});
}
async function keyAgreementBySpecProcess(asyKeySpec, ECDHAlgoName) {
var globalKeyPair;
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalKeyPair = keyPair;
return generateAsySecret(
globalECDHData,
globalKeyPair.priKey,
globalKeyPair.pubKey
);
})
.then((result) => {
if (result.data != null) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] keyAgreementBySpecProcess catch err:" + err.code
);
reject(err);
});
});
var globalKeyPair;
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let globalECDHData = createAsyKeyAgreement(ECDHAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalKeyPair = keyPair;
return generateAsySecret(
globalECDHData,
globalKeyPair.priKey,
globalKeyPair.pubKey
);
})
.then((result) => {
if (result.data != null) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] keyAgreementBySpecProcess catch err:" + err.code
);
reject(err);
});
});
}
async function encryptAndDecryptBySpecProcess(asyKeyPairSpec, cipherAlgoName) {
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDncrypt = createAsyCipher(cipherAlgoName);
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptBySpecProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDncrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptBySpecProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDncrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] encryptAndDecryptBySpecProcess catch err:" + err
);
reject(err);
});
});
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
return new Promise((resolve, reject) => {
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDncrypt = createAsyCipher(cipherAlgoName);
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptBySpecProcess encryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDncrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptAndDecryptBySpecProcess decryptMode initCipherOut:" +
initCipherOut
);
return doFinalCipher(
cipherGeneratorDncrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] encryptAndDecryptBySpecProcess catch err:" + err
);
reject(err);
});
});
}
async function encryptInitSetAndGetSpecProcess(asyKeyPairSpec, cipherAlgoName) {
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
var pSource = new Uint8Array([1, 2, 3, 4]);
return new Promise((resolve, reject) => {
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[promise] encryptInitSetAndGetSpecProcess encryptMode initCipherOut:" +
initCipherOut
);
cipherGeneratorEncrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
cipherGeneratorEncrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptInitSetAndGetSpecProcess decryptMode initCipherOut:" +
initCipherOut
);
cipherGeneratorDecrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
cipherGeneratorDecrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] encryptInitSetAndGetSpecProcess catch err:" + err
);
reject(err);
});
});
var globalCipherText;
var globalText = "This is a cipher test";
var input = { data: stringTouInt8Array(globalText) };
var encryptMode = cryptoFramework.CryptoMode.ENCRYPT_MODE;
var decryptMode = cryptoFramework.CryptoMode.DECRYPT_MODE;
var pSource = new Uint8Array([1, 2, 3, 4]);
return new Promise((resolve, reject) => {
var cipherGeneratorEncrypt = createAsyCipher(cipherAlgoName);
let cipherGeneratorDecrypt = createAsyCipher(cipherAlgoName);
initCipher(cipherGeneratorEncrypt, encryptMode, asyKeyPairSpec.pubKey, null)
.then((initCipherOut) => {
console.log(
"[promise] encryptInitSetAndGetSpecProcess encryptMode initCipherOut:" +
initCipherOut
);
cipherGeneratorEncrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
cipherGeneratorEncrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
return doFinalCipher(cipherGeneratorEncrypt, encryptMode, input);
})
.then((finalOutput) => {
globalCipherText = finalOutput;
return initCipher(
cipherGeneratorDecrypt,
decryptMode,
asyKeyPairSpec.priKey,
null
);
})
.then((initCipherOut) => {
console.log(
"[promise] encryptInitSetAndGetSpecProcess decryptMode initCipherOut:" +
initCipherOut
);
cipherGeneratorDecrypt.setCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR,
pSource
);
cipherGeneratorDecrypt.getCipherSpec(
cryptoFramework.CipherSpecItem.OAEP_MGF1_PSRC_UINT8ARR
);
return doFinalCipher(
cipherGeneratorDecrypt,
decryptMode,
globalCipherText
);
})
.then((finalOutput) => {
let decryptData = uInt8ArrayToString(finalOutput.data);
if (decryptData == globalText) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] encryptInitSetAndGetSpecProcess catch err:" + err
);
reject(err);
});
});
}
async function signAndVerifyBySpecProcess(asyKeySpec, signVerifyAlgoName) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess initVerifyOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess updateVerifyOut:" +
updateVerifyOut
);
resolve(verifyForVerify(verifyGenerator, input, globalSignBlob));
})
.catch((err) => {
console.error("[promise] signAndVerifyBySpecProcess catch err:" + err);
reject(err);
});
});
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess initSignOut:" + initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess updateSignOut:" + updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess initVerifyOut:" + initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[promise] signAndVerifyBySpecProcess updateVerifyOut:" +
updateVerifyOut
);
resolve(verifyForVerify(verifyGenerator, input, globalSignBlob));
})
.catch((err) => {
console.error("[promise] signAndVerifyBySpecProcess catch err:" + err);
reject(err);
});
});
}
async function signAndVerifySetAndGetSpecProcess(
asyKeySpec,
signVerifyAlgoName,
itemType,
itemValue
asyKeySpec,
signVerifyAlgoName,
itemType,
itemValue
) {
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess initSignOut:" +
initSignOut
);
signGenerator.setSignSpec(itemType, itemValue);
verifyGenerator.setVerifySpec(itemType, itemValue);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess updateSignOut:" +
updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess initVerifyOut:" +
initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess updateVerifyOut:" +
updateVerifyOut
);
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((finalStatus) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess finalStatus:" +
finalStatus
);
let signSpec = signGenerator.getSignSpec(itemType);
let verifySpec = verifyGenerator.getVerifySpec(itemType);
if (verifySpec == itemValue && signSpec == itemValue) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] signAndVerifySetAndGetSpecProcess catch err:" + err
);
reject(err);
});
});
var globalRsaKeyPair;
var globalSignBlob;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess initSignOut:" +
initSignOut
);
signGenerator.setSignSpec(itemType, itemValue);
verifyGenerator.setVerifySpec(itemType, itemValue);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess updateSignOut:" +
updateSignOut
);
return signForSign(signGenerator, input);
})
.then((finalOutput) => {
globalSignBlob = finalOutput;
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess initVerifyOut:" +
initVerifyOut
);
return updateVerify(verifyGenerator, input);
})
.then((updateVerifyOut) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess updateVerifyOut:" +
updateVerifyOut
);
return verifyForVerify(verifyGenerator, input, globalSignBlob);
})
.then((finalStatus) => {
console.log(
"[promise] signAndVerifySetAndGetSpecProcess finalStatus:" +
finalStatus
);
let signSpec = signGenerator.getSignSpec(itemType);
let verifySpec = verifyGenerator.getVerifySpec(itemType);
if (verifySpec == itemValue && signSpec == itemValue) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error(
"[promise] signAndVerifySetAndGetSpecProcess catch err:" + err
);
reject(err);
});
});
}
async function updateAbnormalParameterProcess(
asyKeySpec,
signVerifyAlgoName,
updateType
asyKeySpec,
signVerifyAlgoName,
updateType
) {
var globalRsaKeyPair;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((keyPair) => {
globalRsaKeyPair = keyPair;
if (updateType == 4) {
resolve(updateSignFailed(signGenerator, input, updateType));
let globalText = "This is a sign test";
let input = { data: stringTouInt8Array(globalText) };
try {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let keyPair = await specGenerator.generateKeyPair();
switch (updateType) {
case 1:
await initSign(signGenerator, keyPair.priKey);
await updateSign(signGenerator, null);
break;
case 2:
await initSign(signGenerator, keyPair.priKey);
await updateSign(signGenerator, '');
break;
case 3:
await initSign(signGenerator, keyPair.priKey);
await updateSign(signGenerator, 222);
break;
default:
await updateSign(signGenerator, input);
break;
}
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] updateAbnormalParameterProcess initSignOut:" + initSignOut
);
resolve(updateSignFailed(signGenerator, input, updateType));
})
.catch((err) => {
console.error(
"[promise] updateAbnormalParameterProcess catch err:" + err
);
reject(err);
});
});
} catch (err) {
console.error("[Promise] updateAbnormalParameterProcess catch err: " + err);
throw err;
}
}
async function verifyUpdateAbnormalParameterProcess(
asyKeySpec,
signVerifyAlgoName,
updateType
asyKeySpec,
signVerifyAlgoName,
updateType
) {
var globalRsaKeyPair;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[promise] verifyUpdateAbnormalParameterProcess initSignOut:" +
initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[promise] verifyUpdateAbnormalParameterProcess updateSignOut:" +
updateSignOut
);
return signForSign(signGenerator, input);
})
.then((signOut) => {
console.log(
"[promise] verifyUpdateAbnormalParameterProcess signOut:" + signOut
);
if (updateType == 4) {
resolve(updateVerifyFailed(verifyGenerator, input, updateType));
let globalText = "This is a sign test";
let input = { data: stringTouInt8Array(globalText) };
try {
let specGenerator = createAsyKeyGeneratorBySpec(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPair = await specGenerator.generateKeyPair();
await initSign(signGenerator, keyPair.priKey);
await updateSign(signGenerator, input);
await signForSign(signGenerator, input);
switch (updateType) {
case 1:
await initVerify(verifyGenerator, keyPair.pubKey);
await updateVerify(verifyGenerator, null);
break;
case 2:
await initVerify(verifyGenerator, keyPair.pubKey);
await updateVerify(verifyGenerator, '');
break;
case 3:
await initVerify(verifyGenerator, keyPair.pubKey);
await updateVerify(verifyGenerator, 222);
break;
default:
await updateVerify(verifyGenerator, input);
break;
}
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[promise] verifyUpdateAbnormalParameterProcess initVerifyOut:" +
initVerifyOut
);
resolve(updateVerifyFailed(verifyGenerator, input, updateType));
})
.catch((err) => {
console.error(
"[promise] VerifyUpdateAbnormalParameterProcess catch err:" + err
);
reject(err);
});
});
} catch (err) {
console.error("[Promise] verifyUpdateAbnormalParameterProcess catch err: " + err);
throw err;
}
}
async function generateSpecKeyPair(asyKeySpec) {
return new Promise((resolve, reject) => {
asyKeySpec
.generateKeyPair()
.then((asyKeyPair) => {
console.info("Security_CryptoFramework asyKeyPair = " + asyKeyPair);
resolve(asyKeyPair);
})
.catch((err) => {
console.error(
"[Promise] generateSpecAsyKeyPair failed. error is " + err
);
reject(err);
});
});
return new Promise((resolve, reject) => {
asyKeySpec
.generateKeyPair()
.then((asyKeyPair) => {
console.info("Security_CryptoFramework asyKeyPair = " + asyKeyPair);
resolve(asyKeyPair);
})
.catch((err) => {
console.error(
"[Promise] generateSpecAsyKeyPair failed. error is " + err
);
reject(err);
});
});
}
async function keyGenerationBySpecProcess(asyKeySpec) {
var pubkey;
var prikey;
return new Promise((resolve, reject) => {
var specGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeySpec);
generateSpecKeyPair(specGenerator)
.then((keypair) => {
let pubEncoded = keypair.pubKey.getEncoded();
let priEncoded = keypair.priKey.getEncoded();
pubkey = keypair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
prikey = keypair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
console.log("keyGenerationBySpecProcess keypair.pubKey " + pubkey);
console.log("keyGenerationBySpecProcess keypair.priKey " + prikey);
let rsaGenerator = createAsyKeyGenerator("DSA2048");
return convertAsyKey(rsaGenerator, pubEncoded, priEncoded);
})
.then((conKeyPair) => {
let conPrikey = conKeyPair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
let conPubkey = conKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
if (pubkey == conPubkey && prikey == conPrikey) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error("[promise] keyGenerationBySpecProcess catch err:" + err);
reject(err);
});
});
}
async function verifyUpdateAbnormalParameterSM2Process(
asyKeySpec,
signVerifyAlgoName,
updateType
) {
var globalRsaKeyPair;
var globalText = "This is a sign test";
var input = { data: stringTouInt8Array(globalText) };
return new Promise((resolve, reject) => {
let specGenerator = createAsyKeyGenerator(asyKeySpec);
let signGenerator = createAsySign(signVerifyAlgoName);
let verifyGenerator = createAsyVerify(signVerifyAlgoName);
let keyPairPromise = specGenerator.generateKeyPair();
keyPairPromise
.then((rsaKeyPair) => {
globalRsaKeyPair = rsaKeyPair;
return initSign(signGenerator, globalRsaKeyPair.priKey);
})
.then((initSignOut) => {
console.log(
"[Promise] verifyUpdateAbnormalParameterProcess initSignOut:" +
initSignOut
);
return updateSign(signGenerator, input);
})
.then((updateSignOut) => {
console.log(
"[Promise] verifyUpdateAbnormalParameterProcess updateSignOut:" +
updateSignOut
);
return signForSign(signGenerator, input);
})
.then((signOut) => {
console.log(
"[Promise] verifyUpdateAbnormalParameterProcess signOut:" + signOut
);
if (updateType == 4) {
resolve(updateVerifyFailed(verifyGenerator, input, updateType));
}
return initVerify(verifyGenerator, globalRsaKeyPair.pubKey);
})
.then((initVerifyOut) => {
console.log(
"[Promise] verifyUpdateAbnormalParameterProcess initVerifyOut:" +
initVerifyOut
);
resolve(updateVerifyFailed(verifyGenerator, input, updateType));
})
.catch((err) => {
console.error(
"[Promise] verifyUpdateAbnormalParameterProcess catch err:" + err
);
reject(err);
});
});
var pubkey;
var prikey;
return new Promise((resolve, reject) => {
var specGenerator = cryptoFramework.createAsyKeyGeneratorBySpec(asyKeySpec);
generateSpecKeyPair(specGenerator)
.then((keypair) => {
let pubEncoded = keypair.pubKey.getEncoded();
let priEncoded = keypair.priKey.getEncoded();
pubkey = keypair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
prikey = keypair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
console.log("keyGenerationBySpecProcess keypair.pubKey " + pubkey);
console.log("keyGenerationBySpecProcess keypair.priKey " + prikey);
let rsaGenerator = createAsyKeyGenerator("DSA2048");
return convertAsyKey(rsaGenerator, pubEncoded, priEncoded);
})
.then((conKeyPair) => {
let conPrikey = conKeyPair.priKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_SK_BN
);
let conPubkey = conKeyPair.pubKey.getAsyKeySpec(
cryptoFramework.AsyKeySpecItem.DSA_PK_BN
);
if (pubkey == conPubkey && prikey == conPrikey) {
resolve(true);
} else {
resolve(false);
}
})
.catch((err) => {
console.error("[promise] keyGenerationBySpecProcess catch err:" + err);
reject(err);
});
});
}
export {
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
keyAgreementProcess,
keyAgreementGetAlgNameProcess,
encryptAndDecryptBySpecProcess,
keyAgreementBySpecProcess,
encryptInitSetAndGetSpecProcess,
signAndVerifyBySpecProcess,
signAndVerifySetAndGetSpecProcess,
updateAbnormalParameterProcess,
verifyUpdateAbnormalParameterProcess,
keyGenerationBySpecProcess,
verifyUpdateAbnormalParameterSM2Process,
encryptAndDecryptNormalProcess,
signAndVerifyNormalProcess,
keyAgreementProcess,
keyAgreementGetAlgNameProcess,
encryptAndDecryptBySpecProcess,
keyAgreementBySpecProcess,
encryptInitSetAndGetSpecProcess,
signAndVerifyBySpecProcess,
signAndVerifySetAndGetSpecProcess,
updateAbnormalParameterProcess,
verifyUpdateAbnormalParameterProcess,
keyGenerationBySpecProcess
};
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册