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