2626#include <openssl/objects.h>
2727#include "openssl/err.h"
2828
29- #include "clinic/_hashopenssl.c.h"
30- /*[clinic input]
31- module _hashlib
32- [clinic start generated code]*/
33- /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c2b4ff081bac4be1]*/
34-
3529#define MUNCH_SIZE INT_MAX
3630
3731#ifndef HASH_OBJ_CONSTRUCTOR
@@ -72,6 +66,13 @@ DEFINE_CONSTS_FOR_NEW(sha256)
7266DEFINE_CONSTS_FOR_NEW (sha384 )
7367DEFINE_CONSTS_FOR_NEW (sha512 )
7468
69+ #include "clinic/_hashopenssl.c.h"
70+ /*[clinic input]
71+ module _hashlib
72+ class _hashlib.HASH "EVPobject *" "&EVPtype"
73+ [clinic start generated code]*/
74+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=a881a5092eecad28]*/
75+
7576
7677/* LCOV_EXCL_START */
7778static PyObject *
@@ -169,11 +170,15 @@ locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
169170
170171/* External methods for a hash object */
171172
172- PyDoc_STRVAR (EVP_copy__doc__ , "Return a copy of the hash object." );
173+ /*[clinic input]
174+ _hashlib.HASH.copy as EVP_copy
173175
176+ Return a copy of the hash object.
177+ [clinic start generated code]*/
174178
175179static PyObject *
176- EVP_copy (EVPobject * self , PyObject * unused )
180+ EVP_copy_impl (EVPobject * self )
181+ /*[clinic end generated code: output=b370c21cdb8ca0b4 input=31455b6a3e638069]*/
177182{
178183 EVPobject * newobj ;
179184
@@ -186,11 +191,15 @@ EVP_copy(EVPobject *self, PyObject *unused)
186191 return (PyObject * )newobj ;
187192}
188193
189- PyDoc_STRVAR (EVP_digest__doc__ ,
190- "Return the digest value as a bytes object." );
194+ /*[clinic input]
195+ _hashlib.HASH.digest as EVP_digest
196+
197+ Return the digest value as a bytes object.
198+ [clinic start generated code]*/
191199
192200static PyObject *
193- EVP_digest (EVPobject * self , PyObject * unused )
201+ EVP_digest_impl (EVPobject * self )
202+ /*[clinic end generated code: output=0f6a3a0da46dc12d input=03561809a419bf00]*/
194203{
195204 unsigned char digest [EVP_MAX_MD_SIZE ];
196205 EVP_MD_CTX * temp_ctx ;
@@ -217,11 +226,15 @@ EVP_digest(EVPobject *self, PyObject *unused)
217226 return retval ;
218227}
219228
220- PyDoc_STRVAR (EVP_hexdigest__doc__ ,
221- "Return the digest value as a string of hexadecimal digits." );
229+ /*[clinic input]
230+ _hashlib.HASH.hexdigest as EVP_hexdigest
231+
232+ Return the digest value as a string of hexadecimal digits.
233+ [clinic start generated code]*/
222234
223235static PyObject *
224- EVP_hexdigest (EVPobject * self , PyObject * unused )
236+ EVP_hexdigest_impl (EVPobject * self )
237+ /*[clinic end generated code: output=18e6decbaf197296 input=aff9cf0e4c741a9a]*/
225238{
226239 unsigned char digest [EVP_MAX_MD_SIZE ];
227240 EVP_MD_CTX * temp_ctx ;
@@ -248,18 +261,21 @@ EVP_hexdigest(EVPobject *self, PyObject *unused)
248261 return _Py_strhex ((const char * )digest , (Py_ssize_t )digest_size );
249262}
250263
251- PyDoc_STRVAR (EVP_update__doc__ ,
252- "Update this hash object's state with the provided string." );
264+ /*[clinic input]
265+ _hashlib.HASH.update as EVP_update
266+
267+ obj: object
268+ /
269+
270+ Update this hash object's state with the provided string.
271+ [clinic start generated code]*/
253272
254273static PyObject *
255- EVP_update (EVPobject * self , PyObject * args )
274+ EVP_update (EVPobject * self , PyObject * obj )
275+ /*[clinic end generated code: output=ec1d55ed2432e966 input=9b30ec848f015501]*/
256276{
257- PyObject * obj ;
258277 Py_buffer view ;
259278
260- if (!PyArg_ParseTuple (args , "O:update" , & obj ))
261- return NULL ;
262-
263279 GET_BUFFER_VIEW_OR_ERROUT (obj , & view );
264280
265281 if (self -> lock == NULL && view .len >= HASHLIB_GIL_MINSIZE ) {
@@ -282,10 +298,10 @@ EVP_update(EVPobject *self, PyObject *args)
282298}
283299
284300static PyMethodDef EVP_methods [] = {
285- { "update" , ( PyCFunction ) EVP_update , METH_VARARGS , EVP_update__doc__ },
286- { "digest" , ( PyCFunction ) EVP_digest , METH_NOARGS , EVP_digest__doc__ },
287- { "hexdigest" , ( PyCFunction ) EVP_hexdigest , METH_NOARGS , EVP_hexdigest__doc__ },
288- { "copy" , ( PyCFunction ) EVP_copy , METH_NOARGS , EVP_copy__doc__ },
301+ EVP_UPDATE_METHODDEF
302+ EVP_DIGEST_METHODDEF
303+ EVP_HEXDIGEST_METHODDEF
304+ EVP_COPY_METHODDEF
289305 {NULL , NULL } /* sentinel */
290306};
291307
@@ -329,24 +345,37 @@ EVP_repr(EVPobject *self)
329345 return PyUnicode_FromFormat ("<%U HASH object @ %p>" , self -> name , self );
330346}
331347
332- #if HASH_OBJ_CONSTRUCTOR
348+ /*[clinic input]
349+ _hashlib.HASH.__init__ as EVP_tp_init
350+
351+ name as name_obj: object
352+ string as data_obj: object(py_default="b''") = NULL
353+
354+ A hash is an object used to calculate a checksum of a string of information.
355+
356+ Methods:
357+
358+ update() -- updates the current digest with an additional string
359+ digest() -- return the current digest value
360+ hexdigest() -- return the current digest as a string of hexadecimal digits
361+ copy() -- return a copy of the current hash object
362+
363+ Attributes:
364+
365+ name -- the hash algorithm being used by this object
366+ digest_size -- number of bytes in this hashes output
367+ [clinic start generated code]*/
368+
333369static int
334- EVP_tp_init (EVPobject * self , PyObject * args , PyObject * kwds )
370+ EVP_tp_init_impl (EVPobject * self , PyObject * name_obj , PyObject * data_obj )
371+ /*[clinic end generated code: output=44766d27757cf851 input=dac22658387f9b5d]*/
335372{
336- static char * kwlist [] = {"name" , "string" , NULL };
337- PyObject * name_obj = NULL ;
338- PyObject * data_obj = NULL ;
339373 Py_buffer view ;
340374 char * nameStr ;
341375 const EVP_MD * digest ;
342376
343- if (!PyArg_ParseTupleAndKeywords (args , kwds , "O|O:HASH" , kwlist ,
344- & name_obj , & data_obj )) {
345- return -1 ;
346- }
347-
348377 if (data_obj )
349- GET_BUFFER_VIEW_OR_ERROUT (data_obj , & view );
378+ GET_BUFFER_VIEW_OR_ERROR (data_obj , & view , return - 1 );
350379
351380 if (!PyArg_Parse (name_obj , "s" , & nameStr )) {
352381 PyErr_SetString (PyExc_TypeError , "name must be a string" );
@@ -385,24 +414,7 @@ EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
385414
386415 return 0 ;
387416}
388- #endif
389-
390417
391- PyDoc_STRVAR (hashtype_doc ,
392- "A hash represents the object used to calculate a checksum of a\n\
393- string of information.\n\
394- \n\
395- Methods:\n\
396- \n\
397- update() -- updates the current digest with an additional string\n\
398- digest() -- return the current digest value\n\
399- hexdigest() -- return the current digest as a string of hexadecimal digits\n\
400- copy() -- return a copy of the current hash object\n\
401- \n\
402- Attributes:\n\
403- \n\
404- name -- the hash algorithm being used by this object\n\
405- digest_size -- number of bytes in this hashes output\n" );
406418
407419static PyTypeObject EVPtype = {
408420 PyVarObject_HEAD_INIT (NULL , 0 )
@@ -426,7 +438,7 @@ static PyTypeObject EVPtype = {
426438 0 , /*tp_setattro*/
427439 0 , /*tp_as_buffer*/
428440 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE , /*tp_flags*/
429- hashtype_doc , /*tp_doc*/
441+ EVP_tp_init__doc__ , /*tp_doc*/
430442 0 , /*tp_traverse*/
431443 0 , /*tp_clear*/
432444 0 , /*tp_richcompare*/
@@ -489,29 +501,29 @@ EVPnew(PyObject *name_obj,
489501
490502/* The module-level function: new() */
491503
492- PyDoc_STRVAR (EVP_new__doc__ ,
493- "Return a new hash object using the named algorithm.\n\
494- An optional string argument may be provided and will be\n\
495- automatically hashed.\n\
496- \n\
497- The MD5 and SHA1 algorithms are always supported.\n" );
504+ /*[clinic input]
505+ _hashlib.new as EVP_new
506+
507+ name as name_obj: object
508+ string as data_obj: object(py_default="b''") = NULL
509+
510+ Return a new hash object using the named algorithm.
511+
512+ An optional string argument may be provided and will be
513+ automatically hashed.
514+
515+ The MD5 and SHA1 algorithms are always supported.
516+ [clinic start generated code]*/
498517
499518static PyObject *
500- EVP_new (PyObject * self , PyObject * args , PyObject * kwdict )
519+ EVP_new_impl (PyObject * module , PyObject * name_obj , PyObject * data_obj )
520+ /*[clinic end generated code: output=9e7cf664e04b0226 input=1c46e40e0fec91f3]*/
501521{
502- static char * kwlist [] = {"name" , "string" , NULL };
503- PyObject * name_obj = NULL ;
504- PyObject * data_obj = NULL ;
505522 Py_buffer view = { 0 };
506523 PyObject * ret_obj ;
507524 char * name ;
508525 const EVP_MD * digest ;
509526
510- if (!PyArg_ParseTupleAndKeywords (args , kwdict , "O|O:new" , kwlist ,
511- & name_obj , & data_obj )) {
512- return NULL ;
513- }
514-
515527 if (!PyArg_Parse (name_obj , "s" , & name )) {
516528 PyErr_SetString (PyExc_TypeError , "name must be a string" );
517529 return NULL ;
@@ -619,43 +631,44 @@ PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen,
619631#endif
620632
621633
622- PyDoc_STRVAR (pbkdf2_hmac__doc__ ,
623- "pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\
624- \n\
625- Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\
626- pseudorandom function." );
634+
635+ /*[clinic input]
636+ _hashlib.pbkdf2_hmac as pbkdf2_hmac
637+
638+ hash_name: str
639+ password: Py_buffer
640+ salt: Py_buffer
641+ iterations: long
642+ dklen as dklen_obj: object = None
643+
644+ Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as pseudorandom function.
645+ [clinic start generated code]*/
627646
628647static PyObject *
629- pbkdf2_hmac (PyObject * self , PyObject * args , PyObject * kwdict )
648+ pbkdf2_hmac_impl (PyObject * module , const char * hash_name ,
649+ Py_buffer * password , Py_buffer * salt , long iterations ,
650+ PyObject * dklen_obj )
651+ /*[clinic end generated code: output=144b76005416599b input=ed3ab0d2d28b5d5c]*/
630652{
631- static char * kwlist [] = {"hash_name" , "password" , "salt" , "iterations" ,
632- "dklen" , NULL };
633- PyObject * key_obj = NULL , * dklen_obj = Py_None ;
634- char * name , * key ;
635- Py_buffer password , salt ;
636- long iterations , dklen ;
653+ PyObject * key_obj = NULL ;
654+ char * key ;
655+ long dklen ;
637656 int retval ;
638657 const EVP_MD * digest ;
639658
640- if (!PyArg_ParseTupleAndKeywords (args , kwdict , "sy*y*l|O:pbkdf2_hmac" ,
641- kwlist , & name , & password , & salt ,
642- & iterations , & dklen_obj )) {
643- return NULL ;
644- }
645-
646- digest = EVP_get_digestbyname (name );
659+ digest = EVP_get_digestbyname (hash_name );
647660 if (digest == NULL ) {
648661 PyErr_SetString (PyExc_ValueError , "unsupported hash type" );
649662 goto end ;
650663 }
651664
652- if (password . len > INT_MAX ) {
665+ if (password -> len > INT_MAX ) {
653666 PyErr_SetString (PyExc_OverflowError ,
654667 "password is too long." );
655668 goto end ;
656669 }
657670
658- if (salt . len > INT_MAX ) {
671+ if (salt -> len > INT_MAX ) {
659672 PyErr_SetString (PyExc_OverflowError ,
660673 "salt is too long." );
661674 goto end ;
@@ -700,13 +713,13 @@ pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
700713
701714 Py_BEGIN_ALLOW_THREADS
702715#if HAS_FAST_PKCS5_PBKDF2_HMAC
703- retval = PKCS5_PBKDF2_HMAC ((char * )password . buf , (int )password . len ,
704- (unsigned char * )salt . buf , (int )salt . len ,
716+ retval = PKCS5_PBKDF2_HMAC ((char * )password -> buf , (int )password -> len ,
717+ (unsigned char * )salt -> buf , (int )salt -> len ,
705718 iterations , digest , dklen ,
706719 (unsigned char * )key );
707720#else
708- retval = PKCS5_PBKDF2_HMAC_fast ((char * )password . buf , (int )password . len ,
709- (unsigned char * )salt . buf , (int )salt . len ,
721+ retval = PKCS5_PBKDF2_HMAC_fast ((char * )password -> buf , (int )password -> len ,
722+ (unsigned char * )salt -> buf , (int )salt -> len ,
710723 iterations , digest , dklen ,
711724 (unsigned char * )key );
712725#endif
@@ -719,8 +732,6 @@ pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict)
719732 }
720733
721734 end :
722- PyBuffer_Release (& password );
723- PyBuffer_Release (& salt );
724735 return key_obj ;
725736}
726737
@@ -1034,10 +1045,9 @@ GEN_CONSTRUCTOR(sha512)
10341045/* List of functions exported by this module */
10351046
10361047static struct PyMethodDef EVP_functions [] = {
1037- { "new" , ( PyCFunction )( void ( * )( void )) EVP_new , METH_VARARGS | METH_KEYWORDS , EVP_new__doc__ },
1048+ EVP_NEW_METHODDEF
10381049#ifdef PY_PBKDF2_HMAC
1039- {"pbkdf2_hmac" , (PyCFunction )(void (* )(void ))pbkdf2_hmac , METH_VARARGS |METH_KEYWORDS ,
1040- pbkdf2_hmac__doc__ },
1050+ PBKDF2_HMAC_METHODDEF
10411051#endif
10421052 _HASHLIB_SCRYPT_METHODDEF
10431053 _HASHLIB_HMAC_DIGEST_METHODDEF
0 commit comments