Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
crypto: make crypto.sign() support siphash
  • Loading branch information
bnoordhuis committed Mar 24, 2020
commit 60307fc4353205eeeb320a416e10b75d156155a7
7 changes: 5 additions & 2 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {

const {
EVP_PKEY_POLY1305,
EVP_PKEY_SIPHASH,
KeyObject: KeyObjectHandle,
kKeyTypeSecret,
kKeyTypePublic,
Expand Down Expand Up @@ -330,16 +331,18 @@ function createPublicKey(key) {
}

function createPrivateKey(key) {
if (typeof key === 'object' && key && key.type === 'poly1305') {
if (typeof key === 'object' && key &&
(key.type === 'poly1305' || key.type === 'siphash')) {
const secret = key.key;

if (!isArrayBufferView(secret)) {
const allowed = ['Buffer', 'TypedArray', 'DataView'];
throw new ERR_INVALID_ARG_TYPE('key', allowed, secret);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new ERR_INVALID_ARG_TYPE('key', allowed, secret);
throw new ERR_INVALID_ARG_TYPE('key.key', allowed, secret);

??

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially wrote something like what you suggest but then I noticed similar code a few lines up and decided to go with that for consistency.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sigh. Yeah, there's quite a bit of inconsistency in the code base for this case. ERR_INVALID_ARG_TYPE uses different logic for generating the message string for when reporting directly on the argument or a property on the argument. In this case, since it is key.key that is being tested, key.key should be used in the error message and the other similar cases likely should be fixed.

}

const type = key.type === 'poly1305' ? EVP_PKEY_POLY1305 : EVP_PKEY_SIPHASH;
const handle = new KeyObjectHandle(kKeyTypePrivate);
handle.initRaw(EVP_PKEY_POLY1305, secret);
handle.initRaw(type, secret);
return new PrivateKeyObject(handle);
}

Expand Down
1 change: 1 addition & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6915,6 +6915,7 @@ void Initialize(Local<Object> target,
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ED25519);
NODE_DEFINE_CONSTANT(target, EVP_PKEY_ED448);
NODE_DEFINE_CONSTANT(target, EVP_PKEY_POLY1305);
NODE_DEFINE_CONSTANT(target, EVP_PKEY_SIPHASH);
NODE_DEFINE_CONSTANT(target, EVP_PKEY_X25519);
NODE_DEFINE_CONSTANT(target, EVP_PKEY_X448);
NODE_DEFINE_CONSTANT(target, OPENSSL_EC_NAMED_CURVE);
Expand Down
10 changes: 10 additions & 0 deletions test/parallel/test-crypto-sign-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -655,3 +655,13 @@ assert.throws(
const actual = crypto.sign(null, data, privkey);
assert.deepStrictEqual(actual, expected);
}

// siphash
{
const key = Buffer.from('000102030405060708090A0B0C0D0E0F', 'hex');
const data = Buffer.from('000102030405', 'hex');
const privkey = crypto.createPrivateKey({ type: 'siphash', key });
const expected = Buffer.from('14eeca338b208613485ea0308fd7a15e', 'hex');
const actual = crypto.sign(null, data, privkey);
assert.deepStrictEqual(actual, expected);
}