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