annotate Modules/_hashopenssl.c @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 2606d9a00c7b
children 9eabba6ded09 1e7ac14e4938
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 /*
43298
a11b4885b445 email address update
Gregory P. Smith <greg@mad-scientist.com>
parents: 42333
diff changeset
4 * Copyright (C) 2005 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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
19 /* EVP is the preferred interface to hashing in OpenSSL */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
20 #include <openssl/evp.h>
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
21
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
22 #define MUNCH_SIZE INT_MAX
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
23
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
24
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
25 #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
26 #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
27 #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
28
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
29 typedef struct {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
30 PyObject_HEAD
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
31 PyObject *name; /* name of this hash algorithm */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
32 EVP_MD_CTX ctx; /* OpenSSL message digest context */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
33 } EVPobject;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
34
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
35
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
36 static PyTypeObject EVPtype;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
37
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
38
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
39 #define DEFINE_CONSTS_FOR_NEW(Name) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
40 static PyObject *CONST_ ## Name ## _name_obj; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
41 static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
42 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
43
36222
4c8a74a20227 Fix icc warnings: extra semi-colon and signed vs unsigned
Neal Norwitz <nnorwitz@gmail.com>
parents: 35999
diff changeset
44 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
45 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
46 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
47 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
48 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
49 DEFINE_CONSTS_FOR_NEW(sha512)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
50
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
51
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
52 static EVPobject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
53 newEVPobject(PyObject *name)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
54 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
55 EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
56
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
57 /* save the name for .name to return */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
58 if (retval != NULL) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
59 Py_INCREF(name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
60 retval->name = name;
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 return retval;
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 /* Internal methods for a hash object */
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 static void
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
69 EVP_dealloc(PyObject *ptr)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
70 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
71 EVP_MD_CTX_cleanup(&((EVPobject *)ptr)->ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
72 Py_XDECREF(((EVPobject *)ptr)->name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
73 PyObject_Del(ptr);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
74 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
75
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
76
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
77 /* External methods for a hash object */
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 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
80
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
81 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
82 EVP_copy(EVPobject *self, PyObject *unused)
35375
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 EVPobject *newobj;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
85
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
86 if ( (newobj = newEVPobject(self->name))==NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
87 return NULL;
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 EVP_MD_CTX_copy(&newobj->ctx, &self->ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
90 return (PyObject *)newobj;
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 PyDoc_STRVAR(EVP_digest__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
94 "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
95
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
96 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
97 EVP_digest(EVPobject *self, PyObject *unused)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
98 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
99 unsigned char digest[EVP_MAX_MD_SIZE];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
100 EVP_MD_CTX temp_ctx;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
101 PyObject *retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
102 unsigned int digest_size;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
103
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
104 EVP_MD_CTX_copy(&temp_ctx, &self->ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
105 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
106 EVP_DigestFinal(&temp_ctx, digest, NULL);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
107
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
108 retval = PyString_FromStringAndSize((const char *)digest, digest_size);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
109 EVP_MD_CTX_cleanup(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
110 return retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
111 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
112
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
113 PyDoc_STRVAR(EVP_hexdigest__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
114 "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
115
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
116 static PyObject *
38489
3fe3ccdf0155 Make use of METH_O and METH_NOARGS where possible.
Georg Brandl <georg@python.org>
parents: 36801
diff changeset
117 EVP_hexdigest(EVPobject *self, PyObject *unused)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
118 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
119 unsigned char digest[EVP_MAX_MD_SIZE];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
120 EVP_MD_CTX temp_ctx;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
121 PyObject *retval;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
122 char *hex_digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
123 unsigned int i, j, digest_size;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
124
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
125 /* Get the raw (binary) digest value */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
126 EVP_MD_CTX_copy(&temp_ctx, &self->ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
127 digest_size = EVP_MD_CTX_size(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
128 EVP_DigestFinal(&temp_ctx, digest, NULL);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
129
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
130 EVP_MD_CTX_cleanup(&temp_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
131
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
132 /* Create a new string */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
133 /* 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
134 /* (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
135 retval = PyString_FromStringAndSize(NULL, digest_size * 2);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
136 if (!retval)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
137 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
138 hex_digest = PyString_AsString(retval);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
139 if (!hex_digest) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
140 Py_DECREF(retval);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
141 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
142 }
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 /* Make hex version of the digest */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
145 for(i=j=0; i<digest_size; i++) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
146 char c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
147 c = (digest[i] >> 4) & 0xf;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
148 c = (c>9) ? c+'a'-10 : c + '0';
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
149 hex_digest[j++] = c;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
150 c = (digest[i] & 0xf);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
151 c = (c>9) ? c+'a'-10 : c + '0';
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
152 hex_digest[j++] = c;
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 return retval;
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
157 PyDoc_STRVAR(EVP_update__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
158 "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
159
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
160 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
161 EVP_update(EVPobject *self, PyObject *args)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
162 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
163 unsigned char *cp;
36777
1211a68b28dd Make Py_ssize_t-clean.
Thomas Wouters <thomas@python.org>
parents: 36675
diff changeset
164 Py_ssize_t len;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
165
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
166 if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
167 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
168
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
169 if (len > 0 && len <= MUNCH_SIZE) {
36801
515ef7dd16bd Properly fix Py_SAFE_DOWNCAST-triggerd bugs.
Thomas Wouters <thomas@python.org>
parents: 36800
diff changeset
170 EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
515ef7dd16bd Properly fix Py_SAFE_DOWNCAST-triggerd bugs.
Thomas Wouters <thomas@python.org>
parents: 36800
diff changeset
171 unsigned int));
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
172 } else {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
173 Py_ssize_t offset = 0;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
174 while (len) {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
175 unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
176 EVP_DigestUpdate(&self->ctx, cp + offset, process);
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
177 len -= process;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
178 offset += process;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
179 }
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
180 }
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
181 Py_INCREF(Py_None);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
182 return Py_None;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
183 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
184
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
185 static PyMethodDef EVP_methods[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
186 {"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
187 {"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
188 {"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
189 {"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
190 {NULL, NULL} /* sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
191 };
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 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
194 EVP_get_block_size(EVPobject *self, void *closure)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
195 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
196 return PyInt_FromLong(EVP_MD_CTX_block_size(&((EVPobject *)self)->ctx));
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
197 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
198
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
199 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
200 EVP_get_digest_size(EVPobject *self, void *closure)
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 return PyInt_FromLong(EVP_MD_CTX_size(&((EVPobject *)self)->ctx));
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
203 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
204
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
205 static PyMemberDef EVP_members[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
206 {"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
207 {NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
208 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
209
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
210 static PyGetSetDef EVP_getseters[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
211 {"digest_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
212 (getter)EVP_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
213 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
214 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
215 {"block_size",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
216 (getter)EVP_get_block_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
217 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
218 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
219 /* 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
220 * the old sha module also supported 'digestsize'. ugh. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
221 {"digestsize",
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
222 (getter)EVP_get_digest_size, NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
223 NULL,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
224 NULL},
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
225 {NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
226 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
227
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
228
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
229 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
230 EVP_repr(PyObject *self)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
231 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
232 char buf[100];
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
233 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
234 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
235 return PyString_FromString(buf);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
236 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
237
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
238 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
239 static int
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
240 EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
241 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36232
diff changeset
242 static char *kwlist[] = {"name", "string", NULL};
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
243 PyObject *name_obj = NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
244 char *nameStr;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
245 unsigned char *cp = NULL;
36801
515ef7dd16bd Properly fix Py_SAFE_DOWNCAST-triggerd bugs.
Thomas Wouters <thomas@python.org>
parents: 36800
diff changeset
246 Py_ssize_t len = 0;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
247 const EVP_MD *digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
248
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
249 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s#:HASH", kwlist,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
250 &name_obj, &cp, &len)) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
251 return -1;
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 if (!PyArg_Parse(name_obj, "s", &nameStr)) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
255 PyErr_SetString(PyExc_TypeError, "name must be a string");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
256 return -1;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
257 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
258
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
259 digest = EVP_get_digestbyname(nameStr);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
260 if (!digest) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
261 PyErr_SetString(PyExc_ValueError, "unknown hash function");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
262 return -1;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
263 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
264 EVP_DigestInit(&self->ctx, digest);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
265
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
266 self->name = name_obj;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
267 Py_INCREF(self->name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
268
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
269 if (cp && len) {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
270 if (len > 0 && len <= MUNCH_SIZE) {
36801
515ef7dd16bd Properly fix Py_SAFE_DOWNCAST-triggerd bugs.
Thomas Wouters <thomas@python.org>
parents: 36800
diff changeset
271 EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
515ef7dd16bd Properly fix Py_SAFE_DOWNCAST-triggerd bugs.
Thomas Wouters <thomas@python.org>
parents: 36800
diff changeset
272 unsigned int));
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
273 } else {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
274 Py_ssize_t offset = 0;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
275 while (len) {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
276 unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
277 EVP_DigestUpdate(&self->ctx, cp + offset, process);
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
278 len -= process;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
279 offset += process;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
280 }
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
281 }
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
282 }
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
283
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
284 return 0;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
285 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
286 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
287
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
288
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
289 PyDoc_STRVAR(hashtype_doc,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
290 "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
291 string of information.\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
292 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
293 Methods:\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
294 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
295 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
296 digest() -- return the current digest value\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
297 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
298 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
299 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
300 Attributes:\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
301 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
302 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
303 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
304
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
305 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
306 PyVarObject_HEAD_INIT(NULL, 0)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
307 "_hashlib.HASH", /*tp_name*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
308 sizeof(EVPobject), /*tp_basicsize*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
309 0, /*tp_itemsize*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
310 /* methods */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
311 EVP_dealloc, /*tp_dealloc*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
312 0, /*tp_print*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
313 0, /*tp_getattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
314 0, /*tp_setattr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
315 0, /*tp_compare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
316 EVP_repr, /*tp_repr*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
317 0, /*tp_as_number*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
318 0, /*tp_as_sequence*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
319 0, /*tp_as_mapping*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
320 0, /*tp_hash*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
321 0, /*tp_call*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
322 0, /*tp_str*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
323 0, /*tp_getattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
324 0, /*tp_setattro*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
325 0, /*tp_as_buffer*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
326 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
327 hashtype_doc, /*tp_doc*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
328 0, /*tp_traverse*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
329 0, /*tp_clear*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
330 0, /*tp_richcompare*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
331 0, /*tp_weaklistoffset*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
332 0, /*tp_iter*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
333 0, /*tp_iternext*/
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
334 EVP_methods, /* tp_methods */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
335 EVP_members, /* tp_members */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
336 EVP_getseters, /* tp_getset */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
337 #if 1
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
338 0, /* tp_base */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
339 0, /* tp_dict */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
340 0, /* tp_descr_get */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
341 0, /* tp_descr_set */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
342 0, /* tp_dictoffset */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
343 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
344 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
345 (initproc)EVP_tp_init, /* tp_init */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
346 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
347 };
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
348
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
349 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
350 EVPnew(PyObject *name_obj,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
351 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
352 const unsigned char *cp, Py_ssize_t len)
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
353 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
354 EVPobject *self;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
355
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
356 if (!digest && !initial_ctx) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
357 PyErr_SetString(PyExc_ValueError, "unsupported hash type");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
358 return NULL;
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
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
361 if ((self = newEVPobject(name_obj)) == NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
362 return NULL;
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 if (initial_ctx) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
365 EVP_MD_CTX_copy(&self->ctx, initial_ctx);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
366 } else {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
367 EVP_DigestInit(&self->ctx, digest);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
368 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
369
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
370 if (cp && len) {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
371 if (len > 0 && len <= MUNCH_SIZE) {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
372 EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
373 unsigned int));
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
374 } else {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
375 Py_ssize_t offset = 0;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
376 while (len) {
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
377 unsigned int process = len > MUNCH_SIZE ? MUNCH_SIZE : len;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
378 EVP_DigestUpdate(&self->ctx, cp + offset, process);
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
379 len -= process;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
380 offset += process;
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
381 }
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
382 }
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
383 }
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
384
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
385 return (PyObject *)self;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
386 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
387
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
388
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
389 /* The module-level function: new() */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
390
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
391 PyDoc_STRVAR(EVP_new__doc__,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
392 "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
393 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
394 automatically hashed.\n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
395 \n\
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
396 The MD5 and SHA1 algorithms are always supported.\n");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
397
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
398 static PyObject *
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
399 EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
400 {
36675
dba6494735d0 Revert backwards-incompatible const changes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36232
diff changeset
401 static char *kwlist[] = {"name", "string", NULL};
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
402 PyObject *name_obj = NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
403 char *name;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
404 const EVP_MD *digest;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
405 unsigned char *cp = NULL;
36801
515ef7dd16bd Properly fix Py_SAFE_DOWNCAST-triggerd bugs.
Thomas Wouters <thomas@python.org>
parents: 36800
diff changeset
406 Py_ssize_t len = 0;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
407
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
408 if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s#:new", kwlist,
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
409 &name_obj, &cp, &len)) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
410 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
411 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
412
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
413 if (!PyArg_Parse(name_obj, "s", &name)) {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
414 PyErr_SetString(PyExc_TypeError, "name must be a string");
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
415 return NULL;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
416 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
417
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
418 digest = EVP_get_digestbyname(name);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
419
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
420 return EVPnew(name_obj, digest, NULL, cp, len);
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
421 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
422
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 * This macro generates constructor function definitions for specific
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
425 * hash algorithms. These constructors are much faster than calling
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
426 * 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
427 * 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
428 * 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
429 */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
430 #define GEN_CONSTRUCTOR(NAME) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
431 static PyObject * \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
432 EVP_new_ ## NAME (PyObject *self, PyObject *args) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
433 { \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
434 unsigned char *cp = NULL; \
36801
515ef7dd16bd Properly fix Py_SAFE_DOWNCAST-triggerd bugs.
Thomas Wouters <thomas@python.org>
parents: 36800
diff changeset
435 Py_ssize_t len = 0; \
35375
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 (!PyArg_ParseTuple(args, "|s#:" #NAME , &cp, &len)) { \
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 \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
441 return EVPnew( \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
442 CONST_ ## NAME ## _name_obj, \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
443 NULL, \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
444 CONST_new_ ## NAME ## _ctx_p, \
49217
2606d9a00c7b fix possible integer overflows in _hashopenssl #3886
Benjamin Peterson <benjamin@python.org>
parents: 47527
diff changeset
445 cp, len); \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
446 }
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
447
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
448 /* a PyMethodDef structure for the constructor */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
449 #define CONSTRUCTOR_METH_DEF(NAME) \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
450 {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
451 PyDoc_STR("Returns a " #NAME \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
452 " hash object; optionally initialized with a string") \
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 /* used in the init function to setup a constructor */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
456 #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
457 CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
458 if (EVP_get_digestbyname(#NAME)) { \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
459 CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
460 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
461 } \
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
462 } while (0);
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 GEN_CONSTRUCTOR(md5)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
465 GEN_CONSTRUCTOR(sha1)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
466 GEN_CONSTRUCTOR(sha224)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
467 GEN_CONSTRUCTOR(sha256)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
468 GEN_CONSTRUCTOR(sha384)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
469 GEN_CONSTRUCTOR(sha512)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
470
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
471 /* List of functions exported by this module */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
472
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
473 static struct PyMethodDef EVP_functions[] = {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
474 {"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
475 CONSTRUCTOR_METH_DEF(md5),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
476 CONSTRUCTOR_METH_DEF(sha1),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
477 CONSTRUCTOR_METH_DEF(sha224),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
478 CONSTRUCTOR_METH_DEF(sha256),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
479 CONSTRUCTOR_METH_DEF(sha384),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
480 CONSTRUCTOR_METH_DEF(sha512),
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
481 {NULL, NULL} /* Sentinel */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
482 };
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 /* Initialize this module. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
486
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
487 PyMODINIT_FUNC
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
488 init_hashlib(void)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
489 {
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
490 PyObject *m;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
491
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
492 OpenSSL_add_all_digests();
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 /* TODO build EVP_functions openssl_* entries dynamically based
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
495 * on what hashes are supported rather than listing many
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
496 * but having some be unsupported. Only init appropriate
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
497 * constants. */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
498
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
499 Py_TYPE(&EVPtype) = &PyType_Type;
35375
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
500 if (PyType_Ready(&EVPtype) < 0)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
501 return;
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 m = Py_InitModule("_hashlib", EVP_functions);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
504 if (m == NULL)
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
505 return;
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
506
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
507 #if HASH_OBJ_CONSTRUCTOR
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
508 Py_INCREF(&EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
509 PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
510 #endif
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
511
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
512 /* these constants are used by the convenience constructors */
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
513 INIT_CONSTRUCTOR_CONSTANTS(md5);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
514 INIT_CONSTRUCTOR_CONSTANTS(sha1);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
515 INIT_CONSTRUCTOR_CONSTANTS(sha224);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
516 INIT_CONSTRUCTOR_CONSTANTS(sha256);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
517 INIT_CONSTRUCTOR_CONSTANTS(sha384);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
518 INIT_CONSTRUCTOR_CONSTANTS(sha512);
624918e1c1b2 [ sf.net patch # 1121611 ]
Gregory P. Smith <greg@mad-scientist.com>
parents:
diff changeset
519 }