| 1 | /* Module that wraps all OpenSSL hash algorithms */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * Copyright (C) 2005 Gregory P. Smith ([email protected])
|
|---|
| 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 ([email protected])
|
|---|
| 10 | * Greg Stein ([email protected])
|
|---|
| 11 | *
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | #define PY_SSIZE_T_CLEAN
|
|---|
| 15 |
|
|---|
| 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 |
|
|---|
| 23 | #ifndef HASH_OBJ_CONSTRUCTOR
|
|---|
| 24 | #define HASH_OBJ_CONSTRUCTOR 0
|
|---|
| 25 | #endif
|
|---|
| 26 |
|
|---|
| 27 | typedef struct {
|
|---|
| 28 | PyObject_HEAD
|
|---|
| 29 | PyObject *name; /* name of this hash algorithm */
|
|---|
| 30 | EVP_MD_CTX ctx; /* OpenSSL message digest context */
|
|---|
| 31 | } EVPobject;
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | static PyTypeObject EVPtype;
|
|---|
| 35 |
|
|---|
| 36 |
|
|---|
| 37 | #define DEFINE_CONSTS_FOR_NEW(Name) \
|
|---|
| 38 | static PyObject *CONST_ ## Name ## _name_obj; \
|
|---|
| 39 | static EVP_MD_CTX CONST_new_ ## Name ## _ctx; \
|
|---|
| 40 | static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL;
|
|---|
| 41 |
|
|---|
| 42 | DEFINE_CONSTS_FOR_NEW(md5)
|
|---|
| 43 | DEFINE_CONSTS_FOR_NEW(sha1)
|
|---|
| 44 | DEFINE_CONSTS_FOR_NEW(sha224)
|
|---|
| 45 | DEFINE_CONSTS_FOR_NEW(sha256)
|
|---|
| 46 | DEFINE_CONSTS_FOR_NEW(sha384)
|
|---|
| 47 | DEFINE_CONSTS_FOR_NEW(sha512)
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | static EVPobject *
|
|---|
| 51 | newEVPobject(PyObject *name)
|
|---|
| 52 | {
|
|---|
| 53 | EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype);
|
|---|
| 54 |
|
|---|
| 55 | /* save the name for .name to return */
|
|---|
| 56 | if (retval != NULL) {
|
|---|
| 57 | Py_INCREF(name);
|
|---|
| 58 | retval->name = name;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | return retval;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | /* Internal methods for a hash object */
|
|---|
| 65 |
|
|---|
| 66 | static void
|
|---|
| 67 | EVP_dealloc(PyObject *ptr)
|
|---|
| 68 | {
|
|---|
| 69 | EVP_MD_CTX_cleanup(&((EVPobject *)ptr)->ctx);
|
|---|
| 70 | Py_XDECREF(((EVPobject *)ptr)->name);
|
|---|
| 71 | PyObject_Del(ptr);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 |
|
|---|
| 75 | /* External methods for a hash object */
|
|---|
| 76 |
|
|---|
| 77 | PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object.");
|
|---|
| 78 |
|
|---|
| 79 | static PyObject *
|
|---|
| 80 | EVP_copy(EVPobject *self, PyObject *unused)
|
|---|
| 81 | {
|
|---|
| 82 | EVPobject *newobj;
|
|---|
| 83 |
|
|---|
| 84 | if ( (newobj = newEVPobject(self->name))==NULL)
|
|---|
| 85 | return NULL;
|
|---|
| 86 |
|
|---|
| 87 | EVP_MD_CTX_copy(&newobj->ctx, &self->ctx);
|
|---|
| 88 | return (PyObject *)newobj;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | PyDoc_STRVAR(EVP_digest__doc__,
|
|---|
| 92 | "Return the digest value as a string of binary data.");
|
|---|
| 93 |
|
|---|
| 94 | static PyObject *
|
|---|
| 95 | EVP_digest(EVPobject *self, PyObject *unused)
|
|---|
| 96 | {
|
|---|
| 97 | unsigned char digest[EVP_MAX_MD_SIZE];
|
|---|
| 98 | EVP_MD_CTX temp_ctx;
|
|---|
| 99 | PyObject *retval;
|
|---|
| 100 | unsigned int digest_size;
|
|---|
| 101 |
|
|---|
| 102 | EVP_MD_CTX_copy(&temp_ctx, &self->ctx);
|
|---|
| 103 | digest_size = EVP_MD_CTX_size(&temp_ctx);
|
|---|
| 104 | EVP_DigestFinal(&temp_ctx, digest, NULL);
|
|---|
| 105 |
|
|---|
| 106 | retval = PyString_FromStringAndSize((const char *)digest, digest_size);
|
|---|
| 107 | EVP_MD_CTX_cleanup(&temp_ctx);
|
|---|
| 108 | return retval;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | PyDoc_STRVAR(EVP_hexdigest__doc__,
|
|---|
| 112 | "Return the digest value as a string of hexadecimal digits.");
|
|---|
| 113 |
|
|---|
| 114 | static PyObject *
|
|---|
| 115 | EVP_hexdigest(EVPobject *self, PyObject *unused)
|
|---|
| 116 | {
|
|---|
| 117 | unsigned char digest[EVP_MAX_MD_SIZE];
|
|---|
| 118 | EVP_MD_CTX temp_ctx;
|
|---|
| 119 | PyObject *retval;
|
|---|
| 120 | char *hex_digest;
|
|---|
| 121 | unsigned int i, j, digest_size;
|
|---|
| 122 |
|
|---|
| 123 | /* Get the raw (binary) digest value */
|
|---|
| 124 | EVP_MD_CTX_copy(&temp_ctx, &self->ctx);
|
|---|
| 125 | digest_size = EVP_MD_CTX_size(&temp_ctx);
|
|---|
| 126 | EVP_DigestFinal(&temp_ctx, digest, NULL);
|
|---|
| 127 |
|
|---|
| 128 | EVP_MD_CTX_cleanup(&temp_ctx);
|
|---|
| 129 |
|
|---|
| 130 | /* Create a new string */
|
|---|
| 131 | /* NOTE: not thread safe! modifying an already created string object */
|
|---|
| 132 | /* (not a problem because we hold the GIL by default) */
|
|---|
| 133 | retval = PyString_FromStringAndSize(NULL, digest_size * 2);
|
|---|
| 134 | if (!retval)
|
|---|
| 135 | return NULL;
|
|---|
| 136 | hex_digest = PyString_AsString(retval);
|
|---|
| 137 | if (!hex_digest) {
|
|---|
| 138 | Py_DECREF(retval);
|
|---|
| 139 | return NULL;
|
|---|
| 140 | }
|
|---|
| 141 |
|
|---|
| 142 | /* Make hex version of the digest */
|
|---|
| 143 | for(i=j=0; i<digest_size; i++) {
|
|---|
| 144 | char c;
|
|---|
| 145 | c = (digest[i] >> 4) & 0xf;
|
|---|
| 146 | c = (c>9) ? c+'a'-10 : c + '0';
|
|---|
| 147 | hex_digest[j++] = c;
|
|---|
| 148 | c = (digest[i] & 0xf);
|
|---|
| 149 | c = (c>9) ? c+'a'-10 : c + '0';
|
|---|
| 150 | hex_digest[j++] = c;
|
|---|
| 151 | }
|
|---|
| 152 | return retval;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | PyDoc_STRVAR(EVP_update__doc__,
|
|---|
| 156 | "Update this hash object's state with the provided string.");
|
|---|
| 157 |
|
|---|
| 158 | static PyObject *
|
|---|
| 159 | EVP_update(EVPobject *self, PyObject *args)
|
|---|
| 160 | {
|
|---|
| 161 | unsigned char *cp;
|
|---|
| 162 | Py_ssize_t len;
|
|---|
| 163 |
|
|---|
| 164 | if (!PyArg_ParseTuple(args, "s#:update", &cp, &len))
|
|---|
| 165 | return NULL;
|
|---|
| 166 |
|
|---|
| 167 | EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
|
|---|
| 168 | unsigned int));
|
|---|
| 169 |
|
|---|
| 170 | Py_INCREF(Py_None);
|
|---|
| 171 | return Py_None;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | static PyMethodDef EVP_methods[] = {
|
|---|
| 175 | {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__},
|
|---|
| 176 | {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__},
|
|---|
| 177 | {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__},
|
|---|
| 178 | {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__},
|
|---|
| 179 | {NULL, NULL} /* sentinel */
|
|---|
| 180 | };
|
|---|
| 181 |
|
|---|
| 182 | static PyObject *
|
|---|
| 183 | EVP_get_block_size(EVPobject *self, void *closure)
|
|---|
| 184 | {
|
|---|
| 185 | return PyInt_FromLong(EVP_MD_CTX_block_size(&((EVPobject *)self)->ctx));
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | static PyObject *
|
|---|
| 189 | EVP_get_digest_size(EVPobject *self, void *closure)
|
|---|
| 190 | {
|
|---|
| 191 | return PyInt_FromLong(EVP_MD_CTX_size(&((EVPobject *)self)->ctx));
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | static PyMemberDef EVP_members[] = {
|
|---|
| 195 | {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")},
|
|---|
| 196 | {NULL} /* Sentinel */
|
|---|
| 197 | };
|
|---|
| 198 |
|
|---|
| 199 | static PyGetSetDef EVP_getseters[] = {
|
|---|
| 200 | {"digest_size",
|
|---|
| 201 | (getter)EVP_get_digest_size, NULL,
|
|---|
| 202 | NULL,
|
|---|
| 203 | NULL},
|
|---|
| 204 | {"block_size",
|
|---|
| 205 | (getter)EVP_get_block_size, NULL,
|
|---|
| 206 | NULL,
|
|---|
| 207 | NULL},
|
|---|
| 208 | /* the old md5 and sha modules support 'digest_size' as in PEP 247.
|
|---|
| 209 | * the old sha module also supported 'digestsize'. ugh. */
|
|---|
| 210 | {"digestsize",
|
|---|
| 211 | (getter)EVP_get_digest_size, NULL,
|
|---|
| 212 | NULL,
|
|---|
| 213 | NULL},
|
|---|
| 214 | {NULL} /* Sentinel */
|
|---|
| 215 | };
|
|---|
| 216 |
|
|---|
| 217 |
|
|---|
| 218 | static PyObject *
|
|---|
| 219 | EVP_repr(PyObject *self)
|
|---|
| 220 | {
|
|---|
| 221 | char buf[100];
|
|---|
| 222 | PyOS_snprintf(buf, sizeof(buf), "<%s HASH object @ %p>",
|
|---|
| 223 | PyString_AsString(((EVPobject *)self)->name), self);
|
|---|
| 224 | return PyString_FromString(buf);
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | #if HASH_OBJ_CONSTRUCTOR
|
|---|
| 228 | static int
|
|---|
| 229 | EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds)
|
|---|
| 230 | {
|
|---|
| 231 | static char *kwlist[] = {"name", "string", NULL};
|
|---|
| 232 | PyObject *name_obj = NULL;
|
|---|
| 233 | char *nameStr;
|
|---|
| 234 | unsigned char *cp = NULL;
|
|---|
| 235 | Py_ssize_t len = 0;
|
|---|
| 236 | const EVP_MD *digest;
|
|---|
| 237 |
|
|---|
| 238 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|s#:HASH", kwlist,
|
|---|
| 239 | &name_obj, &cp, &len)) {
|
|---|
| 240 | return -1;
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | if (!PyArg_Parse(name_obj, "s", &nameStr)) {
|
|---|
| 244 | PyErr_SetString(PyExc_TypeError, "name must be a string");
|
|---|
| 245 | return -1;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | digest = EVP_get_digestbyname(nameStr);
|
|---|
| 249 | if (!digest) {
|
|---|
| 250 | PyErr_SetString(PyExc_ValueError, "unknown hash function");
|
|---|
| 251 | return -1;
|
|---|
| 252 | }
|
|---|
| 253 | EVP_DigestInit(&self->ctx, digest);
|
|---|
| 254 |
|
|---|
| 255 | self->name = name_obj;
|
|---|
| 256 | Py_INCREF(self->name);
|
|---|
| 257 |
|
|---|
| 258 | if (cp && len)
|
|---|
| 259 | EVP_DigestUpdate(&self->ctx, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
|
|---|
| 260 | unsigned int));
|
|---|
| 261 |
|
|---|
| 262 | return 0;
|
|---|
| 263 | }
|
|---|
| 264 | #endif
|
|---|
| 265 |
|
|---|
| 266 |
|
|---|
| 267 | PyDoc_STRVAR(hashtype_doc,
|
|---|
| 268 | "A hash represents the object used to calculate a checksum of a\n\
|
|---|
| 269 | string of information.\n\
|
|---|
| 270 | \n\
|
|---|
| 271 | Methods:\n\
|
|---|
| 272 | \n\
|
|---|
| 273 | update() -- updates the current digest with an additional string\n\
|
|---|
| 274 | digest() -- return the current digest value\n\
|
|---|
| 275 | hexdigest() -- return the current digest as a string of hexadecimal digits\n\
|
|---|
| 276 | copy() -- return a copy of the current hash object\n\
|
|---|
| 277 | \n\
|
|---|
| 278 | Attributes:\n\
|
|---|
| 279 | \n\
|
|---|
| 280 | name -- the hash algorithm being used by this object\n\
|
|---|
| 281 | digest_size -- number of bytes in this hashes output\n");
|
|---|
| 282 |
|
|---|
| 283 | static PyTypeObject EVPtype = {
|
|---|
| 284 | PyObject_HEAD_INIT(NULL)
|
|---|
| 285 | 0, /*ob_size*/
|
|---|
| 286 | "_hashlib.HASH", /*tp_name*/
|
|---|
| 287 | sizeof(EVPobject), /*tp_basicsize*/
|
|---|
| 288 | 0, /*tp_itemsize*/
|
|---|
| 289 | /* methods */
|
|---|
| 290 | EVP_dealloc, /*tp_dealloc*/
|
|---|
| 291 | 0, /*tp_print*/
|
|---|
| 292 | 0, /*tp_getattr*/
|
|---|
| 293 | 0, /*tp_setattr*/
|
|---|
| 294 | 0, /*tp_compare*/
|
|---|
| 295 | EVP_repr, /*tp_repr*/
|
|---|
| 296 | 0, /*tp_as_number*/
|
|---|
| 297 | 0, /*tp_as_sequence*/
|
|---|
| 298 | 0, /*tp_as_mapping*/
|
|---|
| 299 | 0, /*tp_hash*/
|
|---|
| 300 | 0, /*tp_call*/
|
|---|
| 301 | 0, /*tp_str*/
|
|---|
| 302 | 0, /*tp_getattro*/
|
|---|
| 303 | 0, /*tp_setattro*/
|
|---|
| 304 | 0, /*tp_as_buffer*/
|
|---|
| 305 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
|---|
| 306 | hashtype_doc, /*tp_doc*/
|
|---|
| 307 | 0, /*tp_traverse*/
|
|---|
| 308 | 0, /*tp_clear*/
|
|---|
| 309 | 0, /*tp_richcompare*/
|
|---|
| 310 | 0, /*tp_weaklistoffset*/
|
|---|
| 311 | 0, /*tp_iter*/
|
|---|
| 312 | 0, /*tp_iternext*/
|
|---|
| 313 | EVP_methods, /* tp_methods */
|
|---|
| 314 | EVP_members, /* tp_members */
|
|---|
| 315 | EVP_getseters, /* tp_getset */
|
|---|
| 316 | #if 1
|
|---|
| 317 | 0, /* tp_base */
|
|---|
| 318 | 0, /* tp_dict */
|
|---|
| 319 | 0, /* tp_descr_get */
|
|---|
| 320 | 0, /* tp_descr_set */
|
|---|
| 321 | 0, /* tp_dictoffset */
|
|---|
| 322 | #endif
|
|---|
| 323 | #if HASH_OBJ_CONSTRUCTOR
|
|---|
| 324 | (initproc)EVP_tp_init, /* tp_init */
|
|---|
| 325 | #endif
|
|---|
| 326 | };
|
|---|
| 327 |
|
|---|
| 328 | static PyObject *
|
|---|
| 329 | EVPnew(PyObject *name_obj,
|
|---|
| 330 | const EVP_MD *digest, const EVP_MD_CTX *initial_ctx,
|
|---|
| 331 | const unsigned char *cp, unsigned int len)
|
|---|
| 332 | {
|
|---|
| 333 | EVPobject *self;
|
|---|
| 334 |
|
|---|
| 335 | if (!digest && !initial_ctx) {
|
|---|
| 336 | PyErr_SetString(PyExc_ValueError, "unsupported hash type");
|
|---|
| 337 | return NULL;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | if ((self = newEVPobject(name_obj)) == NULL)
|
|---|
| 341 | return NULL;
|
|---|
| 342 |
|
|---|
| 343 | if (initial_ctx) {
|
|---|
| 344 | EVP_MD_CTX_copy(&self->ctx, initial_ctx);
|
|---|
| 345 | } else {
|
|---|
| 346 | EVP_DigestInit(&self->ctx, digest);
|
|---|
| 347 | }
|
|---|
| 348 |
|
|---|
| 349 | if (cp && len)
|
|---|
| 350 | EVP_DigestUpdate(&self->ctx, cp, len);
|
|---|
| 351 |
|
|---|
| 352 | return (PyObject *)self;
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 |
|
|---|
| 356 | /* The module-level function: new() */
|
|---|
| 357 |
|
|---|
| 358 | PyDoc_STRVAR(EVP_new__doc__,
|
|---|
| 359 | "Return a new hash object using the named algorithm.\n\
|
|---|
| 360 | An optional string argument may be provided and will be\n\
|
|---|
| 361 | automatically hashed.\n\
|
|---|
| 362 | \n\
|
|---|
| 363 | The MD5 and SHA1 algorithms are always supported.\n");
|
|---|
| 364 |
|
|---|
| 365 | static PyObject *
|
|---|
| 366 | EVP_new(PyObject *self, PyObject *args, PyObject *kwdict)
|
|---|
| 367 | {
|
|---|
| 368 | static char *kwlist[] = {"name", "string", NULL};
|
|---|
| 369 | PyObject *name_obj = NULL;
|
|---|
| 370 | char *name;
|
|---|
| 371 | const EVP_MD *digest;
|
|---|
| 372 | unsigned char *cp = NULL;
|
|---|
| 373 | Py_ssize_t len = 0;
|
|---|
| 374 |
|
|---|
| 375 | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|s#:new", kwlist,
|
|---|
| 376 | &name_obj, &cp, &len)) {
|
|---|
| 377 | return NULL;
|
|---|
| 378 | }
|
|---|
| 379 |
|
|---|
| 380 | if (!PyArg_Parse(name_obj, "s", &name)) {
|
|---|
| 381 | PyErr_SetString(PyExc_TypeError, "name must be a string");
|
|---|
| 382 | return NULL;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | digest = EVP_get_digestbyname(name);
|
|---|
| 386 |
|
|---|
| 387 | return EVPnew(name_obj, digest, NULL, cp, Py_SAFE_DOWNCAST(len, Py_ssize_t,
|
|---|
| 388 | unsigned int));
|
|---|
| 389 | }
|
|---|
| 390 |
|
|---|
| 391 | /*
|
|---|
| 392 | * This macro generates constructor function definitions for specific
|
|---|
| 393 | * hash algorithms. These constructors are much faster than calling
|
|---|
| 394 | * the generic one passing it a python string and are noticably
|
|---|
| 395 | * faster than calling a python new() wrapper. Thats important for
|
|---|
| 396 | * code that wants to make hashes of a bunch of small strings.
|
|---|
| 397 | */
|
|---|
| 398 | #define GEN_CONSTRUCTOR(NAME) \
|
|---|
| 399 | static PyObject * \
|
|---|
| 400 | EVP_new_ ## NAME (PyObject *self, PyObject *args) \
|
|---|
| 401 | { \
|
|---|
| 402 | unsigned char *cp = NULL; \
|
|---|
| 403 | Py_ssize_t len = 0; \
|
|---|
| 404 | \
|
|---|
| 405 | if (!PyArg_ParseTuple(args, "|s#:" #NAME , &cp, &len)) { \
|
|---|
| 406 | return NULL; \
|
|---|
| 407 | } \
|
|---|
| 408 | \
|
|---|
| 409 | return EVPnew( \
|
|---|
| 410 | CONST_ ## NAME ## _name_obj, \
|
|---|
| 411 | NULL, \
|
|---|
| 412 | CONST_new_ ## NAME ## _ctx_p, \
|
|---|
| 413 | cp, Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int)); \
|
|---|
| 414 | }
|
|---|
| 415 |
|
|---|
| 416 | /* a PyMethodDef structure for the constructor */
|
|---|
| 417 | #define CONSTRUCTOR_METH_DEF(NAME) \
|
|---|
| 418 | {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_VARARGS, \
|
|---|
| 419 | PyDoc_STR("Returns a " #NAME \
|
|---|
| 420 | " hash object; optionally initialized with a string") \
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | /* used in the init function to setup a constructor */
|
|---|
| 424 | #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \
|
|---|
| 425 | CONST_ ## NAME ## _name_obj = PyString_FromString(#NAME); \
|
|---|
| 426 | if (EVP_get_digestbyname(#NAME)) { \
|
|---|
| 427 | CONST_new_ ## NAME ## _ctx_p = &CONST_new_ ## NAME ## _ctx; \
|
|---|
| 428 | EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \
|
|---|
| 429 | } \
|
|---|
| 430 | } while (0);
|
|---|
| 431 |
|
|---|
| 432 | GEN_CONSTRUCTOR(md5)
|
|---|
| 433 | GEN_CONSTRUCTOR(sha1)
|
|---|
| 434 | GEN_CONSTRUCTOR(sha224)
|
|---|
| 435 | GEN_CONSTRUCTOR(sha256)
|
|---|
| 436 | GEN_CONSTRUCTOR(sha384)
|
|---|
| 437 | GEN_CONSTRUCTOR(sha512)
|
|---|
| 438 |
|
|---|
| 439 | /* List of functions exported by this module */
|
|---|
| 440 |
|
|---|
| 441 | static struct PyMethodDef EVP_functions[] = {
|
|---|
| 442 | {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__},
|
|---|
| 443 | CONSTRUCTOR_METH_DEF(md5),
|
|---|
| 444 | CONSTRUCTOR_METH_DEF(sha1),
|
|---|
| 445 | CONSTRUCTOR_METH_DEF(sha224),
|
|---|
| 446 | CONSTRUCTOR_METH_DEF(sha256),
|
|---|
| 447 | CONSTRUCTOR_METH_DEF(sha384),
|
|---|
| 448 | CONSTRUCTOR_METH_DEF(sha512),
|
|---|
| 449 | {NULL, NULL} /* Sentinel */
|
|---|
| 450 | };
|
|---|
| 451 |
|
|---|
| 452 |
|
|---|
| 453 | /* Initialize this module. */
|
|---|
| 454 |
|
|---|
| 455 | PyMODINIT_FUNC
|
|---|
| 456 | init_hashlib(void)
|
|---|
| 457 | {
|
|---|
| 458 | PyObject *m;
|
|---|
| 459 |
|
|---|
| 460 | OpenSSL_add_all_digests();
|
|---|
| 461 |
|
|---|
| 462 | /* TODO build EVP_functions openssl_* entries dynamically based
|
|---|
| 463 | * on what hashes are supported rather than listing many
|
|---|
| 464 | * but having some be unsupported. Only init appropriate
|
|---|
| 465 | * constants. */
|
|---|
| 466 |
|
|---|
| 467 | EVPtype.ob_type = &PyType_Type;
|
|---|
| 468 | if (PyType_Ready(&EVPtype) < 0)
|
|---|
| 469 | return;
|
|---|
| 470 |
|
|---|
| 471 | m = Py_InitModule("_hashlib", EVP_functions);
|
|---|
| 472 | if (m == NULL)
|
|---|
| 473 | return;
|
|---|
| 474 |
|
|---|
| 475 | #if HASH_OBJ_CONSTRUCTOR
|
|---|
| 476 | Py_INCREF(&EVPtype);
|
|---|
| 477 | PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype);
|
|---|
| 478 | #endif
|
|---|
| 479 |
|
|---|
| 480 | /* these constants are used by the convenience constructors */
|
|---|
| 481 | INIT_CONSTRUCTOR_CONSTANTS(md5);
|
|---|
| 482 | INIT_CONSTRUCTOR_CONSTANTS(sha1);
|
|---|
| 483 | INIT_CONSTRUCTOR_CONSTANTS(sha224);
|
|---|
| 484 | INIT_CONSTRUCTOR_CONSTANTS(sha256);
|
|---|
| 485 | INIT_CONSTRUCTOR_CONSTANTS(sha384);
|
|---|
| 486 | INIT_CONSTRUCTOR_CONSTANTS(sha512);
|
|---|
| 487 | }
|
|---|