Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deoxysii async primitive #175

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions crypto/deoxysii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const nacl = require('tweetnacl');
const deoxys = require('deoxysii');

const boxKDFTweak_str = 'MRAE_Box_Deoxys-II-256-128';
var boxKDFTweak = Buffer.alloc(boxKDFTweak_str.length);
for (var i = 0; i < boxKDFTweak_str.length; i++) {
boxKDFTweak[i] = boxKDFTweak_str.charCodeAt(i);
}

// ECDHAndTweak applies the X25519 scalar multiply with the given public and
// private keys, and applies a HMAC based tweak to the resulting output.
function ECDHAndTweak(PublicKey, PrivateKey) {
let PreMasterKey = nacl.scalarMult(PrivateKey, PublicKey);

let hash = require('crypto').createHmac('sha256', boxKDFTweak);
hash.update(PreMasterKey);
return new Uint8Array(hash.digest());
}

module.exports = {
Seal: async function(Nonce, Plaintext, AdditionalData, PeerPublicKey, PrivateKey) {
let AesKey = ECDHAndTweak(PeerPublicKey, PrivateKey);

let AEAD = new deoxys.AEAD(AesKey);
return AEAD.encrypt(Nonce, Plaintext, AdditionalData);
},

Open: async function(Nonce, Ciphertext, AdditionalData, PeerPublicKey, PrivateKey) {
let AesKey = ECDHAndTweak(PeerPublicKey, PrivateKey);

let AEAD = new deoxys.AEAD(AesKey);
return AEAD.decrypt(Nonce, Ciphertext, AdditionalData);
}
};