1# This file is dual licensed under the terms of the Apache License, Version 
    2# 2.0, and the BSD License. See the LICENSE file in the root of this repository 
    3# for complete details. 
    4 
    5from __future__ import annotations 
    6 
    7import abc 
    8import typing 
    9 
    10from cryptography.hazmat.bindings._rust import openssl as rust_openssl 
    11from cryptography.hazmat.primitives import _serialization, hashes 
    12from cryptography.hazmat.primitives.asymmetric import utils as asym_utils 
    13from cryptography.utils import Buffer 
    14 
    15 
    16class DSAParameters(metaclass=abc.ABCMeta): 
    17    @abc.abstractmethod 
    18    def generate_private_key(self) -> DSAPrivateKey: 
    19        """ 
    20        Generates and returns a DSAPrivateKey. 
    21        """ 
    22 
    23    @abc.abstractmethod 
    24    def parameter_numbers(self) -> DSAParameterNumbers: 
    25        """ 
    26        Returns a DSAParameterNumbers. 
    27        """ 
    28 
    29 
    30DSAParametersWithNumbers = DSAParameters 
    31DSAParameters.register(rust_openssl.dsa.DSAParameters) 
    32 
    33 
    34class DSAPrivateKey(metaclass=abc.ABCMeta): 
    35    @property 
    36    @abc.abstractmethod 
    37    def key_size(self) -> int: 
    38        """ 
    39        The bit length of the prime modulus. 
    40        """ 
    41 
    42    @abc.abstractmethod 
    43    def public_key(self) -> DSAPublicKey: 
    44        """ 
    45        The DSAPublicKey associated with this private key. 
    46        """ 
    47 
    48    @abc.abstractmethod 
    49    def parameters(self) -> DSAParameters: 
    50        """ 
    51        The DSAParameters object associated with this private key. 
    52        """ 
    53 
    54    @abc.abstractmethod 
    55    def sign( 
    56        self, 
    57        data: Buffer, 
    58        algorithm: asym_utils.Prehashed | hashes.HashAlgorithm, 
    59    ) -> bytes: 
    60        """ 
    61        Signs the data 
    62        """ 
    63 
    64    @abc.abstractmethod 
    65    def private_numbers(self) -> DSAPrivateNumbers: 
    66        """ 
    67        Returns a DSAPrivateNumbers. 
    68        """ 
    69 
    70    @abc.abstractmethod 
    71    def private_bytes( 
    72        self, 
    73        encoding: _serialization.Encoding, 
    74        format: _serialization.PrivateFormat, 
    75        encryption_algorithm: _serialization.KeySerializationEncryption, 
    76    ) -> bytes: 
    77        """ 
    78        Returns the key serialized as bytes. 
    79        """ 
    80 
    81    @abc.abstractmethod 
    82    def __copy__(self) -> DSAPrivateKey: 
    83        """ 
    84        Returns a copy. 
    85        """ 
    86 
    87 
    88DSAPrivateKeyWithSerialization = DSAPrivateKey 
    89DSAPrivateKey.register(rust_openssl.dsa.DSAPrivateKey) 
    90 
    91 
    92class DSAPublicKey(metaclass=abc.ABCMeta): 
    93    @property 
    94    @abc.abstractmethod 
    95    def key_size(self) -> int: 
    96        """ 
    97        The bit length of the prime modulus. 
    98        """ 
    99 
    100    @abc.abstractmethod 
    101    def parameters(self) -> DSAParameters: 
    102        """ 
    103        The DSAParameters object associated with this public key. 
    104        """ 
    105 
    106    @abc.abstractmethod 
    107    def public_numbers(self) -> DSAPublicNumbers: 
    108        """ 
    109        Returns a DSAPublicNumbers. 
    110        """ 
    111 
    112    @abc.abstractmethod 
    113    def public_bytes( 
    114        self, 
    115        encoding: _serialization.Encoding, 
    116        format: _serialization.PublicFormat, 
    117    ) -> bytes: 
    118        """ 
    119        Returns the key serialized as bytes. 
    120        """ 
    121 
    122    @abc.abstractmethod 
    123    def verify( 
    124        self, 
    125        signature: Buffer, 
    126        data: Buffer, 
    127        algorithm: asym_utils.Prehashed | hashes.HashAlgorithm, 
    128    ) -> None: 
    129        """ 
    130        Verifies the signature of the data. 
    131        """ 
    132 
    133    @abc.abstractmethod 
    134    def __eq__(self, other: object) -> bool: 
    135        """ 
    136        Checks equality. 
    137        """ 
    138 
    139    @abc.abstractmethod 
    140    def __copy__(self) -> DSAPublicKey: 
    141        """ 
    142        Returns a copy. 
    143        """ 
    144 
    145 
    146DSAPublicKeyWithSerialization = DSAPublicKey 
    147DSAPublicKey.register(rust_openssl.dsa.DSAPublicKey) 
    148 
    149DSAPrivateNumbers = rust_openssl.dsa.DSAPrivateNumbers 
    150DSAPublicNumbers = rust_openssl.dsa.DSAPublicNumbers 
    151DSAParameterNumbers = rust_openssl.dsa.DSAParameterNumbers 
    152 
    153 
    154def generate_parameters( 
    155    key_size: int, backend: typing.Any = None 
    156) -> DSAParameters: 
    157    if key_size not in (1024, 2048, 3072, 4096): 
    158        raise ValueError("Key size must be 1024, 2048, 3072, or 4096 bits.") 
    159 
    160    return rust_openssl.dsa.generate_parameters(key_size) 
    161 
    162 
    163def generate_private_key( 
    164    key_size: int, backend: typing.Any = None 
    165) -> DSAPrivateKey: 
    166    parameters = generate_parameters(key_size) 
    167    return parameters.generate_private_key()