Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions blake2b.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ function blake2bInit (outlen, key, salt, personal) {
if (outlen === 0 || outlen > 64) {
throw new Error('Illegal output length, expected 0 < length <= 64')
}
if (key && !(key instanceof Uint8Array)) {
throw new Error(`Illegal key, expected Uint8Array with 0 < length <= 64, got ${typeof key}`)
}
if (key && key.length > 64) {
throw new Error('Illegal key, expected Uint8Array with 0 < length <= 64')
}
Expand Down
3 changes: 3 additions & 0 deletions blake2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ function blake2sInit (outlen, key) {
if (!(outlen > 0 && outlen <= 32)) {
throw new Error('Incorrect output length, should be in [1, 32]')
}
if (key && !(key instanceof Uint8Array)) {
throw new Error(`Illegal key, expected Uint8Array with 0 < length <= 32, got ${typeof key}`)
}
const keylen = key ? key.length : 0
if (key && !(keylen > 0 && keylen <= 32)) {
throw new Error('Incorrect key length, should be in [1, 32]')
Expand Down
17 changes: 17 additions & 0 deletions test_blake2b.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@ test('Input types', function (t) {
t.end()
})

test('Does not accept non-Uint8 data as "key" parameter', function (t) {
t.throws(() => {
blake2bHex('The quick brown fox jumps over the lazy dog', 'aStringKey')
},
/Illegal key, expected Uint8Array with 0 < length <= 64, got string/
)

t.throws(() => {
const nonUint8Array = ['nonSensicalValue']
blake2bHex('The quick brown fox jumps over the lazy dog', nonUint8Array)
},
'Illegal key, expected Uint8Array with 0 < length <= 64, got array'
)

t.end()
})

test('BLAKE2b generated test vectors', function (t) {
const contents = fs.readFileSync('generated_test_vectors.txt', 'utf8')
contents.split('\n').forEach(function (line) {
Expand Down
17 changes: 17 additions & 0 deletions test_blake2s.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ function generateInput (len, seed) {
return out
}

test('Does not accept non-Uint8 data as "key" parameter', function (t) {
t.throws(() => {
blake2sHex('The quick brown fox jumps over the lazy dog', 'aStringKey')
},
/Illegal key, expected Uint8Array with 0 < length <= 32, got string/
)

t.throws(() => {
const nonUint8Array = ['nonSensicalValue']
blake2sHex('The quick brown fox jumps over the lazy dog', nonUint8Array)
},
'Illegal key, expected Uint8Array with 0 < length <= 32, got array'
)

t.end()
})

test('BLAKE2s performance', function (t) {
const N = 1 << 22 // number of bytes to hash
const RUNS = 3 // how often to repeat, to allow JIT to finish
Expand Down