Skip to content

Commit 62d0c93

Browse files
committed
Drop legacy signature creation code (DO NOT MERGE!)
Signed-off-by: Lukas Puehringer <[email protected]>
1 parent 052b304 commit 62d0c93

9 files changed

+0
-1233
lines changed

securesystemslib/ecdsa_keys.py

Lines changed: 0 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -165,101 +165,6 @@ def generate_public_and_private(scheme="ecdsa-sha2-nistp256"):
165165
return public_pem.decode("utf-8"), private_pem.decode("utf-8")
166166

167167

168-
def create_signature(
169-
public_key, private_key, data, scheme="ecdsa-sha2-nistp256"
170-
):
171-
"""
172-
<Purpose>
173-
Return a (signature, scheme) tuple.
174-
175-
>>> requested_scheme = 'ecdsa-sha2-nistp256'
176-
>>> public, private = generate_public_and_private(requested_scheme)
177-
>>> data = b'The quick brown fox jumps over the lazy dog'
178-
>>> signature, scheme = create_signature(public, private, data, requested_scheme)
179-
>>> securesystemslib.formats.ECDSASIGNATURE_SCHEMA.matches(signature)
180-
True
181-
>>> requested_scheme == scheme
182-
True
183-
184-
<Arguments>
185-
public:
186-
The ECDSA public key in PEM format.
187-
188-
private:
189-
The ECDSA private key in PEM format.
190-
191-
data:
192-
Byte data used by create_signature() to generate the signature returned.
193-
194-
scheme:
195-
The signature scheme used to generate the signature. For example:
196-
'ecdsa-sha2-nistp256'.
197-
198-
<Exceptions>
199-
securesystemslib.exceptions.FormatError, if the arguments are improperly
200-
formatted.
201-
202-
securesystemslib.exceptions.CryptoError, if a signature cannot be created.
203-
204-
securesystemslib.exceptions.UnsupportedAlgorithmError, if 'scheme' is not
205-
one of the supported signature schemes.
206-
207-
securesystemslib.exceptions.UnsupportedLibraryError, if the cryptography
208-
module is not available.
209-
210-
<Side Effects>
211-
None.
212-
213-
<Returns>
214-
A signature dictionary conformat to
215-
'securesystemslib.format.SIGNATURE_SCHEMA'. ECDSA signatures are XX bytes,
216-
however, the hexlified signature is stored in the dictionary returned.
217-
"""
218-
219-
if not CRYPTO: # pragma: no cover
220-
raise exceptions.UnsupportedLibraryError(NO_CRYPTO_MSG)
221-
222-
# Do 'public_key' and 'private_key' have the correct format?
223-
# This check will ensure that the arguments conform to
224-
# 'securesystemslib.formats.PEMECDSA_SCHEMA'. Raise
225-
# 'securesystemslib.exceptions.FormatError' if the check fails.
226-
formats.PEMECDSA_SCHEMA.check_match(public_key)
227-
228-
# Is 'private_key' properly formatted?
229-
formats.PEMECDSA_SCHEMA.check_match(private_key)
230-
231-
# Is 'scheme' properly formatted?
232-
formats.ECDSA_SCHEME_SCHEMA.check_match(scheme)
233-
234-
# 'ecdsa-sha2-nistp256' is the only currently supported ECDSA scheme, so this
235-
# if-clause isn't strictly needed. Nevertheless, the conditional statement
236-
# is included to accommodate multiple schemes that can potentially be added
237-
# in the future.
238-
if scheme == "ecdsa-sha2-nistp256":
239-
try:
240-
private_key = load_pem_private_key(
241-
private_key.encode("utf-8"),
242-
password=None,
243-
backend=default_backend(),
244-
)
245-
246-
signature = private_key.sign(data, ec.ECDSA(hashes.SHA256()))
247-
248-
except TypeError as e:
249-
raise exceptions.CryptoError(
250-
"Could not create" " signature: " + str(e)
251-
)
252-
253-
# A defensive check for an invalid 'scheme'. The
254-
# ECDSA_SCHEME_SCHEMA.check_match() above should have already validated it.
255-
else: # pragma: no cover
256-
raise exceptions.UnsupportedAlgorithmError(
257-
"Unsupported" " signature scheme is specified: " + repr(scheme)
258-
)
259-
260-
return signature, scheme
261-
262-
263168
def verify_signature(public_key, scheme, signature, data):
264169
"""
265170
<Purpose>

securesystemslib/ed25519_keys.py

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -144,110 +144,6 @@ def generate_public_and_private():
144144
return public, seed
145145

146146

147-
def create_signature(public_key, private_key, data, scheme):
148-
"""
149-
<Purpose>
150-
Return a (signature, scheme) tuple, where the signature scheme is 'ed25519'
151-
and is always generated by PyNaCl (i.e., 'nacl'). The signature returned
152-
conforms to 'securesystemslib.formats.ED25519SIGNATURE_SCHEMA', and has the
153-
form:
154-
155-
'\xae\xd7\x9f\xaf\x95{bP\x9e\xa8YO Z\x86\x9d...'
156-
157-
A signature is a 64-byte string.
158-
159-
>>> public, private = generate_public_and_private()
160-
>>> data = b'The quick brown fox jumps over the lazy dog'
161-
>>> scheme = 'ed25519'
162-
>>> signature, scheme = \
163-
create_signature(public, private, data, scheme)
164-
>>> securesystemslib.formats.ED25519SIGNATURE_SCHEMA.matches(signature)
165-
True
166-
>>> scheme == 'ed25519'
167-
True
168-
>>> signature, scheme = \
169-
create_signature(public, private, data, scheme)
170-
>>> securesystemslib.formats.ED25519SIGNATURE_SCHEMA.matches(signature)
171-
True
172-
>>> scheme == 'ed25519'
173-
True
174-
175-
<Arguments>
176-
public:
177-
The ed25519 public key, which is a 32-byte string.
178-
179-
private:
180-
The ed25519 private key, which is a 32-byte string.
181-
182-
data:
183-
Data object used by create_signature() to generate the signature.
184-
185-
scheme:
186-
The signature scheme used to generate the signature.
187-
188-
<Exceptions>
189-
securesystemslib.exceptions.FormatError, if the arguments are improperly
190-
formatted.
191-
192-
securesystemslib.exceptions.CryptoError, if a signature cannot be created.
193-
194-
securesystemslib.exceptions.UnsupportedLibraryError, if the PyNaCl ('nacl')
195-
module is unavailable.
196-
197-
<Side Effects>
198-
nacl.signing.SigningKey.sign() called to generate the actual signature.
199-
200-
<Returns>
201-
A signature dictionary conformat to
202-
'securesystemslib.format.SIGNATURE_SCHEMA'. ed25519 signatures are 64
203-
bytes, however, the hexlified signature is stored in the dictionary
204-
returned.
205-
"""
206-
207-
if not NACL: # pragma: no cover
208-
raise exceptions.UnsupportedLibraryError(NO_NACL_MSG)
209-
210-
# Does 'public_key' have the correct format?
211-
# This check will ensure 'public_key' conforms to
212-
# 'securesystemslib.formats.ED25519PUBLIC_SCHEMA', which must have length 32
213-
# bytes. Raise 'securesystemslib.exceptions.FormatError' if the check fails.
214-
formats.ED25519PUBLIC_SCHEMA.check_match(public_key)
215-
216-
# Is 'private_key' properly formatted?
217-
formats.ED25519SEED_SCHEMA.check_match(private_key)
218-
219-
# Is 'scheme' properly formatted?
220-
formats.ED25519_SIG_SCHEMA.check_match(scheme)
221-
222-
# Signing the 'data' object requires a seed and public key.
223-
# nacl.signing.SigningKey.sign() generates the signature.
224-
signature = None
225-
226-
# An if-clause is not strictly needed here, since 'ed25519' is the only
227-
# currently supported scheme. Nevertheless, include the conditional
228-
# statement to accommodate schemes that might be added in the future.
229-
if scheme == "ed25519":
230-
try:
231-
nacl_key = SigningKey(private_key)
232-
nacl_sig = nacl_key.sign(data)
233-
signature = nacl_sig.signature
234-
235-
except (ValueError, TypeError, nacl_exceptions.CryptoError) as e:
236-
raise exceptions.CryptoError(
237-
'An "ed25519" signature'
238-
" could not be created with PyNaCl." + str(e)
239-
)
240-
241-
# This is a defensive check for a valid 'scheme', which should have already
242-
# been validated in the check_match() above.
243-
else: # pragma: no cover
244-
raise exceptions.UnsupportedAlgorithmError(
245-
"Unsupported" " signature scheme is specified: " + repr(scheme)
246-
)
247-
248-
return signature, scheme
249-
250-
251147
def verify_signature(public_key, scheme, signature, data):
252148
"""
253149
<Purpose>

