Skip to content

Commit

Permalink
fix: unbox nonce and key length
Browse files Browse the repository at this point in the history
  • Loading branch information
bplaster authored and jwerle committed Apr 2, 2021
1 parent c35d221 commit 033d294
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions unbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const through = require('through2')
const {
crypto_secretbox_KEYBYTES,
crypto_secretbox_MACBYTES,
crypto_secretbox_NONCEBYTES,

crypto_secretbox_open_easy,
} = require('./sodium')
Expand Down Expand Up @@ -46,14 +47,15 @@ function unbox(buffer, opts) {
if (false === isBuffer(key)) {
throw new TypeError('crypto.unbox: Expecting secret key.')
}


key = key.slice(0, crypto_secretbox_KEYBYTES)
const nonces = [ copy(nonce), increment(copy(nonce)) ]
const header = Buffer.allocUnsafe(2 + crypto_secretbox_MACBYTES)

crypto_secretbox_open_easy(
header,
buffer.slice(0, 2 + (2 * crypto_secretbox_MACBYTES)),
nonces[0],
nonces[0].slice(0, crypto_secretbox_NONCEBYTES),
key
)

Expand All @@ -75,7 +77,7 @@ function unbox(buffer, opts) {
crypto_secretbox_open_easy(
unboxed,
combined,
nonces[1],
nonces[1].slice(0, crypto_secretbox_NONCEBYTES),
key
)

Expand Down

0 comments on commit 033d294

Please sign in to comment.