Skip to content

Commit d2750a1

Browse files
committed
Use u64Lengths util.
1 parent 68518e5 commit d2750a1

File tree

7 files changed

+25
-33
lines changed

7 files changed

+25
-33
lines changed

‎src/_micro.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ import {
1313
type XorStream,
1414
bytesToHex,
1515
concatBytes,
16-
createView,
1716
equalBytes,
1817
hexToNumber,
1918
numberToBytesBE,
20-
setBigUint64,
19+
u64Lengths,
2120
wrapCipher,
2221
} from './utils.ts';
2322

@@ -235,12 +234,7 @@ function computeTag(
235234
res.push(ciphertext);
236235
const leftover = ciphertext.length % 16;
237236
if (leftover > 0) res.push(new Uint8Array(16 - leftover));
238-
// Lengths
239-
const num = new Uint8Array(16);
240-
const view = createView(num);
241-
setBigUint64(view, 0, BigInt(AAD ? AAD.length : 0), true);
242-
setBigUint64(view, 8, BigInt(ciphertext.length), true);
243-
res.push(num);
237+
res.push(u64Lengths(ciphertext.length, AAD ? AAD.length : 0, true));
244238
const authKey = fn(key, nonce, new Uint8Array(32));
245239
return poly1305(concatBytes(...res), authKey);
246240
}

‎src/_polyval.ts

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ class GHASH implements Hash<GHASH> {
189189
class Polyval extends GHASH {
190190
constructor(key: Input, expectedLength?: number) {
191191
key = toBytes(key);
192+
abytes(key);
192193
const ghKey = _toGHASHKey(copyBytes(key));
193194
super(ghKey, expectedLength);
194195
clean(ghKey);

‎src/aes.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
overlapBytes,
3131
setBigUint64,
3232
u32,
33+
u64Lengths,
3334
u8,
3435
wrapCipher,
3536
} from './utils.ts';
@@ -590,14 +591,11 @@ function computeTag(
590591
data: Uint8Array,
591592
AAD?: Uint8Array
592593
) {
593-
const aadLength = AAD == null ? 0 : AAD.length;
594+
const aadLength = AAD ? AAD.length : 0;
594595
const h = fn.create(key, data.length + aadLength);
595596
if (AAD) h.update(AAD);
597+
const num = u64Lengths(8 * data.length, 8 * aadLength, isLE);
596598
h.update(data);
597-
const num = new Uint8Array(16);
598-
const view = createView(num);
599-
if (AAD) setBigUint64(view, 0, BigInt(aadLength * 8), isLE);
600-
setBigUint64(view, 8, BigInt(data.length * 8), isLE);
601599
h.update(num);
602600
const res = h.digest();
603601
clean(num);

���src/chacha.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ import {
1919
type CipherWithOutput,
2020
type XorStream,
2121
clean,
22-
createView,
2322
equalBytes,
2423
getOutput,
25-
setBigUint64,
24+
u64Lengths,
2625
wrapCipher,
2726
} from './utils.ts';
2827

@@ -223,10 +222,7 @@ function computeTag(
223222
const h = poly1305.create(authKey);
224223
if (AAD) updatePadded(h, AAD);
225224
updatePadded(h, data);
226-
const num = new Uint8Array(16);
227-
const view = createView(num);
228-
setBigUint64(view, 0, BigInt(AAD ? AAD.length : 0), true);
229-
setBigUint64(view, 8, BigInt(data.length), true);
225+
const num = u64Lengths(data.length, AAD ? AAD.length : 0, true);
230226
h.update(num);
231227
const res = h.digest();
232228
clean(authKey, num);

‎src/ff1.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function getRound(radix: number, key: Uint8Array, tweak: Uint8Array, x: number[]
4545
const d = 4 * Math.ceil(b / 4) + 4;
4646
const padding = mod(-tweak.length - b - 1, 16);
4747
// P = [1]1 || [2]1 || [1]1 || [radix]3 || [10]1 || [u mod 256]1 || [n]4 || [t]4.
48-
const P = new Uint8Array([1, 2, 1, 0, 0, 0, 10, u, 0, 0, 0, 0, 0, 0, 0, 0]);
48+
const P = Uint8Array.from([1, 2, 1, 0, 0, 0, 10, u, 0, 0, 0, 0, 0, 0, 0, 0]);
4949
const view = new DataView(P.buffer);
5050
view.setUint16(4, radix, false);
5151
view.setUint32(8, x.length, false);
@@ -93,7 +93,7 @@ function getRound(radix: number, key: Uint8Array, tweak: Uint8Array, x: number[]
9393
return { u, round, destroy };
9494
}
9595

96-
const EMPTY_BUF = new Uint8Array([]);
96+
const EMPTY_BUF = /* @__PURE__ */ Uint8Array.of();
9797

9898
/** FPE-FF1 format-preserving encryption */
9999
export function FF1(

‎src/utils.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -433,12 +433,12 @@ export function setBigUint64(
433433
view.setUint32(byteOffset + l, wl, isLE);
434434
}
435435

436-
// TODO: unused anywhere
437-
export function u64Lengths(ciphertext: Uint8Array, AAD?: Uint8Array): Uint8Array {
436+
export function u64Lengths(dataLength: number, aadLength: number, isLE: boolean): Uint8Array {
437+
abool(isLE);
438438
const num = new Uint8Array(16);
439439
const view = createView(num);
440-
setBigUint64(view, 0, BigInt(AAD ? AAD.length : 0), true);
441-
setBigUint64(view, 8, BigInt(ciphertext.length), true);
440+
setBigUint64(view, 0, BigInt(aadLength), isLE);
441+
setBigUint64(view, 8, BigInt(dataLength), isLE);
442442
return num;
443443
}
444444

‎test/utils.test.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
import { deepStrictEqual, throws } from 'node:assert';
21
import fc from 'fast-check';
32
import { describe, should } from 'micro-should';
4-
import { TYPE_TEST, unalign } from './utils.js';
3+
import { deepStrictEqual, throws } from 'node:assert';
54
import * as assert from '../esm/_assert.js';
65
import {
7-
createView,
86
bytesToHex,
7+
bytesToUtf8,
98
concatBytes,
9+
createView,
10+
getOutput,
1011
hexToBytes,
1112
overlapBytes,
12-
toBytes,
13-
bytesToUtf8,
14-
getOutput,
1513
setBigUint64,
14+
toBytes,
1615
u64Lengths,
1716
} from '../esm/utils.js';
17+
import { TYPE_TEST, unalign } from './utils.js';
1818

1919
describe('utils', () => {
2020
const staticHexVectors = [
@@ -188,9 +188,12 @@ describe('utils', () => {
188188
}
189189
});
190190
should('u64Lengths', () => {
191-
deepStrictEqual(bytesToHex(u64Lengths(new Uint8Array(10))), '00000000000000000a00000000000000');
192191
deepStrictEqual(
193-
bytesToHex(u64Lengths(new Uint8Array(10), new Uint8Array(7))),
192+
bytesToHex(u64Lengths(new Uint8Array(10).length, 0, true)),
193+
'00000000000000000a00000000000000'
194+
);
195+
deepStrictEqual(
196+
bytesToHex(u64Lengths(new Uint8Array(10).length, new Uint8Array(7).length, true)),
194197
'07000000000000000a00000000000000'
195198
);
196199
});

0 commit comments

Comments
 (0)