securesystemslib/keys.py

Lines changed: 0 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -565,142 +565,6 @@ def _get_keyid(keytype, scheme, key_value, hash_algorithm="sha256"):
565565
return keyid
566566

567567

568-
def create_signature(key_dict, data):
569-
"""
570-
<Purpose>
571-
Return a signature dictionary of the form:
572-
{'keyid': 'f30a0870d026980100c0573bd557394f8c1bbd6...',
573-
'sig': '...'}.
574-
575-
The signing process will use the private key in
576-
key_dict['keyval']['private'] and 'data' to generate the signature.
577-
578-
The following signature schemes are supported:
579-
580-
'RSASSA-PSS'
581-
RFC3447 - RSASSA-PSS
582-
http://www.ietf.org/rfc/rfc3447.
583-
584-
'ed25519'
585-
ed25519 - high-speed high security signatures
586-
http://ed25519.cr.yp.to/
587-
588-
Which signature to generate is determined by the key type of 'key_dict'
589-
and the available cryptography library specified in 'settings'.
590-
591-
>>> ed25519_key = generate_ed25519_key()
592-
>>> data = 'The quick brown fox jumps over the lazy dog'
593-
>>> signature = create_signature(ed25519_key, data)
594-
>>> securesystemslib.formats.SIGNATURE_SCHEMA.matches(signature)
595-
True
596-
>>> len(signature['sig'])
597-
128
598-
>>> rsa_key = generate_rsa_key(2048)
599-
>>> signature = create_signature(rsa_key, data)
600-
>>> securesystemslib.formats.SIGNATURE_SCHEMA.matches(signature)
601-
True
602-
>>> ecdsa_key = generate_ecdsa_key()
603-
>>> signature = create_signature(ecdsa_key, data)
604-
>>> securesystemslib.formats.SIGNATURE_SCHEMA.matches(signature)
605-
True
606-
607-
<Arguments>
608-
key_dict:
609-
A dictionary containing the keys. An example RSA key dict has the
610-
form:
611-
612-
{'keytype': 'rsa',
613-
'scheme': 'rsassa-pss-sha256',
614-
'keyid': 'f30a0870d026980100c0573bd557394f8c1bbd6...',
615-
'keyval': {'public': '-----BEGIN RSA PUBLIC KEY----- ...',
616-
'private': '-----BEGIN RSA PRIVATE KEY----- ...'}}
617-
618-
The public and private keys are strings in PEM format.
619-
620-
data:
621-
Data to be signed. This should be a bytes object; data should be
622-
encoded/serialized before it is passed here. The same value can be be
623-
passed into securesystemslib.verify_signature() (along with the public
624-
key) to later verify the signature.
625-
626-
<Exceptions>
627-
securesystemslib.exceptions.FormatError, if 'key_dict' is improperly
628-
formatted.
629-
630-
securesystemslib.exceptions.UnsupportedAlgorithmError, if 'key_dict'
631-
specifies an unsupported key type or signing scheme.
632-
633-
securesystemslib.exceptions.CryptoError, if the signature cannot be
634-
generated.
635-
636-
TypeError, if 'key_dict' contains an invalid keytype.
637-
638-
<Side Effects>
639-
The cryptography library specified in 'settings' is called to perform the
640-
actual signing routine.
641-
642-
<Returns>
643-
A signature dictionary conformant to
644-
'securesystemslib_format.SIGNATURE_SCHEMA'.
645-
"""
646-
647-
# Does 'key_dict' have the correct format?
648-
# This check will ensure 'key_dict' has the appropriate number of objects
649-
# and object types, and that all dict keys are properly named.
650-
# Raise 'securesystemslib.exceptions.FormatError' if the check fails.
651-
# The key type of 'key_dict' must be either 'rsa' or 'ed25519'.
652-
formats.ANYKEY_SCHEMA.check_match(key_dict)
653-
654-
# Signing the 'data' object requires a private key. Signing schemes that are
655-
# currently supported are: 'ed25519', 'ecdsa-sha2-nistp256',
656-
# 'ecdsa-sha2-nistp384' and rsa schemes defined in
657-
# `securesystemslib.keys.RSA_SIGNATURE_SCHEMES`.
658-
# RSASSA-PSS and RSA-PKCS1v15 keys and signatures can be generated and
659-
# verified by rsa_keys.py, and Ed25519 keys by PyNaCl and PyCA's
660-
# optimized, pure python implementation of Ed25519.
661-
signature = {}
662-
keytype = key_dict["keytype"]
663-
scheme = key_dict["scheme"]
664-
public = key_dict["keyval"]["public"]
665-
private = key_dict["keyval"]["private"]
666-
keyid = key_dict["keyid"]
667-
sig = None
668-
669-
if keytype == "rsa":
670-
if scheme in RSA_SIGNATURE_SCHEMES:
671-
private = private.replace("\r\n", "\n")
672-
sig, scheme = rsa_keys.create_rsa_signature(private, data, scheme)
673-
674-
else:
675-
raise exceptions.UnsupportedAlgorithmError(
676-
"Unsupported" " RSA signature scheme specified: " + repr(scheme)
677-
)
678-
679-
elif keytype == "ed25519":
680-
public = binascii.unhexlify(public.encode("utf-8"))
681-
private = binascii.unhexlify(private.encode("utf-8"))
682-
sig, scheme = ed25519_keys.create_signature(
683-
public, private, data, scheme
684-
)
685-
686-
# Continue to support keytypes of ecdsa-sha2-nistp256 and ecdsa-sha2-nistp384
687-
# for backwards compatibility with older securesystemslib releases
688-
elif keytype in ["ecdsa", "ecdsa-sha2-nistp256", "ecdsa-sha2-nistp384"]:
689-
sig, scheme = ecdsa_keys.create_signature(public, private, data, scheme)
690-
691-
# 'securesystemslib.formats.ANYKEY_SCHEMA' should have detected invalid key
692-
# types. This is a defensive check against an invalid key type.
693-
else: # pragma: no cover
694-
raise TypeError("Invalid key type.")
695-
696-
# Build the signature dictionary to be returned.
697-
# The hexadecimal representation of 'sig' is stored in the signature.
698-
signature["keyid"] = keyid
699-
signature["sig"] = binascii.hexlify(sig).decode()
700-
701-
return signature
702-
703-
704568
def verify_signature(
705569
key_dict, signature, data
706570
): # pylint: disable=too-many-branches

0 commit comments

Comments
 (0)