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