Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 1 | /* Module that wraps all OpenSSL hash algorithms */ |
| 2 | |
| 3 | /* |
Benjamin Peterson | 46a9900 | 2010-01-09 18:45:30 +0000 | [diff] [blame] | 4 | * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 5 | * Licensed to PSF under a Contributor Agreement. |
| 6 | * |
| 7 | * Derived from a skeleton of shamodule.c containing work performed by: |
| 8 | * |
| 9 | * Andrew Kuchling (amk@amk.ca) |
| 10 | * Greg Stein (gstein@lyra.org) |
| 11 | * |
| 12 | */ |
| 13 | |
Thomas Wouters | 9bc844e | 2006-03-01 21:50:07 +0000 | [diff] [blame] | 14 | #define PY_SSIZE_T_CLEAN |
| 15 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 16 | #include "Python.h" |
| 17 | #include "structmember.h" |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 18 | #include "hashlib.h" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 19 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 20 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 21 | /* EVP is the preferred interface to hashing in OpenSSL */ |
| 22 | #include <openssl/evp.h> |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 23 | /* We use the object interface to discover what hashes OpenSSL supports. */ |
| 24 | #include <openssl/objects.h> |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 25 | |
| 26 | #define MUNCH_SIZE INT_MAX |
| 27 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 28 | #ifndef HASH_OBJ_CONSTRUCTOR |
| 29 | #define HASH_OBJ_CONSTRUCTOR 0 |
| 30 | #endif |
| 31 | |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 32 | /* Minimum OpenSSL version needed to support sha224 and higher. */ |
| 33 | #if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER >= 0x00908000) |
| 34 | #define _OPENSSL_SUPPORTS_SHA2 |
| 35 | #endif |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 36 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 37 | typedef struct { |
| 38 | PyObject_HEAD |
| 39 | PyObject *name; /* name of this hash algorithm */ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 40 | EVP_MD_CTX ctx; /* OpenSSL message digest context */ |
| 41 | #ifdef WITH_THREAD |
| 42 | PyThread_type_lock lock; /* OpenSSL context lock */ |
| 43 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 44 | } EVPobject; |
| 45 | |
| 46 | |
| 47 | static PyTypeObject EVPtype; |
| 48 | |
| 49 | |
| 50 | #define DEFINE_CONSTS_FOR_NEW(Name) \ |
Gregory P. Smith | aded2e5 | 2013-02-01 17:05:29 -0800 | [diff] [blame] | 51 | static PyObject *CONST_ ## Name ## _name_obj = NULL; \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 52 | static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \ |
| 53 | static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL; |
| 54 | |
Neal Norwitz | f045914 | 2006-01-07 21:20:24 +0000 | [diff] [blame] | 55 | DEFINE_CONSTS_FOR_NEW(md5) |
| 56 | DEFINE_CONSTS_FOR_NEW(sha1) |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 57 | #ifdef _OPENSSL_SUPPORTS_SHA2 |
Neal Norwitz | f045914 | 2006-01-07 21:20:24 +0000 | [diff] [blame] | 58 | DEFINE_CONSTS_FOR_NEW(sha224) |
| 59 | DEFINE_CONSTS_FOR_NEW(sha256) |
| 60 | DEFINE_CONSTS_FOR_NEW(sha384) |
| 61 | DEFINE_CONSTS_FOR_NEW(sha512) |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 62 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 63 | |
| 64 | |
| 65 | static EVPobject * |
| 66 | newEVPobject(PyObject *name) |
| 67 | { |
| 68 | EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype); |
| 69 | |
| 70 | /* save the name for .name to return */ |
| 71 | if (retval != NULL) { |
| 72 | Py_INCREF(name); |
| 73 | retval->name = name; |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 74 | #ifdef WITH_THREAD |
| 75 | retval->lock = NULL; |
| 76 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | return retval; |
| 80 | } |
| 81 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 82 | static void |
| 83 | EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) |
| 84 | { |
| 85 | unsigned int process; |
| 86 | const unsigned char *cp = (const unsigned char *)vp; |
| 87 | while (0 < len) { |
| 88 | if (len > (Py_ssize_t)MUNCH_SIZE) |
| 89 | process = MUNCH_SIZE; |
| 90 | else |
| 91 | process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); |
| 92 | EVP_DigestUpdate(&self->ctx, (const void*)cp, process); |
| 93 | len -= process; |
| 94 | cp += process; |
| 95 | } |
| 96 | } |
| 97 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 98 | /* Internal methods for a hash object */ |
| 99 | |
| 100 | static void |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 101 | EVP_dealloc(EVPobject *self) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 102 | { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 103 | #ifdef WITH_THREAD |
| 104 | if (self->lock != NULL) |
| 105 | PyThread_free_lock(self->lock); |
| 106 | #endif |
| 107 | EVP_MD_CTX_cleanup(&self->ctx); |
| 108 | Py_XDECREF(self->name); |
| 109 | PyObject_Del(self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 112 | static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self) |
| 113 | { |
| 114 | ENTER_HASHLIB(self); |
| 115 | EVP_MD_CTX_copy(new_ctx_p, &self->ctx); |
| 116 | LEAVE_HASHLIB(self); |
| 117 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 118 | |
| 119 | /* External methods for a hash object */ |
| 120 | |
| 121 | PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object."); |
| 122 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 123 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 124 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 125 | EVP_copy(EVPobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 126 | { |
| 127 | EVPobject *newobj; |
| 128 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 129 | if ( (newobj = newEVPobject(self->name))==NULL) |
| 130 | return NULL; |
| 131 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 132 | locked_EVP_MD_CTX_copy(&newobj->ctx, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 133 | return (PyObject *)newobj; |
| 134 | } |
| 135 | |
| 136 | PyDoc_STRVAR(EVP_digest__doc__, |
| 137 | "Return the digest value as a string of binary data."); |
| 138 | |
| 139 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 140 | EVP_digest(EVPobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 141 | { |
| 142 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 143 | EVP_MD_CTX temp_ctx; |
| 144 | PyObject *retval; |
| 145 | unsigned int digest_size; |
| 146 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 147 | locked_EVP_MD_CTX_copy(&temp_ctx, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 148 | digest_size = EVP_MD_CTX_size(&temp_ctx); |
Neal Norwitz | f045914 | 2006-01-07 21:20:24 +0000 | [diff] [blame] | 149 | EVP_DigestFinal(&temp_ctx, digest, NULL); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 150 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 151 | retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 152 | EVP_MD_CTX_cleanup(&temp_ctx); |
| 153 | return retval; |
| 154 | } |
| 155 | |
| 156 | PyDoc_STRVAR(EVP_hexdigest__doc__, |
| 157 | "Return the digest value as a string of hexadecimal digits."); |
| 158 | |
| 159 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 160 | EVP_hexdigest(EVPobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 161 | { |
| 162 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 163 | EVP_MD_CTX temp_ctx; |
| 164 | PyObject *retval; |
| 165 | char *hex_digest; |
| 166 | unsigned int i, j, digest_size; |
| 167 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 168 | /* Get the raw (binary) digest value */ |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 169 | locked_EVP_MD_CTX_copy(&temp_ctx, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 170 | digest_size = EVP_MD_CTX_size(&temp_ctx); |
| 171 | EVP_DigestFinal(&temp_ctx, digest, NULL); |
| 172 | |
| 173 | EVP_MD_CTX_cleanup(&temp_ctx); |
| 174 | |
Guido van Rossum | f895307 | 2007-07-10 13:20:29 +0000 | [diff] [blame] | 175 | /* Allocate a new buffer */ |
| 176 | hex_digest = PyMem_Malloc(digest_size * 2 + 1); |
| 177 | if (!hex_digest) |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 178 | return PyErr_NoMemory(); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 179 | |
| 180 | /* Make hex version of the digest */ |
| 181 | for(i=j=0; i<digest_size; i++) { |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 182 | unsigned char c; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 183 | c = (digest[i] >> 4) & 0xf; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 184 | hex_digest[j++] = Py_hexdigits[c]; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 185 | c = (digest[i] & 0xf); |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 186 | hex_digest[j++] = Py_hexdigits[c]; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 187 | } |
Guido van Rossum | f895307 | 2007-07-10 13:20:29 +0000 | [diff] [blame] | 188 | retval = PyUnicode_FromStringAndSize(hex_digest, digest_size * 2); |
| 189 | PyMem_Free(hex_digest); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 190 | return retval; |
| 191 | } |
| 192 | |
| 193 | PyDoc_STRVAR(EVP_update__doc__, |
| 194 | "Update this hash object's state with the provided string."); |
| 195 | |
| 196 | static PyObject * |
| 197 | EVP_update(EVPobject *self, PyObject *args) |
| 198 | { |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 199 | PyObject *obj; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 200 | Py_buffer view; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 201 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 202 | if (!PyArg_ParseTuple(args, "O:update", &obj)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 203 | return NULL; |
| 204 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 205 | GET_BUFFER_VIEW_OR_ERROUT(obj, &view); |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 206 | |
| 207 | #ifdef WITH_THREAD |
| 208 | if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) { |
| 209 | self->lock = PyThread_allocate_lock(); |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 210 | /* fail? lock = NULL and we fail over to non-threaded code. */ |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 211 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 212 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 213 | if (self->lock != NULL) { |
| 214 | Py_BEGIN_ALLOW_THREADS |
| 215 | PyThread_acquire_lock(self->lock, 1); |
| 216 | EVP_hash(self, view.buf, view.len); |
| 217 | PyThread_release_lock(self->lock); |
| 218 | Py_END_ALLOW_THREADS |
| 219 | } else { |
| 220 | EVP_hash(self, view.buf, view.len); |
| 221 | } |
| 222 | #else |
| 223 | EVP_hash(self, view.buf, view.len); |
| 224 | #endif |
| 225 | |
| 226 | PyBuffer_Release(&view); |
| 227 | Py_RETURN_NONE; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static PyMethodDef EVP_methods[] = { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 231 | {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__}, |
| 232 | {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 233 | {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__}, |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 234 | {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__}, |
| 235 | {NULL, NULL} /* sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 236 | }; |
| 237 | |
| 238 | static PyObject * |
| 239 | EVP_get_block_size(EVPobject *self, void *closure) |
| 240 | { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 241 | long block_size; |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 242 | block_size = EVP_MD_CTX_block_size(&self->ctx); |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 243 | return PyLong_FromLong(block_size); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | static PyObject * |
| 247 | EVP_get_digest_size(EVPobject *self, void *closure) |
| 248 | { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 249 | long size; |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 250 | size = EVP_MD_CTX_size(&self->ctx); |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 251 | return PyLong_FromLong(size); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | static PyMemberDef EVP_members[] = { |
| 255 | {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")}, |
| 256 | {NULL} /* Sentinel */ |
| 257 | }; |
| 258 | |
| 259 | static PyGetSetDef EVP_getseters[] = { |
| 260 | {"digest_size", |
| 261 | (getter)EVP_get_digest_size, NULL, |
| 262 | NULL, |
| 263 | NULL}, |
| 264 | {"block_size", |
| 265 | (getter)EVP_get_block_size, NULL, |
| 266 | NULL, |
| 267 | NULL}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 268 | {NULL} /* Sentinel */ |
| 269 | }; |
| 270 | |
| 271 | |
| 272 | static PyObject * |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 273 | EVP_repr(EVPobject *self) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 274 | { |
Victor Stinner | 3f1af5c | 2010-03-12 17:00:41 +0000 | [diff] [blame] | 275 | return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | #if HASH_OBJ_CONSTRUCTOR |
| 279 | static int |
| 280 | EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds) |
| 281 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 282 | static char *kwlist[] = {"name", "string", NULL}; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 283 | PyObject *name_obj = NULL; |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 284 | PyObject *data_obj = NULL; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 285 | Py_buffer view; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 286 | char *nameStr; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 287 | const EVP_MD *digest; |
| 288 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 289 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist, |
| 290 | &name_obj, &data_obj)) { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 291 | return -1; |
| 292 | } |
| 293 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 294 | if (data_obj) |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 295 | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 296 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 297 | if (!PyArg_Parse(name_obj, "s", &nameStr)) { |
| 298 | PyErr_SetString(PyExc_TypeError, "name must be a string"); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 299 | if (data_obj) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 300 | PyBuffer_Release(&view); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | digest = EVP_get_digestbyname(nameStr); |
| 305 | if (!digest) { |
| 306 | PyErr_SetString(PyExc_ValueError, "unknown hash function"); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 307 | if (data_obj) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 308 | PyBuffer_Release(&view); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 309 | return -1; |
| 310 | } |
| 311 | EVP_DigestInit(&self->ctx, digest); |
| 312 | |
| 313 | self->name = name_obj; |
| 314 | Py_INCREF(self->name); |
| 315 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 316 | if (data_obj) { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 317 | if (view.len >= HASHLIB_GIL_MINSIZE) { |
| 318 | Py_BEGIN_ALLOW_THREADS |
| 319 | EVP_hash(self, view.buf, view.len); |
| 320 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 321 | } else { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 322 | EVP_hash(self, view.buf, view.len); |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 323 | } |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 324 | PyBuffer_Release(&view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 325 | } |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 326 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 327 | return 0; |
| 328 | } |
| 329 | #endif |
| 330 | |
| 331 | |
| 332 | PyDoc_STRVAR(hashtype_doc, |
| 333 | "A hash represents the object used to calculate a checksum of a\n\ |
| 334 | string of information.\n\ |
| 335 | \n\ |
| 336 | Methods:\n\ |
| 337 | \n\ |
| 338 | update() -- updates the current digest with an additional string\n\ |
| 339 | digest() -- return the current digest value\n\ |
| 340 | hexdigest() -- return the current digest as a string of hexadecimal digits\n\ |
| 341 | copy() -- return a copy of the current hash object\n\ |
| 342 | \n\ |
| 343 | Attributes:\n\ |
| 344 | \n\ |
| 345 | name -- the hash algorithm being used by this object\n\ |
| 346 | digest_size -- number of bytes in this hashes output\n"); |
| 347 | |
| 348 | static PyTypeObject EVPtype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 349 | PyVarObject_HEAD_INIT(NULL, 0) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 350 | "_hashlib.HASH", /*tp_name*/ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 351 | sizeof(EVPobject), /*tp_basicsize*/ |
| 352 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 353 | /* methods */ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 354 | (destructor)EVP_dealloc, /*tp_dealloc*/ |
| 355 | 0, /*tp_print*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 356 | 0, /*tp_getattr*/ |
| 357 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 358 | 0, /*tp_reserved*/ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 359 | (reprfunc)EVP_repr, /*tp_repr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 360 | 0, /*tp_as_number*/ |
| 361 | 0, /*tp_as_sequence*/ |
| 362 | 0, /*tp_as_mapping*/ |
| 363 | 0, /*tp_hash*/ |
| 364 | 0, /*tp_call*/ |
| 365 | 0, /*tp_str*/ |
| 366 | 0, /*tp_getattro*/ |
| 367 | 0, /*tp_setattro*/ |
| 368 | 0, /*tp_as_buffer*/ |
| 369 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 370 | hashtype_doc, /*tp_doc*/ |
| 371 | 0, /*tp_traverse*/ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 372 | 0, /*tp_clear*/ |
| 373 | 0, /*tp_richcompare*/ |
| 374 | 0, /*tp_weaklistoffset*/ |
| 375 | 0, /*tp_iter*/ |
| 376 | 0, /*tp_iternext*/ |
| 377 | EVP_methods, /* tp_methods */ |
| 378 | EVP_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 379 | EVP_getseters, /* tp_getset */ |
| 380 | #if 1 |
| 381 | 0, /* tp_base */ |
| 382 | 0, /* tp_dict */ |
| 383 | 0, /* tp_descr_get */ |
| 384 | 0, /* tp_descr_set */ |
| 385 | 0, /* tp_dictoffset */ |
| 386 | #endif |
| 387 | #if HASH_OBJ_CONSTRUCTOR |
| 388 | (initproc)EVP_tp_init, /* tp_init */ |
| 389 | #endif |
| 390 | }; |
| 391 | |
| 392 | static PyObject * |
| 393 | EVPnew(PyObject *name_obj, |
| 394 | const EVP_MD *digest, const EVP_MD_CTX *initial_ctx, |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 395 | const unsigned char *cp, Py_ssize_t len) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 396 | { |
| 397 | EVPobject *self; |
| 398 | |
| 399 | if (!digest && !initial_ctx) { |
| 400 | PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
| 401 | return NULL; |
| 402 | } |
| 403 | |
| 404 | if ((self = newEVPobject(name_obj)) == NULL) |
| 405 | return NULL; |
| 406 | |
| 407 | if (initial_ctx) { |
| 408 | EVP_MD_CTX_copy(&self->ctx, initial_ctx); |
| 409 | } else { |
| 410 | EVP_DigestInit(&self->ctx, digest); |
| 411 | } |
| 412 | |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 413 | if (cp && len) { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 414 | if (len >= HASHLIB_GIL_MINSIZE) { |
| 415 | Py_BEGIN_ALLOW_THREADS |
| 416 | EVP_hash(self, cp, len); |
| 417 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 418 | } else { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 419 | EVP_hash(self, cp, len); |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 420 | } |
| 421 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 422 | |
| 423 | return (PyObject *)self; |
| 424 | } |
| 425 | |
| 426 | |
| 427 | /* The module-level function: new() */ |
| 428 | |
| 429 | PyDoc_STRVAR(EVP_new__doc__, |
| 430 | "Return a new hash object using the named algorithm.\n\ |
| 431 | An optional string argument may be provided and will be\n\ |
| 432 | automatically hashed.\n\ |
| 433 | \n\ |
| 434 | The MD5 and SHA1 algorithms are always supported.\n"); |
| 435 | |
| 436 | static PyObject * |
| 437 | EVP_new(PyObject *self, PyObject *args, PyObject *kwdict) |
| 438 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 439 | static char *kwlist[] = {"name", "string", NULL}; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 440 | PyObject *name_obj = NULL; |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 441 | PyObject *data_obj = NULL; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 442 | Py_buffer view = { 0 }; |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 443 | PyObject *ret_obj; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 444 | char *name; |
| 445 | const EVP_MD *digest; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 446 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 447 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist, |
| 448 | &name_obj, &data_obj)) { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 449 | return NULL; |
| 450 | } |
| 451 | |
| 452 | if (!PyArg_Parse(name_obj, "s", &name)) { |
| 453 | PyErr_SetString(PyExc_TypeError, "name must be a string"); |
| 454 | return NULL; |
| 455 | } |
| 456 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 457 | if (data_obj) |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 458 | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 459 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 460 | digest = EVP_get_digestbyname(name); |
| 461 | |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 462 | ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 463 | |
| 464 | if (data_obj) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 465 | PyBuffer_Release(&view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 466 | return ret_obj; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 467 | } |
| 468 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 469 | |
| 470 | /* State for our callback function so that it can accumulate a result. */ |
| 471 | typedef struct _internal_name_mapper_state { |
| 472 | PyObject *set; |
| 473 | int error; |
| 474 | } _InternalNameMapperState; |
| 475 | |
| 476 | |
| 477 | /* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */ |
| 478 | static void |
| 479 | _openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg) |
| 480 | { |
| 481 | _InternalNameMapperState *state = (_InternalNameMapperState *)arg; |
| 482 | PyObject *py_name; |
| 483 | |
| 484 | assert(state != NULL); |
| 485 | if (openssl_obj_name == NULL) |
| 486 | return; |
| 487 | /* Ignore aliased names, they pollute the list and OpenSSL appears to |
| 488 | * have a its own definition of alias as the resulting list still |
| 489 | * contains duplicate and alternate names for several algorithms. */ |
| 490 | if (openssl_obj_name->alias) |
| 491 | return; |
| 492 | |
| 493 | py_name = PyUnicode_FromString(openssl_obj_name->name); |
| 494 | if (py_name == NULL) { |
| 495 | state->error = 1; |
| 496 | } else { |
| 497 | if (PySet_Add(state->set, py_name) != 0) { |
| 498 | Py_DECREF(py_name); |
| 499 | state->error = 1; |
| 500 | } |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | |
| 505 | /* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */ |
| 506 | static PyObject* |
| 507 | generate_hash_name_list(void) |
| 508 | { |
| 509 | _InternalNameMapperState state; |
| 510 | state.set = PyFrozenSet_New(NULL); |
| 511 | if (state.set == NULL) |
| 512 | return NULL; |
| 513 | state.error = 0; |
| 514 | |
| 515 | OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state); |
| 516 | |
| 517 | if (state.error) { |
| 518 | Py_DECREF(state.set); |
| 519 | return NULL; |
| 520 | } |
| 521 | return state.set; |
| 522 | } |
| 523 | |
| 524 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 525 | /* |
| 526 | * This macro generates constructor function definitions for specific |
| 527 | * hash algorithms. These constructors are much faster than calling |
| 528 | * the generic one passing it a python string and are noticably |
| 529 | * faster than calling a python new() wrapper. Thats important for |
| 530 | * code that wants to make hashes of a bunch of small strings. |
| 531 | */ |
| 532 | #define GEN_CONSTRUCTOR(NAME) \ |
| 533 | static PyObject * \ |
| 534 | EVP_new_ ## NAME (PyObject *self, PyObject *args) \ |
| 535 | { \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 536 | PyObject *data_obj = NULL; \ |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 537 | Py_buffer view = { 0 }; \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 538 | PyObject *ret_obj; \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 539 | \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 540 | if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 541 | return NULL; \ |
| 542 | } \ |
| 543 | \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 544 | if (data_obj) \ |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 545 | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 546 | \ |
| 547 | ret_obj = EVPnew( \ |
| 548 | CONST_ ## NAME ## _name_obj, \ |
| 549 | NULL, \ |
| 550 | CONST_new_ ## NAME ## _ctx_p, \ |
| 551 | (unsigned char*)view.buf, \ |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 552 | view.len); \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 553 | \ |
| 554 | if (data_obj) \ |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 555 | PyBuffer_Release(&view); \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 556 | return ret_obj; \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /* a PyMethodDef structure for the constructor */ |
| 560 | #define CONSTRUCTOR_METH_DEF(NAME) \ |
| 561 | {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \ |
| 562 | PyDoc_STR("Returns a " #NAME \ |
| 563 | " hash object; optionally initialized with a string") \ |
| 564 | } |
| 565 | |
Gregory P. Smith | aded2e5 | 2013-02-01 17:05:29 -0800 | [diff] [blame] | 566 | /* used in the init function to setup a constructor: initialize OpenSSL |
| 567 | constructor constants if they haven't been initialized already. */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 568 | #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \ |
Gregory P. Smith | aded2e5 | 2013-02-01 17:05:29 -0800 | [diff] [blame] | 569 | if (CONST_ ## NAME ## _name_obj == NULL) { \ |
| 570 | CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \ |
| 571 | if (EVP_get_digestbyname(#NAME)) { \ |
| 572 | CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \ |
| 573 | EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \ |
| 574 | } \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 575 | } \ |
| 576 | } while (0); |
| 577 | |
| 578 | GEN_CONSTRUCTOR(md5) |
| 579 | GEN_CONSTRUCTOR(sha1) |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 580 | #ifdef _OPENSSL_SUPPORTS_SHA2 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 581 | GEN_CONSTRUCTOR(sha224) |
| 582 | GEN_CONSTRUCTOR(sha256) |
| 583 | GEN_CONSTRUCTOR(sha384) |
| 584 | GEN_CONSTRUCTOR(sha512) |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 585 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 586 | |
| 587 | /* List of functions exported by this module */ |
| 588 | |
| 589 | static struct PyMethodDef EVP_functions[] = { |
| 590 | {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__}, |
| 591 | CONSTRUCTOR_METH_DEF(md5), |
| 592 | CONSTRUCTOR_METH_DEF(sha1), |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 593 | #ifdef _OPENSSL_SUPPORTS_SHA2 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 594 | CONSTRUCTOR_METH_DEF(sha224), |
| 595 | CONSTRUCTOR_METH_DEF(sha256), |
| 596 | CONSTRUCTOR_METH_DEF(sha384), |
| 597 | CONSTRUCTOR_METH_DEF(sha512), |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 598 | #endif |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 599 | {NULL, NULL} /* Sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 600 | }; |
| 601 | |
| 602 | |
| 603 | /* Initialize this module. */ |
| 604 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 605 | |
| 606 | static struct PyModuleDef _hashlibmodule = { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 607 | PyModuleDef_HEAD_INIT, |
| 608 | "_hashlib", |
| 609 | NULL, |
| 610 | -1, |
| 611 | EVP_functions, |
| 612 | NULL, |
| 613 | NULL, |
| 614 | NULL, |
| 615 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 616 | }; |
| 617 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 618 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 619 | PyInit__hashlib(void) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 620 | { |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 621 | PyObject *m, *openssl_md_meth_names; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 622 | |
| 623 | OpenSSL_add_all_digests(); |
| 624 | |
| 625 | /* TODO build EVP_functions openssl_* entries dynamically based |
| 626 | * on what hashes are supported rather than listing many |
| 627 | * but having some be unsupported. Only init appropriate |
| 628 | * constants. */ |
| 629 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 630 | Py_TYPE(&EVPtype) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 631 | if (PyType_Ready(&EVPtype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 632 | return NULL; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 633 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 634 | m = PyModule_Create(&_hashlibmodule); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 635 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 636 | return NULL; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 637 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 638 | openssl_md_meth_names = generate_hash_name_list(); |
| 639 | if (openssl_md_meth_names == NULL) { |
| 640 | Py_DECREF(m); |
| 641 | return NULL; |
| 642 | } |
| 643 | if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) { |
| 644 | Py_DECREF(m); |
| 645 | return NULL; |
| 646 | } |
| 647 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 648 | #if HASH_OBJ_CONSTRUCTOR |
| 649 | Py_INCREF(&EVPtype); |
| 650 | PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype); |
| 651 | #endif |
| 652 | |
| 653 | /* these constants are used by the convenience constructors */ |
| 654 | INIT_CONSTRUCTOR_CONSTANTS(md5); |
| 655 | INIT_CONSTRUCTOR_CONSTANTS(sha1); |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 656 | #ifdef _OPENSSL_SUPPORTS_SHA2 |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 657 | INIT_CONSTRUCTOR_CONSTANTS(sha224); |
| 658 | INIT_CONSTRUCTOR_CONSTANTS(sha256); |
| 659 | INIT_CONSTRUCTOR_CONSTANTS(sha384); |
| 660 | INIT_CONSTRUCTOR_CONSTANTS(sha512); |
Benjamin Peterson | 5e55b3e | 2010-02-03 02:35:45 +0000 | [diff] [blame] | 661 | #endif |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 662 | return m; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 663 | } |