Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 1 | """ |
| 2 | X25519 key management |
| 3 | """ |
| 4 | |
David Brown | 79c4fcf | 2021-01-26 15:04:05 -0700 | [diff] [blame] | 5 | # SPDX-License-Identifier: Apache-2.0 |
| 6 | |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -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 x25519 |
| 10 | |
| 11 | from .general import KeyClass |
| 12 | |
| 13 | |
| 14 | class X25519UsageError(Exception): |
| 15 | pass |
| 16 | |
| 17 | |
| 18 | class X25519Public(KeyClass): |
| 19 | def __init__(self, key): |
| 20 | self.key = key |
| 21 | |
| 22 | def shortname(self): |
| 23 | return "x25519" |
| 24 | |
| 25 | def _unsupported(self, name): |
| 26 | raise X25519UsageError("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 | |
Fabio Utzig | 6f28677 | 2022-09-04 20:03:11 -0300 | [diff] [blame^] | 37 | def get_public_pem(self): |
| 38 | return self._get_public().public_bytes( |
| 39 | encoding=serialization.Encoding.PEM, |
| 40 | format=serialization.PublicFormat.SubjectPublicKeyInfo) |
| 41 | |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 42 | def get_private_bytes(self, minimal): |
| 43 | self._unsupported('get_private_bytes') |
| 44 | |
| 45 | def export_private(self, path, passwd=None): |
| 46 | self._unsupported('export_private') |
| 47 | |
| 48 | def export_public(self, path): |
| 49 | """Write the public key to the given file.""" |
| 50 | pem = self._get_public().public_bytes( |
| 51 | encoding=serialization.Encoding.PEM, |
| 52 | format=serialization.PublicFormat.SubjectPublicKeyInfo) |
| 53 | with open(path, 'wb') as f: |
| 54 | f.write(pem) |
| 55 | |
| 56 | def sig_type(self): |
| 57 | return "X25519" |
| 58 | |
| 59 | def sig_tlv(self): |
| 60 | return "X25519" |
| 61 | |
| 62 | def sig_len(self): |
| 63 | return 32 |
| 64 | |
| 65 | |
| 66 | class X25519(X25519Public): |
| 67 | """ |
| 68 | Wrapper around an X25519 private key. |
| 69 | """ |
| 70 | |
| 71 | def __init__(self, key): |
| 72 | """key should be an instance of EllipticCurvePrivateKey""" |
| 73 | self.key = key |
| 74 | |
| 75 | @staticmethod |
| 76 | def generate(): |
| 77 | pk = x25519.X25519PrivateKey.generate() |
| 78 | return X25519(pk) |
| 79 | |
| 80 | def _get_public(self): |
| 81 | return self.key.public_key() |
| 82 | |
| 83 | def get_private_bytes(self, minimal): |
Fabio Utzig | 4facd1b | 2020-04-02 13:17:38 -0300 | [diff] [blame] | 84 | return self.key.private_bytes( |
| 85 | encoding=serialization.Encoding.DER, |
| 86 | format=serialization.PrivateFormat.PKCS8, |
| 87 | encryption_algorithm=serialization.NoEncryption()) |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 88 | |
| 89 | def export_private(self, path, passwd=None): |
| 90 | """ |
| 91 | Write the private key to the given file, protecting it with the |
| 92 | optional password. |
| 93 | """ |
| 94 | if passwd is None: |
| 95 | enc = serialization.NoEncryption() |
| 96 | else: |
| 97 | enc = serialization.BestAvailableEncryption(passwd) |
| 98 | pem = self.key.private_bytes( |
| 99 | encoding=serialization.Encoding.PEM, |
| 100 | format=serialization.PrivateFormat.PKCS8, |
| 101 | encryption_algorithm=enc) |
| 102 | with open(path, 'wb') as f: |
| 103 | f.write(pem) |
| 104 | |
| 105 | def sign_digest(self, digest): |
| 106 | """Return the actual signature""" |
| 107 | return self.key.sign(data=digest) |
| 108 | |
| 109 | def verify_digest(self, signature, digest): |
| 110 | """Verify that signature is valid for given digest""" |
| 111 | k = self.key |
| 112 | if isinstance(self.key, x25519.X25519PrivateKey): |
| 113 | k = self.key.public_key() |
| 114 | return k.verify(signature=signature, data=digest) |