1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
# Copyright (c) 2014-2023 by Ron Frederick <ronf@timeheart.net> and others.
#
# This program and the accompanying materials are made available under
# the terms of the Eclipse Public License v2.0 which accompanies this
# distribution and is available at:
#
# http://www.eclipse.org/legal/epl-2.0/
#
# This program may also be made available under the following secondary
# licenses when the conditions for such availability set forth in the
# Eclipse Public License v2.0 are satisfied:
#
# GNU General Public License, Version 2.0, or any later versions of
# that license
#
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
#
# Contributors:
# Ron Frederick - initial implementation, API, and documentation
"""A shim around PyCA for DSA public and private keys"""
from typing import Optional, cast
from cryptography.exceptions import InvalidSignature
from cryptography.hazmat.primitives.asymmetric import dsa
from .misc import CryptoKey, PyCAKey, hashes
# Short variable names are used here, matching names in the spec
# pylint: disable=invalid-name
class _DSAKey(CryptoKey):
"""Base class for shim around PyCA for DSA keys"""
def __init__(self, pyca_key: PyCAKey, params: dsa.DSAParameterNumbers,
pub: dsa.DSAPublicNumbers,
priv: Optional[dsa.DSAPrivateNumbers] = None):
super().__init__(pyca_key)
self._params = params
self._pub = pub
self._priv = priv
@property
def p(self) -> int:
"""Return the DSA public modulus"""
return self._params.p
@property
def q(self) -> int:
"""Return the DSA sub-group order"""
return self._params.q
@property
def g(self) -> int:
"""Return the DSA generator"""
return self._params.g
@property
def y(self) -> int:
"""Return the DSA public value"""
return self._pub.y
@property
def x(self) -> Optional[int]:
"""Return the DSA private value"""
return self._priv.x if self._priv else None
class DSAPrivateKey(_DSAKey):
"""A shim around PyCA for DSA private keys"""
@classmethod
def construct(cls, p: int, q: int, g: int,
y: int, x: int) -> 'DSAPrivateKey':
"""Construct a DSA private key"""
params = dsa.DSAParameterNumbers(p, q, g)
pub = dsa.DSAPublicNumbers(y, params)
priv = dsa.DSAPrivateNumbers(x, pub)
priv_key = priv.private_key()
return cls(priv_key, params, pub, priv)
@classmethod
def generate(cls, key_size: int) -> 'DSAPrivateKey':
"""Generate a new DSA private key"""
priv_key = dsa.generate_private_key(key_size)
priv = priv_key.private_numbers()
pub = priv.public_numbers
params = pub.parameter_numbers
return cls(priv_key, params, pub, priv)
def sign(self, data: bytes, hash_name: str = '') -> bytes:
"""Sign a block of data"""
priv_key = cast('dsa.DSAPrivateKey', self.pyca_key)
return priv_key.sign(data, hashes[hash_name]())
class DSAPublicKey(_DSAKey):
"""A shim around PyCA for DSA public keys"""
@classmethod
def construct(cls, p: int, q: int, g: int, y: int) -> 'DSAPublicKey':
"""Construct a DSA public key"""
params = dsa.DSAParameterNumbers(p, q, g)
pub = dsa.DSAPublicNumbers(y, params)
pub_key = pub.public_key()
return cls(pub_key, params, pub)
def verify(self, data: bytes, sig: bytes, hash_name: str = '') -> bool:
"""Verify the signature on a block of data"""
try:
pub_key = cast('dsa.DSAPublicKey', self.pyca_key)
pub_key.verify(sig, data, hashes[hash_name]())
return True
except InvalidSignature:
return False
|