@lindorm/aes 0.5.5 → 0.6.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/CHANGELOG.md +26 -0
- package/MERMAID.md +155 -0
- package/README.md +365 -199
- package/__tests__/INTEROP-RESULTS.md +66 -0
- package/__tests__/esm-smoke.test.ts +15 -0
- package/__tests__/fixtures/keys.ts +60 -0
- package/__tests__/helpers/buffer-utils.ts +11 -0
- package/__tests__/helpers/index.ts +2 -0
- package/__tests__/helpers/jwe-adapter.ts +117 -0
- package/__tests__/jose-jwe.test.ts +463 -0
- package/__tests__/noble-ciphers.test.ts +208 -0
- package/dist/classes/AesKit.d.ts +10 -8
- package/dist/classes/AesKit.d.ts.map +1 -1
- package/dist/classes/AesKit.js +73 -34
- package/dist/classes/AesKit.js.map +1 -1
- package/dist/constants/private/index.d.ts +0 -1
- package/dist/constants/private/index.d.ts.map +1 -1
- package/dist/constants/private/index.js +0 -1
- package/dist/constants/private/index.js.map +1 -1
- package/dist/constants/private/version.d.ts +3 -1
- package/dist/constants/private/version.d.ts.map +1 -1
- package/dist/constants/private/version.js +4 -2
- package/dist/constants/private/version.js.map +1 -1
- package/dist/interfaces/AesKit.d.ts +12 -7
- package/dist/interfaces/AesKit.d.ts.map +1 -1
- package/dist/mocks/mock-aes-kit.d.ts.map +1 -1
- package/dist/mocks/mock-aes-kit.js +12 -2
- package/dist/mocks/mock-aes-kit.js.map +1 -1
- package/dist/types/aes-decryption-data.d.ts +26 -17
- package/dist/types/aes-decryption-data.d.ts.map +1 -1
- package/dist/types/aes-encryption-data.d.ts +7 -17
- package/dist/types/aes-encryption-data.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/private/aes-data.d.ts.map +1 -1
- package/dist/types/private/aes-key-derivation.d.ts +1 -1
- package/dist/types/private/aes-key-derivation.d.ts.map +1 -1
- package/dist/types/private/auth-tag.d.ts +3 -0
- package/dist/types/private/auth-tag.d.ts.map +1 -1
- package/dist/types/private/content-encryption-key.d.ts +4 -2
- package/dist/types/private/content-encryption-key.d.ts.map +1 -1
- package/dist/types/private/index.d.ts +1 -1
- package/dist/types/private/index.d.ts.map +1 -1
- package/dist/types/private/index.js +1 -1
- package/dist/types/private/index.js.map +1 -1
- package/dist/types/private/prepared-encryption.d.ts +35 -0
- package/dist/types/private/prepared-encryption.d.ts.map +1 -0
- package/dist/types/private/{aes-string.js → prepared-encryption.js} +1 -1
- package/dist/types/private/prepared-encryption.js.map +1 -0
- package/dist/utils/is-aes.d.ts.map +1 -1
- package/dist/utils/is-aes.js +1 -5
- package/dist/utils/is-aes.js.map +1 -1
- package/dist/utils/parse-aes.js +3 -3
- package/dist/utils/parse-aes.js.map +1 -1
- package/dist/utils/private/aes-header.d.ts +42 -0
- package/dist/utils/private/aes-header.d.ts.map +1 -0
- package/dist/utils/private/aes-header.js +75 -0
- package/dist/utils/private/aes-header.js.map +1 -0
- package/dist/utils/private/calculate/calculate-content-encryption-key-size.js +3 -3
- package/dist/utils/private/calculate/calculate-key-wrap-encryption.d.ts.map +1 -1
- package/dist/utils/private/calculate/calculate-key-wrap-encryption.js +2 -1
- package/dist/utils/private/calculate/calculate-key-wrap-encryption.js.map +1 -1
- package/dist/utils/private/data/auth-tag-hmac.d.ts +2 -2
- package/dist/utils/private/data/auth-tag-hmac.d.ts.map +1 -1
- package/dist/utils/private/data/auth-tag-hmac.js +12 -4
- package/dist/utils/private/data/auth-tag-hmac.js.map +1 -1
- package/dist/utils/private/data/auth-tag.d.ts +2 -2
- package/dist/utils/private/data/auth-tag.d.ts.map +1 -1
- package/dist/utils/private/data/auth-tag.js +4 -2
- package/dist/utils/private/data/auth-tag.js.map +1 -1
- package/dist/utils/private/data/split-content-encryption-key.d.ts.map +1 -1
- package/dist/utils/private/data/split-content-encryption-key.js +6 -2
- package/dist/utils/private/data/split-content-encryption-key.js.map +1 -1
- package/dist/utils/private/diffie-hellman/diffie-hellman-key-wrap.d.ts +2 -2
- package/dist/utils/private/diffie-hellman/diffie-hellman-key-wrap.d.ts.map +1 -1
- package/dist/utils/private/diffie-hellman/diffie-hellman-key-wrap.js +12 -8
- package/dist/utils/private/diffie-hellman/diffie-hellman-key-wrap.js.map +1 -1
- package/dist/utils/private/diffie-hellman/diffie-hellman.d.ts +2 -2
- package/dist/utils/private/diffie-hellman/diffie-hellman.d.ts.map +1 -1
- package/dist/utils/private/diffie-hellman/diffie-hellman.js +12 -8
- package/dist/utils/private/diffie-hellman/diffie-hellman.js.map +1 -1
- package/dist/utils/private/diffie-hellman/shared-secret.d.ts.map +1 -1
- package/dist/utils/private/diffie-hellman/shared-secret.js +5 -1
- package/dist/utils/private/diffie-hellman/shared-secret.js.map +1 -1
- package/dist/utils/private/encoded-aes.d.ts +2 -2
- package/dist/utils/private/encoded-aes.d.ts.map +1 -1
- package/dist/utils/private/encoded-aes.js +86 -149
- package/dist/utils/private/encoded-aes.js.map +1 -1
- package/dist/utils/private/encrypt-content.d.ts +3 -0
- package/dist/utils/private/encrypt-content.d.ts.map +1 -0
- package/dist/utils/private/encrypt-content.js +35 -0
- package/dist/utils/private/encrypt-content.js.map +1 -0
- package/dist/utils/private/encrypt-encoded.d.ts +9 -0
- package/dist/utils/private/encrypt-encoded.d.ts.map +1 -0
- package/dist/utils/private/encrypt-encoded.js +53 -0
- package/dist/utils/private/encrypt-encoded.js.map +1 -0
- package/dist/utils/private/encrypt-serialised.d.ts +9 -0
- package/dist/utils/private/encrypt-serialised.d.ts.map +1 -0
- package/dist/utils/private/encrypt-serialised.js +48 -0
- package/dist/utils/private/encrypt-serialised.js.map +1 -0
- package/dist/utils/private/encrypt-tokenised.d.ts +9 -0
- package/dist/utils/private/encrypt-tokenised.d.ts.map +1 -0
- package/dist/utils/private/encrypt-tokenised.js +45 -0
- package/dist/utils/private/encrypt-tokenised.js.map +1 -0
- package/dist/utils/private/encryption.d.ts.map +1 -1
- package/dist/utils/private/encryption.js +27 -27
- package/dist/utils/private/encryption.js.map +1 -1
- package/dist/utils/private/index.d.ts +6 -0
- package/dist/utils/private/index.d.ts.map +1 -1
- package/dist/utils/private/index.js +6 -0
- package/dist/utils/private/index.js.map +1 -1
- package/dist/utils/private/key-derivation/concat-kdf.d.ts +14 -0
- package/dist/utils/private/key-derivation/concat-kdf.d.ts.map +1 -0
- package/dist/utils/private/key-derivation/concat-kdf.js +26 -0
- package/dist/utils/private/key-derivation/concat-kdf.js.map +1 -0
- package/dist/utils/private/key-derivation/index.d.ts +1 -1
- package/dist/utils/private/key-derivation/index.d.ts.map +1 -1
- package/dist/utils/private/key-derivation/index.js +1 -1
- package/dist/utils/private/key-derivation/index.js.map +1 -1
- package/dist/utils/private/key-derivation/pbkdf.d.ts +1 -0
- package/dist/utils/private/key-derivation/pbkdf.d.ts.map +1 -1
- package/dist/utils/private/key-derivation/pbkdf.js +13 -2
- package/dist/utils/private/key-derivation/pbkdf.js.map +1 -1
- package/dist/utils/private/key-wrap/ecb-key-wrap.d.ts.map +1 -1
- package/dist/utils/private/key-wrap/ecb-key-wrap.js +10 -3
- package/dist/utils/private/key-wrap/ecb-key-wrap.js.map +1 -1
- package/dist/utils/private/key-wrap/gcm-key-wrap.d.ts.map +1 -1
- package/dist/utils/private/key-wrap/gcm-key-wrap.js +6 -0
- package/dist/utils/private/key-wrap/gcm-key-wrap.js.map +1 -1
- package/dist/utils/private/oct/get-oct-key-key-wrap.d.ts +1 -1
- package/dist/utils/private/oct/get-oct-key-key-wrap.d.ts.map +1 -1
- package/dist/utils/private/oct/get-oct-key-key-wrap.js +7 -14
- package/dist/utils/private/oct/get-oct-key-key-wrap.js.map +1 -1
- package/dist/utils/private/oct/get-oct-pbkdf-key-wrap-keys.d.ts.map +1 -1
- package/dist/utils/private/oct/get-oct-pbkdf-key-wrap-keys.js +2 -0
- package/dist/utils/private/oct/get-oct-pbkdf-key-wrap-keys.js.map +1 -1
- package/dist/utils/private/prepare-encryption.d.ts +3 -0
- package/dist/utils/private/prepare-encryption.d.ts.map +1 -0
- package/dist/utils/private/prepare-encryption.js +27 -0
- package/dist/utils/private/prepare-encryption.js.map +1 -0
- package/dist/utils/private/serialised-aes.d.ts.map +1 -1
- package/dist/utils/private/serialised-aes.js +38 -46
- package/dist/utils/private/serialised-aes.js.map +1 -1
- package/dist/utils/private/tokenised-aes.d.ts +3 -3
- package/dist/utils/private/tokenised-aes.d.ts.map +1 -1
- package/dist/utils/private/tokenised-aes.js +73 -55
- package/dist/utils/private/tokenised-aes.js.map +1 -1
- package/dist/utils/private/validate-version.d.ts +2 -0
- package/dist/utils/private/validate-version.d.ts.map +1 -0
- package/dist/utils/private/validate-version.js +27 -0
- package/dist/utils/private/validate-version.js.map +1 -0
- package/jest.config.interop.mjs +24 -0
- package/package.json +16 -15
- package/tsconfig.interop.json +9 -0
- package/dist/constants/private/format.d.ts +0 -2
- package/dist/constants/private/format.d.ts.map +0 -1
- package/dist/constants/private/format.js +0 -5
- package/dist/constants/private/format.js.map +0 -1
- package/dist/types/private/aes-string.d.ts +0 -21
- package/dist/types/private/aes-string.d.ts.map +0 -1
- package/dist/types/private/aes-string.js.map +0 -1
- package/dist/utils/private/key-derivation/hkdf.d.ts +0 -13
- package/dist/utils/private/key-derivation/hkdf.d.ts.map +0 -1
- package/dist/utils/private/key-derivation/hkdf.js +0 -12
- package/dist/utils/private/key-derivation/hkdf.js.map +0 -1
|
@@ -0,0 +1,463 @@
|
|
|
1
|
+
import { createCipheriv, CipherGCM, randomBytes } from "crypto";
|
|
2
|
+
import { FlattenedEncrypt, flattenedDecrypt } from "jose";
|
|
3
|
+
import { encryptAes, decryptAes } from "../src/utils/private/encryption";
|
|
4
|
+
import { createHmacAuthTag } from "../src/utils/private/data/auth-tag-hmac";
|
|
5
|
+
import { splitContentEncryptionKey } from "../src/utils/private/data/split-content-encryption-key";
|
|
6
|
+
import { toUint8Array } from "./helpers/buffer-utils";
|
|
7
|
+
import {
|
|
8
|
+
buildProtectedHeader,
|
|
9
|
+
toFlattenedJWE,
|
|
10
|
+
fromFlattenedJWE,
|
|
11
|
+
} from "./helpers/jwe-adapter";
|
|
12
|
+
import {
|
|
13
|
+
RAW_KEY_128,
|
|
14
|
+
RAW_KEY_192,
|
|
15
|
+
RAW_KEY_256,
|
|
16
|
+
RAW_KEY_256_CBC,
|
|
17
|
+
RAW_KEY_384_CBC,
|
|
18
|
+
RAW_KEY_512_CBC,
|
|
19
|
+
KEK_128,
|
|
20
|
+
KEK_192,
|
|
21
|
+
KEK_256,
|
|
22
|
+
createOctKryptos,
|
|
23
|
+
} from "./fixtures/keys";
|
|
24
|
+
|
|
25
|
+
const PLAINTEXT = "hello jose interop";
|
|
26
|
+
const PLAINTEXT_BYTES = new TextEncoder().encode(PLAINTEXT);
|
|
27
|
+
|
|
28
|
+
// ---------------------------------------------------------------------------
|
|
29
|
+
// 4.1 dir + AES-GCM Cross-Reference
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
|
|
32
|
+
describe("jose JWE interop: dir + AES-GCM", () => {
|
|
33
|
+
describe.each([
|
|
34
|
+
{ enc: "A128GCM" as const, key: RAW_KEY_128 },
|
|
35
|
+
{ enc: "A192GCM" as const, key: RAW_KEY_192 },
|
|
36
|
+
{ enc: "A256GCM" as const, key: RAW_KEY_256 },
|
|
37
|
+
])("dir + $enc", ({ enc, key }) => {
|
|
38
|
+
test("our encrypt -> jose decrypt", async () => {
|
|
39
|
+
const kryptos = createOctKryptos(key, "dir", enc);
|
|
40
|
+
const { headerB64u, aadBuffer } = buildProtectedHeader("dir", enc);
|
|
41
|
+
|
|
42
|
+
const record = encryptAes({
|
|
43
|
+
data: PLAINTEXT,
|
|
44
|
+
encryption: enc,
|
|
45
|
+
kryptos,
|
|
46
|
+
aad: aadBuffer,
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
const jwe = toFlattenedJWE(record, headerB64u);
|
|
50
|
+
const result = await flattenedDecrypt(jwe, toUint8Array(key));
|
|
51
|
+
|
|
52
|
+
expect(Buffer.from(result.plaintext).toString("utf8")).toBe(PLAINTEXT);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("jose encrypt -> our decrypt", async () => {
|
|
56
|
+
const kryptos = createOctKryptos(key, "dir", enc);
|
|
57
|
+
|
|
58
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
59
|
+
.setProtectedHeader({ alg: "dir", enc })
|
|
60
|
+
.encrypt(toUint8Array(key));
|
|
61
|
+
|
|
62
|
+
const { record, aad } = fromFlattenedJWE(jwe);
|
|
63
|
+
|
|
64
|
+
const result = decryptAes({
|
|
65
|
+
...record,
|
|
66
|
+
aad,
|
|
67
|
+
contentType: "text/plain",
|
|
68
|
+
kryptos,
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
expect(result).toBe(PLAINTEXT);
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
// 4.2 dir + AES-CBC-HMAC Cross-Reference
|
|
78
|
+
// ---------------------------------------------------------------------------
|
|
79
|
+
|
|
80
|
+
describe("jose JWE interop: dir + AES-CBC-HMAC", () => {
|
|
81
|
+
describe.each([
|
|
82
|
+
{ enc: "A128CBC-HS256" as const, key: RAW_KEY_256_CBC },
|
|
83
|
+
{ enc: "A192CBC-HS384" as const, key: RAW_KEY_384_CBC },
|
|
84
|
+
{ enc: "A256CBC-HS512" as const, key: RAW_KEY_512_CBC },
|
|
85
|
+
])("dir + $enc", ({ enc, key }) => {
|
|
86
|
+
test("our encrypt -> jose decrypt", async () => {
|
|
87
|
+
const kryptos = createOctKryptos(key, "dir", enc);
|
|
88
|
+
const { headerB64u, aadBuffer } = buildProtectedHeader("dir", enc);
|
|
89
|
+
|
|
90
|
+
const record = encryptAes({
|
|
91
|
+
data: PLAINTEXT,
|
|
92
|
+
encryption: enc,
|
|
93
|
+
kryptos,
|
|
94
|
+
aad: aadBuffer,
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const jwe = toFlattenedJWE(record, headerB64u);
|
|
98
|
+
const result = await flattenedDecrypt(jwe, toUint8Array(key));
|
|
99
|
+
|
|
100
|
+
expect(Buffer.from(result.plaintext).toString("utf8")).toBe(PLAINTEXT);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("jose encrypt -> our decrypt", async () => {
|
|
104
|
+
const kryptos = createOctKryptos(key, "dir", enc);
|
|
105
|
+
|
|
106
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
107
|
+
.setProtectedHeader({ alg: "dir", enc })
|
|
108
|
+
.encrypt(toUint8Array(key));
|
|
109
|
+
|
|
110
|
+
const { record, aad } = fromFlattenedJWE(jwe);
|
|
111
|
+
|
|
112
|
+
const result = decryptAes({
|
|
113
|
+
...record,
|
|
114
|
+
aad,
|
|
115
|
+
contentType: "text/plain",
|
|
116
|
+
kryptos,
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
expect(result).toBe(PLAINTEXT);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
// ---------------------------------------------------------------------------
|
|
125
|
+
// 4.3 A*KW + AES-GCM Cross-Reference
|
|
126
|
+
// ---------------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
describe("jose JWE interop: A*KW + AES-GCM", () => {
|
|
129
|
+
describe.each([
|
|
130
|
+
{ alg: "A128KW" as const, kek: KEK_128 },
|
|
131
|
+
{ alg: "A192KW" as const, kek: KEK_192 },
|
|
132
|
+
{ alg: "A256KW" as const, kek: KEK_256 },
|
|
133
|
+
])("$alg", ({ alg, kek }) => {
|
|
134
|
+
describe.each([{ enc: "A128GCM" as const }, { enc: "A256GCM" as const }])(
|
|
135
|
+
"+ $enc",
|
|
136
|
+
({ enc }) => {
|
|
137
|
+
test("our encrypt -> jose decrypt", async () => {
|
|
138
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
139
|
+
const { headerB64u, aadBuffer } = buildProtectedHeader(alg, enc);
|
|
140
|
+
|
|
141
|
+
const record = encryptAes({
|
|
142
|
+
data: PLAINTEXT,
|
|
143
|
+
encryption: enc,
|
|
144
|
+
kryptos,
|
|
145
|
+
aad: aadBuffer,
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const jwe = toFlattenedJWE(record, headerB64u);
|
|
149
|
+
const result = await flattenedDecrypt(jwe, toUint8Array(kek));
|
|
150
|
+
|
|
151
|
+
expect(Buffer.from(result.plaintext).toString("utf8")).toBe(PLAINTEXT);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
test("jose encrypt -> our decrypt", async () => {
|
|
155
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
156
|
+
|
|
157
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
158
|
+
.setProtectedHeader({ alg, enc })
|
|
159
|
+
.encrypt(toUint8Array(kek));
|
|
160
|
+
|
|
161
|
+
const { record, aad } = fromFlattenedJWE(jwe);
|
|
162
|
+
|
|
163
|
+
const result = decryptAes({
|
|
164
|
+
...record,
|
|
165
|
+
aad,
|
|
166
|
+
contentType: "text/plain",
|
|
167
|
+
kryptos,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
expect(result).toBe(PLAINTEXT);
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
);
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
// ---------------------------------------------------------------------------
|
|
178
|
+
// 4.4 A*KW + AES-CBC-HMAC Cross-Reference
|
|
179
|
+
// ---------------------------------------------------------------------------
|
|
180
|
+
|
|
181
|
+
describe("jose JWE interop: A*KW + AES-CBC-HMAC", () => {
|
|
182
|
+
describe.each([
|
|
183
|
+
{ alg: "A128KW" as const, enc: "A128CBC-HS256" as const, kek: KEK_128 },
|
|
184
|
+
{ alg: "A192KW" as const, enc: "A192CBC-HS384" as const, kek: KEK_192 },
|
|
185
|
+
{ alg: "A256KW" as const, enc: "A256CBC-HS512" as const, kek: KEK_256 },
|
|
186
|
+
])("$alg + $enc", ({ alg, enc, kek }) => {
|
|
187
|
+
test("our encrypt -> jose decrypt", async () => {
|
|
188
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
189
|
+
const { headerB64u, aadBuffer } = buildProtectedHeader(alg, enc);
|
|
190
|
+
|
|
191
|
+
const record = encryptAes({
|
|
192
|
+
data: PLAINTEXT,
|
|
193
|
+
encryption: enc,
|
|
194
|
+
kryptos,
|
|
195
|
+
aad: aadBuffer,
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
const jwe = toFlattenedJWE(record, headerB64u);
|
|
199
|
+
const result = await flattenedDecrypt(jwe, toUint8Array(kek));
|
|
200
|
+
|
|
201
|
+
expect(Buffer.from(result.plaintext).toString("utf8")).toBe(PLAINTEXT);
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
test("jose encrypt -> our decrypt", async () => {
|
|
205
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
206
|
+
|
|
207
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
208
|
+
.setProtectedHeader({ alg, enc })
|
|
209
|
+
.encrypt(toUint8Array(kek));
|
|
210
|
+
|
|
211
|
+
const { record, aad } = fromFlattenedJWE(jwe);
|
|
212
|
+
|
|
213
|
+
const result = decryptAes({
|
|
214
|
+
...record,
|
|
215
|
+
aad,
|
|
216
|
+
contentType: "text/plain",
|
|
217
|
+
kryptos,
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
expect(result).toBe(PLAINTEXT);
|
|
221
|
+
});
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
// ---------------------------------------------------------------------------
|
|
226
|
+
// 4.5 A*GCMKW + AES-GCM Cross-Reference
|
|
227
|
+
// ---------------------------------------------------------------------------
|
|
228
|
+
|
|
229
|
+
describe("jose JWE interop: A*GCMKW + AES-GCM", () => {
|
|
230
|
+
describe.each([
|
|
231
|
+
{ alg: "A128GCMKW" as const, kek: KEK_128 },
|
|
232
|
+
{ alg: "A256GCMKW" as const, kek: KEK_256 },
|
|
233
|
+
])("$alg + A256GCM", ({ alg, kek }) => {
|
|
234
|
+
const enc = "A256GCM" as const;
|
|
235
|
+
|
|
236
|
+
test("jose encrypt -> our decrypt", async () => {
|
|
237
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
238
|
+
|
|
239
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
240
|
+
.setProtectedHeader({ alg, enc })
|
|
241
|
+
.encrypt(toUint8Array(kek));
|
|
242
|
+
|
|
243
|
+
const { record, aad } = fromFlattenedJWE(jwe);
|
|
244
|
+
|
|
245
|
+
const result = decryptAes({
|
|
246
|
+
...record,
|
|
247
|
+
aad,
|
|
248
|
+
contentType: "text/plain",
|
|
249
|
+
kryptos,
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
expect(result).toBe(PLAINTEXT);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
// Our encrypt -> jose decrypt for GCMKW has a circular dependency:
|
|
256
|
+
// The protected header must contain the key-wrap iv and tag (per RFC 7518 section 4.7),
|
|
257
|
+
// but the content encryption AAD is computed from the protected header (per RFC 7516
|
|
258
|
+
// section 5.1 step 14). Our encryptAes() generates the key-wrap params and encrypts
|
|
259
|
+
// content in a single pass, so we cannot know the final header (and therefore the
|
|
260
|
+
// correct AAD) before encryption.
|
|
261
|
+
//
|
|
262
|
+
// A two-pass approach (encrypt once to get kw params, build header, re-encrypt with
|
|
263
|
+
// correct AAD) would require exposing the CEK from the first pass, which our API
|
|
264
|
+
// does not currently support via the public encryptAes interface.
|
|
265
|
+
//
|
|
266
|
+
// The jose->ours direction above validates that our decryption is fully compatible.
|
|
267
|
+
// Byte-level correctness of our GCM key-wrap is separately validated in the
|
|
268
|
+
// noble-ciphers cross-reference tests.
|
|
269
|
+
test("our encrypt -> jose decrypt", async () => {
|
|
270
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
271
|
+
|
|
272
|
+
// Perform encryption without AAD first to get the key-wrap parameters
|
|
273
|
+
const record = encryptAes({
|
|
274
|
+
data: PLAINTEXT,
|
|
275
|
+
encryption: enc,
|
|
276
|
+
kryptos,
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
// Build the protected header including the GCMKW iv and tag
|
|
280
|
+
const { headerB64u, aadBuffer } = buildProtectedHeader(alg, enc, {
|
|
281
|
+
iv: record.publicEncryptionIv!,
|
|
282
|
+
tag: record.publicEncryptionTag!,
|
|
283
|
+
});
|
|
284
|
+
|
|
285
|
+
// Re-encrypt with correct AAD using the same CEK that was wrapped.
|
|
286
|
+
// We can recover the CEK by unwrapping the encrypted_key.
|
|
287
|
+
const unwrapKryptos = createOctKryptos(kek, alg, enc);
|
|
288
|
+
const { contentEncryptionKey } = (
|
|
289
|
+
await import("../src/utils/private/key-wrap/gcm-key-wrap")
|
|
290
|
+
).gcmKeyUnwrap({
|
|
291
|
+
keyEncryptionKey: kek,
|
|
292
|
+
kryptos: unwrapKryptos,
|
|
293
|
+
publicEncryptionIv: record.publicEncryptionIv!,
|
|
294
|
+
publicEncryptionKey: record.publicEncryptionKey!,
|
|
295
|
+
publicEncryptionTag: record.publicEncryptionTag!,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Re-encrypt content with the recovered CEK and correct AAD
|
|
299
|
+
const { encryptionKey } = splitContentEncryptionKey(enc, contentEncryptionKey);
|
|
300
|
+
const iv = randomBytes(12);
|
|
301
|
+
const cipher = createCipheriv("aes-256-gcm", encryptionKey, iv, {
|
|
302
|
+
authTagLength: 16,
|
|
303
|
+
}) as CipherGCM;
|
|
304
|
+
cipher.setAAD(aadBuffer);
|
|
305
|
+
|
|
306
|
+
const plaintextBuf = Buffer.from(PLAINTEXT, "utf8");
|
|
307
|
+
const ciphertext = Buffer.concat([cipher.update(plaintextBuf), cipher.final()]);
|
|
308
|
+
const authTag = cipher.getAuthTag();
|
|
309
|
+
|
|
310
|
+
const jwe = {
|
|
311
|
+
protected: headerB64u,
|
|
312
|
+
iv: iv.toString("base64url"),
|
|
313
|
+
ciphertext: ciphertext.toString("base64url"),
|
|
314
|
+
tag: authTag.toString("base64url"),
|
|
315
|
+
encrypted_key: record.publicEncryptionKey!.toString("base64url"),
|
|
316
|
+
};
|
|
317
|
+
|
|
318
|
+
const result = await flattenedDecrypt(jwe, toUint8Array(kek));
|
|
319
|
+
expect(Buffer.from(result.plaintext).toString("utf8")).toBe(PLAINTEXT);
|
|
320
|
+
});
|
|
321
|
+
});
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
// ---------------------------------------------------------------------------
|
|
325
|
+
// 4.6 Deterministic Pinned Tests
|
|
326
|
+
// ---------------------------------------------------------------------------
|
|
327
|
+
//
|
|
328
|
+
// jose v6 does not allow setContentEncryptionKey() with alg: "dir".
|
|
329
|
+
// We use A256KW / A128KW key-wrap algorithms instead, which do allow CEK
|
|
330
|
+
// pinning. This still validates byte-identical ciphertext because the
|
|
331
|
+
// content encryption is the same regardless of the key management algorithm.
|
|
332
|
+
// ---------------------------------------------------------------------------
|
|
333
|
+
|
|
334
|
+
describe("jose JWE interop: deterministic pinned tests", () => {
|
|
335
|
+
describe("A256KW + A256GCM", () => {
|
|
336
|
+
const alg = "A256KW" as const;
|
|
337
|
+
const enc = "A256GCM" as const;
|
|
338
|
+
const kek = KEK_256;
|
|
339
|
+
const cek = Buffer.from(
|
|
340
|
+
"e1e2e3e4e5e6e7e8e9eaebecedeeeff0f1f2f3f4f5f6f7f8f9fafbfcfdfeff00",
|
|
341
|
+
"hex",
|
|
342
|
+
);
|
|
343
|
+
const iv = Buffer.from("0a0b0c0d0e0f10111213141516", "hex").subarray(0, 12);
|
|
344
|
+
const plaintextBuf = Buffer.from(PLAINTEXT, "utf8");
|
|
345
|
+
|
|
346
|
+
test("byte-identical ciphertext and auth tag", async () => {
|
|
347
|
+
// jose: encrypt with pinned CEK and IV
|
|
348
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
349
|
+
.setProtectedHeader({ alg, enc })
|
|
350
|
+
.setContentEncryptionKey(toUint8Array(cek))
|
|
351
|
+
.setInitializationVector(toUint8Array(iv))
|
|
352
|
+
.encrypt(toUint8Array(kek));
|
|
353
|
+
|
|
354
|
+
// The AAD is the ASCII bytes of the base64url-encoded protected header
|
|
355
|
+
const joseAad = Buffer.from(jwe.protected!, "ascii");
|
|
356
|
+
|
|
357
|
+
// Our side: raw createCipheriv with the same CEK, IV, and AAD
|
|
358
|
+
const cipher = createCipheriv("aes-256-gcm", cek, iv, {
|
|
359
|
+
authTagLength: 16,
|
|
360
|
+
}) as CipherGCM;
|
|
361
|
+
cipher.setAAD(joseAad);
|
|
362
|
+
|
|
363
|
+
const ourCiphertext = Buffer.concat([cipher.update(plaintextBuf), cipher.final()]);
|
|
364
|
+
const ourTag = cipher.getAuthTag();
|
|
365
|
+
|
|
366
|
+
const joseCiphertext = Buffer.from(jwe.ciphertext, "base64url");
|
|
367
|
+
const joseTag = Buffer.from(jwe.tag!, "base64url");
|
|
368
|
+
|
|
369
|
+
expect(ourCiphertext.equals(joseCiphertext)).toBe(true);
|
|
370
|
+
expect(ourTag.equals(joseTag)).toBe(true);
|
|
371
|
+
});
|
|
372
|
+
|
|
373
|
+
test("round-trip with pinned values through decryptAes", async () => {
|
|
374
|
+
// jose: encrypt with pinned CEK and IV
|
|
375
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
376
|
+
.setProtectedHeader({ alg, enc })
|
|
377
|
+
.setContentEncryptionKey(toUint8Array(cek))
|
|
378
|
+
.setInitializationVector(toUint8Array(iv))
|
|
379
|
+
.encrypt(toUint8Array(kek));
|
|
380
|
+
|
|
381
|
+
// Parse with our adapter and decrypt
|
|
382
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
383
|
+
const { record, aad } = fromFlattenedJWE(jwe);
|
|
384
|
+
|
|
385
|
+
const result = decryptAes({
|
|
386
|
+
...record,
|
|
387
|
+
aad,
|
|
388
|
+
contentType: "text/plain",
|
|
389
|
+
kryptos,
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
expect(result).toBe(PLAINTEXT);
|
|
393
|
+
});
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
describe("A128KW + A128CBC-HS256", () => {
|
|
397
|
+
const alg = "A128KW" as const;
|
|
398
|
+
const enc = "A128CBC-HS256" as const;
|
|
399
|
+
const kek = KEK_128;
|
|
400
|
+
// A128CBC-HS256 CEK is 32 bytes: first 16 = HMAC key, last 16 = AES key
|
|
401
|
+
const cek = Buffer.from(
|
|
402
|
+
"a1a2a3a4a5a6a7a8a9aaabacadaeafb0b1b2b3b4b5b6b7b8b9babbbcbdbebfc0",
|
|
403
|
+
"hex",
|
|
404
|
+
);
|
|
405
|
+
const iv = Buffer.alloc(16, 0xdd);
|
|
406
|
+
const plaintextBuf = Buffer.from(PLAINTEXT, "utf8");
|
|
407
|
+
|
|
408
|
+
test("byte-identical ciphertext and auth tag", async () => {
|
|
409
|
+
// jose: encrypt with pinned CEK and IV
|
|
410
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
411
|
+
.setProtectedHeader({ alg, enc })
|
|
412
|
+
.setContentEncryptionKey(toUint8Array(cek))
|
|
413
|
+
.setInitializationVector(toUint8Array(iv))
|
|
414
|
+
.encrypt(toUint8Array(kek));
|
|
415
|
+
|
|
416
|
+
const joseAad = Buffer.from(jwe.protected!, "ascii");
|
|
417
|
+
|
|
418
|
+
// Split CEK per RFC 7518 Section 5.2
|
|
419
|
+
const { encryptionKey, hashKey } = splitContentEncryptionKey(enc, cek);
|
|
420
|
+
|
|
421
|
+
// Our side: raw createCipheriv (CBC) with same key and IV
|
|
422
|
+
const cipher = createCipheriv("aes-128-cbc", encryptionKey, iv);
|
|
423
|
+
const ourCiphertext = Buffer.concat([cipher.update(plaintextBuf), cipher.final()]);
|
|
424
|
+
|
|
425
|
+
// HMAC auth tag per RFC 7518 Section 5.2.2.1
|
|
426
|
+
const ourTag = createHmacAuthTag({
|
|
427
|
+
aad: joseAad,
|
|
428
|
+
content: ourCiphertext,
|
|
429
|
+
encryption: enc,
|
|
430
|
+
hashKey,
|
|
431
|
+
initialisationVector: iv,
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
const joseCiphertext = Buffer.from(jwe.ciphertext, "base64url");
|
|
435
|
+
const joseTag = Buffer.from(jwe.tag!, "base64url");
|
|
436
|
+
|
|
437
|
+
expect(ourCiphertext.equals(joseCiphertext)).toBe(true);
|
|
438
|
+
expect(ourTag.equals(joseTag)).toBe(true);
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
test("round-trip with pinned values through decryptAes", async () => {
|
|
442
|
+
// jose: encrypt with pinned CEK and IV
|
|
443
|
+
const jwe = await new FlattenedEncrypt(PLAINTEXT_BYTES)
|
|
444
|
+
.setProtectedHeader({ alg, enc })
|
|
445
|
+
.setContentEncryptionKey(toUint8Array(cek))
|
|
446
|
+
.setInitializationVector(toUint8Array(iv))
|
|
447
|
+
.encrypt(toUint8Array(kek));
|
|
448
|
+
|
|
449
|
+
// Parse with our adapter and decrypt
|
|
450
|
+
const kryptos = createOctKryptos(kek, alg, enc);
|
|
451
|
+
const { record, aad } = fromFlattenedJWE(jwe);
|
|
452
|
+
|
|
453
|
+
const result = decryptAes({
|
|
454
|
+
...record,
|
|
455
|
+
aad,
|
|
456
|
+
contentType: "text/plain",
|
|
457
|
+
kryptos,
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
expect(result).toBe(PLAINTEXT);
|
|
461
|
+
});
|
|
462
|
+
});
|
|
463
|
+
});
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { createCipheriv, randomBytes } from "crypto";
|
|
2
|
+
import { gcm, cbc, aeskw } from "@noble/ciphers/aes";
|
|
3
|
+
import { encryptAes, decryptAes } from "../src/utils/private/encryption";
|
|
4
|
+
import { ecbKeyWrap, ecbKeyUnwrap } from "../src/utils/private/key-wrap/ecb-key-wrap";
|
|
5
|
+
import { toUint8Array, toBuffer } from "./helpers/buffer-utils";
|
|
6
|
+
import {
|
|
7
|
+
RAW_KEY_128,
|
|
8
|
+
RAW_KEY_192,
|
|
9
|
+
RAW_KEY_256,
|
|
10
|
+
RAW_KEY_256_CBC,
|
|
11
|
+
RAW_KEY_384_CBC,
|
|
12
|
+
RAW_KEY_512_CBC,
|
|
13
|
+
KEK_128,
|
|
14
|
+
KEK_192,
|
|
15
|
+
KEK_256,
|
|
16
|
+
createOctKryptos,
|
|
17
|
+
} from "./fixtures/keys";
|
|
18
|
+
|
|
19
|
+
const GCM_TAG_LENGTH = 16;
|
|
20
|
+
const GCM_IV_LENGTH = 12;
|
|
21
|
+
const AES_BLOCK_SIZE = 16;
|
|
22
|
+
|
|
23
|
+
// ---------------------------------------------------------------------------
|
|
24
|
+
// 3.1 AES-GCM Cross-Reference
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
|
|
27
|
+
describe("AES-GCM cross-reference with @noble/ciphers", () => {
|
|
28
|
+
describe.each([
|
|
29
|
+
{ name: "A128GCM", key: RAW_KEY_128, encryption: "A128GCM" as const },
|
|
30
|
+
{ name: "A192GCM", key: RAW_KEY_192, encryption: "A192GCM" as const },
|
|
31
|
+
{ name: "A256GCM", key: RAW_KEY_256, encryption: "A256GCM" as const },
|
|
32
|
+
])("$name", ({ key, encryption }) => {
|
|
33
|
+
const plaintext = "test plaintext";
|
|
34
|
+
|
|
35
|
+
test("our encryptAes -> noble gcm decrypt", () => {
|
|
36
|
+
const kryptos = createOctKryptos(key, "dir", encryption);
|
|
37
|
+
|
|
38
|
+
const record = encryptAes({ data: plaintext, encryption, kryptos });
|
|
39
|
+
|
|
40
|
+
const iv = toUint8Array(record.initialisationVector);
|
|
41
|
+
const ciphertext = toUint8Array(record.content);
|
|
42
|
+
const tag = toUint8Array(record.authTag);
|
|
43
|
+
|
|
44
|
+
// noble expects ciphertext || tag concatenated
|
|
45
|
+
const ciphertextWithTag = new Uint8Array(ciphertext.length + tag.length);
|
|
46
|
+
ciphertextWithTag.set(ciphertext, 0);
|
|
47
|
+
ciphertextWithTag.set(tag, ciphertext.length);
|
|
48
|
+
|
|
49
|
+
const decrypted = gcm(toUint8Array(key), iv).decrypt(ciphertextWithTag);
|
|
50
|
+
const result = new TextDecoder().decode(decrypted);
|
|
51
|
+
|
|
52
|
+
expect(result).toBe(plaintext);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
test("noble gcm encrypt -> our decryptAes", () => {
|
|
56
|
+
const kryptos = createOctKryptos(key, "dir", encryption);
|
|
57
|
+
const iv = randomBytes(GCM_IV_LENGTH);
|
|
58
|
+
const plaintextBytes = new TextEncoder().encode(plaintext);
|
|
59
|
+
|
|
60
|
+
const encrypted = gcm(toUint8Array(key), toUint8Array(iv)).encrypt(plaintextBytes);
|
|
61
|
+
|
|
62
|
+
// noble returns ciphertext || tag (last 16 bytes are the auth tag)
|
|
63
|
+
const ciphertext = toBuffer(encrypted.slice(0, -GCM_TAG_LENGTH));
|
|
64
|
+
const authTag = toBuffer(encrypted.slice(-GCM_TAG_LENGTH));
|
|
65
|
+
|
|
66
|
+
const result = decryptAes({
|
|
67
|
+
content: ciphertext,
|
|
68
|
+
authTag,
|
|
69
|
+
initialisationVector: iv,
|
|
70
|
+
encryption,
|
|
71
|
+
contentType: "text/plain",
|
|
72
|
+
kryptos,
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
expect(result).toBe(plaintext);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test("byte-identical ciphertext with pinned IV", () => {
|
|
79
|
+
const iv = Buffer.alloc(GCM_IV_LENGTH, 0xab);
|
|
80
|
+
const plaintextBytes = Buffer.from(plaintext, "utf8");
|
|
81
|
+
|
|
82
|
+
// Our side: raw Node.js createCipheriv
|
|
83
|
+
const cipher = createCipheriv(
|
|
84
|
+
`aes-${key.length * 8}-gcm` as "aes-128-gcm" | "aes-192-gcm" | "aes-256-gcm",
|
|
85
|
+
key,
|
|
86
|
+
iv,
|
|
87
|
+
{ authTagLength: GCM_TAG_LENGTH },
|
|
88
|
+
);
|
|
89
|
+
const ourCiphertext = Buffer.concat([
|
|
90
|
+
cipher.update(plaintextBytes),
|
|
91
|
+
cipher.final(),
|
|
92
|
+
]);
|
|
93
|
+
const ourTag = cipher.getAuthTag();
|
|
94
|
+
|
|
95
|
+
// Noble side
|
|
96
|
+
const nobleResult = gcm(toUint8Array(key), toUint8Array(iv)).encrypt(
|
|
97
|
+
toUint8Array(plaintextBytes),
|
|
98
|
+
);
|
|
99
|
+
const nobleCiphertext = nobleResult.slice(0, -GCM_TAG_LENGTH);
|
|
100
|
+
const nobleTag = nobleResult.slice(-GCM_TAG_LENGTH);
|
|
101
|
+
|
|
102
|
+
expect(ourCiphertext.equals(toBuffer(nobleCiphertext))).toBe(true);
|
|
103
|
+
expect(ourTag.equals(toBuffer(nobleTag))).toBe(true);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// ---------------------------------------------------------------------------
|
|
109
|
+
// 3.2 AES-KW (RFC 3394) Cross-Reference
|
|
110
|
+
// ---------------------------------------------------------------------------
|
|
111
|
+
|
|
112
|
+
describe("AES-KW cross-reference with @noble/ciphers", () => {
|
|
113
|
+
describe.each([
|
|
114
|
+
{ name: "A128KW", kek: KEK_128, algorithm: "A128KW" as const },
|
|
115
|
+
{ name: "A192KW", kek: KEK_192, algorithm: "A192KW" as const },
|
|
116
|
+
{ name: "A256KW", kek: KEK_256, algorithm: "A256KW" as const },
|
|
117
|
+
])("$name", ({ kek, algorithm }) => {
|
|
118
|
+
// CEK to wrap: 32 bytes (suitable for A256GCM)
|
|
119
|
+
const cek = randomBytes(32);
|
|
120
|
+
|
|
121
|
+
test("our ecbKeyWrap -> noble aeskw decrypt", () => {
|
|
122
|
+
const kryptos = createOctKryptos(kek, algorithm);
|
|
123
|
+
|
|
124
|
+
const { publicEncryptionKey } = ecbKeyWrap({
|
|
125
|
+
contentEncryptionKey: cek,
|
|
126
|
+
keyEncryptionKey: kek,
|
|
127
|
+
kryptos,
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const unwrapped = aeskw(toUint8Array(kek)).decrypt(
|
|
131
|
+
toUint8Array(publicEncryptionKey),
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
expect(cek.equals(toBuffer(unwrapped))).toBe(true);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
test("noble aeskw encrypt -> our ecbKeyUnwrap", () => {
|
|
138
|
+
const kryptos = createOctKryptos(kek, algorithm);
|
|
139
|
+
|
|
140
|
+
const wrapped = aeskw(toUint8Array(kek)).encrypt(toUint8Array(cek));
|
|
141
|
+
|
|
142
|
+
const { contentEncryptionKey } = ecbKeyUnwrap({
|
|
143
|
+
keyEncryptionKey: kek,
|
|
144
|
+
kryptos,
|
|
145
|
+
publicEncryptionKey: toBuffer(wrapped),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
expect(cek.equals(contentEncryptionKey)).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
test("byte-identical wrapped key (deterministic)", () => {
|
|
152
|
+
const kryptos = createOctKryptos(kek, algorithm);
|
|
153
|
+
|
|
154
|
+
const { publicEncryptionKey } = ecbKeyWrap({
|
|
155
|
+
contentEncryptionKey: cek,
|
|
156
|
+
keyEncryptionKey: kek,
|
|
157
|
+
kryptos,
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
const nobleWrapped = aeskw(toUint8Array(kek)).encrypt(toUint8Array(cek));
|
|
161
|
+
|
|
162
|
+
expect(publicEncryptionKey.equals(toBuffer(nobleWrapped))).toBe(true);
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
// ---------------------------------------------------------------------------
|
|
168
|
+
// 3.3 AES-CBC Raw Cross-Reference
|
|
169
|
+
// ---------------------------------------------------------------------------
|
|
170
|
+
|
|
171
|
+
describe("AES-CBC raw cross-reference with @noble/ciphers", () => {
|
|
172
|
+
describe.each([
|
|
173
|
+
{
|
|
174
|
+
name: "A128CBC",
|
|
175
|
+
// A128CBC-HS256: 32B CEK -> first 16B = HMAC key, last 16B = AES-128-CBC key
|
|
176
|
+
encKey: RAW_KEY_256_CBC.subarray(16),
|
|
177
|
+
nodeCipher: "aes-128-cbc" as const,
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
name: "A192CBC",
|
|
181
|
+
// A192CBC-HS384: 48B CEK -> first 24B = HMAC key, last 24B = AES-192-CBC key
|
|
182
|
+
encKey: RAW_KEY_384_CBC.subarray(24),
|
|
183
|
+
nodeCipher: "aes-192-cbc" as const,
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: "A256CBC",
|
|
187
|
+
// A256CBC-HS512: 64B CEK -> first 32B = HMAC key, last 32B = AES-256-CBC key
|
|
188
|
+
encKey: RAW_KEY_512_CBC.subarray(32),
|
|
189
|
+
nodeCipher: "aes-256-cbc" as const,
|
|
190
|
+
},
|
|
191
|
+
])("$name", ({ encKey, nodeCipher }) => {
|
|
192
|
+
test("byte-identical ciphertext (PKCS7 padding)", () => {
|
|
193
|
+
const iv = Buffer.alloc(AES_BLOCK_SIZE, 0xcd);
|
|
194
|
+
const plaintext = Buffer.from("cross-reference CBC test", "utf8");
|
|
195
|
+
|
|
196
|
+
// Our side: raw Node.js createCipheriv
|
|
197
|
+
const cipher = createCipheriv(nodeCipher, encKey, iv);
|
|
198
|
+
const ourCiphertext = Buffer.concat([cipher.update(plaintext), cipher.final()]);
|
|
199
|
+
|
|
200
|
+
// Noble side
|
|
201
|
+
const nobleCiphertext = cbc(toUint8Array(encKey), toUint8Array(iv)).encrypt(
|
|
202
|
+
toUint8Array(plaintext),
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
expect(ourCiphertext.equals(toBuffer(nobleCiphertext))).toBe(true);
|
|
206
|
+
});
|
|
207
|
+
});
|
|
208
|
+
});
|
package/dist/classes/AesKit.d.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { IKryptos } from "@lindorm/kryptos";
|
|
2
|
-
import { IAesKit } from "../interfaces";
|
|
2
|
+
import { AesOperationOptions, IAesKit } from "../interfaces";
|
|
3
3
|
import { AesContent, AesContentType, AesDecryptionRecord, AesEncryptionRecord, AesKitOptions, SerialisedAesDecryption, SerialisedAesEncryption } from "../types";
|
|
4
|
+
import { PreparedEncryption } from "../types/private";
|
|
4
5
|
export declare class AesKit implements IAesKit {
|
|
5
6
|
private readonly encryption;
|
|
6
7
|
readonly kryptos: IKryptos;
|
|
7
8
|
constructor(options: AesKitOptions);
|
|
8
|
-
encrypt(data: AesContent, mode?: "encoded"): string;
|
|
9
|
-
encrypt(data: AesContent, mode: "record"): AesEncryptionRecord;
|
|
10
|
-
encrypt(data: AesContent, mode: "serialised"): SerialisedAesEncryption;
|
|
11
|
-
encrypt(data: AesContent, mode: "tokenised"): string;
|
|
12
|
-
decrypt<T extends AesContent = string>(data: AesDecryptionRecord | SerialisedAesDecryption | string): T;
|
|
13
|
-
verify(input:
|
|
14
|
-
assert(input:
|
|
9
|
+
encrypt(data: AesContent, mode?: "encoded", options?: AesOperationOptions): string;
|
|
10
|
+
encrypt(data: AesContent, mode: "record", options?: AesOperationOptions): AesEncryptionRecord;
|
|
11
|
+
encrypt(data: AesContent, mode: "serialised", options?: AesOperationOptions): SerialisedAesEncryption;
|
|
12
|
+
encrypt(data: AesContent, mode: "tokenised", options?: AesOperationOptions): string;
|
|
13
|
+
decrypt<T extends AesContent = string>(data: AesDecryptionRecord | SerialisedAesDecryption | string, options?: AesOperationOptions): T;
|
|
14
|
+
verify(input: AesContent, data: AesDecryptionRecord | SerialisedAesDecryption | string, options?: AesOperationOptions): boolean;
|
|
15
|
+
assert(input: AesContent, data: AesDecryptionRecord | SerialisedAesDecryption | string, options?: AesOperationOptions): void;
|
|
16
|
+
prepareEncryption(): PreparedEncryption;
|
|
15
17
|
static contentType(input: any): AesContentType;
|
|
16
18
|
static isAesTokenised(input: any): input is string;
|
|
17
19
|
static parse(data: AesDecryptionRecord | SerialisedAesDecryption | string): AesDecryptionRecord;
|