@@ -313,32 +313,63 @@ def generate_ed25519_key(scheme="ed25519"):
313
313
Conforms to 'securesystemslib.formats.ED25519KEY_SCHEMA'.
314
314
"""
315
315
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
+
316
355
# Are the arguments properly formatted? If not, raise an
317
356
# 'securesystemslib.exceptions.FormatError' exceptions.
318
357
formats .ED25519_SIG_SCHEMA .check_match (scheme )
319
358
320
359
# Begin building the Ed25519 key dictionary.
321
360
ed25519_key = {}
322
361
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 ()
332
362
333
363
# Generate the keyid of the Ed25519 key. 'key_value' corresponds to the
334
364
# 'keyval' entry of the 'Ed25519KEY_SCHEMA' dictionary. The private key
335
365
# information is not included in the generation of the 'keyid' identifier.
336
366
key_value = {"public" : binascii .hexlify (public ).decode (), "private" : "" }
337
367
keyid = _get_keyid (keytype , scheme , key_value )
338
368
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 ()
342
373
343
374
ed25519_key ["keytype" ] = keytype
344
375
ed25519_key ["scheme" ] = scheme
0 commit comments