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