Skip to content

Commit

Permalink
Code reviewed. Documentation for ECIES-DOA-DS and ECIES updated.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christos Patsonakis committed Apr 12, 2021
1 parent 993d508 commit ee8b7d9
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 122 deletions.
164 changes: 93 additions & 71 deletions README.md

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions bench/ecdh-ecdsa-comp.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
const crypto = require('crypto')
const mycrypto = require('../crypto')
const curveName = require('../crypto').params.curveName; //get the default named curve

const NS_PER_SEC = 1e9;
const iterations = 1000

let message = crypto.pseudoRandomBytes(32)

let aliceECDH = crypto.createECDH(curveName)
let aliceECDH = crypto.createECDH(mycrypto.params.curveName)
aliceECDH.generateKeys()
let aliceECDHPrivateKey = aliceECDH.getPrivateKey()
let aliceECSigningKeyPair = crypto.generateKeyPairSync(
Expand All @@ -17,7 +16,7 @@ let aliceECSigningKeyPair = crypto.generateKeyPairSync(
}
)
// Generate Bob's ECDH key pair (message receiver)
let bobECDH = crypto.createECDH(curveName)
let bobECDH = crypto.createECDH(mycrypto.params.curveName)
let bobECDHPublicKey = bobECDH.generateKeys();

var startTime = process.hrtime();
Expand Down
1 change: 0 additions & 1 deletion crypto/digitalsig.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function computeDigitalSignature(privateECSigningKey, buffer) {
}

