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
13 changes: 12 additions & 1 deletion lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,18 @@ function createPublicKey(key) {
return new PublicKeyObject(handle);
}

function createPrivateKey(key) {
function createPrivateKey(key, secret = null) {
if (key === 'poly1305') {
if (!isArrayBufferView(secret)) {
throw new ERR_INVALID_ARG_TYPE(
'secret',
['Buffer', 'TypedArray', 'DataView'],
secret);
}
const handle = new KeyObjectHandle(kKeyTypePrivate);
handle.initRaw(/* NID_poly1305 */ 1061, secret);
Comment thread
bnoordhuis marked this conversation as resolved.
Resolved
return new PrivateKeyObject(handle);
}
Comment thread
bnoordhuis marked this conversation as resolved.
Resolved
const { format, type, data, passphrase } =
prepareAsymmetricKey(key, kCreatePrivate);
const handle = new KeyObjectHandle(kKeyTypePrivate);
Expand Down
19 changes: 19 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3242,6 +3242,7 @@ Local<Function> KeyObject::Initialize(Environment* env, Local<Object> target) {
KeyObject::kInternalFieldCount);

env->SetProtoMethod(t, "init", Init);
env->SetProtoMethod(t, "initRaw", InitRaw);
env->SetProtoMethodNoSideEffect(t, "getSymmetricKeySize",
GetSymmetricKeySize);
env->SetProtoMethodNoSideEffect(t, "getAsymmetricKeyType",
Expand Down Expand Up @@ -3350,6 +3351,24 @@ void KeyObject::Init(const FunctionCallbackInfo<Value>& args) {
}
}

void KeyObject::InitRaw(const FunctionCallbackInfo<Value>& args) {
KeyObject* key;
ASSIGN_OR_RETURN_UNWRAP(&key, args.Holder());
MarkPopErrorOnReturn mark_pop_error_on_return;
CHECK(args[0]->IsInt32());
CHECK(args[1]->IsArrayBufferView());
const int32_t nid = args[0].As<Int32>()->Value();
Local<ArrayBufferView> abv = args[1].As<ArrayBufferView>();
const size_t raw_pkey_len = abv->ByteLength();
unsigned char* raw_pkey = MallocOpenSSL<unsigned char>(raw_pkey_len);
abv->CopyContents(raw_pkey, raw_pkey_len);
EVPKeyPointer pkey_ptr(
EVP_PKEY_new_raw_private_key(nid, nullptr, raw_pkey, raw_pkey_len));
OPENSSL_clear_free(raw_pkey, raw_pkey_len);
ManagedEVPPKey pkey(std::move(pkey_ptr));
key->InitPrivate(pkey);
}

void KeyObject::InitSecret(v8::Local<v8::ArrayBufferView> abv) {
CHECK_EQ(this->key_type_, kKeyTypeSecret);

Expand Down
1 change: 1 addition & 0 deletions src/node_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ class KeyObject : public BaseObject {
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);

static void Init(const v8::FunctionCallbackInfo<v8::Value>& args);
static void InitRaw(const v8::FunctionCallbackInfo<v8::Value>& args);
void InitSecret(v8::Local<v8::ArrayBufferView> abv);
void InitPublic(const ManagedEVPPKey& pkey);
void InitPrivate(const ManagedEVPPKey& pkey);
Expand Down
20 changes: 20 additions & 0 deletions test/parallel/test-crypto-sign-verify.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,3 +635,23 @@ assert.throws(
assert(stdout.includes('Verified OK'));
}));
}

// poly1305
{
const key =
Buffer.from(
'1c9240a5eb55d38af333888604f6b5f0473917c1402b80099dca5cbc207075c0',
'hex');
const data =
Buffer.from(
'2754776173206272696c6c69672c20616e642074686520736c6974687920' +
'746f7665730a446964206779726520616e642067696d626c6520696e2074' +
'686520776162653a0a416c6c206d696d737920776572652074686520626f' +
'726f676f7665732c0a416e6420746865206d6f6d65207261746873206f75' +
'7467726162652e',
'hex');
const expected = Buffer.from('4541669a7eaaee61e708dc7cbcc5eb62', 'hex');
const actual =
crypto.sign(null, data, crypto.createPrivateKey('poly1305', key));
assert.deepStrictEqual(actual, expected);
}