Skip to content
This repository was archived by the owner on Apr 22, 2023. It is now read-only.
This repository was archived by the owner on Apr 22, 2023. It is now read-only.

crypto.diffieHellman padding issue #5239

@Jimmy-Z

Description

@Jimmy-Z

here is a special case I encountered:

var df = require('crypto').createDiffieHellman('90f8cb2b', 'hex');
df.setPrivateKey('4e953092', 'hex');
df.setPublicKey('479fd00c', 'hex'); // comment below
console.log(df.computeSecret('8e366f02', 'hex', 'hex'));

the output is:

694f8500

the correct secret should be 00694f85, which can be verified by a small python script:

class DiffieHellman(object):
    def __init__(self, p, g):
        self.p = p
        self.g = g

    def generateKeys(self, a):
        self.a = a
        return pow(self.g, self.a, self.p)

    def computeSecret(self, B):
        return pow(B, self.a, self.p)

df = DiffieHellman(0x90f8cb2b, 2)
print '%08x' % df.generateKeys(0x4e953092)
print '%08x' % df.computeSecret(0x8e366f02)

the output is:

479fd00c
00694f85

And another verification snippet using jsbn has the result 00694f85 too, the code is short but jsbn needs a little work to be happy with node.js so it's not pasted here.

It looks like a padding issue to me.

Using setPublicKey after setPrivateKey looks less convincing but sadly setPrivateKey won't generate the corresponding public key and generateKeys does not support specifying a private key so using generateKeys will need a loop to get such a special case and make the code longer (and less obvious).

Please do not discuss about "one shall not use Diffie-Hellman on his own" like topics here, I know that :(

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions