Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure blake3 inputs are immutable #51

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/blake3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ class BLAKE3 extends BLAKE2<BLAKE3> implements HashXOF<BLAKE3> {
this.IV = u32(context_key);
this.flags = flags | Flags.DERIVE_KEY_MATERIAL;
} else {
this.IV = IV.slice();
this.IV = IV;
this.flags = flags;
}
this.IV = this.IV.slice();
this.state = this.IV.slice();
this.bufferOut = u8(this.bufferOut32);
}
Expand Down
43 changes: 43 additions & 0 deletions test/blake.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,47 @@ should('Blake3 XOF', () => {
assert.deepStrictEqual(concatBytes(...out), bigOut, 'xof check against fixed size');
});

should('BLAKE2b inputs are immuatable', () => {
const msg = new Uint8Array([1, 2, 3, 4]);
const key = new Uint8Array([1, 2, 3, 4]);
const pers = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]);
const salt = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]);
blake2b(msg, { key, salt, personalization: pers });
assert.deepStrictEqual(msg, new Uint8Array([1, 2, 3, 4]));
assert.deepStrictEqual(key, new Uint8Array([1, 2, 3, 4]));
assert.deepStrictEqual(pers, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]));
assert.deepStrictEqual(salt, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8]));
});

should('BLAKE2s inputs are immuatable', () => {
const msg = new Uint8Array([1, 2, 3, 4]);
const key = new Uint8Array([1, 2, 3, 4]);
const pers = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
const salt = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]);
blake2s(msg, { key, salt, personalization: pers });
assert.deepStrictEqual(msg, new Uint8Array([1, 2, 3, 4]));
assert.deepStrictEqual(key, new Uint8Array([1, 2, 3, 4]));
assert.deepStrictEqual(pers, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]));
assert.deepStrictEqual(salt, new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]));
});

should('BLAKE3 inputs are immuatable', () => {
const msg = new Uint8Array([1, 2, 3, 4]);
const ctx = new Uint8Array([1, 2, 3, 4]);
const key = new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8,
]);
blake3(msg, { key });
blake3(msg, { context: ctx });
assert.deepStrictEqual(msg, new Uint8Array([1, 2, 3, 4]));
assert.deepStrictEqual(ctx, new Uint8Array([1, 2, 3, 4]));
assert.deepStrictEqual(
key,
new Uint8Array([
1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7,
8,
])
);
});

if (require.main === module) should.run();