Skip to content

Commit cc0ffc5

Browse files
mscdexBridgeAR
authored andcommitted
crypto: fix EdDSA support for KeyObject
PR-URL: #26319 Fixes: #26316 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Sam Roberts <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Ujjwal Sharma <[email protected]>
1 parent f128008 commit cc0ffc5

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
@@ -1124,11 +1124,16 @@ passing keys as strings or `Buffer`s due to improved security features.
11241124
### keyObject.asymmetricKeyType
11251125
<!-- YAML
11261126
added: v11.6.0
1127+
changes:
1128+
- version: REPLACEME
1129+
pr-url: https://github.com/nodejs/node/pull/26319
1130+
description: Added support for `'ed25519'` and `'ed448'`
11271131
-->
11281132
* {string}
11291133

11301134
For asymmetric keys, this property represents the type of the embedded key
1131-
(`'rsa'`, `'dsa'` or `'ec'`). This property is `undefined` for symmetric keys.
1135+
(`'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, or `'ed448'`).
1136+
This property is `undefined` for symmetric keys.
11321137

11331138
### keyObject.export([options])
11341139
<!-- YAML

src/env.h

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

src/node_crypto.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3434,6 +3434,10 @@ Local<String> KeyObject::GetAsymmetricKeyType() const {
34343434
return env()->crypto_dsa_string();
34353435
case EVP_PKEY_EC:
34363436
return env()->crypto_ec_string();
3437+
case EVP_PKEY_ED25519:
3438+
return env()->crypto_ed25519_string();
3439+
case EVP_PKEY_ED448:
3440+
return env()->crypto_ed448_string();
34373441
default:
34383442
CHECK(false);
34393443
}
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
@@ -123,3 +123,34 @@ const privatePem = fixtures.readSync('test_rsa_privkey.pem', 'ascii');
123123
createPrivateKey({ key: '' });
124124
}, /null/);
125125
}
126+
127+
[
128+
{ private: fixtures.readSync('test_ed25519_privkey.pem', 'ascii'),
129+
public: fixtures.readSync('test_ed25519_pubkey.pem', 'ascii'),
130+
keyType: 'ed25519' },
131+
{ private: fixtures.readSync('test_ed448_privkey.pem', 'ascii'),
132+
public: fixtures.readSync('test_ed448_pubkey.pem', 'ascii'),
133+
keyType: 'ed448' }
134+
].forEach((info) => {
135+
const keyType = info.keyType;
136+
137+
{
138+
const exportOptions = { type: 'pkcs8', format: 'pem' };
139+
const key = createPrivateKey(info.private);
140+
assert.strictEqual(key.type, 'private');
141+
assert.strictEqual(key.asymmetricKeyType, keyType);
142+
assert.strictEqual(key.symmetricKeySize, undefined);
143+
assert.strictEqual(key.export(exportOptions), info.private);
144+
}
145+
146+
{
147+
const exportOptions = { type: 'spki', format: 'pem' };
148+
[info.private, info.public].forEach((pem) => {
149+
const key = createPublicKey(pem);
150+
assert.strictEqual(key.type, 'public');
151+
assert.strictEqual(key.asymmetricKeyType, keyType);
152+
assert.strictEqual(key.symmetricKeySize, undefined);
153+
assert.strictEqual(key.export(exportOptions), info.public);
154+
});
155+
}
156+
});

0 commit comments

Comments
 (0)