function verifyDigitalSignature(publicECVerificationKey, signature, buffer) {
let encodingFormat = require('./index').encodingFormat;
let verifyObject = crypto.createVerify(config.signAlgoName)
verifyObject.update(buffer)
verifyObject.end()
Expand Down
2 changes: 1 addition & 1 deletion crypto/private_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

module.exports = {
macAlgoName: 'sha256',
macKeySize: 16,
hashFunctionName: 'sha256',
hashSize: 32,
macKeySize: 16,
signAlgoName: 'sha256',
symmetricCipherName: 'aes-128-cbc',
symmetricCipherKeySize: 16,
Expand Down
12 changes: 7 additions & 5 deletions ecies-doa-ds/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,20 @@ module.exports.decrypt = function (receiverECDHPrivateKey, encEnvelope) {
const tag = Buffer.from(encEnvelope.tag, mycrypto.encodingFormat)
const iv = Buffer.from(encEnvelope.iv, mycrypto.encodingFormat)

if(!mycrypto.KMAC.verifyKMAC(tag,
if (!mycrypto.KMAC.verifyKMAC(tag,
macKey,
Buffer.concat([ciphertext, iv], ciphertext.length + iv.length))) {
Buffer.concat([ciphertext, iv],
ciphertext.length + iv.length))
) {
throw new Error("Bad MAC")
}

let wrappedMessageObject = JSON.parse(mycrypto.symmetricDecrypt(symmetricEncryptionKey, ciphertext, iv).toString())
checkWrappedMessageMandatoryProperties(wrappedMessageObject)
const senderECSigVerPublicKey = crypto.createPublicKey({
key: wrappedMessageObject.from_ecsig,
format: 'pem',
type: 'spki'
key: wrappedMessageObject.from_ecsig,
format: 'pem',
type: 'spki'
})

if (!mycrypto.verifyDigitalSignature(senderECSigVerPublicKey,
Expand Down
34 changes: 4 additions & 30 deletions ecies-doa-ds/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,12 @@ function senderMessageWrapAndSerialization(senderECSigVerPublicKey, message, sig
});
}

function checkECSigningKeyPairTypeInput(senderECSigningKeyPair) {
if (typeof senderECSigningKeyPair.publicKey === undefined) {
throw new Error("Mandatory property publicKey is missing from input EC signing key pair object");
}
if (typeof senderECSigningKeyPair.publicKey.type === undefined ||
senderECSigningKeyPair.publicKey.type !== 'public') {
throw new Error("Public key is not of type public")
}
if (typeof senderECSigningKeyPair.publicKey.asymmetricKeyType === undefined ||
senderECSigningKeyPair.publicKey.asymmetricKeyType !== 'ec') {
throw new Error("Invalid asymmetric type for EC public key")
}
if (typeof senderECSigningKeyPair.privateKey === undefined) {
throw new Error("Mandatory property privateKey is missing from input EC signing key pair object");
}
if (typeof senderECSigningKeyPair.privateKey.type === undefined ||
senderECSigningKeyPair.privateKey.type !== 'private') {
throw new Error("Private key is not of type public")
}
if (typeof senderECSigningKeyPair.privateKey.asymmetricKeyType === undefined ||
senderECSigningKeyPair.publicKey.asymmetricKeyType !== 'ec') {
throw new Error("Invalid asymmetric type for EC private key")
}


}

module.exports.encrypt = function (senderECSigningKeyPair, receiverECDHPublicKey, message) {

if (!Buffer.isBuffer(message)) {
throw new Error('Input message has to be of type Buffer')
}

checkECSigningKeyPairTypeInput(senderECSigningKeyPair)

const ephemeralKeyAgreement = new mycrypto.ECEphemeralKeyAgreement()
const ephemeralPublicKey = ephemeralKeyAgreement.generateEphemeralPublicKey()
const sharedSecret = ephemeralKeyAgreement.generateSharedSecretForPublicKey(receiverECDHPublicKey)
Expand All @@ -61,7 +32,10 @@ module.exports.encrypt = function (senderECSigningKeyPair, receiverECDHPublicKey

const iv = mycrypto.getRandomBytes(mycrypto.params.ivSize)
const ciphertext = mycrypto.symmetricEncrypt(symmetricEncryptionKey, senderAuthMsgEnvelopeSerialized, iv)
const tag = mycrypto.KMAC.computeKMAC(macKey, Buffer.concat([ciphertext, iv], ciphertext.length + iv.length))
const tag = mycrypto.KMAC.computeKMAC(macKey,
Buffer.concat([ciphertext, iv],
ciphertext.length + iv.length)
)

return common.createEncryptedEnvelopeObject(receiverECDHPublicKey, ephemeralPublicKey, ciphertext, iv, tag)
};
8 changes: 7 additions & 1 deletion ecies/decrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ module.exports.decrypt = function (receiverECDHPrivateKey, encEnvelope) {
const tag = Buffer.from(encEnvelope.tag, mycrypto.encodingFormat)
const iv = Buffer.from(encEnvelope.iv, mycrypto.encodingFormat)

mycrypto.KMAC.verifyKMAC(tag, macKey, Buffer.concat([ciphertext, iv], ciphertext.length + iv.length))
if (!mycrypto.KMAC.verifyKMAC(tag,
macKey,
Buffer.concat([ciphertext, iv],
ciphertext.length + iv.length))
) {
throw new Error("Bad MAC")
}

return mycrypto.symmetricDecrypt(symmetricEncryptionKey, ciphertext, iv)
}
9 changes: 6 additions & 3 deletions ecies/encrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const mycrypto = require('../crypto')
const common = require('../common')

module.exports.encrypt = function(receiverECDHPublicKey, message) {
module.exports.encrypt = function (receiverECDHPublicKey, message) {

if (!Buffer.isBuffer(message)) {
throw new Error('Input message has to be of type Buffer')
Expand All @@ -18,7 +18,10 @@ module.exports.encrypt = function(receiverECDHPublicKey, message) {

const iv = mycrypto.getRandomBytes(mycrypto.params.ivSize)
const ciphertext = mycrypto.symmetricEncrypt(symmetricEncryptionKey, message, iv)
const tag = mycrypto.KMAC.computeKMAC(macKey, Buffer.concat([ciphertext, iv], ciphertext.length + iv.length))

const tag = mycrypto.KMAC.computeKMAC(macKey,
Buffer.concat([ciphertext, iv],
ciphertext.length + iv.length)
)

return common.createEncryptedEnvelopeObject(receiverECDHPublicKey, ephemeralPublicKey, ciphertext, iv, tag)
}
6 changes: 4 additions & 2 deletions example-ecies-doa-ds.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ let encEnvelope = ecies.encrypt(aliceECSigningKeyPair, bobECDHPublicKey, plainTe
console.log("Encrypted Envelope:")
console.log(encEnvelope)

// ... Message is somehow transmitted to Bob
// Bob receives the message
// ... The encrypted envelope is somehow transmitted to Bob
// Bob receives the encrypted envelope
// Bob decodes the ECDH public key for which this encrypted envelope is intended for
let myECDHPublicKey = ecies.getDecodedECDHPublicKeyFromEncEnvelope(encEnvelope)
// ... Bob searches his key database for the corresponding ECDH private key
// ... We assume here that Bob finds it
assert(Buffer.compare(myECDHPublicKey, bobECDHPublicKey) === 0, "PUBLIC KEYS ARE NOT EQUAL")
// Bob calls the decryption function and gets back an object.
let decEnvelope = ecies.decrypt(bobECDHPrivateKey, encEnvelope)
Expand Down
8 changes: 5 additions & 3 deletions example-ecies.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ let encEnvelope = ecies.encrypt(bobECDHPublicKey, plainTextMessage)
console.log("Encrypted Envelope:")
console.log(encEnvelope)

// ... Message is somehow transmitted to Bob
// Bob receives the message
// ... The encrypted envelope is somehow transmitted to Bob
// Bob receives the encrypted envelope
// Bob decodes the ECDH public key for which this encrypted envelope is intended for
let myECDHPublicKey = ecies.getDecodedECDHPublicKeyFromEncEnvelope(encEnvelope)
// ... Bob searches his key database for the corresponding ECDH private key
// ... We assume here that Bob finds it
assert(Buffer.compare(myECDHPublicKey, bobECDHPublicKey) === 0, "PUBLIC KEYS ARE NOT EQUAL")
// Bob calls the decryption function and gets back an object.
// Bob calls the decryption function and gets back the message
let decMessage = ecies.decrypt(bobECDHPrivateKey, encEnvelope)
assert(Buffer.compare(decMessage, plainTextMessage) === 0, "MESSAGES ARE NOT EQUAL")
// Here is the decrypted message!
Expand Down
3 changes: 1 addition & 2 deletions test/kdf2.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const eciesds = require('../eciesds')
const crypto = require('crypto')
const kdf2 = require('../crypto').KDF

function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}

kdf2 = eciesds.config.evaluateKDF
const maxInputSize = 8
const maxOutputSize = 300
const testIterations = 10000
Expand Down

0 comments on commit ee8b7d9

Please sign in to comment.