sm-crypto-v2 0.3.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * as sm2 from './sm2/index'
2
+ export * as sm3 from './sm3/index'
3
+ export * as sm4 from './sm4/index'
@@ -0,0 +1,158 @@
1
+ /* eslint-disable class-methods-use-this */
2
+
3
+ import { BigInteger } from 'jsbn'
4
+
5
+ export function bigintToValue(bigint: BigInteger) {
6
+ let h = bigint.toString(16)
7
+ if (h[0] !== '-') {
8
+ // 正数
9
+ if (h.length % 2 === 1) h = '0' + h // 补齐到整字节
10
+ else if (!h.match(/^[0-7]/)) h = '00' + h // 非0开头,则补一个全0字节
11
+ } else {
12
+ // 负数
13
+ h = h.substr(1)
14
+
15
+ let len = h.length
16
+ if (len % 2 === 1) len += 1 // 补齐到整字节
17
+ else if (!h.match(/^[0-7]/)) len += 2 // 非0开头,则补一个全0字节
18
+
19
+ let maskString = ''
20
+ for (let i = 0; i < len; i++) maskString += 'f'
21
+ let mask = new BigInteger(maskString, 16)
22
+
23
+ // 对绝对值取反,加1
24
+ let output = mask.xor(bigint).add(BigInteger.ONE)
25
+ h = output.toString(16).replace(/^-/, '')
26
+ }
27
+ return h
28
+ }
29
+
30
+ class ASN1Object {
31
+ constructor(
32
+ public tlv: string | null = null,
33
+ public t = '00',
34
+ public l = '00',
35
+ public v = ''
36
+ ) { }
37
+
38
+ /**
39
+ * 获取 der 编码比特流16进制串
40
+ */
41
+ getEncodedHex() {
42
+ if (!this.tlv) {
43
+ this.v = this.getValue()
44
+ this.l = this.getLength()
45
+ this.tlv = this.t + this.l + this.v
46
+ }
47
+ return this.tlv
48
+ }
49
+
50
+ getLength() {
51
+ const n = this.v.length / 2 // 字节数
52
+ let nHex = n.toString(16)
53
+ if (nHex.length % 2 === 1) nHex = '0' + nHex // 补齐到整字节
54
+
55
+ if (n < 128) {
56
+ // 短格式,以 0 开头
57
+ return nHex
58
+ } else {
59
+ // 长格式,以 1 开头
60
+ const head = 128 + nHex.length / 2 // 1(1位) + 真正的长度占用字节数(7位) + 真正的长度
61
+ return head.toString(16) + nHex
62
+ }
63
+ }
64
+
65
+ getValue() {
66
+ return ''
67
+ }
68
+ }
69
+
70
+ class DERInteger extends ASN1Object {
71
+ constructor(bigint: BigInteger) {
72
+ super()
73
+
74
+ this.t = '02' // 整型标签说明
75
+ if (bigint) this.v = bigintToValue(bigint)
76
+ }
77
+
78
+ getValue() {
79
+ return this.v
80
+ }
81
+ }
82
+
83
+ class DERSequence extends ASN1Object {
84
+ public t = '30'
85
+ constructor(public asn1Array: ASN1Object[]) {
86
+ super()
87
+ }
88
+
89
+ getValue() {
90
+ this.v = this.asn1Array.map(asn1Object => asn1Object.getEncodedHex()).join('')
91
+ return this.v
92
+ }
93
+ }
94
+
95
+ /**
96
+ * 获取 l 占用字节数
97
+ */
98
+ function getLenOfL(str: string, start: number) {
99
+ if (+str[start + 2] < 8) return 1 // l 以0开头,则表示短格式,只占一个字节
100
+ return +str.substring(start + 2, start + 4) & 0x7f + 1 // 长格式,取第一个字节后7位作为长度真正占用字节数,再加上本身
101
+ }
102
+
103
+ /**
104
+ * 获取 l
105
+ */
106
+ function getL(str: string, start: number) {
107
+ // 获取 l
108
+ const len = getLenOfL(str, start)
109
+ const l = str.substring(start + 2, start + 2 + len * 2)
110
+
111
+ if (!l) return -1
112
+ const bigint = +l[0] < 8 ? new BigInteger(l, 16) : new BigInteger(l.substring(2), 16)
113
+
114
+ return bigint.intValue()
115
+ }
116
+
117
+ /**
118
+ * 获取 v 的位置
119
+ */
120
+ function getStartOfV(str: string, start: number) {
121
+ const len = getLenOfL(str, start)
122
+ return start + (len + 1) * 2
123
+ }
124
+
125
+ /**
126
+ * ASN.1 der 编码,针对 sm2 签名
127
+ */
128
+ export function encodeDer(r: BigInteger, s: BigInteger) {
129
+ const derR = new DERInteger(r)
130
+ const derS = new DERInteger(s)
131
+ const derSeq = new DERSequence([derR, derS])
132
+
133
+ return derSeq.getEncodedHex()
134
+ }
135
+
136
+ /**
137
+ * 解析 ASN.1 der,针对 sm2 验签
138
+ */
139
+ export function decodeDer(input: string) {
140
+ // 结构:
141
+ // input = | tSeq | lSeq | vSeq |
142
+ // vSeq = | tR | lR | vR | tS | lS | vS |
143
+ const start = getStartOfV(input, 0)
144
+
145
+ const vIndexR = getStartOfV(input, start)
146
+ const lR = getL(input, start)
147
+ const vR = input.substr(vIndexR, lR * 2)
148
+
149
+ const nextStart = vIndexR + vR.length
150
+ const vIndexS = getStartOfV(input, nextStart)
151
+ const lS = getL(input, nextStart)
152
+ const vS = input.substring(vIndexS, vIndexS + lS * 2)
153
+
154
+ const r = new BigInteger(vR, 16)
155
+ const s = new BigInteger(vS, 16)
156
+
157
+ return { r, s }
158
+ }
package/src/sm2/ec.ts ADDED
@@ -0,0 +1,333 @@
1
+ /* eslint-disable no-case-declarations, max-len */
2
+
3
+ import { BigInteger } from 'jsbn'
4
+
5
+ /**
6
+ * thanks for Tom Wu : http://www-cs-students.stanford.edu/~tjw/jsbn/
7
+ *
8
+ * Basic Javascript Elliptic Curve implementation
9
+ * Ported loosely from BouncyCastle's Java EC code
10
+ * Only Fp curves implemented for now
11
+ */
12
+
13
+ const TWO = new BigInteger('2')
14
+ const THREE = new BigInteger('3')
15
+
16
+ /**
17
+ * 椭圆曲线域元素
18
+ */
19
+ class ECFieldElementFp {
20
+ constructor(public q: BigInteger, public x: BigInteger) {
21
+ // TODO if (x.compareTo(q) >= 0) error
22
+ }
23
+
24
+ /**
25
+ * 判断相等
26
+ */
27
+ equals(other: ECFieldElementFp) {
28
+ if (other === this) return true
29
+ return (this.q.equals(other.q) && this.x.equals(other.x))
30
+ }
31
+
32
+ /**
33
+ * 返回具体数值
34
+ */
35
+ toBigInteger() {
36
+ return this.x
37
+ }
38
+
39
+ /**
40
+ * 取反
41
+ */
42
+ negate() {
43
+ return new ECFieldElementFp(this.q, this.x.negate().mod(this.q))
44
+ }
45
+
46
+ /**
47
+ * 相加
48
+ */
49
+ add(b) {
50
+ return new ECFieldElementFp(this.q, this.x.add(b.toBigInteger()).mod(this.q))
51
+ }
52
+
53
+ /**
54
+ * 相减
55
+ */
56
+ subtract(b) {
57
+ return new ECFieldElementFp(this.q, this.x.subtract(b.toBigInteger()).mod(this.q))
58
+ }
59
+
60
+ /**
61
+ * 相乘
62
+ */
63
+ multiply(b) {
64
+ return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger()).mod(this.q))
65
+ }
66
+
67
+ /**
68
+ * 相除
69
+ */
70
+ divide(b) {
71
+ return new ECFieldElementFp(this.q, this.x.multiply(b.toBigInteger().modInverse(this.q)).mod(this.q))
72
+ }
73
+
74
+ /**
75
+ * 平方
76
+ */
77
+ square() {
78
+ return new ECFieldElementFp(this.q, this.x.square().mod(this.q))
79
+ }
80
+ }
81
+
82
+ class ECPointFp {
83
+ zinv: BigInteger | null
84
+ z: BigInteger
85
+ constructor(public curve: ECCurveFp, public x: ECFieldElementFp | null, public y: ECFieldElementFp | null, z?: BigInteger) {
86
+ // 标准射影坐标系:zinv == null 或 z * zinv == 1
87
+ this.z = z == null ? BigInteger.ONE : z
88
+ this.zinv = null
89
+ // TODO: compression flag
90
+ }
91
+
92
+ getX() {
93
+ if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q)
94
+
95
+ return this.curve.fromBigInteger(this.x!.toBigInteger().multiply(this.zinv).mod(this.curve.q))
96
+ }
97
+
98
+ getY() {
99
+ if (this.zinv === null) this.zinv = this.z.modInverse(this.curve.q)
100
+
101
+ return this.curve.fromBigInteger(this.y!.toBigInteger().multiply(this.zinv).mod(this.curve.q))
102
+ }
103
+
104
+ /**
105
+ * 判断相等
106
+ */
107
+ equals(other: ECPointFp) {
108
+ if (other === this) return true
109
+ if (this.isInfinity()) return other.isInfinity()
110
+ if (other.isInfinity()) return this.isInfinity()
111
+
112
+ // u = y2 * z1 - y1 * z2
113
+ const u = other.y!.toBigInteger().multiply(this.z).subtract(this.y!.toBigInteger().multiply(other.z)).mod(this.curve.q)
114
+ if (!u.equals(BigInteger.ZERO)) return false
115
+
116
+ // v = x2 * z1 - x1 * z2
117
+ const v = other.x!.toBigInteger().multiply(this.z).subtract(this.x!.toBigInteger().multiply(other.z)).mod(this.curve.q)
118
+ return v.equals(BigInteger.ZERO)
119
+ }
120
+
121
+ /**
122
+ * 是否是无穷远点
123
+ */
124
+ isInfinity() {
125
+ if ((this.x === null) && (this.y === null)) return true
126
+ return this.z.equals(BigInteger.ZERO) && !this.y!.toBigInteger().equals(BigInteger.ZERO)
127
+ }
128
+
129
+ /**
130
+ * 取反,x 轴对称点
131
+ */
132
+ negate() {
133
+ return new ECPointFp(this.curve, this.x, this.y!.negate(), this.z)
134
+ }
135
+
136
+ /**
137
+ * 相加
138
+ *
139
+ * 标准射影坐标系:
140
+ *
141
+ * λ1 = x1 * z2
142
+ * λ2 = x2 * z1
143
+ * λ3 = λ1 − λ2
144
+ * λ4 = y1 * z2
145
+ * λ5 = y2 * z1
146
+ * λ6 = λ4 − λ5
147
+ * λ7 = λ1 + λ2
148
+ * λ8 = z1 * z2
149
+ * λ9 = λ3^2
150
+ * λ10 = λ3 * λ9
151
+ * λ11 = λ8 * λ6^2 − λ7 * λ9
152
+ * x3 = λ3 * λ11
153
+ * y3 = λ6 * (λ9 * λ1 − λ11) − λ4 * λ10
154
+ * z3 = λ10 * λ8
155
+ */
156
+ add(b: ECPointFp) {
157
+ if (this.isInfinity()) return b
158
+ if (b.isInfinity()) return this
159
+
160
+ const x1 = this.x!.toBigInteger()
161
+ const y1 = this.y!.toBigInteger()
162
+ const z1 = this.z
163
+ const x2 = b.x!.toBigInteger()
164
+ const y2 = b.y!.toBigInteger()
165
+ const z2 = b.z
166
+ const q = this.curve.q
167
+
168
+ const w1 = x1.multiply(z2).mod(q)
169
+ const w2 = x2.multiply(z1).mod(q)
170
+ const w3 = w1.subtract(w2)
171
+ const w4 = y1.multiply(z2).mod(q)
172
+ const w5 = y2.multiply(z1).mod(q)
173
+ const w6 = w4.subtract(w5)
174
+
175
+ if (BigInteger.ZERO.equals(w3)) {
176
+ if (BigInteger.ZERO.equals(w6)) {
177
+ return this.twice() // this == b,计算自加
178
+ }
179
+ return this.curve.infinity // this == -b,则返回无穷远点
180
+ }
181
+
182
+ const w7 = w1.add(w2)
183
+ const w8 = z1.multiply(z2).mod(q)
184
+ const w9 = w3.square().mod(q)
185
+ const w10 = w3.multiply(w9).mod(q)
186
+ const w11 = w8.multiply(w6.square()).subtract(w7.multiply(w9)).mod(q)
187
+
188
+ const x3 = w3.multiply(w11).mod(q)
189
+ const y3 = w6.multiply(w9.multiply(w1).subtract(w11)).subtract(w4.multiply(w10)).mod(q)
190
+ const z3 = w10.multiply(w8).mod(q)
191
+
192
+ return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3)
193
+ }
194
+
195
+ /**
196
+ * 自加
197
+ *
198
+ * 标准射影坐标系:
199
+ *
200
+ * λ1 = 3 * x1^2 + a * z1^2
201
+ * λ2 = 2 * y1 * z1
202
+ * λ3 = y1^2
203
+ * λ4 = λ3 * x1 * z1
204
+ * λ5 = λ2^2
205
+ * λ6 = λ1^2 − 8 * λ4
206
+ * x3 = λ2 * λ6
207
+ * y3 = λ1 * (4 * λ4 − λ6) − 2 * λ5 * λ3
208
+ * z3 = λ2 * λ5
209
+ */
210
+ twice() {
211
+ if (this.isInfinity()) return this
212
+ if (!this.y!.toBigInteger().signum()) return this.curve.infinity
213
+
214
+ const x1 = this.x!.toBigInteger()
215
+ const y1 = this.y!.toBigInteger()
216
+ const z1 = this.z
217
+ const q = this.curve.q
218
+ const a = this.curve.a.toBigInteger()
219
+
220
+ const w1 = x1.square().multiply(THREE).add(a.multiply(z1.square())).mod(q)
221
+ const w2 = y1.shiftLeft(1).multiply(z1).mod(q)
222
+ const w3 = y1.square().mod(q)
223
+ const w4 = w3.multiply(x1).multiply(z1).mod(q)
224
+ const w5 = w2.square().mod(q)
225
+ const w6 = w1.square().subtract(w4.shiftLeft(3)).mod(q)
226
+
227
+ const x3 = w2.multiply(w6).mod(q)
228
+ const y3 = w1.multiply(w4.shiftLeft(2).subtract(w6)).subtract(w5.shiftLeft(1).multiply(w3)).mod(q)
229
+ const z3 = w2.multiply(w5).mod(q)
230
+
231
+ return new ECPointFp(this.curve, this.curve.fromBigInteger(x3), this.curve.fromBigInteger(y3), z3)
232
+ }
233
+
234
+ /**
235
+ * 倍点计算
236
+ */
237
+ multiply(k: BigInteger) {
238
+ if (this.isInfinity()) return this
239
+ if (!k.signum()) return this.curve.infinity
240
+
241
+ // 使用加减法
242
+ const k3 = k.multiply(THREE)
243
+ const neg = this.negate()
244
+ let Q: ECPointFp = this
245
+
246
+ for (let i = k3.bitLength() - 2; i > 0; i--) {
247
+ Q = Q.twice()
248
+
249
+ const k3Bit = k3.testBit(i)
250
+ const kBit = k.testBit(i)
251
+
252
+ if (k3Bit !== kBit) {
253
+ Q = Q.add(k3Bit ? this : neg)
254
+ }
255
+ }
256
+
257
+ return Q
258
+ }
259
+ }
260
+
261
+ /**
262
+ * 椭圆曲线 y^2 = x^3 + ax + b
263
+ */
264
+ export class ECCurveFp {
265
+ infinity: ECPointFp
266
+ a: ECFieldElementFp
267
+ b: ECFieldElementFp
268
+ constructor(public q: BigInteger, a: BigInteger, b: BigInteger) {
269
+ this.q = q
270
+ this.a = this.fromBigInteger(a)
271
+ this.b = this.fromBigInteger(b)
272
+ this.infinity = new ECPointFp(this, null, null) // 无穷远点
273
+ }
274
+
275
+ /**
276
+ * 判断两个椭圆曲线是否相等
277
+ */
278
+ equals(other: ECCurveFp) {
279
+ if (other === this) return true
280
+ return (this.q.equals(other.q) && this.a.equals(other.a) && this.b.equals(other.b))
281
+ }
282
+
283
+ /**
284
+ * 生成椭圆曲线域元素
285
+ */
286
+ fromBigInteger(x: BigInteger) {
287
+ return new ECFieldElementFp(this.q, x)
288
+ }
289
+
290
+ /**
291
+ * 解析 16 进制串为椭圆曲线点
292
+ */
293
+ decodePointHex(s: string) {
294
+ switch (parseInt(s.substring(0, 2), 16)) {
295
+ // 第一个字节
296
+ case 0:
297
+ return this.infinity
298
+ case 2:
299
+ case 3:
300
+ // 压缩
301
+ const x = this.fromBigInteger(new BigInteger(s.substring(2), 16))
302
+ // 对 p ≡ 3 (mod4),即存在正整数 u,使得 p = 4u + 3
303
+ // 计算 y = (√ (x^3 + ax + b) % p)^(u + 1) modp
304
+ let y = this.fromBigInteger(x.multiply(x.square()).add(
305
+ x.multiply(this.a)
306
+ ).add(this.b).toBigInteger()
307
+ .modPow(
308
+ this.q.divide(new BigInteger('4')).add(BigInteger.ONE), this.q
309
+ ))
310
+ // 算出结果 2 进制最后 1 位不等于第 1 个字节减 2 则取反
311
+ if (!y.toBigInteger().mod(TWO).equals(new BigInteger(s.substring(0, 2), 16).subtract(TWO))) {
312
+ y = y.negate()
313
+ }
314
+ return new ECPointFp(this, x, y)
315
+ case 4:
316
+ case 6:
317
+ case 7:
318
+ const len = (s.length - 2) / 2
319
+ const xHex = s.substring(2, 2 + len)
320
+ const yHex = s.substring(len + 2, len + 2 + len)
321
+
322
+ return new ECPointFp(this, this.fromBigInteger(new BigInteger(xHex, 16)), this.fromBigInteger(new BigInteger(yHex, 16)))
323
+ default:
324
+ // 不支持
325
+ return null
326
+ }
327
+ }
328
+ }
329
+
330
+ export const ec = {
331
+ ECPointFp,
332
+ ECCurveFp,
333
+ }