raz-encrypt 0.2.0 → 0.3.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.
|
@@ -13,11 +13,20 @@ export interface EncryptedFrame {
|
|
|
13
13
|
* `counter`. The caller owns counter bookkeeping: increment it once per
|
|
14
14
|
* frame sent under this key, one counter sequence per direction, and never
|
|
15
15
|
* reuse a value.
|
|
16
|
+
*
|
|
17
|
+
* `aad` is optional Associated Authenticated Data: extra context (e.g.
|
|
18
|
+
* direction, frame/message type) bound into the AEAD tag but not encrypted.
|
|
19
|
+
* It is never transmitted as part of EncryptedFrame — the receiver must
|
|
20
|
+
* reconstruct the identical bytes out of band (from the frame's own
|
|
21
|
+
* metadata, e.g. its direction and type) and pass them to decryptFrame.
|
|
22
|
+
* Omitting it behaves exactly as before this parameter existed.
|
|
16
23
|
*/
|
|
17
|
-
export declare function encryptFrame(key: Uint8Array, counter: number, plaintext: Uint8Array): EncryptedFrame;
|
|
24
|
+
export declare function encryptFrame(key: Uint8Array, counter: number, plaintext: Uint8Array, aad?: Uint8Array): EncryptedFrame;
|
|
18
25
|
/**
|
|
19
26
|
* Decrypts a frame produced by encryptFrame. Fails closed — throws
|
|
20
|
-
* FrameDecryptionError — on a tampered ciphertext, a mismatched counter,
|
|
21
|
-
*
|
|
27
|
+
* FrameDecryptionError — on a tampered ciphertext, a mismatched counter, the
|
|
28
|
+
* wrong directional key, or an `aad` that doesn't match what the sender used
|
|
29
|
+
* (including one side supplying `aad` and the other omitting it), rather
|
|
30
|
+
* than ever returning corrupted plaintext.
|
|
22
31
|
*/
|
|
23
|
-
export declare function decryptFrame(key: Uint8Array, frame: EncryptedFrame): Uint8Array;
|
|
32
|
+
export declare function decryptFrame(key: Uint8Array, frame: EncryptedFrame, aad?: Uint8Array): Uint8Array;
|
|
@@ -37,19 +37,28 @@ function counterToNonce(counter) {
|
|
|
37
37
|
* `counter`. The caller owns counter bookkeeping: increment it once per
|
|
38
38
|
* frame sent under this key, one counter sequence per direction, and never
|
|
39
39
|
* reuse a value.
|
|
40
|
+
*
|
|
41
|
+
* `aad` is optional Associated Authenticated Data: extra context (e.g.
|
|
42
|
+
* direction, frame/message type) bound into the AEAD tag but not encrypted.
|
|
43
|
+
* It is never transmitted as part of EncryptedFrame — the receiver must
|
|
44
|
+
* reconstruct the identical bytes out of band (from the frame's own
|
|
45
|
+
* metadata, e.g. its direction and type) and pass them to decryptFrame.
|
|
46
|
+
* Omitting it behaves exactly as before this parameter existed.
|
|
40
47
|
*/
|
|
41
|
-
export function encryptFrame(key, counter, plaintext) {
|
|
42
|
-
const ciphertext = gcm(key, counterToNonce(counter)).encrypt(plaintext);
|
|
48
|
+
export function encryptFrame(key, counter, plaintext, aad) {
|
|
49
|
+
const ciphertext = gcm(key, counterToNonce(counter), aad).encrypt(plaintext);
|
|
43
50
|
return { counter, ciphertext: bytesToHex(ciphertext) };
|
|
44
51
|
}
|
|
45
52
|
/**
|
|
46
53
|
* Decrypts a frame produced by encryptFrame. Fails closed — throws
|
|
47
|
-
* FrameDecryptionError — on a tampered ciphertext, a mismatched counter,
|
|
48
|
-
*
|
|
54
|
+
* FrameDecryptionError — on a tampered ciphertext, a mismatched counter, the
|
|
55
|
+
* wrong directional key, or an `aad` that doesn't match what the sender used
|
|
56
|
+
* (including one side supplying `aad` and the other omitting it), rather
|
|
57
|
+
* than ever returning corrupted plaintext.
|
|
49
58
|
*/
|
|
50
|
-
export function decryptFrame(key, frame) {
|
|
59
|
+
export function decryptFrame(key, frame, aad) {
|
|
51
60
|
try {
|
|
52
|
-
return gcm(key, counterToNonce(frame.counter)).decrypt(hexToBytes(frame.ciphertext));
|
|
61
|
+
return gcm(key, counterToNonce(frame.counter), aad).decrypt(hexToBytes(frame.ciphertext));
|
|
53
62
|
}
|
|
54
63
|
catch {
|
|
55
64
|
throw new FrameDecryptionError();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"frameCipher.js","sourceRoot":"","sources":["../../src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