Mercurial > cpython-lru_cache-weakref
annotate Modules/_hashopenssl.c @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | ce19003cbfc3 |
| children |
| rev | line source |
|---|---|
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
1 /* Module that wraps all OpenSSL hash algorithms */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
2 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
3 /* |
|
58218
89a9dd8c628d
Merged revisions 77209,77229,77359-77360,77371 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
53757
diff
changeset
|
4 * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
5 * Licensed to PSF under a Contributor Agreement. |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
6 * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
7 * Derived from a skeleton of shamodule.c containing work performed by: |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
8 * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
9 * Andrew Kuchling (amk@amk.ca) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
10 * Greg Stein (gstein@lyra.org) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
11 * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
12 */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
13 |
|
36777
1211a68b28dd
Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
14 #define PY_SSIZE_T_CLEAN |
|
1211a68b28dd
Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents:
36675
diff
changeset
|
15 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
16 #include "Python.h" |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
17 #include "structmember.h" |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
18 #include "hashlib.h" |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
19 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
20 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
21 /* EVP is the preferred interface to hashing in OpenSSL */ |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
22 #include <openssl/evp.h> |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
23 #include <openssl/hmac.h> |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
24 /* We use the object interface to discover what hashes OpenSSL supports. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
25 #include <openssl/objects.h> |
|
86264
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
26 #include "openssl/err.h" |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
27 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
28 #define MUNCH_SIZE INT_MAX |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
29 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
30 #ifndef HASH_OBJ_CONSTRUCTOR |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
31 #define HASH_OBJ_CONSTRUCTOR 0 |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
32 #endif |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
33 |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
34 /* Minimum OpenSSL version needed to support sha224 and higher. */ |
|
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
35 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000) |
|
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
36 #define _OPENSSL_SUPPORTS_SHA2 |
|
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
37 #endif |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
38 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
39 typedef struct { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
40 PyObject_HEAD |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
41 PyObject *name; /* name of this hash algorithm */ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
42 EVP_MD_CTX ctx; /* OpenSSL message digest context */ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
43 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
44 PyThread_type_lock lock; /* OpenSSL context lock */ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
45 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
46 } EVPobject; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
47 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
48 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
49 static PyTypeObject EVPtype; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
50 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
51 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
52 #define DEFINE_CONSTS_FOR_NEW(Name) \ |
|
81898
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
53 static PyObject *CONST_ ## Name ## _name_obj = NULL; \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
54 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
55 static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
56 |
|
36222
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
57 DEFINE_CONSTS_FOR_NEW(md5) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
58 DEFINE_CONSTS_FOR_NEW(sha1) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
59 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
36222
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
60 DEFINE_CONSTS_FOR_NEW(sha224) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
61 DEFINE_CONSTS_FOR_NEW(sha256) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
62 DEFINE_CONSTS_FOR_NEW(sha384) |
|
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
63 DEFINE_CONSTS_FOR_NEW(sha512) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
64 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
65 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
66 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
67 static EVPobject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
68 newEVPobject(PyObject *name) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
69 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
70 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
71 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
72 /* save the name for .name to return */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
73 if (retval != NULL) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
74 Py_INCREF(name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
75 retval->name = name; |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
76 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
77 retval->lock = NULL; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
78 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
79 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
80 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
81 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
82 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
83 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
84 static void |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
85 EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
86 { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
87 unsigned int process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
88 const unsigned char *cp = (const unsigned char *)vp; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
89 while (0 < len) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
90 if (len > (Py_ssize_t)MUNCH_SIZE) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
91 process = MUNCH_SIZE; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
92 else |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
93 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
94 EVP_DigestUpdate(&self->ctx, (const void*)cp, process); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
95 len -= process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
96 cp += process; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
97 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
98 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
99 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
100 /* Internal methods for a hash object */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
101 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
102 static void |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
103 EVP_dealloc(EVPobject *self) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
104 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
105 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
106 if (self->lock != NULL) |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
107 PyThread_free_lock(self->lock); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
108 #endif |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
109 EVP_MD_CTX_cleanup(&self->ctx); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
110 Py_XDECREF(self->name); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
111 PyObject_Del(self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
112 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
113 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
114 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self) |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
115 { |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
116 ENTER_HASHLIB(self); |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
117 EVP_MD_CTX_copy(new_ctx_p, &self->ctx); |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
118 LEAVE_HASHLIB(self); |
|
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
119 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
120 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
121 /* External methods for a hash object */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
122 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
123 PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
124 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
125 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
126 static PyObject * |
|
38489
3fe3ccdf0155
Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents:
36801
diff
changeset
|
127 EVP_copy(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
128 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
129 EVPobject *newobj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
130 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
131 if ( (newobj = newEVPobject(self->name))==NULL) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
132 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
133 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
134 locked_EVP_MD_CTX_copy(&newobj->ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
135 return (PyObject *)newobj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
136 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
137 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
138 PyDoc_STRVAR(EVP_digest__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
139 "Return the digest value as a string of binary data."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
140 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
141 static PyObject * |
|
38489
3fe3ccdf0155
Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents:
36801
diff
changeset
|
142 EVP_digest(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
143 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
144 unsigned char digest[EVP_MAX_MD_SIZE]; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
145 EVP_MD_CTX temp_ctx; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
146 PyObject *retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
147 unsigned int digest_size; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
148 |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
149 locked_EVP_MD_CTX_copy(&temp_ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
150 digest_size = EVP_MD_CTX_size(&temp_ctx); |
|
36222
4c8a74a20227
Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents:
35999
diff
changeset
|
151 EVP_DigestFinal(&temp_ctx, digest, NULL); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
152 |
|
47301
23ab586c427a
Renamed PyString to PyBytes
Christian Heimes <christian@cheimes.de>
parents:
44351
diff
changeset
|
153 retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
154 EVP_MD_CTX_cleanup(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
155 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
156 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
157 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
158 PyDoc_STRVAR(EVP_hexdigest__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
159 "Return the digest value as a string of hexadecimal digits."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
160 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
161 static PyObject * |
|
38489
3fe3ccdf0155
Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents:
36801
diff
changeset
|
162 EVP_hexdigest(EVPobject *self, PyObject *unused) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
163 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
164 unsigned char digest[EVP_MAX_MD_SIZE]; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
165 EVP_MD_CTX temp_ctx; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
166 PyObject *retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
167 char *hex_digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
168 unsigned int i, j, digest_size; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
169 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
170 /* Get the raw (binary) digest value */ |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
171 locked_EVP_MD_CTX_copy(&temp_ctx, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
172 digest_size = EVP_MD_CTX_size(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
173 EVP_DigestFinal(&temp_ctx, digest, NULL); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
174 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
175 EVP_MD_CTX_cleanup(&temp_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
176 |
|
42178
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
177 /* Allocate a new buffer */ |
|
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
178 hex_digest = PyMem_Malloc(digest_size * 2 + 1); |
|
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
179 if (!hex_digest) |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
180 return PyErr_NoMemory(); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
181 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
182 /* Make hex version of the digest */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
183 for(i=j=0; i<digest_size; i++) { |
|
72918
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
184 unsigned char c; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
185 c = (digest[i] >> 4) & 0xf; |
|
72918
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
186 hex_digest[j++] = Py_hexdigits[c]; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
187 c = (digest[i] & 0xf); |
|
72918
d76338eacf7c
Issue #13088: Add shared Py_hexdigits constant to format a number into base 16
Victor Stinner <victor.stinner@haypocalc.com>
parents:
68257
diff
changeset
|
188 hex_digest[j++] = Py_hexdigits[c]; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
189 } |
|
42178
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
190 retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2); |
|
b2dc31718b03
Make sure hexdigest() returns str, not str8.
Guido van Rossum <guido@python.org>
parents:
42166
diff
changeset
|
191 PyMem_Free(hex_digest); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
192 return retval; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
193 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
194 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
195 PyDoc_STRVAR(EVP_update__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
196 "Update this hash object's state with the provided string."); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
197 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
198 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
199 EVP_update(EVPobject *self, PyObject *args) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
200 { |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
201 PyObject *obj; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
202 Py_buffer view; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
203 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
204 if (!PyArg_ParseTuple(args, "O:update", &obj)) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
205 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
206 |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
207 GET_BUFFER_VIEW_OR_ERROUT(obj, &view); |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
208 |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
209 #ifdef WITH_THREAD |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
210 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
211 self->lock = PyThread_allocate_lock(); |
|
53727
4cd437dface8
Merge refactoring I did when committing r72267 to trunk into the
Gregory P. Smith <greg@mad-scientist.com>
parents:
51543
diff
changeset
|
212 /* fail? lock = NULL and we fail over to non-threaded code. */ |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
213 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
214 |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
215 if (self->lock != NULL) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
216 Py_BEGIN_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
217 PyThread_acquire_lock(self->lock, 1); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
218 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
219 PyThread_release_lock(self->lock); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
220 Py_END_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
221 } else { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
222 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
223 } |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
224 #else |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
225 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
226 #endif |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
227 |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
228 PyBuffer_Release(&view); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
229 Py_RETURN_NONE; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
230 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
231 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
232 static PyMethodDef EVP_methods[] = { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
233 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__}, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
234 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__}, |
|
38489
3fe3ccdf0155
Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents:
36801
diff
changeset
|
235 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__}, |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
236 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__}, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
237 {NULL, NULL} /* sentinel */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
238 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
239 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
240 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
241 EVP_get_block_size(EVPobject *self, void *closure) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
242 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
243 long block_size; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
244 block_size = EVP_MD_CTX_block_size(&self->ctx); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
245 return PyLong_FromLong(block_size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
246 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
247 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
248 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
249 EVP_get_digest_size(EVPobject *self, void *closure) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
250 { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
251 long size; |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
252 size = EVP_MD_CTX_size(&self->ctx); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
253 return PyLong_FromLong(size); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
254 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
255 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
256 static PyMemberDef EVP_members[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
257 {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
258 {NULL} /* Sentinel */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
259 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
260 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
261 static PyGetSetDef EVP_getseters[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
262 {"digest_size", |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
263 (getter)EVP_get_digest_size, NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
264 NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
265 NULL}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
266 {"block_size", |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
267 (getter)EVP_get_block_size, NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
268 NULL, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
269 NULL}, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
270 {NULL} /* Sentinel */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
271 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
272 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
273 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
274 static PyObject * |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
275 EVP_repr(EVPobject *self) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
276 { |
|
59858
9714f8a77b16
Merged revisions 78875 via svnmerge from
Victor Stinner <victor.stinner@haypocalc.com>
parents:
59688
diff
changeset
|
277 return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
278 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
279 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
280 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
281 static int |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
282 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
283 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36232
diff
changeset
|
284 static char *kwlist[] = {"name", "string", NULL}; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
285 PyObject *name_obj = NULL; |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
286 PyObject *data_obj = NULL; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
287 Py_buffer view; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
288 char *nameStr; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
289 const EVP_MD *digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
290 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
291 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist, |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
292 &name_obj, &data_obj)) { |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
293 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
294 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
295 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
296 if (data_obj) |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
297 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
298 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
299 if (!PyArg_Parse(name_obj, "s", &nameStr)) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
300 PyErr_SetString(PyExc_TypeError, "name must be a string"); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
301 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
302 PyBuffer_Release(&view); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
303 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
304 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
305 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
306 digest = EVP_get_digestbyname(nameStr); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
307 if (!digest) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
308 PyErr_SetString(PyExc_ValueError, "unknown hash function"); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
309 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
310 PyBuffer_Release(&view); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
311 return -1; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
312 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
313 EVP_DigestInit(&self->ctx, digest); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
314 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
315 self->name = name_obj; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
316 Py_INCREF(self->name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
317 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
318 if (data_obj) { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
319 if (view.len >= HASHLIB_GIL_MINSIZE) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
320 Py_BEGIN_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
321 EVP_hash(self, view.buf, view.len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
322 Py_END_ALLOW_THREADS |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
323 } else { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
324 EVP_hash(self, view.buf, view.len); |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
325 } |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
326 PyBuffer_Release(&view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
327 } |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
328 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
329 return 0; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
330 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
331 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
332 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
333 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
334 PyDoc_STRVAR(hashtype_doc, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
335 "A hash represents the object used to calculate a checksum of a\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
336 string of information.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
337 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
338 Methods:\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
339 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
340 update() -- updates the current digest with an additional string\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
341 digest() -- return the current digest value\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
342 hexdigest() -- return the current digest as a string of hexadecimal digits\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
343 copy() -- return a copy of the current hash object\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
344 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
345 Attributes:\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
346 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
347 name -- the hash algorithm being used by this object\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
348 digest_size -- number of bytes in this hashes output\n"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
349 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
350 static PyTypeObject EVPtype = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
42178
diff
changeset
|
351 PyVarObject_HEAD_INIT(NULL, 0) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
352 "_hashlib.HASH", /*tp_name*/ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
353 sizeof(EVPobject), /*tp_basicsize*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
354 0, /*tp_itemsize*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
355 /* methods */ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
356 (destructor)EVP_dealloc, /*tp_dealloc*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
357 0, /*tp_print*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
358 0, /*tp_getattr*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
359 0, /*tp_setattr*/ |
|
51270
debd14c489b9
Issue #1717: rename tp_compare to tp_reserved. I'll change the
Mark Dickinson <dickinsm@gmail.com>
parents:
50636
diff
changeset
|
360 0, /*tp_reserved*/ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
361 (reprfunc)EVP_repr, /*tp_repr*/ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
362 0, /*tp_as_number*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
363 0, /*tp_as_sequence*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
364 0, /*tp_as_mapping*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
365 0, /*tp_hash*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
366 0, /*tp_call*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
367 0, /*tp_str*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
368 0, /*tp_getattro*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
369 0, /*tp_setattro*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
370 0, /*tp_as_buffer*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
371 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
372 hashtype_doc, /*tp_doc*/ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
373 0, /*tp_traverse*/ |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
374 0, /*tp_clear*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
375 0, /*tp_richcompare*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
376 0, /*tp_weaklistoffset*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
377 0, /*tp_iter*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
378 0, /*tp_iternext*/ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
379 EVP_methods, /* tp_methods */ |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
380 EVP_members, /* tp_members */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
381 EVP_getseters, /* tp_getset */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
382 #if 1 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
383 0, /* tp_base */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
384 0, /* tp_dict */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
385 0, /* tp_descr_get */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
386 0, /* tp_descr_set */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
387 0, /* tp_dictoffset */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
388 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
389 #if HASH_OBJ_CONSTRUCTOR |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
390 (initproc)EVP_tp_init, /* tp_init */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
391 #endif |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
392 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
393 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
394 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
395 EVPnew(PyObject *name_obj, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
396 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx, |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
397 const unsigned char *cp, Py_ssize_t len) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
398 { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
399 EVPobject *self; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
400 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
401 if (!digest && !initial_ctx) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
402 PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
403 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
404 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
405 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
406 if ((self = newEVPobject(name_obj)) == NULL) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
407 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
408 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
409 if (initial_ctx) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
410 EVP_MD_CTX_copy(&self->ctx, initial_ctx); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
411 } else { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
412 EVP_DigestInit(&self->ctx, digest); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
413 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
414 |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
415 if (cp && len) { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
416 if (len >= HASHLIB_GIL_MINSIZE) { |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
417 Py_BEGIN_ALLOW_THREADS |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
418 EVP_hash(self, cp, len); |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
419 Py_END_ALLOW_THREADS |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
420 } else { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
421 EVP_hash(self, cp, len); |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
422 } |
|
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
423 } |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
424 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
425 return (PyObject *)self; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
426 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
427 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
428 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
429 /* The module-level function: new() */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
430 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
431 PyDoc_STRVAR(EVP_new__doc__, |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
432 "Return a new hash object using the named algorithm.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
433 An optional string argument may be provided and will be\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
434 automatically hashed.\n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
435 \n\ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
436 The MD5 and SHA1 algorithms are always supported.\n"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
437 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
438 static PyObject * |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
439 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
440 { |
|
36675
dba6494735d0
Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36232
diff
changeset
|
441 static char *kwlist[] = {"name", "string", NULL}; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
442 PyObject *name_obj = NULL; |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
443 PyObject *data_obj = NULL; |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
444 Py_buffer view = { 0 }; |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
445 PyObject *ret_obj; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
446 char *name; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
447 const EVP_MD *digest; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
448 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
449 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist, |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
450 &name_obj, &data_obj)) { |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
451 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
452 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
453 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
454 if (!PyArg_Parse(name_obj, "s", &name)) { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
455 PyErr_SetString(PyExc_TypeError, "name must be a string"); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
456 return NULL; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
457 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
458 |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
459 if (data_obj) |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
460 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
461 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
462 digest = EVP_get_digestbyname(name); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
463 |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
464 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
465 |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
466 if (data_obj) |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
467 PyBuffer_Release(&view); |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
468 return ret_obj; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
469 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
470 |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
471 |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
472 |
|
86473
88fac1574049
Issue #18582: HMAC_CTX_copy() is not available on OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86469
diff
changeset
|
473 #if (OPENSSL_VERSION_NUMBER >= 0x10000000 && !defined(OPENSSL_NO_HMAC) \ |
|
88fac1574049
Issue #18582: HMAC_CTX_copy() is not available on OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86469
diff
changeset
|
474 && !defined(OPENSSL_NO_SHA)) |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
475 |
|
86264
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
476 #define PY_PBKDF2_HMAC 1 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
477 |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
478 /* Improved implementation of PKCS5_PBKDF2_HMAC() |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
479 * |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
480 * PKCS5_PBKDF2_HMAC_fast() hashes the password exactly one time instead of |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
481 * `iter` times. Today (2013) the iteration count is typically 100,000 or |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
482 * more. The improved algorithm is not subject to a Denial-of-Service |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
483 * vulnerability with overly large passwords. |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
484 * |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
485 * Also OpenSSL < 1.0 don't provide PKCS5_PBKDF2_HMAC(), only |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
486 * PKCS5_PBKDF2_SHA1. |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
487 */ |
|
86544
7f4e0c869c2e
Make PKCS5_PBKDF2_HMAC_fast() a static function
Christian Heimes <christian@cheimes.de>
parents:
86473
diff
changeset
|
488 static int |
|
7f4e0c869c2e
Make PKCS5_PBKDF2_HMAC_fast() a static function
Christian Heimes <christian@cheimes.de>
parents:
86473
diff
changeset
|
489 PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen, |
|
7f4e0c869c2e
Make PKCS5_PBKDF2_HMAC_fast() a static function
Christian Heimes <christian@cheimes.de>
parents:
86473
diff
changeset
|
490 const unsigned char *salt, int saltlen, |
|
7f4e0c869c2e
Make PKCS5_PBKDF2_HMAC_fast() a static function
Christian Heimes <christian@cheimes.de>
parents:
86473
diff
changeset
|
491 int iter, const EVP_MD *digest, |
|
7f4e0c869c2e
Make PKCS5_PBKDF2_HMAC_fast() a static function
Christian Heimes <christian@cheimes.de>
parents:
86473
diff
changeset
|
492 int keylen, unsigned char *out) |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
493 { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
494 unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
495 int cplen, j, k, tkeylen, mdlen; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
496 unsigned long i = 1; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
497 HMAC_CTX hctx_tpl, hctx; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
498 |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
499 mdlen = EVP_MD_size(digest); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
500 if (mdlen < 0) |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
501 return 0; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
502 |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
503 HMAC_CTX_init(&hctx_tpl); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
504 HMAC_CTX_init(&hctx); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
505 p = out; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
506 tkeylen = keylen; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
507 if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
508 HMAC_CTX_cleanup(&hctx_tpl); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
509 return 0; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
510 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
511 while(tkeylen) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
512 if(tkeylen > mdlen) |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
513 cplen = mdlen; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
514 else |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
515 cplen = tkeylen; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
516 /* We are unlikely to ever use more than 256 blocks (5120 bits!) |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
517 * but just in case... |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
518 */ |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
519 itmp[0] = (unsigned char)((i >> 24) & 0xff); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
520 itmp[1] = (unsigned char)((i >> 16) & 0xff); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
521 itmp[2] = (unsigned char)((i >> 8) & 0xff); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
522 itmp[3] = (unsigned char)(i & 0xff); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
523 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
524 HMAC_CTX_cleanup(&hctx_tpl); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
525 return 0; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
526 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
527 if (!HMAC_Update(&hctx, salt, saltlen) |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
528 || !HMAC_Update(&hctx, itmp, 4) |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
529 || !HMAC_Final(&hctx, digtmp, NULL)) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
530 HMAC_CTX_cleanup(&hctx_tpl); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
531 HMAC_CTX_cleanup(&hctx); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
532 return 0; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
533 } |
|
86963
07fa1ed0d551
Issue #18582: fix memory leak in pbkdf2 code
Christian Heimes <christian@cheimes.de>
parents:
86822
diff
changeset
|
534 HMAC_CTX_cleanup(&hctx); |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
535 memcpy(p, digtmp, cplen); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
536 for (j = 1; j < iter; j++) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
537 if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
538 HMAC_CTX_cleanup(&hctx_tpl); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
539 return 0; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
540 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
541 if (!HMAC_Update(&hctx, digtmp, mdlen) |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
542 || !HMAC_Final(&hctx, digtmp, NULL)) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
543 HMAC_CTX_cleanup(&hctx_tpl); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
544 HMAC_CTX_cleanup(&hctx); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
545 return 0; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
546 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
547 HMAC_CTX_cleanup(&hctx); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
548 for (k = 0; k < cplen; k++) { |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
549 p[k] ^= digtmp[k]; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
550 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
551 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
552 tkeylen-= cplen; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
553 i++; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
554 p+= cplen; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
555 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
556 HMAC_CTX_cleanup(&hctx_tpl); |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
557 return 1; |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
558 } |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
559 |
|
86822
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
560 static PyObject * |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
561 _setException(PyObject *exc) |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
562 { |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
563 unsigned long errcode; |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
564 const char *lib, *func, *reason; |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
565 |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
566 errcode = ERR_peek_last_error(); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
567 if (!errcode) { |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
568 PyErr_SetString(exc, "unknown reasons"); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
569 return NULL; |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
570 } |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
571 ERR_clear_error(); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
572 |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
573 lib = ERR_lib_error_string(errcode); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
574 func = ERR_func_error_string(errcode); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
575 reason = ERR_reason_error_string(errcode); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
576 |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
577 if (lib && func) { |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
578 PyErr_Format(exc, "[%s: %s] %s", lib, func, reason); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
579 } |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
580 else if (lib) { |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
581 PyErr_Format(exc, "[%s] %s", lib, reason); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
582 } |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
583 else { |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
584 PyErr_SetString(exc, reason); |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
585 } |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
586 return NULL; |
|
b59ef5f203da
Silence a compiler warning about an unused function
Brett Cannon <brett@python.org>
parents:
86735
diff
changeset
|
587 } |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
588 |
|
86264
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
589 PyDoc_STRVAR(pbkdf2_hmac__doc__, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
590 "pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\ |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
591 \n\ |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
592 Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\ |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
593 pseudorandom function."); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
594 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
595 static PyObject * |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
596 pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict) |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
597 { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
598 static char *kwlist[] = {"hash_name", "password", "salt", "iterations", |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
599 "dklen", NULL}; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
600 PyObject *key_obj = NULL, *dklen_obj = Py_None; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
601 char *name, *key; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
602 Py_buffer password, salt; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
603 long iterations, dklen; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
604 int retval; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
605 const EVP_MD *digest; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
606 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
607 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac", |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
608 kwlist, &name, &password, &salt, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
609 &iterations, &dklen_obj)) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
610 return NULL; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
611 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
612 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
613 digest = EVP_get_digestbyname(name); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
614 if (digest == NULL) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
615 PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
616 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
617 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
618 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
619 if (password.len > INT_MAX) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
620 PyErr_SetString(PyExc_OverflowError, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
621 "password is too long."); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
622 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
623 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
624 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
625 if (salt.len > INT_MAX) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
626 PyErr_SetString(PyExc_OverflowError, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
627 "salt is too long."); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
628 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
629 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
630 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
631 if (iterations < 1) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
632 PyErr_SetString(PyExc_ValueError, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
633 "iteration value must be greater than 0."); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
634 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
635 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
636 if (iterations > INT_MAX) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
637 PyErr_SetString(PyExc_OverflowError, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
638 "iteration value is too great."); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
639 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
640 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
641 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
642 if (dklen_obj == Py_None) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
643 dklen = EVP_MD_size(digest); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
644 } else { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
645 dklen = PyLong_AsLong(dklen_obj); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
646 if ((dklen == -1) && PyErr_Occurred()) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
647 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
648 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
649 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
650 if (dklen < 1) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
651 PyErr_SetString(PyExc_ValueError, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
652 "key length must be greater than 0."); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
653 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
654 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
655 if (dklen > INT_MAX) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
656 /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */ |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
657 PyErr_SetString(PyExc_OverflowError, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
658 "key length is too great."); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
659 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
660 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
661 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
662 key_obj = PyBytes_FromStringAndSize(NULL, dklen); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
663 if (key_obj == NULL) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
664 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
665 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
666 key = PyBytes_AS_STRING(key_obj); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
667 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
668 Py_BEGIN_ALLOW_THREADS |
|
87135
9e25367095c4
Fix compiler warnings on Windows 64 bit: add an explicit cast from Py_ssize_t
Victor Stinner <victor.stinner@gmail.com>
parents:
86963
diff
changeset
|
669 retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, (int)password.len, |
|
87240
ce19003cbfc3
Remove unused code path from PBKDF2 that is causing a warning on Win64
Christian Heimes <christian@cheimes.de>
parents:
87135
diff
changeset
|
670 (unsigned char *)salt.buf, (int)salt.len, |
|
86469
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
671 iterations, digest, dklen, |
|
0a26ef834a49
Issue #18582: provide a faster C implementation of pbkdf2_hmac that works with OpenSSL < 1.0
Christian Heimes <christian@cheimes.de>
parents:
86264
diff
changeset
|
672 (unsigned char *)key); |
|
86264
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
673 Py_END_ALLOW_THREADS |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
674 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
675 if (!retval) { |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
676 Py_CLEAR(key_obj); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
677 _setException(PyExc_ValueError); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
678 goto end; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
679 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
680 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
681 end: |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
682 PyBuffer_Release(&password); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
683 PyBuffer_Release(&salt); |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
684 return key_obj; |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
685 } |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
686 |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
687 #endif |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
688 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
689 /* State for our callback function so that it can accumulate a result. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
690 typedef struct _internal_name_mapper_state { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
691 PyObject *set; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
692 int error; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
693 } _InternalNameMapperState; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
694 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
695 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
696 /* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
697 static void |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
698 _openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
699 { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
700 _InternalNameMapperState *state = (_InternalNameMapperState *)arg; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
701 PyObject *py_name; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
702 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
703 assert(state != NULL); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
704 if (openssl_obj_name == NULL) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
705 return; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
706 /* Ignore aliased names, they pollute the list and OpenSSL appears to |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
707 * have a its own definition of alias as the resulting list still |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
708 * contains duplicate and alternate names for several algorithms. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
709 if (openssl_obj_name->alias) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
710 return; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
711 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
712 py_name = PyUnicode_FromString(openssl_obj_name->name); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
713 if (py_name == NULL) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
714 state->error = 1; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
715 } else { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
716 if (PySet_Add(state->set, py_name) != 0) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
717 state->error = 1; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
718 } |
|
86735
6fdbb81b4020
Issue #19420: Fix reference leak in module initalization code of _hashopenssl.c
Christian Heimes <christian@cheimes.de>
parents:
86570
diff
changeset
|
719 Py_DECREF(py_name); |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
720 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
721 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
722 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
723 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
724 /* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */ |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
725 static PyObject* |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
726 generate_hash_name_list(void) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
727 { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
728 _InternalNameMapperState state; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
729 state.set = PyFrozenSet_New(NULL); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
730 if (state.set == NULL) |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
731 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
732 state.error = 0; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
733 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
734 OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
735 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
736 if (state.error) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
737 Py_DECREF(state.set); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
738 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
739 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
740 return state.set; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
741 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
742 |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
743 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
744 /* |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
745 * This macro generates constructor function definitions for specific |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
746 * hash algorithms. These constructors are much faster than calling |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
747 * the generic one passing it a python string and are noticably |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
748 * faster than calling a python new() wrapper. Thats important for |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
749 * code that wants to make hashes of a bunch of small strings. |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
750 */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
751 #define GEN_CONSTRUCTOR(NAME) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
752 static PyObject * \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
753 EVP_new_ ## NAME (PyObject *self, PyObject *args) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
754 { \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
755 PyObject *data_obj = NULL; \ |
|
43423
0476a2cc5658
Change PyBuffer to Py_buffer to be consistent with other non-object structures like Py_complex. Add some more functionality to the memoryview object.
Travis E. Oliphant <oliphant@enthought.com>
parents:
42987
diff
changeset
|
756 Py_buffer view = { 0 }; \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
757 PyObject *ret_obj; \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
758 \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
759 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
760 return NULL; \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
761 } \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
762 \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
763 if (data_obj) \ |
|
51543
2ac02daef2ee
Fixes Issue #3745: Fix hashlib to always reject unicode and non
Gregory P. Smith <greg@mad-scientist.com>
parents:
51270
diff
changeset
|
764 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
765 \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
766 ret_obj = EVPnew( \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
767 CONST_ ## NAME ## _name_obj, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
768 NULL, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
769 CONST_new_ ## NAME ## _ctx_p, \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
770 (unsigned char*)view.buf, \ |
|
49292
a00626f2d705
Merged revisions 66496 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
48683
diff
changeset
|
771 view.len); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
772 \ |
|
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
773 if (data_obj) \ |
|
48683
cd3b0b7fd8e1
Merged revisions 65654 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
48660
diff
changeset
|
774 PyBuffer_Release(&view); \ |
|
42849
bbc44326b53d
Use the new buffer api for input data.
Gregory P. Smith <greg@mad-scientist.com>
parents:
42739
diff
changeset
|
775 return ret_obj; \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
776 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
777 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
778 /* a PyMethodDef structure for the constructor */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
779 #define CONSTRUCTOR_METH_DEF(NAME) \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
780 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
781 PyDoc_STR("Returns a " #NAME \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
782 " hash object; optionally initialized with a string") \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
783 } |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
784 |
|
81898
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
785 /* used in the init function to setup a constructor: initialize OpenSSL |
|
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
786 constructor constants if they haven't been initialized already. */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
787 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \ |
|
81898
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
788 if (CONST_ ## NAME ## _name_obj == NULL) { \ |
|
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
789 CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \ |
|
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
790 if (EVP_get_digestbyname(#NAME)) { \ |
|
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
791 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \ |
|
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
792 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \ |
|
b6792067aafa
In the _hashlib module, only initialize the static data for OpenSSL's
Gregory P. Smith <greg@krypto.org>
parents:
68257
diff
changeset
|
793 } \ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
794 } \ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
795 } while (0); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
796 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
797 GEN_CONSTRUCTOR(md5) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
798 GEN_CONSTRUCTOR(sha1) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
799 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
800 GEN_CONSTRUCTOR(sha224) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
801 GEN_CONSTRUCTOR(sha256) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
802 GEN_CONSTRUCTOR(sha384) |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
803 GEN_CONSTRUCTOR(sha512) |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
804 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
805 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
806 /* List of functions exported by this module */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
807 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
808 static struct PyMethodDef EVP_functions[] = { |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
809 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__}, |
|
86264
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
810 #ifdef PY_PBKDF2_HMAC |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
811 {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
812 pbkdf2_hmac__doc__}, |
|
5fd56d6d3fce
Issue #18582: Add 'pbkdf2_hmac' to the hashlib module.
Christian Heimes <christian@cheimes.de>
parents:
81900
diff
changeset
|
813 #endif |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
814 CONSTRUCTOR_METH_DEF(md5), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
815 CONSTRUCTOR_METH_DEF(sha1), |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
816 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
817 CONSTRUCTOR_METH_DEF(sha224), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
818 CONSTRUCTOR_METH_DEF(sha256), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
819 CONSTRUCTOR_METH_DEF(sha384), |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
820 CONSTRUCTOR_METH_DEF(sha512), |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
821 #endif |
|
63924
9108e7dcd7d8
Issue #2443: Added a new macro, Py_VA_COPY, which is equivalent to C99
Alexander Belopolsky <alexander.belopolsky@gmail.com>
parents:
59538
diff
changeset
|
822 {NULL, NULL} /* Sentinel */ |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
823 }; |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
824 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
825 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
826 /* Initialize this module. */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
827 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
828 |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
829 static struct PyModuleDef _hashlibmodule = { |
|
50636
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
830 PyModuleDef_HEAD_INIT, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
831 "_hashlib", |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
832 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
833 -1, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
834 EVP_functions, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
835 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
836 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
837 NULL, |
|
f9f5d9047a05
Issue #4751: hashlib now releases the GIL when hashing large buffers
Antoine Pitrou <solipsis@pitrou.net>
parents:
49292
diff
changeset
|
838 NULL |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
839 }; |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
840 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
841 PyMODINIT_FUNC |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
842 PyInit__hashlib(void) |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
843 { |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
844 PyObject *m, *openssl_md_meth_names; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
845 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
846 OpenSSL_add_all_digests(); |
|
86551
1581d69453d6
Load SSL's error strings in hashlib.
Christian Heimes <christian@cheimes.de>
parents:
86544
diff
changeset
|
847 ERR_load_crypto_strings(); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
848 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
849 /* TODO build EVP_functions openssl_* entries dynamically based |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
850 * on what hashes are supported rather than listing many |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
851 * but having some be unsupported. Only init appropriate |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
852 * constants. */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
853 |
|
44351
86a2679dca46
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT.
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
854 Py_TYPE(&EVPtype) = &PyType_Type; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
855 if (PyType_Ready(&EVPtype) < 0) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
856 return NULL; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
857 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
858 m = PyModule_Create(&_hashlibmodule); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
859 if (m == NULL) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
860 return NULL; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
861 |
|
64488
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
862 openssl_md_meth_names = generate_hash_name_list(); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
863 if (openssl_md_meth_names == NULL) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
864 Py_DECREF(m); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
865 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
866 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
867 if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) { |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
868 Py_DECREF(m); |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
869 return NULL; |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
870 } |
|
49441e88ccc1
hashlib has two new constant attributes: algorithms_guaranteed and
Gregory P. Smith <greg@mad-scientist.com>
parents:
63924
diff
changeset
|
871 |
|
86570
27da6e790c41
Issue #18742: Expose the internal hash type object for ABCs.
Christian Heimes <christian@cheimes.de>
parents:
86551
diff
changeset
|
872 Py_INCREF((PyObject *)&EVPtype); |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
873 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
874 |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
875 /* these constants are used by the convenience constructors */ |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
876 INIT_CONSTRUCTOR_CONSTANTS(md5); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
877 INIT_CONSTRUCTOR_CONSTANTS(sha1); |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
878 #ifdef _OPENSSL_SUPPORTS_SHA2 |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
879 INIT_CONSTRUCTOR_CONSTANTS(sha224); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
880 INIT_CONSTRUCTOR_CONSTANTS(sha256); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
881 INIT_CONSTRUCTOR_CONSTANTS(sha384); |
|
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
882 INIT_CONSTRUCTOR_CONSTANTS(sha512); |
|
58664
993f703a61ba
Merged revisions 77484,77487,77561,77570,77593,77603,77608,77667,77702-77703,77739,77858,77887,77889 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
58218
diff
changeset
|
883 #endif |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
47301
diff
changeset
|
884 return m; |
|
35375
624918e1c1b2
[ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff
changeset
|
885 } |
