-
Notifications
You must be signed in to change notification settings - Fork 10
/
browser.js
254 lines (225 loc) · 10.1 KB
/
browser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* global crypto, btoa, atob */
/* ========================================================================== *
* From RFC-4492 (Appendix A) Equivalent Curves (Informative) *
* ========================================================================== *
* *
* +------------------------------------------------------------------------+ *
* | Curve names chosen by | *
* | different standards organizations | *
* +-----------+------------+------------+------------+---------------------+ *
* | SECG | ANSI X9.62 | NIST | OpenSSL | ASN.1 OID | *
* +-----------+------------+------------+------------+---------------------+ *
* | secp256r1 | prime256v1 | NIST P-256 | prime256v1 | 1.2.840.10045.3.1.7 | *
* +-----------+------------+------------+------------+---------------------+ *
* ========================================================================== */
;(function() {
var ALGO = { name: 'ECDSA', namedCurve: 'P-256' }
var SIGN_ALGO = { name: 'ECDSA', hash: { name: 'SHA-256' } }
/* ========================================================================== *
* CLASS DEFINITION *
* ========================================================================== */
function ECDSA(keys /* { publicKey: CryptoKey, privateKey?: CryptoKey } */) {
if (keys.publicKey) this.publicKey = keys.publicKey
if (keys.privateKey) this.privateKey = keys.privateKey
}
/* ========================================================================== *
* UTILS *
* ========================================================================== */
function toBase64(buffer) {
return btoa(buffer)
}
function fromBase64(string) {
return atob(string)
}
function arrayBufferToString(buffer) {
return String.fromCharCode.apply(null, new Uint8Array(buffer))
}
function stringToArrayBuffer(string) {
if (window.TextEncoder) { // Chrome, Firefox, Opera
return new TextEncoder('utf-8').encode(string)
} else {
// TextEncoder polyfill (https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder)
var stringLength = string.length
var buffer = new Uint8Array(stringLength * 3)
var resPos = -1
for (var point = 0, nextcode = 0, i = 0; i !== stringLength;) {
;(point = string.charCodeAt(i)), (i += 1)
if (point >= 0xd800 && point <= 0xdbff) {
if (i === stringLength) {
buffer[(resPos += 1)] = 0xef /* 0b11101111 */
buffer[(resPos += 1)] = 0xbf /* 0b10111111 */
buffer[(resPos += 1)] = 0xbd /* 0b10111101 */
break
}
nextcode = string.charCodeAt(i)
if (nextcode >= 0xdc00 && nextcode <= 0xdfff) {
point = (point - 0xd800) * 0x400 + nextcode - 0xdc00 + 0x10000
i += 1
if (point > 0xffff) {
buffer[(resPos += 1)] = (0x1e /* 0b11110 */ << 3) | (point >>> 18)
buffer[(resPos += 1)] = (0x2 /* 0b10 */ << 6) | ((point >>> 12) & 0x3f) /* 0b00111111 */
buffer[(resPos += 1)] = (0x2 /* 0b10 */ << 6) | ((point >>> 6) & 0x3f) /* 0b00111111 */
buffer[(resPos += 1)] = (0x2 /* 0b10 */ << 6) | (point & 0x3f) /* 0b00111111 */
continue
}
} else {
buffer[(resPos += 1)] = 0xef /* 0b11101111 */
buffer[(resPos += 1)] = 0xbf /* 0b10111111 */
buffer[(resPos += 1)] = 0xbd /* 0b10111101 */
continue
}
}
if (point <= 0x007f) {
buffer[(resPos += 1)] = (0x0 /* 0b0 */ << 7) | point
} else if (point <= 0x07ff) {
buffer[(resPos += 1)] = (0x6 /* 0b110 */ << 5) | (point >>> 6)
buffer[(resPos += 1)] = (0x2 /* 0b10 */ << 6) | (point & 0x3f) /* 0b00111111 */
} else {
buffer[(resPos += 1)] = (0xe /* 0b1110 */ << 4) | (point >>> 12)
buffer[(resPos += 1)] = (0x2 /* 0b10 */ << 6) | ((point >>> 6) & 0x3f) /* 0b00111111 */
buffer[(resPos += 1)] = (0x2 /* 0b10 */ << 6) | (point & 0x3f) /* 0b00111111 */
}
}
buffer = new Uint8Array(buffer.buffer.slice(0, resPos + 1))
return buffer
}
}
function hash(object) {
return new Promise(resolve => {
var buffer = stringToArrayBuffer(typeof object === 'string' ? object : JSON.stringify(object))
crypto.subtle.digest('SHA-256', buffer).then(sha256 => {
resolve(toBase64(arrayBufferToString(sha256)))
})
})
}
/* ========================================================================== *
* FACTORIES *
* ========================================================================== */
ECDSA.generateKey = () => /*: Promise<ECDSA> */ {
return new Promise(resolve => {
crypto.subtle.generateKey(ALGO, true, ['sign', 'verify']).then((key) => {
resolve(new ECDSA(key))
})
})
}
ECDSA.fromJWK = (jwk /*: Object */) => /*: Promise<ECDSA> */ {
return new Promise(resolve => {
var publicJwk = { kty: jwk.kty, crv: jwk.crv, x: jwk.x, y: jwk.y }
var keyPromises = [
crypto.subtle.importKey('jwk', publicJwk, ALGO, true, ['verify'])
]
if (jwk.d) {
var privateJwk = { kty: jwk.kty, crv: jwk.crv, d: jwk.d, x: jwk.x, y: jwk.y }
keyPromises.push(crypto.subtle.importKey('jwk', privateJwk, ALGO, true, ['sign']))
}
Promise.all(keyPromises).then(keys => {
resolve(new ECDSA({ publicKey: keys[0], privateKey: keys[1] }))
})
})
}
ECDSA.fromCompressedPublicKey = (base64Key /*: string */) => /*: Promise<ECDSA> */ {
return new Promise(resolve => {
var rawCompressedKey = stringToArrayBuffer(fromBase64(base64Key))
crypto.subtle.importKey('raw', rawCompressedKey, ALGO, true, ['verify']).then((key) => {
resolve(new ECDSA({ publicKey: key }))
})
})
}
ECDSA.fromBase64PrivateKey = (base64Key /*: string */) => /*: Promise<ECDSA> */ {
return new Promise(resolve => {
var pkcs8Key = stringToArrayBuffer(fromBase64(base64Key))
crypto.subtle.importKey('pkcs8', pkcs8Key, ALGO, true, ['sign']).then((key) => {
resolve(new ECDSA({ privateKey: key }))
})
})
}
/* ========================================================================== *
* SIGNING / VALIDATION *
* ========================================================================== */
ECDSA.prototype.sign = function sign(message /*: string */) /*: Promise<string> */ {
return new Promise(resolve => {
crypto.subtle.sign(SIGN_ALGO, this.privateKey, stringToArrayBuffer(message)).then(signature => {
resolve(toBase64(arrayBufferToString(signature)))
})
})
}
ECDSA.prototype.hashAndSign = function hashAndSign(message /*: string | Object */) /*: Promise<string> */ {
return new Promise(resolve => {
hash(message).then(hashed => {
resolve(this.sign(hashed))
})
})
}
ECDSA.prototype.verify = function verify(message /*: string */, signature /*: string */) /*: Promise<Boolean> */ {
var signatureBuffer = stringToArrayBuffer(fromBase64(signature))
return crypto.subtle.verify(
SIGN_ALGO,
this.publicKey,
signatureBuffer,
stringToArrayBuffer(message)
)
}
ECDSA.prototype.hashAndVerify = function hashAndVerify(message /*: string | Object */, signature /*: string */) /*: Promise<Boolean> */ {
return new Promise(resolve => {
hash(message).then(hashed => {
resolve(this.verify(hashed , signature))
})
})
}
/* ========================================================================== *
* CONVERSION *
* ========================================================================== */
ECDSA.prototype.asPublic = function asPublic() {
if (!this.privateKey) return this
return new ECDSA({ publicKey: this.publicKey })
}
ECDSA.prototype.toJWK = function toJWK() /*: Promise<Object> */ {
return new Promise(resolve => {
var key = this.privateKey ? this.privateKey : this.publicKey
crypto.subtle.exportKey('jwk', key).then(jwk => {
resolve({ kty: jwk.kty, crv: jwk.crv, d: jwk.d, x: jwk.x, y: jwk.y })
})
})
}
ECDSA.prototype.toBase64PrivateKey = function toBase64PrivateKey() /*: Promise<string> */ {
return new Promise(resolve => {
crypto.subtle.exportKey('pkcs8', this.privateKey).then(key => {
resolve(toBase64(arrayBufferToString(key)))
})
})
}
ECDSA.prototype.toCompressedPublicKey = function toCompressedPublicKey() /*: Promise<Uint8Array> */ {
return new Promise(resolve => {
crypto.subtle.exportKey('raw', this.publicKey).then(key => {
var rawKey = new Uint8Array(key)
var x = new Uint8Array(rawKey.slice(1, rawKey.length / 2 + 1))
var y = new Uint8Array(rawKey.slice(rawKey.length / 2 + 1))
var compressedKey = new Uint8Array(x.length + 1)
compressedKey[0] = 2 + (y[y.length - 1] & 1)
compressedKey.set(x, 1)
resolve(compressedKey)
})
})
}
ECDSA.prototype.toBase64CompressedPublicKey = function toBase64CompressedPublicKey() /*: Promise<string> */ {
return new Promise(resolve => {
this.toCompressedPublicKey().then(compressedKey => {
resolve(toBase64(arrayBufferToString(compressedKey)))
})
})
}
/* ========================================================================== *
* EXPORTS *
* ========================================================================== */
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = ECDSA
} else {
if (typeof define === 'function' && define.amd) {
define([], function() {
return ECDSA
})
} else {
window.ECDSA = ECDSA
}
}
})()