@libp2p/crypto 5.0.15 → 5.1.0
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/dist/index.min.js +1 -1
- package/dist/src/keys/ecdsa/ecdsa.d.ts +27 -0
- package/dist/src/keys/ecdsa/ecdsa.d.ts.map +1 -0
- package/dist/src/keys/ecdsa/ecdsa.js +72 -0
- package/dist/src/keys/ecdsa/ecdsa.js.map +1 -0
- package/dist/src/keys/ecdsa/index.d.ts +10 -0
- package/dist/src/keys/ecdsa/index.d.ts.map +1 -0
- package/dist/src/keys/ecdsa/index.js +39 -0
- package/dist/src/keys/ecdsa/index.js.map +1 -0
- package/dist/src/keys/ecdsa/utils.d.ts +11 -0
- package/dist/src/keys/ecdsa/utils.d.ts.map +1 -0
- package/dist/src/keys/ecdsa/utils.js +165 -0
- package/dist/src/keys/ecdsa/utils.js.map +1 -0
- package/dist/src/keys/index.d.ts +13 -3
- package/dist/src/keys/index.d.ts.map +1 -1
- package/dist/src/keys/index.js +101 -11
- package/dist/src/keys/index.js.map +1 -1
- package/dist/src/keys/keys.d.ts +2 -1
- package/dist/src/keys/keys.d.ts.map +1 -1
- package/dist/src/keys/keys.js +2 -0
- package/dist/src/keys/keys.js.map +1 -1
- package/dist/src/keys/rsa/der.d.ts +2 -1
- package/dist/src/keys/rsa/der.d.ts.map +1 -1
- package/dist/src/keys/rsa/der.js +53 -10
- package/dist/src/keys/rsa/der.js.map +1 -1
- package/dist/src/keys/rsa/index.browser.d.ts +1 -0
- package/dist/src/keys/rsa/index.browser.d.ts.map +1 -1
- package/dist/src/keys/rsa/index.browser.js +1 -0
- package/dist/src/keys/rsa/index.browser.js.map +1 -1
- package/dist/src/keys/rsa/index.d.ts +1 -0
- package/dist/src/keys/rsa/index.d.ts.map +1 -1
- package/dist/src/keys/rsa/index.js +1 -0
- package/dist/src/keys/rsa/index.js.map +1 -1
- package/dist/src/keys/rsa/rsa.d.ts +4 -4
- package/dist/src/keys/rsa/rsa.d.ts.map +1 -1
- package/dist/src/keys/rsa/rsa.js +10 -10
- package/dist/src/keys/rsa/rsa.js.map +1 -1
- package/dist/src/keys/rsa/utils.d.ts +12 -2
- package/dist/src/keys/rsa/utils.d.ts.map +1 -1
- package/dist/src/keys/rsa/utils.js +41 -16
- package/dist/src/keys/rsa/utils.js.map +1 -1
- package/dist/src/keys/secp256k1/index.browser.d.ts +4 -0
- package/dist/src/keys/secp256k1/index.browser.d.ts.map +1 -1
- package/dist/src/keys/secp256k1/index.browser.js +4 -0
- package/dist/src/keys/secp256k1/index.browser.js.map +1 -1
- package/dist/src/keys/secp256k1/index.d.ts +4 -0
- package/dist/src/keys/secp256k1/index.d.ts.map +1 -1
- package/dist/src/keys/secp256k1/index.js +4 -0
- package/dist/src/keys/secp256k1/index.js.map +1 -1
- package/dist/typedoc-urls.json +4 -0
- package/package.json +2 -2
- package/src/keys/ecdsa/ecdsa.ts +91 -0
- package/src/keys/ecdsa/index.ts +50 -0
- package/src/keys/ecdsa/utils.ts +212 -0
- package/src/keys/index.ts +132 -15
- package/src/keys/keys.proto +1 -0
- package/src/keys/keys.ts +4 -2
- package/src/keys/rsa/der.ts +68 -11
- package/src/keys/rsa/index.browser.ts +1 -0
- package/src/keys/rsa/index.ts +2 -0
- package/src/keys/rsa/rsa.ts +10 -10
- package/src/keys/rsa/utils.ts +48 -16
- package/src/keys/secp256k1/index.browser.ts +6 -0
- package/src/keys/secp256k1/index.ts +6 -0
package/src/keys/rsa/der.ts
CHANGED
|
@@ -13,8 +13,11 @@ interface Decoder {
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
const decoders: Record<number, Decoder> = {
|
|
16
|
+
0x0: readSequence,
|
|
17
|
+
0x1: readSequence,
|
|
16
18
|
0x2: readInteger,
|
|
17
19
|
0x3: readBitString,
|
|
20
|
+
0x4: readOctetString,
|
|
18
21
|
0x5: readNull,
|
|
19
22
|
0x6: readObjectIdentifier,
|
|
20
23
|
0x10: readSequence,
|
|
@@ -96,13 +99,53 @@ function readInteger (buf: Uint8Array, context: Context): Uint8Array {
|
|
|
96
99
|
return Uint8Array.from(vals)
|
|
97
100
|
}
|
|
98
101
|
|
|
99
|
-
function readObjectIdentifier (buf: Uint8Array, context: Context): string
|
|
102
|
+
function readObjectIdentifier (buf: Uint8Array, context: Context): string {
|
|
100
103
|
const count = readLength(buf, context)
|
|
104
|
+
const finalOffset = context.offset + count
|
|
101
105
|
|
|
102
|
-
|
|
103
|
-
context.offset
|
|
106
|
+
const byte = buf[context.offset]
|
|
107
|
+
context.offset++
|
|
108
|
+
|
|
109
|
+
let val1 = 0
|
|
110
|
+
let val2 = 0
|
|
111
|
+
|
|
112
|
+
if (byte < 40) {
|
|
113
|
+
val1 = 0
|
|
114
|
+
val2 = byte
|
|
115
|
+
} else if (byte < 80) {
|
|
116
|
+
val1 = 1
|
|
117
|
+
val2 = byte - 40
|
|
118
|
+
} else {
|
|
119
|
+
val1 = 2
|
|
120
|
+
val2 = byte - 80
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
let oid = `${val1}.${val2}`
|
|
124
|
+
let num: number[] = []
|
|
125
|
+
|
|
126
|
+
while (context.offset < finalOffset) {
|
|
127
|
+
const byte = buf[context.offset]
|
|
128
|
+
context.offset++
|
|
129
|
+
|
|
130
|
+
// remove msb
|
|
131
|
+
num.push(byte & 0b01111111)
|
|
132
|
+
|
|
133
|
+
if (byte < 128) {
|
|
134
|
+
num.reverse()
|
|
135
|
+
|
|
136
|
+
// reached the end of the encoding
|
|
137
|
+
let val = 0
|
|
104
138
|
|
|
105
|
-
|
|
139
|
+
for (let i = 0; i < num.length; i++) {
|
|
140
|
+
val += num[i] << (i * 7)
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
oid += `.${val}`
|
|
144
|
+
num = []
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return oid
|
|
106
149
|
}
|
|
107
150
|
|
|
108
151
|
function readNull (buf: Uint8Array, context: Context): null {
|
|
@@ -115,7 +158,7 @@ function readBitString (buf: Uint8Array, context: Context): any {
|
|
|
115
158
|
const length = readLength(buf, context)
|
|
116
159
|
const unusedBits = buf[context.offset]
|
|
117
160
|
context.offset++
|
|
118
|
-
const bytes = buf.subarray(context.offset, context.offset + length)
|
|
161
|
+
const bytes = buf.subarray(context.offset, context.offset + length - 1)
|
|
119
162
|
context.offset += length
|
|
120
163
|
|
|
121
164
|
if (unusedBits !== 0) {
|
|
@@ -123,9 +166,15 @@ function readBitString (buf: Uint8Array, context: Context): any {
|
|
|
123
166
|
throw new Error('Unused bits in bit string is unimplemented')
|
|
124
167
|
}
|
|
125
168
|
|
|
126
|
-
return
|
|
127
|
-
|
|
128
|
-
|
|
169
|
+
return bytes
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function readOctetString (buf: Uint8Array, context: Context): any {
|
|
173
|
+
const length = readLength(buf, context)
|
|
174
|
+
const bytes = buf.subarray(context.offset, context.offset + length)
|
|
175
|
+
context.offset += length
|
|
176
|
+
|
|
177
|
+
return bytes
|
|
129
178
|
}
|
|
130
179
|
|
|
131
180
|
function encodeNumber (value: number): Uint8ArrayList {
|
|
@@ -163,7 +212,7 @@ function encodeLength (bytes: { byteLength: number }): Uint8Array | Uint8ArrayLi
|
|
|
163
212
|
export function encodeInteger (value: Uint8Array | Uint8ArrayList): Uint8ArrayList {
|
|
164
213
|
const contents = new Uint8ArrayList()
|
|
165
214
|
|
|
166
|
-
const mask =
|
|
215
|
+
const mask = 0b10000000
|
|
167
216
|
const positive = (value.subarray()[0] & mask) === mask
|
|
168
217
|
|
|
169
218
|
if (positive) {
|
|
@@ -195,7 +244,15 @@ export function encodeBitString (value: Uint8Array | Uint8ArrayList): Uint8Array
|
|
|
195
244
|
)
|
|
196
245
|
}
|
|
197
246
|
|
|
198
|
-
export function
|
|
247
|
+
export function encodeOctetString (value: Uint8Array | Uint8ArrayList): Uint8ArrayList {
|
|
248
|
+
return new Uint8ArrayList(
|
|
249
|
+
Uint8Array.from([0x04]),
|
|
250
|
+
encodeLength(value),
|
|
251
|
+
value
|
|
252
|
+
)
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function encodeSequence (values: Array<Uint8Array | Uint8ArrayList>, tag = 0x30): Uint8ArrayList {
|
|
199
256
|
const output = new Uint8ArrayList()
|
|
200
257
|
|
|
201
258
|
for (const buf of values) {
|
|
@@ -205,7 +262,7 @@ export function encodeSequence (values: Array<Uint8Array | Uint8ArrayList>): Uin
|
|
|
205
262
|
}
|
|
206
263
|
|
|
207
264
|
return new Uint8ArrayList(
|
|
208
|
-
Uint8Array.from([
|
|
265
|
+
Uint8Array.from([tag]),
|
|
209
266
|
encodeLength(output),
|
|
210
267
|
output
|
|
211
268
|
)
|
|
@@ -6,6 +6,7 @@ import * as utils from './utils.js'
|
|
|
6
6
|
import type { JWKKeyPair } from '../interface.js'
|
|
7
7
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
8
8
|
|
|
9
|
+
export const RSAES_PKCS1_V1_5_OID = '1.2.840.113549.1.1.1'
|
|
9
10
|
export { utils }
|
|
10
11
|
|
|
11
12
|
export async function generateRSAKey (bits: number): Promise<JWKKeyPair> {
|
package/src/keys/rsa/index.ts
CHANGED
|
@@ -9,6 +9,8 @@ import type { Uint8ArrayList } from 'uint8arraylist'
|
|
|
9
9
|
|
|
10
10
|
const keypair = promisify(crypto.generateKeyPair)
|
|
11
11
|
|
|
12
|
+
export const RSAES_PKCS1_V1_5_OID = '1.2.840.113549.1.1.1'
|
|
13
|
+
|
|
12
14
|
export { utils }
|
|
13
15
|
|
|
14
16
|
export async function generateRSAKey (bits: number): Promise<JWKKeyPair> {
|
package/src/keys/rsa/rsa.ts
CHANGED
|
@@ -8,18 +8,18 @@ import type { Uint8ArrayList } from 'uint8arraylist'
|
|
|
8
8
|
|
|
9
9
|
export class RSAPublicKey implements RSAPublicKeyInterface {
|
|
10
10
|
public readonly type = 'RSA'
|
|
11
|
-
|
|
11
|
+
public readonly jwk: JsonWebKey
|
|
12
12
|
private _raw?: Uint8Array
|
|
13
13
|
private readonly _multihash: Digest<18, number>
|
|
14
14
|
|
|
15
|
-
constructor (
|
|
16
|
-
this.
|
|
15
|
+
constructor (jwk: JsonWebKey, digest: Digest<18, number>) {
|
|
16
|
+
this.jwk = jwk
|
|
17
17
|
this._multihash = digest
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
get raw (): Uint8Array {
|
|
21
21
|
if (this._raw == null) {
|
|
22
|
-
this._raw = utils.jwkToPkix(this.
|
|
22
|
+
this._raw = utils.jwkToPkix(this.jwk)
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
return this._raw
|
|
@@ -46,24 +46,24 @@ export class RSAPublicKey implements RSAPublicKeyInterface {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
verify (data: Uint8Array | Uint8ArrayList, sig: Uint8Array): boolean | Promise<boolean> {
|
|
49
|
-
return hashAndVerify(this.
|
|
49
|
+
return hashAndVerify(this.jwk, sig, data)
|
|
50
50
|
}
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
export class RSAPrivateKey implements RSAPrivateKeyInterface {
|
|
54
54
|
public readonly type = 'RSA'
|
|
55
|
-
|
|
55
|
+
public readonly jwk: JsonWebKey
|
|
56
56
|
private _raw?: Uint8Array
|
|
57
57
|
public readonly publicKey: RSAPublicKey
|
|
58
58
|
|
|
59
|
-
constructor (
|
|
60
|
-
this.
|
|
59
|
+
constructor (jwk: JsonWebKey, publicKey: RSAPublicKey) {
|
|
60
|
+
this.jwk = jwk
|
|
61
61
|
this.publicKey = publicKey
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
get raw (): Uint8Array {
|
|
65
65
|
if (this._raw == null) {
|
|
66
|
-
this._raw = utils.jwkToPkcs1(this.
|
|
66
|
+
this._raw = utils.jwkToPkcs1(this.jwk)
|
|
67
67
|
}
|
|
68
68
|
|
|
69
69
|
return this._raw
|
|
@@ -78,6 +78,6 @@ export class RSAPrivateKey implements RSAPrivateKeyInterface {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
sign (message: Uint8Array | Uint8ArrayList): Uint8Array | Promise<Uint8Array> {
|
|
81
|
-
return hashAndSign(this.
|
|
81
|
+
return hashAndSign(this.jwk, message)
|
|
82
82
|
}
|
|
83
83
|
}
|
package/src/keys/rsa/utils.ts
CHANGED
|
@@ -23,17 +23,24 @@ const RSA_ALGORITHM_IDENTIFIER = Uint8Array.from([
|
|
|
23
23
|
* Convert a PKCS#1 in ASN1 DER format to a JWK private key
|
|
24
24
|
*/
|
|
25
25
|
export function pkcs1ToJwk (bytes: Uint8Array): JsonWebKey {
|
|
26
|
-
const
|
|
26
|
+
const message = decodeDer(bytes)
|
|
27
27
|
|
|
28
|
+
return pkcs1MessageToJwk(message)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Convert a PKCS#1 in ASN1 DER format to a JWK private key
|
|
33
|
+
*/
|
|
34
|
+
export function pkcs1MessageToJwk (message: any): JsonWebKey {
|
|
28
35
|
return {
|
|
29
|
-
n: uint8ArrayToString(
|
|
30
|
-
e: uint8ArrayToString(
|
|
31
|
-
d: uint8ArrayToString(
|
|
32
|
-
p: uint8ArrayToString(
|
|
33
|
-
q: uint8ArrayToString(
|
|
34
|
-
dp: uint8ArrayToString(
|
|
35
|
-
dq: uint8ArrayToString(
|
|
36
|
-
qi: uint8ArrayToString(
|
|
36
|
+
n: uint8ArrayToString(message[1], 'base64url'),
|
|
37
|
+
e: uint8ArrayToString(message[2], 'base64url'),
|
|
38
|
+
d: uint8ArrayToString(message[3], 'base64url'),
|
|
39
|
+
p: uint8ArrayToString(message[4], 'base64url'),
|
|
40
|
+
q: uint8ArrayToString(message[5], 'base64url'),
|
|
41
|
+
dp: uint8ArrayToString(message[6], 'base64url'),
|
|
42
|
+
dq: uint8ArrayToString(message[7], 'base64url'),
|
|
43
|
+
qi: uint8ArrayToString(message[8], 'base64url'),
|
|
37
44
|
kty: 'RSA'
|
|
38
45
|
}
|
|
39
46
|
}
|
|
@@ -63,7 +70,15 @@ export function jwkToPkcs1 (jwk: JsonWebKey): Uint8Array {
|
|
|
63
70
|
* Convert a PKIX in ASN1 DER format to a JWK public key
|
|
64
71
|
*/
|
|
65
72
|
export function pkixToJwk (bytes: Uint8Array): JsonWebKey {
|
|
66
|
-
const
|
|
73
|
+
const message = decodeDer(bytes, {
|
|
74
|
+
offset: 0
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
return pkixMessageToJwk(message)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function pkixMessageToJwk (message: any): JsonWebKey {
|
|
81
|
+
const keys = decodeDer(message[1], {
|
|
67
82
|
offset: 0
|
|
68
83
|
})
|
|
69
84
|
|
|
@@ -72,11 +87,11 @@ export function pkixToJwk (bytes: Uint8Array): JsonWebKey {
|
|
|
72
87
|
return {
|
|
73
88
|
kty: 'RSA',
|
|
74
89
|
n: uint8ArrayToString(
|
|
75
|
-
|
|
90
|
+
keys[0],
|
|
76
91
|
'base64url'
|
|
77
92
|
),
|
|
78
93
|
e: uint8ArrayToString(
|
|
79
|
-
|
|
94
|
+
keys[1],
|
|
80
95
|
'base64url'
|
|
81
96
|
)
|
|
82
97
|
}
|
|
@@ -104,23 +119,40 @@ export function jwkToPkix (jwk: JsonWebKey): Uint8Array {
|
|
|
104
119
|
}
|
|
105
120
|
|
|
106
121
|
/**
|
|
107
|
-
* Turn PKCS#1 DER bytes
|
|
122
|
+
* Turn PKCS#1 DER bytes into a PrivateKey
|
|
108
123
|
*/
|
|
109
124
|
export function pkcs1ToRSAPrivateKey (bytes: Uint8Array): RSAPrivateKey {
|
|
110
|
-
const
|
|
125
|
+
const message = decodeDer(bytes)
|
|
126
|
+
|
|
127
|
+
return pkcs1MessageToRSAPrivateKey(message)
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Turn PKCS#1 DER bytes into a PrivateKey
|
|
132
|
+
*/
|
|
133
|
+
export function pkcs1MessageToRSAPrivateKey (message: any): RSAPrivateKey {
|
|
134
|
+
const jwk = pkcs1MessageToJwk(message)
|
|
111
135
|
|
|
112
136
|
return jwkToRSAPrivateKey(jwk)
|
|
113
137
|
}
|
|
114
138
|
|
|
115
139
|
/**
|
|
116
|
-
* Turn PKIX
|
|
140
|
+
* Turn a PKIX message into a PublicKey
|
|
117
141
|
*/
|
|
118
142
|
export function pkixToRSAPublicKey (bytes: Uint8Array, digest?: Digest<18, number>): RSAPublicKey {
|
|
119
143
|
if (bytes.byteLength >= MAX_RSA_JWK_SIZE) {
|
|
120
144
|
throw new InvalidPublicKeyError('Key size is too large')
|
|
121
145
|
}
|
|
122
146
|
|
|
123
|
-
const
|
|
147
|
+
const message = decodeDer(bytes, {
|
|
148
|
+
offset: 0
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
return pkixMessageToRSAPublicKey(message, bytes, digest)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export function pkixMessageToRSAPublicKey (message: any, bytes: Uint8Array, digest?: Digest<18, number>): RSAPublicKey {
|
|
155
|
+
const jwk = pkixMessageToJwk(message)
|
|
124
156
|
|
|
125
157
|
if (digest == null) {
|
|
126
158
|
const hash = sha256(pb.PublicKey.encode({
|
|
@@ -4,6 +4,12 @@ import { SigningError, VerificationError } from '../../errors.js'
|
|
|
4
4
|
import { isPromise } from '../../util.js'
|
|
5
5
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
6
6
|
|
|
7
|
+
const PUBLIC_KEY_BYTE_LENGTH = 33
|
|
8
|
+
const PRIVATE_KEY_BYTE_LENGTH = 32
|
|
9
|
+
|
|
10
|
+
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
|
|
11
|
+
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
12
|
+
|
|
7
13
|
/**
|
|
8
14
|
* Hash and sign message with private key
|
|
9
15
|
*/
|
|
@@ -3,6 +3,12 @@ import { secp256k1 as secp } from '@noble/curves/secp256k1'
|
|
|
3
3
|
import { SigningError, VerificationError } from '../../errors.js'
|
|
4
4
|
import type { Uint8ArrayList } from 'uint8arraylist'
|
|
5
5
|
|
|
6
|
+
const PUBLIC_KEY_BYTE_LENGTH = 33
|
|
7
|
+
const PRIVATE_KEY_BYTE_LENGTH = 32
|
|
8
|
+
|
|
9
|
+
export { PUBLIC_KEY_BYTE_LENGTH as publicKeyLength }
|
|
10
|
+
export { PRIVATE_KEY_BYTE_LENGTH as privateKeyLength }
|
|
11
|
+
|
|
6
12
|
/**
|
|
7
13
|
* Hash and sign message with private key
|
|
8
14
|
*/
|