annotate Modules/_hashopenssl.c @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 66f75008c3a8
children adb921f04b87
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 /*
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
4 * Copyright (C) 2005-2009 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"
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
18 #include "hashlib.h"
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
20 #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
21 #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
22 #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
23 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
24 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
25 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
26 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
27 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
28 } \
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
29 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
30 #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
31 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
32 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
33 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
34 #else
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
35 #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
36 #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
37 #endif
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
38
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39 /* EVP is the preferred interface to hashing in OpenSSL */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
40 #include <openssl/evp.h>
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
41
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
42 #define MUNCH_SIZE INT_MAX
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
43
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
44 /* 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
45 * 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
46 #define HASHLIB_GIL_MINSIZE 2048
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
47
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
48 #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
49 #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
50 #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
51
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
52
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
53 typedef struct {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
54 PyObject_HEAD
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
55 PyObject *name; /* name of this hash algorithm */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
56 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
57 #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
58 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
59 #endif
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
60 } EVPobject;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
61
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
62
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
63 static PyTypeObject EVPtype;
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 #define DEFINE_CONSTS_FOR_NEW(Name) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
67 static PyObject *CONST_ ## Name ## _name_obj; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
68 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
69 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
70
36222
4c8a74a20227 Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
71 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
72 DEFINE_CONSTS_FOR_NEW(sha1)
4c8a74a20227 Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
73 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
74 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
75 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
76 DEFINE_CONSTS_FOR_NEW(sha512)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
77
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
78
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
79 static EVPobject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
80 newEVPobject(PyObject *name)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
81 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
82 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
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 /* save the name for .name to return */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
85 if (retval != NULL) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
86 Py_INCREF(name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
87 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
88 #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
89 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
90 #endif
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
91 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
92
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
93 return retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
94 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
95
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
96 static void
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
97 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
98 {
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
99 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
100 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
101 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
102 {
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
103 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
104 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
105 else
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
106 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
107 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
108 len -= process;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
109 cp += process;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
110 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
111 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
112
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
113 /* Internal methods for a hash object */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
114
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
115 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
116 EVP_dealloc(EVPobject *self)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
117 {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
118 #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
119 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
120 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
121 #endif
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
122 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
123 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
124 PyObject_Del(self);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
125 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
126
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
127 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
128 {
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
129 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
130 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
131 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
132 }
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
133
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
134 /* External methods for a hash object */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
135
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
136 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
137
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
138
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
139 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
140 EVP_copy(EVPobject *self, PyObject *unused)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
141 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
142 EVPobject *newobj;
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 if ( (newobj = newEVPobject(self->name))==NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
145 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
146
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
147 locked_EVP_MD_CTX_copy(&newobj->ctx, self);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
148 return (PyObject *)newobj;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
149 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
151 PyDoc_STRVAR(EVP_digest__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
152 "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
153
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
154 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
155 EVP_digest(EVPobject *self, PyObject *unused)
35375
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 unsigned char digest[EVP_MAX_MD_SIZE];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
158 EVP_MD_CTX temp_ctx;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
159 PyObject *retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
160 unsigned int digest_size;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
161
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
162 locked_EVP_MD_CTX_copy(&temp_ctx, self);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
163 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
164 EVP_DigestFinal(&temp_ctx, digest, NULL);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
165
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
166 retval = PyString_FromStringAndSize((const char *)digest, digest_size);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
167 EVP_MD_CTX_cleanup(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
168 return retval;
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
171 PyDoc_STRVAR(EVP_hexdigest__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
172 "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
173
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
174 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
175 EVP_hexdigest(EVPobject *self, PyObject *unused)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
176 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
177 unsigned char digest[EVP_MAX_MD_SIZE];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
178 EVP_MD_CTX temp_ctx;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
179 PyObject *retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
180 char *hex_digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
181 unsigned int i, j, digest_size;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
182
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
183 /* 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
184 locked_EVP_MD_CTX_copy(&temp_ctx, self);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
185 digest_size = EVP_MD_CTX_size(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
186 EVP_DigestFinal(&temp_ctx, digest, NULL);
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 EVP_MD_CTX_cleanup(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
189
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
190 /* Create a new string */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
191 /* 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
192 /* (not a problem because we hold the GIL by default) */
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
193 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
194 if (!retval)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
195 return NULL;
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
196 hex_digest = PyString_AsString(retval);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
197 if (!hex_digest) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
198 Py_DECREF(retval);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
199 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
200 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
201
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
202 /* Make hex version of the digest */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
203 for(i=j=0; i<digest_size; i++) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
204 char c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205 c = (digest[i] >> 4) & 0xf;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
206 c = (c>9) ? c+'a'-10 : c + '0';
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
207 hex_digest[j++] = c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
208 c = (digest[i] & 0xf);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
209 c = (c>9) ? c+'a'-10 : c + '0';
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
210 hex_digest[j++] = c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
211 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
212 return retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
213 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
214
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
215 PyDoc_STRVAR(EVP_update__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
216 "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
217
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
218 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
219 EVP_update(EVPobject *self, PyObject *args)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
220 {
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
221 PyObject *obj;
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
222 Py_buffer view;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
223
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
224 if (!PyArg_ParseTuple(args, "O:update", &obj))
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
225 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
226
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
227 GET_BUFFER_VIEW_OR_ERROUT(obj, &view, NULL);
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
228
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
229 #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
230 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
231 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
232 /* fail? lock = NULL and we fail over to non-threaded code. */
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
233 }
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
234
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
235 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
236 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
237 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
238 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
239 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
240 Py_END_ALLOW_THREADS
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
241 } else {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
242 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
243 }
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
244 #else
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
245 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
246 #endif
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
247
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
248 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
249
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
250 Py_INCREF(Py_None);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
251 return Py_None;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
252 }
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 static PyMethodDef EVP_methods[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
255 {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
256 {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
257 {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
258 {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
259 {NULL, NULL} /* sentinel */
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
262 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
263 EVP_get_block_size(EVPobject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
264 {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
265 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
266 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
267 return PyLong_FromLong(block_size);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
268 }
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 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
271 EVP_get_digest_size(EVPobject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
272 {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
273 long size;
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
274 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
275 return PyLong_FromLong(size);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
276 }
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 static PyMemberDef EVP_members[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
279 {"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
280 {NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
281 };
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 static PyGetSetDef EVP_getseters[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
284 {"digest_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
285 (getter)EVP_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286 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 {"block_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
289 (getter)EVP_get_block_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
290 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 /* 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
293 * the old sha module also supported 'digestsize'. ugh. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
294 {"digestsize",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
295 (getter)EVP_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
296 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} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
299 };
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 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
303 EVP_repr(PyObject *self)
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 char buf[100];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
306 PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
307 PyString_AsString(((EVPobject *)self)->name), self);
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
308 return PyString_FromString(buf);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
309 }
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 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
312 static int
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
313 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
314 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36232
diff changeset
315 static char *kwlist[] = {"name", "string", NULL};
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
316 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
317 PyObject *data_obj = NULL;
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
318 Py_buffer view;
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
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
322 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist,
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
323 &name_obj, &data_obj)) {
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
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
327 if (data_obj)
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
328 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, -1);
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
329
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
330 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
331 PyErr_SetString(PyExc_TypeError, "name must be a string");
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
332 if (data_obj)
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
333 PyBuffer_Release(&view);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
334 return -1;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
336
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
337 digest = EVP_get_digestbyname(nameStr);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
338 if (!digest) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
339 PyErr_SetString(PyExc_ValueError, "unknown hash function");
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
340 if (data_obj)
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
341 PyBuffer_Release(&view);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
342 return -1;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
343 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
344 EVP_DigestInit(&self->ctx, digest);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
345
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
346 self->name = name_obj;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
347 Py_INCREF(self->name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348
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
349 if (data_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
350 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
351 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
352 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
353 Py_END_ALLOW_THREADS
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
354 } else {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
355 EVP_hash(self, view.buf, view.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
356 }
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
357 PyBuffer_Release(&view);
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
358 }
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
359
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
360 return 0;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
361 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
362 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
363
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
364
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
365 PyDoc_STRVAR(hashtype_doc,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
366 "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
367 string of information.\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
368 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
369 Methods:\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 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
372 digest() -- return the current digest value\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
373 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
374 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
375 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
376 Attributes:\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
377 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
378 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
379 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
380
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
381 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
382 PyVarObject_HEAD_INIT(NULL, 0)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
383 "_hashlib.HASH", /*tp_name*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
384 sizeof(EVPobject), /*tp_basicsize*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
385 0, /*tp_itemsize*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
386 /* methods */
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
387 (destructor)EVP_dealloc, /*tp_dealloc*/
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
388 0, /*tp_print*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
389 0, /*tp_getattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
390 0, /*tp_setattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
391 0, /*tp_compare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
392 EVP_repr, /*tp_repr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
393 0, /*tp_as_number*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
394 0, /*tp_as_sequence*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
395 0, /*tp_as_mapping*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396 0, /*tp_hash*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
397 0, /*tp_call*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
398 0, /*tp_str*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
399 0, /*tp_getattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
400 0, /*tp_setattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
401 0, /*tp_as_buffer*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
402 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
403 hashtype_doc, /*tp_doc*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
404 0, /*tp_traverse*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
405 0, /*tp_clear*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
406 0, /*tp_richcompare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
407 0, /*tp_weaklistoffset*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
408 0, /*tp_iter*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
409 0, /*tp_iternext*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
410 EVP_methods, /* tp_methods */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
411 EVP_members, /* tp_members */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
412 EVP_getseters, /* tp_getset */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
413 #if 1
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
414 0, /* tp_base */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
415 0, /* tp_dict */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
416 0, /* tp_descr_get */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
417 0, /* tp_descr_set */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
418 0, /* tp_dictoffset */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
419 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
420 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
421 (initproc)EVP_tp_init, /* tp_init */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
422 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
423 };
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 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
426 EVPnew(PyObject *name_obj,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
427 const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
428 const unsigned char *cp, Py_ssize_t len)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
429 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
430 EVPobject *self;
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 (!digest && !initial_ctx) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
433 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
434 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
435 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
436
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
437 if ((self = newEVPobject(name_obj)) == NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
438 return NULL;
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 if (initial_ctx) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
441 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
442 } else {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
443 EVP_DigestInit(&self->ctx, digest);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
444 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
445
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
446 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
447 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
448 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
449 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
450 Py_END_ALLOW_THREADS
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
451 } else {
53726
e4ff4273493b Issue #4751: For hashlib algorithms provided by OpenSSL, the Python
Gregory P. Smith <greg@mad-scientist.com>
parents: 51580
diff changeset
452 EVP_hash(self, cp, len);
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
453 }
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
454 }
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
455
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
456 return (PyObject *)self;
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
459
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
460 /* The module-level function: new() */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
461
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
462 PyDoc_STRVAR(EVP_new__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
463 "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
464 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
465 automatically hashed.\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
466 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
467 The MD5 and SHA1 algorithms are always supported.\n");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
468
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
469 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
470 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
471 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36232
diff changeset
472 static char *kwlist[] = {"name", "string", NULL};
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
473 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
474 PyObject *data_obj = NULL;
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
475 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
476 PyObject *ret_obj;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
477 char *name;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
478 const EVP_MD *digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
479
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
480 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist,
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
481 &name_obj, &data_obj)) {
35375
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 if (!PyArg_Parse(name_obj, "s", &name)) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
486 PyErr_SetString(PyExc_TypeError, "name must be a string");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
487 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
488 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
489
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 if (data_obj)
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 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL);
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
492
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
493 digest = EVP_get_digestbyname(name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
494
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
495 ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf,
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
496 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int));
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
497
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
498 if (data_obj)
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
499 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
500 return ret_obj;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
501 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
502
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
503 /*
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
504 * This macro generates constructor function definitions for specific
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
505 * hash algorithms. These constructors are much faster than calling
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
506 * 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
507 * 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
508 * 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
509 */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
510 #define GEN_CONSTRUCTOR(NAME) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
511 static PyObject * \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
512 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
513 { \
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
514 PyObject *data_obj = NULL; \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
515 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
516 PyObject *ret_obj; \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
517 \
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 if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
519 return NULL; \
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 \
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
522 if (data_obj) \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
523 GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view, NULL); \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
524 \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
525 ret_obj = EVPnew( \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
526 CONST_ ## NAME ## _name_obj, \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
527 NULL, \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
528 CONST_new_ ## NAME ## _ctx_p, \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
529 (unsigned char*)view.buf, \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
530 Py_SAFE_DOWNCAST(view.len, Py_ssize_t, unsigned int)); \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
531 \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
532 if (data_obj) \
1e7ac14e4938 - Issue #3745: Fix hashlib to always reject unicode and non buffer-api
Gregory P. Smith <greg@mad-scientist.com>
parents: 49217
diff changeset
533 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
534 return ret_obj; \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
535 }
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 /* a PyMethodDef structure for the constructor */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
538 #define CONSTRUCTOR_METH_DEF(NAME) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
539 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
540 PyDoc_STR("Returns a " #NAME \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
541 " hash object; optionally initialized with a string") \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
542 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
543
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
544 /* used in the init function to setup a constructor */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
545 #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
546 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
547 if (EVP_get_digestbyname(#NAME)) { \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
548 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
549 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
550 } \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
551 } while (0);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
552
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
553 GEN_CONSTRUCTOR(md5)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
554 GEN_CONSTRUCTOR(sha1)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
555 GEN_CONSTRUCTOR(sha224)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
556 GEN_CONSTRUCTOR(sha256)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
557 GEN_CONSTRUCTOR(sha384)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
558 GEN_CONSTRUCTOR(sha512)
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 /* List of functions exported by this module */
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 static struct PyMethodDef EVP_functions[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
563 {"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
564 CONSTRUCTOR_METH_DEF(md5),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
565 CONSTRUCTOR_METH_DEF(sha1),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
566 CONSTRUCTOR_METH_DEF(sha224),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
567 CONSTRUCTOR_METH_DEF(sha256),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
568 CONSTRUCTOR_METH_DEF(sha384),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
569 CONSTRUCTOR_METH_DEF(sha512),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
570 {NULL, NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
571 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
572
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
573
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
574 /* Initialize this module. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
575
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
576 PyMODINIT_FUNC
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
577 init_hashlib(void)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
578 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
579 PyObject *m;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
580
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
581 OpenSSL_add_all_digests();
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
582
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
583 /* TODO build EVP_functions openssl_* entries dynamically based
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
584 * on what hashes are supported rather than listing many
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
585 * but having some be unsupported. Only init appropriate
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
586 * constants. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
587
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
588 Py_TYPE(&EVPtype) = &PyType_Type;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
589 if (PyType_Ready(&EVPtype) < 0)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
590 return;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
591
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
592 m = Py_InitModule("_hashlib", EVP_functions);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
593 if (m == NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
594 return;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
595
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
596 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
597 Py_INCREF(&EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
598 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
599 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
600
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
601 /* these constants are used by the convenience constructors */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
602 INIT_CONSTRUCTOR_CONSTANTS(md5);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
603 INIT_CONSTRUCTOR_CONSTANTS(sha1);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
604 INIT_CONSTRUCTOR_CONSTANTS(sha224);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
605 INIT_CONSTRUCTOR_CONSTANTS(sha256);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
606 INIT_CONSTRUCTOR_CONSTANTS(sha384);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
607 INIT_CONSTRUCTOR_CONSTANTS(sha512);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
608 }