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