@@ -19,8 +19,8 @@ export class SM2 {
19
19
/**
20
20
* Constructor for SM2 class; sets up with the curve and the output format as specified in user args
21
21
*
22
- * @param {* } curve
23
- * @param {* } format
22
+ * @param {* } curve
23
+ * @param {* } format
24
24
*/
25
25
constructor ( curve , format ) {
26
26
this . ecParams = null ;
@@ -39,7 +39,7 @@ export class SM2 {
39
39
"32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7" , // gx
40
40
"BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0" , // gy
41
41
[ ]
42
- ) // alias
42
+ ) ; // alias
43
43
this . ecParams = r . crypto . ECParameterDB . getByName ( curve ) ;
44
44
45
45
this . format = format ;
@@ -79,15 +79,15 @@ export class SM2 {
79
79
* @returns {string }
80
80
*/
81
81
encrypt ( input ) {
82
- const G = this . ecParams . G
82
+ const G = this . ecParams . G ;
83
83
84
84
/*
85
85
* 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)
86
86
* k: Randomly generated BigInteger
87
87
* c1: Result of dotting our curve generator point `G` with the value of `k`
88
88
*/
89
- var k = this . generatePublicKey ( ) ;
90
- var c1 = G . multiply ( k ) ;
89
+ const k = this . generatePublicKey ( ) ;
90
+ const c1 = G . multiply ( k ) ;
91
91
const [ hexC1X , hexC1Y ] = this . getPointAsHex ( c1 ) ;
92
92
93
93
/*
@@ -98,21 +98,21 @@ export class SM2 {
98
98
/*
99
99
* Compute the C3 SM3 hash before we transform the array
100
100
*/
101
- var c3 = this . c3 ( p2 , input ) ;
101
+ const c3 = this . c3 ( p2 , input ) ;
102
102
103
103
/*
104
104
* Genreate a proper length encryption key, XOR iteratively, and convert newly encrypted data to hex
105
105
*/
106
- var key = this . kdf ( p2 , input . byteLength ) ;
106
+ const key = this . kdf ( p2 , input . byteLength ) ;
107
107
for ( let i = 0 ; i < input . byteLength ; i ++ ) {
108
108
input [ i ] ^= Utils . ord ( key [ i ] ) ;
109
109
}
110
- var c2 = Buffer . from ( input ) . toString ( ' hex' ) ;
110
+ const c2 = Buffer . from ( input ) . toString ( " hex" ) ;
111
111
112
112
/*
113
113
* Check user input specs; order the output components as selected
114
114
*/
115
- if ( this . format == "C1C3C2" ) {
115
+ if ( this . format === "C1C3C2" ) {
116
116
return hexC1X + hexC1Y + c3 + c2 ;
117
117
} else {
118
118
return hexC1X + hexC1Y + c2 + c3 ;
@@ -124,37 +124,37 @@ export class SM2 {
124
124
* @param {* } input
125
125
*/
126
126
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 ) ;
129
129
130
- var c3 = ""
131
- var c2 = ""
130
+ let c3 = "" ;
131
+ let c2 = "" ;
132
132
133
- if ( this . format == "C1C3C2" ) {
134
- c3 = input . slice ( 128 , 192 ) ;
133
+ if ( this . format === "C1C3C2" ) {
134
+ c3 = input . slice ( 128 , 192 ) ;
135
135
c2 = input . slice ( 192 ) ;
136
136
} else {
137
137
c2 = input . slice ( 128 , - 64 ) ;
138
138
c3 = input . slice ( - 64 ) ;
139
139
}
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 ) ;
142
142
143
143
/*
144
144
* Compute the p2 (secret) value by taking the C1 point provided in the encrypted package, and multiplying by the private k value
145
145
*/
146
- var p2 = c1 . multiply ( this . privateKey ) ;
146
+ const p2 = c1 . multiply ( this . privateKey ) ;
147
147
148
148
/*
149
149
* Similar to encryption; compute sufficient length key material and XOR the input data to recover the original message
150
150
*/
151
- var key = this . kdf ( p2 , c2 . byteLength ) ;
151
+ const key = this . kdf ( p2 , c2 . byteLength ) ;
152
152
153
153
for ( let i = 0 ; i < c2 . byteLength ; i ++ ) {
154
154
c2 [ i ] ^= Utils . ord ( key [ i ] ) ;
155
155
}
156
156
157
- var check = this . c3 ( p2 , c2 ) ;
157
+ const check = this . c3 ( p2 , c2 ) ;
158
158
if ( check === c3 ) {
159
159
return c2 . buffer ;
160
160
} else {
@@ -165,9 +165,9 @@ export class SM2 {
165
165
166
166
/**
167
167
* Generates a large random number
168
- *
169
- * @param {* } limit
170
- * @returns
168
+ *
169
+ * @param {* } limit
170
+ * @returns
171
171
*/
172
172
getBigRandom ( limit ) {
173
173
return new r . BigInteger ( limit . bitLength ( ) , this . rng )
@@ -177,82 +177,82 @@ export class SM2 {
177
177
178
178
/**
179
179
* 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
+ *
182
182
* @returns {BigInteger }
183
183
*/
184
184
generatePublicKey ( ) {
185
185
const n = this . ecParams . n ;
186
- var k = this . getBigRandom ( n ) ;
186
+ const k = this . getBigRandom ( n ) ;
187
187
return k ;
188
188
}
189
189
190
190
/**
191
191
* 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
195
195
* @returns {string }
196
196
*/
197
197
kdf ( p2 , len ) {
198
198
const [ hX , hY ] = this . getPointAsHex ( p2 ) ;
199
199
200
- var total = Math . ceil ( len / 32 ) + 1 ;
201
- var cnt = 1 ;
200
+ const total = Math . ceil ( len / 32 ) + 1 ;
201
+ let cnt = 1 ;
202
202
203
- var keyMaterial = ""
203
+ let keyMaterial = "" ;
204
204
205
205
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 ) ;
208
208
keyMaterial += this . sm3 ( overall ) ;
209
209
cnt ++ ;
210
210
}
211
- return keyMaterial
211
+ return keyMaterial ;
212
212
}
213
213
214
214
/**
215
215
* 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 }
220
220
*/
221
221
c3 ( p2 , input ) {
222
222
const [ hX , hY ] = this . getPointAsHex ( p2 ) ;
223
223
224
- var overall = fromHex ( hX ) . concat ( Array . from ( input ) ) . concat ( fromHex ( hY ) ) ;
224
+ const overall = fromHex ( hX ) . concat ( Array . from ( input ) ) . concat ( fromHex ( hY ) ) ;
225
225
226
226
return toHex ( this . sm3 ( overall ) ) ;
227
227
228
228
}
229
229
230
230
/**
231
231
* 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
234
234
* @returns {string }
235
235
*/
236
236
sm3 ( data ) {
237
- var hashData = Utils . arrayBufferToStr ( Uint8Array . from ( data ) . buffer , false ) ;
237
+ const hashData = Utils . arrayBufferToStr ( Uint8Array . from ( data ) . buffer , false ) ;
238
238
const hasher = new Sm3 ( ) ;
239
239
hasher . update ( hashData ) ;
240
240
return hasher . finalize ( ) ;
241
241
}
242
242
243
243
/**
244
244
* Utility function, returns an elliptic curve points X and Y values as hex;
245
- *
245
+ *
246
246
* @param {EcPointFp } point
247
247
* @returns {[] }
248
248
*/
249
249
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 ( ) ;
252
252
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 ] ;
257
257
}
258
- }
258
+ }
0 commit comments