14
14
AESCCM ,
15
15
AESGCM ,
16
16
AESOCB3 ,
17
- ChaCha20Poly1305 ,
18
17
)
19
18
20
- _AEADTypes = typing .Union [AESCCM , AESGCM , AESOCB3 , ChaCha20Poly1305 ]
21
-
22
-
23
- def _is_evp_aead_supported_cipher (
24
- backend : Backend , cipher : _AEADTypes
25
- ) -> bool :
26
- """
27
- Checks whether the given cipher is supported through
28
- EVP_AEAD rather than the normal OpenSSL EVP_CIPHER API.
29
- """
30
- from cryptography .hazmat .primitives .ciphers .aead import ChaCha20Poly1305
31
-
32
- return backend ._lib .Cryptography_HAS_EVP_AEAD and isinstance (
33
- cipher , ChaCha20Poly1305
34
- )
19
+ _AEADTypes = typing .Union [AESCCM , AESGCM , AESOCB3 ]
35
20
36
21
37
22
def _aead_cipher_supported (backend : Backend , cipher : _AEADTypes ) -> bool :
38
- if _is_evp_aead_supported_cipher (backend , cipher ):
39
- return True
40
- else :
41
- cipher_name = _evp_cipher_cipher_name (cipher )
42
- if backend ._fips_enabled and cipher_name not in backend ._fips_aead :
43
- return False
44
- return (
45
- backend ._lib .EVP_get_cipherbyname (cipher_name ) != backend ._ffi .NULL
46
- )
23
+ cipher_name = _evp_cipher_cipher_name (cipher )
24
+ if backend ._fips_enabled and cipher_name not in backend ._fips_aead :
25
+ return False
26
+ return backend ._lib .EVP_get_cipherbyname (cipher_name ) != backend ._ffi .NULL
47
27
48
28
49
29
def _aead_create_ctx (
50
30
backend : Backend ,
51
31
cipher : _AEADTypes ,
52
32
key : bytes ,
53
33
):
54
- if _is_evp_aead_supported_cipher (backend , cipher ):
55
- return _evp_aead_create_ctx (backend , cipher , key )
56
- else :
57
- return _evp_cipher_create_ctx (backend , cipher , key )
34
+ return _evp_cipher_create_ctx (backend , cipher , key )
58
35
59
36
60
37
def _encrypt (
@@ -66,14 +43,9 @@ def _encrypt(
66
43
tag_length : int ,
67
44
ctx : typing .Any = None ,
68
45
) -> bytes :
69
- if _is_evp_aead_supported_cipher (backend , cipher ):
70
- return _evp_aead_encrypt (
71
- backend , cipher , nonce , data , associated_data , tag_length , ctx
72
- )
73
- else :
74
- return _evp_cipher_encrypt (
75
- backend , cipher , nonce , data , associated_data , tag_length , ctx
76
- )
46
+ return _evp_cipher_encrypt (
47
+ backend , cipher , nonce , data , associated_data , tag_length , ctx
48
+ )
77
49
78
50
79
51
def _decrypt (
@@ -85,132 +57,10 @@ def _decrypt(
85
57
tag_length : int ,
86
58
ctx : typing .Any = None ,
87
59
) -> bytes :
88
- if _is_evp_aead_supported_cipher (backend , cipher ):
89
- return _evp_aead_decrypt (
90
- backend , cipher , nonce , data , associated_data , tag_length , ctx
91
- )
92
- else :
93
- return _evp_cipher_decrypt (
94
- backend , cipher , nonce , data , associated_data , tag_length , ctx
95
- )
96
-
97
-
98
- def _evp_aead_create_ctx (
99
- backend : Backend ,
100
- cipher : _AEADTypes ,
101
- key : bytes ,
102
- tag_len : typing .Optional [int ] = None ,
103
- ):
104
- aead_cipher = _evp_aead_get_cipher (backend , cipher )
105
- assert aead_cipher is not None
106
- key_ptr = backend ._ffi .from_buffer (key )
107
- tag_len = (
108
- backend ._lib .EVP_AEAD_DEFAULT_TAG_LENGTH
109
- if tag_len is None
110
- else tag_len
111
- )
112
- ctx = backend ._lib .Cryptography_EVP_AEAD_CTX_new (
113
- aead_cipher , key_ptr , len (key ), tag_len
114
- )
115
- backend .openssl_assert (ctx != backend ._ffi .NULL )
116
- ctx = backend ._ffi .gc (ctx , backend ._lib .EVP_AEAD_CTX_free )
117
- return ctx
118
-
119
-
120
- def _evp_aead_get_cipher (backend : Backend , cipher : _AEADTypes ):
121
- from cryptography .hazmat .primitives .ciphers .aead import (
122
- ChaCha20Poly1305 ,
60
+ return _evp_cipher_decrypt (
61
+ backend , cipher , nonce , data , associated_data , tag_length , ctx
123
62
)
124
63
125
- # Currently only ChaCha20-Poly1305 is supported using this API
126
- assert isinstance (cipher , ChaCha20Poly1305 )
127
- return backend ._lib .EVP_aead_chacha20_poly1305 ()
128
-
129
-
130
- def _evp_aead_encrypt (
131
- backend : Backend ,
132
- cipher : _AEADTypes ,
133
- nonce : bytes ,
134
- data : bytes ,
135
- associated_data : typing .List [bytes ],
136
- tag_length : int ,
137
- ctx : typing .Any ,
138
- ) -> bytes :
139
- assert ctx is not None
140
-
141
- aead_cipher = _evp_aead_get_cipher (backend , cipher )
142
- assert aead_cipher is not None
143
-
144
- out_len = backend ._ffi .new ("size_t *" )
145
- # max_out_len should be in_len plus the result of
146
- # EVP_AEAD_max_overhead.
147
- max_out_len = len (data ) + backend ._lib .EVP_AEAD_max_overhead (aead_cipher )
148
- out_buf = backend ._ffi .new ("uint8_t[]" , max_out_len )
149
- data_ptr = backend ._ffi .from_buffer (data )
150
- nonce_ptr = backend ._ffi .from_buffer (nonce )
151
- aad = b"" .join (associated_data )
152
- aad_ptr = backend ._ffi .from_buffer (aad )
153
-
154
- res = backend ._lib .EVP_AEAD_CTX_seal (
155
- ctx ,
156
- out_buf ,
157
- out_len ,
158
- max_out_len ,
159
- nonce_ptr ,
160
- len (nonce ),
161
- data_ptr ,
162
- len (data ),
163
- aad_ptr ,
164
- len (aad ),
165
- )
166
- backend .openssl_assert (res == 1 )
167
- encrypted_data = backend ._ffi .buffer (out_buf , out_len [0 ])[:]
168
- return encrypted_data
169
-
170
-
171
- def _evp_aead_decrypt (
172
- backend : Backend ,
173
- cipher : _AEADTypes ,
174
- nonce : bytes ,
175
- data : bytes ,
176
- associated_data : typing .List [bytes ],
177
- tag_length : int ,
178
- ctx : typing .Any ,
179
- ) -> bytes :
180
- if len (data ) < tag_length :
181
- raise InvalidTag
182
-
183
- assert ctx is not None
184
-
185
- out_len = backend ._ffi .new ("size_t *" )
186
- # max_out_len should at least in_len
187
- max_out_len = len (data )
188
- out_buf = backend ._ffi .new ("uint8_t[]" , max_out_len )
189
- data_ptr = backend ._ffi .from_buffer (data )
190
- nonce_ptr = backend ._ffi .from_buffer (nonce )
191
- aad = b"" .join (associated_data )
192
- aad_ptr = backend ._ffi .from_buffer (aad )
193
-
194
- res = backend ._lib .EVP_AEAD_CTX_open (
195
- ctx ,
196
- out_buf ,
197
- out_len ,
198
- max_out_len ,
199
- nonce_ptr ,
200
- len (nonce ),
201
- data_ptr ,
202
- len (data ),
203
- aad_ptr ,
204
- len (aad ),
205
- )
206
-
207
- if res == 0 :
208
- backend ._consume_errors ()
209
- raise InvalidTag
210
-
211
- decrypted_data = backend ._ffi .buffer (out_buf , out_len [0 ])[:]
212
- return decrypted_data
213
-
214
64
215
65
_ENCRYPT = 1
216
66
_DECRYPT = 0
@@ -221,12 +71,9 @@ def _evp_cipher_cipher_name(cipher: _AEADTypes) -> bytes:
221
71
AESCCM ,
222
72
AESGCM ,
223
73
AESOCB3 ,
224
- ChaCha20Poly1305 ,
225
74
)
226
75
227
- if isinstance (cipher , ChaCha20Poly1305 ):
228
- return b"chacha20-poly1305"
229
- elif isinstance (cipher , AESCCM ):
76
+ if isinstance (cipher , AESCCM ):
230
77
return f"aes-{ len (cipher ._key ) * 8 } -ccm" .encode ("ascii" )
231
78
elif isinstance (cipher , AESOCB3 ):
232
79
return f"aes-{ len (cipher ._key ) * 8 } -ocb" .encode ("ascii" )
0 commit comments