Skip to content

Commit 047caa9

Browse files
jlainesrittau
authored andcommitted
Add more stubs for cryptography (#3307)
1 parent 66c3945 commit 047caa9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1537
-26
lines changed
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
from typing import Any
2-
3-
def __getattr__(name: str) -> Any: ...
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class AlreadyFinalized(Exception): ...
2+
class AlreadyUpdated(Exception): ...
3+
class InvalidKey(Exception): ...
4+
class InvalidSignature(Exception): ...
5+
class InvalidTag(Exception): ...
6+
class NotYetFinalized(Exception): ...
7+
class UnsupportedAlgorithm(Exception): ...
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from typing import List, Optional
2+
3+
class InvalidToken(Exception): ...
4+
5+
class Fernet(object):
6+
def __init__(self, key: bytes) -> None: ...
7+
def decrypt(self, token: bytes, ttl: Optional[int] = ...) -> bytes: ...
8+
def encrypt(self, data: bytes) -> bytes: ...
9+
def extract_timestamp(self, token: bytes) -> int: ...
10+
@classmethod
11+
def generate_key(cls) -> bytes: ...
12+
13+
class MultiFernet(object):
14+
def __init__(self, fernets: List[Fernet]) -> None: ...
15+
def decrypt(self, token: bytes, ttl: Optional[int] = ...) -> bytes: ...
16+
def encrypt(self, data: bytes) -> bytes: ...
17+
def rotate(self, msg: bytes) -> bytes: ...
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from typing import Any
2+
3+
def default_backend() -> Any: ...
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
from abc import ABCMeta, abstractmethod
2+
from typing import Any, Optional, Union
3+
4+
from cryptography.hazmat.primitives.asymmetric.dh import (
5+
DHParameterNumbers,
6+
DHParameters,
7+
DHPrivateKey,
8+
DHPrivateNumbers,
9+
DHPublicKey,
10+
DHPublicNumbers,
11+
)
12+
from cryptography.hazmat.primitives.asymmetric.dsa import (
13+
DSAParameterNumbers,
14+
DSAParameters,
15+
DSAPrivateKey,
16+
DSAPrivateNumbers,
17+
DSAPublicKey,
18+
DSAPublicNumbers,
19+
)
20+
from cryptography.hazmat.primitives.asymmetric.ec import (
21+
EllipticCurve,
22+
EllipticCurvePrivateKey,
23+
EllipticCurvePrivateNumbers,
24+
EllipticCurvePublicKey,
25+
EllipticCurvePublicNumbers,
26+
EllipticCurveSignatureAlgorithm,
27+
)
28+
from cryptography.hazmat.primitives.asymmetric.padding import AsymmetricPadding
29+
from cryptography.hazmat.primitives.asymmetric.rsa import RSAPrivateKey, RSAPrivateNumbers, RSAPublicKey, RSAPublicNumbers
30+
from cryptography.hazmat.primitives.ciphers import BlockCipherAlgorithm, CipherAlgorithm, CipherContext
31+
from cryptography.hazmat.primitives.ciphers.modes import Mode
32+
from cryptography.hazmat.primitives.hashes import HashAlgorithm, HashContext
33+
from cryptography.x509 import (
34+
Certificate,
35+
CertificateBuilder,
36+
CertificateRevocationList,
37+
CertificateRevocationListBuilder,
38+
CertificateSigningRequest,
39+
CertificateSigningRequestBuilder,
40+
Name,
41+
RevokedCertificate,
42+
RevokedCertificateBuilder,
43+
)
44+
45+
class CipherBackend(metaclass=ABCMeta):
46+
@abstractmethod
47+
def cipher_supported(self, cipher: CipherAlgorithm, mode: Mode) -> bool: ...
48+
@abstractmethod
49+
def create_symmetric_encryption_ctx(self, cipher: CipherAlgorithm, mode: Mode) -> CipherContext: ...
50+
@abstractmethod
51+
def create_symmetric_decryption_ctx(self, cipher: CipherAlgorithm, mode: Mode) -> CipherContext: ...
52+
53+
class CMACBackend(metaclass=ABCMeta):
54+
@abstractmethod
55+
def cmac_algorithm_supported(self, algorithm: BlockCipherAlgorithm) -> bool: ...
56+
@abstractmethod
57+
def create_cmac_ctx(self, algorithm: BlockCipherAlgorithm) -> Any: ...
58+
59+
class DERSerializationBackend(metaclass=ABCMeta):
60+
@abstractmethod
61+
def load_der_parameters(self, data: bytes) -> Any: ...
62+
@abstractmethod
63+
def load_der_private_key(self, data: bytes, password: Optional[bytes]) -> Any: ...
64+
@abstractmethod
65+
def load_der_public_key(self, data: bytes) -> Any: ...
66+
67+
class DHBackend(metaclass=ABCMeta):
68+
@abstractmethod
69+
def dh_parameters_supported(self, p: int, g: int, q: Optional[int]) -> bool: ...
70+
@abstractmethod
71+
def dh_x942_serialization_supported(self) -> bool: ...
72+
@abstractmethod
73+
def generate_dh_parameters(self, generator: int, key_size: int) -> DHParameters: ...
74+
@abstractmethod
75+
def generate_dh_private_key(self, parameters: DHParameters) -> DHPrivateKey: ...
76+
@abstractmethod
77+
def generate_dh_private_key_and_parameters(self, generator: int, key_size: int) -> DHPrivateKey: ...
78+
@abstractmethod
79+
def load_dh_parameter_numbers(self, numbers: DHParameterNumbers) -> DHParameters: ...
80+
@abstractmethod
81+
def load_dh_private_numbers(self, numbers: DHPrivateNumbers) -> DHPrivateKey: ...
82+
@abstractmethod
83+
def load_dh_public_numbers(self, numbers: DHPublicNumbers) -> DHPublicKey: ...
84+
85+
class DSABackend(metaclass=ABCMeta):
86+
@abstractmethod
87+
def dsa_hash_supported(self, algorithm: HashAlgorithm) -> bool: ...
88+
@abstractmethod
89+
def dsa_parameters_supported(self, p: int, q: int, g: int) -> bool: ...
90+
@abstractmethod
91+
def generate_dsa_parameters(self, key_size: int) -> DSAParameters: ...
92+
@abstractmethod
93+
def generate_dsa_private_key(self, parameters: DSAParameters) -> DSAPrivateKey: ...
94+
@abstractmethod
95+
def generate_dsa_private_key_and_parameters(self, key_size: int) -> DSAPrivateKey: ...
96+
@abstractmethod
97+
def load_dsa_parameter_numbers(self, numbers: DSAParameterNumbers) -> DSAParameters: ...
98+
@abstractmethod
99+
def load_dsa_private_numbers(self, numbers: DSAPrivateNumbers) -> DSAPrivateKey: ...
100+
@abstractmethod
101+
def load_dsa_public_numbers(self, numbers: DSAPublicNumbers) -> DSAPublicKey: ...
102+
103+
class EllipticCurveBackend(metaclass=ABCMeta):
104+
@abstractmethod
105+
def derive_elliptic_curve_private_key(self, private_value: int, curve: EllipticCurve) -> EllipticCurvePrivateKey: ...
106+
@abstractmethod
107+
def elliptic_curve_signature_algorithm_supported(
108+
self, signature_algorithm: EllipticCurveSignatureAlgorithm, curve: EllipticCurve
109+
) -> bool: ...
110+
@abstractmethod
111+
def elliptic_curve_supported(self, curve: EllipticCurve) -> bool: ...
112+
@abstractmethod
113+
def generate_elliptic_curve_private_key(self, curve: EllipticCurve) -> EllipticCurvePrivateKey: ...
114+
@abstractmethod
115+
def load_elliptic_curve_private_numbers(self, numbers: EllipticCurvePrivateNumbers) -> EllipticCurvePrivateKey: ...
116+
@abstractmethod
117+
def load_elliptic_curve_public_numbers(self, numbers: EllipticCurvePublicNumbers) -> EllipticCurvePublicKey: ...
118+
119+
class HMACBackend(metaclass=ABCMeta):
120+
@abstractmethod
121+
def create_hmac_ctx(self, key: bytes, algorithm: HashAlgorithm) -> HashContext: ...
122+
@abstractmethod
123+
def cmac_algorithm_supported(self, algorithm: HashAlgorithm) -> bool: ...
124+
125+
class HashBackend(metaclass=ABCMeta):
126+
@abstractmethod
127+
def create_hash_ctx(self, algorithm: HashAlgorithm) -> HashContext: ...
128+
@abstractmethod
129+
def hash_supported(self, algorithm: HashAlgorithm) -> bool: ...
130+
131+
class PBKDF2HMACBackend(metaclass=ABCMeta):
132+
@abstractmethod
133+
def derive_pbkdf2_hmac(
134+
self, algorithm: HashAlgorithm, length: int, salt: bytes, iterations: int, key_material: bytes
135+
) -> bytes: ...
136+
@abstractmethod
137+
def pbkdf2_hmac_supported(self, algorithm: HashAlgorithm) -> bool: ...
138+
139+
class PEMSerializationBackend(metaclass=ABCMeta):
140+
@abstractmethod
141+
def load_pem_parameters(self, data: bytes) -> Any: ...
142+
@abstractmethod
143+
def load_pem_private_key(self, data: bytes, password: Optional[bytes]) -> Any: ...
144+
@abstractmethod
145+
def load_pem_public_key(self, data: bytes) -> Any: ...
146+
147+
class RSABackend(metaclass=ABCMeta):
148+
@abstractmethod
149+
def generate_rsa_parameters_supported(self, public_exponent: int, key_size: int) -> bool: ...
150+
@abstractmethod
151+
def generate_rsa_private_key(self, public_exponent: int, key_size: int) -> RSAPrivateKey: ...
152+
@abstractmethod
153+
def load_rsa_public_numbers(self, numbers: RSAPublicNumbers) -> RSAPublicKey: ...
154+
@abstractmethod
155+
def load_rsa_private_numbers(self, numbers: RSAPrivateNumbers) -> RSAPrivateKey: ...
156+
@abstractmethod
157+
def rsa_padding_supported(self, padding: AsymmetricPadding) -> bool: ...
158+
159+
class ScryptBackend(metaclass=ABCMeta):
160+
@abstractmethod
161+
def derive_scrypt(self, key_material: bytes, salt: bytes, length: int, n: int, r: int, p: int) -> bytes: ...
162+
163+
class X509Backend(metaclass=ABCMeta):
164+
@abstractmethod
165+
def create_x509_certificate(
166+
self,
167+
builder: CertificateBuilder,
168+
private_key: Union[DSAPrivateKey, EllipticCurvePrivateKey, RSAPrivateKey],
169+
algorithm: HashAlgorithm,
170+
) -> Certificate: ...
171+
@abstractmethod
172+
def create_x509_crl(
173+
self,
174+
builder: CertificateRevocationListBuilder,
175+
private_key: Union[DSAPrivateKey, EllipticCurvePrivateKey, RSAPrivateKey],
176+
algorithm: HashAlgorithm,
177+
) -> CertificateRevocationList: ...
178+
@abstractmethod
179+
def create_x509_csr(
180+
self,
181+
builder: CertificateSigningRequestBuilder,
182+
private_key: Union[DSAPrivateKey, EllipticCurvePrivateKey, RSAPrivateKey],
183+
algorithm: HashAlgorithm,
184+
) -> CertificateSigningRequest: ...
185+
@abstractmethod
186+
def create_x509_revoked_certificate(self, builder: RevokedCertificateBuilder) -> RevokedCertificate: ...
187+
@abstractmethod
188+
def load_der_x509_certificate(self, data: bytes) -> Certificate: ...
189+
@abstractmethod
190+
def load_der_x509_csr(self, data: bytes) -> CertificateSigningRequest: ...
191+
@abstractmethod
192+
def load_pem_x509_certificate(self, data: bytes) -> Certificate: ...
193+
@abstractmethod
194+
def load_pem_x509_csr(self, data: bytes) -> CertificateSigningRequest: ...
195+
@abstractmethod
196+
def x509_name_bytes(self, name: Name) -> bytes: ...

third_party/2and3/cryptography/hazmat/bindings/__init__.pyi

Whitespace-only changes.

third_party/2and3/cryptography/hazmat/bindings/openssl/__init__.pyi

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from typing import Any, Optional
2+
3+
class Binding(object):
4+
ffi: Optional[Any]
5+
lib: Optional[Any]
6+
def init_static_locks(self) -> None: ...
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
from abc import ABCMeta, abstractmethod
2+
from typing import Optional
3+
4+
from cryptography.hazmat.backends.interfaces import DHBackend
5+
from cryptography.hazmat.primitives.serialization import (
6+
Encoding,
7+
KeySerializationEncryption,
8+
ParameterFormat,
9+
PrivateFormat,
10+
PublicFormat,
11+
)
12+
13+
class DHParameters(metaclass=ABCMeta):
14+
@abstractmethod
15+
def generate_private_key(self) -> DHPrivateKey: ...
16+
@abstractmethod
17+
def parameter_bytes(self, encoding: Encoding, format: ParameterFormat) -> bytes: ...
18+
@abstractmethod
19+
def parameter_numbers(self) -> DHParameterNumbers: ...
20+
21+
DHParametersWithSerialization = DHParameters
22+
23+
class DHParameterNumbers(object):
24+
@property
25+
def p(self) -> int: ...
26+
@property
27+
def g(self) -> int: ...
28+
@property
29+
def q(self) -> int: ...
30+
def __init__(self, p: int, g: int, q: Optional[int]) -> None: ...
31+
def parameters(self, backend: DHBackend) -> DHParameters: ...
32+
33+
class DHPrivateKey(metaclass=ABCMeta):
34+
key_size: int
35+
@abstractmethod
36+
def exchange(self, peer_public_key: DHPublicKey) -> bytes: ...
37+
@abstractmethod
38+
def parameters(self) -> DHParameters: ...
39+
@abstractmethod
40+
def public_key(self) -> DHPublicKey: ...
41+
42+
class DHPrivateKeyWithSerialization(DHPrivateKey):
43+
@abstractmethod
44+
def private_bytes(
45+
self, encoding: Encoding, format: PrivateFormat, encryption_algorithm: KeySerializationEncryption
46+
) -> bytes: ...
47+
@abstractmethod
48+
def private_numbers(self) -> DHPrivateNumbers: ...
49+
50+
class DHPrivateNumbers(object):
51+
@property
52+
def public_numbers(self) -> DHPublicNumbers: ...
53+
@property
54+
def x(self) -> int: ...
55+
def __init__(self, x: int, public_numbers: DHPublicNumbers) -> None: ...
56+
def private_key(self, backend: DHBackend) -> DHPrivateKey: ...
57+
58+
class DHPublicKey(metaclass=ABCMeta):
59+
@property
60+
@abstractmethod
61+
def key_size(self) -> int: ...
62+
@abstractmethod
63+
def parameters(self) -> DHParameters: ...
64+
@abstractmethod
65+
def public_bytes(self, encoding: Encoding, format: PublicFormat) -> bytes: ...
66+
@abstractmethod
67+
def public_numbers(self) -> DHPublicNumbers: ...
68+
69+
DHPublicKeyWithSerialization = DHPublicKey
70+
71+
class DHPublicNumbers(object):
72+
@property
73+
def parameter_numbers(self) -> DHParameterNumbers: ...
74+
@property
75+
def y(self) -> int: ...
76+
def __init__(self, y: int, parameter_numbers: DHParameterNumbers) -> None: ...
77+
def public_key(self, backend: DHBackend) -> DHPublicKey: ...
Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,75 @@
1-
# Minimal stub expressing only the classes required by OpenSSL.crypto.
1+
from abc import ABCMeta, abstractmethod
22

3-
class DSAPrivateKey: ...
4-
class DSAPublicKey: ...
3+
from cryptography.hazmat.backends.interfaces import DSABackend
4+
from cryptography.hazmat.primitives.asymmetric.padding import AsymmetricPadding
5+
from cryptography.hazmat.primitives.hashes import HashAlgorithm
6+
from cryptography.hazmat.primitives.serialization import Encoding, KeySerializationEncryption, PrivateFormat, PublicFormat
7+
8+
class DSAParameters(metaclass=ABCMeta):
9+
@abstractmethod
10+
def generate_private_key(self) -> DSAPrivateKey: ...
11+
12+
class DSAParametersWithNumbers(DSAParameters):
13+
@abstractmethod
14+
def parameter_numbers(self) -> DSAParameterNumbers: ...
15+
16+
class DSAParameterNumbers(object):
17+
@property
18+
def p(self) -> int: ...
19+
@property
20+
def q(self) -> int: ...
21+
@property
22+
def g(self) -> int: ...
23+
def __init__(self, p: int, q: int, g: int) -> None: ...
24+
def parameters(self, backend: DSABackend) -> DSAParameters: ...
25+
26+
class DSAPrivateKey(metaclass=ABCMeta):
27+
@property
28+
@abstractmethod
29+
def key_size(self) -> int: ...
30+
@abstractmethod
31+
def parameters(self) -> DSAParameters: ...
32+
@abstractmethod
33+
def public_key(self) -> DSAPublicKey: ...
34+
@abstractmethod
35+
def sign(self, data: bytes, algorithm: HashAlgorithm) -> bytes: ...
36+
37+
class DSAPrivateKeyWithSerialization(DSAPrivateKey):
38+
@abstractmethod
39+
def private_bytes(
40+
self, encoding: Encoding, format: PrivateFormat, encryption_algorithm: KeySerializationEncryption
41+
) -> bytes: ...
42+
@abstractmethod
43+
def private_numbers(self) -> DSAPrivateNumbers: ...
44+
45+
class DSAPrivateNumbers(object):
46+
@property
47+
def x(self) -> int: ...
48+
@property
49+
def public_numbers(self) -> DSAPublicNumbers: ...
50+
def __init__(self, x: int, public_numbers: DSAPublicNumbers) -> None: ...
51+
52+
class DSAPublicKey(metaclass=ABCMeta):
53+
@property
54+
@abstractmethod
55+
def key_size(self) -> int: ...
56+
@abstractmethod
57+
def public_bytes(self, encoding: Encoding, format: PublicFormat) -> bytes: ...
58+
@abstractmethod
59+
def public_numbers(self) -> DSAPublicNumbers: ...
60+
@abstractmethod
61+
def sign(self, data: bytes, padding: AsymmetricPadding, algorithm: HashAlgorithm) -> bytes: ...
62+
@abstractmethod
63+
def verify(self, signature: bytes, data: bytes, padding: AsymmetricPadding, algorithm: HashAlgorithm) -> None: ...
64+
65+
DSAPublicKeyWithSerialization = DSAPublicKey
66+
67+
class DSAPublicNumbers(object):
68+
@property
69+
def y(self) -> int: ...
70+
@property
71+
def parameter_numbers(self) -> DSAParameterNumbers: ...
72+
def __init__(self, y: int, parameter_numbers: DSAParameterNumbers) -> None: ...
73+
74+
def generate_parameters(key_size: int, backend: DSABackend) -> DSAParameters: ...
75+
def generate_private_key(key_size: int, backend: DSABackend) -> DSAPrivateKey: ...

0 commit comments

Comments
 (0)