annotate Modules/_hashopenssl.c @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 49dee01d72f9
children 09fc7f466bd4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 /*
59834
9eabba6ded09 Merged revisions 77120,77151,77155,77209,77229,77256,77317,77331,77333,77359-77360,77382,77561,77570 via svnmerge from
Georg Brandl <georg@python.org>
parents: 49217
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"
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
18
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
19 #ifdef WITH_THREAD
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
20 #include "pythread.h"
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
21 #define ENTER_HASHLIB(obj) \
53728
66f75008c3a8 cleanup applied patch to match style that is already in py3k branch.
Gregory P. Smith <greg@mad-scientist.com>
parents: 53726
diff changeset
22 if ((obj)->lock) { \
66f75008c3a8 cleanup applied patch to match style that is already in py3k branch.
Gregory P. Smith <greg@mad-scientist.com>
parents: 53726
diff changeset
23 if (!PyThread_acquire_lock((obj)->lock, 0)) { \
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
24 Py_BEGIN_ALLOW_THREADS \
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
25 PyThread_acquire_lock((obj)->lock, 1); \
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
26 Py_END_ALLOW_THREADS \
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
27 } \
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
28 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
29 #define LEAVE_HASHLIB(obj) \
53728
66f75008c3a8 cleanup applied patch to match style that is already in py3k branch.
Gregory P. Smith <greg@mad-scientist.com>
parents: 53726
diff changeset
30 if ((obj)->lock) { \
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
31 PyThread_release_lock((obj)->lock); \
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
32 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
33 #else
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
34 #define ENTER_HASHLIB(obj)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
35 #define LEAVE_HASHLIB(obj)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
36 #endif
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
37
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
38 /* EVP is the preferred interface to hashing in OpenSSL */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39 #include <openssl/evp.h>
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
40
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
41 #define MUNCH_SIZE INT_MAX
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
42
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
43 /* TODO(gps): We should probably make this a module or EVPobject attribute
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
44 * to allow the user to optimize based on the platform they're using. */
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
45 #define HASHLIB_GIL_MINSIZE 2048
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
46
36232
a77e5b046a50 Fix icc warnings: HASH_OBJ_CONSTRUCTOR was not defined and using #if HASH_OBJ_CONSTRUCTOR
Neal Norwitz <nnorwitz@gmail.com>
parents: 36222
diff changeset
47 #ifndef HASH_OBJ_CONSTRUCTOR
a77e5b046a50 Fix icc warnings: HASH_OBJ_CONSTRUCTOR was not defined and using #if HASH_OBJ_CONSTRUCTOR
Neal Norwitz <nnorwitz@gmail.com>
parents: 36222
diff changeset
48 #define HASH_OBJ_CONSTRUCTOR 0
a77e5b046a50 Fix icc warnings: HASH_OBJ_CONSTRUCTOR was not defined and using #if HASH_OBJ_CONSTRUCTOR
Neal Norwitz <nnorwitz@gmail.com>
parents: 36222
diff changeset
49 #endif
a77e5b046a50 Fix icc warnings: HASH_OBJ_CONSTRUCTOR was not defined and using #if HASH_OBJ_CONSTRUCTOR
Neal Norwitz <nnorwitz@gmail.com>
parents: 36222
diff changeset
50
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
51 /* Minimum OpenSSL version needed to support sha224 and higher. */
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
52 #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000)
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
53 #define _OPENSSL_SUPPORTS_SHA2
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
54 #endif
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
55
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
56 typedef struct {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
57 PyObject_HEAD
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
58 PyObject *name; /* name of this hash algorithm */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
59 EVP_MD_CTX ctx; /* OpenSSL message digest context */
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
60 #ifdef WITH_THREAD
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
61 PyThread_type_lock lock; /* OpenSSL context lock */
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
62 #endif
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
63 } EVPobject;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
64
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 static PyTypeObject EVPtype;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
67
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
68
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
69 #define DEFINE_CONSTS_FOR_NEW(Name) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
70 static PyObject *CONST_ ## Name ## _name_obj; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
71 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
72 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
73
36222
4c8a74a20227 Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
74 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
75 DEFINE_CONSTS_FOR_NEW(sha1)
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
76 #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
77 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
78 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
79 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
80 DEFINE_CONSTS_FOR_NEW(sha512)
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
81 #endif
35375
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
84 static EVPobject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
85 newEVPobject(PyObject *name)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
86 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
87 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
88
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
89 /* save the name for .name to return */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
90 if (retval != NULL) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
91 Py_INCREF(name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
92 retval->name = name;
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
93 #ifdef WITH_THREAD
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
94 retval->lock = NULL;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
95 #endif
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
96 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
97
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
98 return retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
99 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
100
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
101 static void
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
102 EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
103 {
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
104 unsigned int process;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
105 const unsigned char *cp = (const unsigned char *)vp;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
106 while (0 < len)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
107 {
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
108 if (len > (Py_ssize_t)MUNCH_SIZE)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
109 process = MUNCH_SIZE;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
110 else
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
111 process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
112 EVP_DigestUpdate(&self->ctx, (const void*)cp, process);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
113 len -= process;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
114 cp += process;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
115 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
116 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
117
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
118 /* Internal methods for a hash object */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
119
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
120 static void
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
121 EVP_dealloc(EVPobject *self)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
122 {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
123 #ifdef WITH_THREAD
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
124 if (self->lock != NULL)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
125 PyThread_free_lock(self->lock);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
126 #endif
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
127 EVP_MD_CTX_cleanup(&self->ctx);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
128 Py_XDECREF(self->name);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
129 PyObject_Del(self);
35375
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
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
132 static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
133 {
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
134 ENTER_HASHLIB(self);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
135 EVP_MD_CTX_copy(new_ctx_p, &self->ctx);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
136 LEAVE_HASHLIB(self);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
137 }
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
138
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
139 /* External methods for a hash object */
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 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
142
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
143
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
144 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
145 EVP_copy(EVPobject *self, PyObject *unused)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
146 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
147 EVPobject *newobj;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
148
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
149 if ( (newobj = newEVPobject(self->name))==NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
151
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
152 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
153 return (PyObject *)newobj;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
154 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
155
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
156 PyDoc_STRVAR(EVP_digest__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
157 "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
158
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
159 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
160 EVP_digest(EVPobject *self, PyObject *unused)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
161 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
162 unsigned char digest[EVP_MAX_MD_SIZE];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
163 EVP_MD_CTX temp_ctx;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
164 PyObject *retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
165 unsigned int digest_size;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
166
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
167 locked_EVP_MD_CTX_copy(&temp_ctx, self);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
168 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
169 EVP_DigestFinal(&temp_ctx, digest, NULL);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
170
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
171 retval = PyString_FromStringAndSize((const char *)digest, digest_size);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
172 EVP_MD_CTX_cleanup(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
173 return retval;
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
176 PyDoc_STRVAR(EVP_hexdigest__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
177 "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
178
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
179 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
180 EVP_hexdigest(EVPobject *self, PyObject *unused)
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 unsigned char digest[EVP_MAX_MD_SIZE];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
183 EVP_MD_CTX temp_ctx;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
184 PyObject *retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
185 char *hex_digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
186 unsigned int i, j, digest_size;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
187
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
188 /* Get the raw (binary) digest value */
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
189 locked_EVP_MD_CTX_copy(&temp_ctx, self);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
190 digest_size = EVP_MD_CTX_size(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
191 EVP_DigestFinal(&temp_ctx, digest, NULL);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
192
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
193 EVP_MD_CTX_cleanup(&temp_ctx);
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 /* Create a new string */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
196 /* NOTE: not thread safe! modifying an already created string object */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
197 /* (not a problem because we hold the GIL by default) */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
198 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
199 if (!retval)
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
200 return NULL;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
201 hex_digest = PyString_AsString(retval);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
202 if (!hex_digest) {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
203 Py_DECREF(retval);
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
204 return NULL;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
206
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
207 /* Make hex version of the digest */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
208 for(i=j=0; i<digest_size; i++) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
209 char c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
210 c = (digest[i] >> 4) & 0xf;
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
211 c = (c>9) ? c+'a'-10 : c + '0';
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
212 hex_digest[j++] = c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
213 c = (digest[i] & 0xf);
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
214 c = (c>9) ? c+'a'-10 : c + '0';
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
215 hex_digest[j++] = c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
216 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
217 return retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
218 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
219
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
220 PyDoc_STRVAR(EVP_update__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
221 "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
222
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
223 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
224 EVP_update(EVPobject *self, PyObject *args)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
225 {
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
226 Py_buffer view;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
227
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
228 if (!PyArg_ParseTuple(args, "s*:update", &view))
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
229 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
230
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
231 #ifdef WITH_THREAD
53728
66f75008c3a8 cleanup applied patch to match style that is already in py3k branch.
Gregory P. Smith <greg@mad-scientist.com>
parents: 53726
diff changeset
232 if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
233 self->lock = PyThread_allocate_lock();
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
234 /* fail? lock = NULL and we fail over to non-threaded code. */
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
235 }
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
236
53728
66f75008c3a8 cleanup applied patch to match style that is already in py3k branch.
Gregory P. Smith <greg@mad-scientist.com>
parents: 53726
diff changeset
237 if (self->lock != NULL) {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
238 Py_BEGIN_ALLOW_THREADS
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
239 PyThread_acquire_lock(self->lock, 1);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
240 EVP_hash(self, view.buf, view.len);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
241 PyThread_release_lock(self->lock);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
242 Py_END_ALLOW_THREADS
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
243 }
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
244 else
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
245 #endif
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
246 {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
247 EVP_hash(self, view.buf, view.len);
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
248 }
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
249
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
250 PyBuffer_Release(&view);
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
251
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
252 Py_RETURN_NONE;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
253 }
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 static PyMethodDef EVP_methods[] = {
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
256 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
257 {"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
258 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
259 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
260 {NULL, NULL} /* sentinel */
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
261 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
262
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
263 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
264 EVP_get_block_size(EVPobject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
265 {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
266 long block_size;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
267 block_size = EVP_MD_CTX_block_size(&self->ctx);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
268 return PyLong_FromLong(block_size);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
269 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
270
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
271 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
272 EVP_get_digest_size(EVPobject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
273 {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
274 long size;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
275 size = EVP_MD_CTX_size(&self->ctx);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
276 return PyLong_FromLong(size);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
277 }
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 static PyMemberDef EVP_members[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
280 {"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
281 {NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
282 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
283
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
284 static PyGetSetDef EVP_getseters[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
285 {"digest_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286 (getter)EVP_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
287 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
288 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
289 {"block_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
290 (getter)EVP_get_block_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
291 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
292 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
293 /* the old md5 and sha modules support 'digest_size' as in PEP 247.
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
294 * the old sha module also supported 'digestsize'. ugh. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
295 {"digestsize",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
296 (getter)EVP_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
297 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
298 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
299 {NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
300 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
301
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
302
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
303 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
304 EVP_repr(PyObject *self)
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 char buf[100];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
307 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
308 PyString_AsString(((EVPobject *)self)->name), self);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
309 return PyString_FromString(buf);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
310 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
311
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
312 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
313 static int
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
314 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
315 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36232
diff changeset
316 static char *kwlist[] = {"name", "string", NULL};
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
317 PyObject *name_obj = NULL;
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
318 Py_buffer view = { 0 };
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
319 char *nameStr;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
320 const EVP_MD *digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
321
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
322 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s*:HASH", kwlist,
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
323 &name_obj, &view)) {
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
324 return -1;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
325 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
326
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
327 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
328 PyErr_SetString(PyExc_TypeError, "name must be a string");
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
329 PyBuffer_Release(&view);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
330 return -1;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
331 }
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 digest = EVP_get_digestbyname(nameStr);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
334 if (!digest) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335 PyErr_SetString(PyExc_ValueError, "unknown hash function");
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
336 PyBuffer_Release(&view);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
337 return -1;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
338 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
339 EVP_DigestInit(&self->ctx, digest);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
340
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
341 self->name = name_obj;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
342 Py_INCREF(self->name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
343
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
344 if (view.obj) {
53728
66f75008c3a8 cleanup applied patch to match style that is already in py3k branch.
Gregory P. Smith <greg@mad-scientist.com>
parents: 53726
diff changeset
345 if (view.len >= HASHLIB_GIL_MINSIZE) {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
346 Py_BEGIN_ALLOW_THREADS
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
347 EVP_hash(self, view.buf, view.len);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
348 Py_END_ALLOW_THREADS
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
349 } else {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
350 EVP_hash(self, view.buf, view.len);
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
351 }
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
352 PyBuffer_Release(&view);
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
353 }
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
354
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
355 return 0;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
356 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
357 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
358
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
359
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
360 PyDoc_STRVAR(hashtype_doc,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
361 "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
362 string of information.\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
363 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
364 Methods:\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
365 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
366 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
367 digest() -- return the current digest value\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
368 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
369 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
370 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
371 Attributes:\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
372 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
373 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
374 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
375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
376 static PyTypeObject EVPtype = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 38489
diff changeset
377 PyVarObject_HEAD_INIT(NULL, 0)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
378 "_hashlib.HASH", /*tp_name*/
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
379 sizeof(EVPobject), /*tp_basicsize*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
380 0, /*tp_itemsize*/
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
381 /* methods */
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
382 (destructor)EVP_dealloc, /*tp_dealloc*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
383 0, /*tp_print*/
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
384 0, /*tp_getattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
385 0, /*tp_setattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
386 0, /*tp_compare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
387 EVP_repr, /*tp_repr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
388 0, /*tp_as_number*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
389 0, /*tp_as_sequence*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
390 0, /*tp_as_mapping*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
391 0, /*tp_hash*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
392 0, /*tp_call*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
393 0, /*tp_str*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
394 0, /*tp_getattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
395 0, /*tp_setattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396 0, /*tp_as_buffer*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
397 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
398 hashtype_doc, /*tp_doc*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
399 0, /*tp_traverse*/
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
400 0, /*tp_clear*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
401 0, /*tp_richcompare*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
402 0, /*tp_weaklistoffset*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
403 0, /*tp_iter*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
404 0, /*tp_iternext*/
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
405 EVP_methods, /* tp_methods */
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
406 EVP_members, /* tp_members */
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
407 EVP_getseters, /* tp_getset */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
408 #if 1
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
409 0, /* tp_base */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
410 0, /* tp_dict */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
411 0, /* tp_descr_get */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
412 0, /* tp_descr_set */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
413 0, /* tp_dictoffset */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
414 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
415 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
416 (initproc)EVP_tp_init, /* tp_init */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
417 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
418 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
419
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
420 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
421 EVPnew(PyObject *name_obj,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
422 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
423 const unsigned char *cp, Py_ssize_t len)
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 EVPobject *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 if (!digest && !initial_ctx) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
428 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
429 return NULL;
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
432 if ((self = newEVPobject(name_obj)) == NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
433 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
434
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
435 if (initial_ctx) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
436 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
437 } else {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
438 EVP_DigestInit(&self->ctx, digest);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
439 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
440
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
441 if (cp && len) {
53728
66f75008c3a8 cleanup applied patch to match style that is already in py3k branch.
Gregory P. Smith <greg@mad-scientist.com>
parents: 53726
diff changeset
442 if (len >= HASHLIB_GIL_MINSIZE) {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
443 Py_BEGIN_ALLOW_THREADS
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
444 EVP_hash(self, cp, len);
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
445 Py_END_ALLOW_THREADS
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
446 } else {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
447 EVP_hash(self, cp, len);
49218
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
448 }
58818846363b backport r66496: integer overflow in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 38489
diff changeset
449 }
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
450
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
451 return (PyObject *)self;
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
455 /* The module-level function: new() */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
456
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
457 PyDoc_STRVAR(EVP_new__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
458 "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
459 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
460 automatically hashed.\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
461 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
462 The MD5 and SHA1 algorithms are always supported.\n");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
463
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
464 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
465 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
466 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36232
diff changeset
467 static char *kwlist[] = {"name", "string", NULL};
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
468 PyObject *name_obj = NULL;
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
469 Py_buffer view = { 0 };
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
470 PyObject *ret_obj;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
471 char *name;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
472 const EVP_MD *digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
473
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
474 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s*:new", kwlist,
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
475 &name_obj, &view)) {
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
476 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
477 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
478
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
479 if (!PyArg_Parse(name_obj, "s", &name)) {
77838
49dee01d72f9 Issue #15219: Fix a reference leak when hashlib.new() is called with
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents: 68256
diff changeset
480 PyBuffer_Release(&view);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
481 PyErr_SetString(PyExc_TypeError, "name must be a string");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
482 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
483 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
484
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
485 digest = EVP_get_digestbyname(name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
486
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
487 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
488 view.len);
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
489 PyBuffer_Release(&view);
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
490
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
491 return ret_obj;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
492 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
493
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
494 /*
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
495 * This macro generates constructor function definitions for specific
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
496 * hash algorithms. These constructors are much faster than calling
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
497 * 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
498 * 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
499 * 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
500 */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
501 #define GEN_CONSTRUCTOR(NAME) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
502 static PyObject * \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
503 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
504 { \
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
505 Py_buffer view = { 0 }; \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
506 PyObject *ret_obj; \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
507 \
58101
ff5bc2fa38d6 Issue #3745: Undo the requirement for new buffer API only objects to be passed
Gregory P. Smith <greg@mad-scientist.com>
parents: 58063
diff changeset
508 if (!PyArg_ParseTuple(args, "|s*:" #NAME , &view)) { \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
509 return NULL; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
510 } \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
511 \
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
512 ret_obj = EVPnew( \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
513 CONST_ ## NAME ## _name_obj, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
514 NULL, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
515 CONST_new_ ## NAME ## _ctx_p, \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
516 (unsigned char*)view.buf, view.len); \
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
517 PyBuffer_Release(&view); \
51580
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
518 return ret_obj; \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
519 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
520
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
521 /* a PyMethodDef structure for the constructor */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
522 #define CONSTRUCTOR_METH_DEF(NAME) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
523 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
524 PyDoc_STR("Returns a " #NAME \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
525 " hash object; optionally initialized with a string") \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
526 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
527
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
528 /* used in the init function to setup a constructor */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
529 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
530 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
531 if (EVP_get_digestbyname(#NAME)) { \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
532 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
533 EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
534 } \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
535 } while (0);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
536
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
537 GEN_CONSTRUCTOR(md5)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
538 GEN_CONSTRUCTOR(sha1)
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
539 #ifdef _OPENSSL_SUPPORTS_SHA2
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
540 GEN_CONSTRUCTOR(sha224)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
541 GEN_CONSTRUCTOR(sha256)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
542 GEN_CONSTRUCTOR(sha384)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
543 GEN_CONSTRUCTOR(sha512)
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
544 #endif
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
545
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
546 /* List of functions exported by this module */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
547
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
548 static struct PyMethodDef EVP_functions[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
549 {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
550 CONSTRUCTOR_METH_DEF(md5),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
551 CONSTRUCTOR_METH_DEF(sha1),
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
552 #ifdef _OPENSSL_SUPPORTS_SHA2
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
553 CONSTRUCTOR_METH_DEF(sha224),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
554 CONSTRUCTOR_METH_DEF(sha256),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
555 CONSTRUCTOR_METH_DEF(sha384),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
556 CONSTRUCTOR_METH_DEF(sha512),
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
557 #endif
61463
fd25030223b0 Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents: 58417
diff changeset
558 {NULL, NULL} /* Sentinel */
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
559 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
560
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
561
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
562 /* Initialize this module. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
563
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
564 PyMODINIT_FUNC
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
565 init_hashlib(void)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
566 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
567 PyObject *m;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
568
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
569 OpenSSL_add_all_digests();
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
570
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
571 /* TODO build EVP_functions openssl_* entries dynamically based
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
572 * on what hashes are supported rather than listing many
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
573 * but having some be unsupported. Only init appropriate
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
574 * constants. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
575
44350
26384b8180c7 #1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents: 43298
diff changeset
576 Py_TYPE(&EVPtype) = &PyType_Type;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
577 if (PyType_Ready(&EVPtype) < 0)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
578 return;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
579
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
580 m = Py_InitModule("_hashlib", EVP_functions);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
581 if (m == NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
582 return;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
583
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
584 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
585 Py_INCREF(&EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
586 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
587 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
588
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
589 /* these constants are used by the convenience constructors */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
590 INIT_CONSTRUCTOR_CONSTANTS(md5);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
591 INIT_CONSTRUCTOR_CONSTANTS(sha1);
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
592 #ifdef _OPENSSL_SUPPORTS_SHA2
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
593 INIT_CONSTRUCTOR_CONSTANTS(sha224);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
594 INIT_CONSTRUCTOR_CONSTANTS(sha256);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
595 INIT_CONSTRUCTOR_CONSTANTS(sha384);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
596 INIT_CONSTRUCTOR_CONSTANTS(sha512);
58417
75bfa5da1beb Do not compile stubs for the sha2 series hashes in the openssl hashlib
Gregory P. Smith <greg@mad-scientist.com>
parents: 58101
diff changeset
597 #endif
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
598 }