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 | 4dff6f6 | 2015-04-25 23:42:38 +0000 | [diff] [blame] | 19 | #include "pystrhex.h" |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 20 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 21 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 22 | /* EVP is the preferred interface to hashing in OpenSSL */ |
| 23 | #include <openssl/evp.h> |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 24 | #include <openssl/hmac.h> |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 25 | /* We use the object interface to discover what hashes OpenSSL supports. */ |
| 26 | #include <openssl/objects.h> |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 27 | #include "openssl/err.h" |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 28 | |
| 29 | #define MUNCH_SIZE INT_MAX |
| 30 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 31 | #ifndef HASH_OBJ_CONSTRUCTOR |
| 32 | #define HASH_OBJ_CONSTRUCTOR 0 |
| 33 | #endif |
| 34 | |
| 35 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 36 | typedef struct { |
| 37 | PyObject_HEAD |
| 38 | PyObject *name; /* name of this hash algorithm */ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 39 | EVP_MD_CTX ctx; /* OpenSSL message digest context */ |
| 40 | #ifdef WITH_THREAD |
| 41 | PyThread_type_lock lock; /* OpenSSL context lock */ |
| 42 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 43 | } EVPobject; |
| 44 | |
| 45 | |
| 46 | static PyTypeObject EVPtype; |
| 47 | |
| 48 | |
| 49 | #define DEFINE_CONSTS_FOR_NEW(Name) \ |
Gregory P. Smith | aded2e5 | 2013-02-01 17:05:29 -0800 | [diff] [blame] | 50 | static PyObject *CONST_ ## Name ## _name_obj = NULL; \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 51 | static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \ |
| 52 | static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL; |
| 53 | |
Neal Norwitz | f045914 | 2006-01-07 21:20:24 +0000 | [diff] [blame] | 54 | DEFINE_CONSTS_FOR_NEW(md5) |
| 55 | DEFINE_CONSTS_FOR_NEW(sha1) |
| 56 | DEFINE_CONSTS_FOR_NEW(sha224) |
| 57 | DEFINE_CONSTS_FOR_NEW(sha256) |
| 58 | DEFINE_CONSTS_FOR_NEW(sha384) |
| 59 | DEFINE_CONSTS_FOR_NEW(sha512) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 60 | |
| 61 | |
| 62 | static EVPobject * |
| 63 | newEVPobject(PyObject *name) |
| 64 | { |
| 65 | EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype); |
| 66 | |
| 67 | /* save the name for .name to return */ |
| 68 | if (retval != NULL) { |
| 69 | Py_INCREF(name); |
| 70 | retval->name = name; |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 71 | #ifdef WITH_THREAD |
| 72 | retval->lock = NULL; |
| 73 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | return retval; |
| 77 | } |
| 78 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 79 | static void |
| 80 | EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) |
| 81 | { |
| 82 | unsigned int process; |
| 83 | const unsigned char *cp = (const unsigned char *)vp; |
| 84 | while (0 < len) { |
| 85 | if (len > (Py_ssize_t)MUNCH_SIZE) |
| 86 | process = MUNCH_SIZE; |
| 87 | else |
| 88 | process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); |
| 89 | EVP_DigestUpdate(&self->ctx, (const void*)cp, process); |
| 90 | len -= process; |
| 91 | cp += process; |
| 92 | } |
| 93 | } |
| 94 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 95 | /* Internal methods for a hash object */ |
| 96 | |
| 97 | static void |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 98 | EVP_dealloc(EVPobject *self) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 99 | { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 100 | #ifdef WITH_THREAD |
| 101 | if (self->lock != NULL) |
| 102 | PyThread_free_lock(self->lock); |
| 103 | #endif |
| 104 | EVP_MD_CTX_cleanup(&self->ctx); |
| 105 | Py_XDECREF(self->name); |
| 106 | PyObject_Del(self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 109 | static void locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self) |
| 110 | { |
| 111 | ENTER_HASHLIB(self); |
| 112 | EVP_MD_CTX_copy(new_ctx_p, &self->ctx); |
| 113 | LEAVE_HASHLIB(self); |
| 114 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 115 | |
| 116 | /* External methods for a hash object */ |
| 117 | |
| 118 | PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object."); |
| 119 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 120 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 121 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 122 | EVP_copy(EVPobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 123 | { |
| 124 | EVPobject *newobj; |
| 125 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 126 | if ( (newobj = newEVPobject(self->name))==NULL) |
| 127 | return NULL; |
| 128 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 129 | locked_EVP_MD_CTX_copy(&newobj->ctx, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 130 | return (PyObject *)newobj; |
| 131 | } |
| 132 | |
| 133 | PyDoc_STRVAR(EVP_digest__doc__, |
| 134 | "Return the digest value as a string of binary data."); |
| 135 | |
| 136 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 137 | EVP_digest(EVPobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 138 | { |
| 139 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 140 | EVP_MD_CTX temp_ctx; |
| 141 | PyObject *retval; |
| 142 | unsigned int digest_size; |
| 143 | |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 144 | locked_EVP_MD_CTX_copy(&temp_ctx, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 145 | digest_size = EVP_MD_CTX_size(&temp_ctx); |
Neal Norwitz | f045914 | 2006-01-07 21:20:24 +0000 | [diff] [blame] | 146 | EVP_DigestFinal(&temp_ctx, digest, NULL); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 147 | |
Christian Heimes | 72b710a | 2008-05-26 13:28:38 +0000 | [diff] [blame] | 148 | retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 149 | EVP_MD_CTX_cleanup(&temp_ctx); |
| 150 | return retval; |
| 151 | } |
| 152 | |
| 153 | PyDoc_STRVAR(EVP_hexdigest__doc__, |
| 154 | "Return the digest value as a string of hexadecimal digits."); |
| 155 | |
| 156 | static PyObject * |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 157 | EVP_hexdigest(EVPobject *self, PyObject *unused) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 158 | { |
| 159 | unsigned char digest[EVP_MAX_MD_SIZE]; |
| 160 | EVP_MD_CTX temp_ctx; |
Gregory P. Smith | 4dff6f6 | 2015-04-25 23:42:38 +0000 | [diff] [blame] | 161 | unsigned int digest_size; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 162 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 163 | /* Get the raw (binary) digest value */ |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 164 | locked_EVP_MD_CTX_copy(&temp_ctx, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 165 | digest_size = EVP_MD_CTX_size(&temp_ctx); |
| 166 | EVP_DigestFinal(&temp_ctx, digest, NULL); |
| 167 | |
| 168 | EVP_MD_CTX_cleanup(&temp_ctx); |
| 169 | |
Gregory P. Smith | 4dff6f6 | 2015-04-25 23:42:38 +0000 | [diff] [blame] | 170 | return _Py_strhex((const char *)digest, digest_size); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | PyDoc_STRVAR(EVP_update__doc__, |
| 174 | "Update this hash object's state with the provided string."); |
| 175 | |
| 176 | static PyObject * |
| 177 | EVP_update(EVPobject *self, PyObject *args) |
| 178 | { |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 179 | PyObject *obj; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 180 | Py_buffer view; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 181 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 182 | if (!PyArg_ParseTuple(args, "O:update", &obj)) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 183 | return NULL; |
| 184 | |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 185 | GET_BUFFER_VIEW_OR_ERROUT(obj, &view); |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 186 | |
| 187 | #ifdef WITH_THREAD |
| 188 | if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) { |
| 189 | self->lock = PyThread_allocate_lock(); |
Gregory P. Smith | 3f61d61 | 2009-05-04 00:45:33 +0000 | [diff] [blame] | 190 | /* fail? lock = NULL and we fail over to non-threaded code. */ |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 191 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 192 | |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 193 | if (self->lock != NULL) { |
| 194 | Py_BEGIN_ALLOW_THREADS |
| 195 | PyThread_acquire_lock(self->lock, 1); |
| 196 | EVP_hash(self, view.buf, view.len); |
| 197 | PyThread_release_lock(self->lock); |
| 198 | Py_END_ALLOW_THREADS |
| 199 | } else { |
| 200 | EVP_hash(self, view.buf, view.len); |
| 201 | } |
| 202 | #else |
| 203 | EVP_hash(self, view.buf, view.len); |
| 204 | #endif |
| 205 | |
| 206 | PyBuffer_Release(&view); |
| 207 | Py_RETURN_NONE; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static PyMethodDef EVP_methods[] = { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 211 | {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__}, |
| 212 | {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__}, |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 213 | {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__}, |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 214 | {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__}, |
| 215 | {NULL, NULL} /* sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | static PyObject * |
| 219 | EVP_get_block_size(EVPobject *self, void *closure) |
| 220 | { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 221 | long block_size; |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 222 | block_size = EVP_MD_CTX_block_size(&self->ctx); |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 223 | return PyLong_FromLong(block_size); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static PyObject * |
| 227 | EVP_get_digest_size(EVPobject *self, void *closure) |
| 228 | { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 229 | long size; |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 230 | size = EVP_MD_CTX_size(&self->ctx); |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 231 | return PyLong_FromLong(size); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static PyMemberDef EVP_members[] = { |
| 235 | {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")}, |
| 236 | {NULL} /* Sentinel */ |
| 237 | }; |
| 238 | |
| 239 | static PyGetSetDef EVP_getseters[] = { |
| 240 | {"digest_size", |
| 241 | (getter)EVP_get_digest_size, NULL, |
| 242 | NULL, |
| 243 | NULL}, |
| 244 | {"block_size", |
| 245 | (getter)EVP_get_block_size, NULL, |
| 246 | NULL, |
| 247 | NULL}, |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 248 | {NULL} /* Sentinel */ |
| 249 | }; |
| 250 | |
| 251 | |
| 252 | static PyObject * |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 253 | EVP_repr(EVPobject *self) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 254 | { |
Victor Stinner | 3f1af5c | 2010-03-12 17:00:41 +0000 | [diff] [blame] | 255 | return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | #if HASH_OBJ_CONSTRUCTOR |
| 259 | static int |
| 260 | EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds) |
| 261 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 262 | static char *kwlist[] = {"name", "string", NULL}; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 263 | PyObject *name_obj = NULL; |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 264 | PyObject *data_obj = NULL; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 265 | Py_buffer view; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 266 | char *nameStr; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 267 | const EVP_MD *digest; |
| 268 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 269 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist, |
| 270 | &name_obj, &data_obj)) { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 271 | return -1; |
| 272 | } |
| 273 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 274 | if (data_obj) |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 275 | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 276 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 277 | if (!PyArg_Parse(name_obj, "s", &nameStr)) { |
| 278 | PyErr_SetString(PyExc_TypeError, "name must be a string"); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 279 | if (data_obj) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 280 | PyBuffer_Release(&view); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 281 | return -1; |
| 282 | } |
| 283 | |
| 284 | digest = EVP_get_digestbyname(nameStr); |
| 285 | if (!digest) { |
| 286 | PyErr_SetString(PyExc_ValueError, "unknown hash function"); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 287 | if (data_obj) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 288 | PyBuffer_Release(&view); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 289 | return -1; |
| 290 | } |
| 291 | EVP_DigestInit(&self->ctx, digest); |
| 292 | |
| 293 | self->name = name_obj; |
| 294 | Py_INCREF(self->name); |
| 295 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 296 | if (data_obj) { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 297 | if (view.len >= HASHLIB_GIL_MINSIZE) { |
| 298 | Py_BEGIN_ALLOW_THREADS |
| 299 | EVP_hash(self, view.buf, view.len); |
| 300 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 301 | } else { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 302 | EVP_hash(self, view.buf, view.len); |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 303 | } |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 304 | PyBuffer_Release(&view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 305 | } |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 306 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | #endif |
| 310 | |
| 311 | |
| 312 | PyDoc_STRVAR(hashtype_doc, |
| 313 | "A hash represents the object used to calculate a checksum of a\n\ |
| 314 | string of information.\n\ |
| 315 | \n\ |
| 316 | Methods:\n\ |
| 317 | \n\ |
| 318 | update() -- updates the current digest with an additional string\n\ |
| 319 | digest() -- return the current digest value\n\ |
| 320 | hexdigest() -- return the current digest as a string of hexadecimal digits\n\ |
| 321 | copy() -- return a copy of the current hash object\n\ |
| 322 | \n\ |
| 323 | Attributes:\n\ |
| 324 | \n\ |
| 325 | name -- the hash algorithm being used by this object\n\ |
| 326 | digest_size -- number of bytes in this hashes output\n"); |
| 327 | |
| 328 | static PyTypeObject EVPtype = { |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 329 | PyVarObject_HEAD_INIT(NULL, 0) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 330 | "_hashlib.HASH", /*tp_name*/ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 331 | sizeof(EVPobject), /*tp_basicsize*/ |
| 332 | 0, /*tp_itemsize*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 333 | /* methods */ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 334 | (destructor)EVP_dealloc, /*tp_dealloc*/ |
| 335 | 0, /*tp_print*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 336 | 0, /*tp_getattr*/ |
| 337 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 338 | 0, /*tp_reserved*/ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 339 | (reprfunc)EVP_repr, /*tp_repr*/ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 340 | 0, /*tp_as_number*/ |
| 341 | 0, /*tp_as_sequence*/ |
| 342 | 0, /*tp_as_mapping*/ |
| 343 | 0, /*tp_hash*/ |
| 344 | 0, /*tp_call*/ |
| 345 | 0, /*tp_str*/ |
| 346 | 0, /*tp_getattro*/ |
| 347 | 0, /*tp_setattro*/ |
| 348 | 0, /*tp_as_buffer*/ |
| 349 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
| 350 | hashtype_doc, /*tp_doc*/ |
| 351 | 0, /*tp_traverse*/ |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 352 | 0, /*tp_clear*/ |
| 353 | 0, /*tp_richcompare*/ |
| 354 | 0, /*tp_weaklistoffset*/ |
| 355 | 0, /*tp_iter*/ |
| 356 | 0, /*tp_iternext*/ |
| 357 | EVP_methods, /* tp_methods */ |
| 358 | EVP_members, /* tp_members */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 359 | EVP_getseters, /* tp_getset */ |
| 360 | #if 1 |
| 361 | 0, /* tp_base */ |
| 362 | 0, /* tp_dict */ |
| 363 | 0, /* tp_descr_get */ |
| 364 | 0, /* tp_descr_set */ |
| 365 | 0, /* tp_dictoffset */ |
| 366 | #endif |
| 367 | #if HASH_OBJ_CONSTRUCTOR |
| 368 | (initproc)EVP_tp_init, /* tp_init */ |
| 369 | #endif |
| 370 | }; |
| 371 | |
| 372 | static PyObject * |
| 373 | EVPnew(PyObject *name_obj, |
| 374 | const EVP_MD *digest, const EVP_MD_CTX *initial_ctx, |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 375 | const unsigned char *cp, Py_ssize_t len) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 376 | { |
| 377 | EVPobject *self; |
| 378 | |
| 379 | if (!digest && !initial_ctx) { |
| 380 | PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
| 381 | return NULL; |
| 382 | } |
| 383 | |
| 384 | if ((self = newEVPobject(name_obj)) == NULL) |
| 385 | return NULL; |
| 386 | |
| 387 | if (initial_ctx) { |
| 388 | EVP_MD_CTX_copy(&self->ctx, initial_ctx); |
| 389 | } else { |
| 390 | EVP_DigestInit(&self->ctx, digest); |
| 391 | } |
| 392 | |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 393 | if (cp && len) { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 394 | if (len >= HASHLIB_GIL_MINSIZE) { |
| 395 | Py_BEGIN_ALLOW_THREADS |
| 396 | EVP_hash(self, cp, len); |
| 397 | Py_END_ALLOW_THREADS |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 398 | } else { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 399 | EVP_hash(self, cp, len); |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 400 | } |
| 401 | } |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 402 | |
| 403 | return (PyObject *)self; |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /* The module-level function: new() */ |
| 408 | |
| 409 | PyDoc_STRVAR(EVP_new__doc__, |
| 410 | "Return a new hash object using the named algorithm.\n\ |
| 411 | An optional string argument may be provided and will be\n\ |
| 412 | automatically hashed.\n\ |
| 413 | \n\ |
| 414 | The MD5 and SHA1 algorithms are always supported.\n"); |
| 415 | |
| 416 | static PyObject * |
| 417 | EVP_new(PyObject *self, PyObject *args, PyObject *kwdict) |
| 418 | { |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 419 | static char *kwlist[] = {"name", "string", NULL}; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 420 | PyObject *name_obj = NULL; |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 421 | PyObject *data_obj = NULL; |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 422 | Py_buffer view = { 0 }; |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 423 | PyObject *ret_obj; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 424 | char *name; |
| 425 | const EVP_MD *digest; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 426 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 427 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist, |
| 428 | &name_obj, &data_obj)) { |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 429 | return NULL; |
| 430 | } |
| 431 | |
| 432 | if (!PyArg_Parse(name_obj, "s", &name)) { |
| 433 | PyErr_SetString(PyExc_TypeError, "name must be a string"); |
| 434 | return NULL; |
| 435 | } |
| 436 | |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 437 | if (data_obj) |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 438 | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 439 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 440 | digest = EVP_get_digestbyname(name); |
| 441 | |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 442 | 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] | 443 | |
| 444 | if (data_obj) |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 445 | PyBuffer_Release(&view); |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 446 | return ret_obj; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 449 | |
| 450 | |
Christian Heimes | 351f539 | 2013-10-19 17:59:48 +0200 | [diff] [blame] | 451 | #if (OPENSSL_VERSION_NUMBER >= 0x10000000 && !defined(OPENSSL_NO_HMAC) \ |
| 452 | && !defined(OPENSSL_NO_SHA)) |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 453 | |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 454 | #define PY_PBKDF2_HMAC 1 |
| 455 | |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 456 | /* Improved implementation of PKCS5_PBKDF2_HMAC() |
| 457 | * |
| 458 | * PKCS5_PBKDF2_HMAC_fast() hashes the password exactly one time instead of |
| 459 | * `iter` times. Today (2013) the iteration count is typically 100,000 or |
| 460 | * more. The improved algorithm is not subject to a Denial-of-Service |
| 461 | * vulnerability with overly large passwords. |
| 462 | * |
| 463 | * Also OpenSSL < 1.0 don't provide PKCS5_PBKDF2_HMAC(), only |
| 464 | * PKCS5_PBKDF2_SHA1. |
| 465 | */ |
Christian Heimes | c6564b9 | 2013-10-20 13:23:03 +0200 | [diff] [blame] | 466 | static int |
| 467 | PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen, |
| 468 | const unsigned char *salt, int saltlen, |
| 469 | int iter, const EVP_MD *digest, |
| 470 | int keylen, unsigned char *out) |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 471 | { |
| 472 | unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; |
| 473 | int cplen, j, k, tkeylen, mdlen; |
| 474 | unsigned long i = 1; |
| 475 | HMAC_CTX hctx_tpl, hctx; |
| 476 | |
| 477 | mdlen = EVP_MD_size(digest); |
| 478 | if (mdlen < 0) |
| 479 | return 0; |
| 480 | |
| 481 | HMAC_CTX_init(&hctx_tpl); |
| 482 | HMAC_CTX_init(&hctx); |
| 483 | p = out; |
| 484 | tkeylen = keylen; |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 485 | if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) { |
| 486 | HMAC_CTX_cleanup(&hctx_tpl); |
| 487 | return 0; |
| 488 | } |
Benjamin Peterson | 3c0769d | 2015-09-27 02:13:40 -0700 | [diff] [blame] | 489 | while (tkeylen) { |
| 490 | if (tkeylen > mdlen) |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 491 | cplen = mdlen; |
| 492 | else |
| 493 | cplen = tkeylen; |
| 494 | /* We are unlikely to ever use more than 256 blocks (5120 bits!) |
| 495 | * but just in case... |
| 496 | */ |
| 497 | itmp[0] = (unsigned char)((i >> 24) & 0xff); |
| 498 | itmp[1] = (unsigned char)((i >> 16) & 0xff); |
| 499 | itmp[2] = (unsigned char)((i >> 8) & 0xff); |
| 500 | itmp[3] = (unsigned char)(i & 0xff); |
| 501 | if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { |
| 502 | HMAC_CTX_cleanup(&hctx_tpl); |
| 503 | return 0; |
| 504 | } |
| 505 | if (!HMAC_Update(&hctx, salt, saltlen) |
| 506 | || !HMAC_Update(&hctx, itmp, 4) |
| 507 | || !HMAC_Final(&hctx, digtmp, NULL)) { |
| 508 | HMAC_CTX_cleanup(&hctx_tpl); |
| 509 | HMAC_CTX_cleanup(&hctx); |
| 510 | return 0; |
| 511 | } |
Christian Heimes | 6853108 | 2013-11-06 17:25:17 +0100 | [diff] [blame] | 512 | HMAC_CTX_cleanup(&hctx); |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 513 | memcpy(p, digtmp, cplen); |
| 514 | for (j = 1; j < iter; j++) { |
| 515 | if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { |
| 516 | HMAC_CTX_cleanup(&hctx_tpl); |
| 517 | return 0; |
| 518 | } |
| 519 | if (!HMAC_Update(&hctx, digtmp, mdlen) |
| 520 | || !HMAC_Final(&hctx, digtmp, NULL)) { |
| 521 | HMAC_CTX_cleanup(&hctx_tpl); |
| 522 | HMAC_CTX_cleanup(&hctx); |
| 523 | return 0; |
| 524 | } |
| 525 | HMAC_CTX_cleanup(&hctx); |
| 526 | for (k = 0; k < cplen; k++) { |
| 527 | p[k] ^= digtmp[k]; |
| 528 | } |
| 529 | } |
| 530 | tkeylen-= cplen; |
| 531 | i++; |
| 532 | p+= cplen; |
| 533 | } |
| 534 | HMAC_CTX_cleanup(&hctx_tpl); |
| 535 | return 1; |
| 536 | } |
| 537 | |
Christian Heimes | 48b7df7 | 2013-12-05 07:38:13 +0100 | [diff] [blame] | 538 | /* LCOV_EXCL_START */ |
Brett Cannon | 2be28a6 | 2013-11-01 10:25:13 -0400 | [diff] [blame] | 539 | static PyObject * |
| 540 | _setException(PyObject *exc) |
| 541 | { |
| 542 | unsigned long errcode; |
| 543 | const char *lib, *func, *reason; |
| 544 | |
| 545 | errcode = ERR_peek_last_error(); |
| 546 | if (!errcode) { |
| 547 | PyErr_SetString(exc, "unknown reasons"); |
| 548 | return NULL; |
| 549 | } |
| 550 | ERR_clear_error(); |
| 551 | |
| 552 | lib = ERR_lib_error_string(errcode); |
| 553 | func = ERR_func_error_string(errcode); |
| 554 | reason = ERR_reason_error_string(errcode); |
| 555 | |
| 556 | if (lib && func) { |
| 557 | PyErr_Format(exc, "[%s: %s] %s", lib, func, reason); |
| 558 | } |
| 559 | else if (lib) { |
| 560 | PyErr_Format(exc, "[%s] %s", lib, reason); |
| 561 | } |
| 562 | else { |
| 563 | PyErr_SetString(exc, reason); |
| 564 | } |
| 565 | return NULL; |
| 566 | } |
Christian Heimes | 48b7df7 | 2013-12-05 07:38:13 +0100 | [diff] [blame] | 567 | /* LCOV_EXCL_STOP */ |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 568 | |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 569 | PyDoc_STRVAR(pbkdf2_hmac__doc__, |
| 570 | "pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\ |
| 571 | \n\ |
| 572 | Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\ |
| 573 | pseudorandom function."); |
| 574 | |
| 575 | static PyObject * |
| 576 | pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict) |
| 577 | { |
| 578 | static char *kwlist[] = {"hash_name", "password", "salt", "iterations", |
| 579 | "dklen", NULL}; |
| 580 | PyObject *key_obj = NULL, *dklen_obj = Py_None; |
| 581 | char *name, *key; |
| 582 | Py_buffer password, salt; |
| 583 | long iterations, dklen; |
| 584 | int retval; |
| 585 | const EVP_MD *digest; |
| 586 | |
| 587 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac", |
| 588 | kwlist, &name, &password, &salt, |
| 589 | &iterations, &dklen_obj)) { |
| 590 | return NULL; |
| 591 | } |
| 592 | |
| 593 | digest = EVP_get_digestbyname(name); |
| 594 | if (digest == NULL) { |
| 595 | PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
| 596 | goto end; |
| 597 | } |
| 598 | |
| 599 | if (password.len > INT_MAX) { |
| 600 | PyErr_SetString(PyExc_OverflowError, |
| 601 | "password is too long."); |
| 602 | goto end; |
| 603 | } |
| 604 | |
| 605 | if (salt.len > INT_MAX) { |
| 606 | PyErr_SetString(PyExc_OverflowError, |
| 607 | "salt is too long."); |
| 608 | goto end; |
| 609 | } |
| 610 | |
| 611 | if (iterations < 1) { |
| 612 | PyErr_SetString(PyExc_ValueError, |
| 613 | "iteration value must be greater than 0."); |
| 614 | goto end; |
| 615 | } |
| 616 | if (iterations > INT_MAX) { |
| 617 | PyErr_SetString(PyExc_OverflowError, |
| 618 | "iteration value is too great."); |
| 619 | goto end; |
| 620 | } |
| 621 | |
| 622 | if (dklen_obj == Py_None) { |
| 623 | dklen = EVP_MD_size(digest); |
| 624 | } else { |
| 625 | dklen = PyLong_AsLong(dklen_obj); |
| 626 | if ((dklen == -1) && PyErr_Occurred()) { |
| 627 | goto end; |
| 628 | } |
| 629 | } |
| 630 | if (dklen < 1) { |
| 631 | PyErr_SetString(PyExc_ValueError, |
| 632 | "key length must be greater than 0."); |
| 633 | goto end; |
| 634 | } |
| 635 | if (dklen > INT_MAX) { |
| 636 | /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */ |
| 637 | PyErr_SetString(PyExc_OverflowError, |
| 638 | "key length is too great."); |
| 639 | goto end; |
| 640 | } |
| 641 | |
| 642 | key_obj = PyBytes_FromStringAndSize(NULL, dklen); |
| 643 | if (key_obj == NULL) { |
| 644 | goto end; |
| 645 | } |
| 646 | key = PyBytes_AS_STRING(key_obj); |
| 647 | |
| 648 | Py_BEGIN_ALLOW_THREADS |
Victor Stinner | c1a57d3 | 2013-11-16 00:27:16 +0100 | [diff] [blame] | 649 | retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, (int)password.len, |
Christian Heimes | cc6cdce | 2013-11-18 09:59:44 +0100 | [diff] [blame] | 650 | (unsigned char *)salt.buf, (int)salt.len, |
Christian Heimes | e723622 | 2013-10-19 14:24:44 +0200 | [diff] [blame] | 651 | iterations, digest, dklen, |
| 652 | (unsigned char *)key); |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 653 | Py_END_ALLOW_THREADS |
| 654 | |
| 655 | if (!retval) { |
| 656 | Py_CLEAR(key_obj); |
| 657 | _setException(PyExc_ValueError); |
| 658 | goto end; |
| 659 | } |
| 660 | |
| 661 | end: |
| 662 | PyBuffer_Release(&password); |
| 663 | PyBuffer_Release(&salt); |
| 664 | return key_obj; |
| 665 | } |
| 666 | |
| 667 | #endif |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 668 | |
| 669 | /* State for our callback function so that it can accumulate a result. */ |
| 670 | typedef struct _internal_name_mapper_state { |
| 671 | PyObject *set; |
| 672 | int error; |
| 673 | } _InternalNameMapperState; |
| 674 | |
| 675 | |
| 676 | /* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */ |
| 677 | static void |
| 678 | _openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg) |
| 679 | { |
| 680 | _InternalNameMapperState *state = (_InternalNameMapperState *)arg; |
| 681 | PyObject *py_name; |
| 682 | |
| 683 | assert(state != NULL); |
| 684 | if (openssl_obj_name == NULL) |
| 685 | return; |
| 686 | /* Ignore aliased names, they pollute the list and OpenSSL appears to |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 687 | * have its own definition of alias as the resulting list still |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 688 | * contains duplicate and alternate names for several algorithms. */ |
| 689 | if (openssl_obj_name->alias) |
| 690 | return; |
| 691 | |
| 692 | py_name = PyUnicode_FromString(openssl_obj_name->name); |
| 693 | if (py_name == NULL) { |
| 694 | state->error = 1; |
| 695 | } else { |
| 696 | if (PySet_Add(state->set, py_name) != 0) { |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 697 | state->error = 1; |
| 698 | } |
Christian Heimes | db816d6 | 2013-10-29 12:14:55 +0100 | [diff] [blame] | 699 | Py_DECREF(py_name); |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
| 703 | |
| 704 | /* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */ |
| 705 | static PyObject* |
| 706 | generate_hash_name_list(void) |
| 707 | { |
| 708 | _InternalNameMapperState state; |
| 709 | state.set = PyFrozenSet_New(NULL); |
| 710 | if (state.set == NULL) |
| 711 | return NULL; |
| 712 | state.error = 0; |
| 713 | |
| 714 | OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state); |
| 715 | |
| 716 | if (state.error) { |
| 717 | Py_DECREF(state.set); |
| 718 | return NULL; |
| 719 | } |
| 720 | return state.set; |
| 721 | } |
| 722 | |
| 723 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 724 | /* |
| 725 | * This macro generates constructor function definitions for specific |
| 726 | * hash algorithms. These constructors are much faster than calling |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame^] | 727 | * the generic one passing it a python string and are noticeably |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 728 | * faster than calling a python new() wrapper. Thats important for |
| 729 | * code that wants to make hashes of a bunch of small strings. |
| 730 | */ |
| 731 | #define GEN_CONSTRUCTOR(NAME) \ |
| 732 | static PyObject * \ |
| 733 | EVP_new_ ## NAME (PyObject *self, PyObject *args) \ |
| 734 | { \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 735 | PyObject *data_obj = NULL; \ |
Travis E. Oliphant | 8ae62b6 | 2007-09-23 02:00:13 +0000 | [diff] [blame] | 736 | Py_buffer view = { 0 }; \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 737 | PyObject *ret_obj; \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 738 | \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 739 | if (!PyArg_ParseTuple(args, "|O:" #NAME , &data_obj)) { \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 740 | return NULL; \ |
| 741 | } \ |
| 742 | \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 743 | if (data_obj) \ |
Gregory P. Smith | 365a186 | 2009-02-12 07:35:29 +0000 | [diff] [blame] | 744 | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 745 | \ |
| 746 | ret_obj = EVPnew( \ |
| 747 | CONST_ ## NAME ## _name_obj, \ |
| 748 | NULL, \ |
| 749 | CONST_new_ ## NAME ## _ctx_p, \ |
| 750 | (unsigned char*)view.buf, \ |
Benjamin Peterson | 78cb491 | 2008-09-24 22:53:33 +0000 | [diff] [blame] | 751 | view.len); \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 752 | \ |
| 753 | if (data_obj) \ |
Martin v. Löwis | 423be95 | 2008-08-13 15:53:07 +0000 | [diff] [blame] | 754 | PyBuffer_Release(&view); \ |
Gregory P. Smith | 9406f5c | 2007-08-26 02:58:36 +0000 | [diff] [blame] | 755 | return ret_obj; \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | /* a PyMethodDef structure for the constructor */ |
| 759 | #define CONSTRUCTOR_METH_DEF(NAME) \ |
| 760 | {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \ |
| 761 | PyDoc_STR("Returns a " #NAME \ |
| 762 | " hash object; optionally initialized with a string") \ |
| 763 | } |
| 764 | |
Gregory P. Smith | aded2e5 | 2013-02-01 17:05:29 -0800 | [diff] [blame] | 765 | /* used in the init function to setup a constructor: initialize OpenSSL |
| 766 | constructor constants if they haven't been initialized already. */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 767 | #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \ |
Gregory P. Smith | aded2e5 | 2013-02-01 17:05:29 -0800 | [diff] [blame] | 768 | if (CONST_ ## NAME ## _name_obj == NULL) { \ |
| 769 | CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \ |
| 770 | if (EVP_get_digestbyname(#NAME)) { \ |
| 771 | CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \ |
| 772 | EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \ |
| 773 | } \ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 774 | } \ |
| 775 | } while (0); |
| 776 | |
| 777 | GEN_CONSTRUCTOR(md5) |
| 778 | GEN_CONSTRUCTOR(sha1) |
| 779 | GEN_CONSTRUCTOR(sha224) |
| 780 | GEN_CONSTRUCTOR(sha256) |
| 781 | GEN_CONSTRUCTOR(sha384) |
| 782 | GEN_CONSTRUCTOR(sha512) |
| 783 | |
| 784 | /* List of functions exported by this module */ |
| 785 | |
| 786 | static struct PyMethodDef EVP_functions[] = { |
| 787 | {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__}, |
Christian Heimes | e92ef13 | 2013-10-13 00:52:43 +0200 | [diff] [blame] | 788 | #ifdef PY_PBKDF2_HMAC |
| 789 | {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS, |
| 790 | pbkdf2_hmac__doc__}, |
| 791 | #endif |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 792 | CONSTRUCTOR_METH_DEF(md5), |
| 793 | CONSTRUCTOR_METH_DEF(sha1), |
| 794 | CONSTRUCTOR_METH_DEF(sha224), |
| 795 | CONSTRUCTOR_METH_DEF(sha256), |
| 796 | CONSTRUCTOR_METH_DEF(sha384), |
| 797 | CONSTRUCTOR_METH_DEF(sha512), |
Alexander Belopolsky | f0f4514 | 2010-08-11 17:31:17 +0000 | [diff] [blame] | 798 | {NULL, NULL} /* Sentinel */ |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 799 | }; |
| 800 | |
| 801 | |
| 802 | /* Initialize this module. */ |
| 803 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 804 | |
| 805 | static struct PyModuleDef _hashlibmodule = { |
Antoine Pitrou | bcd5cbe | 2009-01-08 21:17:16 +0000 | [diff] [blame] | 806 | PyModuleDef_HEAD_INIT, |
| 807 | "_hashlib", |
| 808 | NULL, |
| 809 | -1, |
| 810 | EVP_functions, |
| 811 | NULL, |
| 812 | NULL, |
| 813 | NULL, |
| 814 | NULL |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 815 | }; |
| 816 | |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 817 | PyMODINIT_FUNC |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 818 | PyInit__hashlib(void) |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 819 | { |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 820 | PyObject *m, *openssl_md_meth_names; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 821 | |
| 822 | OpenSSL_add_all_digests(); |
Christian Heimes | b7ddbc8 | 2013-10-21 19:48:22 +0200 | [diff] [blame] | 823 | ERR_load_crypto_strings(); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 824 | |
| 825 | /* TODO build EVP_functions openssl_* entries dynamically based |
| 826 | * on what hashes are supported rather than listing many |
| 827 | * but having some be unsupported. Only init appropriate |
| 828 | * constants. */ |
| 829 | |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 830 | Py_TYPE(&EVPtype) = &PyType_Type; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 831 | if (PyType_Ready(&EVPtype) < 0) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 832 | return NULL; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 833 | |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 834 | m = PyModule_Create(&_hashlibmodule); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 835 | if (m == NULL) |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 836 | return NULL; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 837 | |
Gregory P. Smith | 13b5529 | 2010-09-06 08:30:23 +0000 | [diff] [blame] | 838 | openssl_md_meth_names = generate_hash_name_list(); |
| 839 | if (openssl_md_meth_names == NULL) { |
| 840 | Py_DECREF(m); |
| 841 | return NULL; |
| 842 | } |
| 843 | if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) { |
| 844 | Py_DECREF(m); |
| 845 | return NULL; |
| 846 | } |
| 847 | |
Christian Heimes | 327dd73 | 2013-10-22 15:05:23 +0200 | [diff] [blame] | 848 | Py_INCREF((PyObject *)&EVPtype); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 849 | PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype); |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 850 | |
| 851 | /* these constants are used by the convenience constructors */ |
| 852 | INIT_CONSTRUCTOR_CONSTANTS(md5); |
| 853 | INIT_CONSTRUCTOR_CONSTANTS(sha1); |
| 854 | INIT_CONSTRUCTOR_CONSTANTS(sha224); |
| 855 | INIT_CONSTRUCTOR_CONSTANTS(sha256); |
| 856 | INIT_CONSTRUCTOR_CONSTANTS(sha384); |
| 857 | INIT_CONSTRUCTOR_CONSTANTS(sha512); |
Martin v. Löwis | 1a21451 | 2008-06-11 05:26:20 +0000 | [diff] [blame] | 858 | return m; |
Gregory P. Smith | f21a5f7 | 2005-08-21 18:45:59 +0000 | [diff] [blame] | 859 | } |