Skip to content

Commit 3b3af16

Browse files
committed
Add tests
1 parent b239435 commit 3b3af16

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

tests/operations/index.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ import "./tests/SetIntersection.mjs";
139139
import "./tests/SetUnion.mjs";
140140
import "./tests/Shuffle.mjs";
141141
import "./tests/SIGABA.mjs";
142+
import "./tests/SM2.mjs";
142143
import "./tests/SM4.mjs";
143144
// import "./tests/SplitColourChannels.mjs"; // Cannot test operations that use the File type yet
144145
import "./tests/StrUtils.mjs";

tests/operations/tests/SM2.mjs

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/**
2+
* SM2 Tests
3+
*
4+
* @author flakjacket95 [[email protected]]
5+
* @copyright Crown Copyright 2024
6+
* @license Apache-2.0
7+
*/
8+
import TestRegister from "../../lib/TestRegister.mjs";
9+
10+
/* Plaintexts */
11+
12+
const SMALL_PLAIN = "I am a small plaintext"
13+
const LARGE_PLAIN = "I am a larger plaintext, that will require the encryption KDF to generate a much larger key to properly encrypt me"
14+
15+
/* Test Key Parameters */
16+
const PUBLIC_X = "f7d903cab7925066c31150a92b31e548e63f954f92d01eaa0271fb2a336baef8"
17+
const PUBLIC_Y = "fb0c45e410ef7a6cdae724e6a78dbff52562e97ede009e762b667d9b14adea6c"
18+
const PRIVATE_K = "e74a72505084c3269aa9b696d603e3e08c74c6740212c11a31e26cdfe08bdf6a"
19+
20+
const CURVE = "sm2p256v1"
21+
22+
/* Decryption Test Ciphertext*/
23+
24+
const CIPHERTEXT_1 = "9a31bc0adb4677cdc4141479e3949572a55c3e6fb52094721f741c2bd2e179aaa87be6263bc1be602e473be3d5de5dce97f8248948b3a7e15f9f67f64aef21575e0c05e6171870a10ff9ab778dbef24267ad90e1a9d47d68f757d57c4816612e9829f804025dea05a511cda39371c22a2828f976f72e"
25+
const CIPHERTEXT_2 = "d3647d68568a2e7a4f8e843286be7bf2b4d80256697d19a73df306ae1a7e6d0364d942e23d2340606e7a2502a838b132f9242587b2ea7e4c207e87242eea8cae68f5ff4da2a95a7f6d350608ae5b6777e1d925bf9c560087af84aba7befba713130106ddb4082d803811bca3864594722f3198d58257fe4ba37f4aa540adf4cb0568bddd2d8140ad3030deea0a87e3198655cc4d22bfc3d73b1c4afec2ff15d68c8d1298d97132cace922ee8a4e41ca288a7e748b77ca94aa81dc283439923ae7939e00898e16fe5111fbe1d928d152b216a"
26+
const CIPHERTEXT_3 = "5f340eeb4398fa8950ee3408d0e3fe34bf7728c9fdb060c94b916891b5c693610274160b52a7132a2bf16ad5cdb57d1e00da2f3ddbd55350729aa9c268b53e40c05ccce9912daa14406e8c132e389484e69757350be25351755dcc6c25c94b3c1a448b2cf8c2017582125eb6cf782055b199a875e966"
27+
const CIPHERTEXT_4 = "0649bac46c3f9fd7fb3b2be4bff27414d634651efd02ca67d8c802bbc5468e77d035c39b581d6b56227f5d87c0b4efbea5032c0761139295ae194b9f1fce698f2f4b51d89fa5554171a1aad2e61fe9de89831aec472ecc5ab178ebf4d2230c1fb94fca03e536b87b9eba6db71ba9939260a08ffd230ca86cb45cf754854222364231bdb8b873791d63ad57a4b3fa5b6375388dc879373f5f1be9051bc5072a8afbec5b7b034e4907aa5bb4b6b1f50e725d09cb6a02e07ce20263005f6c9157ce05d3ea739d231d4f09396fb72aa680884d78"
28+
29+
30+
TestRegister.addTests([
31+
{
32+
name: "SM2 Decrypt: Small Input; Format One",
33+
input: CIPHERTEXT_1,
34+
expectedOutput: SMALL_PLAIN,
35+
recipeConfig: [
36+
{
37+
"op": "SM2 Decrypt",
38+
"args": [PRIVATE_K, "C1C3C2", CURVE]
39+
}
40+
]
41+
},
42+
{
43+
name: "SM2 Decrypt: Large Input; Format One",
44+
input: CIPHERTEXT_2,
45+
expectedOutput: LARGE_PLAIN,
46+
recipeConfig: [
47+
{
48+
"op": "SM2 Decrypt",
49+
"args": [PRIVATE_K, "C1C3C2", CURVE]
50+
}
51+
]
52+
},
53+
{
54+
name: "SM2 Decrypt: Small Input; Format Two",
55+
input: CIPHERTEXT_3,
56+
expectedOutput: SMALL_PLAIN,
57+
recipeConfig: [
58+
{
59+
"op": "SM2 Decrypt",
60+
"args": [PRIVATE_K, "C1C2C3", CURVE]
61+
}
62+
]
63+
},
64+
{
65+
name: "SM2 Decrypt: Large Input; Format Two",
66+
input: CIPHERTEXT_4,
67+
expectedOutput: LARGE_PLAIN,
68+
recipeConfig: [
69+
{
70+
"op": "SM2 Decrypt",
71+
"args": [PRIVATE_K, "C1C2C3", CURVE]
72+
}
73+
]
74+
},
75+
{
76+
name: "SM2 Encrypt And Decrypt: Small Input; Format One",
77+
input: SMALL_PLAIN,
78+
expectedOutput: SMALL_PLAIN,
79+
recipeConfig: [
80+
{
81+
"op": "SM2 Encrypt",
82+
"args": [PUBLIC_X, PUBLIC_Y, "C1C3C2", CURVE],
83+
},
84+
{
85+
"op": "SM2 Decrypt",
86+
"args": [PRIVATE_K, "C1C3C2", CURVE]
87+
}
88+
]
89+
},
90+
{
91+
name: "SM2 Encrypt And Decrypt: Large Input; Format One",
92+
input: LARGE_PLAIN,
93+
expectedOutput: LARGE_PLAIN,
94+
recipeConfig: [
95+
{
96+
"op": "SM2 Encrypt",
97+
"args": [PUBLIC_X, PUBLIC_Y, "C1C3C2", CURVE],
98+
},
99+
{
100+
"op": "SM2 Decrypt",
101+
"args": [PRIVATE_K, "C1C3C2", CURVE]
102+
}
103+
]
104+
},
105+
{
106+
name: "SM2 Encrypt And Decrypt: Small Input; Format Two",
107+
input: SMALL_PLAIN,
108+
expectedOutput: SMALL_PLAIN,
109+
recipeConfig: [
110+
{
111+
"op": "SM2 Encrypt",
112+
"args": [PUBLIC_X, PUBLIC_Y, "C1C2C3", CURVE],
113+
},
114+
{
115+
"op": "SM2 Decrypt",
116+
"args": [PRIVATE_K, "C1C2C2", CURVE]
117+
}
118+
]
119+
},
120+
{
121+
name: "SM2 Encrypt And Decrypt: Large Input; Format Two",
122+
input: LARGE_PLAIN,
123+
expectedOutput: LARGE_PLAIN,
124+
recipeConfig: [
125+
{
126+
"op": "SM2 Encrypt",
127+
"args": [PUBLIC_X, PUBLIC_Y, "C1C2C3", CURVE],
128+
},
129+
{
130+
"op": "SM2 Decrypt",
131+
"args": [PRIVATE_K, "C1C2C3", CURVE]
132+
}
133+
]
134+
},
135+
]);

0 commit comments

Comments
 (0)