@dwn-protocol/id-sdk 0.2.5 → 0.2.6
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/package.json +2 -3
- package/src/agent/app-data-store.ts +0 -365
- package/src/agent/did-manager.ts +0 -393
- package/src/agent/dwn-manager.ts +0 -548
- package/src/agent/identity-manager.ts +0 -165
- package/src/agent/index.ts +0 -19
- package/src/agent/json-rpc.ts +0 -107
- package/src/agent/key-manager.ts +0 -302
- package/src/agent/kms-local.ts +0 -412
- package/src/agent/outbox.ts +0 -128
- package/src/agent/rpc-client.ts +0 -223
- package/src/agent/store-managed-did.ts +0 -295
- package/src/agent/store-managed-identity.ts +0 -243
- package/src/agent/store-managed-key.ts +0 -754
- package/src/agent/sync-manager.ts +0 -631
- package/src/agent/test-managed-agent.ts +0 -299
- package/src/agent/types/agent.ts +0 -145
- package/src/agent/types/managed-key.ts +0 -442
- package/src/agent/utils.ts +0 -190
- package/src/common/convert.ts +0 -424
- package/src/common/index.ts +0 -9
- package/src/common/multicodec.ts +0 -176
- package/src/common/object.ts +0 -43
- package/src/common/stores.ts +0 -125
- package/src/common/stream-node.ts +0 -381
- package/src/common/stream.ts +0 -406
- package/src/common/type-utils.ts +0 -117
- package/src/common/types.ts +0 -48
- package/src/credentials/credential-bbs.ts +0 -419
- package/src/credentials/credential.ts +0 -324
- package/src/credentials/index.ts +0 -5
- package/src/credentials/presentation.ts +0 -182
- package/src/credentials/status-list.ts +0 -365
- package/src/credentials/utils.ts +0 -58
- package/src/credentials/validators.ts +0 -52
- package/src/crypto/algorithms-api/aes/base.ts +0 -49
- package/src/crypto/algorithms-api/aes/ctr.ts +0 -51
- package/src/crypto/algorithms-api/aes/index.ts +0 -2
- package/src/crypto/algorithms-api/crypto-algorithm.ts +0 -127
- package/src/crypto/algorithms-api/crypto-key.ts +0 -56
- package/src/crypto/algorithms-api/ec/base.ts +0 -39
- package/src/crypto/algorithms-api/ec/ecdh.ts +0 -53
- package/src/crypto/algorithms-api/ec/ecdsa.ts +0 -37
- package/src/crypto/algorithms-api/ec/eddsa.ts +0 -30
- package/src/crypto/algorithms-api/ec/index.ts +0 -4
- package/src/crypto/algorithms-api/errors.ts +0 -29
- package/src/crypto/algorithms-api/index.ts +0 -6
- package/src/crypto/algorithms-api/pbkdf/index.ts +0 -1
- package/src/crypto/algorithms-api/pbkdf/pbkdf2.ts +0 -91
- package/src/crypto/crypto-algorithms/aes-ctr.ts +0 -70
- package/src/crypto/crypto-algorithms/bbs.ts +0 -110
- package/src/crypto/crypto-algorithms/ecdh.ts +0 -115
- package/src/crypto/crypto-algorithms/ecdsa.ts +0 -111
- package/src/crypto/crypto-algorithms/eddsa.ts +0 -110
- package/src/crypto/crypto-algorithms/index.ts +0 -6
- package/src/crypto/crypto-algorithms/pbkdf2.ts +0 -54
- package/src/crypto/crypto-primitives/aes-ctr.ts +0 -131
- package/src/crypto/crypto-primitives/aes-gcm.ts +0 -138
- package/src/crypto/crypto-primitives/bbs.ts +0 -183
- package/src/crypto/crypto-primitives/concat-kdf.ts +0 -207
- package/src/crypto/crypto-primitives/ed25519.ts +0 -201
- package/src/crypto/crypto-primitives/index.ts +0 -10
- package/src/crypto/crypto-primitives/pbkdf2.ts +0 -78
- package/src/crypto/crypto-primitives/secp256k1.ts +0 -322
- package/src/crypto/crypto-primitives/x25519.ts +0 -101
- package/src/crypto/crypto-primitives/xchacha20-poly1305.ts +0 -46
- package/src/crypto/crypto-primitives/xchacha20.ts +0 -34
- package/src/crypto/index.ts +0 -8
- package/src/crypto/jose.ts +0 -948
- package/src/crypto/types/crypto-key.ts +0 -4
- package/src/crypto/types/iddwn-crypto.ts +0 -119
- package/src/crypto/utils.ts +0 -200
- package/src/did-api.ts +0 -72
- package/src/dids/dht.ts +0 -412
- package/src/dids/did-dht.ts +0 -436
- package/src/dids/did-ion.ts +0 -613
- package/src/dids/did-key.ts +0 -791
- package/src/dids/did-resolver.ts +0 -107
- package/src/dids/index.ts +0 -9
- package/src/dids/resolver-cache-level.ts +0 -82
- package/src/dids/resolver-cache-noop.ts +0 -25
- package/src/dids/types.ts +0 -278
- package/src/dids/utils.ts +0 -129
- package/src/dwn-api.ts +0 -584
- package/src/iddwn.ts +0 -241
- package/src/identity-agent/index.ts +0 -270
- package/src/index.ts +0 -26
- package/src/interfaces/metadata.ts +0 -163
- package/src/interfaces/queue.ts +0 -108
- package/src/interfaces/services.ts +0 -122
- package/src/interfaces/transactions.ts +0 -220
- package/src/protocol.ts +0 -68
- package/src/proxy-agent/index.ts +0 -255
- package/src/record.ts +0 -521
- package/src/service-options.ts +0 -62
- package/src/typings/decentralized-identity__ion-pow-sdk.d.ts +0 -7
- package/src/user-agent/index.ts +0 -295
- package/src/utils.ts +0 -29
- package/src/vc-api.ts +0 -505
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import type { IDCrypto } from '../types/iddwn-crypto.js';
|
|
2
|
-
import type { BytesKeyPair } from '../types/crypto-key.js';
|
|
3
|
-
|
|
4
|
-
import { isBytesKeyPair } from '../utils.js';
|
|
5
|
-
import { Secp256k1, X25519 } from '../crypto-primitives/index.js';
|
|
6
|
-
import { CryptoKey, BaseEcdhAlgorithm, OperationError } from '../algorithms-api/index.js';
|
|
7
|
-
|
|
8
|
-
export class EcdhAlgorithm extends BaseEcdhAlgorithm {
|
|
9
|
-
public readonly namedCurves = ['secp256k1', 'X25519'];
|
|
10
|
-
|
|
11
|
-
public async deriveBits(options: {
|
|
12
|
-
algorithm: IDCrypto.EcdhDeriveKeyOptions,
|
|
13
|
-
baseKey: IDCrypto.CryptoKey,
|
|
14
|
-
length: number | null
|
|
15
|
-
}): Promise<Uint8Array> {
|
|
16
|
-
const { algorithm, baseKey, length } = options;
|
|
17
|
-
|
|
18
|
-
this.checkAlgorithmOptions({ algorithm, baseKey });
|
|
19
|
-
// The base key must be allowed to be used for deriveBits operations.
|
|
20
|
-
this.checkKeyUsages({ keyUsages: ['deriveBits'], allowedKeyUsages: baseKey.usages });
|
|
21
|
-
// The public key must be allowed to be used for deriveBits operations.
|
|
22
|
-
this.checkKeyUsages({ keyUsages: ['deriveBits'], allowedKeyUsages: algorithm.publicKey.usages });
|
|
23
|
-
|
|
24
|
-
let sharedSecret: Uint8Array;
|
|
25
|
-
|
|
26
|
-
const ownKeyAlgorithm = baseKey.algorithm as IDCrypto.EcGenerateKeyOptions; // Type guard.
|
|
27
|
-
|
|
28
|
-
switch (ownKeyAlgorithm.namedCurve) {
|
|
29
|
-
|
|
30
|
-
case 'secp256k1': {
|
|
31
|
-
const ownPrivateKey = baseKey.material;
|
|
32
|
-
const otherPartyPublicKey = algorithm.publicKey.material;
|
|
33
|
-
sharedSecret = await Secp256k1.sharedSecret({
|
|
34
|
-
privateKey : ownPrivateKey,
|
|
35
|
-
publicKey : otherPartyPublicKey
|
|
36
|
-
});
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
case 'X25519': {
|
|
41
|
-
const ownPrivateKey = baseKey.material;
|
|
42
|
-
const otherPartyPublicKey = algorithm.publicKey.material;
|
|
43
|
-
sharedSecret = await X25519.sharedSecret({
|
|
44
|
-
privateKey : ownPrivateKey,
|
|
45
|
-
publicKey : otherPartyPublicKey
|
|
46
|
-
});
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
default:
|
|
51
|
-
throw new TypeError(`Out of range: '${ownKeyAlgorithm.namedCurve}'. Must be one of '${this.namedCurves.join(', ')}'`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Length is null, return the full derived secret.
|
|
55
|
-
if (length === null)
|
|
56
|
-
return sharedSecret;
|
|
57
|
-
|
|
58
|
-
// If the length is not a multiple of 8, throw.
|
|
59
|
-
if (length && length % 8 !== 0)
|
|
60
|
-
throw new OperationError(`To be compatible with all browsers, 'length' must be a multiple of 8.`);
|
|
61
|
-
|
|
62
|
-
// Convert length from bits to bytes.
|
|
63
|
-
const lengthInBytes = length / 8;
|
|
64
|
-
|
|
65
|
-
// If length (converted to bytes) is larger than the derived secret, throw.
|
|
66
|
-
if (sharedSecret.byteLength < lengthInBytes)
|
|
67
|
-
throw new OperationError(`Requested 'length' exceeds the byte length of the derived secret.`);
|
|
68
|
-
|
|
69
|
-
// Otherwise, either return the secret or a truncated slice.
|
|
70
|
-
return lengthInBytes === sharedSecret.byteLength ?
|
|
71
|
-
sharedSecret :
|
|
72
|
-
sharedSecret.slice(0, lengthInBytes);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public async generateKey(options: {
|
|
76
|
-
algorithm: IDCrypto.EcGenerateKeyOptions | IDCrypto.EcdsaGenerateKeyOptions,
|
|
77
|
-
extractable: boolean,
|
|
78
|
-
keyUsages: IDCrypto.KeyUsage[]
|
|
79
|
-
}): Promise<IDCrypto.CryptoKeyPair> {
|
|
80
|
-
const { algorithm, extractable, keyUsages } = options;
|
|
81
|
-
|
|
82
|
-
this.checkGenerateKey({ algorithm, keyUsages });
|
|
83
|
-
|
|
84
|
-
let keyPair: BytesKeyPair | undefined;
|
|
85
|
-
let cryptoKeyPair: IDCrypto.CryptoKeyPair;
|
|
86
|
-
|
|
87
|
-
switch (algorithm.namedCurve) {
|
|
88
|
-
|
|
89
|
-
case 'secp256k1': {
|
|
90
|
-
(algorithm as IDCrypto.EcdsaGenerateKeyOptions).compressedPublicKey ??= true;
|
|
91
|
-
keyPair = await Secp256k1.generateKeyPair({
|
|
92
|
-
compressedPublicKey: (algorithm as IDCrypto.EcdsaGenerateKeyOptions).compressedPublicKey
|
|
93
|
-
});
|
|
94
|
-
break;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
case 'X25519': {
|
|
98
|
-
keyPair = await X25519.generateKeyPair();
|
|
99
|
-
break;
|
|
100
|
-
}
|
|
101
|
-
// Default case not needed because checkGenerateKey() already validates the specified namedCurve is supported.
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
if (!isBytesKeyPair(keyPair)) {
|
|
105
|
-
throw new Error('Operation failed to generate key pair.');
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
cryptoKeyPair = {
|
|
109
|
-
privateKey : new CryptoKey(algorithm, extractable, keyPair.privateKey, 'private', this.keyUsages.privateKey),
|
|
110
|
-
publicKey : new CryptoKey(algorithm, true, keyPair.publicKey, 'public', this.keyUsages.publicKey)
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
return cryptoKeyPair;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
import type { IDCrypto } from '../types/iddwn-crypto.js';
|
|
2
|
-
import type { BytesKeyPair } from '../types/crypto-key.js';
|
|
3
|
-
|
|
4
|
-
import { isBytesKeyPair } from '../utils.js';
|
|
5
|
-
import { Secp256k1 } from '../crypto-primitives/index.js';
|
|
6
|
-
import { CryptoKey, BaseEcdsaAlgorithm } from '../algorithms-api/index.js';
|
|
7
|
-
export class EcdsaAlgorithm extends BaseEcdsaAlgorithm {
|
|
8
|
-
public readonly hashAlgorithms = ['SHA-256'];
|
|
9
|
-
public readonly namedCurves = ['secp256k1'];
|
|
10
|
-
|
|
11
|
-
public async generateKey(options: {
|
|
12
|
-
algorithm: IDCrypto.EcdsaGenerateKeyOptions,
|
|
13
|
-
extractable: boolean,
|
|
14
|
-
keyUsages: IDCrypto.KeyUsage[]
|
|
15
|
-
}): Promise<IDCrypto.CryptoKeyPair> {
|
|
16
|
-
const { algorithm, extractable, keyUsages } = options;
|
|
17
|
-
|
|
18
|
-
this.checkGenerateKey({ algorithm, keyUsages });
|
|
19
|
-
|
|
20
|
-
let keyPair: BytesKeyPair | undefined;
|
|
21
|
-
let cryptoKeyPair: IDCrypto.CryptoKeyPair;
|
|
22
|
-
|
|
23
|
-
switch (algorithm.namedCurve) {
|
|
24
|
-
|
|
25
|
-
case 'secp256k1': {
|
|
26
|
-
algorithm.compressedPublicKey ??= true;
|
|
27
|
-
keyPair = await Secp256k1.generateKeyPair({ compressedPublicKey: algorithm.compressedPublicKey });
|
|
28
|
-
break;
|
|
29
|
-
}
|
|
30
|
-
// Default case not needed because checkGenerateKey() already validates the specified namedCurve is supported.
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
if (!isBytesKeyPair(keyPair)) {
|
|
34
|
-
throw new Error('Operation failed to generate key pair.');
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
cryptoKeyPair = {
|
|
38
|
-
privateKey : new CryptoKey(algorithm, extractable, keyPair.privateKey, 'private', this.keyUsages.privateKey),
|
|
39
|
-
publicKey : new CryptoKey(algorithm, true, keyPair.publicKey, 'public', this.keyUsages.publicKey)
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
return cryptoKeyPair;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
public async sign(options: {
|
|
46
|
-
algorithm: IDCrypto.EcdsaOptions,
|
|
47
|
-
key: IDCrypto.CryptoKey,
|
|
48
|
-
data: Uint8Array
|
|
49
|
-
}): Promise<Uint8Array> {
|
|
50
|
-
const { algorithm, key, data } = options;
|
|
51
|
-
|
|
52
|
-
this.checkAlgorithmOptions({ algorithm });
|
|
53
|
-
// The key's algorithm must match the algorithm implementation processing the operation.
|
|
54
|
-
this.checkKeyAlgorithm({ keyAlgorithmName: key.algorithm.name });
|
|
55
|
-
// The key must be a private key.
|
|
56
|
-
this.checkKeyType({ keyType: key.type, allowedKeyType: 'private' });
|
|
57
|
-
// The key must be allowed to be used for sign operations.
|
|
58
|
-
this.checkKeyUsages({ keyUsages: ['sign'], allowedKeyUsages: key.usages });
|
|
59
|
-
|
|
60
|
-
let signature: Uint8Array;
|
|
61
|
-
|
|
62
|
-
const keyAlgorithm = key.algorithm as IDCrypto.EcdsaGenerateKeyOptions; // Type guard.
|
|
63
|
-
|
|
64
|
-
switch (keyAlgorithm.namedCurve) {
|
|
65
|
-
|
|
66
|
-
case 'secp256k1': {
|
|
67
|
-
signature = await Secp256k1.sign({ hash: algorithm.hash, key: key.material, data });
|
|
68
|
-
break;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
default:
|
|
72
|
-
throw new TypeError(`Out of range: '${keyAlgorithm.namedCurve}'. Must be one of '${this.namedCurves.join(', ')}'`);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return signature;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
public async verify(options: {
|
|
79
|
-
algorithm: IDCrypto.EcdsaOptions;
|
|
80
|
-
key: IDCrypto.CryptoKey;
|
|
81
|
-
signature: Uint8Array;
|
|
82
|
-
data: Uint8Array;
|
|
83
|
-
}): Promise<boolean> {
|
|
84
|
-
const { algorithm, key, signature, data } = options;
|
|
85
|
-
|
|
86
|
-
this.checkAlgorithmOptions({ algorithm });
|
|
87
|
-
// The key's algorithm must match the algorithm implementation processing the operation.
|
|
88
|
-
this.checkKeyAlgorithm({ keyAlgorithmName: key.algorithm.name });
|
|
89
|
-
// The key must be a public key.
|
|
90
|
-
this.checkKeyType({ keyType: key.type, allowedKeyType: 'public' });
|
|
91
|
-
// The key must be allowed to be used for verify operations.
|
|
92
|
-
this.checkKeyUsages({ keyUsages: ['verify'], allowedKeyUsages: key.usages });
|
|
93
|
-
|
|
94
|
-
let isValid: boolean;
|
|
95
|
-
|
|
96
|
-
const keyAlgorithm = key.algorithm as IDCrypto.EcdsaGenerateKeyOptions; // Type guard.
|
|
97
|
-
|
|
98
|
-
switch (keyAlgorithm.namedCurve) {
|
|
99
|
-
|
|
100
|
-
case 'secp256k1': {
|
|
101
|
-
isValid = await Secp256k1.verify({ hash: algorithm.hash, key: key.material, signature, data });
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
default:
|
|
106
|
-
throw new TypeError(`Out of range: '${keyAlgorithm.namedCurve}'. Must be one of '${this.namedCurves.join(', ')}'`);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
return isValid;
|
|
110
|
-
}
|
|
111
|
-
}
|
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import type { IDCrypto } from '../types/iddwn-crypto.js';
|
|
2
|
-
import type { BytesKeyPair } from '../types/crypto-key.js';
|
|
3
|
-
|
|
4
|
-
import { isBytesKeyPair } from '../utils.js';
|
|
5
|
-
import { Ed25519 } from '../crypto-primitives/index.js';
|
|
6
|
-
import { CryptoKey, BaseEdDsaAlgorithm } from '../algorithms-api/index.js';
|
|
7
|
-
|
|
8
|
-
export class EdDsaAlgorithm extends BaseEdDsaAlgorithm {
|
|
9
|
-
public readonly namedCurves = ['Ed25519', 'Ed448'];
|
|
10
|
-
|
|
11
|
-
public async generateKey(options: {
|
|
12
|
-
algorithm: IDCrypto.EdDsaGenerateKeyOptions,
|
|
13
|
-
extractable: boolean,
|
|
14
|
-
keyUsages: IDCrypto.KeyUsage[]
|
|
15
|
-
}): Promise<IDCrypto.CryptoKeyPair> {
|
|
16
|
-
const { algorithm, extractable, keyUsages } = options;
|
|
17
|
-
|
|
18
|
-
this.checkGenerateKey({ algorithm, keyUsages });
|
|
19
|
-
|
|
20
|
-
let keyPair: BytesKeyPair | undefined;
|
|
21
|
-
let cryptoKeyPair: IDCrypto.CryptoKeyPair;
|
|
22
|
-
|
|
23
|
-
switch (algorithm.namedCurve) {
|
|
24
|
-
|
|
25
|
-
case 'Ed25519': {
|
|
26
|
-
keyPair = await Ed25519.generateKeyPair();
|
|
27
|
-
break;
|
|
28
|
-
}
|
|
29
|
-
// Default case not needed because checkGenerateKey() already validates the specified namedCurve is supported.
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (!isBytesKeyPair(keyPair)) {
|
|
33
|
-
throw new Error('Operation failed to generate key pair.');
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
cryptoKeyPair = {
|
|
37
|
-
privateKey : new CryptoKey(algorithm, extractable, keyPair.privateKey, 'private', this.keyUsages.privateKey),
|
|
38
|
-
publicKey : new CryptoKey(algorithm, true, keyPair.publicKey, 'public', this.keyUsages.publicKey)
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
return cryptoKeyPair;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
public async sign(options: {
|
|
45
|
-
algorithm: IDCrypto.EdDsaOptions,
|
|
46
|
-
key: IDCrypto.CryptoKey,
|
|
47
|
-
data: Uint8Array
|
|
48
|
-
}): Promise<Uint8Array> {
|
|
49
|
-
const { algorithm, key, data } = options;
|
|
50
|
-
|
|
51
|
-
this.checkAlgorithmOptions({ algorithm });
|
|
52
|
-
// The key's algorithm must match the algorithm implementation processing the operation.
|
|
53
|
-
this.checkKeyAlgorithm({ keyAlgorithmName: key.algorithm.name });
|
|
54
|
-
// The key must be a private key.
|
|
55
|
-
this.checkKeyType({ keyType: key.type, allowedKeyType: 'private' });
|
|
56
|
-
// The key must be allowed to be used for sign operations.
|
|
57
|
-
this.checkKeyUsages({ keyUsages: ['sign'], allowedKeyUsages: key.usages });
|
|
58
|
-
|
|
59
|
-
let signature: Uint8Array;
|
|
60
|
-
|
|
61
|
-
const keyAlgorithm = key.algorithm as IDCrypto.EdDsaGenerateKeyOptions; // Type guard.
|
|
62
|
-
|
|
63
|
-
switch (keyAlgorithm.namedCurve) {
|
|
64
|
-
|
|
65
|
-
case 'Ed25519': {
|
|
66
|
-
signature = await Ed25519.sign({ key: key.material, data });
|
|
67
|
-
break;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
default:
|
|
71
|
-
throw new TypeError(`Out of range: '${keyAlgorithm.namedCurve}'. Must be one of '${this.namedCurves.join(', ')}'`);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
return signature;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
public async verify(options: {
|
|
78
|
-
algorithm: IDCrypto.EdDsaOptions;
|
|
79
|
-
key: IDCrypto.CryptoKey;
|
|
80
|
-
signature: Uint8Array;
|
|
81
|
-
data: Uint8Array;
|
|
82
|
-
}): Promise<boolean> {
|
|
83
|
-
const { algorithm, key, signature, data } = options;
|
|
84
|
-
|
|
85
|
-
this.checkAlgorithmOptions({ algorithm });
|
|
86
|
-
// The key's algorithm must match the algorithm implementation processing the operation.
|
|
87
|
-
this.checkKeyAlgorithm({ keyAlgorithmName: key.algorithm.name });
|
|
88
|
-
// The key must be a public key.
|
|
89
|
-
this.checkKeyType({ keyType: key.type, allowedKeyType: 'public' });
|
|
90
|
-
// The key must be allowed to be used for verify operations.
|
|
91
|
-
this.checkKeyUsages({ keyUsages: ['verify'], allowedKeyUsages: key.usages });
|
|
92
|
-
|
|
93
|
-
let isValid: boolean;
|
|
94
|
-
|
|
95
|
-
const keyAlgorithm = key.algorithm as IDCrypto.EdDsaGenerateKeyOptions; // Type guard.
|
|
96
|
-
|
|
97
|
-
switch (keyAlgorithm.namedCurve) {
|
|
98
|
-
|
|
99
|
-
case 'Ed25519': {
|
|
100
|
-
isValid = await Ed25519.verify({ key: key.material, signature, data });
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
default:
|
|
105
|
-
throw new TypeError(`Out of range: '${keyAlgorithm.namedCurve}'. Must be one of '${this.namedCurves.join(', ')}'`);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return isValid;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { IDCrypto } from '../types/iddwn-crypto.js';
|
|
2
|
-
|
|
3
|
-
import { BasePbkdf2Algorithm, CryptoKey, OperationError } from '../algorithms-api/index.js';
|
|
4
|
-
import { Pbkdf2 } from '../crypto-primitives/pbkdf2.js';
|
|
5
|
-
|
|
6
|
-
export class Pbkdf2Algorithm extends BasePbkdf2Algorithm {
|
|
7
|
-
public readonly hashAlgorithms = ['SHA-256', 'SHA-384', 'SHA-512'];
|
|
8
|
-
|
|
9
|
-
public async deriveBits(options: {
|
|
10
|
-
algorithm: IDCrypto.Pbkdf2Options,
|
|
11
|
-
baseKey: IDCrypto.CryptoKey,
|
|
12
|
-
length: number
|
|
13
|
-
}): Promise<Uint8Array> {
|
|
14
|
-
const { algorithm, baseKey, length } = options;
|
|
15
|
-
|
|
16
|
-
this.checkAlgorithmOptions({ algorithm, baseKey });
|
|
17
|
-
// The base key must be allowed to be used for deriveBits operations.
|
|
18
|
-
this.checkKeyUsages({ keyUsages: ['deriveBits'], allowedKeyUsages: baseKey.usages });
|
|
19
|
-
// If the length is 0, throw.
|
|
20
|
-
if (typeof length !== 'undefined' && length === 0) {
|
|
21
|
-
throw new OperationError(`The value of 'length' cannot be zero.`);
|
|
22
|
-
}
|
|
23
|
-
// If the length is not a multiple of 8, throw.
|
|
24
|
-
if (length && length % 8 !== 0) {
|
|
25
|
-
throw new OperationError(`To be compatible with all browsers, 'length' must be a multiple of 8.`);
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
const derivedBits = Pbkdf2.deriveKey({
|
|
29
|
-
hash : algorithm.hash as 'SHA-256' | 'SHA-384' | 'SHA-512',
|
|
30
|
-
iterations : algorithm.iterations,
|
|
31
|
-
length : length,
|
|
32
|
-
password : baseKey.material,
|
|
33
|
-
salt : algorithm.salt
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
return derivedBits;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public async importKey(options: {
|
|
40
|
-
format: IDCrypto.KeyFormat,
|
|
41
|
-
keyData: Uint8Array,
|
|
42
|
-
algorithm: IDCrypto.Algorithm,
|
|
43
|
-
extractable: boolean,
|
|
44
|
-
keyUsages: IDCrypto.KeyUsage[]
|
|
45
|
-
}): Promise<IDCrypto.CryptoKey> {
|
|
46
|
-
const { format, keyData, algorithm, extractable, keyUsages } = options;
|
|
47
|
-
|
|
48
|
-
this.checkImportKey({ algorithm, format, extractable, keyUsages });
|
|
49
|
-
|
|
50
|
-
const cryptoKey = new CryptoKey(algorithm, extractable, keyData, 'secret', keyUsages);
|
|
51
|
-
|
|
52
|
-
return cryptoKey;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { crypto } from '@noble/hashes/crypto';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The `AesCtr` class provides an interface for AES-CTR
|
|
5
|
-
* (Advanced Encryption Standard - Counter) encryption and decryption
|
|
6
|
-
* operations. The class uses the Web Crypto API for cryptographic operations.
|
|
7
|
-
*
|
|
8
|
-
* All methods of this class are asynchronous and return Promises. They all
|
|
9
|
-
* use the Uint8Array type for keys and data, providing a consistent
|
|
10
|
-
* interface for working with binary data.
|
|
11
|
-
*
|
|
12
|
-
* Example usage:
|
|
13
|
-
*
|
|
14
|
-
* ```ts
|
|
15
|
-
* const key = await AesCtr.generateKey({ length: 128 });
|
|
16
|
-
* const counter = new Uint8Array(16); // initialize a 16-byte counter
|
|
17
|
-
* const message = new TextEncoder().encode('Hello, world!');
|
|
18
|
-
* const ciphertext = await AesCtr.encrypt({
|
|
19
|
-
* counter,
|
|
20
|
-
* data: message,
|
|
21
|
-
* key,
|
|
22
|
-
* length: 128 // counter length in bits
|
|
23
|
-
* });
|
|
24
|
-
* const plaintext = await AesCtr.decrypt({
|
|
25
|
-
* counter,
|
|
26
|
-
* data: ciphertext,
|
|
27
|
-
* key,
|
|
28
|
-
* length: 128 // counter length in bits
|
|
29
|
-
* });
|
|
30
|
-
* console.log(new TextDecoder().decode(plaintext)); // 'Hello, world!'
|
|
31
|
-
* ```
|
|
32
|
-
*/
|
|
33
|
-
export class AesCtr {
|
|
34
|
-
/**
|
|
35
|
-
* Decrypts the provided data using AES-CTR.
|
|
36
|
-
*
|
|
37
|
-
* @param options - The options for the decryption operation.
|
|
38
|
-
* @param options.counter - The initial value of the counter block.
|
|
39
|
-
* @param options.data - The data to decrypt.
|
|
40
|
-
* @param options.key - The key to use for decryption.
|
|
41
|
-
* @param options.length - The length of the counter block in bits.
|
|
42
|
-
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
43
|
-
*/
|
|
44
|
-
public static async decrypt(options: {
|
|
45
|
-
counter: Uint8Array,
|
|
46
|
-
data: Uint8Array,
|
|
47
|
-
key: Uint8Array,
|
|
48
|
-
length: number
|
|
49
|
-
}): Promise<Uint8Array> {
|
|
50
|
-
const { counter, data, key, length } = options;
|
|
51
|
-
|
|
52
|
-
const webCryptoKey = await this.importKey(key);
|
|
53
|
-
|
|
54
|
-
const plaintextBuffer = await crypto.subtle.decrypt(
|
|
55
|
-
{ name: 'AES-CTR', counter, length },
|
|
56
|
-
webCryptoKey,
|
|
57
|
-
data
|
|
58
|
-
);
|
|
59
|
-
|
|
60
|
-
// Convert from ArrayBuffer to Uint8Array.
|
|
61
|
-
const plaintext = new Uint8Array(plaintextBuffer);
|
|
62
|
-
|
|
63
|
-
return plaintext;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Encrypts the provided data using AES-CTR.
|
|
68
|
-
*
|
|
69
|
-
* @param options - The options for the encryption operation.
|
|
70
|
-
* @param options.counter - The initial value of the counter block.
|
|
71
|
-
* @param options.data - The data to encrypt.
|
|
72
|
-
* @param options.key - The key to use for encryption.
|
|
73
|
-
* @param options.length - The length of the counter block in bits.
|
|
74
|
-
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
75
|
-
*/
|
|
76
|
-
public static async encrypt(options: {
|
|
77
|
-
counter: Uint8Array,
|
|
78
|
-
data: Uint8Array,
|
|
79
|
-
key: Uint8Array,
|
|
80
|
-
length: number
|
|
81
|
-
}): Promise<Uint8Array> {
|
|
82
|
-
const { counter, data, key, length } = options;
|
|
83
|
-
|
|
84
|
-
const webCryptoKey = await this.importKey(key);
|
|
85
|
-
|
|
86
|
-
const ciphertextBuffer = await crypto.subtle.encrypt(
|
|
87
|
-
{ name: 'AES-CTR', counter, length },
|
|
88
|
-
webCryptoKey,
|
|
89
|
-
data
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
// Convert from ArrayBuffer to Uint8Array.
|
|
93
|
-
const ciphertext = new Uint8Array(ciphertextBuffer);
|
|
94
|
-
|
|
95
|
-
return ciphertext;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Generates an AES key of a given length.
|
|
100
|
-
*
|
|
101
|
-
* @param length - The length of the key in bits.
|
|
102
|
-
* @returns A Promise that resolves to the generated key as a Uint8Array.
|
|
103
|
-
*/
|
|
104
|
-
public static async generateKey(options: {
|
|
105
|
-
length: number
|
|
106
|
-
}): Promise<Uint8Array> {
|
|
107
|
-
const { length } = options;
|
|
108
|
-
|
|
109
|
-
// Generate the secret key.
|
|
110
|
-
const lengthInBytes = length / 8;
|
|
111
|
-
const secretKey = crypto.getRandomValues(new Uint8Array(lengthInBytes));
|
|
112
|
-
|
|
113
|
-
return secretKey;
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* A private method to import a raw key for use with the Web Crypto API.
|
|
118
|
-
*
|
|
119
|
-
* @param key - The raw key material.
|
|
120
|
-
* @returns A Promise that resolves to a CryptoKey.
|
|
121
|
-
*/
|
|
122
|
-
private static async importKey(key: Uint8Array): Promise<CryptoKey> {
|
|
123
|
-
return crypto.subtle.importKey(
|
|
124
|
-
'raw',
|
|
125
|
-
key.buffer,
|
|
126
|
-
{ name: 'AES-CTR', length: key.byteLength * 8 },
|
|
127
|
-
true,
|
|
128
|
-
['encrypt', 'decrypt']
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
@@ -1,138 +0,0 @@
|
|
|
1
|
-
import { crypto } from '@noble/hashes/crypto';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* The `AesGcm` class provides an interface for AES-GCM
|
|
5
|
-
* (Advanced Encryption Standard - Galois/Counter Mode) encryption and
|
|
6
|
-
* decryption operations. The class uses the Web Crypto API for
|
|
7
|
-
* cryptographic operations.
|
|
8
|
-
*
|
|
9
|
-
* All methods of this class are asynchronous and return Promises. They all
|
|
10
|
-
* use the Uint8Array type for keys and data, providing a consistent
|
|
11
|
-
* interface for working with binary data.
|
|
12
|
-
*
|
|
13
|
-
* Example usage:
|
|
14
|
-
*
|
|
15
|
-
* ```ts
|
|
16
|
-
* const key = await AesGcm.generateKey({ length: 128 });
|
|
17
|
-
* const iv = new Uint8Array(12); // generate a 12-byte initialization vector
|
|
18
|
-
* const message = new TextEncoder().encode('Hello, world!');
|
|
19
|
-
* const ciphertext = await AesGcm.encrypt({
|
|
20
|
-
* data: message,
|
|
21
|
-
* iv,
|
|
22
|
-
* key,
|
|
23
|
-
* tagLength: 128
|
|
24
|
-
* });
|
|
25
|
-
* const plaintext = await AesGcm.decrypt({
|
|
26
|
-
* data: ciphertext,
|
|
27
|
-
* iv,
|
|
28
|
-
* key,
|
|
29
|
-
* tagLength: 128
|
|
30
|
-
* });
|
|
31
|
-
* console.log(new TextDecoder().decode(plaintext)); // 'Hello, world!'
|
|
32
|
-
* ```
|
|
33
|
-
*/
|
|
34
|
-
export class AesGcm {
|
|
35
|
-
/**
|
|
36
|
-
* Decrypts the provided data using AES-GCM.
|
|
37
|
-
*
|
|
38
|
-
* @param options - The options for the decryption operation.
|
|
39
|
-
* @param options.additionalData - Data that will be authenticated along with the encrypted data.
|
|
40
|
-
* @param options.data - The data to decrypt.
|
|
41
|
-
* @param options.iv - A unique initialization vector.
|
|
42
|
-
* @param options.key - The key to use for decryption.
|
|
43
|
-
* @param options.tagLength - This size of the authentication tag generated in bits.
|
|
44
|
-
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
45
|
-
*/
|
|
46
|
-
public static async decrypt(options: {
|
|
47
|
-
additionalData?: Uint8Array,
|
|
48
|
-
data: Uint8Array,
|
|
49
|
-
iv: Uint8Array,
|
|
50
|
-
key: Uint8Array,
|
|
51
|
-
tagLength?: number
|
|
52
|
-
}): Promise<Uint8Array> {
|
|
53
|
-
const { additionalData, data, iv, key, tagLength } = options;
|
|
54
|
-
|
|
55
|
-
const webCryptoKey = await this.importKey(key);
|
|
56
|
-
|
|
57
|
-
// Web browsers throw an error if additionalData is undefined.
|
|
58
|
-
const algorithm = (additionalData === undefined)
|
|
59
|
-
? { name: 'AES-GCM', iv, tagLength }
|
|
60
|
-
: { name: 'AES-GCM', additionalData, iv, tagLength };
|
|
61
|
-
|
|
62
|
-
const plaintextBuffer = await crypto.subtle.decrypt(algorithm, webCryptoKey, data);
|
|
63
|
-
|
|
64
|
-
// Convert from ArrayBuffer to Uint8Array.
|
|
65
|
-
const plaintext = new Uint8Array(plaintextBuffer);
|
|
66
|
-
|
|
67
|
-
return plaintext;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Encrypts the provided data using AES-GCM.
|
|
72
|
-
*
|
|
73
|
-
* @param options - The options for the encryption operation.
|
|
74
|
-
* @param options.additionalData - Data that will be authenticated along with the encrypted data.
|
|
75
|
-
* @param options.data - The data to decrypt.
|
|
76
|
-
* @param options.iv - A unique initialization vector.
|
|
77
|
-
* @param options.key - The key to use for decryption.
|
|
78
|
-
* @param options.tagLength - This size of the authentication tag generated in bits.
|
|
79
|
-
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
80
|
-
*/
|
|
81
|
-
public static async encrypt(options: {
|
|
82
|
-
additionalData?: Uint8Array,
|
|
83
|
-
data: Uint8Array,
|
|
84
|
-
iv: Uint8Array,
|
|
85
|
-
key: Uint8Array,
|
|
86
|
-
tagLength?: number
|
|
87
|
-
}): Promise<Uint8Array> {
|
|
88
|
-
const { additionalData, data, iv, key, tagLength } = options;
|
|
89
|
-
|
|
90
|
-
const webCryptoKey = await this.importKey(key);
|
|
91
|
-
|
|
92
|
-
// Web browsers throw an error if additionalData is undefined.
|
|
93
|
-
const algorithm = (additionalData === undefined)
|
|
94
|
-
? { name: 'AES-GCM', iv, tagLength }
|
|
95
|
-
: { name: 'AES-GCM', additionalData, iv, tagLength };
|
|
96
|
-
|
|
97
|
-
const ciphertextBuffer = await crypto.subtle.encrypt(algorithm, webCryptoKey, data);
|
|
98
|
-
|
|
99
|
-
// Convert from ArrayBuffer to Uint8Array.
|
|
100
|
-
const ciphertext = new Uint8Array(ciphertextBuffer);
|
|
101
|
-
|
|
102
|
-
return ciphertext;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Generates an AES key of a given length.
|
|
107
|
-
*
|
|
108
|
-
* @param length - The length of the key in bits.
|
|
109
|
-
* @returns A Promise that resolves to the generated key as a Uint8Array.
|
|
110
|
-
*/
|
|
111
|
-
public static async generateKey(options: {
|
|
112
|
-
length: number
|
|
113
|
-
}): Promise<Uint8Array> {
|
|
114
|
-
const { length } = options;
|
|
115
|
-
|
|
116
|
-
// Generate the secret key.
|
|
117
|
-
const lengthInBytes = length / 8;
|
|
118
|
-
const secretKey = crypto.getRandomValues(new Uint8Array(lengthInBytes));
|
|
119
|
-
|
|
120
|
-
return secretKey;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* A private method to import a raw key for use with the Web Crypto API.
|
|
125
|
-
*
|
|
126
|
-
* @param key - The raw key material.
|
|
127
|
-
* @returns A Promise that resolves to a CryptoKey.
|
|
128
|
-
*/
|
|
129
|
-
private static async importKey(key: Uint8Array): Promise<CryptoKey> {
|
|
130
|
-
return crypto.subtle.importKey(
|
|
131
|
-
'raw',
|
|
132
|
-
key.buffer,
|
|
133
|
-
{ name: 'AES-GCM', length: key.byteLength * 8 },
|
|
134
|
-
true,
|
|
135
|
-
['encrypt', 'decrypt']
|
|
136
|
-
);
|
|
137
|
-
}
|
|
138
|
-
}
|