Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// PRF Count to benchmark
var count = process.argv[2];
if (count == null) {
count = 200;
}
// Dependencies
var _sodium = require('libsodium-wrappers-sumo');
var _oprf = require('oprf');
var BN = require('bn.js');
_sodium.ready.then(function () {
var oprf = new _oprf.OPRF(_sodium);
var scalarKey = new BN(oprf.generateRandomScalar()).toString();
for (var i = 1; i <= count; i++) {
var point = oprf.hashToPoint(i.toString());
oprf.scalarMult(point, scalarKey);
}
console.log('done');
});
it('encodes a message', () => {
const key_material_buffer = new ArrayBuffer(32);
const typed_key_material = new Uint8Array(key_material_buffer);
const mac_key = new Proteus.derived.MacKey(typed_key_material);
const message = sodium.from_string('hello');
const authentication_code = mac_key.sign(message);
// prettier-ignore
const expected = new Uint8Array([67, 82, 178, 110, 51, 254, 13, 118, 154, 137, 34, 166, 186, 41, 0, 65, 9, 240, 22, 136, 226, 106, 204, 158, 108, 179, 71, 229, 165, 175, 196, 218]);
expect(authentication_code).toEqual(expected);
});
} = config;
if (!fs.existsSync(dataPath)) {
fs.mkdirSync(dataPath);
}
app.storage = new Storage(dataPath, appId);
if (testMode) {
app.trustchaindClient = new FakeTrustchaindClient();
} else {
const trustchaindUrl = config.url || 'https://api.tanker.io';
app.trustchaindClient = new TrustchaindClient({ trustchaindUrl, authToken, appId });
}
// Libsodium loads asynchronously (Wasm module)
await sodium.ready;
};
state = bob.session_states[bob.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(1);
expect(state.recv_chains[0].chain_key.idx).toBe(1);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(0);
const hello_alice0_plaintext = 'Hello0';
const hello_alice0_encrypted = await bob.encrypt(hello_alice0_plaintext);
await bob.encrypt('Hello1'); // unused result
const hello_alice2_plaintext = 'Hello2';
const hello_alice2_encrypted = await bob.encrypt(hello_alice2_plaintext);
const hello_alice2_decrypted = await alice.decrypt(alice_store, hello_alice2_encrypted);
expect(sodium.to_string(hello_alice2_decrypted)).toBe(hello_alice2_plaintext);
// Alice has two skipped message keys in her new receive chain:
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].chain_key.idx).toBe(3);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(2);
expect(state.recv_chains[0].message_keys[0].counter).toBe(0);
expect(state.recv_chains[0].message_keys[1].counter).toBe(1);
const hello_bob0_plaintext = 'Hello0';
const hello_bob0_encrypted = await alice.encrypt(hello_bob0_plaintext);
const hello_bob0_decrypted = await bob.decrypt(bob_store, hello_bob0_encrypted);
expect(sodium.to_string(hello_bob0_decrypted)).toBe(hello_bob0_plaintext);
expect(sodium.to_string(hello_alice2_decrypted)).toBe(hello_alice2_plaintext);
// Alice has two skipped message keys in her new receive chain:
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].chain_key.idx).toBe(3);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(2);
expect(state.recv_chains[0].message_keys[0].counter).toBe(0);
expect(state.recv_chains[0].message_keys[1].counter).toBe(1);
const hello_bob0_plaintext = 'Hello0';
const hello_bob0_encrypted = await alice.encrypt(hello_bob0_plaintext);
const hello_bob0_decrypted = await bob.decrypt(bob_store, hello_bob0_encrypted);
expect(sodium.to_string(hello_bob0_decrypted)).toBe(hello_bob0_plaintext);
// For Bob everything is normal still. A new message from Alice means a
// new receive chain has been created and again no skipped message keys.
state = bob.session_states[bob.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].chain_key.idx).toBe(1);
expect(state.send_chain.chain_key.idx).toBe(0);
expect(state.recv_chains[0].message_keys.length).toBe(0);
const hello_alice0_decrypted = await alice.decrypt(alice_store, hello_alice0_encrypted);
expect(sodium.to_string(hello_alice0_decrypted)).toBe(hello_alice0_plaintext);
// Alice received the first of the two missing messages. Therefore
// only one message key is still skipped (counter value = 1).
// Alice received the first of the two missing messages. Therefore
// only one message key is still skipped (counter value = 1).
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(2);
expect(state.recv_chains[0].message_keys.length).toBe(1);
expect(state.recv_chains[0].message_keys[0].counter).toBe(1);
const hello_again0_plaintext = 'Again0';
const hello_again0_encrypted = await bob.encrypt(hello_again0_plaintext);
const hello_again1_plaintext = 'Again1';
const hello_again1_encrypted = await bob.encrypt(hello_again1_plaintext);
const hello_again1_decrypted = await alice.decrypt(alice_store, hello_again1_encrypted);
expect(sodium.to_string(hello_again1_decrypted)).toBe(hello_again1_plaintext);
// Alice received the first of the two missing messages. Therefore
// only one message key is still skipped (counter value = 1).
state = alice.session_states[alice.session_tag.toString()].state;
expect(state.recv_chains.length).toBe(3);
expect(state.recv_chains[0].message_keys.length).toBe(1);
expect(state.recv_chains[1].message_keys.length).toBe(1);
expect(state.recv_chains[0].message_keys[0].counter).toBe(0);
expect(state.recv_chains[1].message_keys[0].counter).toBe(1);
const hello_again0_decrypted = await alice.decrypt(alice_store, hello_again0_encrypted);
expect(sodium.to_string(hello_again0_decrypted)).toBe(hello_again0_plaintext);
});
export async function unlockAddress(deviceType: HardwareDeviceType, derivationPath: string): Promise {
if (!deviceType) {
const hexEncodedPublicKey = await ledgerUtils.getTezosPublicKey(derivationPath);
// We slice off a byte to make sure we have a 64 bits coming in from the ledger package
const publicKeyBytes = sodium.from_hex(hexEncodedPublicKey).slice(1);
const publicKey = base58CheckEncode(publicKeyBytes, "edpk");
const publicKeyHash = base58CheckEncode(sodium.crypto_generichash(20, publicKeyBytes), "tz1");
return {
publicKey: publicKey,
privateKey: '',
publicKeyHash: publicKeyHash,
seed: '',
storeType: StoreType.Hardware
}
} else {
const {publicKey, address} = await trezorUtils.getTezosPublicKey(derivationPath);
return {
publicKey: publicKey,
privateKey: '',
publicKeyHash: address,
seed: '',
sodium.ready.then(() => {
if (!this.initPwd) {
// Get salt from stream to re-generate key
if (!this.pwdHeader) {
this.pwdHeader = Buffer.alloc(sodium.crypto_pwhash_SALTBYTES + 1);
}
if (!this.key) {
const saltBytesCopied = data.copy(this.pwdHeader, this.pwdHeaderOffset);
this.pwdHeaderOffset += saltBytesCopied;
if (this.pwdHeaderOffset < sodium.crypto_pwhash_SALTBYTES) {
return callback(null, null);
}
if (this.pwdHeader[0] !== PWD_STREAM_VERSION) {
return callback(new Error('Unsupported PwdEncryptionStream version'));
}
this.key = sodium.crypto_pwhash(
KEY_SIZE,
this.pwd,
this.pwdHeader.slice(1),
sodium.crypto_pwhash_OPSLIMIT_INTERACTIVE,
sodium.crypto_pwhash_MEMLIMIT_INTERACTIVE,
sodium.crypto_pwhash_ALG_DEFAULT
);
this.initPwd = true;
data = data.slice(saltBytesCopied);
TransactionHelper.prototype.signShielded = function (signatureHashFn) {
// Empty output script
var scriptCode = Buffer.alloc(0)
var dataToBeSigned = signatureHashFn(NOT_AN_INPUT, scriptCode, SIGHASH_ALL)
// Add the signature
this.joinSplitSig = Buffer.from(sodium.crypto_sign_detached(dataToBeSigned, this._joinSplitPrivKey))
// Sanity check
sodium.crypto_sign_verify_detached(this.joinSplitSig, dataToBeSigned, this.joinSplitPubKey)
}
const sodium = require('libsodium-wrappers-sumo');
const extcrypto = require('../../extcrypto');
const V1 = require('../protocol/V1');
const V2 = require('../protocol/V2');
const utils = require('../utils');
const PublicKey = require('./public');
// patch
sodium.crypto_sign_KEYPAIRBYTES = sodium.crypto_sign_SECRETKEYBYTES + sodium.crypto_sign_PUBLICKEYBYTES;
/***
* PrivateKey
*
* private key for asymmetric cryptography
*
* @constructor
*
* @api public
*
* @param {Object} protocol
*/
module.exports = PrivateKey;
function PrivateKey(protocol) {
const self = this;