| 1 |
from cryptography.hazmat.backends import default_backend |
| 2 |
from cryptography.hazmat.primitives import hashes |
| 3 |
from cryptography.hazmat.primitives.asymmetric import ec, utils |
| 4 |
|
| 5 |
from dns.dnssecalgs.cryptography import CryptographyPrivateKey, CryptographyPublicKey |
| 6 |
from dns.dnssectypes import Algorithm |
| 7 |
from dns.rdtypes.ANY.DNSKEY import DNSKEY |
| 8 |
|
| 9 |
|
| 10 |
class PublicECDSA(CryptographyPublicKey): |
| 11 |
key: ec.EllipticCurvePublicKey |
| 12 |
key_cls = ec.EllipticCurvePublicKey |
| 13 |
algorithm: Algorithm |
| 14 |
chosen_hash: hashes.HashAlgorithm |
| 15 |
curve: ec.EllipticCurve |
| 16 |
octets: int |
| 17 |
|
| 18 |
def verify(self, signature: bytes, data: bytes) -> None: |
| 19 |
sig_r = signature[0 : self.octets] |
| 20 |
sig_s = signature[self.octets :] |
| 21 |
sig = utils.encode_dss_signature( |
| 22 |
int.from_bytes(sig_r, "big"), int.from_bytes(sig_s, "big") |
| 23 |
) |
| 24 |
self.key.verify(sig, data, ec.ECDSA(self.chosen_hash)) |
| 25 |
|
| 26 |
def encode_key_bytes(self) -> bytes: |
| 27 |
"""Encode a public key per RFC 6605, section 4.""" |
| 28 |
pn = self.key.public_numbers() |
| 29 |
return pn.x.to_bytes(self.octets, "big") + pn.y.to_bytes(self.octets, "big") |
| 30 |
|
| 31 |
@classmethod |
| 32 |
def from_dnskey(cls, key: DNSKEY) -> "PublicECDSA": |
| 33 |
cls._ensure_algorithm_key_combination(key) |
| 34 |
ecdsa_x = key.key[0 : cls.octets] |
| 35 |
ecdsa_y = key.key[cls.octets : cls.octets * 2] |
| 36 |
return cls( |
| 37 |
key=ec.EllipticCurvePublicNumbers( |
| 38 |
curve=cls.curve, |
| 39 |
x=int.from_bytes(ecdsa_x, "big"), |
| 40 |
y=int.from_bytes(ecdsa_y, "big"), |
| 41 |
).public_key(default_backend()), |
| 42 |
) |
| 43 |
|
| 44 |
|
| 45 |
class PrivateECDSA(CryptographyPrivateKey): |
| 46 |
key: ec.EllipticCurvePrivateKey |
| 47 |
key_cls = ec.EllipticCurvePrivateKey |
| 48 |
public_cls = PublicECDSA |
| 49 |
|
| 50 |
def sign( |
| 51 |
self, |
| 52 |
data: bytes, |
| 53 |
verify: bool = False, |
| 54 |
deterministic: bool = True, |
| 55 |
) -> bytes: |
| 56 |
"""Sign using a private key per RFC 6605, section 4.""" |
| 57 |
algorithm = ec.ECDSA( |
| 58 |
self.public_cls.chosen_hash, # pyright: ignore |
| 59 |
deterministic_signing=deterministic, |
| 60 |
) |
| 61 |
der_signature = self.key.sign(data, algorithm) |
| 62 |
dsa_r, dsa_s = utils.decode_dss_signature(der_signature) |
| 63 |
signature = int.to_bytes( |
| 64 |
dsa_r, length=self.public_cls.octets, byteorder="big" # pyright: ignore |
| 65 |
) + int.to_bytes( |
| 66 |
dsa_s, length=self.public_cls.octets, byteorder="big" # pyright: ignore |
| 67 |
) |
| 68 |
if verify: |
| 69 |
self.public_key().verify(signature, data) |
| 70 |
return signature |
| 71 |
|
| 72 |
@classmethod |
| 73 |
def generate(cls) -> "PrivateECDSA": |
| 74 |
return cls( |
| 75 |
key=ec.generate_private_key( |
| 76 |
curve=cls.public_cls.curve, backend=default_backend() # pyright: ignore |
| 77 |
), |
| 78 |
) |
| 79 |
|
| 80 |
|
| 81 |
class PublicECDSAP256SHA256(PublicECDSA): |
| 82 |
algorithm = Algorithm.ECDSAP256SHA256 |
| 83 |
chosen_hash = hashes.SHA256() |
| 84 |
curve = ec.SECP256R1() |
| 85 |
octets = 32 |
| 86 |
|
| 87 |
|
| 88 |
class PrivateECDSAP256SHA256(PrivateECDSA): |
| 89 |
public_cls = PublicECDSAP256SHA256 |
| 90 |
|
| 91 |
|
| 92 |
class PublicECDSAP384SHA384(PublicECDSA): |
| 93 |
algorithm = Algorithm.ECDSAP384SHA384 |
| 94 |
chosen_hash = hashes.SHA384() |
| 95 |
curve = ec.SECP384R1() |
| 96 |
octets = 48 |
| 97 |
|
| 98 |
|
| 99 |
class PrivateECDSAP384SHA384(PrivateECDSA): |
| 100 |
public_cls = PublicECDSAP384SHA384 |