Skip to content

Commit f61bdf0

Browse files
committed
Additional linter corrections
1 parent 0f16fa0 commit f61bdf0

File tree

4 files changed

+74
-83
lines changed

4 files changed

+74
-83
lines changed

src/core/lib/SM2.mjs

+52-52
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export class SM2 {
1919
/**
2020
* Constructor for SM2 class; sets up with the curve and the output format as specified in user args
2121
*
22-
* @param {*} curve
23-
* @param {*} format
22+
* @param {*} curve
23+
* @param {*} format
2424
*/
2525
constructor(curve, format) {
2626
this.ecParams = null;
@@ -39,7 +39,7 @@ export class SM2 {
3939
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", // gx
4040
"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", // gy
4141
[]
42-
) // alias
42+
); // alias
4343
this.ecParams = r.crypto.ECParameterDB.getByName(curve);
4444

4545
this.format = format;
@@ -79,15 +79,15 @@ export class SM2 {
7979
* @returns {string}
8080
*/
8181
encrypt(input) {
82-
const G = this.ecParams.G
82+
const G = this.ecParams.G;
8383

8484
/*
8585
* Compute a new, random public key along the same elliptic curve to form the starting point for our encryption process (record the resulting X and Y as hex to provide as part of the operation output)
8686
* k: Randomly generated BigInteger
8787
* c1: Result of dotting our curve generator point `G` with the value of `k`
8888
*/
89-
var k = this.generatePublicKey();
90-
var c1 = G.multiply(k);
89+
const k = this.generatePublicKey();
90+
const c1 = G.multiply(k);
9191
const [hexC1X, hexC1Y] = this.getPointAsHex(c1);
9292

9393
/*
@@ -98,21 +98,21 @@ export class SM2 {
9898
/*
9999
* Compute the C3 SM3 hash before we transform the array
100100
*/
101-
var c3 = this.c3(p2, input);
101+
const c3 = this.c3(p2, input);
102102

103103
/*
104104
* Genreate a proper length encryption key, XOR iteratively, and convert newly encrypted data to hex
105105
*/
106-
var key = this.kdf(p2, input.byteLength);
106+
const key = this.kdf(p2, input.byteLength);
107107
for (let i = 0; i < input.byteLength; i++) {
108108
input[i] ^= Utils.ord(key[i]);
109109
}
110-
var c2 = Buffer.from(input).toString('hex');
110+
const c2 = Buffer.from(input).toString("hex");
111111

112112
/*
113113
* Check user input specs; order the output components as selected
114114
*/
115-
if (this.format == "C1C3C2") {
115+
if (this.format === "C1C3C2") {
116116
return hexC1X + hexC1Y + c3 + c2;
117117
} else {
118118
return hexC1X + hexC1Y + c2 + c3;
@@ -124,37 +124,37 @@ export class SM2 {
124124
* @param {*} input
125125
*/
126126
decrypt(input) {
127-
var c1X = input.slice(0, 64);
128-
var c1Y = input.slice(64, 128);
127+
const c1X = input.slice(0, 64);
128+
const c1Y = input.slice(64, 128);
129129

130-
var c3 = ""
131-
var c2 = ""
130+
let c3 = "";
131+
let c2 = "";
132132

133-
if (this.format == "C1C3C2") {
134-
c3 = input.slice(128,192);
133+
if (this.format === "C1C3C2") {
134+
c3 = input.slice(128, 192);
135135
c2 = input.slice(192);
136136
} else {
137137
c2 = input.slice(128, -64);
138138
c3 = input.slice(-64);
139139
}
140-
c2 = Uint8Array.from(fromHex(c2))
141-
var c1 = this.ecParams.curve.decodePointHex("04" + c1X + c1Y);
140+
c2 = Uint8Array.from(fromHex(c2));
141+
const c1 = this.ecParams.curve.decodePointHex("04" + c1X + c1Y);
142142

143143
/*
144144
* Compute the p2 (secret) value by taking the C1 point provided in the encrypted package, and multiplying by the private k value
145145
*/
146-
var p2 = c1.multiply(this.privateKey);
146+
const p2 = c1.multiply(this.privateKey);
147147

148148
/*
149149
* Similar to encryption; compute sufficient length key material and XOR the input data to recover the original message
150150
*/
151-
var key = this.kdf(p2, c2.byteLength);
151+
const key = this.kdf(p2, c2.byteLength);
152152

153153
for (let i = 0; i < c2.byteLength; i++) {
154154
c2[i] ^= Utils.ord(key[i]);
155155
}
156156

157-
var check = this.c3(p2, c2);
157+
const check = this.c3(p2, c2);
158158
if (check === c3) {
159159
return c2.buffer;
160160
} else {
@@ -165,9 +165,9 @@ export class SM2 {
165165

166166
/**
167167
* Generates a large random number
168-
*
169-
* @param {*} limit
170-
* @returns
168+
*
169+
* @param {*} limit
170+
* @returns
171171
*/
172172
getBigRandom(limit) {
173173
return new r.BigInteger(limit.bitLength(), this.rng)
@@ -177,82 +177,82 @@ export class SM2 {
177177

178178
/**
179179
* Helper function for generating a large random K number; utilized for generating our initial C1 point
180-
* TODO: Do we need to do any sort of validation on the resulting k values?
181-
*
180+
* TODO: Do we need to do any sort of validation on the resulting k values?
181+
*
182182
* @returns {BigInteger}
183183
*/
184184
generatePublicKey() {
185185
const n = this.ecParams.n;
186-
var k = this.getBigRandom(n);
186+
const k = this.getBigRandom(n);
187187
return k;
188188
}
189189

190190
/**
191191
* SM2 Key Derivation Function (KDF); Takes P2 point, and generates a key material stream large enough to encrypt all of the input data
192-
*
193-
* @param {*} p2
194-
* @param {*} len
192+
*
193+
* @param {*} p2
194+
* @param {*} len
195195
* @returns {string}
196196
*/
197197
kdf(p2, len) {
198198
const [hX, hY] = this.getPointAsHex(p2);
199199

200-
var total = Math.ceil(len / 32) + 1;
201-
var cnt = 1;
200+
const total = Math.ceil(len / 32) + 1;
201+
let cnt = 1;
202202

203-
var keyMaterial = ""
203+
let keyMaterial = "";
204204

205205
while (cnt < total) {
206-
var num = Utils.intToByteArray(cnt, 4, "big");
207-
var overall = fromHex(hX).concat(fromHex(hY)).concat(num)
206+
const num = Utils.intToByteArray(cnt, 4, "big");
207+
const overall = fromHex(hX).concat(fromHex(hY)).concat(num);
208208
keyMaterial += this.sm3(overall);
209209
cnt++;
210210
}
211-
return keyMaterial
211+
return keyMaterial;
212212
}
213213

214214
/**
215215
* Calculates the C3 component of our final encrypted payload; which is the SM3 hash of the P2 point and the original, unencrypted input data
216-
*
217-
* @param {*} p2
218-
* @param {*} input
219-
* @returns {string}
216+
*
217+
* @param {*} p2
218+
* @param {*} input
219+
* @returns {string}
220220
*/
221221
c3(p2, input) {
222222
const [hX, hY] = this.getPointAsHex(p2);
223223

224-
var overall = fromHex(hX).concat(Array.from(input)).concat(fromHex(hY));
224+
const overall = fromHex(hX).concat(Array.from(input)).concat(fromHex(hY));
225225

226226
return toHex(this.sm3(overall));
227227

228228
}
229229

230230
/**
231231
* SM3 setup helper function; takes input data as an array, processes the hash and returns the result
232-
*
233-
* @param {*} data
232+
*
233+
* @param {*} data
234234
* @returns {string}
235235
*/
236236
sm3(data) {
237-
var hashData = Utils.arrayBufferToStr(Uint8Array.from(data).buffer, false);
237+
const hashData = Utils.arrayBufferToStr(Uint8Array.from(data).buffer, false);
238238
const hasher = new Sm3();
239239
hasher.update(hashData);
240240
return hasher.finalize();
241241
}
242242

243243
/**
244244
* Utility function, returns an elliptic curve points X and Y values as hex;
245-
*
245+
*
246246
* @param {EcPointFp} point
247247
* @returns {[]}
248248
*/
249249
getPointAsHex(point) {
250-
var biX = point.getX().toBigInteger();
251-
var biY = point.getY().toBigInteger();
250+
const biX = point.getX().toBigInteger();
251+
const biY = point.getY().toBigInteger();
252252

253-
var charlen = this.ecParams.keycharlen;
254-
var hX = ("0000000000" + biX.toString(16)).slice(- charlen);
255-
var hY = ("0000000000" + biY.toString(16)).slice(- charlen);
256-
return [hX, hY]
253+
const charlen = this.ecParams.keycharlen;
254+
const hX = ("0000000000" + biX.toString(16)).slice(- charlen);
255+
const hY = ("0000000000" + biY.toString(16)).slice(- charlen);
256+
return [hX, hY];
257257
}
258-
}
258+
}

src/core/operations/SM2Decrypt.mjs

+3-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
*/
66

77
import Operation from "../Operation.mjs";
8-
import OperationError from "../errors/OperationError.mjs";
98

109
import { SM2 } from "../lib/SM2.mjs";
1110

@@ -55,12 +54,11 @@ class SM2Decrypt extends Operation {
5554
run(input, args) {
5655
const [privateKey, inputFormat, curveName] = args;
5756

58-
var sm2 = new SM2(curveName, inputFormat);
57+
const sm2 = new SM2(curveName, inputFormat);
5958
sm2.setPrivateKey(privateKey);
6059

61-
62-
var result = sm2.decrypt(input);
63-
return result
60+
const result = sm2.decrypt(input);
61+
return result;
6462
}
6563

6664
}

src/core/operations/SM2Encrypt.mjs

+8-15
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,9 @@
55
*/
66

77
import Operation from "../Operation.mjs";
8-
import OperationError from "../errors/OperationError.mjs";
98

109
import { SM2 } from "../lib/SM2.mjs";
1110

12-
import { fromHex } from "../lib/Hex.mjs";
13-
import Utils from "../Utils.mjs";
14-
import Sm3 from "crypto-api/src/hasher/sm3.mjs";
15-
import {toHex} from "crypto-api/src/encoder/hex.mjs";
16-
import r from "jsrsasign";
17-
1811
/**
1912
* SM2 Encrypt operation
2013
*/
@@ -68,11 +61,11 @@ class SM2Encrypt extends Operation {
6861
const [publicKeyX, publicKeyY, outputFormat, curveName] = args;
6962
this.outputFormat = outputFormat;
7063

71-
var sm2 = new SM2(curveName, outputFormat);
64+
const sm2 = new SM2(curveName, outputFormat);
7265
sm2.setPublicKey(publicKeyX, publicKeyY);
7366

74-
var result = sm2.encrypt(new Uint8Array(input))
75-
return result
67+
const result = sm2.encrypt(new Uint8Array(input));
68+
return result;
7669
}
7770

7871
/**
@@ -85,11 +78,11 @@ class SM2Encrypt extends Operation {
8578
* @returns {Object[]} pos
8679
*/
8780
highlight(pos, args) {
88-
const [privateKeyX, privateKeyY, outputFormat, curveName] = args;
89-
var num = pos[0].end - pos[0].start
90-
var adjust = 128
91-
if (outputFormat == "C1C3C2") {
92-
adjust = 192
81+
const outputFormat = args[2];
82+
const num = pos[0].end - pos[0].start;
83+
let adjust = 128;
84+
if (outputFormat === "C1C3C2") {
85+
adjust = 192;
9386
}
9487
pos[0].start = Math.ceil(pos[0].start + adjust);
9588
pos[0].end = Math.floor(pos[0].end + adjust + num);

tests/operations/tests/SM2.mjs

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* SM2 Tests
3-
*
3+
*
44
* @author flakjacket95 [[email protected]]
55
* @copyright Crown Copyright 2024
66
* @license Apache-2.0
@@ -9,22 +9,22 @@ import TestRegister from "../../lib/TestRegister.mjs";
99

1010
/* Plaintexts */
1111

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"
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";
1414

1515
/* Test Key Parameters */
16-
const PUBLIC_X = "f7d903cab7925066c31150a92b31e548e63f954f92d01eaa0271fb2a336baef8"
17-
const PUBLIC_Y = "fb0c45e410ef7a6cdae724e6a78dbff52562e97ede009e762b667d9b14adea6c"
18-
const PRIVATE_K = "e74a72505084c3269aa9b696d603e3e08c74c6740212c11a31e26cdfe08bdf6a"
16+
const PUBLIC_X = "f7d903cab7925066c31150a92b31e548e63f954f92d01eaa0271fb2a336baef8";
17+
const PUBLIC_Y = "fb0c45e410ef7a6cdae724e6a78dbff52562e97ede009e762b667d9b14adea6c";
18+
const PRIVATE_K = "e74a72505084c3269aa9b696d603e3e08c74c6740212c11a31e26cdfe08bdf6a";
1919

20-
const CURVE = "sm2p256v1"
20+
const CURVE = "sm2p256v1";
2121

2222
/* Decryption Test Ciphertext*/
2323

24-
const CIPHERTEXT_1 = "9a31bc0adb4677cdc4141479e3949572a55c3e6fb52094721f741c2bd2e179aaa87be6263bc1be602e473be3d5de5dce97f8248948b3a7e15f9f67f64aef21575e0c05e6171870a10ff9ab778dbef24267ad90e1a9d47d68f757d57c4816612e9829f804025dea05a511cda39371c22a2828f976f72e"
25-
const CIPHERTEXT_2 = "d3647d68568a2e7a4f8e843286be7bf2b4d80256697d19a73df306ae1a7e6d0364d942e23d2340606e7a2502a838b132f9242587b2ea7e4c207e87242eea8cae68f5ff4da2a95a7f6d350608ae5b6777e1d925bf9c560087af84aba7befba713130106ddb4082d803811bca3864594722f3198d58257fe4ba37f4aa540adf4cb0568bddd2d8140ad3030deea0a87e3198655cc4d22bfc3d73b1c4afec2ff15d68c8d1298d97132cace922ee8a4e41ca288a7e748b77ca94aa81dc283439923ae7939e00898e16fe5111fbe1d928d152b216a"
26-
const CIPHERTEXT_3 = "5f340eeb4398fa8950ee3408d0e3fe34bf7728c9fdb060c94b916891b5c693610274160b52a7132a2bf16ad5cdb57d1e00da2f3ddbd55350729aa9c268b53e40c05ccce9912daa14406e8c132e389484e69757350be25351755dcc6c25c94b3c1a448b2cf8c2017582125eb6cf782055b199a875e966"
27-
const CIPHERTEXT_4 = "0649bac46c3f9fd7fb3b2be4bff27414d634651efd02ca67d8c802bbc5468e77d035c39b581d6b56227f5d87c0b4efbea5032c0761139295ae194b9f1fce698f2f4b51d89fa5554171a1aad2e61fe9de89831aec472ecc5ab178ebf4d2230c1fb94fca03e536b87b9eba6db71ba9939260a08ffd230ca86cb45cf754854222364231bdb8b873791d63ad57a4b3fa5b6375388dc879373f5f1be9051bc5072a8afbec5b7b034e4907aa5bb4b6b1f50e725d09cb6a02e07ce20263005f6c9157ce05d3ea739d231d4f09396fb72aa680884d78"
24+
const CIPHERTEXT_1 = "9a31bc0adb4677cdc4141479e3949572a55c3e6fb52094721f741c2bd2e179aaa87be6263bc1be602e473be3d5de5dce97f8248948b3a7e15f9f67f64aef21575e0c05e6171870a10ff9ab778dbef24267ad90e1a9d47d68f757d57c4816612e9829f804025dea05a511cda39371c22a2828f976f72e";
25+
const CIPHERTEXT_2 = "d3647d68568a2e7a4f8e843286be7bf2b4d80256697d19a73df306ae1a7e6d0364d942e23d2340606e7a2502a838b132f9242587b2ea7e4c207e87242eea8cae68f5ff4da2a95a7f6d350608ae5b6777e1d925bf9c560087af84aba7befba713130106ddb4082d803811bca3864594722f3198d58257fe4ba37f4aa540adf4cb0568bddd2d8140ad3030deea0a87e3198655cc4d22bfc3d73b1c4afec2ff15d68c8d1298d97132cace922ee8a4e41ca288a7e748b77ca94aa81dc283439923ae7939e00898e16fe5111fbe1d928d152b216a";
26+
const CIPHERTEXT_3 = "5f340eeb4398fa8950ee3408d0e3fe34bf7728c9fdb060c94b916891b5c693610274160b52a7132a2bf16ad5cdb57d1e00da2f3ddbd55350729aa9c268b53e40c05ccce9912daa14406e8c132e389484e69757350be25351755dcc6c25c94b3c1a448b2cf8c2017582125eb6cf782055b199a875e966";
27+
const CIPHERTEXT_4 = "0649bac46c3f9fd7fb3b2be4bff27414d634651efd02ca67d8c802bbc5468e77d035c39b581d6b56227f5d87c0b4efbea5032c0761139295ae194b9f1fce698f2f4b51d89fa5554171a1aad2e61fe9de89831aec472ecc5ab178ebf4d2230c1fb94fca03e536b87b9eba6db71ba9939260a08ffd230ca86cb45cf754854222364231bdb8b873791d63ad57a4b3fa5b6375388dc879373f5f1be9051bc5072a8afbec5b7b034e4907aa5bb4b6b1f50e725d09cb6a02e07ce20263005f6c9157ce05d3ea739d231d4f09396fb72aa680884d78";
2828

2929

3030
TestRegister.addTests([

0 commit comments

Comments
 (0)