blob: adc865377325024a9aa61ee39e9cdad8de479c72 [file] [log] [blame]
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001/* Module that wraps all OpenSSL hash algorithms */
2
3/*
Benjamin Peterson46a99002010-01-09 18:45:30 +00004 * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00005 * 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
Thomas Wouters9bc844e2006-03-01 21:50:07 +000014#define PY_SSIZE_T_CLEAN
15
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000016#include "Python.h"
Gregory P. Smith365a1862009-02-12 07:35:29 +000017#include "hashlib.h"
Gregory P. Smith4dff6f62015-04-25 23:42:38 +000018#include "pystrhex.h"
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000019
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +000020
Gregory P. Smith3f61d612009-05-04 00:45:33 +000021/* EVP is the preferred interface to hashing in OpenSSL */
22#include <openssl/evp.h>
Christian Heimes2f050c72018-01-27 09:53:43 +010023#include <openssl/hmac.h>
Christian Heimesdb5aed92020-05-27 21:50:06 +020024#include <openssl/crypto.h>
Gregory P. Smith13b55292010-09-06 08:30:23 +000025/* We use the object interface to discover what hashes OpenSSL supports. */
26#include <openssl/objects.h>
Christian Heimese92ef132013-10-13 00:52:43 +020027#include "openssl/err.h"
Gregory P. Smith3f61d612009-05-04 00:45:33 +000028
Victor Stinnere3dfb9b2020-04-29 18:04:22 +020029#include <openssl/crypto.h> // FIPS_mode()
30
Christian Heimesc087a262020-05-15 20:55:25 +020031#ifndef OPENSSL_THREADS
32# error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL"
33#endif
34
Christian Heimes9a4963b2019-09-12 16:33:26 +020035#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
36/* OpenSSL < 1.1.0 */
37#define EVP_MD_CTX_new EVP_MD_CTX_create
38#define EVP_MD_CTX_free EVP_MD_CTX_destroy
Christian Heimes54f28982020-05-17 13:49:10 +020039
40HMAC_CTX *
41HMAC_CTX_new(void)
42{
43 HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
44 if (ctx != NULL) {
45 memset(ctx, 0, sizeof(HMAC_CTX));
46 HMAC_CTX_init(ctx);
47 }
48 return ctx;
49}
50
51void
52HMAC_CTX_free(HMAC_CTX *ctx)
53{
54 if (ctx != NULL) {
55 HMAC_CTX_cleanup(ctx);
56 OPENSSL_free(ctx);
57 }
58}
59
60const EVP_MD *
61HMAC_CTX_get_md(const HMAC_CTX *ctx)
62{
63 return ctx->md;
64}
Christian Heimes9a4963b2019-09-12 16:33:26 +020065#endif
66
Gregory P. Smith3f61d612009-05-04 00:45:33 +000067#define MUNCH_SIZE INT_MAX
68
Christian Heimeseb2b0c62019-09-14 17:29:54 +020069#ifdef NID_sha3_224
Christian Heimes995b5d32019-09-13 15:31:19 +020070#define PY_OPENSSL_HAS_SHA3 1
71#endif
72
Christian Heimeseb2b0c62019-09-14 17:29:54 +020073#if defined(EVP_MD_FLAG_XOF) && defined(NID_shake128)
74#define PY_OPENSSL_HAS_SHAKE 1
75#endif
76
Alexandru Ardelean65525632019-11-04 16:55:56 +020077#if defined(NID_blake2b512) && !defined(OPENSSL_NO_BLAKE2)
Christian Heimes995b5d32019-09-13 15:31:19 +020078#define PY_OPENSSL_HAS_BLAKE2 1
79#endif
80
Christian Heimesdf69e752019-09-25 23:03:30 +020081static PyModuleDef _hashlibmodule;
82
83typedef struct {
84 PyTypeObject *EVPtype;
Christian Heimes54f28982020-05-17 13:49:10 +020085 PyTypeObject *HMACtype;
Christian Heimesd5b3f6b2020-05-16 22:27:06 +020086#ifdef PY_OPENSSL_HAS_SHAKE
87 PyTypeObject *EVPXOFtype;
88#endif
Christian Heimesdf69e752019-09-25 23:03:30 +020089} _hashlibstate;
90
Hai Shif707d942020-03-16 21:15:01 +080091static inline _hashlibstate*
92get_hashlib_state(PyObject *module)
93{
94 void *state = PyModule_GetState(module);
95 assert(state != NULL);
96 return (_hashlibstate *)state;
97}
98
Gregory P. Smithf21a5f72005-08-21 18:45:59 +000099typedef struct {
100 PyObject_HEAD
Christian Heimes598894f2016-09-05 23:19:05 +0200101 EVP_MD_CTX *ctx; /* OpenSSL message digest context */
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000102 PyThread_type_lock lock; /* OpenSSL context lock */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000103} EVPobject;
104
Christian Heimes54f28982020-05-17 13:49:10 +0200105typedef struct {
106 PyObject_HEAD
107 HMAC_CTX *ctx; /* OpenSSL hmac context */
108 PyThread_type_lock lock; /* HMAC context lock */
109} HMACobject;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000110
Tal Einatc6c72372018-12-27 15:43:43 +0200111#include "clinic/_hashopenssl.c.h"
112/*[clinic input]
113module _hashlib
Christian Heimesdf69e752019-09-25 23:03:30 +0200114class _hashlib.HASH "EVPobject *" "((_hashlibstate *)PyModule_GetState(module))->EVPtype"
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200115class _hashlib.HASHXOF "EVPobject *" "((_hashlibstate *)PyModule_GetState(module))->EVPXOFtype"
Christian Heimes54f28982020-05-17 13:49:10 +0200116class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))->HMACtype"
Tal Einatc6c72372018-12-27 15:43:43 +0200117[clinic start generated code]*/
Christian Heimes54f28982020-05-17 13:49:10 +0200118/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7df1bcf6f75cb8ef]*/
Tal Einatc6c72372018-12-27 15:43:43 +0200119
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000120
Christian Heimes598894f2016-09-05 23:19:05 +0200121/* LCOV_EXCL_START */
122static PyObject *
123_setException(PyObject *exc)
124{
125 unsigned long errcode;
126 const char *lib, *func, *reason;
127
128 errcode = ERR_peek_last_error();
129 if (!errcode) {
130 PyErr_SetString(exc, "unknown reasons");
131 return NULL;
132 }
133 ERR_clear_error();
134
135 lib = ERR_lib_error_string(errcode);
136 func = ERR_func_error_string(errcode);
137 reason = ERR_reason_error_string(errcode);
138
139 if (lib && func) {
140 PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
141 }
142 else if (lib) {
143 PyErr_Format(exc, "[%s] %s", lib, reason);
144 }
145 else {
146 PyErr_SetString(exc, reason);
147 }
148 return NULL;
149}
150/* LCOV_EXCL_STOP */
151
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200152/* {Py_tp_new, NULL} doesn't block __new__ */
153static PyObject *
154_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
155{
156 PyErr_Format(PyExc_TypeError,
157 "cannot create '%.100s' instances", _PyType_Name(type));
158 return NULL;
159}
160
Christian Heimes995b5d32019-09-13 15:31:19 +0200161static PyObject*
162py_digest_name(const EVP_MD *md)
163{
164 int nid = EVP_MD_nid(md);
165 const char *name = NULL;
166
167 /* Hard-coded names for well-known hashing algorithms.
168 * OpenSSL uses slightly different names algorithms like SHA3.
169 */
170 switch (nid) {
171 case NID_md5:
172 name = "md5";
173 break;
174 case NID_sha1:
175 name = "sha1";
176 break;
177 case NID_sha224:
178 name ="sha224";
179 break;
180 case NID_sha256:
181 name ="sha256";
182 break;
183 case NID_sha384:
184 name ="sha384";
185 break;
186 case NID_sha512:
187 name ="sha512";
188 break;
189#ifdef NID_sha512_224
190 case NID_sha512_224:
191 name ="sha512_224";
192 break;
193 case NID_sha512_256:
194 name ="sha512_256";
195 break;
196#endif
197#ifdef PY_OPENSSL_HAS_SHA3
198 case NID_sha3_224:
199 name ="sha3_224";
200 break;
201 case NID_sha3_256:
202 name ="sha3_256";
203 break;
204 case NID_sha3_384:
205 name ="sha3_384";
206 break;
207 case NID_sha3_512:
208 name ="sha3_512";
209 break;
Christian Heimeseb2b0c62019-09-14 17:29:54 +0200210#endif
211#ifdef PY_OPENSSL_HAS_SHAKE
Christian Heimes995b5d32019-09-13 15:31:19 +0200212 case NID_shake128:
213 name ="shake_128";
214 break;
215 case NID_shake256:
216 name ="shake_256";
217 break;
218#endif
219#ifdef PY_OPENSSL_HAS_BLAKE2
220 case NID_blake2s256:
221 name ="blake2s";
222 break;
223 case NID_blake2b512:
224 name ="blake2b";
225 break;
226#endif
227 default:
228 /* Ignore aliased names and only use long, lowercase name. The aliases
229 * pollute the list and OpenSSL appears to have its own definition of
230 * alias as the resulting list still contains duplicate and alternate
231 * names for several algorithms.
232 */
233 name = OBJ_nid2ln(nid);
234 if (name == NULL)
235 name = OBJ_nid2sn(nid);
236 break;
237 }
238
239 return PyUnicode_FromString(name);
240}
241
242static const EVP_MD*
243py_digest_by_name(const char *name)
244{
245 const EVP_MD *digest = EVP_get_digestbyname(name);
246
247 /* OpenSSL uses dash instead of underscore in names of some algorithms
248 * like SHA3 and SHAKE. Detect different spellings. */
249 if (digest == NULL) {
Christian Heimeseb2b0c62019-09-14 17:29:54 +0200250 if (0) {}
Christian Heimes995b5d32019-09-13 15:31:19 +0200251#ifdef NID_sha512_224
Christian Heimeseb2b0c62019-09-14 17:29:54 +0200252 else if (!strcmp(name, "sha512_224") || !strcmp(name, "SHA512_224")) {
Christian Heimes995b5d32019-09-13 15:31:19 +0200253 digest = EVP_sha512_224();
254 }
255 else if (!strcmp(name, "sha512_256") || !strcmp(name, "SHA512_256")) {
256 digest = EVP_sha512_256();
257 }
258#endif
259#ifdef PY_OPENSSL_HAS_SHA3
260 /* could be sha3_ or shake_, Python never defined upper case */
261 else if (!strcmp(name, "sha3_224")) {
262 digest = EVP_sha3_224();
263 }
264 else if (!strcmp(name, "sha3_256")) {
265 digest = EVP_sha3_256();
266 }
267 else if (!strcmp(name, "sha3_384")) {
268 digest = EVP_sha3_384();
269 }
270 else if (!strcmp(name, "sha3_512")) {
271 digest = EVP_sha3_512();
272 }
Christian Heimeseb2b0c62019-09-14 17:29:54 +0200273#endif
274#ifdef PY_OPENSSL_HAS_SHAKE
Christian Heimes995b5d32019-09-13 15:31:19 +0200275 else if (!strcmp(name, "shake_128")) {
276 digest = EVP_shake128();
277 }
278 else if (!strcmp(name, "shake_256")) {
279 digest = EVP_shake256();
280 }
281#endif
282#ifdef PY_OPENSSL_HAS_BLAKE2
283 else if (!strcmp(name, "blake2s256")) {
284 digest = EVP_blake2s256();
285 }
286 else if (!strcmp(name, "blake2b512")) {
287 digest = EVP_blake2b512();
288 }
289#endif
290 }
291
292 return digest;
293}
294
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000295static EVPobject *
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200296newEVPobject(PyTypeObject *type)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000297{
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200298 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, type);
Christian Heimes598894f2016-09-05 23:19:05 +0200299 if (retval == NULL) {
300 return NULL;
301 }
302
Christian Heimes598894f2016-09-05 23:19:05 +0200303 retval->lock = NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000304
Christian Heimesb7bc2832019-03-04 16:45:41 +0100305 retval->ctx = EVP_MD_CTX_new();
306 if (retval->ctx == NULL) {
307 Py_DECREF(retval);
308 PyErr_NoMemory();
309 return NULL;
310 }
311
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000312 return retval;
313}
314
Christian Heimes8c745742019-09-12 15:30:47 +0200315static int
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000316EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
317{
318 unsigned int process;
319 const unsigned char *cp = (const unsigned char *)vp;
320 while (0 < len) {
321 if (len > (Py_ssize_t)MUNCH_SIZE)
322 process = MUNCH_SIZE;
323 else
324 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
Gregory P. Smith07244a82017-05-24 00:04:38 -0700325 if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) {
326 _setException(PyExc_ValueError);
Christian Heimes8c745742019-09-12 15:30:47 +0200327 return -1;
Gregory P. Smith07244a82017-05-24 00:04:38 -0700328 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000329 len -= process;
330 cp += process;
331 }
Christian Heimes8c745742019-09-12 15:30:47 +0200332 return 0;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000333}
334
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000335/* Internal methods for a hash object */
336
337static void
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000338EVP_dealloc(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000339{
Christian Heimesdf69e752019-09-25 23:03:30 +0200340 PyTypeObject *tp = Py_TYPE(self);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000341 if (self->lock != NULL)
342 PyThread_free_lock(self->lock);
Christian Heimes598894f2016-09-05 23:19:05 +0200343 EVP_MD_CTX_free(self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000344 PyObject_Del(self);
Christian Heimesdf69e752019-09-25 23:03:30 +0200345 Py_DECREF(tp);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000346}
347
Christian Heimes598894f2016-09-05 23:19:05 +0200348static int
349locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000350{
Christian Heimes598894f2016-09-05 23:19:05 +0200351 int result;
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000352 ENTER_HASHLIB(self);
Christian Heimes598894f2016-09-05 23:19:05 +0200353 result = EVP_MD_CTX_copy(new_ctx_p, self->ctx);
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000354 LEAVE_HASHLIB(self);
Christian Heimes598894f2016-09-05 23:19:05 +0200355 return result;
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000356}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000357
358/* External methods for a hash object */
359
Tal Einatc6c72372018-12-27 15:43:43 +0200360/*[clinic input]
361_hashlib.HASH.copy as EVP_copy
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000362
Tal Einatc6c72372018-12-27 15:43:43 +0200363Return a copy of the hash object.
364[clinic start generated code]*/
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000365
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000366static PyObject *
Tal Einatc6c72372018-12-27 15:43:43 +0200367EVP_copy_impl(EVPobject *self)
368/*[clinic end generated code: output=b370c21cdb8ca0b4 input=31455b6a3e638069]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000369{
370 EVPobject *newobj;
371
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200372 if ((newobj = newEVPobject(Py_TYPE(self))) == NULL)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000373 return NULL;
374
Christian Heimes598894f2016-09-05 23:19:05 +0200375 if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) {
Christian Heimesb7bc2832019-03-04 16:45:41 +0100376 Py_DECREF(newobj);
Christian Heimes598894f2016-09-05 23:19:05 +0200377 return _setException(PyExc_ValueError);
378 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000379 return (PyObject *)newobj;
380}
381
Tal Einatc6c72372018-12-27 15:43:43 +0200382/*[clinic input]
383_hashlib.HASH.digest as EVP_digest
384
385Return the digest value as a bytes object.
386[clinic start generated code]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000387
388static PyObject *
Tal Einatc6c72372018-12-27 15:43:43 +0200389EVP_digest_impl(EVPobject *self)
390/*[clinic end generated code: output=0f6a3a0da46dc12d input=03561809a419bf00]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000391{
392 unsigned char digest[EVP_MAX_MD_SIZE];
Christian Heimes598894f2016-09-05 23:19:05 +0200393 EVP_MD_CTX *temp_ctx;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000394 PyObject *retval;
395 unsigned int digest_size;
396
Christian Heimes598894f2016-09-05 23:19:05 +0200397 temp_ctx = EVP_MD_CTX_new();
398 if (temp_ctx == NULL) {
399 PyErr_NoMemory();
400 return NULL;
401 }
402
403 if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
404 return _setException(PyExc_ValueError);
405 }
406 digest_size = EVP_MD_CTX_size(temp_ctx);
Gregory P. Smith07244a82017-05-24 00:04:38 -0700407 if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
408 _setException(PyExc_ValueError);
409 return NULL;
410 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000411
Christian Heimes72b710a2008-05-26 13:28:38 +0000412 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
Christian Heimes598894f2016-09-05 23:19:05 +0200413 EVP_MD_CTX_free(temp_ctx);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000414 return retval;
415}
416
Tal Einatc6c72372018-12-27 15:43:43 +0200417/*[clinic input]
418_hashlib.HASH.hexdigest as EVP_hexdigest
419
420Return the digest value as a string of hexadecimal digits.
421[clinic start generated code]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000422
423static PyObject *
Tal Einatc6c72372018-12-27 15:43:43 +0200424EVP_hexdigest_impl(EVPobject *self)
425/*[clinic end generated code: output=18e6decbaf197296 input=aff9cf0e4c741a9a]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000426{
427 unsigned char digest[EVP_MAX_MD_SIZE];
Christian Heimes598894f2016-09-05 23:19:05 +0200428 EVP_MD_CTX *temp_ctx;
Gregory P. Smith4dff6f62015-04-25 23:42:38 +0000429 unsigned int digest_size;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000430
Christian Heimes598894f2016-09-05 23:19:05 +0200431 temp_ctx = EVP_MD_CTX_new();
432 if (temp_ctx == NULL) {
433 PyErr_NoMemory();
434 return NULL;
435 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000436
Christian Heimes598894f2016-09-05 23:19:05 +0200437 /* Get the raw (binary) digest value */
438 if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
439 return _setException(PyExc_ValueError);
440 }
441 digest_size = EVP_MD_CTX_size(temp_ctx);
Gregory P. Smith07244a82017-05-24 00:04:38 -0700442 if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
443 _setException(PyExc_ValueError);
444 return NULL;
445 }
Christian Heimes598894f2016-09-05 23:19:05 +0200446
447 EVP_MD_CTX_free(temp_ctx);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000448
Benjamin Petersone5024512018-09-12 12:06:42 -0700449 return _Py_strhex((const char *)digest, (Py_ssize_t)digest_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000450}
451
Tal Einatc6c72372018-12-27 15:43:43 +0200452/*[clinic input]
453_hashlib.HASH.update as EVP_update
454
455 obj: object
456 /
457
458Update this hash object's state with the provided string.
459[clinic start generated code]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000460
461static PyObject *
Tal Einatc6c72372018-12-27 15:43:43 +0200462EVP_update(EVPobject *self, PyObject *obj)
463/*[clinic end generated code: output=ec1d55ed2432e966 input=9b30ec848f015501]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000464{
Christian Heimes8c745742019-09-12 15:30:47 +0200465 int result;
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000466 Py_buffer view;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000467
Gregory P. Smith365a1862009-02-12 07:35:29 +0000468 GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000469
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000470 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
471 self->lock = PyThread_allocate_lock();
Gregory P. Smith3f61d612009-05-04 00:45:33 +0000472 /* fail? lock = NULL and we fail over to non-threaded code. */
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000473 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000474
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000475 if (self->lock != NULL) {
476 Py_BEGIN_ALLOW_THREADS
477 PyThread_acquire_lock(self->lock, 1);
Christian Heimes8c745742019-09-12 15:30:47 +0200478 result = EVP_hash(self, view.buf, view.len);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000479 PyThread_release_lock(self->lock);
480 Py_END_ALLOW_THREADS
481 } else {
Christian Heimes8c745742019-09-12 15:30:47 +0200482 result = EVP_hash(self, view.buf, view.len);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000483 }
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000484
485 PyBuffer_Release(&view);
Christian Heimes8c745742019-09-12 15:30:47 +0200486
487 if (result == -1)
488 return NULL;
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000489 Py_RETURN_NONE;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000490}
491
492static PyMethodDef EVP_methods[] = {
Tal Einatc6c72372018-12-27 15:43:43 +0200493 EVP_UPDATE_METHODDEF
494 EVP_DIGEST_METHODDEF
495 EVP_HEXDIGEST_METHODDEF
496 EVP_COPY_METHODDEF
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000497 {NULL, NULL} /* sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000498};
499
500static PyObject *
501EVP_get_block_size(EVPobject *self, void *closure)
502{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000503 long block_size;
Christian Heimes598894f2016-09-05 23:19:05 +0200504 block_size = EVP_MD_CTX_block_size(self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000505 return PyLong_FromLong(block_size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000506}
507
508static PyObject *
509EVP_get_digest_size(EVPobject *self, void *closure)
510{
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000511 long size;
Christian Heimes598894f2016-09-05 23:19:05 +0200512 size = EVP_MD_CTX_size(self->ctx);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000513 return PyLong_FromLong(size);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000514}
515
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200516static PyObject *
517EVP_get_name(EVPobject *self, void *closure)
518{
Christian Heimes995b5d32019-09-13 15:31:19 +0200519 return py_digest_name(EVP_MD_CTX_md(self->ctx));
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200520}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000521
522static PyGetSetDef EVP_getseters[] = {
523 {"digest_size",
524 (getter)EVP_get_digest_size, NULL,
525 NULL,
526 NULL},
527 {"block_size",
528 (getter)EVP_get_block_size, NULL,
529 NULL,
530 NULL},
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200531 {"name",
532 (getter)EVP_get_name, NULL,
533 NULL,
534 PyDoc_STR("algorithm name.")},
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000535 {NULL} /* Sentinel */
536};
537
538
539static PyObject *
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000540EVP_repr(EVPobject *self)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000541{
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200542 PyObject *name_obj, *repr;
Christian Heimes995b5d32019-09-13 15:31:19 +0200543 name_obj = py_digest_name(EVP_MD_CTX_md(self->ctx));
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200544 if (!name_obj) {
545 return NULL;
546 }
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200547 repr = PyUnicode_FromFormat("<%U %s object @ %p>",
548 name_obj, Py_TYPE(self)->tp_name, self);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200549 Py_DECREF(name_obj);
550 return repr;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000551}
552
Gregory P. Smithc7e21912018-12-30 17:54:53 -0800553PyDoc_STRVAR(hashtype_doc,
554"HASH(name, string=b\'\')\n"
555"--\n"
556"\n"
557"A hash is an object used to calculate a checksum of a string of information.\n"
558"\n"
559"Methods:\n"
560"\n"
561"update() -- updates the current digest with an additional string\n"
562"digest() -- return the current digest value\n"
563"hexdigest() -- return the current digest as a string of hexadecimal digits\n"
564"copy() -- return a copy of the current hash object\n"
565"\n"
566"Attributes:\n"
567"\n"
568"name -- the hash algorithm being used by this object\n"
569"digest_size -- number of bytes in this hashes output");
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000570
Christian Heimesdf69e752019-09-25 23:03:30 +0200571static PyType_Slot EVPtype_slots[] = {
572 {Py_tp_dealloc, EVP_dealloc},
573 {Py_tp_repr, EVP_repr},
574 {Py_tp_doc, (char *)hashtype_doc},
575 {Py_tp_methods, EVP_methods},
576 {Py_tp_getset, EVP_getseters},
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200577 {Py_tp_new, _disabled_new},
Christian Heimesdf69e752019-09-25 23:03:30 +0200578 {0, 0},
579};
580
581static PyType_Spec EVPtype_spec = {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000582 "_hashlib.HASH", /*tp_name*/
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000583 sizeof(EVPobject), /*tp_basicsize*/
584 0, /*tp_itemsize*/
Christian Heimesdf69e752019-09-25 23:03:30 +0200585 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
586 EVPtype_slots
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000587};
588
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200589#ifdef PY_OPENSSL_HAS_SHAKE
590
591/*[clinic input]
592_hashlib.HASHXOF.digest as EVPXOF_digest
593
594 length: Py_ssize_t
595
596Return the digest value as a bytes object.
597[clinic start generated code]*/
598
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000599static PyObject *
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200600EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
601/*[clinic end generated code: output=ef9320c23280efad input=816a6537cea3d1db]*/
602{
603 EVP_MD_CTX *temp_ctx;
604 PyObject *retval = PyBytes_FromStringAndSize(NULL, length);
605
606 if (retval == NULL) {
607 return NULL;
608 }
609
610 temp_ctx = EVP_MD_CTX_new();
611 if (temp_ctx == NULL) {
612 Py_DECREF(retval);
613 PyErr_NoMemory();
614 return NULL;
615 }
616
617 if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
618 Py_DECREF(retval);
619 EVP_MD_CTX_free(temp_ctx);
620 return _setException(PyExc_ValueError);
621 }
622 if (!EVP_DigestFinalXOF(temp_ctx,
623 (unsigned char*)PyBytes_AS_STRING(retval),
624 length)) {
625 Py_DECREF(retval);
626 EVP_MD_CTX_free(temp_ctx);
627 _setException(PyExc_ValueError);
628 return NULL;
629 }
630
631 EVP_MD_CTX_free(temp_ctx);
632 return retval;
633}
634
635/*[clinic input]
636_hashlib.HASHXOF.hexdigest as EVPXOF_hexdigest
637
638 length: Py_ssize_t
639
640Return the digest value as a string of hexadecimal digits.
641[clinic start generated code]*/
642
643static PyObject *
644EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
645/*[clinic end generated code: output=eb3e6ee7788bf5b2 input=5f9d6a8f269e34df]*/
646{
647 unsigned char *digest;
648 EVP_MD_CTX *temp_ctx;
649 PyObject *retval;
650
651 digest = (unsigned char*)PyMem_Malloc(length);
652 if (digest == NULL) {
653 PyErr_NoMemory();
654 return NULL;
655 }
656
657 temp_ctx = EVP_MD_CTX_new();
658 if (temp_ctx == NULL) {
659 PyMem_Free(digest);
660 PyErr_NoMemory();
661 return NULL;
662 }
663
664 /* Get the raw (binary) digest value */
665 if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
666 PyMem_Free(digest);
667 EVP_MD_CTX_free(temp_ctx);
668 return _setException(PyExc_ValueError);
669 }
670 if (!EVP_DigestFinalXOF(temp_ctx, digest, length)) {
671 PyMem_Free(digest);
672 EVP_MD_CTX_free(temp_ctx);
673 _setException(PyExc_ValueError);
674 return NULL;
675 }
676
677 EVP_MD_CTX_free(temp_ctx);
678
679 retval = _Py_strhex((const char *)digest, length);
680 PyMem_Free(digest);
681 return retval;
682}
683
684static PyMethodDef EVPXOF_methods[] = {
685 EVPXOF_DIGEST_METHODDEF
686 EVPXOF_HEXDIGEST_METHODDEF
687 {NULL, NULL} /* sentinel */
688};
689
690
691static PyObject *
692EVPXOF_get_digest_size(EVPobject *self, void *closure)
693{
694 return PyLong_FromLong(0);
695}
696
697static PyGetSetDef EVPXOF_getseters[] = {
698 {"digest_size",
699 (getter)EVPXOF_get_digest_size, NULL,
700 NULL,
701 NULL},
702 {NULL} /* Sentinel */
703};
704
705PyDoc_STRVAR(hashxoftype_doc,
706"HASHXOF(name, string=b\'\')\n"
707"--\n"
708"\n"
709"A hash is an object used to calculate a checksum of a string of information.\n"
710"\n"
711"Methods:\n"
712"\n"
713"update() -- updates the current digest with an additional string\n"
714"digest(length) -- return the current digest value\n"
715"hexdigest(length) -- return the current digest as a string of hexadecimal digits\n"
716"copy() -- return a copy of the current hash object\n"
717"\n"
718"Attributes:\n"
719"\n"
720"name -- the hash algorithm being used by this object\n"
721"digest_size -- number of bytes in this hashes output");
722
723static PyType_Slot EVPXOFtype_slots[] = {
724 {Py_tp_doc, (char *)hashxoftype_doc},
725 {Py_tp_methods, EVPXOF_methods},
726 {Py_tp_getset, EVPXOF_getseters},
727 {Py_tp_new, _disabled_new},
728 {0, 0},
729};
730
731static PyType_Spec EVPXOFtype_spec = {
732 "_hashlib.HASHXOF", /*tp_name*/
733 sizeof(EVPobject), /*tp_basicsize*/
734 0, /*tp_itemsize*/
735 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
736 EVPXOFtype_slots
737};
738
739
740#endif
741
742static PyObject *
743EVPnew(PyObject *module, const EVP_MD *digest,
Christian Heimes7cad53e2019-09-13 02:30:00 +0200744 const unsigned char *cp, Py_ssize_t len, int usedforsecurity)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000745{
Christian Heimes8c745742019-09-12 15:30:47 +0200746 int result = 0;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000747 EVPobject *self;
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200748 PyTypeObject *type = get_hashlib_state(module)->EVPtype;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000749
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200750 if (!digest) {
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000751 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
752 return NULL;
753 }
754
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200755#ifdef PY_OPENSSL_HAS_SHAKE
756 if ((EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) == EVP_MD_FLAG_XOF) {
757 type = get_hashlib_state(module)->EVPXOFtype;
758 }
759#endif
760
761 if ((self = newEVPobject(type)) == NULL)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000762 return NULL;
763
Christian Heimes7cad53e2019-09-13 02:30:00 +0200764 if (!usedforsecurity) {
765#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
766 EVP_MD_CTX_set_flags(self->ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
767#endif
768 }
769
770
Christian Heimes90558152019-09-27 15:03:53 +0200771 if (!EVP_DigestInit_ex(self->ctx, digest, NULL)) {
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200772 _setException(PyExc_ValueError);
773 Py_DECREF(self);
774 return NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000775 }
776
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000777 if (cp && len) {
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000778 if (len >= HASHLIB_GIL_MINSIZE) {
779 Py_BEGIN_ALLOW_THREADS
Christian Heimes8c745742019-09-12 15:30:47 +0200780 result = EVP_hash(self, cp, len);
Antoine Pitroubcd5cbe2009-01-08 21:17:16 +0000781 Py_END_ALLOW_THREADS
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000782 } else {
Christian Heimes8c745742019-09-12 15:30:47 +0200783 result = EVP_hash(self, cp, len);
784 }
785 if (result == -1) {
786 Py_DECREF(self);
787 return NULL;
Benjamin Peterson78cb4912008-09-24 22:53:33 +0000788 }
789 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000790
791 return (PyObject *)self;
792}
793
794
795/* The module-level function: new() */
796
Tal Einatc6c72372018-12-27 15:43:43 +0200797/*[clinic input]
798_hashlib.new as EVP_new
799
800 name as name_obj: object
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300801 string as data_obj: object(c_default="NULL") = b''
Christian Heimes7cad53e2019-09-13 02:30:00 +0200802 *
803 usedforsecurity: bool = True
Tal Einatc6c72372018-12-27 15:43:43 +0200804
805Return a new hash object using the named algorithm.
806
807An optional string argument may be provided and will be
808automatically hashed.
809
810The MD5 and SHA1 algorithms are always supported.
811[clinic start generated code]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000812
813static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200814EVP_new_impl(PyObject *module, PyObject *name_obj, PyObject *data_obj,
815 int usedforsecurity)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300816/*[clinic end generated code: output=ddd5053f92dffe90 input=c24554d0337be1b0]*/
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000817{
Travis E. Oliphant8ae62b62007-09-23 02:00:13 +0000818 Py_buffer view = { 0 };
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000819 PyObject *ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000820 char *name;
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200821 const EVP_MD *digest = NULL;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000822
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000823 if (!PyArg_Parse(name_obj, "s", &name)) {
824 PyErr_SetString(PyExc_TypeError, "name must be a string");
825 return NULL;
826 }
827
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000828 if (data_obj)
Gregory P. Smith365a1862009-02-12 07:35:29 +0000829 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000830
Christian Heimes995b5d32019-09-13 15:31:19 +0200831 digest = py_digest_by_name(name);
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000832
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200833 ret_obj = EVPnew(module, digest,
Christian Heimes7cad53e2019-09-13 02:30:00 +0200834 (unsigned char*)view.buf, view.len,
835 usedforsecurity);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000836
837 if (data_obj)
Martin v. Löwis423be952008-08-13 15:53:07 +0000838 PyBuffer_Release(&view);
Gregory P. Smith9406f5c2007-08-26 02:58:36 +0000839 return ret_obj;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +0000840}
841
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200842static PyObject*
Christian Heimes7cad53e2019-09-13 02:30:00 +0200843EVP_fast_new(PyObject *module, PyObject *data_obj, const EVP_MD *digest,
844 int usedforsecurity)
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200845{
846 Py_buffer view = { 0 };
847 PyObject *ret_obj;
848
849 if (data_obj)
850 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
851
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200852 ret_obj = EVPnew(module, digest,
Christian Heimes7cad53e2019-09-13 02:30:00 +0200853 (unsigned char*)view.buf, view.len,
854 usedforsecurity);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200855
856 if (data_obj)
857 PyBuffer_Release(&view);
858
859 return ret_obj;
860}
861
862/*[clinic input]
863_hashlib.openssl_md5
864
865 string as data_obj: object(py_default="b''") = NULL
Christian Heimes7cad53e2019-09-13 02:30:00 +0200866 *
867 usedforsecurity: bool = True
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200868
869Returns a md5 hash object; optionally initialized with a string
870
871[clinic start generated code]*/
872
873static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200874_hashlib_openssl_md5_impl(PyObject *module, PyObject *data_obj,
875 int usedforsecurity)
876/*[clinic end generated code: output=87b0186440a44f8c input=990e36d5e689b16e]*/
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200877{
Christian Heimes7cad53e2019-09-13 02:30:00 +0200878 return EVP_fast_new(module, data_obj, EVP_md5(), usedforsecurity);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200879}
880
881
882/*[clinic input]
883_hashlib.openssl_sha1
884
885 string as data_obj: object(py_default="b''") = NULL
Christian Heimes7cad53e2019-09-13 02:30:00 +0200886 *
887 usedforsecurity: bool = True
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200888
889Returns a sha1 hash object; optionally initialized with a string
890
891[clinic start generated code]*/
892
893static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200894_hashlib_openssl_sha1_impl(PyObject *module, PyObject *data_obj,
895 int usedforsecurity)
896/*[clinic end generated code: output=6813024cf690670d input=948f2f4b6deabc10]*/
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200897{
Christian Heimes7cad53e2019-09-13 02:30:00 +0200898 return EVP_fast_new(module, data_obj, EVP_sha1(), usedforsecurity);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200899}
900
901
902/*[clinic input]
903_hashlib.openssl_sha224
904
905 string as data_obj: object(py_default="b''") = NULL
Christian Heimes7cad53e2019-09-13 02:30:00 +0200906 *
907 usedforsecurity: bool = True
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200908
909Returns a sha224 hash object; optionally initialized with a string
910
911[clinic start generated code]*/
912
913static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200914_hashlib_openssl_sha224_impl(PyObject *module, PyObject *data_obj,
915 int usedforsecurity)
916/*[clinic end generated code: output=a2dfe7cc4eb14ebb input=f9272821fadca505]*/
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200917{
Christian Heimes7cad53e2019-09-13 02:30:00 +0200918 return EVP_fast_new(module, data_obj, EVP_sha224(), usedforsecurity);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200919}
920
921
922/*[clinic input]
923_hashlib.openssl_sha256
924
925 string as data_obj: object(py_default="b''") = NULL
Christian Heimes7cad53e2019-09-13 02:30:00 +0200926 *
927 usedforsecurity: bool = True
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200928
929Returns a sha256 hash object; optionally initialized with a string
930
931[clinic start generated code]*/
932
933static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200934_hashlib_openssl_sha256_impl(PyObject *module, PyObject *data_obj,
935 int usedforsecurity)
936/*[clinic end generated code: output=1f874a34870f0a68 input=549fad9d2930d4c5]*/
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200937{
Christian Heimes7cad53e2019-09-13 02:30:00 +0200938 return EVP_fast_new(module, data_obj, EVP_sha256(), usedforsecurity);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200939}
940
941
942/*[clinic input]
943_hashlib.openssl_sha384
944
945 string as data_obj: object(py_default="b''") = NULL
Christian Heimes7cad53e2019-09-13 02:30:00 +0200946 *
947 usedforsecurity: bool = True
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200948
949Returns a sha384 hash object; optionally initialized with a string
950
951[clinic start generated code]*/
952
953static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200954_hashlib_openssl_sha384_impl(PyObject *module, PyObject *data_obj,
955 int usedforsecurity)
956/*[clinic end generated code: output=58529eff9ca457b2 input=48601a6e3bf14ad7]*/
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200957{
Christian Heimes7cad53e2019-09-13 02:30:00 +0200958 return EVP_fast_new(module, data_obj, EVP_sha384(), usedforsecurity);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200959}
960
961
962/*[clinic input]
963_hashlib.openssl_sha512
964
965 string as data_obj: object(py_default="b''") = NULL
Christian Heimes7cad53e2019-09-13 02:30:00 +0200966 *
967 usedforsecurity: bool = True
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200968
969Returns a sha512 hash object; optionally initialized with a string
970
971[clinic start generated code]*/
972
973static PyObject *
Christian Heimes7cad53e2019-09-13 02:30:00 +0200974_hashlib_openssl_sha512_impl(PyObject *module, PyObject *data_obj,
975 int usedforsecurity)
976/*[clinic end generated code: output=2c744c9e4a40d5f6 input=c5c46a2a817aa98f]*/
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200977{
Christian Heimes7cad53e2019-09-13 02:30:00 +0200978 return EVP_fast_new(module, data_obj, EVP_sha512(), usedforsecurity);
Christian Heimes5a4f82f2019-09-12 14:42:07 +0200979}
980
981
Christian Heimesd5b3f6b2020-05-16 22:27:06 +0200982#ifdef PY_OPENSSL_HAS_SHA3
983
984/*[clinic input]
985_hashlib.openssl_sha3_224
986
987 string as data_obj: object(py_default="b''") = NULL
988 *
989 usedforsecurity: bool = True
990
991Returns a sha3-224 hash object; optionally initialized with a string
992
993[clinic start generated code]*/
994
995static PyObject *
996_hashlib_openssl_sha3_224_impl(PyObject *module, PyObject *data_obj,
997 int usedforsecurity)
998/*[clinic end generated code: output=144641c1d144b974 input=e3a01b2888916157]*/
999{
1000 return EVP_fast_new(module, data_obj, EVP_sha3_224(), usedforsecurity);
1001}
1002
1003/*[clinic input]
1004_hashlib.openssl_sha3_256
1005
1006 string as data_obj: object(py_default="b''") = NULL
1007 *
1008 usedforsecurity: bool = True
1009
1010Returns a sha3-256 hash object; optionally initialized with a string
1011
1012[clinic start generated code]*/
1013
1014static PyObject *
1015_hashlib_openssl_sha3_256_impl(PyObject *module, PyObject *data_obj,
1016 int usedforsecurity)
1017/*[clinic end generated code: output=c61f1ab772d06668 input=e2908126c1b6deed]*/
1018{
1019 return EVP_fast_new(module, data_obj, EVP_sha3_256(), usedforsecurity);
1020}
1021
1022/*[clinic input]
1023_hashlib.openssl_sha3_384
1024
1025 string as data_obj: object(py_default="b''") = NULL
1026 *
1027 usedforsecurity: bool = True
1028
1029Returns a sha3-384 hash object; optionally initialized with a string
1030
1031[clinic start generated code]*/
1032
1033static PyObject *
1034_hashlib_openssl_sha3_384_impl(PyObject *module, PyObject *data_obj,
1035 int usedforsecurity)
1036/*[clinic end generated code: output=f68e4846858cf0ee input=ec0edf5c792f8252]*/
1037{
1038 return EVP_fast_new(module, data_obj, EVP_sha3_384(), usedforsecurity);
1039}
1040
1041/*[clinic input]
1042_hashlib.openssl_sha3_512
1043
1044 string as data_obj: object(py_default="b''") = NULL
1045 *
1046 usedforsecurity: bool = True
1047
1048Returns a sha3-512 hash object; optionally initialized with a string
1049
1050[clinic start generated code]*/
1051
1052static PyObject *
1053_hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data_obj,
1054 int usedforsecurity)
1055/*[clinic end generated code: output=2eede478c159354a input=64e2cc0c094d56f4]*/
1056{
1057 return EVP_fast_new(module, data_obj, EVP_sha3_512(), usedforsecurity);
1058}
1059#endif /* PY_OPENSSL_HAS_SHA3 */
1060
1061#ifdef PY_OPENSSL_HAS_SHAKE
1062/*[clinic input]
Christian Heimes62ecd8a2020-05-17 18:32:38 +02001063_hashlib.openssl_shake_128
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001064
1065 string as data_obj: object(py_default="b''") = NULL
1066 *
1067 usedforsecurity: bool = True
1068
Christian Heimes62ecd8a2020-05-17 18:32:38 +02001069Returns a shake-128 variable hash object; optionally initialized with a string
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001070
1071[clinic start generated code]*/
1072
1073static PyObject *
Christian Heimes62ecd8a2020-05-17 18:32:38 +02001074_hashlib_openssl_shake_128_impl(PyObject *module, PyObject *data_obj,
1075 int usedforsecurity)
1076/*[clinic end generated code: output=bc49cdd8ada1fa97 input=6c9d67440eb33ec8]*/
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001077{
1078 return EVP_fast_new(module, data_obj, EVP_shake128(), usedforsecurity);
1079}
1080
1081/*[clinic input]
Christian Heimes62ecd8a2020-05-17 18:32:38 +02001082_hashlib.openssl_shake_256
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001083
1084 string as data_obj: object(py_default="b''") = NULL
1085 *
1086 usedforsecurity: bool = True
1087
Christian Heimes62ecd8a2020-05-17 18:32:38 +02001088Returns a shake-256 variable hash object; optionally initialized with a string
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001089
1090[clinic start generated code]*/
1091
1092static PyObject *
Christian Heimes62ecd8a2020-05-17 18:32:38 +02001093_hashlib_openssl_shake_256_impl(PyObject *module, PyObject *data_obj,
1094 int usedforsecurity)
1095/*[clinic end generated code: output=358d213be8852df7 input=479cbe9fefd4a9f8]*/
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001096{
1097 return EVP_fast_new(module, data_obj, EVP_shake256(), usedforsecurity);
1098}
1099#endif /* PY_OPENSSL_HAS_SHAKE */
1100
Tal Einatc6c72372018-12-27 15:43:43 +02001101/*[clinic input]
1102_hashlib.pbkdf2_hmac as pbkdf2_hmac
1103
1104 hash_name: str
1105 password: Py_buffer
1106 salt: Py_buffer
1107 iterations: long
1108 dklen as dklen_obj: object = None
1109
1110Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as pseudorandom function.
1111[clinic start generated code]*/
Christian Heimese92ef132013-10-13 00:52:43 +02001112
1113static PyObject *
Tal Einatc6c72372018-12-27 15:43:43 +02001114pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
1115 Py_buffer *password, Py_buffer *salt, long iterations,
1116 PyObject *dklen_obj)
1117/*[clinic end generated code: output=144b76005416599b input=ed3ab0d2d28b5d5c]*/
Christian Heimese92ef132013-10-13 00:52:43 +02001118{
Tal Einatc6c72372018-12-27 15:43:43 +02001119 PyObject *key_obj = NULL;
1120 char *key;
1121 long dklen;
Christian Heimese92ef132013-10-13 00:52:43 +02001122 int retval;
1123 const EVP_MD *digest;
1124
Christian Heimes54f28982020-05-17 13:49:10 +02001125 digest = py_digest_by_name(hash_name);
Christian Heimese92ef132013-10-13 00:52:43 +02001126 if (digest == NULL) {
1127 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
1128 goto end;
1129 }
1130
Tal Einatc6c72372018-12-27 15:43:43 +02001131 if (password->len > INT_MAX) {
Christian Heimese92ef132013-10-13 00:52:43 +02001132 PyErr_SetString(PyExc_OverflowError,
1133 "password is too long.");
1134 goto end;
1135 }
1136
Tal Einatc6c72372018-12-27 15:43:43 +02001137 if (salt->len > INT_MAX) {
Christian Heimese92ef132013-10-13 00:52:43 +02001138 PyErr_SetString(PyExc_OverflowError,
1139 "salt is too long.");
1140 goto end;
1141 }
1142
1143 if (iterations < 1) {
1144 PyErr_SetString(PyExc_ValueError,
1145 "iteration value must be greater than 0.");
1146 goto end;
1147 }
1148 if (iterations > INT_MAX) {
1149 PyErr_SetString(PyExc_OverflowError,
1150 "iteration value is too great.");
1151 goto end;
1152 }
1153
1154 if (dklen_obj == Py_None) {
1155 dklen = EVP_MD_size(digest);
1156 } else {
1157 dklen = PyLong_AsLong(dklen_obj);
1158 if ((dklen == -1) && PyErr_Occurred()) {
1159 goto end;
1160 }
1161 }
1162 if (dklen < 1) {
1163 PyErr_SetString(PyExc_ValueError,
1164 "key length must be greater than 0.");
1165 goto end;
1166 }
1167 if (dklen > INT_MAX) {
1168 /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
1169 PyErr_SetString(PyExc_OverflowError,
1170 "key length is too great.");
1171 goto end;
1172 }
1173
1174 key_obj = PyBytes_FromStringAndSize(NULL, dklen);
1175 if (key_obj == NULL) {
1176 goto end;
1177 }
1178 key = PyBytes_AS_STRING(key_obj);
1179
1180 Py_BEGIN_ALLOW_THREADS
Tal Einatc6c72372018-12-27 15:43:43 +02001181 retval = PKCS5_PBKDF2_HMAC((char*)password->buf, (int)password->len,
1182 (unsigned char *)salt->buf, (int)salt->len,
Christian Heimes598894f2016-09-05 23:19:05 +02001183 iterations, digest, dklen,
1184 (unsigned char *)key);
Christian Heimese92ef132013-10-13 00:52:43 +02001185 Py_END_ALLOW_THREADS
1186
1187 if (!retval) {
1188 Py_CLEAR(key_obj);
1189 _setException(PyExc_ValueError);
1190 goto end;
1191 }
1192
1193 end:
Christian Heimese92ef132013-10-13 00:52:43 +02001194 return key_obj;
1195}
1196
Christian Heimes39093e92016-09-06 20:22:28 +02001197#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER)
1198#define PY_SCRYPT 1
1199
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +03001200/* XXX: Parameters salt, n, r and p should be required keyword-only parameters.
1201 They are optional in the Argument Clinic declaration only due to a
1202 limitation of PyArg_ParseTupleAndKeywords. */
1203
Christian Heimes39093e92016-09-06 20:22:28 +02001204/*[clinic input]
1205_hashlib.scrypt
1206
1207 password: Py_buffer
1208 *
1209 salt: Py_buffer = None
1210 n as n_obj: object(subclass_of='&PyLong_Type') = None
1211 r as r_obj: object(subclass_of='&PyLong_Type') = None
1212 p as p_obj: object(subclass_of='&PyLong_Type') = None
1213 maxmem: long = 0
1214 dklen: long = 64
1215
1216
1217scrypt password-based key derivation function.
1218[clinic start generated code]*/
1219
1220static PyObject *
1221_hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
1222 PyObject *n_obj, PyObject *r_obj, PyObject *p_obj,
1223 long maxmem, long dklen)
1224/*[clinic end generated code: output=14849e2aa2b7b46c input=48a7d63bf3f75c42]*/
1225{
1226 PyObject *key_obj = NULL;
1227 char *key;
1228 int retval;
1229 unsigned long n, r, p;
1230
1231 if (password->len > INT_MAX) {
1232 PyErr_SetString(PyExc_OverflowError,
1233 "password is too long.");
1234 return NULL;
1235 }
1236
1237 if (salt->buf == NULL) {
1238 PyErr_SetString(PyExc_TypeError,
1239 "salt is required");
1240 return NULL;
1241 }
1242 if (salt->len > INT_MAX) {
1243 PyErr_SetString(PyExc_OverflowError,
1244 "salt is too long.");
1245 return NULL;
1246 }
1247
1248 n = PyLong_AsUnsignedLong(n_obj);
1249 if (n == (unsigned long) -1 && PyErr_Occurred()) {
1250 PyErr_SetString(PyExc_TypeError,
1251 "n is required and must be an unsigned int");
1252 return NULL;
1253 }
1254 if (n < 2 || n & (n - 1)) {
1255 PyErr_SetString(PyExc_ValueError,
1256 "n must be a power of 2.");
1257 return NULL;
1258 }
1259
1260 r = PyLong_AsUnsignedLong(r_obj);
1261 if (r == (unsigned long) -1 && PyErr_Occurred()) {
1262 PyErr_SetString(PyExc_TypeError,
1263 "r is required and must be an unsigned int");
1264 return NULL;
1265 }
1266
1267 p = PyLong_AsUnsignedLong(p_obj);
1268 if (p == (unsigned long) -1 && PyErr_Occurred()) {
1269 PyErr_SetString(PyExc_TypeError,
1270 "p is required and must be an unsigned int");
1271 return NULL;
1272 }
1273
1274 if (maxmem < 0 || maxmem > INT_MAX) {
Victor Stinner8c663fd2017-11-08 14:44:44 -08001275 /* OpenSSL 1.1.0 restricts maxmem to 32 MiB. It may change in the
Christian Heimes39093e92016-09-06 20:22:28 +02001276 future. The maxmem constant is private to OpenSSL. */
1277 PyErr_Format(PyExc_ValueError,
1278 "maxmem must be positive and smaller than %d",
1279 INT_MAX);
1280 return NULL;
1281 }
1282
1283 if (dklen < 1 || dklen > INT_MAX) {
1284 PyErr_Format(PyExc_ValueError,
1285 "dklen must be greater than 0 and smaller than %d",
1286 INT_MAX);
1287 return NULL;
1288 }
1289
1290 /* let OpenSSL validate the rest */
1291 retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0);
1292 if (!retval) {
1293 /* sorry, can't do much better */
1294 PyErr_SetString(PyExc_ValueError,
Emmanuel Ariasb71e28e2019-03-06 11:35:35 -03001295 "Invalid parameter combination for n, r, p, maxmem.");
Christian Heimes39093e92016-09-06 20:22:28 +02001296 return NULL;
1297 }
1298
1299 key_obj = PyBytes_FromStringAndSize(NULL, dklen);
1300 if (key_obj == NULL) {
1301 return NULL;
1302 }
1303 key = PyBytes_AS_STRING(key_obj);
1304
1305 Py_BEGIN_ALLOW_THREADS
1306 retval = EVP_PBE_scrypt(
1307 (const char*)password->buf, (size_t)password->len,
1308 (const unsigned char *)salt->buf, (size_t)salt->len,
1309 n, r, p, maxmem,
1310 (unsigned char *)key, (size_t)dklen
1311 );
1312 Py_END_ALLOW_THREADS
1313
1314 if (!retval) {
1315 Py_CLEAR(key_obj);
1316 _setException(PyExc_ValueError);
1317 return NULL;
1318 }
1319 return key_obj;
1320}
1321#endif
1322
Christian Heimes2f050c72018-01-27 09:53:43 +01001323/* Fast HMAC for hmac.digest()
1324 */
1325
1326/*[clinic input]
Christian Heimes54f28982020-05-17 13:49:10 +02001327_hashlib.hmac_digest as _hashlib_hmac_singleshot
Christian Heimes2f050c72018-01-27 09:53:43 +01001328
1329 key: Py_buffer
1330 msg: Py_buffer
1331 digest: str
1332
Serhiy Storchakaf1d36d82018-07-31 09:50:16 +03001333Single-shot HMAC.
Christian Heimes2f050c72018-01-27 09:53:43 +01001334[clinic start generated code]*/
1335
1336static PyObject *
Christian Heimes54f28982020-05-17 13:49:10 +02001337_hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
1338 Py_buffer *msg, const char *digest)
1339/*[clinic end generated code: output=15658ede5ab98185 input=019dffc571909a46]*/
Christian Heimes2f050c72018-01-27 09:53:43 +01001340{
1341 unsigned char md[EVP_MAX_MD_SIZE] = {0};
1342 unsigned int md_len = 0;
1343 unsigned char *result;
1344 const EVP_MD *evp;
1345
Christian Heimes54f28982020-05-17 13:49:10 +02001346 evp = py_digest_by_name(digest);
Christian Heimes2f050c72018-01-27 09:53:43 +01001347 if (evp == NULL) {
1348 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
1349 return NULL;
1350 }
1351 if (key->len > INT_MAX) {
1352 PyErr_SetString(PyExc_OverflowError,
1353 "key is too long.");
1354 return NULL;
1355 }
1356 if (msg->len > INT_MAX) {
1357 PyErr_SetString(PyExc_OverflowError,
1358 "msg is too long.");
1359 return NULL;
1360 }
1361
1362 Py_BEGIN_ALLOW_THREADS
1363 result = HMAC(
1364 evp,
1365 (const void*)key->buf, (int)key->len,
1366 (const unsigned char*)msg->buf, (int)msg->len,
1367 md, &md_len
1368 );
1369 Py_END_ALLOW_THREADS
1370
1371 if (result == NULL) {
1372 _setException(PyExc_ValueError);
1373 return NULL;
1374 }
1375 return PyBytes_FromStringAndSize((const char*)md, md_len);
1376}
1377
Christian Heimes54f28982020-05-17 13:49:10 +02001378/* OpenSSL-based HMAC implementation
1379 */
1380
1381static int _hmac_update(HMACobject*, PyObject*);
1382
1383/*[clinic input]
1384_hashlib.hmac_new
1385
1386 key: Py_buffer
1387 msg as msg_obj: object(c_default="NULL") = b''
1388 digestmod: str(c_default="NULL") = None
1389
1390Return a new hmac object.
1391[clinic start generated code]*/
1392
1393static PyObject *
1394_hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
1395 const char *digestmod)
1396/*[clinic end generated code: output=9a35673be0cbea1b input=a0878868eb190134]*/
1397{
1398 PyTypeObject *type = get_hashlib_state(module)->HMACtype;
1399 const EVP_MD *digest;
1400 HMAC_CTX *ctx = NULL;
1401 HMACobject *self = NULL;
1402 int r;
1403
Christian Heimesaca46702020-05-20 00:35:51 +02001404 if (key->len > INT_MAX) {
1405 PyErr_SetString(PyExc_OverflowError,
1406 "key is too long.");
1407 return NULL;
1408 }
1409
Christian Heimes54f28982020-05-17 13:49:10 +02001410 if ((digestmod == NULL) || !strlen(digestmod)) {
1411 PyErr_SetString(
1412 PyExc_TypeError, "Missing required parameter 'digestmod'.");
1413 return NULL;
1414 }
1415
1416 digest = py_digest_by_name(digestmod);
1417 if (!digest) {
1418 PyErr_SetString(PyExc_ValueError, "unknown hash function");
1419 return NULL;
1420 }
1421
1422 ctx = HMAC_CTX_new();
1423 if (ctx == NULL) {
1424 _setException(PyExc_ValueError);
1425 goto error;
1426 }
1427
1428 r = HMAC_Init_ex(
1429 ctx,
1430 (const char*)key->buf,
Christian Heimesaca46702020-05-20 00:35:51 +02001431 (int)key->len,
Christian Heimes54f28982020-05-17 13:49:10 +02001432 digest,
1433 NULL /*impl*/);
1434 if (r == 0) {
1435 _setException(PyExc_ValueError);
1436 goto error;
1437 }
1438
1439 self = (HMACobject *)PyObject_New(HMACobject, type);
1440 if (self == NULL) {
1441 goto error;
1442 }
1443
1444 self->ctx = ctx;
1445 self->lock = NULL;
1446
1447 if ((msg_obj != NULL) && (msg_obj != Py_None)) {
1448 if (!_hmac_update(self, msg_obj))
1449 goto error;
1450 }
1451
1452 return (PyObject*)self;
1453
1454error:
1455 if (ctx) HMAC_CTX_free(ctx);
1456 if (self) PyObject_Del(self);
1457 return NULL;
1458}
1459
1460/* helper functions */
1461static int
1462locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self)
1463{
1464 int result;
1465 ENTER_HASHLIB(self);
1466 result = HMAC_CTX_copy(new_ctx_p, self->ctx);
1467 LEAVE_HASHLIB(self);
1468 return result;
1469}
1470
1471static unsigned int
1472_hmac_digest_size(HMACobject *self)
1473{
1474 unsigned int digest_size = EVP_MD_size(HMAC_CTX_get_md(self->ctx));
1475 assert(digest_size <= EVP_MAX_MD_SIZE);
1476 return digest_size;
1477}
1478
1479static int
1480_hmac_update(HMACobject *self, PyObject *obj)
1481{
1482 int r;
1483 Py_buffer view = {0};
1484
1485 GET_BUFFER_VIEW_OR_ERROR(obj, &view, return 0);
1486
1487 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
1488 self->lock = PyThread_allocate_lock();
1489 /* fail? lock = NULL and we fail over to non-threaded code. */
1490 }
1491
1492 if (self->lock != NULL) {
1493 ENTER_HASHLIB(self);
1494 r = HMAC_Update(self->ctx, (const unsigned char*)view.buf, view.len);
1495 LEAVE_HASHLIB(self);
1496 } else {
1497 r = HMAC_Update(self->ctx, (const unsigned char*)view.buf, view.len);
1498 }
1499
1500 PyBuffer_Release(&view);
1501
1502 if (r == 0) {
1503 _setException(PyExc_ValueError);
1504 return 0;
1505 }
1506 return 1;
1507}
1508
1509/*[clinic input]
1510_hashlib.HMAC.copy
1511
1512Return a copy ("clone") of the HMAC object.
1513[clinic start generated code]*/
1514
1515static PyObject *
1516_hashlib_HMAC_copy_impl(HMACobject *self)
1517/*[clinic end generated code: output=29aa28b452833127 input=e2fa6a05db61a4d6]*/
1518{
1519 HMACobject *retval;
1520
1521 HMAC_CTX *ctx = HMAC_CTX_new();
1522 if (ctx == NULL) {
1523 return _setException(PyExc_ValueError);
1524 }
1525 if (!locked_HMAC_CTX_copy(ctx, self)) {
1526 HMAC_CTX_free(ctx);
1527 return _setException(PyExc_ValueError);
1528 }
1529
1530 retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self));
1531 if (retval == NULL) {
1532 HMAC_CTX_free(ctx);
1533 return NULL;
1534 }
1535 retval->ctx = ctx;
1536 retval->lock = NULL;
1537
1538 return (PyObject *)retval;
1539}
1540
1541static void
1542_hmac_dealloc(HMACobject *self)
1543{
1544 PyTypeObject *tp = Py_TYPE(self);
1545 if (self->lock != NULL) {
1546 PyThread_free_lock(self->lock);
1547 }
1548 HMAC_CTX_free(self->ctx);
1549 PyObject_Del(self);
1550 Py_DECREF(tp);
1551}
1552
1553static PyObject *
1554_hmac_repr(HMACobject *self)
1555{
1556 PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx));
1557 if (digest_name == NULL) {
1558 return NULL;
1559 }
1560 PyObject *repr = PyUnicode_FromFormat(
1561 "<%U HMAC object @ %p>", digest_name, self
1562 );
1563 Py_DECREF(digest_name);
1564 return repr;
1565}
1566
1567/*[clinic input]
1568_hashlib.HMAC.update
1569 msg: object
1570
1571Update the HMAC object with msg.
1572[clinic start generated code]*/
1573
1574static PyObject *
1575_hashlib_HMAC_update_impl(HMACobject *self, PyObject *msg)
1576/*[clinic end generated code: output=f31f0ace8c625b00 input=1829173bb3cfd4e6]*/
1577{
1578 if (!_hmac_update(self, msg)) {
1579 return NULL;
1580 }
1581 Py_RETURN_NONE;
1582}
1583
1584static int
1585_hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
1586{
1587 HMAC_CTX *temp_ctx = HMAC_CTX_new();
1588 if (temp_ctx == NULL) {
1589 PyErr_NoMemory();
1590 return 0;
1591 }
1592 if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
1593 _setException(PyExc_ValueError);
1594 return 0;
1595 }
1596 int r = HMAC_Final(temp_ctx, buf, &len);
1597 HMAC_CTX_free(temp_ctx);
1598 if (r == 0) {
1599 _setException(PyExc_ValueError);
1600 return 0;
1601 }
1602 return 1;
1603}
1604
1605/*[clinic input]
1606_hashlib.HMAC.digest
1607Return the digest of the bytes passed to the update() method so far.
1608[clinic start generated code]*/
1609
1610static PyObject *
1611_hashlib_HMAC_digest_impl(HMACobject *self)
1612/*[clinic end generated code: output=1b1424355af7a41e input=bff07f74da318fb4]*/
1613{
1614 unsigned char digest[EVP_MAX_MD_SIZE];
1615 unsigned int digest_size = _hmac_digest_size(self);
1616 if (digest_size == 0) {
1617 return _setException(PyExc_ValueError);
1618 }
1619 int r = _hmac_digest(self, digest, digest_size);
1620 if (r == 0) {
1621 return NULL;
1622 }
1623 return PyBytes_FromStringAndSize((const char *)digest, digest_size);
1624}
1625
1626/*[clinic input]
1627_hashlib.HMAC.hexdigest
1628
1629Return hexadecimal digest of the bytes passed to the update() method so far.
1630
1631This may be used to exchange the value safely in email or other non-binary
1632environments.
1633[clinic start generated code]*/
1634
1635static PyObject *
1636_hashlib_HMAC_hexdigest_impl(HMACobject *self)
1637/*[clinic end generated code: output=80d825be1eaae6a7 input=5abc42702874ddcf]*/
1638{
1639 unsigned char digest[EVP_MAX_MD_SIZE];
1640 unsigned int digest_size = _hmac_digest_size(self);
1641 if (digest_size == 0) {
1642 return _setException(PyExc_ValueError);
1643 }
1644 int r = _hmac_digest(self, digest, digest_size);
1645 if (r == 0) {
1646 return NULL;
1647 }
1648 return _Py_strhex((const char *)digest, digest_size);
1649}
1650
1651static PyObject *
1652_hashlib_hmac_get_digest_size(HMACobject *self, void *closure)
1653{
1654 unsigned int digest_size = _hmac_digest_size(self);
1655 if (digest_size == 0) {
1656 return _setException(PyExc_ValueError);
1657 }
1658 return PyLong_FromLong(digest_size);
1659}
1660
1661static PyObject *
1662_hashlib_hmac_get_block_size(HMACobject *self, void *closure)
1663{
1664 const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
1665 if (md == NULL) {
1666 return _setException(PyExc_ValueError);
1667 }
1668 return PyLong_FromLong(EVP_MD_block_size(md));
1669}
1670
1671static PyObject *
1672_hashlib_hmac_get_name(HMACobject *self, void *closure)
1673{
1674 PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx));
1675 if (digest_name == NULL) {
1676 return NULL;
1677 }
1678 PyObject *name = PyUnicode_FromFormat("hmac-%U", digest_name);
1679 Py_DECREF(digest_name);
1680 return name;
1681}
1682
1683static PyMethodDef HMAC_methods[] = {
1684 _HASHLIB_HMAC_UPDATE_METHODDEF
1685 _HASHLIB_HMAC_DIGEST_METHODDEF
1686 _HASHLIB_HMAC_HEXDIGEST_METHODDEF
1687 _HASHLIB_HMAC_COPY_METHODDEF
1688 {NULL, NULL} /* sentinel */
1689};
1690
1691static PyGetSetDef HMAC_getset[] = {
1692 {"digest_size", (getter)_hashlib_hmac_get_digest_size, NULL, NULL, NULL},
1693 {"block_size", (getter)_hashlib_hmac_get_block_size, NULL, NULL, NULL},
1694 {"name", (getter)_hashlib_hmac_get_name, NULL, NULL, NULL},
1695 {NULL} /* Sentinel */
1696};
1697
1698
1699PyDoc_STRVAR(hmactype_doc,
1700"The object used to calculate HMAC of a message.\n\
1701\n\
1702Methods:\n\
1703\n\
1704update() -- updates the current digest with an additional string\n\
1705digest() -- return the current digest value\n\
1706hexdigest() -- return the current digest as a string of hexadecimal digits\n\
1707copy() -- return a copy of the current hash object\n\
1708\n\
1709Attributes:\n\
1710\n\
1711name -- the name, including the hash algorithm used by this object\n\
1712digest_size -- number of bytes in digest() output\n");
1713
1714static PyType_Slot HMACtype_slots[] = {
1715 {Py_tp_doc, (char *)hmactype_doc},
1716 {Py_tp_repr, (reprfunc)_hmac_repr},
1717 {Py_tp_dealloc,(destructor)_hmac_dealloc},
1718 {Py_tp_methods, HMAC_methods},
1719 {Py_tp_getset, HMAC_getset},
1720 {Py_tp_new, _disabled_new},
1721 {0, NULL}
1722};
1723
1724PyType_Spec HMACtype_spec = {
1725 "_hashlib.HMAC", /* name */
1726 sizeof(HMACobject), /* basicsize */
1727 .flags = Py_TPFLAGS_DEFAULT,
1728 .slots = HMACtype_slots,
1729};
1730
1731
Gregory P. Smith13b55292010-09-06 08:30:23 +00001732/* State for our callback function so that it can accumulate a result. */
1733typedef struct _internal_name_mapper_state {
1734 PyObject *set;
1735 int error;
1736} _InternalNameMapperState;
1737
1738
1739/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
1740static void
Christian Heimes995b5d32019-09-13 15:31:19 +02001741_openssl_hash_name_mapper(const EVP_MD *md, const char *from,
1742 const char *to, void *arg)
Gregory P. Smith13b55292010-09-06 08:30:23 +00001743{
1744 _InternalNameMapperState *state = (_InternalNameMapperState *)arg;
1745 PyObject *py_name;
1746
1747 assert(state != NULL);
Christian Heimes995b5d32019-09-13 15:31:19 +02001748 if (md == NULL)
Gregory P. Smith13b55292010-09-06 08:30:23 +00001749 return;
1750
Christian Heimes995b5d32019-09-13 15:31:19 +02001751 py_name = py_digest_name(md);
Gregory P. Smith13b55292010-09-06 08:30:23 +00001752 if (py_name == NULL) {
1753 state->error = 1;
1754 } else {
1755 if (PySet_Add(state->set, py_name) != 0) {
Gregory P. Smith13b55292010-09-06 08:30:23 +00001756 state->error = 1;
1757 }
Christian Heimesdb816d62013-10-29 12:14:55 +01001758 Py_DECREF(py_name);
Gregory P. Smith13b55292010-09-06 08:30:23 +00001759 }
1760}
1761
1762
1763/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
Christian Heimes20c22db2020-05-25 10:44:51 +02001764static int
1765hashlib_md_meth_names(PyObject *module)
Gregory P. Smith13b55292010-09-06 08:30:23 +00001766{
Christian Heimes20c22db2020-05-25 10:44:51 +02001767 _InternalNameMapperState state = {
1768 .set = PyFrozenSet_New(NULL),
1769 .error = 0
1770 };
1771 if (state.set == NULL) {
1772 return -1;
1773 }
Gregory P. Smith13b55292010-09-06 08:30:23 +00001774
Christian Heimes995b5d32019-09-13 15:31:19 +02001775 EVP_MD_do_all(&_openssl_hash_name_mapper, &state);
Gregory P. Smith13b55292010-09-06 08:30:23 +00001776
1777 if (state.error) {
1778 Py_DECREF(state.set);
Christian Heimes20c22db2020-05-25 10:44:51 +02001779 return -1;
Gregory P. Smith13b55292010-09-06 08:30:23 +00001780 }
Christian Heimes20c22db2020-05-25 10:44:51 +02001781
1782 if (PyModule_AddObject(module, "openssl_md_meth_names", state.set) < 0) {
1783 Py_DECREF(state.set);
1784 return -1;
1785 }
1786
1787 return 0;
Gregory P. Smith13b55292010-09-06 08:30:23 +00001788}
1789
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001790/* LibreSSL doesn't support FIPS:
1791 https://marc.info/?l=openbsd-misc&m=139819485423701&w=2
1792
1793 Ted Unangst wrote: "I figured I should mention our current libressl policy
1794 wrt FIPS mode. It's gone and it's not coming back." */
1795#ifndef LIBRESSL_VERSION_NUMBER
1796/*[clinic input]
1797_hashlib.get_fips_mode -> int
1798
1799Determine the OpenSSL FIPS mode of operation.
1800
Christian Heimes16d4e6f2020-05-15 18:28:05 +02001801For OpenSSL 3.0.0 and newer it returns the state of the default provider
1802in the default OSSL context. It's not quite the same as FIPS_mode() but good
1803enough for unittests.
1804
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001805Effectively any non-zero return value indicates FIPS mode;
1806values other than 1 may have additional significance.
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001807[clinic start generated code]*/
1808
1809static int
1810_hashlib_get_fips_mode_impl(PyObject *module)
Christian Heimes16d4e6f2020-05-15 18:28:05 +02001811/*[clinic end generated code: output=87eece1bab4d3fa9 input=2db61538c41c6fef]*/
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001812
1813{
Christian Heimes16d4e6f2020-05-15 18:28:05 +02001814 int result;
1815#if OPENSSL_VERSION_NUMBER >= 0x30000000L
1816 result = EVP_default_properties_is_fips_enabled(NULL);
1817#else
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001818 ERR_clear_error();
Christian Heimes16d4e6f2020-05-15 18:28:05 +02001819 result = FIPS_mode();
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001820 if (result == 0) {
1821 // "If the library was built without support of the FIPS Object Module,
1822 // then the function will return 0 with an error code of
1823 // CRYPTO_R_FIPS_MODE_NOT_SUPPORTED (0x0f06d065)."
1824 // But 0 is also a valid result value.
1825 unsigned long errcode = ERR_peek_last_error();
1826 if (errcode) {
1827 _setException(PyExc_ValueError);
1828 return -1;
1829 }
1830 }
1831 return result;
Christian Heimes16d4e6f2020-05-15 18:28:05 +02001832#endif
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001833}
1834#endif // !LIBRESSL_VERSION_NUMBER
1835
1836
Christian Heimesdb5aed92020-05-27 21:50:06 +02001837static int
1838_tscmp(const unsigned char *a, const unsigned char *b,
1839 Py_ssize_t len_a, Py_ssize_t len_b)
1840{
1841 /* loop count depends on length of b. Might leak very little timing
1842 * information if sizes are different.
1843 */
1844 Py_ssize_t length = len_b;
1845 const void *left = a;
1846 const void *right = b;
1847 int result = 0;
1848
1849 if (len_a != length) {
1850 left = b;
1851 result = 1;
1852 }
1853
1854 result |= CRYPTO_memcmp(left, right, length);
1855
1856 return (result == 0);
1857}
1858
1859/* NOTE: Keep in sync with _operator.c implementation. */
1860
1861/*[clinic input]
1862_hashlib.compare_digest
1863
1864 a: object
1865 b: object
1866 /
1867
1868Return 'a == b'.
1869
1870This function uses an approach designed to prevent
1871timing analysis, making it appropriate for cryptography.
1872
1873a and b must both be of the same type: either str (ASCII only),
1874or any bytes-like object.
1875
1876Note: If a and b are of different lengths, or if an error occurs,
1877a timing attack could theoretically reveal information about the
1878types and lengths of a and b--but not their values.
1879[clinic start generated code]*/
1880
1881static PyObject *
1882_hashlib_compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
1883/*[clinic end generated code: output=6f1c13927480aed9 input=9c40c6e566ca12f5]*/
1884{
1885 int rc;
1886
1887 /* ASCII unicode string */
1888 if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
1889 if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
1890 return NULL;
1891 }
1892 if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
1893 PyErr_SetString(PyExc_TypeError,
1894 "comparing strings with non-ASCII characters is "
1895 "not supported");
1896 return NULL;
1897 }
1898
1899 rc = _tscmp(PyUnicode_DATA(a),
1900 PyUnicode_DATA(b),
1901 PyUnicode_GET_LENGTH(a),
1902 PyUnicode_GET_LENGTH(b));
1903 }
1904 /* fallback to buffer interface for bytes, bytesarray and other */
1905 else {
1906 Py_buffer view_a;
1907 Py_buffer view_b;
1908
1909 if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
1910 PyErr_Format(PyExc_TypeError,
1911 "unsupported operand types(s) or combination of types: "
1912 "'%.100s' and '%.100s'",
1913 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
1914 return NULL;
1915 }
1916
1917 if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
1918 return NULL;
1919 }
1920 if (view_a.ndim > 1) {
1921 PyErr_SetString(PyExc_BufferError,
1922 "Buffer must be single dimension");
1923 PyBuffer_Release(&view_a);
1924 return NULL;
1925 }
1926
1927 if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
1928 PyBuffer_Release(&view_a);
1929 return NULL;
1930 }
1931 if (view_b.ndim > 1) {
1932 PyErr_SetString(PyExc_BufferError,
1933 "Buffer must be single dimension");
1934 PyBuffer_Release(&view_a);
1935 PyBuffer_Release(&view_b);
1936 return NULL;
1937 }
1938
1939 rc = _tscmp((const unsigned char*)view_a.buf,
1940 (const unsigned char*)view_b.buf,
1941 view_a.len,
1942 view_b.len);
1943
1944 PyBuffer_Release(&view_a);
1945 PyBuffer_Release(&view_b);
1946 }
1947
1948 return PyBool_FromLong(rc);
1949}
1950
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001951/* List of functions exported by this module */
1952
1953static struct PyMethodDef EVP_functions[] = {
Tal Einatc6c72372018-12-27 15:43:43 +02001954 EVP_NEW_METHODDEF
Tal Einatc6c72372018-12-27 15:43:43 +02001955 PBKDF2_HMAC_METHODDEF
Christian Heimes39093e92016-09-06 20:22:28 +02001956 _HASHLIB_SCRYPT_METHODDEF
Victor Stinnere3dfb9b2020-04-29 18:04:22 +02001957 _HASHLIB_GET_FIPS_MODE_METHODDEF
Christian Heimesdb5aed92020-05-27 21:50:06 +02001958 _HASHLIB_COMPARE_DIGEST_METHODDEF
Christian Heimes54f28982020-05-17 13:49:10 +02001959 _HASHLIB_HMAC_SINGLESHOT_METHODDEF
1960 _HASHLIB_HMAC_NEW_METHODDEF
Christian Heimes5a4f82f2019-09-12 14:42:07 +02001961 _HASHLIB_OPENSSL_MD5_METHODDEF
1962 _HASHLIB_OPENSSL_SHA1_METHODDEF
1963 _HASHLIB_OPENSSL_SHA224_METHODDEF
1964 _HASHLIB_OPENSSL_SHA256_METHODDEF
1965 _HASHLIB_OPENSSL_SHA384_METHODDEF
1966 _HASHLIB_OPENSSL_SHA512_METHODDEF
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001967 _HASHLIB_OPENSSL_SHA3_224_METHODDEF
1968 _HASHLIB_OPENSSL_SHA3_256_METHODDEF
1969 _HASHLIB_OPENSSL_SHA3_384_METHODDEF
1970 _HASHLIB_OPENSSL_SHA3_512_METHODDEF
Christian Heimes62ecd8a2020-05-17 18:32:38 +02001971 _HASHLIB_OPENSSL_SHAKE_128_METHODDEF
1972 _HASHLIB_OPENSSL_SHAKE_256_METHODDEF
Alexander Belopolskyf0f45142010-08-11 17:31:17 +00001973 {NULL, NULL} /* Sentinel */
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00001974};
1975
1976
1977/* Initialize this module. */
1978
Christian Heimesdf69e752019-09-25 23:03:30 +02001979static int
1980hashlib_traverse(PyObject *m, visitproc visit, void *arg)
1981{
Hai Shif707d942020-03-16 21:15:01 +08001982 _hashlibstate *state = get_hashlib_state(m);
Christian Heimesdf69e752019-09-25 23:03:30 +02001983 Py_VISIT(state->EVPtype);
Christian Heimes54f28982020-05-17 13:49:10 +02001984 Py_VISIT(state->HMACtype);
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001985#ifdef PY_OPENSSL_HAS_SHAKE
1986 Py_VISIT(state->EVPXOFtype);
1987#endif
Christian Heimesdf69e752019-09-25 23:03:30 +02001988 return 0;
1989}
1990
1991static int
1992hashlib_clear(PyObject *m)
1993{
Hai Shif707d942020-03-16 21:15:01 +08001994 _hashlibstate *state = get_hashlib_state(m);
Christian Heimesdf69e752019-09-25 23:03:30 +02001995 Py_CLEAR(state->EVPtype);
Christian Heimes54f28982020-05-17 13:49:10 +02001996 Py_CLEAR(state->HMACtype);
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02001997#ifdef PY_OPENSSL_HAS_SHAKE
1998 Py_CLEAR(state->EVPXOFtype);
1999#endif
Christian Heimesdf69e752019-09-25 23:03:30 +02002000 return 0;
2001}
2002
2003static void
2004hashlib_free(void *m)
2005{
2006 hashlib_clear((PyObject *)m);
2007}
2008
Christian Heimes20c22db2020-05-25 10:44:51 +02002009/* Py_mod_exec functions */
2010static int
2011hashlib_openssl_legacy_init(PyObject *module)
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00002012{
Christian Heimes724f1a52019-09-16 21:10:05 +02002013#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
Christian Heimesc941e622017-09-05 15:47:11 +02002014 /* Load all digest algorithms and initialize cpuid */
2015 OPENSSL_add_all_algorithms_noconf();
Christian Heimesb7ddbc82013-10-21 19:48:22 +02002016 ERR_load_crypto_strings();
Christian Heimesc941e622017-09-05 15:47:11 +02002017#endif
Christian Heimes20c22db2020-05-25 10:44:51 +02002018 return 0;
2019}
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00002020
Christian Heimes20c22db2020-05-25 10:44:51 +02002021static int
2022hashlib_init_evptype(PyObject *module)
2023{
2024 _hashlibstate *state = get_hashlib_state(module);
2025
2026 state->EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
2027 if (state->EVPtype == NULL) {
2028 return -1;
2029 }
2030 if (PyModule_AddType(module, state->EVPtype) < 0) {
2031 return -1;
2032 }
2033 return 0;
2034}
2035
2036static int
2037hashlib_init_evpxoftype(PyObject *module)
2038{
2039#ifdef PY_OPENSSL_HAS_SHAKE
2040 _hashlibstate *state = get_hashlib_state(module);
2041 PyObject *bases;
2042
2043 if (state->EVPtype == NULL) {
2044 return -1;
2045 }
2046
2047 bases = PyTuple_Pack(1, state->EVPtype);
2048 if (bases == NULL) {
2049 return -1;
2050 }
2051
2052 state->EVPXOFtype = (PyTypeObject *)PyType_FromSpecWithBases(
2053 &EVPXOFtype_spec, bases
2054 );
2055 Py_DECREF(bases);
2056 if (state->EVPXOFtype == NULL) {
2057 return -1;
2058 }
2059 if (PyModule_AddType(module, state->EVPXOFtype) < 0) {
2060 return -1;
2061 }
2062#endif
2063 return 0;
2064}
2065
2066static int
2067hashlib_init_hmactype(PyObject *module)
2068{
2069 _hashlibstate *state = get_hashlib_state(module);
2070
2071 state->HMACtype = (PyTypeObject *)PyType_FromSpec(&HMACtype_spec);
2072 if (state->HMACtype == NULL) {
2073 return -1;
2074 }
2075 if (PyModule_AddType(module, state->HMACtype) < 0) {
2076 return -1;
2077 }
2078 return 0;
2079}
2080
2081#if 0
2082static PyModuleDef_Slot hashlib_slots[] = {
2083 /* OpenSSL 1.0.2 and LibreSSL */
2084 {Py_mod_exec, hashlib_openssl_legacy_init},
2085 {Py_mod_exec, hashlib_init_evptype},
2086 {Py_mod_exec, hashlib_init_evpxoftype},
2087 {Py_mod_exec, hashlib_init_hmactype},
2088 {Py_mod_exec, hashlib_md_meth_names},
2089 {0, NULL}
2090};
2091#endif
2092
2093static struct PyModuleDef _hashlibmodule = {
2094 PyModuleDef_HEAD_INIT,
2095 .m_name = "_hashlib",
2096 .m_doc = "OpenSSL interface for hashlib module",
2097 .m_size = sizeof(_hashlibstate),
2098 .m_methods = EVP_functions,
2099 .m_slots = NULL,
2100 .m_traverse = hashlib_traverse,
2101 .m_clear = hashlib_clear,
2102 .m_free = hashlib_free
2103};
2104
2105PyMODINIT_FUNC
2106PyInit__hashlib(void)
2107{
2108 PyObject *m = PyState_FindModule(&_hashlibmodule);
Christian Heimesdf69e752019-09-25 23:03:30 +02002109 if (m != NULL) {
2110 Py_INCREF(m);
2111 return m;
2112 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00002113
Martin v. Löwis1a214512008-06-11 05:26:20 +00002114 m = PyModule_Create(&_hashlibmodule);
Christian Heimes20c22db2020-05-25 10:44:51 +02002115 if (m == NULL) {
Martin v. Löwis1a214512008-06-11 05:26:20 +00002116 return NULL;
Christian Heimes20c22db2020-05-25 10:44:51 +02002117 }
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00002118
Christian Heimes20c22db2020-05-25 10:44:51 +02002119 if (hashlib_openssl_legacy_init(m) < 0) {
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02002120 Py_DECREF(m);
Christian Heimesdf69e752019-09-25 23:03:30 +02002121 return NULL;
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02002122 }
Christian Heimes20c22db2020-05-25 10:44:51 +02002123 if (hashlib_init_evptype(m) < 0) {
Christian Heimes54f28982020-05-17 13:49:10 +02002124 Py_DECREF(m);
2125 return NULL;
2126 }
Christian Heimes20c22db2020-05-25 10:44:51 +02002127 if (hashlib_init_evpxoftype(m) < 0) {
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02002128 Py_DECREF(m);
2129 return NULL;
2130 }
Christian Heimes20c22db2020-05-25 10:44:51 +02002131 if (hashlib_init_hmactype(m) < 0) {
Christian Heimesd5b3f6b2020-05-16 22:27:06 +02002132 Py_DECREF(m);
2133 return NULL;
2134 }
Christian Heimes20c22db2020-05-25 10:44:51 +02002135 if (hashlib_md_meth_names(m) == -1) {
Gregory P. Smith13b55292010-09-06 08:30:23 +00002136 Py_DECREF(m);
2137 return NULL;
2138 }
2139
Martin v. Löwis1a214512008-06-11 05:26:20 +00002140 return m;
Gregory P. Smithf21a5f72005-08-21 18:45:59 +00002141}