Improved encryption implementation
This commit is contained in:
parent
de7bd68872
commit
0a17e78fdc
@ -58,8 +58,9 @@ module.exports.handler = (argv) => {
|
|||||||
if(lock) {
|
if(lock) {
|
||||||
// Sign the message with the key that corresponds with this lock...
|
// Sign the message with the key that corresponds with this lock...
|
||||||
const encryptedKey = lock.extendedPublicKey.key.encryptedPrivateKey;
|
const encryptedKey = lock.extendedPublicKey.key.encryptedPrivateKey;
|
||||||
|
const iv = lock.extendedPublicKey.key.iv;
|
||||||
const password = "vanished";
|
const password = "vanished";
|
||||||
const walletXpriv = cryptoUtil.decrypt(encryptedKey, password);
|
const walletXpriv = cryptoUtil.decrypt(encryptedKey, password, iv);
|
||||||
|
|
||||||
var challengeDerivationPath = `${lock.extendedPublicKey.derivationPath}/${challenge.derivationPath.split("c/")[1]}`;
|
var challengeDerivationPath = `${lock.extendedPublicKey.derivationPath}/${challenge.derivationPath.split("c/")[1]}`;
|
||||||
|
|
||||||
|
@ -1,21 +1,24 @@
|
|||||||
const crypto = require('crypto');
|
const crypto = require('crypto');
|
||||||
const algorithm = 'aes-256-ecb';
|
const algorithm = 'aes256';
|
||||||
const bip32 = require('bip32');
|
const bip32 = require('bip32');
|
||||||
|
|
||||||
// NOTE I'm not a cryptographer... so don't be reusing any code from below as I don't have no idea what I'm doing
|
// NOTE I'm not a cryptographer... so don't be reusing any code from below as I don't have no idea what I'm doing
|
||||||
|
|
||||||
const iv = null; // If I knew what I was doing I would have a value assigned here...
|
|
||||||
|
|
||||||
module.exports.encrypt = (plainText, password) => {
|
module.exports.encrypt = (plainText, password) => {
|
||||||
|
const iv = crypto.randomBytes(16).toString('hex').slice(0, 16); // TODO figure out this iv stuff...
|
||||||
|
|
||||||
const key = crypto.createHash('sha256').update(password, 'utf8').digest();
|
const key = crypto.createHash('sha256').update(password, 'utf8').digest();
|
||||||
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
const cipher = crypto.createCipheriv(algorithm, key, iv);
|
||||||
|
|
||||||
var cipherText = cipher.update(plainText, 'utf8', 'hex');
|
var cipherText = cipher.update(plainText, 'utf8', 'hex');
|
||||||
cipherText += cipher.final('hex');
|
cipherText += cipher.final('hex');
|
||||||
return cipherText;
|
return {
|
||||||
|
cipherText: cipherText,
|
||||||
|
iv: iv
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.decrypt = (cipherText, password) => {
|
module.exports.decrypt = (cipherText, password, iv) => {
|
||||||
const key = crypto.createHash('sha256').update(password, 'utf8').digest();
|
const key = crypto.createHash('sha256').update(password, 'utf8').digest();
|
||||||
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
const decipher = crypto.createDecipheriv(algorithm, key, iv);
|
||||||
|
|
||||||
@ -36,6 +39,17 @@ module.exports.randomDerivationPath = function(hardenedDerivation) {
|
|||||||
return randomNumbers.join('/')
|
return randomNumbers.join('/')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.verifyChallenge = function(serviceXpub, challenge) {
|
||||||
|
const requestSignature = challenge.request.signature;
|
||||||
|
const requestDerivationPath = challenge.request.signatureDerivationPath;
|
||||||
|
return this.verifyMessage(
|
||||||
|
serviceXpub,
|
||||||
|
requestDerivationPath,
|
||||||
|
JSON.stringify(this.unsignedChallenge(challenge)),
|
||||||
|
requestSignature
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.verifyMessage = function(base58Key, derivationPath, message, signature) {
|
module.exports.verifyMessage = function(base58Key, derivationPath, message, signature) {
|
||||||
if(derivationPath.startsWith("a/")) {
|
if(derivationPath.startsWith("a/")) {
|
||||||
derivationPath = derivationPath.split("a/")[1];
|
derivationPath = derivationPath.split("a/")[1];
|
||||||
@ -51,10 +65,20 @@ module.exports.verifyMessage = function(base58Key, derivationPath, message, sign
|
|||||||
return verificationNode.verify(hash, Buffer.from(signature, 'hex'));
|
return verificationNode.verify(hash, Buffer.from(signature, 'hex'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.unsignedChallenge = function(challenge) {
|
||||||
|
return {
|
||||||
|
// keys must be alphabetically ordered
|
||||||
|
derivationPath: challenge.derivationPath,
|
||||||
|
message: challenge.message,
|
||||||
|
xpub: challenge.xpub
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports.signMessage = function(base58Key, derivationPath, message) {
|
module.exports.signMessage = function(base58Key, derivationPath, message) {
|
||||||
let signingNode = bip32.fromBase58(base58Key).derivePath(derivationPath);
|
let signingNode = bip32.fromBase58(base58Key).derivePath(derivationPath);
|
||||||
|
|
||||||
var hash = crypto.createHash('sha256').update(message, 'utf8').digest();
|
var hash = crypto.createHash('sha256').update(message, 'utf8').digest();
|
||||||
|
|
||||||
return signingNode.sign(hash);
|
return signingNode.sign(hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -12,6 +12,10 @@ module.exports = function (sequelize, DataTypes, options) {
|
|||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
allowNull: false
|
allowNull: false
|
||||||
},
|
},
|
||||||
|
iv: {
|
||||||
|
type: DataTypes.STRING,
|
||||||
|
allowNull: false
|
||||||
|
},
|
||||||
name: {
|
name: {
|
||||||
type: DataTypes.STRING,
|
type: DataTypes.STRING,
|
||||||
unique: true
|
unique: true
|
||||||
|
Loading…
x
Reference in New Issue
Block a user