Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 1 | """ |
Fabio Utzig | 67c59fa | 2020-04-02 13:09:15 -0300 | [diff] [blame] | 2 | ED25519 key management |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 3 | """ |
| 4 | |
David Brown | 79c4fcf | 2021-01-26 15:04:05 -0700 | [diff] [blame] | 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 7 | from cryptography.hazmat.backends import default_backend |
| 8 | from cryptography.hazmat.primitives import serialization |
| 9 | from cryptography.hazmat.primitives.asymmetric import ed25519 |
| 10 | |
| 11 | from .general import KeyClass |
| 12 | |
| 13 | |
| 14 | class Ed25519UsageError(Exception): |
| 15 | pass |
| 16 | |
| 17 | |
| 18 | class Ed25519Public(KeyClass): |
| 19 | def __init__(self, key): |
| 20 | self.key = key |
| 21 | |
| 22 | def shortname(self): |
| 23 | return "ed25519" |
| 24 | |
| 25 | def _unsupported(self, name): |
| 26 | raise Ed25519UsageError("Operation {} requires private key".format(name)) |
| 27 | |
| 28 | def _get_public(self): |
| 29 | return self.key |
| 30 | |
| 31 | def get_public_bytes(self): |
| 32 | # The key is embedded into MBUboot in "SubjectPublicKeyInfo" format |
| 33 | return self._get_public().public_bytes( |
| 34 | encoding=serialization.Encoding.DER, |
| 35 | format=serialization.PublicFormat.SubjectPublicKeyInfo) |
| 36 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 37 | def get_private_bytes(self, minimal): |
| 38 | self._unsupported('get_private_bytes') |
| 39 | |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 40 | def export_private(self, path, passwd=None): |
| 41 | self._unsupported('export_private') |
| 42 | |
| 43 | def export_public(self, path): |
| 44 | """Write the public key to the given file.""" |
| 45 | pem = self._get_public().public_bytes( |
| 46 | encoding=serialization.Encoding.PEM, |
| 47 | format=serialization.PublicFormat.SubjectPublicKeyInfo) |
| 48 | with open(path, 'wb') as f: |
| 49 | f.write(pem) |
| 50 | |
| 51 | def sig_type(self): |
| 52 | return "ED25519" |
| 53 | |
| 54 | def sig_tlv(self): |
| 55 | return "ED25519" |
| 56 | |
| 57 | def sig_len(self): |
| 58 | return 64 |
| 59 | |
| 60 | |
| 61 | class Ed25519(Ed25519Public): |
| 62 | """ |
Fabio Utzig | 67c59fa | 2020-04-02 13:09:15 -0300 | [diff] [blame] | 63 | Wrapper around an ED25519 private key. |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 64 | """ |
| 65 | |
| 66 | def __init__(self, key): |
| 67 | """key should be an instance of EllipticCurvePrivateKey""" |
| 68 | self.key = key |
| 69 | |
| 70 | @staticmethod |
| 71 | def generate(): |
| 72 | pk = ed25519.Ed25519PrivateKey.generate() |
| 73 | return Ed25519(pk) |
| 74 | |
| 75 | def _get_public(self): |
| 76 | return self.key.public_key() |
| 77 | |
Ioannis Konstantelias | 78e57c7 | 2019-11-28 16:06:12 +0200 | [diff] [blame] | 78 | def get_private_bytes(self, minimal): |
| 79 | raise Ed25519UsageError("Operation not supported with {} keys".format( |
| 80 | self.shortname())) |
| 81 | |
Fabio Utzig | 8101d1f | 2019-05-09 15:03:22 -0300 | [diff] [blame] | 82 | def export_private(self, path, passwd=None): |
| 83 | """ |
| 84 | Write the private key to the given file, protecting it with the |
| 85 | optional password. |
| 86 | """ |
| 87 | if passwd is None: |
| 88 | enc = serialization.NoEncryption() |
| 89 | else: |
| 90 | enc = serialization.BestAvailableEncryption(passwd) |
| 91 | pem = self.key.private_bytes( |
| 92 | encoding=serialization.Encoding.PEM, |
| 93 | format=serialization.PrivateFormat.PKCS8, |
| 94 | encryption_algorithm=enc) |
| 95 | with open(path, 'wb') as f: |
| 96 | f.write(pem) |
| 97 | |
| 98 | def sign_digest(self, digest): |
| 99 | """Return the actual signature""" |
| 100 | return self.key.sign(data=digest) |
| 101 | |
| 102 | def verify_digest(self, signature, digest): |
| 103 | """Verify that signature is valid for given digest""" |
| 104 | k = self.key |
| 105 | if isinstance(self.key, ed25519.Ed25519PrivateKey): |
| 106 | k = self.key.public_key() |
| 107 | return k.verify(signature=signature, data=digest) |