1From 3d3b232e1359e189884cac83095b06312e3c82e0 Mon Sep 17 00:00:00 2001
2From: Michael Forney <mforney@mforney.org>
3Date: Sat, 15 May 2021 22:48:13 -0700
4Subject: [PATCH] Add BearSSL implementation of _hashlib
5
6---
7 Modules/_hashbearssl.c | 1129 +++++++++++++++++++++++++
8 Modules/clinic/_hashbearssl.c.h | 1401 +++++++++++++++++++++++++++++++
9 2 files changed, 2530 insertions(+)
10 create mode 100644 Modules/_hashbearssl.c
11 create mode 100644 Modules/clinic/_hashbearssl.c.h
12
13diff --git a/Modules/_hashbearssl.c b/Modules/_hashbearssl.c
14new file mode 100644
15index 00000000000..543fb2c0813
16--- /dev/null
17+++ b/Modules/_hashbearssl.c
18@@ -0,0 +1,1129 @@
19+/* Module that wraps all BearSSL hash algorithms */
20+#ifndef Py_BUILD_CORE_BUILTIN
21+# define Py_BUILD_CORE_MODULE 1
22+#endif
23+#include "Python.h"
24+#include "pycore_strhex.h" // _Py_strhex()
25+#include "hashlib.h"
26+
27+#include <bearssl.h>
28+
29+static PyModuleDef _hashlibmodule;
30+static PyTypeObject HASH_Type;
31+static PyTypeObject HASHXOF_Type;
32+static PyTypeObject HMAC_Type;
33+
34+// --- Module objects ---------------------------------------------------------
35+
36+typedef struct {
37+ HASHLIB_OBJECT_HEAD
38+ br_hash_compat_context ctx;
39+} HASH;
40+
41+typedef struct {
42+ HASHLIB_OBJECT_HEAD
43+ br_hmac_context ctx;
44+ br_hmac_key_context key;
45+} HMAC;
46+
47+typedef struct {
48+ HASHLIB_OBJECT_HEAD
49+ br_shake_context ctx;
50+ int bits;
51+} HASHXOF;
52+
53+// --- Module clinic configuration --------------------------------------------
54+
55+/*[clinic input]
56+module _hashlib
57+class _hashlib.HASH "HASH *" "HASH_Type"
58+class _hashlib.HASHXOF "HASHXOF *" "HASHXOF_Type"
59+class _hashlib.HMAC "HMAC *" "HMAC_Type"
60+[clinic start generated code]*/
61+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=213641e4849871ea]*/
62+
63+#include "clinic/_hashbearssl.c.h"
64+
65+static const br_hash_class *
66+py_hash_by_name(const char *name)
67+{
68+ if (PyOS_stricmp(name, "md5") == 0)
69+ return &br_md5_vtable;
70+ else if (PyOS_stricmp(name, "sha1") == 0)
71+ return &br_sha1_vtable;
72+ else if (PyOS_stricmp(name, "sha224") == 0)
73+ return &br_sha224_vtable;
74+ else if (PyOS_stricmp(name, "sha256") == 0)
75+ return &br_sha256_vtable;
76+ else if (PyOS_stricmp(name, "sha384") == 0)
77+ return &br_sha384_vtable;
78+ else if (PyOS_stricmp(name, "sha512") == 0)
79+ return &br_sha512_vtable;
80+ return NULL;
81+}
82+
83+PyDoc_STRVAR(HASH_doc,
84+"HASH(name, string=b\'\')\n"
85+"--\n"
86+"\n"
87+"A hash is an object used to calculate a checksum of a string of information.\n"
88+"\n"
89+"Methods:\n"
90+"\n"
91+"update() -- updates the current digest with an additional string\n"
92+"digest() -- return the current digest value\n"
93+"hexdigest() -- return the current digest as a string of hexadecimal digits\n"
94+"copy() -- return a copy of the current hash object\n"
95+"\n"
96+"Attributes:\n"
97+"\n"
98+"name -- the hash algorithm being used by this object\n"
99+"digest_size -- number of bytes in this hashes output");
100+
101+static void
102+HASH_dealloc(HASH *self)
103+{
104+ PyObject_Free(self);
105+}
106+
107+/*[clinic input]
108+_hashlib.HASH.update as HASH_update
109+
110+ data: Py_buffer
111+ /
112+
113+Update this hash object's state with the provided string.
114+[clinic start generated code]*/
115+
116+static PyObject *
117+HASH_update_impl(HASH *self, Py_buffer *data)
118+/*[clinic end generated code: output=e7371e3b82c78813 input=8d134669e7bfa813]*/
119+{
120+ HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(
121+ self, data->len,
122+ self->ctx.vtable->update(&self->ctx.vtable, data->buf, data->len)
123+ );
124+ Py_RETURN_NONE;
125+}
126+
127+/*[clinic input]
128+_hashlib.HASH.digest as HASH_digest
129+
130+Return the digest value as a bytes object.
131+[clinic start generated code]*/
132+
133+static PyObject *
134+HASH_digest_impl(HASH *self)
135+/*[clinic end generated code: output=b48bf00fd5875e49 input=9279858aec58f77a]*/
136+{
137+ char digest[64];
138+ Py_ssize_t digest_size;
139+
140+ HASHLIB_ACQUIRE_LOCK(self);
141+ self->ctx.vtable->out(&self->ctx.vtable, digest);
142+ HASHLIB_RELEASE_LOCK(self);
143+ digest_size = (self->ctx.vtable->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
144+
145+ return PyBytes_FromStringAndSize(digest, digest_size);
146+}
147+
148+/*[clinic input]
149+_hashlib.HASH.hexdigest as HASH_hexdigest
150+
151+Return the digest value as a string of hexadecimal digits.
152+[clinic start generated code]*/
153+
154+static PyObject *
155+HASH_hexdigest_impl(HASH *self)
156+/*[clinic end generated code: output=c94a721f9bd8a703 input=2947eb48a8cb30b8]*/
157+{
158+ char digest[64];
159+ Py_ssize_t digest_size;
160+
161+ HASHLIB_ACQUIRE_LOCK(self);
162+ self->ctx.vtable->out(&self->ctx.vtable, digest);
163+ HASHLIB_RELEASE_LOCK(self);
164+ digest_size = (self->ctx.vtable->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
165+
166+ return _Py_strhex(digest, digest_size);
167+}
168+
169+/*[clinic input]
170+_hashlib.HASH.copy as HASH_copy
171+
172+Return a copy of the hash object.
173+[clinic start generated code]*/
174+
175+static PyObject *
176+HASH_copy_impl(HASH *self)
177+/*[clinic end generated code: output=83f55894ab63fa16 input=648c01014857e870]*/
178+{
179+ HASH *newobj;
180+
181+ newobj = PyObject_New(HASH, &HASH_Type);
182+ if (!newobj)
183+ return NULL;
184+ HASHLIB_INIT_MUTEX(newobj);
185+ HASHLIB_ACQUIRE_LOCK(self);
186+ newobj->ctx = self->ctx;
187+ HASHLIB_RELEASE_LOCK(self);
188+
189+ return (PyObject *)newobj;
190+}
191+
192+static PyMethodDef HASH_methods[] = {
193+ HASH_UPDATE_METHODDEF
194+ HASH_DIGEST_METHODDEF
195+ HASH_HEXDIGEST_METHODDEF
196+ HASH_COPY_METHODDEF
197+ {0}
198+};
199+
200+static PyObject *
201+HASH_get_digest_size(HASH *self, void *closure)
202+{
203+ long digest_size;
204+
205+ digest_size = self->ctx.vtable->desc >> BR_HASHDESC_OUT_OFF & BR_HASHDESC_OUT_MASK;
206+ return PyLong_FromLong(digest_size);
207+}
208+
209+static PyObject *
210+HASH_get_block_size(HASH *self, void *closure)
211+{
212+ long block_size;
213+
214+ block_size = 1 << (self->ctx.vtable->desc >> BR_HASHDESC_LBLEN_OFF & BR_HASHDESC_LBLEN_MASK);
215+ return PyLong_FromLong(block_size);
216+}
217+
218+static PyObject *
219+HASH_get_name(HASH *self, void *closure)
220+{
221+ const char *name = NULL;
222+
223+ switch (self->ctx.vtable->desc >> BR_HASHDESC_ID_OFF & BR_HASHDESC_ID_MASK) {
224+ case br_md5_ID: name = "md5"; break;
225+ case br_sha1_ID: name = "sha1"; break;
226+ case br_sha224_ID: name = "sha224"; break;
227+ case br_sha256_ID: name = "sha256"; break;
228+ case br_sha384_ID: name = "sha384"; break;
229+ case br_sha512_ID: name = "sha512"; break;
230+ }
231+
232+ return PyUnicode_FromString(name);
233+}
234+
235+static PyGetSetDef HASH_getset[] = {
236+ {"digest_size", (getter)HASH_get_digest_size, NULL, NULL, NULL},
237+ {"block_size", (getter)HASH_get_block_size, NULL, NULL, NULL},
238+ {"name", (getter)HASH_get_name, NULL, NULL, NULL},
239+ {0}
240+};
241+
242+static PyObject *
243+HASH_repr(HASH *self)
244+{
245+ PyObject *name = HASH_get_name(self, NULL);
246+ if (name == NULL) {
247+ return NULL;
248+ }
249+ PyObject *repr = PyUnicode_FromFormat("<%U %T object @ %p>",
250+ name, self, self);
251+ Py_DECREF(name);
252+ return repr;
253+}
254+
255+static PyTypeObject HASH_Type = {
256+ PyVarObject_HEAD_INIT(NULL, 0)
257+ .tp_name = "_hashlib.HASH",
258+ .tp_dealloc = (destructor)HASH_dealloc,
259+ .tp_repr = (reprfunc)HASH_repr,
260+ .tp_doc = HASH_doc,
261+ .tp_basicsize = sizeof(HASH),
262+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
263+ .tp_methods = HASH_methods,
264+ .tp_getset = HASH_getset,
265+};
266+
267+PyDoc_STRVAR(HASHXOF_doc,
268+"HASHXOF(name, string=b\'\')\n"
269+"--\n"
270+"\n"
271+"A hash is an object used to calculate a checksum of a string of information.\n"
272+"\n"
273+"Methods:\n"
274+"\n"
275+"update() -- updates the current digest with an additional string\n"
276+"digest(length) -- return the current digest value\n"
277+"hexdigest(length) -- return the current digest as a string of hexadecimal digits\n"
278+"copy() -- return a copy of the current hash object\n"
279+"\n"
280+"Attributes:\n"
281+"\n"
282+"name -- the hash algorithm being used by this object\n"
283+"digest_size -- number of bytes in this hashes output");
284+
285+static void
286+HASHXOF_dealloc(HASHXOF *self)
287+{
288+ PyObject_Free(self);
289+}
290+
291+/*[clinic input]
292+_hashlib.HASHXOF.update as HASHXOF_update
293+
294+ data: Py_buffer
295+ /
296+
297+Update this hash object's state with the provided string.
298+[clinic start generated code]*/
299+
300+static PyObject *
301+HASHXOF_update_impl(HASHXOF *self, Py_buffer *data)
302+/*[clinic end generated code: output=284c614570e39dcf input=8f56a60e5e1d5126]*/
303+{
304+ HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(
305+ self, data->len,
306+ br_shake_inject(&self->ctx, data->buf, data->len)
307+ );
308+ Py_RETURN_NONE;
309+}
310+
311+/*[clinic input]
312+_hashlib.HASHXOF.digest as HASHXOF_digest
313+
314+ length: Py_ssize_t(allow_negative=False)
315+
316+Return the digest value as a bytes object.
317+[clinic start generated code]*/
318+
319+static PyObject *
320+HASHXOF_digest_impl(HASHXOF *self, Py_ssize_t length)
321+/*[clinic end generated code: output=4acfde5485438ea1 input=57648448f97345a9]*/
322+{
323+ br_shake_context ctx;
324+ PyObject *bytes;
325+
326+ bytes = PyBytes_FromStringAndSize(NULL, length);
327+ if (!bytes)
328+ return NULL;
329+
330+ HASHLIB_ACQUIRE_LOCK(self);
331+ ctx = self->ctx;
332+ HASHLIB_RELEASE_LOCK(self);
333+ br_shake_flip(&ctx);
334+ br_shake_produce(&ctx, PyBytes_AS_STRING(bytes), length);
335+
336+ return bytes;
337+}
338+
339+/*[clinic input]
340+_hashlib.HASHXOF.hexdigest as HASHXOF_hexdigest
341+
342+ length: Py_ssize_t(allow_negative=False)
343+
344+Return the digest value as a string of hexadecimal digits.
345+[clinic start generated code]*/
346+
347+static PyObject *
348+HASHXOF_hexdigest_impl(HASHXOF *self, Py_ssize_t length)
349+/*[clinic end generated code: output=6ca89e7ccb71e4fc input=c341a60302ee6e78]*/
350+{
351+ br_shake_context ctx;
352+ char *bytes;
353+ PyObject *str;
354+
355+ bytes = PyMem_Malloc(length);
356+ if (!bytes) {
357+ PyErr_NoMemory();
358+ return NULL;
359+ }
360+
361+ HASHLIB_ACQUIRE_LOCK(self);
362+ ctx = self->ctx;
363+ HASHLIB_RELEASE_LOCK(self);
364+ br_shake_flip(&ctx);
365+ br_shake_produce(&ctx, bytes, length);
366+
367+ str = _Py_strhex(bytes, length);
368+ PyMem_Free(bytes);
369+ return str;
370+}
371+
372+/*[clinic input]
373+_hashlib.HASHXOF.copy as HASHXOF_copy
374+
375+Return a copy of the HASHXOF object.
376+[clinic start generated code]*/
377+
378+static PyObject *
379+HASHXOF_copy_impl(HASHXOF *self)
380+/*[clinic end generated code: output=26a3ae2ab4f72986 input=c7ae6f5b7497f72e]*/
381+{
382+ HASHXOF *newobj;
383+
384+ newobj = PyObject_New(HASHXOF, &HASHXOF_Type);
385+ if (!newobj)
386+ return NULL;
387+ HASHLIB_INIT_MUTEX(newobj);
388+ HASHLIB_ACQUIRE_LOCK(self);
389+ newobj->ctx = self->ctx;
390+ newobj->bits = self->bits;
391+ HASHLIB_RELEASE_LOCK(self);
392+
393+ return (PyObject *)newobj;
394+}
395+
396+static PyMethodDef HASHXOF_methods[] = {
397+ HASHXOF_UPDATE_METHODDEF
398+ HASHXOF_DIGEST_METHODDEF
399+ HASHXOF_HEXDIGEST_METHODDEF
400+ HASHXOF_COPY_METHODDEF
401+ {0}
402+};
403+
404+static PyObject *
405+HASHXOF_get_digest_size(HASHXOF *self, void *closure)
406+{
407+ return PyLong_FromLong(0);
408+}
409+
410+static PyObject *
411+HASHXOF_get_block_size(HASHXOF *self, void *closure)
412+{
413+ return PyLong_FromLong(self->ctx.rate);
414+}
415+
416+static PyObject *
417+HASHXOF_get_name(HASHXOF *self, void *closure)
418+{
419+ const char *name = NULL;
420+
421+ switch (self->bits) {
422+ case 128: name = "shake_128"; break;
423+ case 256: name = "shake_256"; break;
424+ }
425+
426+ return PyUnicode_FromString(name);
427+}
428+
429+static PyGetSetDef HASHXOF_getset[] = {
430+ {"digest_size", (getter)HASHXOF_get_digest_size, NULL, NULL, NULL},
431+ {"block_size", (getter)HASHXOF_get_block_size, NULL, NULL, NULL},
432+ {"name", (getter)HASHXOF_get_name, NULL, NULL, NULL},
433+ {0}
434+};
435+
436+static PyObject *
437+HASHXOF_repr(HASHXOF *self)
438+{
439+ PyObject *name = HASHXOF_get_name(self, NULL);
440+ if (name == NULL) {
441+ return NULL;
442+ }
443+ PyObject *repr = PyUnicode_FromFormat("<%U %T object @ %p>",
444+ name, self, self);
445+ Py_DECREF(name);
446+ return repr;
447+}
448+
449+static PyTypeObject HASHXOF_Type = {
450+ PyVarObject_HEAD_INIT(NULL, 0)
451+ .tp_name = "_hashlib.HASHXOF",
452+ .tp_dealloc = (destructor)HASHXOF_dealloc,
453+ .tp_repr = (reprfunc)HASHXOF_repr,
454+ .tp_doc = HASHXOF_doc,
455+ .tp_basicsize = sizeof(HASHXOF),
456+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
457+ .tp_methods = HASHXOF_methods,
458+ .tp_getset = HASHXOF_getset,
459+};
460+
461+PyDoc_STRVAR(HMAC_doc,
462+"The object used to calculate HMAC of a message.\n\
463+\n\
464+Methods:\n\
465+\n\
466+update() -- updates the current digest with an additional string\n\
467+digest() -- return the current digest value\n\
468+hexdigest() -- return the current digest as a string of hexadecimal digits\n\
469+copy() -- return a copy of the current hash object\n\
470+\n\
471+Attributes:\n\
472+\n\
473+name -- the name, including the hash algorithm used by this object\n\
474+digest_size -- number of bytes in digest() output\n");
475+
476+static void
477+HMAC_dealloc(HMAC *self)
478+{
479+ PyObject_Free(self);
480+}
481+
482+/*[clinic input]
483+_hashlib.HMAC.copy as HMAC_copy
484+
485+Return a copy of the HMAC object.
486+[clinic start generated code]*/
487+
488+static PyObject *
489+HMAC_copy_impl(HMAC *self)
490+/*[clinic end generated code: output=6a3595133ec708a5 input=c471e903458ee9a7]*/
491+{
492+ HMAC *newobj;
493+
494+ newobj = PyObject_New(HMAC, &HMAC_Type);
495+ if (!newobj)
496+ return NULL;
497+ HASHLIB_INIT_MUTEX(newobj);
498+ HASHLIB_ACQUIRE_LOCK(self);
499+ newobj->ctx = self->ctx;
500+ newobj->key = self->key;
501+ HASHLIB_RELEASE_LOCK(self);
502+
503+ return (PyObject *)newobj;
504+}
505+
506+/*[clinic input]
507+_hashlib.HMAC.update as HMAC_update
508+
509+ data: Py_buffer
510+ /
511+
512+Update the HMAC object with data.
513+[clinic start generated code]*/
514+
515+static PyObject *
516+HMAC_update_impl(HMAC *self, Py_buffer *data)
517+/*[clinic end generated code: output=d3520ecffa6cc3ab input=827e77e990887267]*/
518+{
519+ HASHLIB_EXTERNAL_INSTRUCTIONS_LOCKED(
520+ self, data->len,
521+ br_hmac_update(&self->ctx, data->buf, data->len);
522+ );
523+ Py_RETURN_NONE;
524+}
525+
526+/*[clinic input]
527+_hashlib.HMAC.digest as HMAC_digest
528+
529+Return the digest of the bytes passed to the update() method so far.
530+[clinic start generated code]*/
531+
532+static PyObject *
533+HMAC_digest_impl(HMAC *self)
534+/*[clinic end generated code: output=9853eeca2bdd96df input=d39cb2285b557318]*/
535+{
536+ char digest[64];
537+ Py_ssize_t digest_size;
538+
539+ HASHLIB_ACQUIRE_LOCK(self);
540+ digest_size = br_hmac_out(&self->ctx, digest);
541+ HASHLIB_RELEASE_LOCK(self);
542+
543+ return PyBytes_FromStringAndSize(digest, digest_size);
544+}
545+
546+/*[clinic input]
547+@permit_long_summary
548+_hashlib.HMAC.hexdigest as HMAC_hexdigest
549+
550+Return hexadecimal digest of the bytes passed to the update() method so far.
551+
552+This may be used to exchange the value safely in email or other non-binary
553+environments.
554+[clinic start generated code]*/
555+
556+static PyObject *
557+HMAC_hexdigest_impl(HMAC *self)
558+/*[clinic end generated code: output=9bb0c7abb6940bab input=60d407178aa9b38b]*/
559+{
560+ char digest[64];
561+ Py_ssize_t digest_size;
562+
563+ HASHLIB_ACQUIRE_LOCK(self);
564+ digest_size = br_hmac_out(&self->ctx, digest);
565+ HASHLIB_RELEASE_LOCK(self);
566+
567+ return _Py_strhex(digest, digest_size);
568+}
569+
570+static PyMethodDef HMAC_methods[] = {
571+ HMAC_UPDATE_METHODDEF
572+ HMAC_DIGEST_METHODDEF
573+ HMAC_HEXDIGEST_METHODDEF
574+ HMAC_COPY_METHODDEF
575+ {0},
576+};
577+
578+static PyObject *
579+HMAC_get_digest_size(HMAC *self, void *closure)
580+{
581+ const br_hash_class *hc;
582+ long digest_size;
583+
584+ hc = br_hmac_get_digest(&self->ctx);
585+ digest_size = hc->desc >> BR_HASHDESC_OUT_OFF & BR_HASHDESC_OUT_MASK;
586+ return PyLong_FromLong(digest_size);
587+}
588+
589+static PyObject *
590+HMAC_get_block_size(HMAC *self, void *closure)
591+{
592+ const br_hash_class *hc;
593+ long block_size;
594+
595+ hc = br_hmac_get_digest(&self->ctx);
596+ block_size = 1 << (hc->desc >> BR_HASHDESC_LBLEN_OFF & BR_HASHDESC_LBLEN_MASK);
597+ return PyLong_FromLong(block_size);
598+}
599+
600+static PyObject *
601+HMAC_get_name(HMAC *self, void *closure)
602+{
603+ const br_hash_class *hc;
604+ const char *name = NULL;
605+
606+ hc = br_hmac_get_digest(&self->ctx);
607+ switch (hc->desc >> BR_HASHDESC_ID_OFF & BR_HASHDESC_ID_MASK) {
608+ case br_md5_ID: name = "hmac-md5"; break;
609+ case br_sha1_ID: name = "hmac-sha1"; break;
610+ case br_sha224_ID: name = "hmac-sha224"; break;
611+ case br_sha256_ID: name = "hmac-sha256"; break;
612+ case br_sha384_ID: name = "hmac-sha384"; break;
613+ case br_sha512_ID: name = "hmac-sha512"; break;
614+ }
615+
616+ return PyUnicode_FromString(name);
617+}
618+
619+static PyGetSetDef HMAC_getset[] = {
620+ {"digest_size", (getter)HMAC_get_digest_size, NULL, NULL, NULL},
621+ {"block_size", (getter)HMAC_get_block_size, NULL, NULL, NULL},
622+ {"name", (getter)HMAC_get_name, NULL, NULL, NULL},
623+ {0}
624+};
625+
626+static PyObject *
627+HMAC_repr(HASH *self)
628+{
629+ PyObject *name = HASH_get_name(self, NULL);
630+ if (name == NULL) {
631+ return NULL;
632+ }
633+ PyObject *repr = PyUnicode_FromFormat("<%U %T object @ %p>",
634+ name, self, self);
635+ Py_DECREF(name);
636+ return repr;
637+}
638+
639+static PyTypeObject HMAC_Type = {
640+ PyVarObject_HEAD_INIT(NULL, 0)
641+ .tp_name = "_hashlib.HMAC",
642+ .tp_dealloc = (destructor)HMAC_dealloc,
643+ .tp_repr = (reprfunc)HMAC_repr,
644+ .tp_doc = HMAC_doc,
645+ .tp_basicsize = sizeof(HMAC),
646+ .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
647+ .tp_methods = HMAC_methods,
648+ .tp_getset = HMAC_getset,
649+};
650+
651+static PyObject *
652+HASH_new_vtable(PyObject *module, const br_hash_class *hc,
653+ Py_buffer *data, int usedforsecurity)
654+{
655+ HASH *self;
656+
657+ self = PyObject_New(HASH, &HASH_Type);
658+ if (!self)
659+ return NULL;
660+ hc->init(&self->ctx.vtable);
661+ HASHLIB_INIT_MUTEX(self);
662+ HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED(
663+ data->len,
664+ hc->update(&self->ctx.vtable, data->buf, data->len);
665+ );
666+
667+ return (PyObject *)self;
668+}
669+
670+static PyObject *
671+HASHXOF_new(PyObject *module, int bits, Py_buffer *data, int usedforsecurity)
672+{
673+ HASHXOF *self;
674+
675+ self = PyObject_New(HASHXOF, &HASHXOF_Type);
676+ if (!self)
677+ return NULL;
678+ self->bits = bits;
679+ br_shake_init(&self->ctx, bits);
680+ HASHLIB_INIT_MUTEX(self);
681+ HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED(
682+ data->len,
683+ br_shake_inject(&self->ctx, data->buf, data->len);
684+ );
685+
686+ return (PyObject *)self;
687+}
688+
689+/*[clinic input]
690+_hashlib.new
691+
692+ name: str
693+ data: Py_buffer = NULL
694+ *
695+ usedforsecurity: bool = True
696+
697+Return a new hash object using the named algorithm.
698+
699+An optional string argument may be provided and will be
700+automatically hashed.
701+
702+The MD5 and SHA1 algorithms are always supported.
703+[clinic start generated code]*/
704+
705+static PyObject *
706+_hashlib_new_impl(PyObject *module, const char *name, Py_buffer *data,
707+ int usedforsecurity)
708+/*[clinic end generated code: output=f3016644a1db3557 input=025e0e84c253e3cc]*/
709+{
710+ const br_hash_class *hc = NULL;
711+ int shake = 0;
712+
713+ hc = py_hash_by_name(name);
714+ if (hc)
715+ ;
716+ else if (strcmp(name, "shake_128") == 0)
717+ shake = 128;
718+ else if (strcmp(name, "shake_256") == 0)
719+ shake = 256;
720+ else {
721+ PyErr_SetString(PyExc_ValueError, "unsupported hash type");
722+ return NULL;
723+ }
724+
725+ if (hc)
726+ return HASH_new_vtable(module, hc, data, usedforsecurity);
727+ if (shake)
728+ return HASHXOF_new(module, shake, data, usedforsecurity);
729+}
730+
731+/*[clinic input]
732+_hashlib.openssl_md5
733+
734+ data: Py_buffer = NULL
735+ *
736+ usedforsecurity: bool = True
737+
738+Returns a md5 hash object; optionally initialized with a string.
739+[clinic start generated code]*/
740+
741+static PyObject *
742+_hashlib_openssl_md5_impl(PyObject *module, Py_buffer *data,
743+ int usedforsecurity)
744+/*[clinic end generated code: output=6d004e8d482f12c6 input=724269f0762d2fd4]*/
745+{
746+ return HASH_new_vtable(module, &br_md5_vtable, data, usedforsecurity);
747+}
748+
749+/*[clinic input]
750+_hashlib.openssl_sha1
751+
752+ data: Py_buffer = NULL
753+ *
754+ usedforsecurity: bool = True
755+
756+Returns a sha1 hash object; optionally initialized with a string.
757+[clinic start generated code]*/
758+
759+static PyObject *
760+_hashlib_openssl_sha1_impl(PyObject *module, Py_buffer *data,
761+ int usedforsecurity)
762+/*[clinic end generated code: output=8e06eb1aeb33b307 input=88c8091b662a132d]*/
763+{
764+ return HASH_new_vtable(module, &br_sha1_vtable, data, usedforsecurity);
765+}
766+
767+/*[clinic input]
768+_hashlib.openssl_sha224
769+
770+ data: Py_buffer = NULL
771+ *
772+ usedforsecurity: bool = True
773+
774+Returns a sha224 hash object; optionally initialized with a string.
775+[clinic start generated code]*/
776+
777+static PyObject *
778+_hashlib_openssl_sha224_impl(PyObject *module, Py_buffer *data,
779+ int usedforsecurity)
780+/*[clinic end generated code: output=a90080c8e4a51352 input=96ec3828e64ac9e6]*/
781+{
782+ return HASH_new_vtable(module, &br_sha224_vtable, data, usedforsecurity);
783+}
784+
785+/*[clinic input]
786+_hashlib.openssl_sha256
787+
788+ data: Py_buffer = NULL
789+ *
790+ usedforsecurity: bool = True
791+
792+Returns a sha256 hash object; optionally initialized with a string.
793+[clinic start generated code]*/
794+
795+static PyObject *
796+_hashlib_openssl_sha256_impl(PyObject *module, Py_buffer *data,
797+ int usedforsecurity)
798+/*[clinic end generated code: output=5c7518b7915f17ef input=e95702979596ee26]*/
799+{
800+ return HASH_new_vtable(module, &br_sha256_vtable, data, usedforsecurity);
801+}
802+
803+/*[clinic input]
804+_hashlib.openssl_sha384
805+
806+ data: Py_buffer = NULL
807+ *
808+ usedforsecurity: bool = True
809+
810+Returns a sha384 hash object; optionally initialized with a string.
811+[clinic start generated code]*/
812+
813+static PyObject *
814+_hashlib_openssl_sha384_impl(PyObject *module, Py_buffer *data,
815+ int usedforsecurity)
816+/*[clinic end generated code: output=4492199f69d4124c input=6e9480f8c631a66a]*/
817+{
818+ return HASH_new_vtable(module, &br_sha384_vtable, data, usedforsecurity);
819+}
820+
821+/*[clinic input]
822+_hashlib.openssl_sha512
823+
824+ data: Py_buffer = NULL
825+ *
826+ usedforsecurity: bool = True
827+
828+Returns a sha512 hash object; optionally initialized with a string.
829+[clinic start generated code]*/
830+
831+static PyObject *
832+_hashlib_openssl_sha512_impl(PyObject *module, Py_buffer *data,
833+ int usedforsecurity)
834+/*[clinic end generated code: output=e6cdcd262498123f input=696e89c85ed8fb93]*/
835+{
836+ return HASH_new_vtable(module, &br_sha512_vtable, data, usedforsecurity);
837+}
838+
839+/*[clinic input]
840+@permit_long_summary
841+_hashlib.openssl_shake_128
842+
843+ data: Py_buffer = NULL
844+ *
845+ usedforsecurity: bool = True
846+
847+Returns a shake-128 variable hash object; optionally initialized with a string.
848+[clinic start generated code]*/
849+
850+static PyObject *
851+_hashlib_openssl_shake_128_impl(PyObject *module, Py_buffer *data,
852+ int usedforsecurity)
853+/*[clinic end generated code: output=4659f2817328725e input=348d765e70cfeb51]*/
854+{
855+ return HASHXOF_new(module, 128, data, usedforsecurity);
856+}
857+
858+/*[clinic input]
859+@permit_long_summary
860+_hashlib.openssl_shake_256
861+
862+ data: Py_buffer = NULL
863+ *
864+ usedforsecurity: bool = True
865+
866+Returns a shake-256 variable hash object; optionally initialized with a string.
867+[clinic start generated code]*/
868+
869+static PyObject *
870+_hashlib_openssl_shake_256_impl(PyObject *module, Py_buffer *data,
871+ int usedforsecurity)
872+/*[clinic end generated code: output=7b9e5bfe8c1d042e input=45671e5e1ef81b49]*/
873+{
874+ return HASHXOF_new(module, 256, data, usedforsecurity);
875+}
876+
877+static int
878+_tscmp(const unsigned char *a, const unsigned char *b,
879+ Py_ssize_t len_a, Py_ssize_t len_b)
880+{
881+ /* loop count depends on length of b. Might leak very little timing
882+ * information if sizes are different.
883+ */
884+ Py_ssize_t length = len_b;
885+ unsigned result = 0;
886+ size_t i;
887+
888+ if (len_a != length) {
889+ a = b;
890+ result = 1;
891+ }
892+
893+ for (i = 0; i < length; i++)
894+ result |= (unsigned)a[i] ^ (unsigned)b[i];
895+
896+ return ~(result | -result) >> CHAR_BIT * sizeof(result) - 1;
897+}
898+
899+/*[clinic input]
900+_hashlib.compare_digest
901+
902+ a: object
903+ b: object
904+ /
905+
906+Return 'a == b'.
907+
908+This function uses an approach designed to prevent
909+timing analysis, making it appropriate for cryptography.
910+
911+a and b must both be of the same type: either str (ASCII only),
912+or any bytes-like object.
913+
914+Note: If a and b are of different lengths, or if an error occurs,
915+a timing attack could theoretically reveal information about the
916+types and lengths of a and b--but not their values.
917+[clinic start generated code]*/
918+
919+static PyObject *
920+_hashlib_compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
921+/*[clinic end generated code: output=6f1c13927480aed9 input=9c40c6e566ca12f5]*/
922+{
923+ int rc;
924+
925+ /* ASCII unicode string */
926+ if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
927+ if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
928+ return NULL;
929+ }
930+ if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
931+ PyErr_SetString(PyExc_TypeError,
932+ "comparing strings with non-ASCII characters is "
933+ "not supported");
934+ return NULL;
935+ }
936+
937+ rc = _tscmp(PyUnicode_DATA(a),
938+ PyUnicode_DATA(b),
939+ PyUnicode_GET_LENGTH(a),
940+ PyUnicode_GET_LENGTH(b));
941+ }
942+ /* fallback to buffer interface for bytes, bytesarray and other */
943+ else {
944+ Py_buffer view_a;
945+ Py_buffer view_b;
946+
947+ if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
948+ PyErr_Format(PyExc_TypeError,
949+ "unsupported operand types(s) or combination of types: "
950+ "'%.100s' and '%.100s'",
951+ Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
952+ return NULL;
953+ }
954+
955+ if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
956+ return NULL;
957+ }
958+ if (view_a.ndim > 1) {
959+ PyErr_SetString(PyExc_BufferError,
960+ "Buffer must be single dimension");
961+ PyBuffer_Release(&view_a);
962+ return NULL;
963+ }
964+
965+ if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
966+ PyBuffer_Release(&view_a);
967+ return NULL;
968+ }
969+ if (view_b.ndim > 1) {
970+ PyErr_SetString(PyExc_BufferError,
971+ "Buffer must be single dimension");
972+ PyBuffer_Release(&view_a);
973+ PyBuffer_Release(&view_b);
974+ return NULL;
975+ }
976+
977+ rc = _tscmp((const unsigned char*)view_a.buf,
978+ (const unsigned char*)view_b.buf,
979+ view_a.len,
980+ view_b.len);
981+
982+ PyBuffer_Release(&view_a);
983+ PyBuffer_Release(&view_b);
984+ }
985+
986+ return PyBool_FromLong(rc);
987+}
988+
989+static struct PyMethodDef hashlib_methods[] = {
990+ _HASHLIB_NEW_METHODDEF
991+ _HASHLIB_HMAC_SINGLESHOT_METHODDEF
992+ _HASHLIB_HMAC_NEW_METHODDEF
993+ _HASHLIB_OPENSSL_MD5_METHODDEF
994+ _HASHLIB_OPENSSL_SHA1_METHODDEF
995+ _HASHLIB_OPENSSL_SHA224_METHODDEF
996+ _HASHLIB_OPENSSL_SHA256_METHODDEF
997+ _HASHLIB_OPENSSL_SHA384_METHODDEF
998+ _HASHLIB_OPENSSL_SHA512_METHODDEF
999+ _HASHLIB_OPENSSL_SHAKE_128_METHODDEF
1000+ _HASHLIB_OPENSSL_SHAKE_256_METHODDEF
1001+ _HASHLIB_COMPARE_DIGEST_METHODDEF
1002+ {0}
1003+};
1004+
1005+// --- One-shot HMAC interface ------------------------------------------------
1006+
1007+/*[clinic input]
1008+_hashlib.hmac_digest as _hashlib_hmac_singleshot
1009+
1010+ key: Py_buffer
1011+ msg: Py_buffer
1012+ digest: str
1013+
1014+Single-shot HMAC.
1015+[clinic start generated code]*/
1016+
1017+static PyObject *
1018+_hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
1019+ Py_buffer *msg, const char *digest)
1020+/*[clinic end generated code: output=15658ede5ab98185 input=019dffc571909a46]*/
1021+{
1022+ char buf[64];
1023+ Py_ssize_t buf_len;
1024+ br_hmac_context ctx;
1025+ br_hmac_key_context keyctx;
1026+ const br_hash_class *hc;
1027+
1028+ hc = py_hash_by_name(digest);
1029+ if (!hc) {
1030+ PyErr_SetString(PyExc_ValueError, "unsupported hash type");
1031+ return NULL;
1032+ }
1033+ br_hmac_key_init(&keyctx, hc, key->buf, key->len);
1034+ br_hmac_init(&ctx, &keyctx, 0);
1035+ HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED(
1036+ msg->len,
1037+ br_hmac_update(&ctx, msg->buf, msg->len);
1038+ );
1039+ br_hmac_out(&ctx, buf);
1040+ buf_len = br_hmac_size(&ctx);
1041+
1042+ return PyBytes_FromStringAndSize(buf, buf_len);
1043+}
1044+
1045+/*[clinic input]
1046+_hashlib.hmac_new
1047+
1048+ key: Py_buffer
1049+ msg: Py_buffer = NULL
1050+ digestmod: str(c_default="NULL") = None
1051+
1052+Return a new hmac object.
1053+[clinic start generated code]*/
1054+
1055+static PyObject *
1056+_hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, Py_buffer *msg,
1057+ const char *digestmod)
1058+/*[clinic end generated code: output=5733a8600554bba2 input=76547bd3e681ea5e]*/
1059+{
1060+ HMAC *self;
1061+ const br_hash_class *hc;
1062+
1063+ if (!digestmod || !digestmod[0]) {
1064+ PyErr_SetString(
1065+ PyExc_TypeError, "missing required parameter 'digestmod'");
1066+ return NULL;
1067+ }
1068+ hc = py_hash_by_name(digestmod);
1069+ if (!hc) {
1070+ PyErr_SetString(PyExc_ValueError, "unknown hash function");
1071+ return NULL;
1072+ }
1073+ self = PyObject_New(HMAC, &HMAC_Type);
1074+ if (!self)
1075+ return NULL;
1076+ br_hmac_key_init(&self->key, hc, key->buf, key->len);
1077+ br_hmac_init(&self->ctx, &self->key, 0);
1078+ HASHLIB_INIT_MUTEX(self);
1079+ HASHLIB_EXTERNAL_INSTRUCTIONS_UNLOCKED(
1080+ msg->len,
1081+ br_hmac_update(&self->ctx, msg->buf, msg->len);
1082+ );
1083+
1084+ return (PyObject *)self;
1085+}
1086+
1087+static struct PyModuleDef _hashlibmodule = {
1088+ PyModuleDef_HEAD_INIT,
1089+ .m_name = "_hashlib",
1090+ .m_doc = "BearSSL interface for hashlib module",
1091+ .m_methods = hashlib_methods,
1092+};
1093+
1094+PyMODINIT_FUNC
1095+PyInit__hashlib(void)
1096+{
1097+ PyObject *m = NULL, *set = NULL, *name;
1098+ static const char *const names[] = {
1099+ "md5",
1100+ "sha1",
1101+ "sha224",
1102+ "sha256",
1103+ "sha384",
1104+ "sha512",
1105+ "shake_128",
1106+ "shake_256",
1107+ };
1108+ int r;
1109+
1110+ m = PyState_FindModule(&_hashlibmodule);
1111+ if (m) {
1112+ Py_INCREF(m);
1113+ return m;
1114+ }
1115+ m = PyModule_Create(&_hashlibmodule);
1116+ if (!m)
1117+ goto err;
1118+ if (PyModule_AddType(m, &HASH_Type) < 0)
1119+ goto err;
1120+ if (PyModule_AddType(m, &HASHXOF_Type) < 0)
1121+ goto err;
1122+ if (PyModule_AddType(m, &HMAC_Type) < 0)
1123+ goto err;
1124+ set = PyFrozenSet_New(NULL);
1125+ if (!set)
1126+ goto err;
1127+ for (size_t i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
1128+ name = PyUnicode_FromString(names[i]);
1129+ if (!name)
1130+ goto err;
1131+ r = PySet_Add(set, name);
1132+ Py_DECREF(name);
1133+ if (r != 0)
1134+ goto err;
1135+ }
1136+ if (PyModule_AddObject(m, "openssl_md_meth_names", set) < 0)
1137+ goto err;
1138+
1139+ return m;
1140+
1141+ err:
1142+ if (set)
1143+ Py_DECREF(set);
1144+ if (m)
1145+ Py_DECREF(m);
1146+ return NULL;
1147+}
1148diff --git a/Modules/clinic/_hashbearssl.c.h b/Modules/clinic/_hashbearssl.c.h
1149new file mode 100644
1150index 00000000000..5fa0c88da6b
1151--- /dev/null
1152+++ b/Modules/clinic/_hashbearssl.c.h
1153@@ -0,0 +1,1401 @@
1154+/*[clinic input]
1155+preserve
1156+[clinic start generated code]*/
1157+
1158+#if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1159+# include "pycore_gc.h" // PyGC_Head
1160+# include "pycore_runtime.h" // _Py_ID()
1161+#endif
1162+#include "pycore_abstract.h" // _PyNumber_Index()
1163+#include "pycore_modsupport.h" // _PyArg_BadArgument()
1164+
1165+PyDoc_STRVAR(HASH_update__doc__,
1166+"update($self, data, /)\n"
1167+"--\n"
1168+"\n"
1169+"Update this hash object\'s state with the provided string.");
1170+
1171+#define HASH_UPDATE_METHODDEF \
1172+ {"update", (PyCFunction)HASH_update, METH_O, HASH_update__doc__},
1173+
1174+static PyObject *
1175+HASH_update_impl(HASH *self, Py_buffer *data);
1176+
1177+static PyObject *
1178+HASH_update(PyObject *self, PyObject *arg)
1179+{
1180+ PyObject *return_value = NULL;
1181+ Py_buffer data = {NULL, NULL};
1182+
1183+ if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
1184+ goto exit;
1185+ }
1186+ return_value = HASH_update_impl((HASH *)self, &data);
1187+
1188+exit:
1189+ /* Cleanup for data */
1190+ if (data.obj) {
1191+ PyBuffer_Release(&data);
1192+ }
1193+
1194+ return return_value;
1195+}
1196+
1197+PyDoc_STRVAR(HASH_digest__doc__,
1198+"digest($self, /)\n"
1199+"--\n"
1200+"\n"
1201+"Return the digest value as a bytes object.");
1202+
1203+#define HASH_DIGEST_METHODDEF \
1204+ {"digest", (PyCFunction)HASH_digest, METH_NOARGS, HASH_digest__doc__},
1205+
1206+static PyObject *
1207+HASH_digest_impl(HASH *self);
1208+
1209+static PyObject *
1210+HASH_digest(PyObject *self, PyObject *Py_UNUSED(ignored))
1211+{
1212+ return HASH_digest_impl((HASH *)self);
1213+}
1214+
1215+PyDoc_STRVAR(HASH_hexdigest__doc__,
1216+"hexdigest($self, /)\n"
1217+"--\n"
1218+"\n"
1219+"Return the digest value as a string of hexadecimal digits.");
1220+
1221+#define HASH_HEXDIGEST_METHODDEF \
1222+ {"hexdigest", (PyCFunction)HASH_hexdigest, METH_NOARGS, HASH_hexdigest__doc__},
1223+
1224+static PyObject *
1225+HASH_hexdigest_impl(HASH *self);
1226+
1227+static PyObject *
1228+HASH_hexdigest(PyObject *self, PyObject *Py_UNUSED(ignored))
1229+{
1230+ return HASH_hexdigest_impl((HASH *)self);
1231+}
1232+
1233+PyDoc_STRVAR(HASH_copy__doc__,
1234+"copy($self, /)\n"
1235+"--\n"
1236+"\n"
1237+"Return a copy of the hash object.");
1238+
1239+#define HASH_COPY_METHODDEF \
1240+ {"copy", (PyCFunction)HASH_copy, METH_NOARGS, HASH_copy__doc__},
1241+
1242+static PyObject *
1243+HASH_copy_impl(HASH *self);
1244+
1245+static PyObject *
1246+HASH_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
1247+{
1248+ return HASH_copy_impl((HASH *)self);
1249+}
1250+
1251+PyDoc_STRVAR(HASHXOF_update__doc__,
1252+"update($self, data, /)\n"
1253+"--\n"
1254+"\n"
1255+"Update this hash object\'s state with the provided string.");
1256+
1257+#define HASHXOF_UPDATE_METHODDEF \
1258+ {"update", (PyCFunction)HASHXOF_update, METH_O, HASHXOF_update__doc__},
1259+
1260+static PyObject *
1261+HASHXOF_update_impl(HASHXOF *self, Py_buffer *data);
1262+
1263+static PyObject *
1264+HASHXOF_update(PyObject *self, PyObject *arg)
1265+{
1266+ PyObject *return_value = NULL;
1267+ Py_buffer data = {NULL, NULL};
1268+
1269+ if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
1270+ goto exit;
1271+ }
1272+ return_value = HASHXOF_update_impl((HASHXOF *)self, &data);
1273+
1274+exit:
1275+ /* Cleanup for data */
1276+ if (data.obj) {
1277+ PyBuffer_Release(&data);
1278+ }
1279+
1280+ return return_value;
1281+}
1282+
1283+PyDoc_STRVAR(HASHXOF_digest__doc__,
1284+"digest($self, /, length)\n"
1285+"--\n"
1286+"\n"
1287+"Return the digest value as a bytes object.");
1288+
1289+#define HASHXOF_DIGEST_METHODDEF \
1290+ {"digest", _PyCFunction_CAST(HASHXOF_digest), METH_FASTCALL|METH_KEYWORDS, HASHXOF_digest__doc__},
1291+
1292+static PyObject *
1293+HASHXOF_digest_impl(HASHXOF *self, Py_ssize_t length);
1294+
1295+static PyObject *
1296+HASHXOF_digest(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1297+{
1298+ PyObject *return_value = NULL;
1299+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1300+
1301+ #define NUM_KEYWORDS 1
1302+ static struct {
1303+ PyGC_Head _this_is_not_used;
1304+ PyObject_VAR_HEAD
1305+ Py_hash_t ob_hash;
1306+ PyObject *ob_item[NUM_KEYWORDS];
1307+ } _kwtuple = {
1308+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1309+ .ob_hash = -1,
1310+ .ob_item = { &_Py_ID(length), },
1311+ };
1312+ #undef NUM_KEYWORDS
1313+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1314+
1315+ #else // !Py_BUILD_CORE
1316+ # define KWTUPLE NULL
1317+ #endif // !Py_BUILD_CORE
1318+
1319+ static const char * const _keywords[] = {"length", NULL};
1320+ static _PyArg_Parser _parser = {
1321+ .keywords = _keywords,
1322+ .fname = "digest",
1323+ .kwtuple = KWTUPLE,
1324+ };
1325+ #undef KWTUPLE
1326+ PyObject *argsbuf[1];
1327+ Py_ssize_t length;
1328+
1329+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1330+ /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1331+ if (!args) {
1332+ goto exit;
1333+ }
1334+ {
1335+ Py_ssize_t ival = -1;
1336+ PyObject *iobj = _PyNumber_Index(args[0]);
1337+ if (iobj != NULL) {
1338+ ival = PyLong_AsSsize_t(iobj);
1339+ Py_DECREF(iobj);
1340+ }
1341+ if (ival == -1 && PyErr_Occurred()) {
1342+ goto exit;
1343+ }
1344+ length = ival;
1345+ if (length < 0) {
1346+ PyErr_SetString(PyExc_ValueError,
1347+ "length cannot be negative");
1348+ goto exit;
1349+ }
1350+ }
1351+ return_value = HASHXOF_digest_impl((HASHXOF *)self, length);
1352+
1353+exit:
1354+ return return_value;
1355+}
1356+
1357+PyDoc_STRVAR(HASHXOF_hexdigest__doc__,
1358+"hexdigest($self, /, length)\n"
1359+"--\n"
1360+"\n"
1361+"Return the digest value as a string of hexadecimal digits.");
1362+
1363+#define HASHXOF_HEXDIGEST_METHODDEF \
1364+ {"hexdigest", _PyCFunction_CAST(HASHXOF_hexdigest), METH_FASTCALL|METH_KEYWORDS, HASHXOF_hexdigest__doc__},
1365+
1366+static PyObject *
1367+HASHXOF_hexdigest_impl(HASHXOF *self, Py_ssize_t length);
1368+
1369+static PyObject *
1370+HASHXOF_hexdigest(PyObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1371+{
1372+ PyObject *return_value = NULL;
1373+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1374+
1375+ #define NUM_KEYWORDS 1
1376+ static struct {
1377+ PyGC_Head _this_is_not_used;
1378+ PyObject_VAR_HEAD
1379+ Py_hash_t ob_hash;
1380+ PyObject *ob_item[NUM_KEYWORDS];
1381+ } _kwtuple = {
1382+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1383+ .ob_hash = -1,
1384+ .ob_item = { &_Py_ID(length), },
1385+ };
1386+ #undef NUM_KEYWORDS
1387+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1388+
1389+ #else // !Py_BUILD_CORE
1390+ # define KWTUPLE NULL
1391+ #endif // !Py_BUILD_CORE
1392+
1393+ static const char * const _keywords[] = {"length", NULL};
1394+ static _PyArg_Parser _parser = {
1395+ .keywords = _keywords,
1396+ .fname = "hexdigest",
1397+ .kwtuple = KWTUPLE,
1398+ };
1399+ #undef KWTUPLE
1400+ PyObject *argsbuf[1];
1401+ Py_ssize_t length;
1402+
1403+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1404+ /*minpos*/ 1, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1405+ if (!args) {
1406+ goto exit;
1407+ }
1408+ {
1409+ Py_ssize_t ival = -1;
1410+ PyObject *iobj = _PyNumber_Index(args[0]);
1411+ if (iobj != NULL) {
1412+ ival = PyLong_AsSsize_t(iobj);
1413+ Py_DECREF(iobj);
1414+ }
1415+ if (ival == -1 && PyErr_Occurred()) {
1416+ goto exit;
1417+ }
1418+ length = ival;
1419+ if (length < 0) {
1420+ PyErr_SetString(PyExc_ValueError,
1421+ "length cannot be negative");
1422+ goto exit;
1423+ }
1424+ }
1425+ return_value = HASHXOF_hexdigest_impl((HASHXOF *)self, length);
1426+
1427+exit:
1428+ return return_value;
1429+}
1430+
1431+PyDoc_STRVAR(HASHXOF_copy__doc__,
1432+"copy($self, /)\n"
1433+"--\n"
1434+"\n"
1435+"Return a copy of the HASHXOF object.");
1436+
1437+#define HASHXOF_COPY_METHODDEF \
1438+ {"copy", (PyCFunction)HASHXOF_copy, METH_NOARGS, HASHXOF_copy__doc__},
1439+
1440+static PyObject *
1441+HASHXOF_copy_impl(HASHXOF *self);
1442+
1443+static PyObject *
1444+HASHXOF_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
1445+{
1446+ return HASHXOF_copy_impl((HASHXOF *)self);
1447+}
1448+
1449+PyDoc_STRVAR(HMAC_copy__doc__,
1450+"copy($self, /)\n"
1451+"--\n"
1452+"\n"
1453+"Return a copy of the HMAC object.");
1454+
1455+#define HMAC_COPY_METHODDEF \
1456+ {"copy", (PyCFunction)HMAC_copy, METH_NOARGS, HMAC_copy__doc__},
1457+
1458+static PyObject *
1459+HMAC_copy_impl(HMAC *self);
1460+
1461+static PyObject *
1462+HMAC_copy(PyObject *self, PyObject *Py_UNUSED(ignored))
1463+{
1464+ return HMAC_copy_impl((HMAC *)self);
1465+}
1466+
1467+PyDoc_STRVAR(HMAC_update__doc__,
1468+"update($self, data, /)\n"
1469+"--\n"
1470+"\n"
1471+"Update the HMAC object with data.");
1472+
1473+#define HMAC_UPDATE_METHODDEF \
1474+ {"update", (PyCFunction)HMAC_update, METH_O, HMAC_update__doc__},
1475+
1476+static PyObject *
1477+HMAC_update_impl(HMAC *self, Py_buffer *data);
1478+
1479+static PyObject *
1480+HMAC_update(PyObject *self, PyObject *arg)
1481+{
1482+ PyObject *return_value = NULL;
1483+ Py_buffer data = {NULL, NULL};
1484+
1485+ if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
1486+ goto exit;
1487+ }
1488+ return_value = HMAC_update_impl((HMAC *)self, &data);
1489+
1490+exit:
1491+ /* Cleanup for data */
1492+ if (data.obj) {
1493+ PyBuffer_Release(&data);
1494+ }
1495+
1496+ return return_value;
1497+}
1498+
1499+PyDoc_STRVAR(HMAC_digest__doc__,
1500+"digest($self, /)\n"
1501+"--\n"
1502+"\n"
1503+"Return the digest of the bytes passed to the update() method so far.");
1504+
1505+#define HMAC_DIGEST_METHODDEF \
1506+ {"digest", (PyCFunction)HMAC_digest, METH_NOARGS, HMAC_digest__doc__},
1507+
1508+static PyObject *
1509+HMAC_digest_impl(HMAC *self);
1510+
1511+static PyObject *
1512+HMAC_digest(PyObject *self, PyObject *Py_UNUSED(ignored))
1513+{
1514+ return HMAC_digest_impl((HMAC *)self);
1515+}
1516+
1517+PyDoc_STRVAR(HMAC_hexdigest__doc__,
1518+"hexdigest($self, /)\n"
1519+"--\n"
1520+"\n"
1521+"Return hexadecimal digest of the bytes passed to the update() method so far.\n"
1522+"\n"
1523+"This may be used to exchange the value safely in email or other non-binary\n"
1524+"environments.");
1525+
1526+#define HMAC_HEXDIGEST_METHODDEF \
1527+ {"hexdigest", (PyCFunction)HMAC_hexdigest, METH_NOARGS, HMAC_hexdigest__doc__},
1528+
1529+static PyObject *
1530+HMAC_hexdigest_impl(HMAC *self);
1531+
1532+static PyObject *
1533+HMAC_hexdigest(PyObject *self, PyObject *Py_UNUSED(ignored))
1534+{
1535+ return HMAC_hexdigest_impl((HMAC *)self);
1536+}
1537+
1538+PyDoc_STRVAR(_hashlib_new__doc__,
1539+"new($module, /, name, data=<unrepresentable>, *, usedforsecurity=True)\n"
1540+"--\n"
1541+"\n"
1542+"Return a new hash object using the named algorithm.\n"
1543+"\n"
1544+"An optional string argument may be provided and will be\n"
1545+"automatically hashed.\n"
1546+"\n"
1547+"The MD5 and SHA1 algorithms are always supported.");
1548+
1549+#define _HASHLIB_NEW_METHODDEF \
1550+ {"new", _PyCFunction_CAST(_hashlib_new), METH_FASTCALL|METH_KEYWORDS, _hashlib_new__doc__},
1551+
1552+static PyObject *
1553+_hashlib_new_impl(PyObject *module, const char *name, Py_buffer *data,
1554+ int usedforsecurity);
1555+
1556+static PyObject *
1557+_hashlib_new(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1558+{
1559+ PyObject *return_value = NULL;
1560+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1561+
1562+ #define NUM_KEYWORDS 3
1563+ static struct {
1564+ PyGC_Head _this_is_not_used;
1565+ PyObject_VAR_HEAD
1566+ Py_hash_t ob_hash;
1567+ PyObject *ob_item[NUM_KEYWORDS];
1568+ } _kwtuple = {
1569+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1570+ .ob_hash = -1,
1571+ .ob_item = { &_Py_ID(name), &_Py_ID(data), &_Py_ID(usedforsecurity), },
1572+ };
1573+ #undef NUM_KEYWORDS
1574+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1575+
1576+ #else // !Py_BUILD_CORE
1577+ # define KWTUPLE NULL
1578+ #endif // !Py_BUILD_CORE
1579+
1580+ static const char * const _keywords[] = {"name", "data", "usedforsecurity", NULL};
1581+ static _PyArg_Parser _parser = {
1582+ .keywords = _keywords,
1583+ .fname = "new",
1584+ .kwtuple = KWTUPLE,
1585+ };
1586+ #undef KWTUPLE
1587+ PyObject *argsbuf[3];
1588+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1589+ const char *name;
1590+ Py_buffer data = {NULL, NULL};
1591+ int usedforsecurity = 1;
1592+
1593+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1594+ /*minpos*/ 1, /*maxpos*/ 2, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1595+ if (!args) {
1596+ goto exit;
1597+ }
1598+ if (!PyUnicode_Check(args[0])) {
1599+ _PyArg_BadArgument("new", "argument 'name'", "str", args[0]);
1600+ goto exit;
1601+ }
1602+ Py_ssize_t name_length;
1603+ name = PyUnicode_AsUTF8AndSize(args[0], &name_length);
1604+ if (name == NULL) {
1605+ goto exit;
1606+ }
1607+ if (strlen(name) != (size_t)name_length) {
1608+ PyErr_SetString(PyExc_ValueError, "embedded null character");
1609+ goto exit;
1610+ }
1611+ if (!noptargs) {
1612+ goto skip_optional_pos;
1613+ }
1614+ if (args[1]) {
1615+ if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) {
1616+ goto exit;
1617+ }
1618+ if (!--noptargs) {
1619+ goto skip_optional_pos;
1620+ }
1621+ }
1622+skip_optional_pos:
1623+ if (!noptargs) {
1624+ goto skip_optional_kwonly;
1625+ }
1626+ usedforsecurity = PyObject_IsTrue(args[2]);
1627+ if (usedforsecurity < 0) {
1628+ goto exit;
1629+ }
1630+skip_optional_kwonly:
1631+ return_value = _hashlib_new_impl(module, name, &data, usedforsecurity);
1632+
1633+exit:
1634+ /* Cleanup for data */
1635+ if (data.obj) {
1636+ PyBuffer_Release(&data);
1637+ }
1638+
1639+ return return_value;
1640+}
1641+
1642+PyDoc_STRVAR(_hashlib_openssl_md5__doc__,
1643+"openssl_md5($module, /, data=<unrepresentable>, *, usedforsecurity=True)\n"
1644+"--\n"
1645+"\n"
1646+"Returns a md5 hash object; optionally initialized with a string.");
1647+
1648+#define _HASHLIB_OPENSSL_MD5_METHODDEF \
1649+ {"openssl_md5", _PyCFunction_CAST(_hashlib_openssl_md5), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_md5__doc__},
1650+
1651+static PyObject *
1652+_hashlib_openssl_md5_impl(PyObject *module, Py_buffer *data,
1653+ int usedforsecurity);
1654+
1655+static PyObject *
1656+_hashlib_openssl_md5(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1657+{
1658+ PyObject *return_value = NULL;
1659+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1660+
1661+ #define NUM_KEYWORDS 2
1662+ static struct {
1663+ PyGC_Head _this_is_not_used;
1664+ PyObject_VAR_HEAD
1665+ Py_hash_t ob_hash;
1666+ PyObject *ob_item[NUM_KEYWORDS];
1667+ } _kwtuple = {
1668+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1669+ .ob_hash = -1,
1670+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
1671+ };
1672+ #undef NUM_KEYWORDS
1673+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1674+
1675+ #else // !Py_BUILD_CORE
1676+ # define KWTUPLE NULL
1677+ #endif // !Py_BUILD_CORE
1678+
1679+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
1680+ static _PyArg_Parser _parser = {
1681+ .keywords = _keywords,
1682+ .fname = "openssl_md5",
1683+ .kwtuple = KWTUPLE,
1684+ };
1685+ #undef KWTUPLE
1686+ PyObject *argsbuf[2];
1687+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1688+ Py_buffer data = {NULL, NULL};
1689+ int usedforsecurity = 1;
1690+
1691+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1692+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1693+ if (!args) {
1694+ goto exit;
1695+ }
1696+ if (!noptargs) {
1697+ goto skip_optional_pos;
1698+ }
1699+ if (args[0]) {
1700+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1701+ goto exit;
1702+ }
1703+ if (!--noptargs) {
1704+ goto skip_optional_pos;
1705+ }
1706+ }
1707+skip_optional_pos:
1708+ if (!noptargs) {
1709+ goto skip_optional_kwonly;
1710+ }
1711+ usedforsecurity = PyObject_IsTrue(args[1]);
1712+ if (usedforsecurity < 0) {
1713+ goto exit;
1714+ }
1715+skip_optional_kwonly:
1716+ return_value = _hashlib_openssl_md5_impl(module, &data, usedforsecurity);
1717+
1718+exit:
1719+ /* Cleanup for data */
1720+ if (data.obj) {
1721+ PyBuffer_Release(&data);
1722+ }
1723+
1724+ return return_value;
1725+}
1726+
1727+PyDoc_STRVAR(_hashlib_openssl_sha1__doc__,
1728+"openssl_sha1($module, /, data=<unrepresentable>, *,\n"
1729+" usedforsecurity=True)\n"
1730+"--\n"
1731+"\n"
1732+"Returns a sha1 hash object; optionally initialized with a string.");
1733+
1734+#define _HASHLIB_OPENSSL_SHA1_METHODDEF \
1735+ {"openssl_sha1", _PyCFunction_CAST(_hashlib_openssl_sha1), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha1__doc__},
1736+
1737+static PyObject *
1738+_hashlib_openssl_sha1_impl(PyObject *module, Py_buffer *data,
1739+ int usedforsecurity);
1740+
1741+static PyObject *
1742+_hashlib_openssl_sha1(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1743+{
1744+ PyObject *return_value = NULL;
1745+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1746+
1747+ #define NUM_KEYWORDS 2
1748+ static struct {
1749+ PyGC_Head _this_is_not_used;
1750+ PyObject_VAR_HEAD
1751+ Py_hash_t ob_hash;
1752+ PyObject *ob_item[NUM_KEYWORDS];
1753+ } _kwtuple = {
1754+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1755+ .ob_hash = -1,
1756+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
1757+ };
1758+ #undef NUM_KEYWORDS
1759+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1760+
1761+ #else // !Py_BUILD_CORE
1762+ # define KWTUPLE NULL
1763+ #endif // !Py_BUILD_CORE
1764+
1765+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
1766+ static _PyArg_Parser _parser = {
1767+ .keywords = _keywords,
1768+ .fname = "openssl_sha1",
1769+ .kwtuple = KWTUPLE,
1770+ };
1771+ #undef KWTUPLE
1772+ PyObject *argsbuf[2];
1773+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1774+ Py_buffer data = {NULL, NULL};
1775+ int usedforsecurity = 1;
1776+
1777+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1778+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1779+ if (!args) {
1780+ goto exit;
1781+ }
1782+ if (!noptargs) {
1783+ goto skip_optional_pos;
1784+ }
1785+ if (args[0]) {
1786+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1787+ goto exit;
1788+ }
1789+ if (!--noptargs) {
1790+ goto skip_optional_pos;
1791+ }
1792+ }
1793+skip_optional_pos:
1794+ if (!noptargs) {
1795+ goto skip_optional_kwonly;
1796+ }
1797+ usedforsecurity = PyObject_IsTrue(args[1]);
1798+ if (usedforsecurity < 0) {
1799+ goto exit;
1800+ }
1801+skip_optional_kwonly:
1802+ return_value = _hashlib_openssl_sha1_impl(module, &data, usedforsecurity);
1803+
1804+exit:
1805+ /* Cleanup for data */
1806+ if (data.obj) {
1807+ PyBuffer_Release(&data);
1808+ }
1809+
1810+ return return_value;
1811+}
1812+
1813+PyDoc_STRVAR(_hashlib_openssl_sha224__doc__,
1814+"openssl_sha224($module, /, data=<unrepresentable>, *,\n"
1815+" usedforsecurity=True)\n"
1816+"--\n"
1817+"\n"
1818+"Returns a sha224 hash object; optionally initialized with a string.");
1819+
1820+#define _HASHLIB_OPENSSL_SHA224_METHODDEF \
1821+ {"openssl_sha224", _PyCFunction_CAST(_hashlib_openssl_sha224), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha224__doc__},
1822+
1823+static PyObject *
1824+_hashlib_openssl_sha224_impl(PyObject *module, Py_buffer *data,
1825+ int usedforsecurity);
1826+
1827+static PyObject *
1828+_hashlib_openssl_sha224(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1829+{
1830+ PyObject *return_value = NULL;
1831+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1832+
1833+ #define NUM_KEYWORDS 2
1834+ static struct {
1835+ PyGC_Head _this_is_not_used;
1836+ PyObject_VAR_HEAD
1837+ Py_hash_t ob_hash;
1838+ PyObject *ob_item[NUM_KEYWORDS];
1839+ } _kwtuple = {
1840+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1841+ .ob_hash = -1,
1842+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
1843+ };
1844+ #undef NUM_KEYWORDS
1845+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1846+
1847+ #else // !Py_BUILD_CORE
1848+ # define KWTUPLE NULL
1849+ #endif // !Py_BUILD_CORE
1850+
1851+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
1852+ static _PyArg_Parser _parser = {
1853+ .keywords = _keywords,
1854+ .fname = "openssl_sha224",
1855+ .kwtuple = KWTUPLE,
1856+ };
1857+ #undef KWTUPLE
1858+ PyObject *argsbuf[2];
1859+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1860+ Py_buffer data = {NULL, NULL};
1861+ int usedforsecurity = 1;
1862+
1863+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1864+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1865+ if (!args) {
1866+ goto exit;
1867+ }
1868+ if (!noptargs) {
1869+ goto skip_optional_pos;
1870+ }
1871+ if (args[0]) {
1872+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1873+ goto exit;
1874+ }
1875+ if (!--noptargs) {
1876+ goto skip_optional_pos;
1877+ }
1878+ }
1879+skip_optional_pos:
1880+ if (!noptargs) {
1881+ goto skip_optional_kwonly;
1882+ }
1883+ usedforsecurity = PyObject_IsTrue(args[1]);
1884+ if (usedforsecurity < 0) {
1885+ goto exit;
1886+ }
1887+skip_optional_kwonly:
1888+ return_value = _hashlib_openssl_sha224_impl(module, &data, usedforsecurity);
1889+
1890+exit:
1891+ /* Cleanup for data */
1892+ if (data.obj) {
1893+ PyBuffer_Release(&data);
1894+ }
1895+
1896+ return return_value;
1897+}
1898+
1899+PyDoc_STRVAR(_hashlib_openssl_sha256__doc__,
1900+"openssl_sha256($module, /, data=<unrepresentable>, *,\n"
1901+" usedforsecurity=True)\n"
1902+"--\n"
1903+"\n"
1904+"Returns a sha256 hash object; optionally initialized with a string.");
1905+
1906+#define _HASHLIB_OPENSSL_SHA256_METHODDEF \
1907+ {"openssl_sha256", _PyCFunction_CAST(_hashlib_openssl_sha256), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha256__doc__},
1908+
1909+static PyObject *
1910+_hashlib_openssl_sha256_impl(PyObject *module, Py_buffer *data,
1911+ int usedforsecurity);
1912+
1913+static PyObject *
1914+_hashlib_openssl_sha256(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
1915+{
1916+ PyObject *return_value = NULL;
1917+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
1918+
1919+ #define NUM_KEYWORDS 2
1920+ static struct {
1921+ PyGC_Head _this_is_not_used;
1922+ PyObject_VAR_HEAD
1923+ Py_hash_t ob_hash;
1924+ PyObject *ob_item[NUM_KEYWORDS];
1925+ } _kwtuple = {
1926+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
1927+ .ob_hash = -1,
1928+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
1929+ };
1930+ #undef NUM_KEYWORDS
1931+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1932+
1933+ #else // !Py_BUILD_CORE
1934+ # define KWTUPLE NULL
1935+ #endif // !Py_BUILD_CORE
1936+
1937+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
1938+ static _PyArg_Parser _parser = {
1939+ .keywords = _keywords,
1940+ .fname = "openssl_sha256",
1941+ .kwtuple = KWTUPLE,
1942+ };
1943+ #undef KWTUPLE
1944+ PyObject *argsbuf[2];
1945+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
1946+ Py_buffer data = {NULL, NULL};
1947+ int usedforsecurity = 1;
1948+
1949+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
1950+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
1951+ if (!args) {
1952+ goto exit;
1953+ }
1954+ if (!noptargs) {
1955+ goto skip_optional_pos;
1956+ }
1957+ if (args[0]) {
1958+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1959+ goto exit;
1960+ }
1961+ if (!--noptargs) {
1962+ goto skip_optional_pos;
1963+ }
1964+ }
1965+skip_optional_pos:
1966+ if (!noptargs) {
1967+ goto skip_optional_kwonly;
1968+ }
1969+ usedforsecurity = PyObject_IsTrue(args[1]);
1970+ if (usedforsecurity < 0) {
1971+ goto exit;
1972+ }
1973+skip_optional_kwonly:
1974+ return_value = _hashlib_openssl_sha256_impl(module, &data, usedforsecurity);
1975+
1976+exit:
1977+ /* Cleanup for data */
1978+ if (data.obj) {
1979+ PyBuffer_Release(&data);
1980+ }
1981+
1982+ return return_value;
1983+}
1984+
1985+PyDoc_STRVAR(_hashlib_openssl_sha384__doc__,
1986+"openssl_sha384($module, /, data=<unrepresentable>, *,\n"
1987+" usedforsecurity=True)\n"
1988+"--\n"
1989+"\n"
1990+"Returns a sha384 hash object; optionally initialized with a string.");
1991+
1992+#define _HASHLIB_OPENSSL_SHA384_METHODDEF \
1993+ {"openssl_sha384", _PyCFunction_CAST(_hashlib_openssl_sha384), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha384__doc__},
1994+
1995+static PyObject *
1996+_hashlib_openssl_sha384_impl(PyObject *module, Py_buffer *data,
1997+ int usedforsecurity);
1998+
1999+static PyObject *
2000+_hashlib_openssl_sha384(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2001+{
2002+ PyObject *return_value = NULL;
2003+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
2004+
2005+ #define NUM_KEYWORDS 2
2006+ static struct {
2007+ PyGC_Head _this_is_not_used;
2008+ PyObject_VAR_HEAD
2009+ Py_hash_t ob_hash;
2010+ PyObject *ob_item[NUM_KEYWORDS];
2011+ } _kwtuple = {
2012+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
2013+ .ob_hash = -1,
2014+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
2015+ };
2016+ #undef NUM_KEYWORDS
2017+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
2018+
2019+ #else // !Py_BUILD_CORE
2020+ # define KWTUPLE NULL
2021+ #endif // !Py_BUILD_CORE
2022+
2023+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
2024+ static _PyArg_Parser _parser = {
2025+ .keywords = _keywords,
2026+ .fname = "openssl_sha384",
2027+ .kwtuple = KWTUPLE,
2028+ };
2029+ #undef KWTUPLE
2030+ PyObject *argsbuf[2];
2031+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
2032+ Py_buffer data = {NULL, NULL};
2033+ int usedforsecurity = 1;
2034+
2035+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
2036+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
2037+ if (!args) {
2038+ goto exit;
2039+ }
2040+ if (!noptargs) {
2041+ goto skip_optional_pos;
2042+ }
2043+ if (args[0]) {
2044+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
2045+ goto exit;
2046+ }
2047+ if (!--noptargs) {
2048+ goto skip_optional_pos;
2049+ }
2050+ }
2051+skip_optional_pos:
2052+ if (!noptargs) {
2053+ goto skip_optional_kwonly;
2054+ }
2055+ usedforsecurity = PyObject_IsTrue(args[1]);
2056+ if (usedforsecurity < 0) {
2057+ goto exit;
2058+ }
2059+skip_optional_kwonly:
2060+ return_value = _hashlib_openssl_sha384_impl(module, &data, usedforsecurity);
2061+
2062+exit:
2063+ /* Cleanup for data */
2064+ if (data.obj) {
2065+ PyBuffer_Release(&data);
2066+ }
2067+
2068+ return return_value;
2069+}
2070+
2071+PyDoc_STRVAR(_hashlib_openssl_sha512__doc__,
2072+"openssl_sha512($module, /, data=<unrepresentable>, *,\n"
2073+" usedforsecurity=True)\n"
2074+"--\n"
2075+"\n"
2076+"Returns a sha512 hash object; optionally initialized with a string.");
2077+
2078+#define _HASHLIB_OPENSSL_SHA512_METHODDEF \
2079+ {"openssl_sha512", _PyCFunction_CAST(_hashlib_openssl_sha512), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha512__doc__},
2080+
2081+static PyObject *
2082+_hashlib_openssl_sha512_impl(PyObject *module, Py_buffer *data,
2083+ int usedforsecurity);
2084+
2085+static PyObject *
2086+_hashlib_openssl_sha512(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2087+{
2088+ PyObject *return_value = NULL;
2089+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
2090+
2091+ #define NUM_KEYWORDS 2
2092+ static struct {
2093+ PyGC_Head _this_is_not_used;
2094+ PyObject_VAR_HEAD
2095+ Py_hash_t ob_hash;
2096+ PyObject *ob_item[NUM_KEYWORDS];
2097+ } _kwtuple = {
2098+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
2099+ .ob_hash = -1,
2100+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
2101+ };
2102+ #undef NUM_KEYWORDS
2103+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
2104+
2105+ #else // !Py_BUILD_CORE
2106+ # define KWTUPLE NULL
2107+ #endif // !Py_BUILD_CORE
2108+
2109+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
2110+ static _PyArg_Parser _parser = {
2111+ .keywords = _keywords,
2112+ .fname = "openssl_sha512",
2113+ .kwtuple = KWTUPLE,
2114+ };
2115+ #undef KWTUPLE
2116+ PyObject *argsbuf[2];
2117+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
2118+ Py_buffer data = {NULL, NULL};
2119+ int usedforsecurity = 1;
2120+
2121+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
2122+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
2123+ if (!args) {
2124+ goto exit;
2125+ }
2126+ if (!noptargs) {
2127+ goto skip_optional_pos;
2128+ }
2129+ if (args[0]) {
2130+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
2131+ goto exit;
2132+ }
2133+ if (!--noptargs) {
2134+ goto skip_optional_pos;
2135+ }
2136+ }
2137+skip_optional_pos:
2138+ if (!noptargs) {
2139+ goto skip_optional_kwonly;
2140+ }
2141+ usedforsecurity = PyObject_IsTrue(args[1]);
2142+ if (usedforsecurity < 0) {
2143+ goto exit;
2144+ }
2145+skip_optional_kwonly:
2146+ return_value = _hashlib_openssl_sha512_impl(module, &data, usedforsecurity);
2147+
2148+exit:
2149+ /* Cleanup for data */
2150+ if (data.obj) {
2151+ PyBuffer_Release(&data);
2152+ }
2153+
2154+ return return_value;
2155+}
2156+
2157+PyDoc_STRVAR(_hashlib_openssl_shake_128__doc__,
2158+"openssl_shake_128($module, /, data=<unrepresentable>, *,\n"
2159+" usedforsecurity=True)\n"
2160+"--\n"
2161+"\n"
2162+"Returns a shake-128 variable hash object; optionally initialized with a string.");
2163+
2164+#define _HASHLIB_OPENSSL_SHAKE_128_METHODDEF \
2165+ {"openssl_shake_128", _PyCFunction_CAST(_hashlib_openssl_shake_128), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_shake_128__doc__},
2166+
2167+static PyObject *
2168+_hashlib_openssl_shake_128_impl(PyObject *module, Py_buffer *data,
2169+ int usedforsecurity);
2170+
2171+static PyObject *
2172+_hashlib_openssl_shake_128(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2173+{
2174+ PyObject *return_value = NULL;
2175+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
2176+
2177+ #define NUM_KEYWORDS 2
2178+ static struct {
2179+ PyGC_Head _this_is_not_used;
2180+ PyObject_VAR_HEAD
2181+ Py_hash_t ob_hash;
2182+ PyObject *ob_item[NUM_KEYWORDS];
2183+ } _kwtuple = {
2184+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
2185+ .ob_hash = -1,
2186+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
2187+ };
2188+ #undef NUM_KEYWORDS
2189+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
2190+
2191+ #else // !Py_BUILD_CORE
2192+ # define KWTUPLE NULL
2193+ #endif // !Py_BUILD_CORE
2194+
2195+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
2196+ static _PyArg_Parser _parser = {
2197+ .keywords = _keywords,
2198+ .fname = "openssl_shake_128",
2199+ .kwtuple = KWTUPLE,
2200+ };
2201+ #undef KWTUPLE
2202+ PyObject *argsbuf[2];
2203+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
2204+ Py_buffer data = {NULL, NULL};
2205+ int usedforsecurity = 1;
2206+
2207+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
2208+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
2209+ if (!args) {
2210+ goto exit;
2211+ }
2212+ if (!noptargs) {
2213+ goto skip_optional_pos;
2214+ }
2215+ if (args[0]) {
2216+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
2217+ goto exit;
2218+ }
2219+ if (!--noptargs) {
2220+ goto skip_optional_pos;
2221+ }
2222+ }
2223+skip_optional_pos:
2224+ if (!noptargs) {
2225+ goto skip_optional_kwonly;
2226+ }
2227+ usedforsecurity = PyObject_IsTrue(args[1]);
2228+ if (usedforsecurity < 0) {
2229+ goto exit;
2230+ }
2231+skip_optional_kwonly:
2232+ return_value = _hashlib_openssl_shake_128_impl(module, &data, usedforsecurity);
2233+
2234+exit:
2235+ /* Cleanup for data */
2236+ if (data.obj) {
2237+ PyBuffer_Release(&data);
2238+ }
2239+
2240+ return return_value;
2241+}
2242+
2243+PyDoc_STRVAR(_hashlib_openssl_shake_256__doc__,
2244+"openssl_shake_256($module, /, data=<unrepresentable>, *,\n"
2245+" usedforsecurity=True)\n"
2246+"--\n"
2247+"\n"
2248+"Returns a shake-256 variable hash object; optionally initialized with a string.");
2249+
2250+#define _HASHLIB_OPENSSL_SHAKE_256_METHODDEF \
2251+ {"openssl_shake_256", _PyCFunction_CAST(_hashlib_openssl_shake_256), METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_shake_256__doc__},
2252+
2253+static PyObject *
2254+_hashlib_openssl_shake_256_impl(PyObject *module, Py_buffer *data,
2255+ int usedforsecurity);
2256+
2257+static PyObject *
2258+_hashlib_openssl_shake_256(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2259+{
2260+ PyObject *return_value = NULL;
2261+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
2262+
2263+ #define NUM_KEYWORDS 2
2264+ static struct {
2265+ PyGC_Head _this_is_not_used;
2266+ PyObject_VAR_HEAD
2267+ Py_hash_t ob_hash;
2268+ PyObject *ob_item[NUM_KEYWORDS];
2269+ } _kwtuple = {
2270+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
2271+ .ob_hash = -1,
2272+ .ob_item = { &_Py_ID(data), &_Py_ID(usedforsecurity), },
2273+ };
2274+ #undef NUM_KEYWORDS
2275+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
2276+
2277+ #else // !Py_BUILD_CORE
2278+ # define KWTUPLE NULL
2279+ #endif // !Py_BUILD_CORE
2280+
2281+ static const char * const _keywords[] = {"data", "usedforsecurity", NULL};
2282+ static _PyArg_Parser _parser = {
2283+ .keywords = _keywords,
2284+ .fname = "openssl_shake_256",
2285+ .kwtuple = KWTUPLE,
2286+ };
2287+ #undef KWTUPLE
2288+ PyObject *argsbuf[2];
2289+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
2290+ Py_buffer data = {NULL, NULL};
2291+ int usedforsecurity = 1;
2292+
2293+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
2294+ /*minpos*/ 0, /*maxpos*/ 1, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
2295+ if (!args) {
2296+ goto exit;
2297+ }
2298+ if (!noptargs) {
2299+ goto skip_optional_pos;
2300+ }
2301+ if (args[0]) {
2302+ if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
2303+ goto exit;
2304+ }
2305+ if (!--noptargs) {
2306+ goto skip_optional_pos;
2307+ }
2308+ }
2309+skip_optional_pos:
2310+ if (!noptargs) {
2311+ goto skip_optional_kwonly;
2312+ }
2313+ usedforsecurity = PyObject_IsTrue(args[1]);
2314+ if (usedforsecurity < 0) {
2315+ goto exit;
2316+ }
2317+skip_optional_kwonly:
2318+ return_value = _hashlib_openssl_shake_256_impl(module, &data, usedforsecurity);
2319+
2320+exit:
2321+ /* Cleanup for data */
2322+ if (data.obj) {
2323+ PyBuffer_Release(&data);
2324+ }
2325+
2326+ return return_value;
2327+}
2328+
2329+PyDoc_STRVAR(_hashlib_compare_digest__doc__,
2330+"compare_digest($module, a, b, /)\n"
2331+"--\n"
2332+"\n"
2333+"Return \'a == b\'.\n"
2334+"\n"
2335+"This function uses an approach designed to prevent\n"
2336+"timing analysis, making it appropriate for cryptography.\n"
2337+"\n"
2338+"a and b must both be of the same type: either str (ASCII only),\n"
2339+"or any bytes-like object.\n"
2340+"\n"
2341+"Note: If a and b are of different lengths, or if an error occurs,\n"
2342+"a timing attack could theoretically reveal information about the\n"
2343+"types and lengths of a and b--but not their values.");
2344+
2345+#define _HASHLIB_COMPARE_DIGEST_METHODDEF \
2346+ {"compare_digest", _PyCFunction_CAST(_hashlib_compare_digest), METH_FASTCALL, _hashlib_compare_digest__doc__},
2347+
2348+static PyObject *
2349+_hashlib_compare_digest_impl(PyObject *module, PyObject *a, PyObject *b);
2350+
2351+static PyObject *
2352+_hashlib_compare_digest(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
2353+{
2354+ PyObject *return_value = NULL;
2355+ PyObject *a;
2356+ PyObject *b;
2357+
2358+ if (!_PyArg_CheckPositional("compare_digest", nargs, 2, 2)) {
2359+ goto exit;
2360+ }
2361+ a = args[0];
2362+ b = args[1];
2363+ return_value = _hashlib_compare_digest_impl(module, a, b);
2364+
2365+exit:
2366+ return return_value;
2367+}
2368+
2369+PyDoc_STRVAR(_hashlib_hmac_singleshot__doc__,
2370+"hmac_digest($module, /, key, msg, digest)\n"
2371+"--\n"
2372+"\n"
2373+"Single-shot HMAC.");
2374+
2375+#define _HASHLIB_HMAC_SINGLESHOT_METHODDEF \
2376+ {"hmac_digest", _PyCFunction_CAST(_hashlib_hmac_singleshot), METH_FASTCALL|METH_KEYWORDS, _hashlib_hmac_singleshot__doc__},
2377+
2378+static PyObject *
2379+_hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
2380+ Py_buffer *msg, const char *digest);
2381+
2382+static PyObject *
2383+_hashlib_hmac_singleshot(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2384+{
2385+ PyObject *return_value = NULL;
2386+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
2387+
2388+ #define NUM_KEYWORDS 3
2389+ static struct {
2390+ PyGC_Head _this_is_not_used;
2391+ PyObject_VAR_HEAD
2392+ Py_hash_t ob_hash;
2393+ PyObject *ob_item[NUM_KEYWORDS];
2394+ } _kwtuple = {
2395+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
2396+ .ob_hash = -1,
2397+ .ob_item = { &_Py_ID(key), &_Py_ID(msg), &_Py_ID(digest), },
2398+ };
2399+ #undef NUM_KEYWORDS
2400+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
2401+
2402+ #else // !Py_BUILD_CORE
2403+ # define KWTUPLE NULL
2404+ #endif // !Py_BUILD_CORE
2405+
2406+ static const char * const _keywords[] = {"key", "msg", "digest", NULL};
2407+ static _PyArg_Parser _parser = {
2408+ .keywords = _keywords,
2409+ .fname = "hmac_digest",
2410+ .kwtuple = KWTUPLE,
2411+ };
2412+ #undef KWTUPLE
2413+ PyObject *argsbuf[3];
2414+ Py_buffer key = {NULL, NULL};
2415+ Py_buffer msg = {NULL, NULL};
2416+ const char *digest;
2417+
2418+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
2419+ /*minpos*/ 3, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
2420+ if (!args) {
2421+ goto exit;
2422+ }
2423+ if (PyObject_GetBuffer(args[0], &key, PyBUF_SIMPLE) != 0) {
2424+ goto exit;
2425+ }
2426+ if (PyObject_GetBuffer(args[1], &msg, PyBUF_SIMPLE) != 0) {
2427+ goto exit;
2428+ }
2429+ if (!PyUnicode_Check(args[2])) {
2430+ _PyArg_BadArgument("hmac_digest", "argument 'digest'", "str", args[2]);
2431+ goto exit;
2432+ }
2433+ Py_ssize_t digest_length;
2434+ digest = PyUnicode_AsUTF8AndSize(args[2], &digest_length);
2435+ if (digest == NULL) {
2436+ goto exit;
2437+ }
2438+ if (strlen(digest) != (size_t)digest_length) {
2439+ PyErr_SetString(PyExc_ValueError, "embedded null character");
2440+ goto exit;
2441+ }
2442+ return_value = _hashlib_hmac_singleshot_impl(module, &key, &msg, digest);
2443+
2444+exit:
2445+ /* Cleanup for key */
2446+ if (key.obj) {
2447+ PyBuffer_Release(&key);
2448+ }
2449+ /* Cleanup for msg */
2450+ if (msg.obj) {
2451+ PyBuffer_Release(&msg);
2452+ }
2453+
2454+ return return_value;
2455+}
2456+
2457+PyDoc_STRVAR(_hashlib_hmac_new__doc__,
2458+"hmac_new($module, /, key, msg=<unrepresentable>, digestmod=None)\n"
2459+"--\n"
2460+"\n"
2461+"Return a new hmac object.");
2462+
2463+#define _HASHLIB_HMAC_NEW_METHODDEF \
2464+ {"hmac_new", _PyCFunction_CAST(_hashlib_hmac_new), METH_FASTCALL|METH_KEYWORDS, _hashlib_hmac_new__doc__},
2465+
2466+static PyObject *
2467+_hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, Py_buffer *msg,
2468+ const char *digestmod);
2469+
2470+static PyObject *
2471+_hashlib_hmac_new(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
2472+{
2473+ PyObject *return_value = NULL;
2474+ #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
2475+
2476+ #define NUM_KEYWORDS 3
2477+ static struct {
2478+ PyGC_Head _this_is_not_used;
2479+ PyObject_VAR_HEAD
2480+ Py_hash_t ob_hash;
2481+ PyObject *ob_item[NUM_KEYWORDS];
2482+ } _kwtuple = {
2483+ .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
2484+ .ob_hash = -1,
2485+ .ob_item = { &_Py_ID(key), &_Py_ID(msg), &_Py_ID(digestmod), },
2486+ };
2487+ #undef NUM_KEYWORDS
2488+ #define KWTUPLE (&_kwtuple.ob_base.ob_base)
2489+
2490+ #else // !Py_BUILD_CORE
2491+ # define KWTUPLE NULL
2492+ #endif // !Py_BUILD_CORE
2493+
2494+ static const char * const _keywords[] = {"key", "msg", "digestmod", NULL};
2495+ static _PyArg_Parser _parser = {
2496+ .keywords = _keywords,
2497+ .fname = "hmac_new",
2498+ .kwtuple = KWTUPLE,
2499+ };
2500+ #undef KWTUPLE
2501+ PyObject *argsbuf[3];
2502+ Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
2503+ Py_buffer key = {NULL, NULL};
2504+ Py_buffer msg = {NULL, NULL};
2505+ const char *digestmod = NULL;
2506+
2507+ args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser,
2508+ /*minpos*/ 1, /*maxpos*/ 3, /*minkw*/ 0, /*varpos*/ 0, argsbuf);
2509+ if (!args) {
2510+ goto exit;
2511+ }
2512+ if (PyObject_GetBuffer(args[0], &key, PyBUF_SIMPLE) != 0) {
2513+ goto exit;
2514+ }
2515+ if (!noptargs) {
2516+ goto skip_optional_pos;
2517+ }
2518+ if (args[1]) {
2519+ if (PyObject_GetBuffer(args[1], &msg, PyBUF_SIMPLE) != 0) {
2520+ goto exit;
2521+ }
2522+ if (!--noptargs) {
2523+ goto skip_optional_pos;
2524+ }
2525+ }
2526+ if (!PyUnicode_Check(args[2])) {
2527+ _PyArg_BadArgument("hmac_new", "argument 'digestmod'", "str", args[2]);
2528+ goto exit;
2529+ }
2530+ Py_ssize_t digestmod_length;
2531+ digestmod = PyUnicode_AsUTF8AndSize(args[2], &digestmod_length);
2532+ if (digestmod == NULL) {
2533+ goto exit;
2534+ }
2535+ if (strlen(digestmod) != (size_t)digestmod_length) {
2536+ PyErr_SetString(PyExc_ValueError, "embedded null character");
2537+ goto exit;
2538+ }
2539+skip_optional_pos:
2540+ return_value = _hashlib_hmac_new_impl(module, &key, &msg, digestmod);
2541+
2542+exit:
2543+ /* Cleanup for key */
2544+ if (key.obj) {
2545+ PyBuffer_Release(&key);
2546+ }
2547+ /* Cleanup for msg */
2548+ if (msg.obj) {
2549+ PyBuffer_Release(&msg);
2550+ }
2551+
2552+ return return_value;
2553+}
2554+/*[clinic end generated code: output=6b2c621573a26c54 input=a9049054013a1b77]*/
2555--
25562.54.0
2557