Skip to content

Commit 247c14c

Browse files
committed
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 4562697 commit 247c14c

8 files changed

+57
-1
lines changed

doc/api/crypto.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1133,11 +1133,16 @@ bytes. This property is `undefined` for symmetric keys.
11331133
### keyObject.asymmetricKeyType
11341134
<!-- YAML
11351135
added: v11.6.0
1136+
changes:
1137+
- version: REPLACEME
1138+
pr-url: https://github.com/nodejs/node/pull/26319
1139+
description: Added support for `'ed25519'` and `'ed448'`
11361140
-->
11371141
* {string}
11381142

11391143
For asymmetric keys, this property represents the type of the embedded key
1140-
(`'rsa'`, `'dsa'` or `'ec'`). This property is `undefined` for symmetric keys.
1144+
(`'rsa'`, `'dsa'`, `'ec'`, `'ed25519'`, or `'ed448'`).
1145+
This property is `undefined` for symmetric keys.
11411146

11421147
### keyObject.export([options])
11431148
<!-- YAML

src/env.h

+2
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

+4
Original file line numberDiff line numberDiff line change
@@ -3468,6 +3468,10 @@ Local<String> KeyObject::GetAsymmetricKeyType() const {
34683468
return env()->crypto_dsa_string();
34693469
case EVP_PKEY_EC:
34703470
return env()->crypto_ec_string();
3471+
case EVP_PKEY_ED25519:
3472+
return env()->crypto_ed25519_string();
3473+
case EVP_PKEY_ED448:
3474+
return env()->crypto_ed448_string();
34713475
default:
34723476
CHECK(false);
34733477
}
+3
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

+3
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

+4
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

+4
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

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

0 commit comments

Comments
 (0)