@@ -314,32 +314,63 @@ def generate_ed25519_key(scheme="ed25519"):
314314 Conforms to 'securesystemslib.formats.ED25519KEY_SCHEMA'.
315315 """
316316
317+ # Generate the public and private Ed25519 key with the 'pynacl' library.
318+ # Unlike in the verification of Ed25519 signatures, do not fall back to the
319+ # optimized, pure python implementation provided by PyCA. Ed25519 should
320+ # always be generated with a backend like libsodium to prevent side-channel
321+ # attacks.
322+ public , private = ed25519_keys .generate_public_and_private ()
323+
324+ return format_ed25519_dict (public , private , scheme = scheme )
325+
326+
327+ def format_ed25519_dict (public : bytes , private : bytes , scheme = "ed25519" ):
328+ """
329+ <Purpose>
330+ Formats a ed25519 private key dict.
331+
332+ <Arguments>
333+ public:
334+ Bytes of public key.
335+
336+ private:
337+ Bytes of private key.
338+
339+ scheme:
340+ The signature scheme used by the generated Ed25519 key.
341+
342+ <Exceptions>
343+ None.
344+
345+ <Side Effects>
346+ None.
347+
348+ <Returns>
349+ A dictionary containing the ED25519 keys and other identifying information.
350+ Conforms to 'securesystemslib.formats.ED25519KEY_SCHEMA'.
351+ """
352+
353+ assert private is None or len (private ) == 32 # nosec assert_used
354+ assert len (public ) == 32 # nosec assert_used
355+
317356 # Are the arguments properly formatted? If not, raise an
318357 # 'securesystemslib.exceptions.FormatError' exceptions.
319358 formats .ED25519_SIG_SCHEMA .check_match (scheme )
320359
321360 # Begin building the Ed25519 key dictionary.
322361 ed25519_key = {}
323362 keytype = "ed25519"
324- public = None
325- private = None
326-
327- # Generate the public and private Ed25519 key with the 'pynacl' library.
328- # Unlike in the verification of Ed25519 signatures, do not fall back to the
329- # optimized, pure python implementation provided by PyCA. Ed25519 should
330- # always be generated with a backend like libsodium to prevent side-channel
331- # attacks.
332- public , private = ed25519_keys .generate_public_and_private ()
333363
334364 # Generate the keyid of the Ed25519 key. 'key_value' corresponds to the
335365 # 'keyval' entry of the 'Ed25519KEY_SCHEMA' dictionary. The private key
336366 # information is not included in the generation of the 'keyid' identifier.
337367 key_value = {"public" : binascii .hexlify (public ).decode (), "private" : "" }
338368 keyid = _get_keyid (keytype , scheme , key_value )
339369
340- # Build the 'ed25519_key' dictionary. Update 'key_value' with the Ed25519
341- # private key prior to adding 'key_value' to 'ed25519_key'.
342- key_value ["private" ] = binascii .hexlify (private ).decode ()
370+ if private is not None :
371+ # Build the 'ed25519_key' dictionary. Update 'key_value' with the Ed25519
372+ # private key prior to adding 'key_value' to 'ed25519_key'.
373+ key_value ["private" ] = binascii .hexlify (private ).decode ()
343374
344375 ed25519_key ["keytype" ] = keytype
345376 ed25519_key ["scheme" ] = scheme
0 commit comments