Skip to content

Commit

Permalink
Merge pull request #51 from libitx/main
Browse files Browse the repository at this point in the history
Ensure blake3 inputs are immutable
  • Loading branch information
paulmillr committed Apr 27, 2023
2 parents 5ee1bce + 21cbc80 commit 15d067e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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();

0 comments on commit 15d067e

Please sign in to comment.