Skip to content

Commit bf1e4f2

Browse files
committed
Split formating of a ed25519 dict from key generation.
1 parent 7252e60 commit bf1e4f2

File tree

1 file changed

+43
-12
lines changed

1 file changed

+43
-12
lines changed

securesystemslib/keys.py

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -313,32 +313,63 @@ def generate_ed25519_key(scheme="ed25519"):
313313
Conforms to 'securesystemslib.formats.ED25519KEY_SCHEMA'.
314314
"""
315315

316+
# Generate the public and private Ed25519 key with the 'pynacl' library.
317+
# Unlike in the verification of Ed25519 signatures, do not fall back to the
318+
# optimized, pure python implementation provided by PyCA. Ed25519 should
319+
# always be generated with a backend like libsodium to prevent side-channel
320+
# attacks.
321+
public, private = ed25519_keys.generate_public_and_private()
322+
323+
return format_ed25519_dict(public, private, scheme=scheme)
324+
325+
326+
def format_ed25519_dict(public: bytes, private: bytes, scheme="ed25519"):
327+
"""
328+
<Purpose>
329+
Formats a ed25519 private key dict.
330+
331+
<Arguments>
332+
public:
333+
Bytes of public key.
334+
335+
private:
336+
Bytes of private key.
337+
338+
scheme:
339+
The signature scheme used by the generated Ed25519 key.
340+
341+
<Exceptions>
342+
None.
343+
344+
<Side Effects>
345+
None.
346+
347+
<Returns>
348+
A dictionary containing the ED25519 keys and other identifying information.
349+
Conforms to 'securesystemslib.formats.ED25519KEY_SCHEMA'.
350+
"""
351+
352+
assert private is None or len(private) == 32
353+
assert len(public) == 32
354+
316355
# Are the arguments properly formatted? If not, raise an
317356
# 'securesystemslib.exceptions.FormatError' exceptions.
318357
formats.ED25519_SIG_SCHEMA.check_match(scheme)
319358

320359
# Begin building the Ed25519 key dictionary.
321360
ed25519_key = {}
322361
keytype = "ed25519"
323-
public = None
324-
private = None
325-
326-
# Generate the public and private Ed25519 key with the 'pynacl' library.
327-
# Unlike in the verification of Ed25519 signatures, do not fall back to the
328-
# optimized, pure python implementation provided by PyCA. Ed25519 should
329-
# always be generated with a backend like libsodium to prevent side-channel
330-
# attacks.
331-
public, private = ed25519_keys.generate_public_and_private()
332362

333363
# Generate the keyid of the Ed25519 key. 'key_value' corresponds to the
334364
# 'keyval' entry of the 'Ed25519KEY_SCHEMA' dictionary. The private key
335365
# information is not included in the generation of the 'keyid' identifier.
336366
key_value = {"public": binascii.hexlify(public).decode(), "private": ""}
337367
keyid = _get_keyid(keytype, scheme, key_value)
338368

339-
# Build the 'ed25519_key' dictionary. Update 'key_value' with the Ed25519
340-
# private key prior to adding 'key_value' to 'ed25519_key'.
341-
key_value["private"] = binascii.hexlify(private).decode()
369+
if private is not None:
370+
# Build the 'ed25519_key' dictionary. Update 'key_value' with the Ed25519
371+
# private key prior to adding 'key_value' to 'ed25519_key'.
372+
key_value["private"] = binascii.hexlify(private).decode()
342373

343374
ed25519_key["keytype"] = keytype
344375
ed25519_key["scheme"] = scheme

0 commit comments

Comments
 (0)