Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async init (size) {
await super.init(size)
// Create sodium buffer
this._alignSize = Math.ceil(size / 8) * 8
this._b = sodium.sodium_malloc(this._alignSize)
// lock it
sodium.sodium_mprotect_noaccess(this._b)
// Destructor: Clear buffer memory
this.$pushDestructor(() => {
// normally sodium free would clear the buffer...
// but since we're waiting for js gc, let's clear it now
sodium.sodium_mprotect_readwrite(this._b)
this._b.fill(0)
sodium.sodium_mprotect_noaccess(this._b)
this._b = null
})
}
async init (size) {
await super.init(size)
this._alignSize = Math.ceil(size / 8) * 8
this._b = sodium.sodium_malloc(this._alignSize)
sodium.sodium_mprotect_noaccess(this._b)
this.$pushDestructor(() => {
// normally sodium free would clear the buffer...
// but since we're waiting for js gc, let's clear it now
sodium.sodium_mprotect_readwrite(this._b)
this._b.fill(0)
sodium.sodium_mprotect_noaccess(this._b)
this._b = null
})
}
test('sign with keypairGen keys', (t) => {
var emptyBuf = Buffer.from('')
var pwd = sodium.sodium_malloc(emptyBuf.byteLength)
pwd.fill(emptyBuf)
var keyGen = minisign.keypairGen(pwd)
var key = minisign.formatKeys(keyGen)
var PK = minisign.parsePubKey(key.PK)
var SKinfo = minisign.parseSecretKey(key.SK)
var SK = minisign.extractSecretKey(pwd, SKinfo)
var toSign = Buffer.from('sign me please.')
var signOutput = minisign.signContent(toSign, SK)
var parsedOutput = minisign.parseSignature(signOutput.outputBuf)
t.ok(minisign.verifySignature(parsedOutput, toSign, PK))
t.end()
test('keypairGen output', function (t) {
var emptyBuf = Buffer.from('')
var pwd = sodium.sodium_malloc(emptyBuf.byteLength)
pwd.fill(emptyBuf)
var keyGen = minisign.keypairGen(pwd)
var key = minisign.formatKeys(keyGen)
var PKiD = minisign.parsePubKey(key.PK).keyID
var SKinfo = minisign.parseSecretKey(key.SK)
var SK = minisign.extractSecretKey(pwd, SKinfo)
t.equal(SK.secretKey.byteLength, sodium.crypto_sign_SECRETKEYBYTES)
t.deepEqual(SK.keyID, PKiD)
t.equal(SK.signatureAlgorithm, 'Ed')
t.end()
})
test('keypairGen with only one comment', (t) => {
const untrustedPrelude = Buffer.from('untrusted comment: ')
var startIndex = untrustedPrelude.byteLength
var comment1 = Buffer.from('this will appear in public key,')
var comment2 = Buffer.from('but this appears in secret key.')
var endIndex1 = comment1.byteLength + startIndex
var endIndex2 = comment2.byteLength + startIndex
var emptyBuf = Buffer.from('')
var pwd = sodium.sodium_malloc(emptyBuf.byteLength)
pwd.fill(emptyBuf)
var opts1 = {
PKcomment: comment1.toString()
}
var opts2 = {
SKcomment: comment2.toString()
}
var keyGenOpts1 = minisign.keypairGen(pwd, opts1)
var keyOpts1 = minisign.formatKeys(keyGenOpts1)
var keyGenOpts2 = minisign.keypairGen(pwd, opts2)
var keyOpts2 = minisign.formatKeys(keyGenOpts2)
t.equal(keyOpts1.PKcomment, keyOpts1.SKcomment)
test('using too small kdfOpsLimit', function (t) {
var emptyBuf = Buffer.from('')
var pwd = sodium.sodium_malloc(emptyBuf.byteLength)
pwd.fill(emptyBuf)
fs.readFile('./test/fixtures/no-string.key', function (err, SK) {
t.error(err)
var SKinfo = minisign.parseSecretKey(SK)
SKinfo.kdfOpsLimit--
t.throws(() => minisign.extractSecretKey(pwd, SKinfo), 'invalid check sum')
t.end()
})
})
test('signContent generated input', (t) => {
var toSign = Buffer.alloc(200)
sodium.randombytes_buf(toSign)
var emptyBuf = Buffer.from('')
var pwd = sodium.sodium_malloc(emptyBuf.byteLength)
pwd.fill(emptyBuf)
fs.readFile('./test/fixtures/minisign.key', (err, SK) => {
t.error(err)
var SKinfo = minisign.parseSecretKey(SK)
var SKdetails = minisign.extractSecretKey(pwd, SKinfo)
var signedOutput = minisign.signContent(toSign, SKdetails).outputBuf
var parsedOutput = minisign.parseSignature(signedOutput)
fs.readFile('./test/fixtures/minisign.pub', (err, PK) => {
t.error(err)
var PKinfo = minisign.parsePubKey(PK)
t.ok(minisign.verifySignature(parsedOutput, toSign, PKinfo))
t.end()
Rendezvous.prototype.read = function (remoteKey, cb) {
assert(remoteKey.byteLength === sodium.crypto_kx_PUBLICKEYBYTES)
var self = this
var rendevousPoint = Buffer.allocUnsafe(sodium.crypto_kx_SESSIONKEYBYTES)
sodium.crypto_kx_client_session_keys(
null,
rendevousPoint,
self.keypair.publicKey,
self.keypair.secretKey,
remoteKey
)
var discoveryPk = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES)
var discoverySk = sodium.sodium_malloc(sodium.crypto_sign_SECRETKEYBYTES)
sodium.crypto_sign_seed_keypair(discoveryPk, discoverySk, rendevousPoint)
var hash = crypto.createHash('sha1').update(discoveryPk).digest()
self.dht.get(hash, {
verify: sodium.crypto_sign_verify_detached,
cache: false
}, cb)
}