@enbox/crypto 0.0.1
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/LICENSE +201 -0
- package/README.md +708 -0
- package/dist/browser.js +64 -0
- package/dist/browser.js.map +7 -0
- package/dist/browser.mjs +64 -0
- package/dist/browser.mjs.map +7 -0
- package/dist/cjs/algorithms/aes-ctr.js +188 -0
- package/dist/cjs/algorithms/aes-ctr.js.map +1 -0
- package/dist/cjs/algorithms/aes-gcm.js +196 -0
- package/dist/cjs/algorithms/aes-gcm.js.map +1 -0
- package/dist/cjs/algorithms/crypto-algorithm.js +13 -0
- package/dist/cjs/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/cjs/algorithms/ecdsa.js +352 -0
- package/dist/cjs/algorithms/ecdsa.js.map +1 -0
- package/dist/cjs/algorithms/eddsa.js +325 -0
- package/dist/cjs/algorithms/eddsa.js.map +1 -0
- package/dist/cjs/algorithms/sha-2.js +119 -0
- package/dist/cjs/algorithms/sha-2.js.map +1 -0
- package/dist/cjs/index.js +41 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/jose/jwe.js +3 -0
- package/dist/cjs/jose/jwe.js.map +1 -0
- package/dist/cjs/jose/jwk.js +278 -0
- package/dist/cjs/jose/jwk.js.map +1 -0
- package/dist/cjs/jose/jws.js +3 -0
- package/dist/cjs/jose/jws.js.map +1 -0
- package/dist/cjs/jose/jwt.js +3 -0
- package/dist/cjs/jose/jwt.js.map +1 -0
- package/dist/cjs/jose/utils.js +60 -0
- package/dist/cjs/jose/utils.js.map +1 -0
- package/dist/cjs/local-key-manager.js +521 -0
- package/dist/cjs/local-key-manager.js.map +1 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/primitives/aes-ctr.js +398 -0
- package/dist/cjs/primitives/aes-ctr.js.map +1 -0
- package/dist/cjs/primitives/aes-gcm.js +425 -0
- package/dist/cjs/primitives/aes-gcm.js.map +1 -0
- package/dist/cjs/primitives/concat-kdf.js +215 -0
- package/dist/cjs/primitives/concat-kdf.js.map +1 -0
- package/dist/cjs/primitives/ed25519.js +651 -0
- package/dist/cjs/primitives/ed25519.js.map +1 -0
- package/dist/cjs/primitives/pbkdf2.js +120 -0
- package/dist/cjs/primitives/pbkdf2.js.map +1 -0
- package/dist/cjs/primitives/secp256k1.js +958 -0
- package/dist/cjs/primitives/secp256k1.js.map +1 -0
- package/dist/cjs/primitives/secp256r1.js +959 -0
- package/dist/cjs/primitives/secp256r1.js.map +1 -0
- package/dist/cjs/primitives/sha256.js +93 -0
- package/dist/cjs/primitives/sha256.js.map +1 -0
- package/dist/cjs/primitives/x25519.js +498 -0
- package/dist/cjs/primitives/x25519.js.map +1 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js +340 -0
- package/dist/cjs/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/cjs/primitives/xchacha20.js +316 -0
- package/dist/cjs/primitives/xchacha20.js.map +1 -0
- package/dist/cjs/types/cipher.js +3 -0
- package/dist/cjs/types/cipher.js.map +1 -0
- package/dist/cjs/types/crypto-api.js +3 -0
- package/dist/cjs/types/crypto-api.js.map +1 -0
- package/dist/cjs/types/hasher.js +3 -0
- package/dist/cjs/types/hasher.js.map +1 -0
- package/dist/cjs/types/identifier.js +3 -0
- package/dist/cjs/types/identifier.js.map +1 -0
- package/dist/cjs/types/key-compressor.js +3 -0
- package/dist/cjs/types/key-compressor.js.map +1 -0
- package/dist/cjs/types/key-converter.js +3 -0
- package/dist/cjs/types/key-converter.js.map +1 -0
- package/dist/cjs/types/key-deriver.js +3 -0
- package/dist/cjs/types/key-deriver.js.map +1 -0
- package/dist/cjs/types/key-generator.js +3 -0
- package/dist/cjs/types/key-generator.js.map +1 -0
- package/dist/cjs/types/key-io.js +3 -0
- package/dist/cjs/types/key-io.js.map +1 -0
- package/dist/cjs/types/key-wrapper.js +3 -0
- package/dist/cjs/types/key-wrapper.js.map +1 -0
- package/dist/cjs/types/params-direct.js +3 -0
- package/dist/cjs/types/params-direct.js.map +1 -0
- package/dist/cjs/types/params-enclosed.js +3 -0
- package/dist/cjs/types/params-enclosed.js.map +1 -0
- package/dist/cjs/types/params-kms.js +3 -0
- package/dist/cjs/types/params-kms.js.map +1 -0
- package/dist/cjs/types/signer.js +3 -0
- package/dist/cjs/types/signer.js.map +1 -0
- package/dist/cjs/utils.js +173 -0
- package/dist/cjs/utils.js.map +1 -0
- package/dist/esm/algorithms/aes-ctr.js +124 -0
- package/dist/esm/algorithms/aes-ctr.js.map +1 -0
- package/dist/esm/algorithms/aes-gcm.js +132 -0
- package/dist/esm/algorithms/aes-gcm.js.map +1 -0
- package/dist/esm/algorithms/crypto-algorithm.js +6 -0
- package/dist/esm/algorithms/crypto-algorithm.js.map +1 -0
- package/dist/esm/algorithms/ecdsa.js +237 -0
- package/dist/esm/algorithms/ecdsa.js.map +1 -0
- package/dist/esm/algorithms/eddsa.js +213 -0
- package/dist/esm/algorithms/eddsa.js.map +1 -0
- package/dist/esm/algorithms/sha-2.js +57 -0
- package/dist/esm/algorithms/sha-2.js.map +1 -0
- package/dist/esm/index.js +25 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/jose/jwe.js +2 -0
- package/dist/esm/jose/jwe.js.map +1 -0
- package/dist/esm/jose/jwk.js +241 -0
- package/dist/esm/jose/jwk.js.map +1 -0
- package/dist/esm/jose/jws.js +2 -0
- package/dist/esm/jose/jws.js.map +1 -0
- package/dist/esm/jose/jwt.js +2 -0
- package/dist/esm/jose/jwt.js.map +1 -0
- package/dist/esm/jose/utils.js +34 -0
- package/dist/esm/jose/utils.js.map +1 -0
- package/dist/esm/local-key-manager.js +417 -0
- package/dist/esm/local-key-manager.js.map +1 -0
- package/dist/esm/primitives/aes-ctr.js +327 -0
- package/dist/esm/primitives/aes-ctr.js.map +1 -0
- package/dist/esm/primitives/aes-gcm.js +347 -0
- package/dist/esm/primitives/aes-gcm.js.map +1 -0
- package/dist/esm/primitives/concat-kdf.js +185 -0
- package/dist/esm/primitives/concat-kdf.js.map +1 -0
- package/dist/esm/primitives/ed25519.js +521 -0
- package/dist/esm/primitives/ed25519.js.map +1 -0
- package/dist/esm/primitives/pbkdf2.js +78 -0
- package/dist/esm/primitives/pbkdf2.js.map +1 -0
- package/dist/esm/primitives/secp256k1.js +805 -0
- package/dist/esm/primitives/secp256k1.js.map +1 -0
- package/dist/esm/primitives/secp256r1.js +806 -0
- package/dist/esm/primitives/secp256r1.js.map +1 -0
- package/dist/esm/primitives/sha256.js +55 -0
- package/dist/esm/primitives/sha256.js.map +1 -0
- package/dist/esm/primitives/x25519.js +392 -0
- package/dist/esm/primitives/x25519.js.map +1 -0
- package/dist/esm/primitives/xchacha20-poly1305.js +270 -0
- package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -0
- package/dist/esm/primitives/xchacha20.js +246 -0
- package/dist/esm/primitives/xchacha20.js.map +1 -0
- package/dist/esm/types/cipher.js +2 -0
- package/dist/esm/types/cipher.js.map +1 -0
- package/dist/esm/types/crypto-api.js +2 -0
- package/dist/esm/types/crypto-api.js.map +1 -0
- package/dist/esm/types/hasher.js +2 -0
- package/dist/esm/types/hasher.js.map +1 -0
- package/dist/esm/types/identifier.js +2 -0
- package/dist/esm/types/identifier.js.map +1 -0
- package/dist/esm/types/key-compressor.js +2 -0
- package/dist/esm/types/key-compressor.js.map +1 -0
- package/dist/esm/types/key-converter.js +2 -0
- package/dist/esm/types/key-converter.js.map +1 -0
- package/dist/esm/types/key-deriver.js +2 -0
- package/dist/esm/types/key-deriver.js.map +1 -0
- package/dist/esm/types/key-generator.js +2 -0
- package/dist/esm/types/key-generator.js.map +1 -0
- package/dist/esm/types/key-io.js +2 -0
- package/dist/esm/types/key-io.js.map +1 -0
- package/dist/esm/types/key-wrapper.js +2 -0
- package/dist/esm/types/key-wrapper.js.map +1 -0
- package/dist/esm/types/params-direct.js +2 -0
- package/dist/esm/types/params-direct.js.map +1 -0
- package/dist/esm/types/params-enclosed.js +2 -0
- package/dist/esm/types/params-enclosed.js.map +1 -0
- package/dist/esm/types/params-kms.js +2 -0
- package/dist/esm/types/params-kms.js.map +1 -0
- package/dist/esm/types/signer.js +2 -0
- package/dist/esm/types/signer.js.map +1 -0
- package/dist/esm/utils.js +165 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/types/algorithms/aes-ctr.d.ts +121 -0
- package/dist/types/algorithms/aes-ctr.d.ts.map +1 -0
- package/dist/types/algorithms/aes-gcm.d.ts +152 -0
- package/dist/types/algorithms/aes-gcm.d.ts.map +1 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts +6 -0
- package/dist/types/algorithms/crypto-algorithm.d.ts.map +1 -0
- package/dist/types/algorithms/ecdsa.d.ts +154 -0
- package/dist/types/algorithms/ecdsa.d.ts.map +1 -0
- package/dist/types/algorithms/eddsa.d.ts +151 -0
- package/dist/types/algorithms/eddsa.d.ts.map +1 -0
- package/dist/types/algorithms/sha-2.d.ts +51 -0
- package/dist/types/algorithms/sha-2.d.ts.map +1 -0
- package/dist/types/index.d.ts +39 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/jose/jwe.d.ts +135 -0
- package/dist/types/jose/jwe.d.ts.map +1 -0
- package/dist/types/jose/jwk.d.ts +439 -0
- package/dist/types/jose/jwk.d.ts.map +1 -0
- package/dist/types/jose/jws.d.ts +67 -0
- package/dist/types/jose/jws.d.ts.map +1 -0
- package/dist/types/jose/jwt.d.ts +139 -0
- package/dist/types/jose/jwt.d.ts.map +1 -0
- package/dist/types/jose/utils.d.ts +14 -0
- package/dist/types/jose/utils.d.ts.map +1 -0
- package/dist/types/local-key-manager.d.ts +307 -0
- package/dist/types/local-key-manager.d.ts.map +1 -0
- package/dist/types/primitives/aes-ctr.d.ts +219 -0
- package/dist/types/primitives/aes-ctr.d.ts.map +1 -0
- package/dist/types/primitives/aes-gcm.d.ts +245 -0
- package/dist/types/primitives/aes-gcm.d.ts.map +1 -0
- package/dist/types/primitives/concat-kdf.d.ts +160 -0
- package/dist/types/primitives/concat-kdf.d.ts.map +1 -0
- package/dist/types/primitives/ed25519.d.ts +359 -0
- package/dist/types/primitives/ed25519.d.ts.map +1 -0
- package/dist/types/primitives/pbkdf2.d.ts +94 -0
- package/dist/types/primitives/pbkdf2.d.ts.map +1 -0
- package/dist/types/primitives/secp256k1.d.ts +598 -0
- package/dist/types/primitives/secp256k1.d.ts.map +1 -0
- package/dist/types/primitives/secp256r1.d.ts +599 -0
- package/dist/types/primitives/secp256r1.d.ts.map +1 -0
- package/dist/types/primitives/sha256.d.ts +42 -0
- package/dist/types/primitives/sha256.d.ts.map +1 -0
- package/dist/types/primitives/x25519.d.ts +283 -0
- package/dist/types/primitives/x25519.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts +210 -0
- package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -0
- package/dist/types/primitives/xchacha20.d.ts +186 -0
- package/dist/types/primitives/xchacha20.d.ts.map +1 -0
- package/dist/types/types/cipher.d.ts +49 -0
- package/dist/types/types/cipher.d.ts.map +1 -0
- package/dist/types/types/crypto-api.d.ts +40 -0
- package/dist/types/types/crypto-api.d.ts.map +1 -0
- package/dist/types/types/hasher.d.ts +33 -0
- package/dist/types/types/hasher.d.ts.map +1 -0
- package/dist/types/types/identifier.d.ts +16 -0
- package/dist/types/types/identifier.d.ts.map +1 -0
- package/dist/types/types/key-compressor.d.ts +28 -0
- package/dist/types/types/key-compressor.d.ts.map +1 -0
- package/dist/types/types/key-converter.d.ts +57 -0
- package/dist/types/types/key-converter.d.ts.map +1 -0
- package/dist/types/types/key-deriver.d.ts +39 -0
- package/dist/types/types/key-deriver.d.ts.map +1 -0
- package/dist/types/types/key-generator.d.ts +105 -0
- package/dist/types/types/key-generator.d.ts.map +1 -0
- package/dist/types/types/key-io.d.ts +37 -0
- package/dist/types/types/key-io.d.ts.map +1 -0
- package/dist/types/types/key-wrapper.d.ts +38 -0
- package/dist/types/types/key-wrapper.d.ts.map +1 -0
- package/dist/types/types/params-direct.d.ts +90 -0
- package/dist/types/types/params-direct.d.ts.map +1 -0
- package/dist/types/types/params-enclosed.d.ts +47 -0
- package/dist/types/types/params-enclosed.d.ts.map +1 -0
- package/dist/types/types/params-kms.d.ts +131 -0
- package/dist/types/types/params-kms.d.ts.map +1 -0
- package/dist/types/types/signer.d.ts +46 -0
- package/dist/types/types/signer.d.ts.map +1 -0
- package/dist/types/utils.d.ts +112 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/dist/utils.js +7 -0
- package/dist/utils.js.map +7 -0
- package/package.json +103 -0
- package/src/algorithms/aes-ctr.ts +156 -0
- package/src/algorithms/aes-gcm.ts +187 -0
- package/src/algorithms/crypto-algorithm.ts +4 -0
- package/src/algorithms/ecdsa.ts +269 -0
- package/src/algorithms/eddsa.ts +243 -0
- package/src/algorithms/sha-2.ts +65 -0
- package/src/index.ts +42 -0
- package/src/jose/jwe.ts +196 -0
- package/src/jose/jwk.ts +632 -0
- package/src/jose/jws.ts +95 -0
- package/src/jose/jwt.ts +147 -0
- package/src/jose/utils.ts +34 -0
- package/src/local-key-manager.ts +540 -0
- package/src/primitives/aes-ctr.ts +352 -0
- package/src/primitives/aes-gcm.ts +378 -0
- package/src/primitives/concat-kdf.ts +240 -0
- package/src/primitives/ed25519.ts +548 -0
- package/src/primitives/pbkdf2.ts +122 -0
- package/src/primitives/secp256k1.ts +848 -0
- package/src/primitives/secp256r1.ts +850 -0
- package/src/primitives/sha256.ts +47 -0
- package/src/primitives/x25519.ts +403 -0
- package/src/primitives/xchacha20-poly1305.ts +274 -0
- package/src/primitives/xchacha20.ts +247 -0
- package/src/types/cipher.ts +53 -0
- package/src/types/crypto-api.ts +56 -0
- package/src/types/hasher.ts +32 -0
- package/src/types/identifier.ts +16 -0
- package/src/types/key-compressor.ts +25 -0
- package/src/types/key-converter.ts +53 -0
- package/src/types/key-deriver.ts +43 -0
- package/src/types/key-generator.ts +119 -0
- package/src/types/key-io.ts +42 -0
- package/src/types/key-wrapper.ts +42 -0
- package/src/types/params-direct.ts +106 -0
- package/src/types/params-enclosed.ts +50 -0
- package/src/types/params-kms.ts +156 -0
- package/src/types/signer.ts +50 -0
- package/src/utils.ts +181 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __rest = (this && this.__rest) || function (s, e) {
|
|
11
|
+
var t = {};
|
|
12
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
13
|
+
t[p] = s[p];
|
|
14
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
15
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
16
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
17
|
+
t[p[i]] = s[p[i]];
|
|
18
|
+
}
|
|
19
|
+
return t;
|
|
20
|
+
};
|
|
21
|
+
import { Convert } from '@enbox/common';
|
|
22
|
+
import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
|
|
23
|
+
import { computeJwkThumbprint, isOctPrivateJwk } from '../jose/jwk.js';
|
|
24
|
+
/**
|
|
25
|
+
* Const defining the AES-GCM initialization vector (IV) length in bits.
|
|
26
|
+
*
|
|
27
|
+
* @remarks
|
|
28
|
+
* NIST Special Publication 800-38D, Section 5.2.1.1 states that the IV length:
|
|
29
|
+
* > For IVs, it is recommended that implementations restrict support to the length of 96 bits, to
|
|
30
|
+
* > promote interoperability, efficiency, and simplicity of design.
|
|
31
|
+
*
|
|
32
|
+
* This implementation does not support IV lengths that are different from the value defined by
|
|
33
|
+
* this constant.
|
|
34
|
+
*
|
|
35
|
+
* @see {@link https://doi.org/10.6028/NIST.SP.800-38D | NIST SP 800-38D}
|
|
36
|
+
*/
|
|
37
|
+
const AES_GCM_IV_LENGTH = 96;
|
|
38
|
+
/**
|
|
39
|
+
* Constant defining the AES key length values in bits.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* NIST publication FIPS 197 states:
|
|
43
|
+
* > The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt
|
|
44
|
+
* > and decrypt data in blocks of 128 bits.
|
|
45
|
+
*
|
|
46
|
+
* This implementation does not support key lengths that are different from the three values
|
|
47
|
+
* defined by this constant.
|
|
48
|
+
*
|
|
49
|
+
* @see {@link https://doi.org/10.6028/NIST.FIPS.197-upd1 | NIST FIPS 197}
|
|
50
|
+
*/
|
|
51
|
+
const AES_KEY_LENGTHS = [128, 192, 256];
|
|
52
|
+
/**
|
|
53
|
+
* Constant defining the AES-GCM tag length values in bits.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* NIST Special Publication 800-38D, Section 5.2.1.2 states that the tag length:
|
|
57
|
+
* > may be any one of the following five values: 128, 120, 112, 104, or 96
|
|
58
|
+
*
|
|
59
|
+
* Although the NIST specification allows for tag lengths of 32 or 64 bits in certain applications,
|
|
60
|
+
* the use of shorter tag lengths can be problematic for GCM due to targeted forgery attacks. As a
|
|
61
|
+
* precaution, this implementation does not support tag lengths that are different from the five
|
|
62
|
+
* values defined by this constant. See Appendix C of the NIST SP 800-38D specification for
|
|
63
|
+
* additional guidance and details.
|
|
64
|
+
*
|
|
65
|
+
* @see {@link https://doi.org/10.6028/NIST.SP.800-38D | NIST SP 800-38D}
|
|
66
|
+
*/
|
|
67
|
+
export const AES_GCM_TAG_LENGTHS = [96, 104, 112, 120, 128];
|
|
68
|
+
/**
|
|
69
|
+
* The `AesGcm` class provides a comprehensive set of utilities for cryptographic operations
|
|
70
|
+
* using the Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM). This class includes
|
|
71
|
+
* methods for key generation, encryption, decryption, and conversions between raw byte arrays
|
|
72
|
+
* and JSON Web Key (JWK) formats. It is designed to support AES-GCM, a symmetric key algorithm
|
|
73
|
+
* that is widely used for its efficiency, security, and provision of authenticated encryption.
|
|
74
|
+
*
|
|
75
|
+
* AES-GCM is particularly favored for scenarios that require both confidentiality and integrity
|
|
76
|
+
* of data. It integrates the counter mode of encryption with the Galois mode of authentication,
|
|
77
|
+
* offering high performance and parallel processing capabilities.
|
|
78
|
+
*
|
|
79
|
+
* Key Features:
|
|
80
|
+
* - Key Generation: Generate AES symmetric keys in JWK format.
|
|
81
|
+
* - Key Conversion: Transform keys between raw byte arrays and JWK formats.
|
|
82
|
+
* - Encryption: Encrypt data using AES-GCM with the provided symmetric key.
|
|
83
|
+
* - Decryption: Decrypt data encrypted with AES-GCM using the corresponding symmetric key.
|
|
84
|
+
*
|
|
85
|
+
* The methods in this class are asynchronous, returning Promises to accommodate various
|
|
86
|
+
* JavaScript environments.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* // Key Generation
|
|
91
|
+
* const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
|
|
92
|
+
* const privateKey = await AesGcm.generateKey({ length });
|
|
93
|
+
*
|
|
94
|
+
* // Encryption
|
|
95
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
96
|
+
* const iv = new Uint8Array(12); // 12-byte initialization vector
|
|
97
|
+
* const encryptedData = await AesGcm.encrypt({
|
|
98
|
+
* data,
|
|
99
|
+
* iv,
|
|
100
|
+
* key: privateKey
|
|
101
|
+
* });
|
|
102
|
+
*
|
|
103
|
+
* // Decryption
|
|
104
|
+
* const decryptedData = await AesGcm.decrypt({
|
|
105
|
+
* data: encryptedData,
|
|
106
|
+
* iv,
|
|
107
|
+
* key: privateKey
|
|
108
|
+
* });
|
|
109
|
+
*
|
|
110
|
+
* // Key Conversion
|
|
111
|
+
* const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
export class AesGcm {
|
|
115
|
+
/**
|
|
116
|
+
* Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
|
|
117
|
+
*
|
|
118
|
+
* @remarks
|
|
119
|
+
* This method accepts a symmetric key represented as a byte array (Uint8Array) and
|
|
120
|
+
* converts it into a JWK object for use with AES-GCM (Advanced Encryption Standard -
|
|
121
|
+
* Galois/Counter Mode). The conversion process involves encoding the key into
|
|
122
|
+
* base64url format and setting the appropriate JWK parameters.
|
|
123
|
+
*
|
|
124
|
+
* The resulting JWK object includes the following properties:
|
|
125
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
|
|
126
|
+
* - `k`: The symmetric key, base64url-encoded.
|
|
127
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
|
|
132
|
+
* const privateKey = await AesGcm.bytesToPrivateKey({ privateKeyBytes });
|
|
133
|
+
* ```
|
|
134
|
+
*
|
|
135
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
136
|
+
* @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
|
|
137
|
+
*
|
|
138
|
+
* @returns A Promise that resolves to the symmetric key in JWK format.
|
|
139
|
+
*/
|
|
140
|
+
static bytesToPrivateKey(_a) {
|
|
141
|
+
return __awaiter(this, arguments, void 0, function* ({ privateKeyBytes }) {
|
|
142
|
+
// Construct the private key in JWK format.
|
|
143
|
+
const privateKey = {
|
|
144
|
+
k: Convert.uint8Array(privateKeyBytes).toBase64Url(),
|
|
145
|
+
kty: 'oct'
|
|
146
|
+
};
|
|
147
|
+
// Compute the JWK thumbprint and set as the key ID.
|
|
148
|
+
privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
|
|
149
|
+
return privateKey;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Decrypts the provided data using AES-GCM.
|
|
154
|
+
*
|
|
155
|
+
* @remarks
|
|
156
|
+
* This method performs AES-GCM decryption on the given encrypted data using the specified key.
|
|
157
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
158
|
+
* and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
|
|
159
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
160
|
+
* tag used when encrypting the data. If not specified, the default tag length of 128 bits is
|
|
161
|
+
* used.
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const encryptedData = new Uint8Array([...]); // Encrypted data
|
|
166
|
+
* const iv = new Uint8Array([...]); // Initialization vector used during encryption
|
|
167
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
168
|
+
* const key = { ... }; // A Jwk object representing the AES key
|
|
169
|
+
* const decryptedData = await AesGcm.decrypt({
|
|
170
|
+
* data: encryptedData,
|
|
171
|
+
* iv,
|
|
172
|
+
* additionalData,
|
|
173
|
+
* key,
|
|
174
|
+
* tagLength: 128 // Optional tag length in bits
|
|
175
|
+
* });
|
|
176
|
+
* ```
|
|
177
|
+
*
|
|
178
|
+
* @param params - The parameters for the decryption operation.
|
|
179
|
+
* @param params.key - The key to use for decryption, represented in JWK format.
|
|
180
|
+
* @param params.data - The encrypted data to decrypt, represented as a Uint8Array.
|
|
181
|
+
* @param params.iv - The initialization vector, represented as a Uint8Array.
|
|
182
|
+
* @param params.additionalData - Optional additional authenticated data. Optional.
|
|
183
|
+
* @param params.tagLength - The length of the authentication tag in bits. Optional.
|
|
184
|
+
*
|
|
185
|
+
* @returns A Promise that resolves to the decrypted data as a Uint8Array.
|
|
186
|
+
*/
|
|
187
|
+
static decrypt(_a) {
|
|
188
|
+
return __awaiter(this, arguments, void 0, function* ({ key, data, iv, additionalData, tagLength }) {
|
|
189
|
+
// Validate the initialization vector length.
|
|
190
|
+
if (iv.byteLength !== AES_GCM_IV_LENGTH / 8) {
|
|
191
|
+
throw new TypeError(`The initialization vector must be ${AES_GCM_IV_LENGTH} bits in length`);
|
|
192
|
+
}
|
|
193
|
+
// Validate the tag length.
|
|
194
|
+
if (tagLength && !AES_GCM_TAG_LENGTHS.includes(tagLength)) {
|
|
195
|
+
throw new RangeError(`The tag length is invalid: Must be ${AES_GCM_TAG_LENGTHS.join(', ')} bits`);
|
|
196
|
+
}
|
|
197
|
+
// Get the Web Crypto API interface.
|
|
198
|
+
const webCrypto = getWebcryptoSubtle();
|
|
199
|
+
// Import the JWK into the Web Crypto API to use for the decrypt operation.
|
|
200
|
+
const webCryptoKey = yield webCrypto.importKey('jwk', key, { name: 'AES-GCM' }, true, ['decrypt']);
|
|
201
|
+
// Note: Some browser implementations of the Web Crypto API throw an error if additionalData or
|
|
202
|
+
// tagLength are undefined, so only include them in the algorithm object if they are defined.
|
|
203
|
+
const algorithm = Object.assign(Object.assign({ name: 'AES-GCM', iv }, (tagLength && { tagLength })), (additionalData && { additionalData }));
|
|
204
|
+
// Decrypt the data.
|
|
205
|
+
const plaintextBuffer = yield webCrypto.decrypt(algorithm, webCryptoKey, data);
|
|
206
|
+
// Convert from ArrayBuffer to Uint8Array.
|
|
207
|
+
const plaintext = new Uint8Array(plaintextBuffer);
|
|
208
|
+
return plaintext;
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Encrypts the provided data using AES-GCM.
|
|
213
|
+
*
|
|
214
|
+
* @remarks
|
|
215
|
+
* This method performs AES-GCM encryption on the given data using the specified key.
|
|
216
|
+
* It requires an initialization vector (IV), the encrypted data along with the decryption key,
|
|
217
|
+
* and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
|
|
218
|
+
* Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
|
|
219
|
+
* tag generated in the encryption operation and used for authentication in the corresponding
|
|
220
|
+
* decryption. If not specified, the default tag length of 128 bits is used.
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```ts
|
|
224
|
+
* const data = new TextEncoder().encode('Messsage');
|
|
225
|
+
* const iv = new Uint8Array([...]); // Initialization vector
|
|
226
|
+
* const additionalData = new Uint8Array([...]); // Optional additional authenticated data
|
|
227
|
+
* const key = { ... }; // A Jwk object representing an AES key
|
|
228
|
+
* const encryptedData = await AesGcm.encrypt({
|
|
229
|
+
* data,
|
|
230
|
+
* iv,
|
|
231
|
+
* additionalData,
|
|
232
|
+
* key,
|
|
233
|
+
* tagLength: 128 // Optional tag length in bits
|
|
234
|
+
* });
|
|
235
|
+
* ```
|
|
236
|
+
*
|
|
237
|
+
* @param params - The parameters for the encryption operation.
|
|
238
|
+
* @param params.key - The key to use for encryption, represented in JWK format.
|
|
239
|
+
* @param params.data - The data to encrypt, represented as a Uint8Array.
|
|
240
|
+
* @param params.iv - The initialization vector, represented as a Uint8Array.
|
|
241
|
+
* @param params.additionalData - Optional additional authenticated data. Optional.
|
|
242
|
+
* @param params.tagLength - The length of the authentication tag in bits. Optional.
|
|
243
|
+
*
|
|
244
|
+
* @returns A Promise that resolves to the encrypted data as a Uint8Array.
|
|
245
|
+
*/
|
|
246
|
+
static encrypt(_a) {
|
|
247
|
+
return __awaiter(this, arguments, void 0, function* ({ data, iv, key, additionalData, tagLength }) {
|
|
248
|
+
// Validate the initialization vector length.
|
|
249
|
+
if (iv.byteLength !== AES_GCM_IV_LENGTH / 8) {
|
|
250
|
+
throw new TypeError(`The initialization vector must be ${AES_GCM_IV_LENGTH} bits in length`);
|
|
251
|
+
}
|
|
252
|
+
// Validate the tag length.
|
|
253
|
+
if (tagLength && !AES_GCM_TAG_LENGTHS.includes(tagLength)) {
|
|
254
|
+
throw new RangeError(`The tag length is invalid: Must be ${AES_GCM_TAG_LENGTHS.join(', ')} bits`);
|
|
255
|
+
}
|
|
256
|
+
// Get the Web Crypto API interface.
|
|
257
|
+
const webCrypto = getWebcryptoSubtle();
|
|
258
|
+
// Import the JWK into the Web Crypto API to use for the encrypt operation.
|
|
259
|
+
const webCryptoKey = yield webCrypto.importKey('jwk', key, { name: 'AES-GCM' }, true, ['encrypt']);
|
|
260
|
+
// Note: Some browser implementations of the Web Crypto API throw an error if additionalData or
|
|
261
|
+
// tagLength are undefined, so only include them in the algorithm object if they are defined.
|
|
262
|
+
const algorithm = Object.assign(Object.assign({ name: 'AES-GCM', iv }, (tagLength && { tagLength })), (additionalData && { additionalData }));
|
|
263
|
+
// Encrypt the data.
|
|
264
|
+
const ciphertextBuffer = yield webCrypto.encrypt(algorithm, webCryptoKey, data);
|
|
265
|
+
// Convert from ArrayBuffer to Uint8Array.
|
|
266
|
+
const ciphertext = new Uint8Array(ciphertextBuffer);
|
|
267
|
+
return ciphertext;
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
|
|
272
|
+
*
|
|
273
|
+
* @remarks
|
|
274
|
+
* This method creates a new symmetric key of a specified length suitable for use with
|
|
275
|
+
* AES-GCM encryption. It leverages cryptographically secure random number generation
|
|
276
|
+
* to ensure the uniqueness and security of the key. The generated key adheres to the JWK
|
|
277
|
+
* format, facilitating compatibility with common cryptographic standards and ease of use
|
|
278
|
+
* in various cryptographic applications.
|
|
279
|
+
*
|
|
280
|
+
* The generated key includes these components:
|
|
281
|
+
* - `kty`: Key Type, set to 'oct' for Octet Sequence, indicating a symmetric key.
|
|
282
|
+
* - `k`: The symmetric key component, base64url-encoded.
|
|
283
|
+
* - `kid`: Key ID, generated based on the JWK thumbprint, providing a unique identifier.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```ts
|
|
287
|
+
* const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
|
|
288
|
+
* const privateKey = await AesGcm.generateKey({ length });
|
|
289
|
+
* ```
|
|
290
|
+
*
|
|
291
|
+
* @param params - The parameters for the key generation.
|
|
292
|
+
* @param params.length - The length of the key in bits. Common lengths are 128, 192, and 256 bits.
|
|
293
|
+
*
|
|
294
|
+
* @returns A Promise that resolves to the generated symmetric key in JWK format.
|
|
295
|
+
*/
|
|
296
|
+
static generateKey(_a) {
|
|
297
|
+
return __awaiter(this, arguments, void 0, function* ({ length }) {
|
|
298
|
+
// Validate the key length.
|
|
299
|
+
if (!AES_KEY_LENGTHS.includes(length)) {
|
|
300
|
+
throw new RangeError(`The key length is invalid: Must be ${AES_KEY_LENGTHS.join(', ')} bits`);
|
|
301
|
+
}
|
|
302
|
+
// Get the Web Crypto API interface.
|
|
303
|
+
const webCrypto = getWebcryptoSubtle();
|
|
304
|
+
// Generate a random private key.
|
|
305
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues#usage_notes for
|
|
306
|
+
// an explanation for why Web Crypto generateKey() is used instead of getRandomValues().
|
|
307
|
+
const webCryptoKey = yield webCrypto.generateKey({ name: 'AES-GCM', length }, true, ['encrypt']);
|
|
308
|
+
// Export the private key in JWK format.
|
|
309
|
+
const _b = yield webCrypto.exportKey('jwk', webCryptoKey), { ext, key_ops } = _b, privateKey = __rest(_b, ["ext", "key_ops"]);
|
|
310
|
+
// Compute the JWK thumbprint and set as the key ID.
|
|
311
|
+
privateKey.kid = yield computeJwkThumbprint({ jwk: privateKey });
|
|
312
|
+
return privateKey;
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
/**
|
|
316
|
+
* Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
|
|
317
|
+
*
|
|
318
|
+
* @remarks
|
|
319
|
+
* This method takes a symmetric key in JWK format and extracts its raw byte representation.
|
|
320
|
+
* It focuses on the 'k' parameter of the JWK, which represents the symmetric key component
|
|
321
|
+
* in base64url encoding. The method decodes this value into a byte array, providing
|
|
322
|
+
* the symmetric key in its raw binary form.
|
|
323
|
+
*
|
|
324
|
+
* @example
|
|
325
|
+
* ```ts
|
|
326
|
+
* const privateKey = { ... }; // A symmetric key in JWK format
|
|
327
|
+
* const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
|
|
328
|
+
* ```
|
|
329
|
+
*
|
|
330
|
+
* @param params - The parameters for the symmetric key conversion.
|
|
331
|
+
* @param params.privateKey - The symmetric key in JWK format.
|
|
332
|
+
*
|
|
333
|
+
* @returns A Promise that resolves to the symmetric key as a Uint8Array.
|
|
334
|
+
*/
|
|
335
|
+
static privateKeyToBytes(_a) {
|
|
336
|
+
return __awaiter(this, arguments, void 0, function* ({ privateKey }) {
|
|
337
|
+
// Verify the provided JWK represents a valid oct private key.
|
|
338
|
+
if (!isOctPrivateJwk(privateKey)) {
|
|
339
|
+
throw new Error(`AesGcm: The provided key is not a valid oct private key.`);
|
|
340
|
+
}
|
|
341
|
+
// Decode the provided private key to bytes.
|
|
342
|
+
const privateKeyBytes = Convert.base64Url(privateKey.k).toUint8Array();
|
|
343
|
+
return privateKeyBytes;
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
//# sourceMappingURL=aes-gcm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"aes-gcm.js","sourceRoot":"","sources":["../../../src/primitives/aes-gcm.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AACxC,OAAO,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAI9D,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAEvE;;;;;;;;;;;;GAYG;AACH,MAAM,iBAAiB,GAAG,EAAE,CAAC;AAE7B;;;;;;;;;;;;GAYG;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AAkE,EAA7D,UAAU,cAA7B,kBAA+B,CAAiD,CAAC;YAEvF,oDAAoD;YACpD,UAAU,CAAC,GAAG,GAAG,MAAM,oBAAoB,CAAC,EAAE,GAAG,EAAE,UAAU,EAAE,CAAC,CAAC;YAEjE,OAAO,UAAU,CAAC;QACpB,CAAC;KAAA;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACI,MAAM,CAAO,iBAAiB;6DAAC,EAAE,UAAU,EAEjD;YACC,8DAA8D;YAC9D,IAAI,CAAC,eAAe,CAAC,UAAU,CAAC,EAAE,CAAC;gBACjC,MAAM,IAAI,KAAK,CAAC,0DAA0D,CAAC,CAAC;YAC9E,CAAC;YAED,4CAA4C;YAC5C,MAAM,eAAe,GAAG,OAAO,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,YAAY,EAAE,CAAC;YAEvE,OAAO,eAAe,CAAC;QACzB,CAAC;KAAA;CACF"}
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import { sha256 } from '@noble/hashes/sha256';
|
|
11
|
+
import { Convert, universalTypeOf } from '@enbox/common';
|
|
12
|
+
import { concatBytes } from '@noble/hashes/utils';
|
|
13
|
+
/**
|
|
14
|
+
* An implementation of the Concatenation Key Derivation Function (ConcatKDF)
|
|
15
|
+
* as specified in NIST.800-56A, a single-step key-derivation function (SSKDF).
|
|
16
|
+
* ConcatKDF produces a derived key from a secret key (like a shared secret
|
|
17
|
+
* from ECDH), and other optional public information. This implementation
|
|
18
|
+
* specifically uses SHA-256 as the pseudorandom function (PRF).
|
|
19
|
+
*
|
|
20
|
+
* Note: This implementation allows for only a single round / repetition using the function
|
|
21
|
+
* `K(1) = H(counter || Z || FixedInfo)`, where:
|
|
22
|
+
* - `K(1)` is the derived key material after one round
|
|
23
|
+
* - `H` is the SHA-256 hashing function
|
|
24
|
+
* - `counter` is a 32-bit, big-endian bit string counter set to 0x00000001
|
|
25
|
+
* - `Z` is the shared secret value obtained from a key agreement protocol
|
|
26
|
+
* - `FixedInfo` is a bit string used to ensure that the derived keying material is adequately
|
|
27
|
+
* "bound" to the key-agreement transaction.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* // Key Derivation
|
|
32
|
+
* const derivedKeyingMaterial = await ConcatKdf.deriveKey({
|
|
33
|
+
* sharedSecret: utils.randomBytes(32),
|
|
34
|
+
* keyDataLen: 128,
|
|
35
|
+
* fixedInfo: {
|
|
36
|
+
* algorithmId: "A128GCM",
|
|
37
|
+
* partyUInfo: "Alice",
|
|
38
|
+
* partyVInfo: "Bob",
|
|
39
|
+
* suppPubInfo: 128,
|
|
40
|
+
* },
|
|
41
|
+
* });
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* Additional Information:
|
|
45
|
+
*
|
|
46
|
+
* `Z`, or "shared secret":
|
|
47
|
+
* The shared secret value obtained from a key agreement protocol, such as
|
|
48
|
+
* Diffie-Hellman, ECDH (Elliptic Curve Diffie-Hellman). Importantly, this
|
|
49
|
+
* shared secret is not directly used as the encryption or authentication
|
|
50
|
+
* key, but as an input to a key derivation function (KDF), such as Concat
|
|
51
|
+
* KDF, to generate the actual key. This adds an extra layer of security, as
|
|
52
|
+
* even if the shared secret gets compromised, the actual encryption or
|
|
53
|
+
* authentication key stays safe. This shared secret `Z` value is kept
|
|
54
|
+
* confidential between the two parties in the key agreement protocol.
|
|
55
|
+
*
|
|
56
|
+
* @see {@link https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf | NIST.800-56A}
|
|
57
|
+
* @see {@link https://datatracker.ietf.org/doc/html/rfc7518#section-4.6.2 | RFC 7518, Section 4.6.2}
|
|
58
|
+
*/
|
|
59
|
+
export class ConcatKdf {
|
|
60
|
+
/**
|
|
61
|
+
* Derives a key of a specified length from the input parameters.
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* ```ts
|
|
65
|
+
* // Key Derivation
|
|
66
|
+
* const derivedKeyingMaterial = await ConcatKdf.deriveKey({
|
|
67
|
+
* sharedSecret: utils.randomBytes(32),
|
|
68
|
+
* keyDataLen: 128,
|
|
69
|
+
* fixedInfo: {
|
|
70
|
+
* algorithmId: "A128GCM",
|
|
71
|
+
* partyUInfo: "Alice",
|
|
72
|
+
* partyVInfo: "Bob",
|
|
73
|
+
* suppPubInfo: 128,
|
|
74
|
+
* },
|
|
75
|
+
* });
|
|
76
|
+
* ```
|
|
77
|
+
*
|
|
78
|
+
* @param params - Input parameters for key derivation.
|
|
79
|
+
* @param params.keyDataLen - The desired length of the derived key in bits.
|
|
80
|
+
* @param params.sharedSecret - The shared secret key to derive from.
|
|
81
|
+
* @param params.fixedInfo - Additional public information to use in key derivation.
|
|
82
|
+
* @returns The derived key as a Uint8Array.
|
|
83
|
+
*
|
|
84
|
+
* @throws {Error} If the `keyDataLen` would require multiple rounds.
|
|
85
|
+
*/
|
|
86
|
+
static deriveKey(_a) {
|
|
87
|
+
return __awaiter(this, arguments, void 0, function* ({ keyDataLen, fixedInfo, sharedSecret }) {
|
|
88
|
+
// RFC 7518 Section 4.6.2 specifies using SHA-256 for ECDH key agreement:
|
|
89
|
+
// "Key derivation is performed using the Concat KDF, as defined in
|
|
90
|
+
// Section 5.8.1 of [NIST.800-56A], where the Digest Method is SHA-256."
|
|
91
|
+
// Reference: https://tools.ietf.org/html/rfc7518#section-4.6.2
|
|
92
|
+
const hashLen = 256;
|
|
93
|
+
// This implementation only supports single round Concat KDF.
|
|
94
|
+
const roundCount = Math.ceil(keyDataLen / hashLen);
|
|
95
|
+
if (roundCount !== 1) {
|
|
96
|
+
throw new Error(`Concat KDF with ${roundCount} rounds not supported.`);
|
|
97
|
+
}
|
|
98
|
+
// Initialize a 32-bit, big-endian bit string counter as 0x00000001.
|
|
99
|
+
const counter = new Uint8Array(4);
|
|
100
|
+
new DataView(counter.buffer).setUint32(0, roundCount);
|
|
101
|
+
// Compute the FixedInfo bit-string.
|
|
102
|
+
const fixedInfoBytes = ConcatKdf.computeFixedInfo(fixedInfo);
|
|
103
|
+
// Compute K(i) = H(counter || Z || FixedInfo)
|
|
104
|
+
// return concatBytes(counter, sharedSecretZ, fixedInfo);
|
|
105
|
+
const derivedKeyingMaterial = sha256(concatBytes(counter, sharedSecret, fixedInfoBytes));
|
|
106
|
+
// Return the bit string of derived keying material of length keyDataLen bits.
|
|
107
|
+
return derivedKeyingMaterial.slice(0, keyDataLen / 8);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Computes the `FixedInfo` parameter for Concat KDF, which binds the derived key material to the
|
|
112
|
+
* context of the key agreement transaction.
|
|
113
|
+
*
|
|
114
|
+
* @remarks
|
|
115
|
+
* This implementation follows the recommended format for `FixedInfo` specified in section
|
|
116
|
+
* 5.8.1.2.1 of the NIST.800-56A publication.
|
|
117
|
+
*
|
|
118
|
+
* `FixedInfo` is a bit string equal to the following concatenation:
|
|
119
|
+
* `AlgorithmID || PartyUInfo || PartyVInfo {|| SuppPubInfo }{|| SuppPrivInfo }`.
|
|
120
|
+
*
|
|
121
|
+
* `SuppPubInfo` is the key length in bits, big endian encoded as a 32-bit number. For example,
|
|
122
|
+
* 128 would be [0, 0, 0, 128] and 256 would be [0, 0, 1, 0].
|
|
123
|
+
*
|
|
124
|
+
* @param params - Input data to construct FixedInfo.
|
|
125
|
+
* @returns FixedInfo as a Uint8Array.
|
|
126
|
+
*/
|
|
127
|
+
static computeFixedInfo(params) {
|
|
128
|
+
// Required sub-fields.
|
|
129
|
+
const algorithmId = ConcatKdf.toDataLenData({ data: params.algorithmId });
|
|
130
|
+
const partyUInfo = ConcatKdf.toDataLenData({ data: params.partyUInfo });
|
|
131
|
+
const partyVInfo = ConcatKdf.toDataLenData({ data: params.partyVInfo });
|
|
132
|
+
// Optional sub-fields.
|
|
133
|
+
const suppPubInfo = ConcatKdf.toDataLenData({ data: params.suppPubInfo, variableLength: false });
|
|
134
|
+
const suppPrivInfo = ConcatKdf.toDataLenData({ data: params.suppPrivInfo });
|
|
135
|
+
// Concatenate AlgorithmID || PartyUInfo || PartyVInfo || SuppPubInfo || SuppPrivInfo.
|
|
136
|
+
const fixedInfo = concatBytes(algorithmId, partyUInfo, partyVInfo, suppPubInfo, suppPrivInfo);
|
|
137
|
+
return fixedInfo;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Encodes input data as a length-prefixed byte string, or
|
|
141
|
+
* as a fixed-length bit string if specified.
|
|
142
|
+
*
|
|
143
|
+
* If variableLength = true, return the data in the form Datalen || Data,
|
|
144
|
+
* where Data is a variable-length string of zero or more (eight-bit)
|
|
145
|
+
* bytes, and Datalen is a fixed-length, big-endian counter that
|
|
146
|
+
* indicates the length (in bytes) of Data.
|
|
147
|
+
*
|
|
148
|
+
* If variableLength = false, return the data formatted as a
|
|
149
|
+
* fixed-length bit string.
|
|
150
|
+
*
|
|
151
|
+
* @param params - Input data and options for the conversion.
|
|
152
|
+
* @param params.data - The input data to encode. Must be a type convertible to Uint8Array by the Convert class.
|
|
153
|
+
* @param params.variableLength - Whether to output the data as variable length. Default is true.
|
|
154
|
+
*
|
|
155
|
+
* @returns The input data encoded as a Uint8Array.
|
|
156
|
+
*
|
|
157
|
+
* @throws {TypeError} If fixed-length data is not a number.
|
|
158
|
+
*/
|
|
159
|
+
static toDataLenData({ data, variableLength = true }) {
|
|
160
|
+
let encodedData;
|
|
161
|
+
const dataType = universalTypeOf(data);
|
|
162
|
+
// Return an emtpy octet sequence if data is not specified.
|
|
163
|
+
if (dataType === 'Undefined') {
|
|
164
|
+
return new Uint8Array(0);
|
|
165
|
+
}
|
|
166
|
+
if (variableLength) {
|
|
167
|
+
const dataU8A = (dataType === 'Uint8Array')
|
|
168
|
+
? data
|
|
169
|
+
: new Convert(data, dataType).toUint8Array();
|
|
170
|
+
const bufferLength = dataU8A.length;
|
|
171
|
+
encodedData = new Uint8Array(4 + bufferLength);
|
|
172
|
+
new DataView(encodedData.buffer).setUint32(0, bufferLength);
|
|
173
|
+
encodedData.set(dataU8A, 4);
|
|
174
|
+
}
|
|
175
|
+
else {
|
|
176
|
+
if (typeof data !== 'number') {
|
|
177
|
+
throw TypeError('Fixed length input must be a number.');
|
|
178
|
+
}
|
|
179
|
+
encodedData = new Uint8Array(4);
|
|
180
|
+
new DataView(encodedData.buffer).setUint32(0, data);
|
|
181
|
+
}
|
|
182
|
+
return encodedData;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
//# sourceMappingURL=concat-kdf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"concat-kdf.js","sourceRoot":"","sources":["../../../src/primitives/concat-kdf.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AACzD,OAAO,EAAc,WAAW,EAAE,MAAM,qBAAqB,CAAC;AA+C9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,MAAM,OAAO,SAAS;IACpB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;IACI,MAAM,CAAO,SAAS;6DAAC,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAIlE;YACC,yEAAyE;YACzE,mEAAm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