Branch data Line data Source code
1 : : /* Module that wraps all OpenSSL hash algorithms */
2 : :
3 : : /*
4 : : * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
5 : : * Licensed to PSF under a Contributor Agreement.
6 : : *
7 : : * Derived from a skeleton of shamodule.c containing work performed by:
8 : : *
9 : : * Andrew Kuchling (amk@amk.ca)
10 : : * Greg Stein (gstein@lyra.org)
11 : : *
12 : : */
13 : :
14 : : /* Don't warn about deprecated functions, */
15 : : #ifndef OPENSSL_API_COMPAT
16 : : // 0x10101000L == 1.1.1, 30000 == 3.0.0
17 : : #define OPENSSL_API_COMPAT 0x10101000L
18 : : #endif
19 : : #define OPENSSL_NO_DEPRECATED 1
20 : :
21 : : #ifndef Py_BUILD_CORE_BUILTIN
22 : : # define Py_BUILD_CORE_MODULE 1
23 : : #endif
24 : :
25 : : #define PY_SSIZE_T_CLEAN
26 : :
27 : : #include "Python.h"
28 : : #include "pycore_hashtable.h"
29 : : #include "hashlib.h"
30 : : #include "pycore_strhex.h" // _Py_strhex()
31 : :
32 : : /* EVP is the preferred interface to hashing in OpenSSL */
33 : : #include <openssl/evp.h>
34 : : #include <openssl/hmac.h>
35 : : #include <openssl/crypto.h> // FIPS_mode()
36 : : /* We use the object interface to discover what hashes OpenSSL supports. */
37 : : #include <openssl/objects.h>
38 : : #include <openssl/err.h>
39 : :
40 : :
41 : : #ifndef OPENSSL_THREADS
42 : : # error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL"
43 : : #endif
44 : :
45 : : #define MUNCH_SIZE INT_MAX
46 : :
47 : : #define PY_OPENSSL_HAS_SCRYPT 1
48 : : #define PY_OPENSSL_HAS_SHA3 1
49 : : #define PY_OPENSSL_HAS_SHAKE 1
50 : : #define PY_OPENSSL_HAS_BLAKE2 1
51 : :
52 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
53 : : #define PY_EVP_MD EVP_MD
54 : : #define PY_EVP_MD_fetch(algorithm, properties) EVP_MD_fetch(NULL, algorithm, properties)
55 : : #define PY_EVP_MD_up_ref(md) EVP_MD_up_ref(md)
56 : : #define PY_EVP_MD_free(md) EVP_MD_free(md)
57 : : #else
58 : : #define PY_EVP_MD const EVP_MD
59 : : #define PY_EVP_MD_fetch(algorithm, properties) EVP_get_digestbyname(algorithm)
60 : : #define PY_EVP_MD_up_ref(md) do {} while(0)
61 : : #define PY_EVP_MD_free(md) do {} while(0)
62 : : #endif
63 : :
64 : : /* hash alias map and fast lookup
65 : : *
66 : : * Map between Python's preferred names and OpenSSL internal names. Maintain
67 : : * cache of fetched EVP MD objects. The EVP_get_digestbyname() and
68 : : * EVP_MD_fetch() API calls have a performance impact.
69 : : *
70 : : * The py_hashentry_t items are stored in a _Py_hashtable_t with py_name and
71 : : * py_alias as keys.
72 : : */
73 : :
74 : : enum Py_hash_type {
75 : : Py_ht_evp, // usedforsecurity=True / default
76 : : Py_ht_evp_nosecurity, // usedforsecurity=False
77 : : Py_ht_mac, // HMAC
78 : : Py_ht_pbkdf2, // PKBDF2
79 : : };
80 : :
81 : : typedef struct {
82 : : const char *py_name;
83 : : const char *py_alias;
84 : : const char *ossl_name;
85 : : int ossl_nid;
86 : : int refcnt;
87 : : PY_EVP_MD *evp;
88 : : PY_EVP_MD *evp_nosecurity;
89 : : } py_hashentry_t;
90 : :
91 : : #define Py_hash_md5 "md5"
92 : : #define Py_hash_sha1 "sha1"
93 : : #define Py_hash_sha224 "sha224"
94 : : #define Py_hash_sha256 "sha256"
95 : : #define Py_hash_sha384 "sha384"
96 : : #define Py_hash_sha512 "sha512"
97 : : #define Py_hash_sha512_224 "sha512_224"
98 : : #define Py_hash_sha512_256 "sha512_256"
99 : : #define Py_hash_sha3_224 "sha3_224"
100 : : #define Py_hash_sha3_256 "sha3_256"
101 : : #define Py_hash_sha3_384 "sha3_384"
102 : : #define Py_hash_sha3_512 "sha3_512"
103 : : #define Py_hash_shake_128 "shake_128"
104 : : #define Py_hash_shake_256 "shake_256"
105 : : #define Py_hash_blake2s "blake2s"
106 : : #define Py_hash_blake2b "blake2b"
107 : :
108 : : #define PY_HASH_ENTRY(py_name, py_alias, ossl_name, ossl_nid) \
109 : : {py_name, py_alias, ossl_name, ossl_nid, 0, NULL, NULL}
110 : :
111 : : static const py_hashentry_t py_hashes[] = {
112 : : /* md5 */
113 : : PY_HASH_ENTRY(Py_hash_md5, "MD5", SN_md5, NID_md5),
114 : : /* sha1 */
115 : : PY_HASH_ENTRY(Py_hash_sha1, "SHA1", SN_sha1, NID_sha1),
116 : : /* sha2 family */
117 : : PY_HASH_ENTRY(Py_hash_sha224, "SHA224", SN_sha224, NID_sha224),
118 : : PY_HASH_ENTRY(Py_hash_sha256, "SHA256", SN_sha256, NID_sha256),
119 : : PY_HASH_ENTRY(Py_hash_sha384, "SHA384", SN_sha384, NID_sha384),
120 : : PY_HASH_ENTRY(Py_hash_sha512, "SHA512", SN_sha512, NID_sha512),
121 : : /* truncated sha2 */
122 : : PY_HASH_ENTRY(Py_hash_sha512_224, "SHA512_224", SN_sha512_224, NID_sha512_224),
123 : : PY_HASH_ENTRY(Py_hash_sha512_256, "SHA512_256", SN_sha512_256, NID_sha512_256),
124 : : /* sha3 */
125 : : PY_HASH_ENTRY(Py_hash_sha3_224, NULL, SN_sha3_224, NID_sha3_224),
126 : : PY_HASH_ENTRY(Py_hash_sha3_256, NULL, SN_sha3_256, NID_sha3_256),
127 : : PY_HASH_ENTRY(Py_hash_sha3_384, NULL, SN_sha3_384, NID_sha3_384),
128 : : PY_HASH_ENTRY(Py_hash_sha3_512, NULL, SN_sha3_512, NID_sha3_512),
129 : : /* sha3 shake */
130 : : PY_HASH_ENTRY(Py_hash_shake_128, NULL, SN_shake128, NID_shake128),
131 : : PY_HASH_ENTRY(Py_hash_shake_256, NULL, SN_shake256, NID_shake256),
132 : : /* blake2 digest */
133 : : PY_HASH_ENTRY(Py_hash_blake2s, "blake2s256", SN_blake2s256, NID_blake2s256),
134 : : PY_HASH_ENTRY(Py_hash_blake2b, "blake2b512", SN_blake2b512, NID_blake2b512),
135 : : PY_HASH_ENTRY(NULL, NULL, NULL, 0),
136 : : };
137 : :
138 : : static Py_uhash_t
139 : 64 : py_hashentry_t_hash_name(const void *key) {
140 : 64 : return _Py_HashBytes(key, strlen((const char *)key));
141 : : }
142 : :
143 : : static int
144 : 12 : py_hashentry_t_compare_name(const void *key1, const void *key2) {
145 : 12 : return strcmp((const char *)key1, (const char *)key2) == 0;
146 : : }
147 : :
148 : : static void
149 : 52 : py_hashentry_t_destroy_value(void *entry) {
150 : 52 : py_hashentry_t *h = (py_hashentry_t *)entry;
151 [ + + ]: 52 : if (--(h->refcnt) == 0) {
152 [ - + ]: 32 : if (h->evp != NULL) {
153 : : PY_EVP_MD_free(h->evp);
154 : 0 : h->evp = NULL;
155 : : }
156 [ + + ]: 32 : if (h->evp_nosecurity != NULL) {
157 : : PY_EVP_MD_free(h->evp_nosecurity);
158 : 12 : h->evp_nosecurity = NULL;
159 : : }
160 : 32 : PyMem_Free(entry);
161 : : }
162 : 52 : }
163 : :
164 : : static _Py_hashtable_t *
165 : 2 : py_hashentry_table_new(void) {
166 : 2 : _Py_hashtable_t *ht = _Py_hashtable_new_full(
167 : : py_hashentry_t_hash_name,
168 : : py_hashentry_t_compare_name,
169 : : NULL,
170 : : py_hashentry_t_destroy_value,
171 : : NULL
172 : : );
173 [ - + ]: 2 : if (ht == NULL) {
174 : 0 : return NULL;
175 : : }
176 : :
177 [ + + ]: 34 : for (const py_hashentry_t *h = py_hashes; h->py_name != NULL; h++) {
178 : 32 : py_hashentry_t *entry = (py_hashentry_t *)PyMem_Malloc(sizeof(py_hashentry_t));
179 [ - + ]: 32 : if (entry == NULL) {
180 : 0 : goto error;
181 : : }
182 : 32 : memcpy(entry, h, sizeof(py_hashentry_t));
183 : :
184 [ - + ]: 32 : if (_Py_hashtable_set(ht, (const void*)entry->py_name, (void*)entry) < 0) {
185 : 0 : PyMem_Free(entry);
186 : 0 : goto error;
187 : : }
188 : 32 : entry->refcnt = 1;
189 : :
190 [ + + ]: 32 : if (h->py_alias != NULL) {
191 [ - + ]: 20 : if (_Py_hashtable_set(ht, (const void*)entry->py_alias, (void*)entry) < 0) {
192 : 0 : PyMem_Free(entry);
193 : 0 : goto error;
194 : : }
195 : 20 : entry->refcnt++;
196 : : }
197 : : }
198 : :
199 : 2 : return ht;
200 : 0 : error:
201 : 0 : _Py_hashtable_destroy(ht);
202 : 0 : return NULL;
203 : : }
204 : :
205 : : /* Module state */
206 : : static PyModuleDef _hashlibmodule;
207 : :
208 : : typedef struct {
209 : : PyTypeObject *EVPtype;
210 : : PyTypeObject *HMACtype;
211 : : #ifdef PY_OPENSSL_HAS_SHAKE
212 : : PyTypeObject *EVPXOFtype;
213 : : #endif
214 : : PyObject *constructs;
215 : : PyObject *unsupported_digestmod_error;
216 : : _Py_hashtable_t *hashtable;
217 : : } _hashlibstate;
218 : :
219 : : static inline _hashlibstate*
220 : 84 : get_hashlib_state(PyObject *module)
221 : : {
222 : 84 : void *state = PyModule_GetState(module);
223 : : assert(state != NULL);
224 : 84 : return (_hashlibstate *)state;
225 : : }
226 : :
227 : : typedef struct {
228 : : PyObject_HEAD
229 : : EVP_MD_CTX *ctx; /* OpenSSL message digest context */
230 : : PyThread_type_lock lock; /* OpenSSL context lock */
231 : : } EVPobject;
232 : :
233 : : typedef struct {
234 : : PyObject_HEAD
235 : : HMAC_CTX *ctx; /* OpenSSL hmac context */
236 : : PyThread_type_lock lock; /* HMAC context lock */
237 : : } HMACobject;
238 : :
239 : : #include "clinic/_hashopenssl.c.h"
240 : : /*[clinic input]
241 : : module _hashlib
242 : : class _hashlib.HASH "EVPobject *" "((_hashlibstate *)PyModule_GetState(module))->EVPtype"
243 : : class _hashlib.HASHXOF "EVPobject *" "((_hashlibstate *)PyModule_GetState(module))->EVPXOFtype"
244 : : class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))->HMACtype"
245 : : [clinic start generated code]*/
246 : : /*[clinic end generated code: output=da39a3ee5e6b4b0d input=7df1bcf6f75cb8ef]*/
247 : :
248 : :
249 : : /* LCOV_EXCL_START */
250 : : static PyObject *
251 : : _setException(PyObject *exc, const char* altmsg, ...)
252 : : {
253 : : unsigned long errcode = ERR_peek_last_error();
254 : : const char *lib, *func, *reason;
255 : : va_list vargs;
256 : :
257 : : va_start(vargs, altmsg);
258 : : if (!errcode) {
259 : : if (altmsg == NULL) {
260 : : PyErr_SetString(exc, "no reason supplied");
261 : : } else {
262 : : PyErr_FormatV(exc, altmsg, vargs);
263 : : }
264 : : va_end(vargs);
265 : : return NULL;
266 : : }
267 : : va_end(vargs);
268 : : ERR_clear_error();
269 : :
270 : : lib = ERR_lib_error_string(errcode);
271 : : func = ERR_func_error_string(errcode);
272 : : reason = ERR_reason_error_string(errcode);
273 : :
274 : : if (lib && func) {
275 : : PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
276 : : }
277 : : else if (lib) {
278 : : PyErr_Format(exc, "[%s] %s", lib, reason);
279 : : }
280 : : else {
281 : : PyErr_SetString(exc, reason);
282 : : }
283 : : return NULL;
284 : : }
285 : : /* LCOV_EXCL_STOP */
286 : :
287 : : static PyObject*
288 : 42 : py_digest_name(const EVP_MD *md)
289 : : {
290 : 42 : int nid = EVP_MD_nid(md);
291 : 42 : const char *name = NULL;
292 : : const py_hashentry_t *h;
293 : :
294 [ + + ]: 442 : for (h = py_hashes; h->py_name != NULL; h++) {
295 [ + + ]: 432 : if (h->ossl_nid == nid) {
296 : 32 : name = h->py_name;
297 : 32 : break;
298 : : }
299 : : }
300 [ + + ]: 42 : if (name == NULL) {
301 : : /* Ignore aliased names and only use long, lowercase name. The aliases
302 : : * pollute the list and OpenSSL appears to have its own definition of
303 : : * alias as the resulting list still contains duplicate and alternate
304 : : * names for several algorithms.
305 : : */
306 : 10 : name = OBJ_nid2ln(nid);
307 [ - + ]: 10 : if (name == NULL)
308 : 0 : name = OBJ_nid2sn(nid);
309 : : }
310 : :
311 : 42 : return PyUnicode_FromString(name);
312 : : }
313 : :
314 : : /* Get EVP_MD by HID and purpose */
315 : : static PY_EVP_MD*
316 : 12 : py_digest_by_name(PyObject *module, const char *name, enum Py_hash_type py_ht)
317 : : {
318 : 12 : PY_EVP_MD *digest = NULL;
319 : 12 : _hashlibstate *state = get_hashlib_state(module);
320 : 12 : py_hashentry_t *entry = (py_hashentry_t *)_Py_hashtable_get(
321 : : state->hashtable, (const void*)name
322 : : );
323 : :
324 [ + - ]: 12 : if (entry != NULL) {
325 [ - + - ]: 12 : switch (py_ht) {
326 : 0 : case Py_ht_evp:
327 : : case Py_ht_mac:
328 : : case Py_ht_pbkdf2:
329 [ # # ]: 0 : if (entry->evp == NULL) {
330 : 0 : entry->evp = PY_EVP_MD_fetch(entry->ossl_name, NULL);
331 : : }
332 : 0 : digest = entry->evp;
333 : 0 : break;
334 : 12 : case Py_ht_evp_nosecurity:
335 [ + - ]: 12 : if (entry->evp_nosecurity == NULL) {
336 : 12 : entry->evp_nosecurity = PY_EVP_MD_fetch(entry->ossl_name, "-fips");
337 : : }
338 : 12 : digest = entry->evp_nosecurity;
339 : 12 : break;
340 : : }
341 : 12 : if (digest != NULL) {
342 : : PY_EVP_MD_up_ref(digest);
343 : : }
344 : : } else {
345 : : // Fall back for looking up an unindexed OpenSSL specific name.
346 [ # # # ]: 0 : switch (py_ht) {
347 : 0 : case Py_ht_evp:
348 : : case Py_ht_mac:
349 : : case Py_ht_pbkdf2:
350 : 0 : digest = PY_EVP_MD_fetch(name, NULL);
351 : 0 : break;
352 : 0 : case Py_ht_evp_nosecurity:
353 : 0 : digest = PY_EVP_MD_fetch(name, "-fips");
354 : 0 : break;
355 : : }
356 : 12 : }
357 [ - + ]: 12 : if (digest == NULL) {
358 : 0 : _setException(PyExc_ValueError, "unsupported hash type %s", name);
359 : 0 : return NULL;
360 : : }
361 : 12 : return digest;
362 : : }
363 : :
364 : : /* Get digest EVP from object
365 : : *
366 : : * * string
367 : : * * _hashopenssl builtin function
368 : : *
369 : : * on error returns NULL with exception set.
370 : : */
371 : : static PY_EVP_MD*
372 : 0 : py_digest_by_digestmod(PyObject *module, PyObject *digestmod, enum Py_hash_type py_ht) {
373 : : PY_EVP_MD* evp;
374 : 0 : PyObject *name_obj = NULL;
375 : : const char *name;
376 : :
377 [ # # ]: 0 : if (PyUnicode_Check(digestmod)) {
378 : 0 : name_obj = digestmod;
379 : : } else {
380 : 0 : _hashlibstate *state = get_hashlib_state(module);
381 : : // borrowed ref
382 : 0 : name_obj = PyDict_GetItem(state->constructs, digestmod);
383 : : }
384 [ # # ]: 0 : if (name_obj == NULL) {
385 : 0 : _hashlibstate *state = get_hashlib_state(module);
386 : 0 : PyErr_Clear();
387 : 0 : PyErr_Format(
388 : : state->unsupported_digestmod_error,
389 : : "Unsupported digestmod %R", digestmod);
390 : 0 : return NULL;
391 : : }
392 : :
393 : 0 : name = PyUnicode_AsUTF8(name_obj);
394 [ # # ]: 0 : if (name == NULL) {
395 : 0 : return NULL;
396 : : }
397 : :
398 : 0 : evp = py_digest_by_name(module, name, py_ht);
399 [ # # ]: 0 : if (evp == NULL) {
400 : 0 : return NULL;
401 : : }
402 : :
403 : 0 : return evp;
404 : : }
405 : :
406 : : static EVPobject *
407 : 12 : newEVPobject(PyTypeObject *type)
408 : : {
409 : 12 : EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, type);
410 [ - + ]: 12 : if (retval == NULL) {
411 : 0 : return NULL;
412 : : }
413 : :
414 : 12 : retval->lock = NULL;
415 : :
416 : 12 : retval->ctx = EVP_MD_CTX_new();
417 [ - + ]: 12 : if (retval->ctx == NULL) {
418 : 0 : Py_DECREF(retval);
419 : 0 : PyErr_NoMemory();
420 : 0 : return NULL;
421 : : }
422 : :
423 : 12 : return retval;
424 : : }
425 : :
426 : : static int
427 : 0 : EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
428 : : {
429 : : unsigned int process;
430 : 0 : const unsigned char *cp = (const unsigned char *)vp;
431 [ # # ]: 0 : while (0 < len) {
432 [ # # ]: 0 : if (len > (Py_ssize_t)MUNCH_SIZE)
433 : 0 : process = MUNCH_SIZE;
434 : : else
435 : 0 : process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
436 [ # # ]: 0 : if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) {
437 : 0 : _setException(PyExc_ValueError, NULL);
438 : 0 : return -1;
439 : : }
440 : 0 : len -= process;
441 : 0 : cp += process;
442 : : }
443 : 0 : return 0;
444 : : }
445 : :
446 : : /* Internal methods for a hash object */
447 : :
448 : : static void
449 : 12 : EVP_dealloc(EVPobject *self)
450 : : {
451 : 12 : PyTypeObject *tp = Py_TYPE(self);
452 [ - + ]: 12 : if (self->lock != NULL)
453 : 0 : PyThread_free_lock(self->lock);
454 : 12 : EVP_MD_CTX_free(self->ctx);
455 : 12 : PyObject_Free(self);
456 : 12 : Py_DECREF(tp);
457 : 12 : }
458 : :
459 : : static int
460 : 0 : locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
461 : : {
462 : : int result;
463 [ # # # # ]: 0 : ENTER_HASHLIB(self);
464 : 0 : result = EVP_MD_CTX_copy(new_ctx_p, self->ctx);
465 [ # # ]: 0 : LEAVE_HASHLIB(self);
466 : 0 : return result;
467 : : }
468 : :
469 : : /* External methods for a hash object */
470 : :
471 : : /*[clinic input]
472 : : _hashlib.HASH.copy as EVP_copy
473 : :
474 : : Return a copy of the hash object.
475 : : [clinic start generated code]*/
476 : :
477 : : static PyObject *
478 : 0 : EVP_copy_impl(EVPobject *self)
479 : : /*[clinic end generated code: output=b370c21cdb8ca0b4 input=31455b6a3e638069]*/
480 : : {
481 : : EVPobject *newobj;
482 : :
483 [ # # ]: 0 : if ((newobj = newEVPobject(Py_TYPE(self))) == NULL)
484 : 0 : return NULL;
485 : :
486 [ # # ]: 0 : if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) {
487 : 0 : Py_DECREF(newobj);
488 : 0 : return _setException(PyExc_ValueError, NULL);
489 : : }
490 : 0 : return (PyObject *)newobj;
491 : : }
492 : :
493 : : /*[clinic input]
494 : : _hashlib.HASH.digest as EVP_digest
495 : :
496 : : Return the digest value as a bytes object.
497 : : [clinic start generated code]*/
498 : :
499 : : static PyObject *
500 : 0 : EVP_digest_impl(EVPobject *self)
501 : : /*[clinic end generated code: output=0f6a3a0da46dc12d input=03561809a419bf00]*/
502 : : {
503 : : unsigned char digest[EVP_MAX_MD_SIZE];
504 : : EVP_MD_CTX *temp_ctx;
505 : : PyObject *retval;
506 : : unsigned int digest_size;
507 : :
508 : 0 : temp_ctx = EVP_MD_CTX_new();
509 [ # # ]: 0 : if (temp_ctx == NULL) {
510 : 0 : PyErr_NoMemory();
511 : 0 : return NULL;
512 : : }
513 : :
514 [ # # ]: 0 : if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
515 : 0 : return _setException(PyExc_ValueError, NULL);
516 : : }
517 : 0 : digest_size = EVP_MD_CTX_size(temp_ctx);
518 [ # # ]: 0 : if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
519 : 0 : _setException(PyExc_ValueError, NULL);
520 : 0 : return NULL;
521 : : }
522 : :
523 : 0 : retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
524 : 0 : EVP_MD_CTX_free(temp_ctx);
525 : 0 : return retval;
526 : : }
527 : :
528 : : /*[clinic input]
529 : : _hashlib.HASH.hexdigest as EVP_hexdigest
530 : :
531 : : Return the digest value as a string of hexadecimal digits.
532 : : [clinic start generated code]*/
533 : :
534 : : static PyObject *
535 : 0 : EVP_hexdigest_impl(EVPobject *self)
536 : : /*[clinic end generated code: output=18e6decbaf197296 input=aff9cf0e4c741a9a]*/
537 : : {
538 : : unsigned char digest[EVP_MAX_MD_SIZE];
539 : : EVP_MD_CTX *temp_ctx;
540 : : unsigned int digest_size;
541 : :
542 : 0 : temp_ctx = EVP_MD_CTX_new();
543 [ # # ]: 0 : if (temp_ctx == NULL) {
544 : 0 : PyErr_NoMemory();
545 : 0 : return NULL;
546 : : }
547 : :
548 : : /* Get the raw (binary) digest value */
549 [ # # ]: 0 : if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
550 : 0 : return _setException(PyExc_ValueError, NULL);
551 : : }
552 : 0 : digest_size = EVP_MD_CTX_size(temp_ctx);
553 [ # # ]: 0 : if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
554 : 0 : _setException(PyExc_ValueError, NULL);
555 : 0 : return NULL;
556 : : }
557 : :
558 : 0 : EVP_MD_CTX_free(temp_ctx);
559 : :
560 : 0 : return _Py_strhex((const char *)digest, (Py_ssize_t)digest_size);
561 : : }
562 : :
563 : : /*[clinic input]
564 : : _hashlib.HASH.update as EVP_update
565 : :
566 : : obj: object
567 : : /
568 : :
569 : : Update this hash object's state with the provided string.
570 : : [clinic start generated code]*/
571 : :
572 : : static PyObject *
573 : 0 : EVP_update(EVPobject *self, PyObject *obj)
574 : : /*[clinic end generated code: output=ec1d55ed2432e966 input=9b30ec848f015501]*/
575 : : {
576 : : int result;
577 : : Py_buffer view;
578 : :
579 [ # # # # : 0 : GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
# # # # ]
580 : :
581 [ # # # # ]: 0 : if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
582 : 0 : self->lock = PyThread_allocate_lock();
583 : : /* fail? lock = NULL and we fail over to non-threaded code. */
584 : : }
585 : :
586 [ # # ]: 0 : if (self->lock != NULL) {
587 : 0 : Py_BEGIN_ALLOW_THREADS
588 : 0 : PyThread_acquire_lock(self->lock, 1);
589 : 0 : result = EVP_hash(self, view.buf, view.len);
590 : 0 : PyThread_release_lock(self->lock);
591 : 0 : Py_END_ALLOW_THREADS
592 : : } else {
593 : 0 : result = EVP_hash(self, view.buf, view.len);
594 : : }
595 : :
596 : 0 : PyBuffer_Release(&view);
597 : :
598 [ # # ]: 0 : if (result == -1)
599 : 0 : return NULL;
600 : 0 : Py_RETURN_NONE;
601 : : }
602 : :
603 : : static PyMethodDef EVP_methods[] = {
604 : : EVP_UPDATE_METHODDEF
605 : : EVP_DIGEST_METHODDEF
606 : : EVP_HEXDIGEST_METHODDEF
607 : : EVP_COPY_METHODDEF
608 : : {NULL, NULL} /* sentinel */
609 : : };
610 : :
611 : : static PyObject *
612 : 0 : EVP_get_block_size(EVPobject *self, void *closure)
613 : : {
614 : : long block_size;
615 : 0 : block_size = EVP_MD_CTX_block_size(self->ctx);
616 : 0 : return PyLong_FromLong(block_size);
617 : : }
618 : :
619 : : static PyObject *
620 : 0 : EVP_get_digest_size(EVPobject *self, void *closure)
621 : : {
622 : : long size;
623 : 0 : size = EVP_MD_CTX_size(self->ctx);
624 : 0 : return PyLong_FromLong(size);
625 : : }
626 : :
627 : : static PyObject *
628 : 0 : EVP_get_name(EVPobject *self, void *closure)
629 : : {
630 : 0 : return py_digest_name(EVP_MD_CTX_md(self->ctx));
631 : : }
632 : :
633 : : static PyGetSetDef EVP_getseters[] = {
634 : : {"digest_size",
635 : : (getter)EVP_get_digest_size, NULL,
636 : : NULL,
637 : : NULL},
638 : : {"block_size",
639 : : (getter)EVP_get_block_size, NULL,
640 : : NULL,
641 : : NULL},
642 : : {"name",
643 : : (getter)EVP_get_name, NULL,
644 : : NULL,
645 : : PyDoc_STR("algorithm name.")},
646 : : {NULL} /* Sentinel */
647 : : };
648 : :
649 : :
650 : : static PyObject *
651 : 0 : EVP_repr(EVPobject *self)
652 : : {
653 : : PyObject *name_obj, *repr;
654 : 0 : name_obj = py_digest_name(EVP_MD_CTX_md(self->ctx));
655 [ # # ]: 0 : if (!name_obj) {
656 : 0 : return NULL;
657 : : }
658 : 0 : repr = PyUnicode_FromFormat("<%U %s object @ %p>",
659 : 0 : name_obj, Py_TYPE(self)->tp_name, self);
660 : 0 : Py_DECREF(name_obj);
661 : 0 : return repr;
662 : : }
663 : :
664 : : PyDoc_STRVAR(hashtype_doc,
665 : : "HASH(name, string=b\'\')\n"
666 : : "--\n"
667 : : "\n"
668 : : "A hash is an object used to calculate a checksum of a string of information.\n"
669 : : "\n"
670 : : "Methods:\n"
671 : : "\n"
672 : : "update() -- updates the current digest with an additional string\n"
673 : : "digest() -- return the current digest value\n"
674 : : "hexdigest() -- return the current digest as a string of hexadecimal digits\n"
675 : : "copy() -- return a copy of the current hash object\n"
676 : : "\n"
677 : : "Attributes:\n"
678 : : "\n"
679 : : "name -- the hash algorithm being used by this object\n"
680 : : "digest_size -- number of bytes in this hashes output");
681 : :
682 : : static PyType_Slot EVPtype_slots[] = {
683 : : {Py_tp_dealloc, EVP_dealloc},
684 : : {Py_tp_repr, EVP_repr},
685 : : {Py_tp_doc, (char *)hashtype_doc},
686 : : {Py_tp_methods, EVP_methods},
687 : : {Py_tp_getset, EVP_getseters},
688 : : {0, 0},
689 : : };
690 : :
691 : : static PyType_Spec EVPtype_spec = {
692 : : "_hashlib.HASH", /*tp_name*/
693 : : sizeof(EVPobject), /*tp_basicsize*/
694 : : 0, /*tp_itemsize*/
695 : : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
696 : : EVPtype_slots
697 : : };
698 : :
699 : : #ifdef PY_OPENSSL_HAS_SHAKE
700 : :
701 : : /*[clinic input]
702 : : _hashlib.HASHXOF.digest as EVPXOF_digest
703 : :
704 : : length: Py_ssize_t
705 : :
706 : : Return the digest value as a bytes object.
707 : : [clinic start generated code]*/
708 : :
709 : : static PyObject *
710 : 0 : EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
711 : : /*[clinic end generated code: output=ef9320c23280efad input=816a6537cea3d1db]*/
712 : : {
713 : : EVP_MD_CTX *temp_ctx;
714 : 0 : PyObject *retval = PyBytes_FromStringAndSize(NULL, length);
715 : :
716 [ # # ]: 0 : if (retval == NULL) {
717 : 0 : return NULL;
718 : : }
719 : :
720 : 0 : temp_ctx = EVP_MD_CTX_new();
721 [ # # ]: 0 : if (temp_ctx == NULL) {
722 : 0 : Py_DECREF(retval);
723 : 0 : PyErr_NoMemory();
724 : 0 : return NULL;
725 : : }
726 : :
727 [ # # ]: 0 : if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
728 : 0 : Py_DECREF(retval);
729 : 0 : EVP_MD_CTX_free(temp_ctx);
730 : 0 : return _setException(PyExc_ValueError, NULL);
731 : : }
732 [ # # ]: 0 : if (!EVP_DigestFinalXOF(temp_ctx,
733 : 0 : (unsigned char*)PyBytes_AS_STRING(retval),
734 : : length)) {
735 : 0 : Py_DECREF(retval);
736 : 0 : EVP_MD_CTX_free(temp_ctx);
737 : 0 : _setException(PyExc_ValueError, NULL);
738 : 0 : return NULL;
739 : : }
740 : :
741 : 0 : EVP_MD_CTX_free(temp_ctx);
742 : 0 : return retval;
743 : : }
744 : :
745 : : /*[clinic input]
746 : : _hashlib.HASHXOF.hexdigest as EVPXOF_hexdigest
747 : :
748 : : length: Py_ssize_t
749 : :
750 : : Return the digest value as a string of hexadecimal digits.
751 : : [clinic start generated code]*/
752 : :
753 : : static PyObject *
754 : 0 : EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
755 : : /*[clinic end generated code: output=eb3e6ee7788bf5b2 input=5f9d6a8f269e34df]*/
756 : : {
757 : : unsigned char *digest;
758 : : EVP_MD_CTX *temp_ctx;
759 : : PyObject *retval;
760 : :
761 : 0 : digest = (unsigned char*)PyMem_Malloc(length);
762 [ # # ]: 0 : if (digest == NULL) {
763 : 0 : PyErr_NoMemory();
764 : 0 : return NULL;
765 : : }
766 : :
767 : 0 : temp_ctx = EVP_MD_CTX_new();
768 [ # # ]: 0 : if (temp_ctx == NULL) {
769 : 0 : PyMem_Free(digest);
770 : 0 : PyErr_NoMemory();
771 : 0 : return NULL;
772 : : }
773 : :
774 : : /* Get the raw (binary) digest value */
775 [ # # ]: 0 : if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
776 : 0 : PyMem_Free(digest);
777 : 0 : EVP_MD_CTX_free(temp_ctx);
778 : 0 : return _setException(PyExc_ValueError, NULL);
779 : : }
780 [ # # ]: 0 : if (!EVP_DigestFinalXOF(temp_ctx, digest, length)) {
781 : 0 : PyMem_Free(digest);
782 : 0 : EVP_MD_CTX_free(temp_ctx);
783 : 0 : _setException(PyExc_ValueError, NULL);
784 : 0 : return NULL;
785 : : }
786 : :
787 : 0 : EVP_MD_CTX_free(temp_ctx);
788 : :
789 : 0 : retval = _Py_strhex((const char *)digest, length);
790 : 0 : PyMem_Free(digest);
791 : 0 : return retval;
792 : : }
793 : :
794 : : static PyMethodDef EVPXOF_methods[] = {
795 : : EVPXOF_DIGEST_METHODDEF
796 : : EVPXOF_HEXDIGEST_METHODDEF
797 : : {NULL, NULL} /* sentinel */
798 : : };
799 : :
800 : :
801 : : static PyObject *
802 : 0 : EVPXOF_get_digest_size(EVPobject *self, void *closure)
803 : : {
804 : 0 : return PyLong_FromLong(0);
805 : : }
806 : :
807 : : static PyGetSetDef EVPXOF_getseters[] = {
808 : : {"digest_size",
809 : : (getter)EVPXOF_get_digest_size, NULL,
810 : : NULL,
811 : : NULL},
812 : : {NULL} /* Sentinel */
813 : : };
814 : :
815 : : PyDoc_STRVAR(hashxoftype_doc,
816 : : "HASHXOF(name, string=b\'\')\n"
817 : : "--\n"
818 : : "\n"
819 : : "A hash is an object used to calculate a checksum of a string of information.\n"
820 : : "\n"
821 : : "Methods:\n"
822 : : "\n"
823 : : "update() -- updates the current digest with an additional string\n"
824 : : "digest(length) -- return the current digest value\n"
825 : : "hexdigest(length) -- return the current digest as a string of hexadecimal digits\n"
826 : : "copy() -- return a copy of the current hash object\n"
827 : : "\n"
828 : : "Attributes:\n"
829 : : "\n"
830 : : "name -- the hash algorithm being used by this object\n"
831 : : "digest_size -- number of bytes in this hashes output");
832 : :
833 : : static PyType_Slot EVPXOFtype_slots[] = {
834 : : {Py_tp_doc, (char *)hashxoftype_doc},
835 : : {Py_tp_methods, EVPXOF_methods},
836 : : {Py_tp_getset, EVPXOF_getseters},
837 : : {0, 0},
838 : : };
839 : :
840 : : static PyType_Spec EVPXOFtype_spec = {
841 : : "_hashlib.HASHXOF", /*tp_name*/
842 : : sizeof(EVPobject), /*tp_basicsize*/
843 : : 0, /*tp_itemsize*/
844 : : Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
845 : : EVPXOFtype_slots
846 : : };
847 : :
848 : :
849 : : #endif
850 : :
851 : : static PyObject*
852 : 12 : py_evp_fromname(PyObject *module, const char *digestname, PyObject *data_obj,
853 : : int usedforsecurity)
854 : : {
855 : 12 : Py_buffer view = { 0 };
856 : 12 : PY_EVP_MD *digest = NULL;
857 : : PyTypeObject *type;
858 : 12 : EVPobject *self = NULL;
859 : :
860 [ - + ]: 12 : if (data_obj != NULL) {
861 [ # # # # : 0 : GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
# # # # ]
862 : : }
863 : :
864 : 12 : digest = py_digest_by_name(
865 : : module, digestname, usedforsecurity ? Py_ht_evp : Py_ht_evp_nosecurity
866 : : );
867 [ - + ]: 12 : if (digest == NULL) {
868 : 0 : goto exit;
869 : : }
870 : :
871 [ + + ]: 12 : if ((EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) == EVP_MD_FLAG_XOF) {
872 : 2 : type = get_hashlib_state(module)->EVPXOFtype;
873 : : } else {
874 : 10 : type = get_hashlib_state(module)->EVPtype;
875 : : }
876 : :
877 : 12 : self = newEVPobject(type);
878 [ - + ]: 12 : if (self == NULL) {
879 : 0 : goto exit;
880 : : }
881 : :
882 : : #if defined(EVP_MD_CTX_FLAG_NON_FIPS_ALLOW) && OPENSSL_VERSION_NUMBER < 0x30000000L
883 : : // In OpenSSL 1.1.1 the non FIPS allowed flag is context specific while
884 : : // in 3.0.0 it is a different EVP_MD provider.
885 [ + - ]: 12 : if (!usedforsecurity) {
886 : 12 : EVP_MD_CTX_set_flags(self->ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
887 : : }
888 : : #endif
889 : :
890 : 12 : int result = EVP_DigestInit_ex(self->ctx, digest, NULL);
891 [ - + ]: 12 : if (!result) {
892 : 0 : _setException(PyExc_ValueError, NULL);
893 [ # # ]: 0 : Py_CLEAR(self);
894 : 0 : goto exit;
895 : : }
896 : :
897 [ + - - - ]: 12 : if (view.buf && view.len) {
898 [ # # ]: 0 : if (view.len >= HASHLIB_GIL_MINSIZE) {
899 : 0 : Py_BEGIN_ALLOW_THREADS
900 : 0 : result = EVP_hash(self, view.buf, view.len);
901 : 0 : Py_END_ALLOW_THREADS
902 : : } else {
903 : 0 : result = EVP_hash(self, view.buf, view.len);
904 : : }
905 [ # # ]: 0 : if (result == -1) {
906 [ # # ]: 0 : Py_CLEAR(self);
907 : 0 : goto exit;
908 : : }
909 : : }
910 : :
911 : 12 : exit:
912 [ - + ]: 12 : if (data_obj != NULL) {
913 : 0 : PyBuffer_Release(&view);
914 : : }
915 : : if (digest != NULL) {
916 : : PY_EVP_MD_free(digest);
917 : : }
918 : :
919 : 12 : return (PyObject *)self;
920 : : }
921 : :
922 : :
923 : : /* The module-level function: new() */
924 : :
925 : : /*[clinic input]
926 : : _hashlib.new as EVP_new
927 : :
928 : : name as name_obj: object
929 : : string as data_obj: object(c_default="NULL") = b''
930 : : *
931 : : usedforsecurity: bool = True
932 : :
933 : : Return a new hash object using the named algorithm.
934 : :
935 : : An optional string argument may be provided and will be
936 : : automatically hashed.
937 : :
938 : : The MD5 and SHA1 algorithms are always supported.
939 : : [clinic start generated code]*/
940 : :
941 : : static PyObject *
942 : 0 : EVP_new_impl(PyObject *module, PyObject *name_obj, PyObject *data_obj,
943 : : int usedforsecurity)
944 : : /*[clinic end generated code: output=ddd5053f92dffe90 input=c24554d0337be1b0]*/
945 : : {
946 : : char *name;
947 [ # # ]: 0 : if (!PyArg_Parse(name_obj, "s", &name)) {
948 : 0 : PyErr_SetString(PyExc_TypeError, "name must be a string");
949 : 0 : return NULL;
950 : : }
951 : 0 : return py_evp_fromname(module, name, data_obj, usedforsecurity);
952 : : }
953 : :
954 : :
955 : : /*[clinic input]
956 : : _hashlib.openssl_md5
957 : :
958 : : string as data_obj: object(py_default="b''") = NULL
959 : : *
960 : : usedforsecurity: bool = True
961 : :
962 : : Returns a md5 hash object; optionally initialized with a string
963 : :
964 : : [clinic start generated code]*/
965 : :
966 : : static PyObject *
967 : 1 : _hashlib_openssl_md5_impl(PyObject *module, PyObject *data_obj,
968 : : int usedforsecurity)
969 : : /*[clinic end generated code: output=87b0186440a44f8c input=990e36d5e689b16e]*/
970 : : {
971 : 1 : return py_evp_fromname(module, Py_hash_md5, data_obj, usedforsecurity);
972 : : }
973 : :
974 : :
975 : : /*[clinic input]
976 : : _hashlib.openssl_sha1
977 : :
978 : : string as data_obj: object(py_default="b''") = NULL
979 : : *
980 : : usedforsecurity: bool = True
981 : :
982 : : Returns a sha1 hash object; optionally initialized with a string
983 : :
984 : : [clinic start generated code]*/
985 : :
986 : : static PyObject *
987 : 1 : _hashlib_openssl_sha1_impl(PyObject *module, PyObject *data_obj,
988 : : int usedforsecurity)
989 : : /*[clinic end generated code: output=6813024cf690670d input=948f2f4b6deabc10]*/
990 : : {
991 : 1 : return py_evp_fromname(module, Py_hash_sha1, data_obj, usedforsecurity);
992 : : }
993 : :
994 : :
995 : : /*[clinic input]
996 : : _hashlib.openssl_sha224
997 : :
998 : : string as data_obj: object(py_default="b''") = NULL
999 : : *
1000 : : usedforsecurity: bool = True
1001 : :
1002 : : Returns a sha224 hash object; optionally initialized with a string
1003 : :
1004 : : [clinic start generated code]*/
1005 : :
1006 : : static PyObject *
1007 : 1 : _hashlib_openssl_sha224_impl(PyObject *module, PyObject *data_obj,
1008 : : int usedforsecurity)
1009 : : /*[clinic end generated code: output=a2dfe7cc4eb14ebb input=f9272821fadca505]*/
1010 : : {
1011 : 1 : return py_evp_fromname(module, Py_hash_sha224, data_obj, usedforsecurity);
1012 : : }
1013 : :
1014 : :
1015 : : /*[clinic input]
1016 : : _hashlib.openssl_sha256
1017 : :
1018 : : string as data_obj: object(py_default="b''") = NULL
1019 : : *
1020 : : usedforsecurity: bool = True
1021 : :
1022 : : Returns a sha256 hash object; optionally initialized with a string
1023 : :
1024 : : [clinic start generated code]*/
1025 : :
1026 : : static PyObject *
1027 : 1 : _hashlib_openssl_sha256_impl(PyObject *module, PyObject *data_obj,
1028 : : int usedforsecurity)
1029 : : /*[clinic end generated code: output=1f874a34870f0a68 input=549fad9d2930d4c5]*/
1030 : : {
1031 : 1 : return py_evp_fromname(module, Py_hash_sha256, data_obj, usedforsecurity);
1032 : : }
1033 : :
1034 : :
1035 : : /*[clinic input]
1036 : : _hashlib.openssl_sha384
1037 : :
1038 : : string as data_obj: object(py_default="b''") = NULL
1039 : : *
1040 : : usedforsecurity: bool = True
1041 : :
1042 : : Returns a sha384 hash object; optionally initialized with a string
1043 : :
1044 : : [clinic start generated code]*/
1045 : :
1046 : : static PyObject *
1047 : 1 : _hashlib_openssl_sha384_impl(PyObject *module, PyObject *data_obj,
1048 : : int usedforsecurity)
1049 : : /*[clinic end generated code: output=58529eff9ca457b2 input=48601a6e3bf14ad7]*/
1050 : : {
1051 : 1 : return py_evp_fromname(module, Py_hash_sha384, data_obj, usedforsecurity);
1052 : : }
1053 : :
1054 : :
1055 : : /*[clinic input]
1056 : : _hashlib.openssl_sha512
1057 : :
1058 : : string as data_obj: object(py_default="b''") = NULL
1059 : : *
1060 : : usedforsecurity: bool = True
1061 : :
1062 : : Returns a sha512 hash object; optionally initialized with a string
1063 : :
1064 : : [clinic start generated code]*/
1065 : :
1066 : : static PyObject *
1067 : 1 : _hashlib_openssl_sha512_impl(PyObject *module, PyObject *data_obj,
1068 : : int usedforsecurity)
1069 : : /*[clinic end generated code: output=2c744c9e4a40d5f6 input=c5c46a2a817aa98f]*/
1070 : : {
1071 : 1 : return py_evp_fromname(module, Py_hash_sha512, data_obj, usedforsecurity);
1072 : : }
1073 : :
1074 : :
1075 : : #ifdef PY_OPENSSL_HAS_SHA3
1076 : :
1077 : : /*[clinic input]
1078 : : _hashlib.openssl_sha3_224
1079 : :
1080 : : string as data_obj: object(py_default="b''") = NULL
1081 : : *
1082 : : usedforsecurity: bool = True
1083 : :
1084 : : Returns a sha3-224 hash object; optionally initialized with a string
1085 : :
1086 : : [clinic start generated code]*/
1087 : :
1088 : : static PyObject *
1089 : 1 : _hashlib_openssl_sha3_224_impl(PyObject *module, PyObject *data_obj,
1090 : : int usedforsecurity)
1091 : : /*[clinic end generated code: output=144641c1d144b974 input=e3a01b2888916157]*/
1092 : : {
1093 : 1 : return py_evp_fromname(module, Py_hash_sha3_224, data_obj, usedforsecurity);
1094 : : }
1095 : :
1096 : : /*[clinic input]
1097 : : _hashlib.openssl_sha3_256
1098 : :
1099 : : string as data_obj: object(py_default="b''") = NULL
1100 : : *
1101 : : usedforsecurity: bool = True
1102 : :
1103 : : Returns a sha3-256 hash object; optionally initialized with a string
1104 : :
1105 : : [clinic start generated code]*/
1106 : :
1107 : : static PyObject *
1108 : 1 : _hashlib_openssl_sha3_256_impl(PyObject *module, PyObject *data_obj,
1109 : : int usedforsecurity)
1110 : : /*[clinic end generated code: output=c61f1ab772d06668 input=e2908126c1b6deed]*/
1111 : : {
1112 : 1 : return py_evp_fromname(module, Py_hash_sha3_256, data_obj , usedforsecurity);
1113 : : }
1114 : :
1115 : : /*[clinic input]
1116 : : _hashlib.openssl_sha3_384
1117 : :
1118 : : string as data_obj: object(py_default="b''") = NULL
1119 : : *
1120 : : usedforsecurity: bool = True
1121 : :
1122 : : Returns a sha3-384 hash object; optionally initialized with a string
1123 : :
1124 : : [clinic start generated code]*/
1125 : :
1126 : : static PyObject *
1127 : 1 : _hashlib_openssl_sha3_384_impl(PyObject *module, PyObject *data_obj,
1128 : : int usedforsecurity)
1129 : : /*[clinic end generated code: output=f68e4846858cf0ee input=ec0edf5c792f8252]*/
1130 : : {
1131 : 1 : return py_evp_fromname(module, Py_hash_sha3_384, data_obj , usedforsecurity);
1132 : : }
1133 : :
1134 : : /*[clinic input]
1135 : : _hashlib.openssl_sha3_512
1136 : :
1137 : : string as data_obj: object(py_default="b''") = NULL
1138 : : *
1139 : : usedforsecurity: bool = True
1140 : :
1141 : : Returns a sha3-512 hash object; optionally initialized with a string
1142 : :
1143 : : [clinic start generated code]*/
1144 : :
1145 : : static PyObject *
1146 : 1 : _hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data_obj,
1147 : : int usedforsecurity)
1148 : : /*[clinic end generated code: output=2eede478c159354a input=64e2cc0c094d56f4]*/
1149 : : {
1150 : 1 : return py_evp_fromname(module, Py_hash_sha3_512, data_obj , usedforsecurity);
1151 : : }
1152 : : #endif /* PY_OPENSSL_HAS_SHA3 */
1153 : :
1154 : : #ifdef PY_OPENSSL_HAS_SHAKE
1155 : : /*[clinic input]
1156 : : _hashlib.openssl_shake_128
1157 : :
1158 : : string as data_obj: object(py_default="b''") = NULL
1159 : : *
1160 : : usedforsecurity: bool = True
1161 : :
1162 : : Returns a shake-128 variable hash object; optionally initialized with a string
1163 : :
1164 : : [clinic start generated code]*/
1165 : :
1166 : : static PyObject *
1167 : 1 : _hashlib_openssl_shake_128_impl(PyObject *module, PyObject *data_obj,
1168 : : int usedforsecurity)
1169 : : /*[clinic end generated code: output=bc49cdd8ada1fa97 input=6c9d67440eb33ec8]*/
1170 : : {
1171 : 1 : return py_evp_fromname(module, Py_hash_shake_128, data_obj , usedforsecurity);
1172 : : }
1173 : :
1174 : : /*[clinic input]
1175 : : _hashlib.openssl_shake_256
1176 : :
1177 : : string as data_obj: object(py_default="b''") = NULL
1178 : : *
1179 : : usedforsecurity: bool = True
1180 : :
1181 : : Returns a shake-256 variable hash object; optionally initialized with a string
1182 : :
1183 : : [clinic start generated code]*/
1184 : :
1185 : : static PyObject *
1186 : 1 : _hashlib_openssl_shake_256_impl(PyObject *module, PyObject *data_obj,
1187 : : int usedforsecurity)
1188 : : /*[clinic end generated code: output=358d213be8852df7 input=479cbe9fefd4a9f8]*/
1189 : : {
1190 : 1 : return py_evp_fromname(module, Py_hash_shake_256, data_obj , usedforsecurity);
1191 : : }
1192 : : #endif /* PY_OPENSSL_HAS_SHAKE */
1193 : :
1194 : : /*[clinic input]
1195 : : _hashlib.pbkdf2_hmac as pbkdf2_hmac
1196 : :
1197 : : hash_name: str
1198 : : password: Py_buffer
1199 : : salt: Py_buffer
1200 : : iterations: long
1201 : : dklen as dklen_obj: object = None
1202 : :
1203 : : Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as pseudorandom function.
1204 : : [clinic start generated code]*/
1205 : :
1206 : : static PyObject *
1207 : 0 : pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
1208 : : Py_buffer *password, Py_buffer *salt, long iterations,
1209 : : PyObject *dklen_obj)
1210 : : /*[clinic end generated code: output=144b76005416599b input=ed3ab0d2d28b5d5c]*/
1211 : : {
1212 : 0 : PyObject *key_obj = NULL;
1213 : : char *key;
1214 : : long dklen;
1215 : : int retval;
1216 : :
1217 : 0 : PY_EVP_MD *digest = py_digest_by_name(module, hash_name, Py_ht_pbkdf2);
1218 [ # # ]: 0 : if (digest == NULL) {
1219 : 0 : goto end;
1220 : : }
1221 : :
1222 [ # # ]: 0 : if (password->len > INT_MAX) {
1223 : 0 : PyErr_SetString(PyExc_OverflowError,
1224 : : "password is too long.");
1225 : 0 : goto end;
1226 : : }
1227 : :
1228 [ # # ]: 0 : if (salt->len > INT_MAX) {
1229 : 0 : PyErr_SetString(PyExc_OverflowError,
1230 : : "salt is too long.");
1231 : 0 : goto end;
1232 : : }
1233 : :
1234 [ # # ]: 0 : if (iterations < 1) {
1235 : 0 : PyErr_SetString(PyExc_ValueError,
1236 : : "iteration value must be greater than 0.");
1237 : 0 : goto end;
1238 : : }
1239 [ # # ]: 0 : if (iterations > INT_MAX) {
1240 : 0 : PyErr_SetString(PyExc_OverflowError,
1241 : : "iteration value is too great.");
1242 : 0 : goto end;
1243 : : }
1244 : :
1245 [ # # ]: 0 : if (dklen_obj == Py_None) {
1246 : 0 : dklen = EVP_MD_size(digest);
1247 : : } else {
1248 : 0 : dklen = PyLong_AsLong(dklen_obj);
1249 [ # # # # ]: 0 : if ((dklen == -1) && PyErr_Occurred()) {
1250 : 0 : goto end;
1251 : : }
1252 : : }
1253 [ # # ]: 0 : if (dklen < 1) {
1254 : 0 : PyErr_SetString(PyExc_ValueError,
1255 : : "key length must be greater than 0.");
1256 : 0 : goto end;
1257 : : }
1258 [ # # ]: 0 : if (dklen > INT_MAX) {
1259 : : /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
1260 : 0 : PyErr_SetString(PyExc_OverflowError,
1261 : : "key length is too great.");
1262 : 0 : goto end;
1263 : : }
1264 : :
1265 : 0 : key_obj = PyBytes_FromStringAndSize(NULL, dklen);
1266 [ # # ]: 0 : if (key_obj == NULL) {
1267 : 0 : goto end;
1268 : : }
1269 : 0 : key = PyBytes_AS_STRING(key_obj);
1270 : :
1271 : 0 : Py_BEGIN_ALLOW_THREADS
1272 : 0 : retval = PKCS5_PBKDF2_HMAC((char*)password->buf, (int)password->len,
1273 : 0 : (unsigned char *)salt->buf, (int)salt->len,
1274 : : iterations, digest, dklen,
1275 : : (unsigned char *)key);
1276 : 0 : Py_END_ALLOW_THREADS
1277 : :
1278 [ # # ]: 0 : if (!retval) {
1279 [ # # ]: 0 : Py_CLEAR(key_obj);
1280 : 0 : _setException(PyExc_ValueError, NULL);
1281 : 0 : goto end;
1282 : : }
1283 : :
1284 : 0 : end:
1285 : : if (digest != NULL) {
1286 : : PY_EVP_MD_free(digest);
1287 : : }
1288 : 0 : return key_obj;
1289 : : }
1290 : :
1291 : : #ifdef PY_OPENSSL_HAS_SCRYPT
1292 : :
1293 : : /* XXX: Parameters salt, n, r and p should be required keyword-only parameters.
1294 : : They are optional in the Argument Clinic declaration only due to a
1295 : : limitation of PyArg_ParseTupleAndKeywords. */
1296 : :
1297 : : /*[clinic input]
1298 : : _hashlib.scrypt
1299 : :
1300 : : password: Py_buffer
1301 : : *
1302 : : salt: Py_buffer = None
1303 : : n as n_obj: object(subclass_of='&PyLong_Type') = None
1304 : : r as r_obj: object(subclass_of='&PyLong_Type') = None
1305 : : p as p_obj: object(subclass_of='&PyLong_Type') = None
1306 : : maxmem: long = 0
1307 : : dklen: long = 64
1308 : :
1309 : :
1310 : : scrypt password-based key derivation function.
1311 : : [clinic start generated code]*/
1312 : :
1313 : : static PyObject *
1314 : 0 : _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1315 : : PyObject *n_obj, PyObject *r_obj, PyObject *p_obj,
1316 : : long maxmem, long dklen)
1317 : : /*[clinic end generated code: output=14849e2aa2b7b46c input=48a7d63bf3f75c42]*/
1318 : : {
1319 : 0 : PyObject *key_obj = NULL;
1320 : : char *key;
1321 : : int retval;
1322 : : unsigned long n, r, p;
1323 : :
1324 [ # # ]: 0 : if (password->len > INT_MAX) {
1325 : 0 : PyErr_SetString(PyExc_OverflowError,
1326 : : "password is too long.");
1327 : 0 : return NULL;
1328 : : }
1329 : :
1330 [ # # ]: 0 : if (salt->buf == NULL) {
1331 : 0 : PyErr_SetString(PyExc_TypeError,
1332 : : "salt is required");
1333 : 0 : return NULL;
1334 : : }
1335 [ # # ]: 0 : if (salt->len > INT_MAX) {
1336 : 0 : PyErr_SetString(PyExc_OverflowError,
1337 : : "salt is too long.");
1338 : 0 : return NULL;
1339 : : }
1340 : :
1341 : 0 : n = PyLong_AsUnsignedLong(n_obj);
1342 [ # # # # ]: 0 : if (n == (unsigned long) -1 && PyErr_Occurred()) {
1343 : 0 : PyErr_SetString(PyExc_TypeError,
1344 : : "n is required and must be an unsigned int");
1345 : 0 : return NULL;
1346 : : }
1347 [ # # # # ]: 0 : if (n < 2 || n & (n - 1)) {
1348 : 0 : PyErr_SetString(PyExc_ValueError,
1349 : : "n must be a power of 2.");
1350 : 0 : return NULL;
1351 : : }
1352 : :
1353 : 0 : r = PyLong_AsUnsignedLong(r_obj);
1354 [ # # # # ]: 0 : if (r == (unsigned long) -1 && PyErr_Occurred()) {
1355 : 0 : PyErr_SetString(PyExc_TypeError,
1356 : : "r is required and must be an unsigned int");
1357 : 0 : return NULL;
1358 : : }
1359 : :
1360 : 0 : p = PyLong_AsUnsignedLong(p_obj);
1361 [ # # # # ]: 0 : if (p == (unsigned long) -1 && PyErr_Occurred()) {
1362 : 0 : PyErr_SetString(PyExc_TypeError,
1363 : : "p is required and must be an unsigned int");
1364 : 0 : return NULL;
1365 : : }
1366 : :
1367 [ # # # # ]: 0 : if (maxmem < 0 || maxmem > INT_MAX) {
1368 : : /* OpenSSL 1.1.0 restricts maxmem to 32 MiB. It may change in the
1369 : : future. The maxmem constant is private to OpenSSL. */
1370 : 0 : PyErr_Format(PyExc_ValueError,
1371 : : "maxmem must be positive and smaller than %d",
1372 : : INT_MAX);
1373 : 0 : return NULL;
1374 : : }
1375 : :
1376 [ # # # # ]: 0 : if (dklen < 1 || dklen > INT_MAX) {
1377 : 0 : PyErr_Format(PyExc_ValueError,
1378 : : "dklen must be greater than 0 and smaller than %d",
1379 : : INT_MAX);
1380 : 0 : return NULL;
1381 : : }
1382 : :
1383 : : /* let OpenSSL validate the rest */
1384 : 0 : retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0);
1385 [ # # ]: 0 : if (!retval) {
1386 : 0 : _setException(PyExc_ValueError, "Invalid parameter combination for n, r, p, maxmem.");
1387 : 0 : return NULL;
1388 : : }
1389 : :
1390 : 0 : key_obj = PyBytes_FromStringAndSize(NULL, dklen);
1391 [ # # ]: 0 : if (key_obj == NULL) {
1392 : 0 : return NULL;
1393 : : }
1394 : 0 : key = PyBytes_AS_STRING(key_obj);
1395 : :
1396 : 0 : Py_BEGIN_ALLOW_THREADS
1397 : 0 : retval = EVP_PBE_scrypt(
1398 : 0 : (const char*)password->buf, (size_t)password->len,
1399 : 0 : (const unsigned char *)salt->buf, (size_t)salt->len,
1400 : : n, r, p, maxmem,
1401 : : (unsigned char *)key, (size_t)dklen
1402 : : );
1403 : 0 : Py_END_ALLOW_THREADS
1404 : :
1405 [ # # ]: 0 : if (!retval) {
1406 [ # # ]: 0 : Py_CLEAR(key_obj);
1407 : 0 : _setException(PyExc_ValueError, NULL);
1408 : 0 : return NULL;
1409 : : }
1410 : 0 : return key_obj;
1411 : : }
1412 : : #endif /* PY_OPENSSL_HAS_SCRYPT */
1413 : :
1414 : : /* Fast HMAC for hmac.digest()
1415 : : */
1416 : :
1417 : : /*[clinic input]
1418 : : _hashlib.hmac_digest as _hashlib_hmac_singleshot
1419 : :
1420 : : key: Py_buffer
1421 : : msg: Py_buffer
1422 : : digest: object
1423 : :
1424 : : Single-shot HMAC.
1425 : : [clinic start generated code]*/
1426 : :
1427 : : static PyObject *
1428 : 0 : _hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
1429 : : Py_buffer *msg, PyObject *digest)
1430 : : /*[clinic end generated code: output=82f19965d12706ac input=0a0790cc3db45c2e]*/
1431 : : {
1432 : 0 : unsigned char md[EVP_MAX_MD_SIZE] = {0};
1433 : 0 : unsigned int md_len = 0;
1434 : : unsigned char *result;
1435 : : PY_EVP_MD *evp;
1436 : :
1437 [ # # ]: 0 : if (key->len > INT_MAX) {
1438 : 0 : PyErr_SetString(PyExc_OverflowError,
1439 : : "key is too long.");
1440 : 0 : return NULL;
1441 : : }
1442 [ # # ]: 0 : if (msg->len > INT_MAX) {
1443 : 0 : PyErr_SetString(PyExc_OverflowError,
1444 : : "msg is too long.");
1445 : 0 : return NULL;
1446 : : }
1447 : :
1448 : 0 : evp = py_digest_by_digestmod(module, digest, Py_ht_mac);
1449 [ # # ]: 0 : if (evp == NULL) {
1450 : 0 : return NULL;
1451 : : }
1452 : :
1453 : 0 : Py_BEGIN_ALLOW_THREADS
1454 : 0 : result = HMAC(
1455 : : evp,
1456 : 0 : (const void*)key->buf, (int)key->len,
1457 : 0 : (const unsigned char*)msg->buf, (int)msg->len,
1458 : : md, &md_len
1459 : : );
1460 : 0 : Py_END_ALLOW_THREADS
1461 : : PY_EVP_MD_free(evp);
1462 : :
1463 [ # # ]: 0 : if (result == NULL) {
1464 : 0 : _setException(PyExc_ValueError, NULL);
1465 : 0 : return NULL;
1466 : : }
1467 : 0 : return PyBytes_FromStringAndSize((const char*)md, md_len);
1468 : : }
1469 : :
1470 : : /* OpenSSL-based HMAC implementation
1471 : : */
1472 : :
1473 : : static int _hmac_update(HMACobject*, PyObject*);
1474 : :
1475 : : /*[clinic input]
1476 : : _hashlib.hmac_new
1477 : :
1478 : : key: Py_buffer
1479 : : msg as msg_obj: object(c_default="NULL") = b''
1480 : : digestmod: object(c_default="NULL") = None
1481 : :
1482 : : Return a new hmac object.
1483 : : [clinic start generated code]*/
1484 : :
1485 : : static PyObject *
1486 : 0 : _hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1487 : : PyObject *digestmod)
1488 : : /*[clinic end generated code: output=c20d9e4d9ed6d219 input=5f4071dcc7f34362]*/
1489 : : {
1490 : 0 : PyTypeObject *type = get_hashlib_state(module)->HMACtype;
1491 : : PY_EVP_MD *digest;
1492 : 0 : HMAC_CTX *ctx = NULL;
1493 : 0 : HMACobject *self = NULL;
1494 : : int r;
1495 : :
1496 [ # # ]: 0 : if (key->len > INT_MAX) {
1497 : 0 : PyErr_SetString(PyExc_OverflowError,
1498 : : "key is too long.");
1499 : 0 : return NULL;
1500 : : }
1501 : :
1502 [ # # ]: 0 : if (digestmod == NULL) {
1503 : 0 : PyErr_SetString(
1504 : : PyExc_TypeError, "Missing required parameter 'digestmod'.");
1505 : 0 : return NULL;
1506 : : }
1507 : :
1508 : 0 : digest = py_digest_by_digestmod(module, digestmod, Py_ht_mac);
1509 [ # # ]: 0 : if (digest == NULL) {
1510 : 0 : return NULL;
1511 : : }
1512 : :
1513 : 0 : ctx = HMAC_CTX_new();
1514 [ # # ]: 0 : if (ctx == NULL) {
1515 : 0 : _setException(PyExc_ValueError, NULL);
1516 : 0 : goto error;
1517 : : }
1518 : :
1519 : 0 : r = HMAC_Init_ex(
1520 : : ctx,
1521 : 0 : (const char*)key->buf,
1522 : 0 : (int)key->len,
1523 : : digest,
1524 : : NULL /*impl*/);
1525 : : PY_EVP_MD_free(digest);
1526 [ # # ]: 0 : if (r == 0) {
1527 : 0 : _setException(PyExc_ValueError, NULL);
1528 : 0 : goto error;
1529 : : }
1530 : :
1531 : 0 : self = (HMACobject *)PyObject_New(HMACobject, type);
1532 [ # # ]: 0 : if (self == NULL) {
1533 : 0 : goto error;
1534 : : }
1535 : :
1536 : 0 : self->ctx = ctx;
1537 : 0 : self->lock = NULL;
1538 : :
1539 [ # # # # ]: 0 : if ((msg_obj != NULL) && (msg_obj != Py_None)) {
1540 [ # # ]: 0 : if (!_hmac_update(self, msg_obj))
1541 : 0 : goto error;
1542 : : }
1543 : :
1544 : 0 : return (PyObject*)self;
1545 : :
1546 : 0 : error:
1547 [ # # ]: 0 : if (ctx) HMAC_CTX_free(ctx);
1548 [ # # ]: 0 : if (self) PyObject_Free(self);
1549 : 0 : return NULL;
1550 : : }
1551 : :
1552 : : /* helper functions */
1553 : : static int
1554 : 0 : locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self)
1555 : : {
1556 : : int result;
1557 [ # # # # ]: 0 : ENTER_HASHLIB(self);
1558 : 0 : result = HMAC_CTX_copy(new_ctx_p, self->ctx);
1559 [ # # ]: 0 : LEAVE_HASHLIB(self);
1560 : 0 : return result;
1561 : : }
1562 : :
1563 : : static unsigned int
1564 : 0 : _hmac_digest_size(HMACobject *self)
1565 : : {
1566 : 0 : unsigned int digest_size = EVP_MD_size(HMAC_CTX_get_md(self->ctx));
1567 : : assert(digest_size <= EVP_MAX_MD_SIZE);
1568 : 0 : return digest_size;
1569 : : }
1570 : :
1571 : : static int
1572 : 0 : _hmac_update(HMACobject *self, PyObject *obj)
1573 : : {
1574 : : int r;
1575 : 0 : Py_buffer view = {0};
1576 : :
1577 [ # # # # : 0 : GET_BUFFER_VIEW_OR_ERROR(obj, &view, return 0);
# # # # ]
1578 : :
1579 [ # # # # ]: 0 : if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
1580 : 0 : self->lock = PyThread_allocate_lock();
1581 : : /* fail? lock = NULL and we fail over to non-threaded code. */
1582 : : }
1583 : :
1584 [ # # ]: 0 : if (self->lock != NULL) {
1585 : 0 : Py_BEGIN_ALLOW_THREADS
1586 : 0 : PyThread_acquire_lock(self->lock, 1);
1587 : 0 : r = HMAC_Update(self->ctx, (const unsigned char*)view.buf, view.len);
1588 : 0 : PyThread_release_lock(self->lock);
1589 : 0 : Py_END_ALLOW_THREADS
1590 : : } else {
1591 : 0 : r = HMAC_Update(self->ctx, (const unsigned char*)view.buf, view.len);
1592 : : }
1593 : :
1594 : 0 : PyBuffer_Release(&view);
1595 : :
1596 [ # # ]: 0 : if (r == 0) {
1597 : 0 : _setException(PyExc_ValueError, NULL);
1598 : 0 : return 0;
1599 : : }
1600 : 0 : return 1;
1601 : : }
1602 : :
1603 : : /*[clinic input]
1604 : : _hashlib.HMAC.copy
1605 : :
1606 : : Return a copy ("clone") of the HMAC object.
1607 : : [clinic start generated code]*/
1608 : :
1609 : : static PyObject *
1610 : 0 : _hashlib_HMAC_copy_impl(HMACobject *self)
1611 : : /*[clinic end generated code: output=29aa28b452833127 input=e2fa6a05db61a4d6]*/
1612 : : {
1613 : : HMACobject *retval;
1614 : :
1615 : 0 : HMAC_CTX *ctx = HMAC_CTX_new();
1616 [ # # ]: 0 : if (ctx == NULL) {
1617 : 0 : return _setException(PyExc_ValueError, NULL);
1618 : : }
1619 [ # # ]: 0 : if (!locked_HMAC_CTX_copy(ctx, self)) {
1620 : 0 : HMAC_CTX_free(ctx);
1621 : 0 : return _setException(PyExc_ValueError, NULL);
1622 : : }
1623 : :
1624 : 0 : retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self));
1625 [ # # ]: 0 : if (retval == NULL) {
1626 : 0 : HMAC_CTX_free(ctx);
1627 : 0 : return NULL;
1628 : : }
1629 : 0 : retval->ctx = ctx;
1630 : 0 : retval->lock = NULL;
1631 : :
1632 : 0 : return (PyObject *)retval;
1633 : : }
1634 : :
1635 : : static void
1636 : 0 : _hmac_dealloc(HMACobject *self)
1637 : : {
1638 : 0 : PyTypeObject *tp = Py_TYPE(self);
1639 [ # # ]: 0 : if (self->lock != NULL) {
1640 : 0 : PyThread_free_lock(self->lock);
1641 : : }
1642 : 0 : HMAC_CTX_free(self->ctx);
1643 : 0 : PyObject_Free(self);
1644 : 0 : Py_DECREF(tp);
1645 : 0 : }
1646 : :
1647 : : static PyObject *
1648 : 0 : _hmac_repr(HMACobject *self)
1649 : : {
1650 : 0 : PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx));
1651 [ # # ]: 0 : if (digest_name == NULL) {
1652 : 0 : return NULL;
1653 : : }
1654 : 0 : PyObject *repr = PyUnicode_FromFormat(
1655 : : "<%U HMAC object @ %p>", digest_name, self
1656 : : );
1657 : 0 : Py_DECREF(digest_name);
1658 : 0 : return repr;
1659 : : }
1660 : :
1661 : : /*[clinic input]
1662 : : _hashlib.HMAC.update
1663 : : msg: object
1664 : :
1665 : : Update the HMAC object with msg.
1666 : : [clinic start generated code]*/
1667 : :
1668 : : static PyObject *
1669 : 0 : _hashlib_HMAC_update_impl(HMACobject *self, PyObject *msg)
1670 : : /*[clinic end generated code: output=f31f0ace8c625b00 input=1829173bb3cfd4e6]*/
1671 : : {
1672 [ # # ]: 0 : if (!_hmac_update(self, msg)) {
1673 : 0 : return NULL;
1674 : : }
1675 : 0 : Py_RETURN_NONE;
1676 : : }
1677 : :
1678 : : static int
1679 : 0 : _hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
1680 : : {
1681 : 0 : HMAC_CTX *temp_ctx = HMAC_CTX_new();
1682 [ # # ]: 0 : if (temp_ctx == NULL) {
1683 : 0 : PyErr_NoMemory();
1684 : 0 : return 0;
1685 : : }
1686 [ # # ]: 0 : if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
1687 : 0 : _setException(PyExc_ValueError, NULL);
1688 : 0 : return 0;
1689 : : }
1690 : 0 : int r = HMAC_Final(temp_ctx, buf, &len);
1691 : 0 : HMAC_CTX_free(temp_ctx);
1692 [ # # ]: 0 : if (r == 0) {
1693 : 0 : _setException(PyExc_ValueError, NULL);
1694 : 0 : return 0;
1695 : : }
1696 : 0 : return 1;
1697 : : }
1698 : :
1699 : : /*[clinic input]
1700 : : _hashlib.HMAC.digest
1701 : : Return the digest of the bytes passed to the update() method so far.
1702 : : [clinic start generated code]*/
1703 : :
1704 : : static PyObject *
1705 : 0 : _hashlib_HMAC_digest_impl(HMACobject *self)
1706 : : /*[clinic end generated code: output=1b1424355af7a41e input=bff07f74da318fb4]*/
1707 : : {
1708 : : unsigned char digest[EVP_MAX_MD_SIZE];
1709 : 0 : unsigned int digest_size = _hmac_digest_size(self);
1710 [ # # ]: 0 : if (digest_size == 0) {
1711 : 0 : return _setException(PyExc_ValueError, NULL);
1712 : : }
1713 : 0 : int r = _hmac_digest(self, digest, digest_size);
1714 [ # # ]: 0 : if (r == 0) {
1715 : 0 : return NULL;
1716 : : }
1717 : 0 : return PyBytes_FromStringAndSize((const char *)digest, digest_size);
1718 : : }
1719 : :
1720 : : /*[clinic input]
1721 : : _hashlib.HMAC.hexdigest
1722 : :
1723 : : Return hexadecimal digest of the bytes passed to the update() method so far.
1724 : :
1725 : : This may be used to exchange the value safely in email or other non-binary
1726 : : environments.
1727 : : [clinic start generated code]*/
1728 : :
1729 : : static PyObject *
1730 : 0 : _hashlib_HMAC_hexdigest_impl(HMACobject *self)
1731 : : /*[clinic end generated code: output=80d825be1eaae6a7 input=5abc42702874ddcf]*/
1732 : : {
1733 : : unsigned char digest[EVP_MAX_MD_SIZE];
1734 : 0 : unsigned int digest_size = _hmac_digest_size(self);
1735 [ # # ]: 0 : if (digest_size == 0) {
1736 : 0 : return _setException(PyExc_ValueError, NULL);
1737 : : }
1738 : 0 : int r = _hmac_digest(self, digest, digest_size);
1739 [ # # ]: 0 : if (r == 0) {
1740 : 0 : return NULL;
1741 : : }
1742 : 0 : return _Py_strhex((const char *)digest, digest_size);
1743 : : }
1744 : :
1745 : : static PyObject *
1746 : 0 : _hashlib_hmac_get_digest_size(HMACobject *self, void *closure)
1747 : : {
1748 : 0 : unsigned int digest_size = _hmac_digest_size(self);
1749 [ # # ]: 0 : if (digest_size == 0) {
1750 : 0 : return _setException(PyExc_ValueError, NULL);
1751 : : }
1752 : 0 : return PyLong_FromLong(digest_size);
1753 : : }
1754 : :
1755 : : static PyObject *
1756 : 0 : _hashlib_hmac_get_block_size(HMACobject *self, void *closure)
1757 : : {
1758 : 0 : const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
1759 [ # # ]: 0 : if (md == NULL) {
1760 : 0 : return _setException(PyExc_ValueError, NULL);
1761 : : }
1762 : 0 : return PyLong_FromLong(EVP_MD_block_size(md));
1763 : : }
1764 : :
1765 : : static PyObject *
1766 : 0 : _hashlib_hmac_get_name(HMACobject *self, void *closure)
1767 : : {
1768 : 0 : PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx));
1769 [ # # ]: 0 : if (digest_name == NULL) {
1770 : 0 : return NULL;
1771 : : }
1772 : 0 : PyObject *name = PyUnicode_FromFormat("hmac-%U", digest_name);
1773 : 0 : Py_DECREF(digest_name);
1774 : 0 : return name;
1775 : : }
1776 : :
1777 : : static PyMethodDef HMAC_methods[] = {
1778 : : _HASHLIB_HMAC_UPDATE_METHODDEF
1779 : : _HASHLIB_HMAC_DIGEST_METHODDEF
1780 : : _HASHLIB_HMAC_HEXDIGEST_METHODDEF
1781 : : _HASHLIB_HMAC_COPY_METHODDEF
1782 : : {NULL, NULL} /* sentinel */
1783 : : };
1784 : :
1785 : : static PyGetSetDef HMAC_getset[] = {
1786 : : {"digest_size", (getter)_hashlib_hmac_get_digest_size, NULL, NULL, NULL},
1787 : : {"block_size", (getter)_hashlib_hmac_get_block_size, NULL, NULL, NULL},
1788 : : {"name", (getter)_hashlib_hmac_get_name, NULL, NULL, NULL},
1789 : : {NULL} /* Sentinel */
1790 : : };
1791 : :
1792 : :
1793 : : PyDoc_STRVAR(hmactype_doc,
1794 : : "The object used to calculate HMAC of a message.\n\
1795 : : \n\
1796 : : Methods:\n\
1797 : : \n\
1798 : : update() -- updates the current digest with an additional string\n\
1799 : : digest() -- return the current digest value\n\
1800 : : hexdigest() -- return the current digest as a string of hexadecimal digits\n\
1801 : : copy() -- return a copy of the current hash object\n\
1802 : : \n\
1803 : : Attributes:\n\
1804 : : \n\
1805 : : name -- the name, including the hash algorithm used by this object\n\
1806 : : digest_size -- number of bytes in digest() output\n");
1807 : :
1808 : : static PyType_Slot HMACtype_slots[] = {
1809 : : {Py_tp_doc, (char *)hmactype_doc},
1810 : : {Py_tp_repr, (reprfunc)_hmac_repr},
1811 : : {Py_tp_dealloc,(destructor)_hmac_dealloc},
1812 : : {Py_tp_methods, HMAC_methods},
1813 : : {Py_tp_getset, HMAC_getset},
1814 : : {0, NULL}
1815 : : };
1816 : :
1817 : : PyType_Spec HMACtype_spec = {
1818 : : "_hashlib.HMAC", /* name */
1819 : : sizeof(HMACobject), /* basicsize */
1820 : : .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
1821 : : .slots = HMACtype_slots,
1822 : : };
1823 : :
1824 : :
1825 : : /* State for our callback function so that it can accumulate a result. */
1826 : : typedef struct _internal_name_mapper_state {
1827 : : PyObject *set;
1828 : : int error;
1829 : : } _InternalNameMapperState;
1830 : :
1831 : :
1832 : : /* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
1833 : : static void
1834 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1835 : : _openssl_hash_name_mapper(EVP_MD *md, void *arg)
1836 : : #else
1837 : 112 : _openssl_hash_name_mapper(const EVP_MD *md, const char *from,
1838 : : const char *to, void *arg)
1839 : : #endif
1840 : : {
1841 : 112 : _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
1842 : : PyObject *py_name;
1843 : :
1844 : : assert(state != NULL);
1845 : : // ignore all undefined providers
1846 [ + + - + ]: 112 : if ((md == NULL) || (EVP_MD_nid(md) == NID_undef)) {
1847 : 70 : return;
1848 : : }
1849 : :
1850 : 42 : py_name = py_digest_name(md);
1851 [ - + ]: 42 : if (py_name == NULL) {
1852 : 0 : state->error = 1;
1853 : : } else {
1854 [ - + ]: 42 : if (PySet_Add(state->set, py_name) != 0) {
1855 : 0 : state->error = 1;
1856 : : }
1857 : 42 : Py_DECREF(py_name);
1858 : : }
1859 : : }
1860 : :
1861 : :
1862 : : /* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
1863 : : static int
1864 : 2 : hashlib_md_meth_names(PyObject *module)
1865 : : {
1866 : 2 : _InternalNameMapperState state = {
1867 : 2 : .set = PyFrozenSet_New(NULL),
1868 : : .error = 0
1869 : : };
1870 [ - + ]: 2 : if (state.set == NULL) {
1871 : 0 : return -1;
1872 : : }
1873 : :
1874 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1875 : : // get algorithms from all activated providers in default context
1876 : : EVP_MD_do_all_provided(NULL, &_openssl_hash_name_mapper, &state);
1877 : : #else
1878 : 2 : EVP_MD_do_all(&_openssl_hash_name_mapper, &state);
1879 : : #endif
1880 : :
1881 [ - + ]: 2 : if (state.error) {
1882 : 0 : Py_DECREF(state.set);
1883 : 0 : return -1;
1884 : : }
1885 : :
1886 [ - + ]: 2 : if (PyModule_AddObject(module, "openssl_md_meth_names", state.set) < 0) {
1887 : 0 : Py_DECREF(state.set);
1888 : 0 : return -1;
1889 : : }
1890 : :
1891 : 2 : return 0;
1892 : : }
1893 : :
1894 : : /*[clinic input]
1895 : : _hashlib.get_fips_mode -> int
1896 : :
1897 : : Determine the OpenSSL FIPS mode of operation.
1898 : :
1899 : : For OpenSSL 3.0.0 and newer it returns the state of the default provider
1900 : : in the default OSSL context. It's not quite the same as FIPS_mode() but good
1901 : : enough for unittests.
1902 : :
1903 : : Effectively any non-zero return value indicates FIPS mode;
1904 : : values other than 1 may have additional significance.
1905 : : [clinic start generated code]*/
1906 : :
1907 : : static int
1908 : 0 : _hashlib_get_fips_mode_impl(PyObject *module)
1909 : : /*[clinic end generated code: output=87eece1bab4d3fa9 input=2db61538c41c6fef]*/
1910 : :
1911 : : {
1912 : : #if OPENSSL_VERSION_NUMBER >= 0x30000000L
1913 : : return EVP_default_properties_is_fips_enabled(NULL);
1914 : : #else
1915 : 0 : ERR_clear_error();
1916 : 0 : int result = FIPS_mode();
1917 [ # # ]: 0 : if (result == 0) {
1918 : : // "If the library was built without support of the FIPS Object Module,
1919 : : // then the function will return 0 with an error code of
1920 : : // CRYPTO_R_FIPS_MODE_NOT_SUPPORTED (0x0f06d065)."
1921 : : // But 0 is also a valid result value.
1922 : 0 : unsigned long errcode = ERR_peek_last_error();
1923 [ # # ]: 0 : if (errcode) {
1924 : 0 : _setException(PyExc_ValueError, NULL);
1925 : 0 : return -1;
1926 : : }
1927 : : }
1928 : 0 : return result;
1929 : : #endif
1930 : : }
1931 : :
1932 : :
1933 : : static int
1934 : 0 : _tscmp(const unsigned char *a, const unsigned char *b,
1935 : : Py_ssize_t len_a, Py_ssize_t len_b)
1936 : : {
1937 : : /* loop count depends on length of b. Might leak very little timing
1938 : : * information if sizes are different.
1939 : : */
1940 : 0 : Py_ssize_t length = len_b;
1941 : 0 : const void *left = a;
1942 : 0 : const void *right = b;
1943 : 0 : int result = 0;
1944 : :
1945 [ # # ]: 0 : if (len_a != length) {
1946 : 0 : left = b;
1947 : 0 : result = 1;
1948 : : }
1949 : :
1950 : 0 : result |= CRYPTO_memcmp(left, right, length);
1951 : :
1952 : 0 : return (result == 0);
1953 : : }
1954 : :
1955 : : /* NOTE: Keep in sync with _operator.c implementation. */
1956 : :
1957 : : /*[clinic input]
1958 : : _hashlib.compare_digest
1959 : :
1960 : : a: object
1961 : : b: object
1962 : : /
1963 : :
1964 : : Return 'a == b'.
1965 : :
1966 : : This function uses an approach designed to prevent
1967 : : timing analysis, making it appropriate for cryptography.
1968 : :
1969 : : a and b must both be of the same type: either str (ASCII only),
1970 : : or any bytes-like object.
1971 : :
1972 : : Note: If a and b are of different lengths, or if an error occurs,
1973 : : a timing attack could theoretically reveal information about the
1974 : : types and lengths of a and b--but not their values.
1975 : : [clinic start generated code]*/
1976 : :
1977 : : static PyObject *
1978 : 0 : _hashlib_compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
1979 : : /*[clinic end generated code: output=6f1c13927480aed9 input=9c40c6e566ca12f5]*/
1980 : : {
1981 : : int rc;
1982 : :
1983 : : /* ASCII unicode string */
1984 [ # # # # ]: 0 : if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
1985 [ # # # # ]: 0 : if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
1986 : 0 : return NULL;
1987 : : }
1988 [ # # # # ]: 0 : if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
1989 : 0 : PyErr_SetString(PyExc_TypeError,
1990 : : "comparing strings with non-ASCII characters is "
1991 : : "not supported");
1992 : 0 : return NULL;
1993 : : }
1994 : :
1995 : 0 : rc = _tscmp(PyUnicode_DATA(a),
1996 : 0 : PyUnicode_DATA(b),
1997 : : PyUnicode_GET_LENGTH(a),
1998 : : PyUnicode_GET_LENGTH(b));
1999 : : }
2000 : : /* fallback to buffer interface for bytes, bytearray and other */
2001 : : else {
2002 : : Py_buffer view_a;
2003 : : Py_buffer view_b;
2004 : :
2005 [ # # # # ]: 0 : if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
2006 : 0 : PyErr_Format(PyExc_TypeError,
2007 : : "unsupported operand types(s) or combination of types: "
2008 : : "'%.100s' and '%.100s'",
2009 : 0 : Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
2010 : 0 : return NULL;
2011 : : }
2012 : :
2013 [ # # ]: 0 : if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
2014 : 0 : return NULL;
2015 : : }
2016 [ # # ]: 0 : if (view_a.ndim > 1) {
2017 : 0 : PyErr_SetString(PyExc_BufferError,
2018 : : "Buffer must be single dimension");
2019 : 0 : PyBuffer_Release(&view_a);
2020 : 0 : return NULL;
2021 : : }
2022 : :
2023 [ # # ]: 0 : if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
2024 : 0 : PyBuffer_Release(&view_a);
2025 : 0 : return NULL;
2026 : : }
2027 [ # # ]: 0 : if (view_b.ndim > 1) {
2028 : 0 : PyErr_SetString(PyExc_BufferError,
2029 : : "Buffer must be single dimension");
2030 : 0 : PyBuffer_Release(&view_a);
2031 : 0 : PyBuffer_Release(&view_b);
2032 : 0 : return NULL;
2033 : : }
2034 : :
2035 : 0 : rc = _tscmp((const unsigned char*)view_a.buf,
2036 : 0 : (const unsigned char*)view_b.buf,
2037 : : view_a.len,
2038 : : view_b.len);
2039 : :
2040 : 0 : PyBuffer_Release(&view_a);
2041 : 0 : PyBuffer_Release(&view_b);
2042 : : }
2043 : :
2044 : 0 : return PyBool_FromLong(rc);
2045 : : }
2046 : :
2047 : : /* List of functions exported by this module */
2048 : :
2049 : : static struct PyMethodDef EVP_functions[] = {
2050 : : EVP_NEW_METHODDEF
2051 : : PBKDF2_HMAC_METHODDEF
2052 : : _HASHLIB_SCRYPT_METHODDEF
2053 : : _HASHLIB_GET_FIPS_MODE_METHODDEF
2054 : : _HASHLIB_COMPARE_DIGEST_METHODDEF
2055 : : _HASHLIB_HMAC_SINGLESHOT_METHODDEF
2056 : : _HASHLIB_HMAC_NEW_METHODDEF
2057 : : _HASHLIB_OPENSSL_MD5_METHODDEF
2058 : : _HASHLIB_OPENSSL_SHA1_METHODDEF
2059 : : _HASHLIB_OPENSSL_SHA224_METHODDEF
2060 : : _HASHLIB_OPENSSL_SHA256_METHODDEF
2061 : : _HASHLIB_OPENSSL_SHA384_METHODDEF
2062 : : _HASHLIB_OPENSSL_SHA512_METHODDEF
2063 : : _HASHLIB_OPENSSL_SHA3_224_METHODDEF
2064 : : _HASHLIB_OPENSSL_SHA3_256_METHODDEF
2065 : : _HASHLIB_OPENSSL_SHA3_384_METHODDEF
2066 : : _HASHLIB_OPENSSL_SHA3_512_METHODDEF
2067 : : _HASHLIB_OPENSSL_SHAKE_128_METHODDEF
2068 : : _HASHLIB_OPENSSL_SHAKE_256_METHODDEF
2069 : : {NULL, NULL} /* Sentinel */
2070 : : };
2071 : :
2072 : :
2073 : : /* Initialize this module. */
2074 : :
2075 : : static int
2076 : 44 : hashlib_traverse(PyObject *m, visitproc visit, void *arg)
2077 : : {
2078 : 44 : _hashlibstate *state = get_hashlib_state(m);
2079 [ + - - + ]: 44 : Py_VISIT(state->EVPtype);
2080 [ + - - + ]: 44 : Py_VISIT(state->HMACtype);
2081 : : #ifdef PY_OPENSSL_HAS_SHAKE
2082 [ + - - + ]: 44 : Py_VISIT(state->EVPXOFtype);
2083 : : #endif
2084 [ + - - + ]: 44 : Py_VISIT(state->constructs);
2085 [ + - - + ]: 44 : Py_VISIT(state->unsupported_digestmod_error);
2086 : 44 : return 0;
2087 : : }
2088 : :
2089 : : static int
2090 : 4 : hashlib_clear(PyObject *m)
2091 : : {
2092 : 4 : _hashlibstate *state = get_hashlib_state(m);
2093 [ + + ]: 4 : Py_CLEAR(state->EVPtype);
2094 [ + + ]: 4 : Py_CLEAR(state->HMACtype);
2095 : : #ifdef PY_OPENSSL_HAS_SHAKE
2096 [ + + ]: 4 : Py_CLEAR(state->EVPXOFtype);
2097 : : #endif
2098 [ + + ]: 4 : Py_CLEAR(state->constructs);
2099 [ + + ]: 4 : Py_CLEAR(state->unsupported_digestmod_error);
2100 : :
2101 [ + + ]: 4 : if (state->hashtable != NULL) {
2102 : 2 : _Py_hashtable_destroy(state->hashtable);
2103 : 2 : state->hashtable = NULL;
2104 : : }
2105 : :
2106 : 4 : return 0;
2107 : : }
2108 : :
2109 : : static void
2110 : 2 : hashlib_free(void *m)
2111 : : {
2112 : 2 : hashlib_clear((PyObject *)m);
2113 : 2 : }
2114 : :
2115 : : /* Py_mod_exec functions */
2116 : : static int
2117 : 2 : hashlib_init_hashtable(PyObject *module)
2118 : : {
2119 : 2 : _hashlibstate *state = get_hashlib_state(module);
2120 : :
2121 : 2 : state->hashtable = py_hashentry_table_new();
2122 [ - + ]: 2 : if (state->hashtable == NULL) {
2123 : 0 : PyErr_NoMemory();
2124 : 0 : return -1;
2125 : : }
2126 : 2 : return 0;
2127 : : }
2128 : :
2129 : : static int
2130 : 2 : hashlib_init_evptype(PyObject *module)
2131 : : {
2132 : 2 : _hashlibstate *state = get_hashlib_state(module);
2133 : :
2134 : 2 : state->EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
2135 [ - + ]: 2 : if (state->EVPtype == NULL) {
2136 : 0 : return -1;
2137 : : }
2138 [ - + ]: 2 : if (PyModule_AddType(module, state->EVPtype) < 0) {
2139 : 0 : return -1;
2140 : : }
2141 : 2 : return 0;
2142 : : }
2143 : :
2144 : : static int
2145 : 2 : hashlib_init_evpxoftype(PyObject *module)
2146 : : {
2147 : : #ifdef PY_OPENSSL_HAS_SHAKE
2148 : 2 : _hashlibstate *state = get_hashlib_state(module);
2149 : :
2150 [ - + ]: 2 : if (state->EVPtype == NULL) {
2151 : 0 : return -1;
2152 : : }
2153 : :
2154 : 4 : state->EVPXOFtype = (PyTypeObject *)PyType_FromSpecWithBases(
2155 : 2 : &EVPXOFtype_spec, (PyObject *)state->EVPtype
2156 : : );
2157 [ - + ]: 2 : if (state->EVPXOFtype == NULL) {
2158 : 0 : return -1;
2159 : : }
2160 [ - + ]: 2 : if (PyModule_AddType(module, state->EVPXOFtype) < 0) {
2161 : 0 : return -1;
2162 : : }
2163 : : #endif
2164 : 2 : return 0;
2165 : : }
2166 : :
2167 : : static int
2168 : 2 : hashlib_init_hmactype(PyObject *module)
2169 : : {
2170 : 2 : _hashlibstate *state = get_hashlib_state(module);
2171 : :
2172 : 2 : state->HMACtype = (PyTypeObject *)PyType_FromSpec(&HMACtype_spec);
2173 [ - + ]: 2 : if (state->HMACtype == NULL) {
2174 : 0 : return -1;
2175 : : }
2176 [ - + ]: 2 : if (PyModule_AddType(module, state->HMACtype) < 0) {
2177 : 0 : return -1;
2178 : : }
2179 : 2 : return 0;
2180 : : }
2181 : :
2182 : : static int
2183 : 2 : hashlib_init_constructors(PyObject *module)
2184 : : {
2185 : : /* Create dict from builtin openssl_hash functions to name
2186 : : * {_hashlib.openssl_sha256: "sha256", ...}
2187 : : */
2188 : : PyModuleDef *mdef;
2189 : : PyMethodDef *fdef;
2190 : : PyObject *proxy;
2191 : : PyObject *func, *name_obj;
2192 : 2 : _hashlibstate *state = get_hashlib_state(module);
2193 : :
2194 : 2 : mdef = PyModule_GetDef(module);
2195 [ - + ]: 2 : if (mdef == NULL) {
2196 : 0 : return -1;
2197 : : }
2198 : :
2199 : 2 : state->constructs = PyDict_New();
2200 [ - + ]: 2 : if (state->constructs == NULL) {
2201 : 0 : return -1;
2202 : : }
2203 : :
2204 [ + + ]: 40 : for (fdef = mdef->m_methods; fdef->ml_name != NULL; fdef++) {
2205 [ + + ]: 38 : if (strncmp(fdef->ml_name, "openssl_", 8)) {
2206 : 14 : continue;
2207 : : }
2208 : 24 : name_obj = PyUnicode_FromString(fdef->ml_name + 8);
2209 [ - + ]: 24 : if (name_obj == NULL) {
2210 : 0 : return -1;
2211 : : }
2212 : 24 : func = PyObject_GetAttrString(module, fdef->ml_name);
2213 [ - + ]: 24 : if (func == NULL) {
2214 : 0 : Py_DECREF(name_obj);
2215 : 0 : return -1;
2216 : : }
2217 : 24 : int rc = PyDict_SetItem(state->constructs, func, name_obj);
2218 : 24 : Py_DECREF(func);
2219 : 24 : Py_DECREF(name_obj);
2220 [ - + ]: 24 : if (rc < 0) {
2221 : 0 : return -1;
2222 : : }
2223 : : }
2224 : :
2225 : 2 : proxy = PyDictProxy_New(state->constructs);
2226 [ - + ]: 2 : if (proxy == NULL) {
2227 : 0 : return -1;
2228 : : }
2229 : :
2230 : 2 : int rc = PyModule_AddObjectRef(module, "_constructors", proxy);
2231 : 2 : Py_DECREF(proxy);
2232 [ - + ]: 2 : if (rc < 0) {
2233 : 0 : return -1;
2234 : : }
2235 : 2 : return 0;
2236 : : }
2237 : :
2238 : : static int
2239 : 2 : hashlib_exception(PyObject *module)
2240 : : {
2241 : 2 : _hashlibstate *state = get_hashlib_state(module);
2242 : 2 : state->unsupported_digestmod_error = PyErr_NewException(
2243 : : "_hashlib.UnsupportedDigestmodError", PyExc_ValueError, NULL);
2244 [ - + ]: 2 : if (state->unsupported_digestmod_error == NULL) {
2245 : 0 : return -1;
2246 : : }
2247 [ - + ]: 2 : if (PyModule_AddObjectRef(module, "UnsupportedDigestmodError",
2248 : : state->unsupported_digestmod_error) < 0) {
2249 : 0 : return -1;
2250 : : }
2251 : 2 : return 0;
2252 : : }
2253 : :
2254 : :
2255 : : static PyModuleDef_Slot hashlib_slots[] = {
2256 : : {Py_mod_exec, hashlib_init_hashtable},
2257 : : {Py_mod_exec, hashlib_init_evptype},
2258 : : {Py_mod_exec, hashlib_init_evpxoftype},
2259 : : {Py_mod_exec, hashlib_init_hmactype},
2260 : : {Py_mod_exec, hashlib_md_meth_names},
2261 : : {Py_mod_exec, hashlib_init_constructors},
2262 : : {Py_mod_exec, hashlib_exception},
2263 : : {0, NULL}
2264 : : };
2265 : :
2266 : : static struct PyModuleDef _hashlibmodule = {
2267 : : PyModuleDef_HEAD_INIT,
2268 : : .m_name = "_hashlib",
2269 : : .m_doc = "OpenSSL interface for hashlib module",
2270 : : .m_size = sizeof(_hashlibstate),
2271 : : .m_methods = EVP_functions,
2272 : : .m_slots = hashlib_slots,
2273 : : .m_traverse = hashlib_traverse,
2274 : : .m_clear = hashlib_clear,
2275 : : .m_free = hashlib_free
2276 : : };
2277 : :
2278 : : PyMODINIT_FUNC
2279 : 2 : PyInit__hashlib(void)
2280 : : {
2281 : 2 : return PyModuleDef_Init(&_hashlibmodule);
2282 : : }
|