Skip to content

Commit 25c8145

Browse files
mscdextargos
authored andcommitted
crypto: fix EdDSA support for KeyObject
PR-URL: #26319 Fixes: #26316 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Ujjwal Sharma <usharma1998@gmail.com>
1 parent 04441b5 commit 25c8145

File tree

8 files changed

+57
-1
lines changed

8 files changed

+57
-1
lines changed

doc/api/crypto.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1128,11 +1128,16 @@ passing keys as strings or `Buffer`s due to improved security features.
11281128
### keyObject.asymmetricKeyType
11291129
<!-- YAML
11301130
added: v11.6.0
1131+
changes:
1132+
- version: REPLACEME
1133+
pr-url: https://github.com/nodejs/node/pull/26319
1134+
description: Added support for `'ed25519'` and `'ed448'`
11311135
-->
11321136
* {string}
11331137

11341138
For asymmetric keys, this property represents the type of the embedded key
1135-
(`'rsa'`, `'dsa'` or `'ec'`). This property is `undefined` for symmetric keys.
1139+
(`'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, or `'ed448'`).
1140+
This property is `undefined` for symmetric keys.
11361141

11371142
### keyObject.export([options])
11381143
<!-- YAML

src/env.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
145145
V(constants_string, "constants") \
146146
V(crypto_dsa_string, "dsa") \
147147
V(crypto_ec_string, "ec") \
148+
V(crypto_ed25519_string, "ed25519") \
149+
V(crypto_ed448_string, "ed448") \
148150
V(crypto_rsa_string, "rsa") \
149151
V(cwd_string, "cwd") \
150152
V(data_string, "data") \

src/node_crypto.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3429,6 +3429,10 @@ Local<String> KeyObject::GetAsymmetricKeyType() const {
34293429
return env()->crypto_dsa_string();
34303430
case EVP_PKEY_EC:
34313431
return env()->crypto_ec_string();
3432+
case EVP_PKEY_ED25519:
3433+
return env()->crypto_ed25519_string();
3434+
case EVP_PKEY_ED448:
3435+
return env()->crypto_ed448_string();
34323436
default:
34333437
CHECK(false);
34343438
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MC4CAQAwBQYDK2VwBCIEIHXLsXm1lsq5HtyqJwQyFmpfEluuf0KOqP6DqMgGxxDL
3+
-----END PRIVATE KEY-----

test/fixtures/test_ed25519_pubkey.pem

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MCowBQYDK2VwAyEAEXRYV3v5ucrHVR3mKqyPXxXqU34lASwc7Y7MoOvaqcs=
3+
-----END PUBLIC KEY-----

test/fixtures/test_ed448_privkey.pem

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MEcCAQAwBQYDK2VxBDsEObxytD95dGN3Hxk7kVk+Lig1rGYTRr3YdaHjRog++Sgk
3+
QD7KwKmxroBURtkE2N0JbQ3ctdrpGRB5DQ==
4+
-----END PRIVATE KEY-----

test/fixtures/test_ed448_pubkey.pem

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MEMwBQYDK2VxAzoAIESY3jnpGdB5UVJDCznrv0vmBFIzgSMu+gafsbCX1rFtsJwR
3+
M6XUDQiEY7dk6rmm/Fktyawna5EA
4+
-----END PUBLIC KEY-----

test/parallel/test-crypto-key-objects.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,34 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
167167
createPrivateKey({ key: '' });
168168
}, /null/);
169169
}
170+
171+
[
172+
{ private: fixtures.readSync('test_ed25519_privkey.pem', 'ascii'),
173+
public: fixtures.readSync('test_ed25519_pubkey.pem', 'ascii'),
174+
keyType: 'ed25519' },
175+
{ private: fixtures.readSync('test_ed448_privkey.pem', 'ascii'),
176+
public: fixtures.readSync('test_ed448_pubkey.pem', 'ascii'),
177+
keyType: 'ed448' }
178+
].forEach((info) => {
179+
const keyType = info.keyType;
180+
181+
{
182+
const exportOptions = { type: 'pkcs8', format: 'pem' };
183+
const key = createPrivateKey(info.private);
184+
assert.strictEqual(key.type, 'private');
185+
assert.strictEqual(key.asymmetricKeyType, keyType);
186+
assert.strictEqual(key.symmetricKeySize, undefined);
187+
assert.strictEqual(key.export(exportOptions), info.private);
188+
}
189+
190+
{
191+
const exportOptions = { type: 'spki', format: 'pem' };
192+
[info.private, info.public].forEach((pem) => {
193+
const key = createPublicKey(pem);
194+
assert.strictEqual(key.type, 'public');
195+
assert.strictEqual(key.asymmetricKeyType, keyType);
196+
assert.strictEqual(key.symmetricKeySize, undefined);
197+
assert.strictEqual(key.export(exportOptions), info.public);
198+
});
199+
}
200+
});

0 commit comments

Comments
 (0)