Skip to content
This repository has been archived by the owner on Jan 7, 2020. It is now read-only.

Commit

Permalink
fix/Crypto: KeyBytes were not being copied after returned by safe_cor…
Browse files Browse the repository at this point in the history
…e causing a segv as memory was reused by the lib (#96)
  • Loading branch information
bochaco authored and hitman401 committed Jul 4, 2017
1 parent eea78bf commit 585cdb1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
29 changes: 19 additions & 10 deletions src/native/_crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@ function strToBuffer(str) {
}

function toBuffer(app, key) {
let keyArr = key.buffer || key;
if (keyArr.length != 32) throw Error("Sign/Enc Keys _must be_ 32 bytes long.")
keyArr = t.KEYBYTES(keyArr);
return [app, keyArr]
let keyArr = key;
if (!Buffer.isBuffer(key)) {
const b = new Buffer(key);
if (b.length != 32) throw Error("Sign/Enc Keys _must be_ 32 bytes long.")
keyArr = t.KEYBYTES(b).ref().readPointer(0);
}
return [app, keyArr];
}

function appStrToBuffer(appPtr, str) {
return [appPtr].concat(strToBuffer(str)).concat(Array.prototype.slice.call(arguments, 2))
}

// Helper to create a copy of the received KEYBYTES array as it might
// be overwritten after the callback finishes.
function copyKeyBytesArray(res) {
return t.KEYBYTES(ref.reinterpret(res[0], 32));
}

module.exports = {
types: {
SignKeyHandle,
Expand All @@ -39,18 +48,18 @@ module.exports = {
},
functions: {
app_pub_sign_key: [t.Void, [t.AppPtr, 'pointer', 'pointer']],
sign_key_new: [t.Void, [t.AppPtr, t.KEYBYTES, 'pointer', 'pointer']],
sign_key_new: [t.Void, [t.AppPtr, ref.refType(t.KEYBYTES), 'pointer', 'pointer']],
sign_key_get: [t.Void, [t.AppPtr, SignKeyHandle, 'pointer', 'pointer']],
sign_key_free: [t.Void, [t.AppPtr, SignKeyHandle, 'pointer', 'pointer']],

app_pub_enc_key: [t.Void, [t.AppPtr, 'pointer', 'pointer']],
enc_generate_key_pair: [t.Void, [t.AppPtr, 'pointer', 'pointer']],

enc_pub_key_new: [t.Void, [t.AppPtr, t.KEYBYTES, 'pointer', 'pointer']],
enc_pub_key_new: [t.Void, [t.AppPtr, ref.refType(t.KEYBYTES), 'pointer', 'pointer']],
enc_pub_key_get: [t.Void, [t.AppPtr, EncryptPubKeyHandle, 'pointer', 'pointer']],
enc_secret_key_free: [t.Void, [t.AppPtr, EncryptPubKeyHandle, 'pointer', 'pointer']],

enc_secret_key_new: [t.Void, [t.AppPtr, t.KEYBYTES, 'pointer', 'pointer']],
enc_secret_key_new: [t.Void, [t.AppPtr, ref.refType(t.KEYBYTES), 'pointer', 'pointer']],
enc_secret_key_get: [t.Void, [t.AppPtr, EncryptSecKeyHandle, 'pointer', 'pointer']],
enc_secret_key_free: [t.Void, [t.AppPtr, EncryptSecKeyHandle, 'pointer', 'pointer']],

Expand All @@ -66,17 +75,17 @@ module.exports = {
api: {
app_pub_sign_key: h.Promisified(null, SignKeyHandle),
sign_key_new: h.Promisified(toBuffer, SignKeyHandle),
sign_key_get: h.Promisified(null, t.KEYBYTES),
sign_key_get: h.Promisified(null, ['pointer'], copyKeyBytesArray),
sign_key_free: h.Promisified(null, []),

app_pub_enc_key: h.Promisified(null, EncryptKeyHandle),
enc_generate_key_pair: h.Promisified(null, [EncryptPubKeyHandle, EncryptSecKeyHandle]),

enc_pub_key_new: h.Promisified(toBuffer, EncryptPubKeyHandle),
enc_pub_key_get: h.Promisified(null, t.KEYBYTES),
enc_pub_key_get: h.Promisified(null, ['pointer'], copyKeyBytesArray),

enc_secret_key_new: h.Promisified(toBuffer, EncryptPubKeyHandle),
enc_secret_key_get: h.Promisified(null, t.KEYBYTES),
enc_secret_key_get: h.Promisified(null, ['pointer'], copyKeyBytesArray),

encrypt: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
decrypt: h.Promisified(appStrToBuffer, [t.u8Pointer, t.usize], h.asBuffer),
Expand Down
2 changes: 1 addition & 1 deletion test/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe('App Crypto Tests', () => {
})
]).then((r) => should(r[0]).not.equal(r[1])));

it.skip('generate key pair from raw keys', () => {
it('generate key pair from raw keys', () => {
let rawPubKey;
let rawPubKeyFromRaw;
let rawSecKey;
Expand Down

0 comments on commit 585cdb1

Please sign in to comment.