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 | |
| 37 | def get_private_bytes(self, minimal): |
| 38 | self._unsupported('get_private_bytes') |
| 39 | |
| 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 "X25519" |
| 53 | |
| 54 | def sig_tlv(self): |
| 55 | return "X25519" |
| 56 | |
| 57 | def sig_len(self): |
| 58 | return 32 |
| 59 | |
| 60 | |
| 61 | class X25519(X25519Public): |
| 62 | """ |
| 63 | Wrapper around an X25519 private key. |
| 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 = x25519.X25519PrivateKey.generate() |
| 73 | return X25519(pk) |
| 74 | |
| 75 | def _get_public(self): |
| 76 | return self.key.public_key() |
| 77 | |
| 78 | def get_private_bytes(self, minimal): |
Fabio Utzig | 4facd1b | 2020-04-02 13:17:38 -0300 | [diff] [blame] | 79 | return self.key.private_bytes( |
| 80 | encoding=serialization.Encoding.DER, |
| 81 | format=serialization.PrivateFormat.PKCS8, |
| 82 | encryption_algorithm=serialization.NoEncryption()) |
Fabio Utzig | 960b4c5 | 2020-04-02 13:07:12 -0300 | [diff] [blame] | 83 | |
| 84 | def export_private(self, path, passwd=None): |
| 85 | """ |
| 86 | Write the private key to the given file, protecting it with the |
| 87 | optional password. |
| 88 | """ |
| 89 | if passwd is None: |
| 90 | enc = serialization.NoEncryption() |
| 91 | else: |
| 92 | enc = serialization.BestAvailableEncryption(passwd) |
| 93 | pem = self.key.private_bytes( |
| 94 | encoding=serialization.Encoding.PEM, |
| 95 | format=serialization.PrivateFormat.PKCS8, |
| 96 | encryption_algorithm=enc) |
| 97 | with open(path, 'wb') as f: |
| 98 | f.write(pem) |
| 99 | |
| 100 | def sign_digest(self, digest): |
| 101 | """Return the actual signature""" |
| 102 | return self.key.sign(data=digest) |
| 103 | |
| 104 | def verify_digest(self, signature, digest): |
| 105 | """Verify that signature is valid for given digest""" |
| 106 | k = self.key |
| 107 | if isinstance(self.key, x25519.X25519PrivateKey): |
| 108 | k = self.key.public_key() |
| 109 | return k.verify(signature=signature, data=digest) |