@decryption/ciphers 0.2.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/LICENSE +22 -0
- package/README.md +75 -0
- package/_arx.d.ts +144 -0
- package/_arx.js +375 -0
- package/_poly1305.d.ts +72 -0
- package/_poly1305.js +367 -0
- package/_polyval.d.ts +120 -0
- package/_polyval.js +335 -0
- package/aes.d.ts +461 -0
- package/aes.js +1768 -0
- package/chacha.d.ts +248 -0
- package/chacha.js +579 -0
- package/esm/_arx.js +368 -0
- package/esm/_poly1305.js +363 -0
- package/esm/_polyval.js +329 -0
- package/esm/aes.js +1764 -0
- package/esm/chacha.js +574 -0
- package/esm/ff1.js +203 -0
- package/esm/index.js +26 -0
- package/esm/salsa.js +370 -0
- package/esm/utils.js +806 -0
- package/esm/webcrypto.js +166 -0
- package/ff1.d.ts +39 -0
- package/ff1.js +207 -0
- package/index.d.ts +0 -0
- package/index.js +26 -0
- package/package.json +45 -0
- package/salsa.d.ts +114 -0
- package/salsa.js +375 -0
- package/utils.d.ts +716 -0
- package/utils.js +841 -0
- package/webcrypto.d.ts +88 -0
- package/webcrypto.js +169 -0
package/_poly1305.d.ts
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Poly1305 ({@link https://cr.yp.to/mac/poly1305-20050329.pdf | PDF},
|
|
3
|
+
* {@link https://en.wikipedia.org/wiki/Poly1305 | wiki})
|
|
4
|
+
* is a fast and parallel secret-key message-authentication code suitable for
|
|
5
|
+
* a wide variety of applications. It was standardized in
|
|
6
|
+
* {@link https://www.rfc-editor.org/rfc/rfc8439 | RFC 8439} and is now used in TLS 1.3.
|
|
7
|
+
*
|
|
8
|
+
* Polynomial MACs are not perfect for every situation:
|
|
9
|
+
* they lack Random Key Robustness: the MAC can be forged, and can't be used in PAKE schemes.
|
|
10
|
+
* See {@link https://keymaterial.net/2020/09/07/invisible-salamanders-in-aes-gcm-siv/ | the invisible salamanders attack writeup}.
|
|
11
|
+
* To combat invisible salamanders, `hash(key)` can be included in ciphertext,
|
|
12
|
+
* however, this would violate ciphertext indistinguishability:
|
|
13
|
+
* an attacker would know which key was used - so `HKDF(key, i)`
|
|
14
|
+
* could be used instead.
|
|
15
|
+
*
|
|
16
|
+
* Check out the {@link https://cr.yp.to/mac.html | original website}.
|
|
17
|
+
* Based on public-domain {@link https://github.com/floodyberry/poly1305-donna | poly1305-donna}.
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
import { type CMac, type IHash2, type TArg, type TRet } from './utils';
|
|
21
|
+
/**
|
|
22
|
+
* Incremental Poly1305 MAC state.
|
|
23
|
+
* Prefer `poly1305()` for one-shot use.
|
|
24
|
+
* @param key - 32-byte Poly1305 one-time key.
|
|
25
|
+
* @example
|
|
26
|
+
* Feeds one chunk into an incremental Poly1305 state with a fresh one-time key.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { Poly1305 } from './_poly1305';
|
|
30
|
+
* import { randomBytes } from './utils';
|
|
31
|
+
* const key = randomBytes(32);
|
|
32
|
+
* const mac = new Poly1305(key);
|
|
33
|
+
* mac.update(new Uint8Array([1, 2, 3]));
|
|
34
|
+
* mac.digest();
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare class Poly1305 implements IHash2 {
|
|
38
|
+
readonly blockLen = 16;
|
|
39
|
+
readonly outputLen = 16;
|
|
40
|
+
private buffer;
|
|
41
|
+
private r;
|
|
42
|
+
private h;
|
|
43
|
+
private pad;
|
|
44
|
+
private pos;
|
|
45
|
+
protected finished: boolean;
|
|
46
|
+
protected destroyed: boolean;
|
|
47
|
+
constructor(key: TArg<Uint8Array>);
|
|
48
|
+
private process;
|
|
49
|
+
private finalize;
|
|
50
|
+
update(data: TArg<Uint8Array>): this;
|
|
51
|
+
destroy(): void;
|
|
52
|
+
digestInto(out: TArg<Uint8Array>): void;
|
|
53
|
+
digest(): TRet<Uint8Array>;
|
|
54
|
+
}
|
|
55
|
+
/** One-shot keyed hash helper with `.create()`. */
|
|
56
|
+
export type CHash = CMac<Poly1305>;
|
|
57
|
+
/**
|
|
58
|
+
* Poly1305 MAC from RFC 8439.
|
|
59
|
+
* @param msg - Message bytes to authenticate.
|
|
60
|
+
* @param key - 32-byte Poly1305 one-time key.
|
|
61
|
+
* @returns 16-byte authentication tag.
|
|
62
|
+
* @example
|
|
63
|
+
* Authenticates one message with a one-shot Poly1305 call and a fresh key.
|
|
64
|
+
*
|
|
65
|
+
* ```ts
|
|
66
|
+
* import { poly1305 } from './_poly1305';
|
|
67
|
+
* import { randomBytes } from './utils';
|
|
68
|
+
* const key = randomBytes(32);
|
|
69
|
+
* poly1305(new Uint8Array(), key);
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
export declare const poly1305: TRet<CHash>;
|
package/_poly1305.js
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.poly1305 = exports.Poly1305 = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Poly1305 ({@link https://cr.yp.to/mac/poly1305-20050329.pdf | PDF},
|
|
6
|
+
* {@link https://en.wikipedia.org/wiki/Poly1305 | wiki})
|
|
7
|
+
* is a fast and parallel secret-key message-authentication code suitable for
|
|
8
|
+
* a wide variety of applications. It was standardized in
|
|
9
|
+
* {@link https://www.rfc-editor.org/rfc/rfc8439 | RFC 8439} and is now used in TLS 1.3.
|
|
10
|
+
*
|
|
11
|
+
* Polynomial MACs are not perfect for every situation:
|
|
12
|
+
* they lack Random Key Robustness: the MAC can be forged, and can't be used in PAKE schemes.
|
|
13
|
+
* See {@link https://keymaterial.net/2020/09/07/invisible-salamanders-in-aes-gcm-siv/ | the invisible salamanders attack writeup}.
|
|
14
|
+
* To combat invisible salamanders, `hash(key)` can be included in ciphertext,
|
|
15
|
+
* however, this would violate ciphertext indistinguishability:
|
|
16
|
+
* an attacker would know which key was used - so `HKDF(key, i)`
|
|
17
|
+
* could be used instead.
|
|
18
|
+
*
|
|
19
|
+
* Check out the {@link https://cr.yp.to/mac.html | original website}.
|
|
20
|
+
* Based on public-domain {@link https://github.com/floodyberry/poly1305-donna | poly1305-donna}.
|
|
21
|
+
* @module
|
|
22
|
+
*/
|
|
23
|
+
// prettier-ignore
|
|
24
|
+
const utils_1 = require("./utils");
|
|
25
|
+
// Little-endian 2-byte load used by the Poly1305 limb decomposition.
|
|
26
|
+
function u8to16(a, i) {
|
|
27
|
+
return (a[i++] & 0xff) | ((a[i++] & 0xff) << 8);
|
|
28
|
+
}
|
|
29
|
+
function bytesToNumberLE(bytes) {
|
|
30
|
+
return (0, utils_1.hexToNumber)((0, utils_1.bytesToHex)(Uint8Array.from(bytes).reverse()));
|
|
31
|
+
}
|
|
32
|
+
/** Small version of `poly1305` without loop unrolling. Unused, provided for auditability. */
|
|
33
|
+
function poly1305_small(msg, key) {
|
|
34
|
+
(0, utils_1.abytes)(msg);
|
|
35
|
+
(0, utils_1.abytes)(key, 32, 'key');
|
|
36
|
+
const POW_2_130_5 = BigInt(2) ** BigInt(130) - BigInt(5); // 2^130-5
|
|
37
|
+
const POW_2_128_1 = BigInt(2) ** BigInt(128) - BigInt(1); // 2^128-1
|
|
38
|
+
const CLAMP_R = BigInt('0x0ffffffc0ffffffc0ffffffc0fffffff');
|
|
39
|
+
const r = bytesToNumberLE(key.subarray(0, 16)) & CLAMP_R;
|
|
40
|
+
const s = bytesToNumberLE(key.subarray(16));
|
|
41
|
+
// Process by 16 byte chunks
|
|
42
|
+
let acc = BigInt(0);
|
|
43
|
+
for (let i = 0; i < msg.length; i += 16) {
|
|
44
|
+
const m = msg.subarray(i, i + 16);
|
|
45
|
+
// RFC 8439 §2.5.1 / RFC 7539 §2.5.1 append [0x01] to each chunk before multiplying by r.
|
|
46
|
+
const n = bytesToNumberLE(m) | (BigInt(1) << BigInt(8 * m.length));
|
|
47
|
+
acc = ((acc + n) * r) % POW_2_130_5;
|
|
48
|
+
}
|
|
49
|
+
const res = (acc + s) & POW_2_128_1;
|
|
50
|
+
// RFC 8439 §2.5 / RFC 7539 §2.5 serialize the low 128 bits in little-endian order.
|
|
51
|
+
return (0, utils_1.numberToBytesBE)(res, 16).reverse(); // LE
|
|
52
|
+
}
|
|
53
|
+
// Can be used to replace `computeTag` in chacha.ts. Unused, provided for auditability.
|
|
54
|
+
// @ts-expect-error
|
|
55
|
+
function poly1305_computeTag_small(authKey,
|
|
56
|
+
// AEAD trailer must already be the 16-byte length block:
|
|
57
|
+
// 8-byte little-endian AAD length || 8-byte little-endian ciphertext length.
|
|
58
|
+
lengths, ciphertext, AAD) {
|
|
59
|
+
// RFC 8439 §2.8.1 / RFC 7539 §2.8.1 MAC input is
|
|
60
|
+
// AAD || pad16(AAD) || ciphertext || pad16(ciphertext) || lengths.
|
|
61
|
+
const res = [];
|
|
62
|
+
const updatePadded2 = (msg) => {
|
|
63
|
+
res.push(msg);
|
|
64
|
+
const leftover = msg.length % 16;
|
|
65
|
+
// RFC 8439 §2.8.1 / RFC 7539 §2.8.1: pad16(x) is empty for aligned
|
|
66
|
+
// inputs, else 16-(len%16) zero bytes.
|
|
67
|
+
if (leftover)
|
|
68
|
+
res.push(new Uint8Array(16).slice(leftover));
|
|
69
|
+
};
|
|
70
|
+
if (AAD)
|
|
71
|
+
updatePadded2(AAD);
|
|
72
|
+
updatePadded2(ciphertext);
|
|
73
|
+
res.push(lengths);
|
|
74
|
+
return poly1305_small((0, utils_1.concatBytes)(...res), authKey);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Incremental Poly1305 MAC state.
|
|
78
|
+
* Prefer `poly1305()` for one-shot use.
|
|
79
|
+
* @param key - 32-byte Poly1305 one-time key.
|
|
80
|
+
* @example
|
|
81
|
+
* Feeds one chunk into an incremental Poly1305 state with a fresh one-time key.
|
|
82
|
+
*
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { Poly1305 } from './_poly1305';
|
|
85
|
+
* import { randomBytes } from './utils';
|
|
86
|
+
* const key = randomBytes(32);
|
|
87
|
+
* const mac = new Poly1305(key);
|
|
88
|
+
* mac.update(new Uint8Array([1, 2, 3]));
|
|
89
|
+
* mac.digest();
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
class Poly1305 {
|
|
93
|
+
blockLen = 16;
|
|
94
|
+
outputLen = 16;
|
|
95
|
+
buffer = new Uint8Array(16);
|
|
96
|
+
r = new Uint16Array(10); // Allocating 1 array with .subarray() here is slower than 3
|
|
97
|
+
h = new Uint16Array(10);
|
|
98
|
+
pad = new Uint16Array(8);
|
|
99
|
+
pos = 0;
|
|
100
|
+
finished = false;
|
|
101
|
+
destroyed = false;
|
|
102
|
+
// Can be speed-up using BigUint64Array, at the cost of complexity
|
|
103
|
+
constructor(key) {
|
|
104
|
+
key = (0, utils_1.copyBytes)((0, utils_1.abytes)(key, 32, 'key'));
|
|
105
|
+
const t0 = u8to16(key, 0);
|
|
106
|
+
const t1 = u8to16(key, 2);
|
|
107
|
+
const t2 = u8to16(key, 4);
|
|
108
|
+
const t3 = u8to16(key, 6);
|
|
109
|
+
const t4 = u8to16(key, 8);
|
|
110
|
+
const t5 = u8to16(key, 10);
|
|
111
|
+
const t6 = u8to16(key, 12);
|
|
112
|
+
const t7 = u8to16(key, 14);
|
|
113
|
+
// RFC 8439 §2.5.1 / RFC 7539 §2.5.1 clamp r before multiplication.
|
|
114
|
+
// These masks unpack that clamped value into 13-bit limbs, while pad
|
|
115
|
+
// keeps the raw s half for finalize().
|
|
116
|
+
// {@link https://github.com/floodyberry/poly1305-donna/blob/e6ad6e091d30d7f4ec2d4f978be1fcfcbce72781/poly1305-donna-16.h#L47 | poly1305-donna reference}
|
|
117
|
+
this.r[0] = t0 & 0x1fff;
|
|
118
|
+
this.r[1] = ((t0 >>> 13) | (t1 << 3)) & 0x1fff;
|
|
119
|
+
this.r[2] = ((t1 >>> 10) | (t2 << 6)) & 0x1f03;
|
|
120
|
+
this.r[3] = ((t2 >>> 7) | (t3 << 9)) & 0x1fff;
|
|
121
|
+
this.r[4] = ((t3 >>> 4) | (t4 << 12)) & 0x00ff;
|
|
122
|
+
this.r[5] = (t4 >>> 1) & 0x1ffe;
|
|
123
|
+
this.r[6] = ((t4 >>> 14) | (t5 << 2)) & 0x1fff;
|
|
124
|
+
this.r[7] = ((t5 >>> 11) | (t6 << 5)) & 0x1f81;
|
|
125
|
+
this.r[8] = ((t6 >>> 8) | (t7 << 8)) & 0x1fff;
|
|
126
|
+
this.r[9] = (t7 >>> 5) & 0x007f;
|
|
127
|
+
for (let i = 0; i < 8; i++)
|
|
128
|
+
this.pad[i] = u8to16(key, 16 + 2 * i);
|
|
129
|
+
}
|
|
130
|
+
process(data, offset, isLast = false) {
|
|
131
|
+
// RFC 8439 §2.5 / §2.5.1 and RFC 7539 §2.5 / §2.5.1 add an extra high
|
|
132
|
+
// bit to every full 16-byte block. The final partial block gets its
|
|
133
|
+
// explicit `1` byte during digestInto(), so `hibit` stays zero there.
|
|
134
|
+
const hibit = isLast ? 0 : 1 << 11;
|
|
135
|
+
const { h, r } = this;
|
|
136
|
+
const r0 = r[0];
|
|
137
|
+
const r1 = r[1];
|
|
138
|
+
const r2 = r[2];
|
|
139
|
+
const r3 = r[3];
|
|
140
|
+
const r4 = r[4];
|
|
141
|
+
const r5 = r[5];
|
|
142
|
+
const r6 = r[6];
|
|
143
|
+
const r7 = r[7];
|
|
144
|
+
const r8 = r[8];
|
|
145
|
+
const r9 = r[9];
|
|
146
|
+
const t0 = u8to16(data, offset + 0);
|
|
147
|
+
const t1 = u8to16(data, offset + 2);
|
|
148
|
+
const t2 = u8to16(data, offset + 4);
|
|
149
|
+
const t3 = u8to16(data, offset + 6);
|
|
150
|
+
const t4 = u8to16(data, offset + 8);
|
|
151
|
+
const t5 = u8to16(data, offset + 10);
|
|
152
|
+
const t6 = u8to16(data, offset + 12);
|
|
153
|
+
const t7 = u8to16(data, offset + 14);
|
|
154
|
+
let h0 = h[0] + (t0 & 0x1fff);
|
|
155
|
+
let h1 = h[1] + (((t0 >>> 13) | (t1 << 3)) & 0x1fff);
|
|
156
|
+
let h2 = h[2] + (((t1 >>> 10) | (t2 << 6)) & 0x1fff);
|
|
157
|
+
let h3 = h[3] + (((t2 >>> 7) | (t3 << 9)) & 0x1fff);
|
|
158
|
+
let h4 = h[4] + (((t3 >>> 4) | (t4 << 12)) & 0x1fff);
|
|
159
|
+
let h5 = h[5] + ((t4 >>> 1) & 0x1fff);
|
|
160
|
+
let h6 = h[6] + (((t4 >>> 14) | (t5 << 2)) & 0x1fff);
|
|
161
|
+
let h7 = h[7] + (((t5 >>> 11) | (t6 << 5)) & 0x1fff);
|
|
162
|
+
let h8 = h[8] + (((t6 >>> 8) | (t7 << 8)) & 0x1fff);
|
|
163
|
+
let h9 = h[9] + ((t7 >>> 5) | hibit);
|
|
164
|
+
let c = 0;
|
|
165
|
+
let d0 = c + h0 * r0 + h1 * (5 * r9) + h2 * (5 * r8) + h3 * (5 * r7) + h4 * (5 * r6);
|
|
166
|
+
c = d0 >>> 13;
|
|
167
|
+
d0 &= 0x1fff;
|
|
168
|
+
d0 += h5 * (5 * r5) + h6 * (5 * r4) + h7 * (5 * r3) + h8 * (5 * r2) + h9 * (5 * r1);
|
|
169
|
+
c += d0 >>> 13;
|
|
170
|
+
d0 &= 0x1fff;
|
|
171
|
+
let d1 = c + h0 * r1 + h1 * r0 + h2 * (5 * r9) + h3 * (5 * r8) + h4 * (5 * r7);
|
|
172
|
+
c = d1 >>> 13;
|
|
173
|
+
d1 &= 0x1fff;
|
|
174
|
+
d1 += h5 * (5 * r6) + h6 * (5 * r5) + h7 * (5 * r4) + h8 * (5 * r3) + h9 * (5 * r2);
|
|
175
|
+
c += d1 >>> 13;
|
|
176
|
+
d1 &= 0x1fff;
|
|
177
|
+
let d2 = c + h0 * r2 + h1 * r1 + h2 * r0 + h3 * (5 * r9) + h4 * (5 * r8);
|
|
178
|
+
c = d2 >>> 13;
|
|
179
|
+
d2 &= 0x1fff;
|
|
180
|
+
d2 += h5 * (5 * r7) + h6 * (5 * r6) + h7 * (5 * r5) + h8 * (5 * r4) + h9 * (5 * r3);
|
|
181
|
+
c += d2 >>> 13;
|
|
182
|
+
d2 &= 0x1fff;
|
|
183
|
+
let d3 = c + h0 * r3 + h1 * r2 + h2 * r1 + h3 * r0 + h4 * (5 * r9);
|
|
184
|
+
c = d3 >>> 13;
|
|
185
|
+
d3 &= 0x1fff;
|
|
186
|
+
d3 += h5 * (5 * r8) + h6 * (5 * r7) + h7 * (5 * r6) + h8 * (5 * r5) + h9 * (5 * r4);
|
|
187
|
+
c += d3 >>> 13;
|
|
188
|
+
d3 &= 0x1fff;
|
|
189
|
+
let d4 = c + h0 * r4 + h1 * r3 + h2 * r2 + h3 * r1 + h4 * r0;
|
|
190
|
+
c = d4 >>> 13;
|
|
191
|
+
d4 &= 0x1fff;
|
|
192
|
+
d4 += h5 * (5 * r9) + h6 * (5 * r8) + h7 * (5 * r7) + h8 * (5 * r6) + h9 * (5 * r5);
|
|
193
|
+
c += d4 >>> 13;
|
|
194
|
+
d4 &= 0x1fff;
|
|
195
|
+
let d5 = c + h0 * r5 + h1 * r4 + h2 * r3 + h3 * r2 + h4 * r1;
|
|
196
|
+
c = d5 >>> 13;
|
|
197
|
+
d5 &= 0x1fff;
|
|
198
|
+
d5 += h5 * r0 + h6 * (5 * r9) + h7 * (5 * r8) + h8 * (5 * r7) + h9 * (5 * r6);
|
|
199
|
+
c += d5 >>> 13;
|
|
200
|
+
d5 &= 0x1fff;
|
|
201
|
+
let d6 = c + h0 * r6 + h1 * r5 + h2 * r4 + h3 * r3 + h4 * r2;
|
|
202
|
+
c = d6 >>> 13;
|
|
203
|
+
d6 &= 0x1fff;
|
|
204
|
+
d6 += h5 * r1 + h6 * r0 + h7 * (5 * r9) + h8 * (5 * r8) + h9 * (5 * r7);
|
|
205
|
+
c += d6 >>> 13;
|
|
206
|
+
d6 &= 0x1fff;
|
|
207
|
+
let d7 = c + h0 * r7 + h1 * r6 + h2 * r5 + h3 * r4 + h4 * r3;
|
|
208
|
+
c = d7 >>> 13;
|
|
209
|
+
d7 &= 0x1fff;
|
|
210
|
+
d7 += h5 * r2 + h6 * r1 + h7 * r0 + h8 * (5 * r9) + h9 * (5 * r8);
|
|
211
|
+
c += d7 >>> 13;
|
|
212
|
+
d7 &= 0x1fff;
|
|
213
|
+
let d8 = c + h0 * r8 + h1 * r7 + h2 * r6 + h3 * r5 + h4 * r4;
|
|
214
|
+
c = d8 >>> 13;
|
|
215
|
+
d8 &= 0x1fff;
|
|
216
|
+
d8 += h5 * r3 + h6 * r2 + h7 * r1 + h8 * r0 + h9 * (5 * r9);
|
|
217
|
+
c += d8 >>> 13;
|
|
218
|
+
d8 &= 0x1fff;
|
|
219
|
+
let d9 = c + h0 * r9 + h1 * r8 + h2 * r7 + h3 * r6 + h4 * r5;
|
|
220
|
+
c = d9 >>> 13;
|
|
221
|
+
d9 &= 0x1fff;
|
|
222
|
+
d9 += h5 * r4 + h6 * r3 + h7 * r2 + h8 * r1 + h9 * r0;
|
|
223
|
+
c += d9 >>> 13;
|
|
224
|
+
d9 &= 0x1fff;
|
|
225
|
+
c = ((c << 2) + c) | 0;
|
|
226
|
+
c = (c + d0) | 0;
|
|
227
|
+
d0 = c & 0x1fff;
|
|
228
|
+
c = c >>> 13;
|
|
229
|
+
d1 += c;
|
|
230
|
+
h[0] = d0;
|
|
231
|
+
h[1] = d1;
|
|
232
|
+
h[2] = d2;
|
|
233
|
+
h[3] = d3;
|
|
234
|
+
h[4] = d4;
|
|
235
|
+
h[5] = d5;
|
|
236
|
+
h[6] = d6;
|
|
237
|
+
h[7] = d7;
|
|
238
|
+
h[8] = d8;
|
|
239
|
+
h[9] = d9;
|
|
240
|
+
}
|
|
241
|
+
finalize() {
|
|
242
|
+
const { h, pad } = this;
|
|
243
|
+
const g = new Uint16Array(10);
|
|
244
|
+
let c = h[1] >>> 13;
|
|
245
|
+
h[1] &= 0x1fff;
|
|
246
|
+
for (let i = 2; i < 10; i++) {
|
|
247
|
+
h[i] += c;
|
|
248
|
+
c = h[i] >>> 13;
|
|
249
|
+
h[i] &= 0x1fff;
|
|
250
|
+
}
|
|
251
|
+
h[0] += c * 5;
|
|
252
|
+
c = h[0] >>> 13;
|
|
253
|
+
h[0] &= 0x1fff;
|
|
254
|
+
h[1] += c;
|
|
255
|
+
c = h[1] >>> 13;
|
|
256
|
+
h[1] &= 0x1fff;
|
|
257
|
+
h[2] += c;
|
|
258
|
+
// RFC 8439 §2.5 / RFC 7539 §2.5 reduce modulo 2^130-5 before repacking
|
|
259
|
+
// to 16-bit words and adding the raw s half.
|
|
260
|
+
g[0] = h[0] + 5;
|
|
261
|
+
c = g[0] >>> 13;
|
|
262
|
+
g[0] &= 0x1fff;
|
|
263
|
+
for (let i = 1; i < 10; i++) {
|
|
264
|
+
g[i] = h[i] + c;
|
|
265
|
+
c = g[i] >>> 13;
|
|
266
|
+
g[i] &= 0x1fff;
|
|
267
|
+
}
|
|
268
|
+
g[9] -= 1 << 13;
|
|
269
|
+
let mask = (c ^ 1) - 1;
|
|
270
|
+
for (let i = 0; i < 10; i++)
|
|
271
|
+
g[i] &= mask;
|
|
272
|
+
mask = ~mask;
|
|
273
|
+
for (let i = 0; i < 10; i++)
|
|
274
|
+
h[i] = (h[i] & mask) | g[i];
|
|
275
|
+
h[0] = (h[0] | (h[1] << 13)) & 0xffff;
|
|
276
|
+
h[1] = ((h[1] >>> 3) | (h[2] << 10)) & 0xffff;
|
|
277
|
+
h[2] = ((h[2] >>> 6) | (h[3] << 7)) & 0xffff;
|
|
278
|
+
h[3] = ((h[3] >>> 9) | (h[4] << 4)) & 0xffff;
|
|
279
|
+
h[4] = ((h[4] >>> 12) | (h[5] << 1) | (h[6] << 14)) & 0xffff;
|
|
280
|
+
h[5] = ((h[6] >>> 2) | (h[7] << 11)) & 0xffff;
|
|
281
|
+
h[6] = ((h[7] >>> 5) | (h[8] << 8)) & 0xffff;
|
|
282
|
+
h[7] = ((h[8] >>> 8) | (h[9] << 5)) & 0xffff;
|
|
283
|
+
let f = h[0] + pad[0];
|
|
284
|
+
h[0] = f & 0xffff;
|
|
285
|
+
for (let i = 1; i < 8; i++) {
|
|
286
|
+
f = (((h[i] + pad[i]) | 0) + (f >>> 16)) | 0;
|
|
287
|
+
h[i] = f & 0xffff;
|
|
288
|
+
}
|
|
289
|
+
(0, utils_1.clean)(g);
|
|
290
|
+
}
|
|
291
|
+
update(data) {
|
|
292
|
+
(0, utils_1.aexists)(this);
|
|
293
|
+
(0, utils_1.abytes)(data);
|
|
294
|
+
data = (0, utils_1.copyBytes)(data);
|
|
295
|
+
const { buffer, blockLen } = this;
|
|
296
|
+
const len = data.length;
|
|
297
|
+
for (let pos = 0; pos < len;) {
|
|
298
|
+
const take = Math.min(blockLen - this.pos, len - pos);
|
|
299
|
+
// Fast path: we have at least one block in input
|
|
300
|
+
if (take === blockLen) {
|
|
301
|
+
for (; blockLen <= len - pos; pos += blockLen)
|
|
302
|
+
this.process(data, pos);
|
|
303
|
+
continue;
|
|
304
|
+
}
|
|
305
|
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
|
306
|
+
this.pos += take;
|
|
307
|
+
pos += take;
|
|
308
|
+
if (this.pos === blockLen) {
|
|
309
|
+
this.process(buffer, 0, false);
|
|
310
|
+
this.pos = 0;
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
return this;
|
|
314
|
+
}
|
|
315
|
+
destroy() {
|
|
316
|
+
// `aexists(this)` guards update/digest paths, so destroy must mark the instance unusable too.
|
|
317
|
+
this.destroyed = true;
|
|
318
|
+
(0, utils_1.clean)(this.h, this.r, this.buffer, this.pad);
|
|
319
|
+
}
|
|
320
|
+
digestInto(out) {
|
|
321
|
+
(0, utils_1.aexists)(this);
|
|
322
|
+
(0, utils_1.aoutput)(out, this);
|
|
323
|
+
this.finished = true;
|
|
324
|
+
const { buffer, h } = this;
|
|
325
|
+
let { pos } = this;
|
|
326
|
+
if (pos) {
|
|
327
|
+
// RFC 8439 §2.5 / RFC 7539 §2.5: the final short block appends a
|
|
328
|
+
// single `0x01` byte and zero-fills the remaining bytes before the
|
|
329
|
+
// last multiplication step.
|
|
330
|
+
buffer[pos++] = 1;
|
|
331
|
+
for (; pos < 16; pos++)
|
|
332
|
+
buffer[pos] = 0;
|
|
333
|
+
this.process(buffer, 0, true);
|
|
334
|
+
}
|
|
335
|
+
this.finalize();
|
|
336
|
+
let opos = 0;
|
|
337
|
+
for (let i = 0; i < 8; i++) {
|
|
338
|
+
out[opos++] = h[i] >>> 0;
|
|
339
|
+
out[opos++] = h[i] >>> 8;
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
digest() {
|
|
343
|
+
const { buffer, outputLen } = this;
|
|
344
|
+
this.digestInto(buffer);
|
|
345
|
+
// Copy out before destroy() zeroes the internal buffer.
|
|
346
|
+
const res = buffer.slice(0, outputLen);
|
|
347
|
+
this.destroy();
|
|
348
|
+
return res;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
exports.Poly1305 = Poly1305;
|
|
352
|
+
/**
|
|
353
|
+
* Poly1305 MAC from RFC 8439.
|
|
354
|
+
* @param msg - Message bytes to authenticate.
|
|
355
|
+
* @param key - 32-byte Poly1305 one-time key.
|
|
356
|
+
* @returns 16-byte authentication tag.
|
|
357
|
+
* @example
|
|
358
|
+
* Authenticates one message with a one-shot Poly1305 call and a fresh key.
|
|
359
|
+
*
|
|
360
|
+
* ```ts
|
|
361
|
+
* import { poly1305 } from './_poly1305';
|
|
362
|
+
* import { randomBytes } from './utils';
|
|
363
|
+
* const key = randomBytes(32);
|
|
364
|
+
* poly1305(new Uint8Array(), key);
|
|
365
|
+
* ```
|
|
366
|
+
*/
|
|
367
|
+
exports.poly1305 = (0, utils_1.wrapMacConstructor)(32, (key) => new Poly1305(key));
|
package/_polyval.d.ts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GHash from AES-GCM and its little-endian "mirror image" Polyval from AES-SIV.
|
|
3
|
+
*
|
|
4
|
+
* Implemented in terms of GHash with conversion function for keys
|
|
5
|
+
* GCM GHASH from
|
|
6
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf | NIST SP800-38d},
|
|
7
|
+
* SIV from
|
|
8
|
+
* {@link https://www.rfc-editor.org/rfc/rfc8452 | RFC 8452}.
|
|
9
|
+
*
|
|
10
|
+
* GHASH modulo: x^128 + x^7 + x^2 + x + 1
|
|
11
|
+
* POLYVAL modulo: x^128 + x^127 + x^126 + x^121 + 1
|
|
12
|
+
*
|
|
13
|
+
* @module
|
|
14
|
+
*/
|
|
15
|
+
import { type CMac, type IHash2, type TArg, type TRet } from './utils';
|
|
16
|
+
/**
|
|
17
|
+
* `mulX_GHASH(ByteReverse(H))` from RFC 8452 Appendix A.
|
|
18
|
+
* @param k mutated in place
|
|
19
|
+
*/
|
|
20
|
+
export declare function _toGHASHKey(k: TArg<Uint8Array>): TRet<Uint8Array>;
|
|
21
|
+
type Value = {
|
|
22
|
+
s0: number;
|
|
23
|
+
s1: number;
|
|
24
|
+
s2: number;
|
|
25
|
+
s3: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Incremental GHASH state for AES-GCM.
|
|
29
|
+
* @param key - 16-byte GHASH key.
|
|
30
|
+
* @param expectedLength - Expected message length for table sizing.
|
|
31
|
+
* Chunking is segment-based, not hash-streaming: every `update()` call is zero-padded
|
|
32
|
+
* to the next 16-byte boundary before it is absorbed. This matches the internal AES/GCM
|
|
33
|
+
* use where AAD, payload, and length block are separate padded segments.
|
|
34
|
+
* @example
|
|
35
|
+
* Feeds one ciphertext block into an incremental GHASH state with a fresh hash key.
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import { GHASH } from './_polyval';
|
|
39
|
+
* import { randomBytes } from './utils';
|
|
40
|
+
* const key = randomBytes(16);
|
|
41
|
+
* const mac = new GHASH(key);
|
|
42
|
+
* mac.update(new Uint8Array(16));
|
|
43
|
+
* mac.digest();
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare class GHASH implements IHash2 {
|
|
47
|
+
readonly blockLen: number;
|
|
48
|
+
readonly outputLen: number;
|
|
49
|
+
protected s0: number;
|
|
50
|
+
protected s1: number;
|
|
51
|
+
protected s2: number;
|
|
52
|
+
protected s3: number;
|
|
53
|
+
protected finished: boolean;
|
|
54
|
+
protected destroyed: boolean;
|
|
55
|
+
protected t: Value[];
|
|
56
|
+
private W;
|
|
57
|
+
private windowSize;
|
|
58
|
+
constructor(key: TArg<Uint8Array>, expectedLength?: number);
|
|
59
|
+
protected _updateBlock(s0: number, s1: number, s2: number, s3: number): void;
|
|
60
|
+
update(data: TArg<Uint8Array>): this;
|
|
61
|
+
destroy(): void;
|
|
62
|
+
digestInto(out: TArg<Uint8Array>): void;
|
|
63
|
+
digest(): TRet<Uint8Array>;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Incremental POLYVAL state for AES-SIV.
|
|
67
|
+
* @param key - 16-byte POLYVAL key.
|
|
68
|
+
* @param expectedLength - Expected message length for table sizing.
|
|
69
|
+
* Inherits GHASH's segment-padded `update()` behavior: each call is padded
|
|
70
|
+
* independently to a 16-byte boundary before absorption.
|
|
71
|
+
* @example
|
|
72
|
+
* Feeds one block into an incremental POLYVAL state with a fresh hash key.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* import { Polyval } from './_polyval';
|
|
76
|
+
* import { randomBytes } from './utils';
|
|
77
|
+
* const key = randomBytes(16);
|
|
78
|
+
* const mac = new Polyval(key);
|
|
79
|
+
* mac.update(new Uint8Array(16));
|
|
80
|
+
* mac.digest();
|
|
81
|
+
* ```
|
|
82
|
+
*/
|
|
83
|
+
export declare class Polyval extends GHASH {
|
|
84
|
+
constructor(key: TArg<Uint8Array>, expectedLength?: number);
|
|
85
|
+
update(data: TArg<Uint8Array>): this;
|
|
86
|
+
digestInto(out: TArg<Uint8Array>): void;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* GHash MAC for AES-GCM.
|
|
90
|
+
* @param msg - Message bytes to authenticate.
|
|
91
|
+
* @param key - 16-byte GHASH key.
|
|
92
|
+
* @returns 16-byte authentication tag.
|
|
93
|
+
* @example
|
|
94
|
+
* Authenticates a short message with GHASH and a fresh hash key.
|
|
95
|
+
*
|
|
96
|
+
* ```ts
|
|
97
|
+
* import { ghash } from './_polyval';
|
|
98
|
+
* import { randomBytes } from './utils';
|
|
99
|
+
* const key = randomBytes(16);
|
|
100
|
+
* ghash(new Uint8Array(), key);
|
|
101
|
+
* ```
|
|
102
|
+
*/
|
|
103
|
+
export declare const ghash: TRet<CMac<GHASH, [expectedLength?: number]>>;
|
|
104
|
+
/**
|
|
105
|
+
* POLYVAL MAC for AES-SIV.
|
|
106
|
+
* @param msg - Message bytes to authenticate.
|
|
107
|
+
* @param key - 16-byte POLYVAL key.
|
|
108
|
+
* @returns 16-byte authentication tag.
|
|
109
|
+
* @example
|
|
110
|
+
* Authenticates a short message with POLYVAL and a fresh hash key.
|
|
111
|
+
*
|
|
112
|
+
* ```ts
|
|
113
|
+
* import { polyval } from './_polyval';
|
|
114
|
+
* import { randomBytes } from './utils';
|
|
115
|
+
* const key = randomBytes(16);
|
|
116
|
+
* polyval(new Uint8Array(), key);
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare const polyval: TRet<CMac<Polyval, [expectedLength?: number]>>;
|
|
120
|
+
export {};
|