| 1 | n/a | /* Module that wraps all OpenSSL hash algorithms */ |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | /* |
|---|
| 4 | n/a | * Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org) |
|---|
| 5 | n/a | * Licensed to PSF under a Contributor Agreement. |
|---|
| 6 | n/a | * |
|---|
| 7 | n/a | * Derived from a skeleton of shamodule.c containing work performed by: |
|---|
| 8 | n/a | * |
|---|
| 9 | n/a | * Andrew Kuchling (amk@amk.ca) |
|---|
| 10 | n/a | * Greg Stein (gstein@lyra.org) |
|---|
| 11 | n/a | * |
|---|
| 12 | n/a | */ |
|---|
| 13 | n/a | |
|---|
| 14 | n/a | #define PY_SSIZE_T_CLEAN |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | #include "Python.h" |
|---|
| 17 | n/a | #include "structmember.h" |
|---|
| 18 | n/a | #include "hashlib.h" |
|---|
| 19 | n/a | #include "pystrhex.h" |
|---|
| 20 | n/a | |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | /* EVP is the preferred interface to hashing in OpenSSL */ |
|---|
| 23 | n/a | #include <openssl/evp.h> |
|---|
| 24 | n/a | /* We use the object interface to discover what hashes OpenSSL supports. */ |
|---|
| 25 | n/a | #include <openssl/objects.h> |
|---|
| 26 | n/a | #include "openssl/err.h" |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | #include "clinic/_hashopenssl.c.h" |
|---|
| 29 | n/a | /*[clinic input] |
|---|
| 30 | n/a | module _hashlib |
|---|
| 31 | n/a | [clinic start generated code]*/ |
|---|
| 32 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c2b4ff081bac4be1]*/ |
|---|
| 33 | n/a | |
|---|
| 34 | n/a | #define MUNCH_SIZE INT_MAX |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #ifndef HASH_OBJ_CONSTRUCTOR |
|---|
| 37 | n/a | #define HASH_OBJ_CONSTRUCTOR 0 |
|---|
| 38 | n/a | #endif |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER) |
|---|
| 41 | n/a | /* OpenSSL < 1.1.0 */ |
|---|
| 42 | n/a | #define EVP_MD_CTX_new EVP_MD_CTX_create |
|---|
| 43 | n/a | #define EVP_MD_CTX_free EVP_MD_CTX_destroy |
|---|
| 44 | n/a | #define HAS_FAST_PKCS5_PBKDF2_HMAC 0 |
|---|
| 45 | n/a | #include <openssl/hmac.h> |
|---|
| 46 | n/a | #else |
|---|
| 47 | n/a | /* OpenSSL >= 1.1.0 */ |
|---|
| 48 | n/a | #define HAS_FAST_PKCS5_PBKDF2_HMAC 1 |
|---|
| 49 | n/a | #endif |
|---|
| 50 | n/a | |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | typedef struct { |
|---|
| 53 | n/a | PyObject_HEAD |
|---|
| 54 | n/a | PyObject *name; /* name of this hash algorithm */ |
|---|
| 55 | n/a | EVP_MD_CTX *ctx; /* OpenSSL message digest context */ |
|---|
| 56 | n/a | #ifdef WITH_THREAD |
|---|
| 57 | n/a | PyThread_type_lock lock; /* OpenSSL context lock */ |
|---|
| 58 | n/a | #endif |
|---|
| 59 | n/a | } EVPobject; |
|---|
| 60 | n/a | |
|---|
| 61 | n/a | |
|---|
| 62 | n/a | static PyTypeObject EVPtype; |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | #define DEFINE_CONSTS_FOR_NEW(Name) \ |
|---|
| 66 | n/a | static PyObject *CONST_ ## Name ## _name_obj = NULL; \ |
|---|
| 67 | n/a | static EVP_MD_CTX *CONST_new_ ## Name ## _ctx_p = NULL; |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | DEFINE_CONSTS_FOR_NEW(md5) |
|---|
| 70 | n/a | DEFINE_CONSTS_FOR_NEW(sha1) |
|---|
| 71 | n/a | DEFINE_CONSTS_FOR_NEW(sha224) |
|---|
| 72 | n/a | DEFINE_CONSTS_FOR_NEW(sha256) |
|---|
| 73 | n/a | DEFINE_CONSTS_FOR_NEW(sha384) |
|---|
| 74 | n/a | DEFINE_CONSTS_FOR_NEW(sha512) |
|---|
| 75 | n/a | |
|---|
| 76 | n/a | |
|---|
| 77 | n/a | /* LCOV_EXCL_START */ |
|---|
| 78 | n/a | static PyObject * |
|---|
| 79 | n/a | _setException(PyObject *exc) |
|---|
| 80 | n/a | { |
|---|
| 81 | n/a | unsigned long errcode; |
|---|
| 82 | n/a | const char *lib, *func, *reason; |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | errcode = ERR_peek_last_error(); |
|---|
| 85 | n/a | if (!errcode) { |
|---|
| 86 | n/a | PyErr_SetString(exc, "unknown reasons"); |
|---|
| 87 | n/a | return NULL; |
|---|
| 88 | n/a | } |
|---|
| 89 | n/a | ERR_clear_error(); |
|---|
| 90 | n/a | |
|---|
| 91 | n/a | lib = ERR_lib_error_string(errcode); |
|---|
| 92 | n/a | func = ERR_func_error_string(errcode); |
|---|
| 93 | n/a | reason = ERR_reason_error_string(errcode); |
|---|
| 94 | n/a | |
|---|
| 95 | n/a | if (lib && func) { |
|---|
| 96 | n/a | PyErr_Format(exc, "[%s: %s] %s", lib, func, reason); |
|---|
| 97 | n/a | } |
|---|
| 98 | n/a | else if (lib) { |
|---|
| 99 | n/a | PyErr_Format(exc, "[%s] %s", lib, reason); |
|---|
| 100 | n/a | } |
|---|
| 101 | n/a | else { |
|---|
| 102 | n/a | PyErr_SetString(exc, reason); |
|---|
| 103 | n/a | } |
|---|
| 104 | n/a | return NULL; |
|---|
| 105 | n/a | } |
|---|
| 106 | n/a | /* LCOV_EXCL_STOP */ |
|---|
| 107 | n/a | |
|---|
| 108 | n/a | static EVPobject * |
|---|
| 109 | n/a | newEVPobject(PyObject *name) |
|---|
| 110 | n/a | { |
|---|
| 111 | n/a | EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, &EVPtype); |
|---|
| 112 | n/a | if (retval == NULL) { |
|---|
| 113 | n/a | return NULL; |
|---|
| 114 | n/a | } |
|---|
| 115 | n/a | |
|---|
| 116 | n/a | retval->ctx = EVP_MD_CTX_new(); |
|---|
| 117 | n/a | if (retval->ctx == NULL) { |
|---|
| 118 | n/a | PyErr_NoMemory(); |
|---|
| 119 | n/a | return NULL; |
|---|
| 120 | n/a | } |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | /* save the name for .name to return */ |
|---|
| 123 | n/a | Py_INCREF(name); |
|---|
| 124 | n/a | retval->name = name; |
|---|
| 125 | n/a | #ifdef WITH_THREAD |
|---|
| 126 | n/a | retval->lock = NULL; |
|---|
| 127 | n/a | #endif |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | return retval; |
|---|
| 130 | n/a | } |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | static void |
|---|
| 133 | n/a | EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len) |
|---|
| 134 | n/a | { |
|---|
| 135 | n/a | unsigned int process; |
|---|
| 136 | n/a | const unsigned char *cp = (const unsigned char *)vp; |
|---|
| 137 | n/a | while (0 < len) { |
|---|
| 138 | n/a | if (len > (Py_ssize_t)MUNCH_SIZE) |
|---|
| 139 | n/a | process = MUNCH_SIZE; |
|---|
| 140 | n/a | else |
|---|
| 141 | n/a | process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int); |
|---|
| 142 | n/a | EVP_DigestUpdate(self->ctx, (const void*)cp, process); |
|---|
| 143 | n/a | len -= process; |
|---|
| 144 | n/a | cp += process; |
|---|
| 145 | n/a | } |
|---|
| 146 | n/a | } |
|---|
| 147 | n/a | |
|---|
| 148 | n/a | /* Internal methods for a hash object */ |
|---|
| 149 | n/a | |
|---|
| 150 | n/a | static void |
|---|
| 151 | n/a | EVP_dealloc(EVPobject *self) |
|---|
| 152 | n/a | { |
|---|
| 153 | n/a | #ifdef WITH_THREAD |
|---|
| 154 | n/a | if (self->lock != NULL) |
|---|
| 155 | n/a | PyThread_free_lock(self->lock); |
|---|
| 156 | n/a | #endif |
|---|
| 157 | n/a | EVP_MD_CTX_free(self->ctx); |
|---|
| 158 | n/a | Py_XDECREF(self->name); |
|---|
| 159 | n/a | PyObject_Del(self); |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | static int |
|---|
| 163 | n/a | locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self) |
|---|
| 164 | n/a | { |
|---|
| 165 | n/a | int result; |
|---|
| 166 | n/a | ENTER_HASHLIB(self); |
|---|
| 167 | n/a | result = EVP_MD_CTX_copy(new_ctx_p, self->ctx); |
|---|
| 168 | n/a | LEAVE_HASHLIB(self); |
|---|
| 169 | n/a | return result; |
|---|
| 170 | n/a | } |
|---|
| 171 | n/a | |
|---|
| 172 | n/a | /* External methods for a hash object */ |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | PyDoc_STRVAR(EVP_copy__doc__, "Return a copy of the hash object."); |
|---|
| 175 | n/a | |
|---|
| 176 | n/a | |
|---|
| 177 | n/a | static PyObject * |
|---|
| 178 | n/a | EVP_copy(EVPobject *self, PyObject *unused) |
|---|
| 179 | n/a | { |
|---|
| 180 | n/a | EVPobject *newobj; |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | if ( (newobj = newEVPobject(self->name))==NULL) |
|---|
| 183 | n/a | return NULL; |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) { |
|---|
| 186 | n/a | return _setException(PyExc_ValueError); |
|---|
| 187 | n/a | } |
|---|
| 188 | n/a | return (PyObject *)newobj; |
|---|
| 189 | n/a | } |
|---|
| 190 | n/a | |
|---|
| 191 | n/a | PyDoc_STRVAR(EVP_digest__doc__, |
|---|
| 192 | n/a | "Return the digest value as a string of binary data."); |
|---|
| 193 | n/a | |
|---|
| 194 | n/a | static PyObject * |
|---|
| 195 | n/a | EVP_digest(EVPobject *self, PyObject *unused) |
|---|
| 196 | n/a | { |
|---|
| 197 | n/a | unsigned char digest[EVP_MAX_MD_SIZE]; |
|---|
| 198 | n/a | EVP_MD_CTX *temp_ctx; |
|---|
| 199 | n/a | PyObject *retval; |
|---|
| 200 | n/a | unsigned int digest_size; |
|---|
| 201 | n/a | |
|---|
| 202 | n/a | temp_ctx = EVP_MD_CTX_new(); |
|---|
| 203 | n/a | if (temp_ctx == NULL) { |
|---|
| 204 | n/a | PyErr_NoMemory(); |
|---|
| 205 | n/a | return NULL; |
|---|
| 206 | n/a | } |
|---|
| 207 | n/a | |
|---|
| 208 | n/a | if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) { |
|---|
| 209 | n/a | return _setException(PyExc_ValueError); |
|---|
| 210 | n/a | } |
|---|
| 211 | n/a | digest_size = EVP_MD_CTX_size(temp_ctx); |
|---|
| 212 | n/a | EVP_DigestFinal(temp_ctx, digest, NULL); |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | retval = PyBytes_FromStringAndSize((const char *)digest, digest_size); |
|---|
| 215 | n/a | EVP_MD_CTX_free(temp_ctx); |
|---|
| 216 | n/a | return retval; |
|---|
| 217 | n/a | } |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | PyDoc_STRVAR(EVP_hexdigest__doc__, |
|---|
| 220 | n/a | "Return the digest value as a string of hexadecimal digits."); |
|---|
| 221 | n/a | |
|---|
| 222 | n/a | static PyObject * |
|---|
| 223 | n/a | EVP_hexdigest(EVPobject *self, PyObject *unused) |
|---|
| 224 | n/a | { |
|---|
| 225 | n/a | unsigned char digest[EVP_MAX_MD_SIZE]; |
|---|
| 226 | n/a | EVP_MD_CTX *temp_ctx; |
|---|
| 227 | n/a | unsigned int digest_size; |
|---|
| 228 | n/a | |
|---|
| 229 | n/a | temp_ctx = EVP_MD_CTX_new(); |
|---|
| 230 | n/a | if (temp_ctx == NULL) { |
|---|
| 231 | n/a | PyErr_NoMemory(); |
|---|
| 232 | n/a | return NULL; |
|---|
| 233 | n/a | } |
|---|
| 234 | n/a | |
|---|
| 235 | n/a | /* Get the raw (binary) digest value */ |
|---|
| 236 | n/a | if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) { |
|---|
| 237 | n/a | return _setException(PyExc_ValueError); |
|---|
| 238 | n/a | } |
|---|
| 239 | n/a | digest_size = EVP_MD_CTX_size(temp_ctx); |
|---|
| 240 | n/a | EVP_DigestFinal(temp_ctx, digest, NULL); |
|---|
| 241 | n/a | |
|---|
| 242 | n/a | EVP_MD_CTX_free(temp_ctx); |
|---|
| 243 | n/a | |
|---|
| 244 | n/a | return _Py_strhex((const char *)digest, digest_size); |
|---|
| 245 | n/a | } |
|---|
| 246 | n/a | |
|---|
| 247 | n/a | PyDoc_STRVAR(EVP_update__doc__, |
|---|
| 248 | n/a | "Update this hash object's state with the provided string."); |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | static PyObject * |
|---|
| 251 | n/a | EVP_update(EVPobject *self, PyObject *args) |
|---|
| 252 | n/a | { |
|---|
| 253 | n/a | PyObject *obj; |
|---|
| 254 | n/a | Py_buffer view; |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | if (!PyArg_ParseTuple(args, "O:update", &obj)) |
|---|
| 257 | n/a | return NULL; |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | GET_BUFFER_VIEW_OR_ERROUT(obj, &view); |
|---|
| 260 | n/a | |
|---|
| 261 | n/a | #ifdef WITH_THREAD |
|---|
| 262 | n/a | if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) { |
|---|
| 263 | n/a | self->lock = PyThread_allocate_lock(); |
|---|
| 264 | n/a | /* fail? lock = NULL and we fail over to non-threaded code. */ |
|---|
| 265 | n/a | } |
|---|
| 266 | n/a | |
|---|
| 267 | n/a | if (self->lock != NULL) { |
|---|
| 268 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 269 | n/a | PyThread_acquire_lock(self->lock, 1); |
|---|
| 270 | n/a | EVP_hash(self, view.buf, view.len); |
|---|
| 271 | n/a | PyThread_release_lock(self->lock); |
|---|
| 272 | n/a | Py_END_ALLOW_THREADS |
|---|
| 273 | n/a | } else { |
|---|
| 274 | n/a | EVP_hash(self, view.buf, view.len); |
|---|
| 275 | n/a | } |
|---|
| 276 | n/a | #else |
|---|
| 277 | n/a | EVP_hash(self, view.buf, view.len); |
|---|
| 278 | n/a | #endif |
|---|
| 279 | n/a | |
|---|
| 280 | n/a | PyBuffer_Release(&view); |
|---|
| 281 | n/a | Py_RETURN_NONE; |
|---|
| 282 | n/a | } |
|---|
| 283 | n/a | |
|---|
| 284 | n/a | static PyMethodDef EVP_methods[] = { |
|---|
| 285 | n/a | {"update", (PyCFunction)EVP_update, METH_VARARGS, EVP_update__doc__}, |
|---|
| 286 | n/a | {"digest", (PyCFunction)EVP_digest, METH_NOARGS, EVP_digest__doc__}, |
|---|
| 287 | n/a | {"hexdigest", (PyCFunction)EVP_hexdigest, METH_NOARGS, EVP_hexdigest__doc__}, |
|---|
| 288 | n/a | {"copy", (PyCFunction)EVP_copy, METH_NOARGS, EVP_copy__doc__}, |
|---|
| 289 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 290 | n/a | }; |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | static PyObject * |
|---|
| 293 | n/a | EVP_get_block_size(EVPobject *self, void *closure) |
|---|
| 294 | n/a | { |
|---|
| 295 | n/a | long block_size; |
|---|
| 296 | n/a | block_size = EVP_MD_CTX_block_size(self->ctx); |
|---|
| 297 | n/a | return PyLong_FromLong(block_size); |
|---|
| 298 | n/a | } |
|---|
| 299 | n/a | |
|---|
| 300 | n/a | static PyObject * |
|---|
| 301 | n/a | EVP_get_digest_size(EVPobject *self, void *closure) |
|---|
| 302 | n/a | { |
|---|
| 303 | n/a | long size; |
|---|
| 304 | n/a | size = EVP_MD_CTX_size(self->ctx); |
|---|
| 305 | n/a | return PyLong_FromLong(size); |
|---|
| 306 | n/a | } |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | static PyMemberDef EVP_members[] = { |
|---|
| 309 | n/a | {"name", T_OBJECT, offsetof(EVPobject, name), READONLY, PyDoc_STR("algorithm name.")}, |
|---|
| 310 | n/a | {NULL} /* Sentinel */ |
|---|
| 311 | n/a | }; |
|---|
| 312 | n/a | |
|---|
| 313 | n/a | static PyGetSetDef EVP_getseters[] = { |
|---|
| 314 | n/a | {"digest_size", |
|---|
| 315 | n/a | (getter)EVP_get_digest_size, NULL, |
|---|
| 316 | n/a | NULL, |
|---|
| 317 | n/a | NULL}, |
|---|
| 318 | n/a | {"block_size", |
|---|
| 319 | n/a | (getter)EVP_get_block_size, NULL, |
|---|
| 320 | n/a | NULL, |
|---|
| 321 | n/a | NULL}, |
|---|
| 322 | n/a | {NULL} /* Sentinel */ |
|---|
| 323 | n/a | }; |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | static PyObject * |
|---|
| 327 | n/a | EVP_repr(EVPobject *self) |
|---|
| 328 | n/a | { |
|---|
| 329 | n/a | return PyUnicode_FromFormat("<%U HASH object @ %p>", self->name, self); |
|---|
| 330 | n/a | } |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | #if HASH_OBJ_CONSTRUCTOR |
|---|
| 333 | n/a | static int |
|---|
| 334 | n/a | EVP_tp_init(EVPobject *self, PyObject *args, PyObject *kwds) |
|---|
| 335 | n/a | { |
|---|
| 336 | n/a | static char *kwlist[] = {"name", "string", NULL}; |
|---|
| 337 | n/a | PyObject *name_obj = NULL; |
|---|
| 338 | n/a | PyObject *data_obj = NULL; |
|---|
| 339 | n/a | Py_buffer view; |
|---|
| 340 | n/a | char *nameStr; |
|---|
| 341 | n/a | const EVP_MD *digest; |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|O:HASH", kwlist, |
|---|
| 344 | n/a | &name_obj, &data_obj)) { |
|---|
| 345 | n/a | return -1; |
|---|
| 346 | n/a | } |
|---|
| 347 | n/a | |
|---|
| 348 | n/a | if (data_obj) |
|---|
| 349 | n/a | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | if (!PyArg_Parse(name_obj, "s", &nameStr)) { |
|---|
| 352 | n/a | PyErr_SetString(PyExc_TypeError, "name must be a string"); |
|---|
| 353 | n/a | if (data_obj) |
|---|
| 354 | n/a | PyBuffer_Release(&view); |
|---|
| 355 | n/a | return -1; |
|---|
| 356 | n/a | } |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | digest = EVP_get_digestbyname(nameStr); |
|---|
| 359 | n/a | if (!digest) { |
|---|
| 360 | n/a | PyErr_SetString(PyExc_ValueError, "unknown hash function"); |
|---|
| 361 | n/a | if (data_obj) |
|---|
| 362 | n/a | PyBuffer_Release(&view); |
|---|
| 363 | n/a | return -1; |
|---|
| 364 | n/a | } |
|---|
| 365 | n/a | EVP_DigestInit(self->ctx, digest); |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | self->name = name_obj; |
|---|
| 368 | n/a | Py_INCREF(self->name); |
|---|
| 369 | n/a | |
|---|
| 370 | n/a | if (data_obj) { |
|---|
| 371 | n/a | if (view.len >= HASHLIB_GIL_MINSIZE) { |
|---|
| 372 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 373 | n/a | EVP_hash(self, view.buf, view.len); |
|---|
| 374 | n/a | Py_END_ALLOW_THREADS |
|---|
| 375 | n/a | } else { |
|---|
| 376 | n/a | EVP_hash(self, view.buf, view.len); |
|---|
| 377 | n/a | } |
|---|
| 378 | n/a | PyBuffer_Release(&view); |
|---|
| 379 | n/a | } |
|---|
| 380 | n/a | |
|---|
| 381 | n/a | return 0; |
|---|
| 382 | n/a | } |
|---|
| 383 | n/a | #endif |
|---|
| 384 | n/a | |
|---|
| 385 | n/a | |
|---|
| 386 | n/a | PyDoc_STRVAR(hashtype_doc, |
|---|
| 387 | n/a | "A hash represents the object used to calculate a checksum of a\n\ |
|---|
| 388 | n/a | string of information.\n\ |
|---|
| 389 | n/a | \n\ |
|---|
| 390 | n/a | Methods:\n\ |
|---|
| 391 | n/a | \n\ |
|---|
| 392 | n/a | update() -- updates the current digest with an additional string\n\ |
|---|
| 393 | n/a | digest() -- return the current digest value\n\ |
|---|
| 394 | n/a | hexdigest() -- return the current digest as a string of hexadecimal digits\n\ |
|---|
| 395 | n/a | copy() -- return a copy of the current hash object\n\ |
|---|
| 396 | n/a | \n\ |
|---|
| 397 | n/a | Attributes:\n\ |
|---|
| 398 | n/a | \n\ |
|---|
| 399 | n/a | name -- the hash algorithm being used by this object\n\ |
|---|
| 400 | n/a | digest_size -- number of bytes in this hashes output\n"); |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | static PyTypeObject EVPtype = { |
|---|
| 403 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 404 | n/a | "_hashlib.HASH", /*tp_name*/ |
|---|
| 405 | n/a | sizeof(EVPobject), /*tp_basicsize*/ |
|---|
| 406 | n/a | 0, /*tp_itemsize*/ |
|---|
| 407 | n/a | /* methods */ |
|---|
| 408 | n/a | (destructor)EVP_dealloc, /*tp_dealloc*/ |
|---|
| 409 | n/a | 0, /*tp_print*/ |
|---|
| 410 | n/a | 0, /*tp_getattr*/ |
|---|
| 411 | n/a | 0, /*tp_setattr*/ |
|---|
| 412 | n/a | 0, /*tp_reserved*/ |
|---|
| 413 | n/a | (reprfunc)EVP_repr, /*tp_repr*/ |
|---|
| 414 | n/a | 0, /*tp_as_number*/ |
|---|
| 415 | n/a | 0, /*tp_as_sequence*/ |
|---|
| 416 | n/a | 0, /*tp_as_mapping*/ |
|---|
| 417 | n/a | 0, /*tp_hash*/ |
|---|
| 418 | n/a | 0, /*tp_call*/ |
|---|
| 419 | n/a | 0, /*tp_str*/ |
|---|
| 420 | n/a | 0, /*tp_getattro*/ |
|---|
| 421 | n/a | 0, /*tp_setattro*/ |
|---|
| 422 | n/a | 0, /*tp_as_buffer*/ |
|---|
| 423 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/ |
|---|
| 424 | n/a | hashtype_doc, /*tp_doc*/ |
|---|
| 425 | n/a | 0, /*tp_traverse*/ |
|---|
| 426 | n/a | 0, /*tp_clear*/ |
|---|
| 427 | n/a | 0, /*tp_richcompare*/ |
|---|
| 428 | n/a | 0, /*tp_weaklistoffset*/ |
|---|
| 429 | n/a | 0, /*tp_iter*/ |
|---|
| 430 | n/a | 0, /*tp_iternext*/ |
|---|
| 431 | n/a | EVP_methods, /* tp_methods */ |
|---|
| 432 | n/a | EVP_members, /* tp_members */ |
|---|
| 433 | n/a | EVP_getseters, /* tp_getset */ |
|---|
| 434 | n/a | #if 1 |
|---|
| 435 | n/a | 0, /* tp_base */ |
|---|
| 436 | n/a | 0, /* tp_dict */ |
|---|
| 437 | n/a | 0, /* tp_descr_get */ |
|---|
| 438 | n/a | 0, /* tp_descr_set */ |
|---|
| 439 | n/a | 0, /* tp_dictoffset */ |
|---|
| 440 | n/a | #endif |
|---|
| 441 | n/a | #if HASH_OBJ_CONSTRUCTOR |
|---|
| 442 | n/a | (initproc)EVP_tp_init, /* tp_init */ |
|---|
| 443 | n/a | #endif |
|---|
| 444 | n/a | }; |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | static PyObject * |
|---|
| 447 | n/a | EVPnew(PyObject *name_obj, |
|---|
| 448 | n/a | const EVP_MD *digest, const EVP_MD_CTX *initial_ctx, |
|---|
| 449 | n/a | const unsigned char *cp, Py_ssize_t len) |
|---|
| 450 | n/a | { |
|---|
| 451 | n/a | EVPobject *self; |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | if (!digest && !initial_ctx) { |
|---|
| 454 | n/a | PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
|---|
| 455 | n/a | return NULL; |
|---|
| 456 | n/a | } |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | if ((self = newEVPobject(name_obj)) == NULL) |
|---|
| 459 | n/a | return NULL; |
|---|
| 460 | n/a | |
|---|
| 461 | n/a | if (initial_ctx) { |
|---|
| 462 | n/a | EVP_MD_CTX_copy(self->ctx, initial_ctx); |
|---|
| 463 | n/a | } else { |
|---|
| 464 | n/a | EVP_DigestInit(self->ctx, digest); |
|---|
| 465 | n/a | } |
|---|
| 466 | n/a | |
|---|
| 467 | n/a | if (cp && len) { |
|---|
| 468 | n/a | if (len >= HASHLIB_GIL_MINSIZE) { |
|---|
| 469 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 470 | n/a | EVP_hash(self, cp, len); |
|---|
| 471 | n/a | Py_END_ALLOW_THREADS |
|---|
| 472 | n/a | } else { |
|---|
| 473 | n/a | EVP_hash(self, cp, len); |
|---|
| 474 | n/a | } |
|---|
| 475 | n/a | } |
|---|
| 476 | n/a | |
|---|
| 477 | n/a | return (PyObject *)self; |
|---|
| 478 | n/a | } |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | |
|---|
| 481 | n/a | /* The module-level function: new() */ |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | PyDoc_STRVAR(EVP_new__doc__, |
|---|
| 484 | n/a | "Return a new hash object using the named algorithm.\n\ |
|---|
| 485 | n/a | An optional string argument may be provided and will be\n\ |
|---|
| 486 | n/a | automatically hashed.\n\ |
|---|
| 487 | n/a | \n\ |
|---|
| 488 | n/a | The MD5 and SHA1 algorithms are always supported.\n"); |
|---|
| 489 | n/a | |
|---|
| 490 | n/a | static PyObject * |
|---|
| 491 | n/a | EVP_new(PyObject *self, PyObject *args, PyObject *kwdict) |
|---|
| 492 | n/a | { |
|---|
| 493 | n/a | static char *kwlist[] = {"name", "string", NULL}; |
|---|
| 494 | n/a | PyObject *name_obj = NULL; |
|---|
| 495 | n/a | PyObject *data_obj = NULL; |
|---|
| 496 | n/a | Py_buffer view = { 0 }; |
|---|
| 497 | n/a | PyObject *ret_obj; |
|---|
| 498 | n/a | char *name; |
|---|
| 499 | n/a | const EVP_MD *digest; |
|---|
| 500 | n/a | |
|---|
| 501 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "O|O:new", kwlist, |
|---|
| 502 | n/a | &name_obj, &data_obj)) { |
|---|
| 503 | n/a | return NULL; |
|---|
| 504 | n/a | } |
|---|
| 505 | n/a | |
|---|
| 506 | n/a | if (!PyArg_Parse(name_obj, "s", &name)) { |
|---|
| 507 | n/a | PyErr_SetString(PyExc_TypeError, "name must be a string"); |
|---|
| 508 | n/a | return NULL; |
|---|
| 509 | n/a | } |
|---|
| 510 | n/a | |
|---|
| 511 | n/a | if (data_obj) |
|---|
| 512 | n/a | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | digest = EVP_get_digestbyname(name); |
|---|
| 515 | n/a | |
|---|
| 516 | n/a | ret_obj = EVPnew(name_obj, digest, NULL, (unsigned char*)view.buf, view.len); |
|---|
| 517 | n/a | |
|---|
| 518 | n/a | if (data_obj) |
|---|
| 519 | n/a | PyBuffer_Release(&view); |
|---|
| 520 | n/a | return ret_obj; |
|---|
| 521 | n/a | } |
|---|
| 522 | n/a | |
|---|
| 523 | n/a | |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | #if (OPENSSL_VERSION_NUMBER >= 0x10000000 && !defined(OPENSSL_NO_HMAC) \ |
|---|
| 526 | n/a | && !defined(OPENSSL_NO_SHA)) |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | #define PY_PBKDF2_HMAC 1 |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | #if !HAS_FAST_PKCS5_PBKDF2_HMAC |
|---|
| 531 | n/a | /* Improved implementation of PKCS5_PBKDF2_HMAC() |
|---|
| 532 | n/a | * |
|---|
| 533 | n/a | * PKCS5_PBKDF2_HMAC_fast() hashes the password exactly one time instead of |
|---|
| 534 | n/a | * `iter` times. Today (2013) the iteration count is typically 100,000 or |
|---|
| 535 | n/a | * more. The improved algorithm is not subject to a Denial-of-Service |
|---|
| 536 | n/a | * vulnerability with overly large passwords. |
|---|
| 537 | n/a | * |
|---|
| 538 | n/a | * Also OpenSSL < 1.0 don't provide PKCS5_PBKDF2_HMAC(), only |
|---|
| 539 | n/a | * PKCS5_PBKDF2_SHA1. |
|---|
| 540 | n/a | */ |
|---|
| 541 | n/a | static int |
|---|
| 542 | n/a | PKCS5_PBKDF2_HMAC_fast(const char *pass, int passlen, |
|---|
| 543 | n/a | const unsigned char *salt, int saltlen, |
|---|
| 544 | n/a | int iter, const EVP_MD *digest, |
|---|
| 545 | n/a | int keylen, unsigned char *out) |
|---|
| 546 | n/a | { |
|---|
| 547 | n/a | unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; |
|---|
| 548 | n/a | int cplen, j, k, tkeylen, mdlen; |
|---|
| 549 | n/a | unsigned long i = 1; |
|---|
| 550 | n/a | HMAC_CTX hctx_tpl, hctx; |
|---|
| 551 | n/a | |
|---|
| 552 | n/a | mdlen = EVP_MD_size(digest); |
|---|
| 553 | n/a | if (mdlen < 0) |
|---|
| 554 | n/a | return 0; |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | HMAC_CTX_init(&hctx_tpl); |
|---|
| 557 | n/a | HMAC_CTX_init(&hctx); |
|---|
| 558 | n/a | p = out; |
|---|
| 559 | n/a | tkeylen = keylen; |
|---|
| 560 | n/a | if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) { |
|---|
| 561 | n/a | HMAC_CTX_cleanup(&hctx_tpl); |
|---|
| 562 | n/a | return 0; |
|---|
| 563 | n/a | } |
|---|
| 564 | n/a | while (tkeylen) { |
|---|
| 565 | n/a | if (tkeylen > mdlen) |
|---|
| 566 | n/a | cplen = mdlen; |
|---|
| 567 | n/a | else |
|---|
| 568 | n/a | cplen = tkeylen; |
|---|
| 569 | n/a | /* We are unlikely to ever use more than 256 blocks (5120 bits!) |
|---|
| 570 | n/a | * but just in case... |
|---|
| 571 | n/a | */ |
|---|
| 572 | n/a | itmp[0] = (unsigned char)((i >> 24) & 0xff); |
|---|
| 573 | n/a | itmp[1] = (unsigned char)((i >> 16) & 0xff); |
|---|
| 574 | n/a | itmp[2] = (unsigned char)((i >> 8) & 0xff); |
|---|
| 575 | n/a | itmp[3] = (unsigned char)(i & 0xff); |
|---|
| 576 | n/a | if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { |
|---|
| 577 | n/a | HMAC_CTX_cleanup(&hctx_tpl); |
|---|
| 578 | n/a | return 0; |
|---|
| 579 | n/a | } |
|---|
| 580 | n/a | if (!HMAC_Update(&hctx, salt, saltlen) |
|---|
| 581 | n/a | || !HMAC_Update(&hctx, itmp, 4) |
|---|
| 582 | n/a | || !HMAC_Final(&hctx, digtmp, NULL)) { |
|---|
| 583 | n/a | HMAC_CTX_cleanup(&hctx_tpl); |
|---|
| 584 | n/a | HMAC_CTX_cleanup(&hctx); |
|---|
| 585 | n/a | return 0; |
|---|
| 586 | n/a | } |
|---|
| 587 | n/a | HMAC_CTX_cleanup(&hctx); |
|---|
| 588 | n/a | memcpy(p, digtmp, cplen); |
|---|
| 589 | n/a | for (j = 1; j < iter; j++) { |
|---|
| 590 | n/a | if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { |
|---|
| 591 | n/a | HMAC_CTX_cleanup(&hctx_tpl); |
|---|
| 592 | n/a | return 0; |
|---|
| 593 | n/a | } |
|---|
| 594 | n/a | if (!HMAC_Update(&hctx, digtmp, mdlen) |
|---|
| 595 | n/a | || !HMAC_Final(&hctx, digtmp, NULL)) { |
|---|
| 596 | n/a | HMAC_CTX_cleanup(&hctx_tpl); |
|---|
| 597 | n/a | HMAC_CTX_cleanup(&hctx); |
|---|
| 598 | n/a | return 0; |
|---|
| 599 | n/a | } |
|---|
| 600 | n/a | HMAC_CTX_cleanup(&hctx); |
|---|
| 601 | n/a | for (k = 0; k < cplen; k++) { |
|---|
| 602 | n/a | p[k] ^= digtmp[k]; |
|---|
| 603 | n/a | } |
|---|
| 604 | n/a | } |
|---|
| 605 | n/a | tkeylen-= cplen; |
|---|
| 606 | n/a | i++; |
|---|
| 607 | n/a | p+= cplen; |
|---|
| 608 | n/a | } |
|---|
| 609 | n/a | HMAC_CTX_cleanup(&hctx_tpl); |
|---|
| 610 | n/a | return 1; |
|---|
| 611 | n/a | } |
|---|
| 612 | n/a | #endif |
|---|
| 613 | n/a | |
|---|
| 614 | n/a | |
|---|
| 615 | n/a | PyDoc_STRVAR(pbkdf2_hmac__doc__, |
|---|
| 616 | n/a | "pbkdf2_hmac(hash_name, password, salt, iterations, dklen=None) -> key\n\ |
|---|
| 617 | n/a | \n\ |
|---|
| 618 | n/a | Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as\n\ |
|---|
| 619 | n/a | pseudorandom function."); |
|---|
| 620 | n/a | |
|---|
| 621 | n/a | static PyObject * |
|---|
| 622 | n/a | pbkdf2_hmac(PyObject *self, PyObject *args, PyObject *kwdict) |
|---|
| 623 | n/a | { |
|---|
| 624 | n/a | static char *kwlist[] = {"hash_name", "password", "salt", "iterations", |
|---|
| 625 | n/a | "dklen", NULL}; |
|---|
| 626 | n/a | PyObject *key_obj = NULL, *dklen_obj = Py_None; |
|---|
| 627 | n/a | char *name, *key; |
|---|
| 628 | n/a | Py_buffer password, salt; |
|---|
| 629 | n/a | long iterations, dklen; |
|---|
| 630 | n/a | int retval; |
|---|
| 631 | n/a | const EVP_MD *digest; |
|---|
| 632 | n/a | |
|---|
| 633 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwdict, "sy*y*l|O:pbkdf2_hmac", |
|---|
| 634 | n/a | kwlist, &name, &password, &salt, |
|---|
| 635 | n/a | &iterations, &dklen_obj)) { |
|---|
| 636 | n/a | return NULL; |
|---|
| 637 | n/a | } |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | digest = EVP_get_digestbyname(name); |
|---|
| 640 | n/a | if (digest == NULL) { |
|---|
| 641 | n/a | PyErr_SetString(PyExc_ValueError, "unsupported hash type"); |
|---|
| 642 | n/a | goto end; |
|---|
| 643 | n/a | } |
|---|
| 644 | n/a | |
|---|
| 645 | n/a | if (password.len > INT_MAX) { |
|---|
| 646 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 647 | n/a | "password is too long."); |
|---|
| 648 | n/a | goto end; |
|---|
| 649 | n/a | } |
|---|
| 650 | n/a | |
|---|
| 651 | n/a | if (salt.len > INT_MAX) { |
|---|
| 652 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 653 | n/a | "salt is too long."); |
|---|
| 654 | n/a | goto end; |
|---|
| 655 | n/a | } |
|---|
| 656 | n/a | |
|---|
| 657 | n/a | if (iterations < 1) { |
|---|
| 658 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 659 | n/a | "iteration value must be greater than 0."); |
|---|
| 660 | n/a | goto end; |
|---|
| 661 | n/a | } |
|---|
| 662 | n/a | if (iterations > INT_MAX) { |
|---|
| 663 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 664 | n/a | "iteration value is too great."); |
|---|
| 665 | n/a | goto end; |
|---|
| 666 | n/a | } |
|---|
| 667 | n/a | |
|---|
| 668 | n/a | if (dklen_obj == Py_None) { |
|---|
| 669 | n/a | dklen = EVP_MD_size(digest); |
|---|
| 670 | n/a | } else { |
|---|
| 671 | n/a | dklen = PyLong_AsLong(dklen_obj); |
|---|
| 672 | n/a | if ((dklen == -1) && PyErr_Occurred()) { |
|---|
| 673 | n/a | goto end; |
|---|
| 674 | n/a | } |
|---|
| 675 | n/a | } |
|---|
| 676 | n/a | if (dklen < 1) { |
|---|
| 677 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 678 | n/a | "key length must be greater than 0."); |
|---|
| 679 | n/a | goto end; |
|---|
| 680 | n/a | } |
|---|
| 681 | n/a | if (dklen > INT_MAX) { |
|---|
| 682 | n/a | /* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */ |
|---|
| 683 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 684 | n/a | "key length is too great."); |
|---|
| 685 | n/a | goto end; |
|---|
| 686 | n/a | } |
|---|
| 687 | n/a | |
|---|
| 688 | n/a | key_obj = PyBytes_FromStringAndSize(NULL, dklen); |
|---|
| 689 | n/a | if (key_obj == NULL) { |
|---|
| 690 | n/a | goto end; |
|---|
| 691 | n/a | } |
|---|
| 692 | n/a | key = PyBytes_AS_STRING(key_obj); |
|---|
| 693 | n/a | |
|---|
| 694 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 695 | n/a | #if HAS_FAST_PKCS5_PBKDF2_HMAC |
|---|
| 696 | n/a | retval = PKCS5_PBKDF2_HMAC((char*)password.buf, (int)password.len, |
|---|
| 697 | n/a | (unsigned char *)salt.buf, (int)salt.len, |
|---|
| 698 | n/a | iterations, digest, dklen, |
|---|
| 699 | n/a | (unsigned char *)key); |
|---|
| 700 | n/a | #else |
|---|
| 701 | n/a | retval = PKCS5_PBKDF2_HMAC_fast((char*)password.buf, (int)password.len, |
|---|
| 702 | n/a | (unsigned char *)salt.buf, (int)salt.len, |
|---|
| 703 | n/a | iterations, digest, dklen, |
|---|
| 704 | n/a | (unsigned char *)key); |
|---|
| 705 | n/a | #endif |
|---|
| 706 | n/a | Py_END_ALLOW_THREADS |
|---|
| 707 | n/a | |
|---|
| 708 | n/a | if (!retval) { |
|---|
| 709 | n/a | Py_CLEAR(key_obj); |
|---|
| 710 | n/a | _setException(PyExc_ValueError); |
|---|
| 711 | n/a | goto end; |
|---|
| 712 | n/a | } |
|---|
| 713 | n/a | |
|---|
| 714 | n/a | end: |
|---|
| 715 | n/a | PyBuffer_Release(&password); |
|---|
| 716 | n/a | PyBuffer_Release(&salt); |
|---|
| 717 | n/a | return key_obj; |
|---|
| 718 | n/a | } |
|---|
| 719 | n/a | |
|---|
| 720 | n/a | #endif |
|---|
| 721 | n/a | |
|---|
| 722 | n/a | #if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER) |
|---|
| 723 | n/a | #define PY_SCRYPT 1 |
|---|
| 724 | n/a | |
|---|
| 725 | n/a | /*[clinic input] |
|---|
| 726 | n/a | _hashlib.scrypt |
|---|
| 727 | n/a | |
|---|
| 728 | n/a | password: Py_buffer |
|---|
| 729 | n/a | * |
|---|
| 730 | n/a | salt: Py_buffer = None |
|---|
| 731 | n/a | n as n_obj: object(subclass_of='&PyLong_Type') = None |
|---|
| 732 | n/a | r as r_obj: object(subclass_of='&PyLong_Type') = None |
|---|
| 733 | n/a | p as p_obj: object(subclass_of='&PyLong_Type') = None |
|---|
| 734 | n/a | maxmem: long = 0 |
|---|
| 735 | n/a | dklen: long = 64 |
|---|
| 736 | n/a | |
|---|
| 737 | n/a | |
|---|
| 738 | n/a | scrypt password-based key derivation function. |
|---|
| 739 | n/a | [clinic start generated code]*/ |
|---|
| 740 | n/a | |
|---|
| 741 | n/a | static PyObject * |
|---|
| 742 | n/a | _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt, |
|---|
| 743 | n/a | PyObject *n_obj, PyObject *r_obj, PyObject *p_obj, |
|---|
| 744 | n/a | long maxmem, long dklen) |
|---|
| 745 | n/a | /*[clinic end generated code: output=14849e2aa2b7b46c input=48a7d63bf3f75c42]*/ |
|---|
| 746 | n/a | { |
|---|
| 747 | n/a | PyObject *key_obj = NULL; |
|---|
| 748 | n/a | char *key; |
|---|
| 749 | n/a | int retval; |
|---|
| 750 | n/a | unsigned long n, r, p; |
|---|
| 751 | n/a | |
|---|
| 752 | n/a | if (password->len > INT_MAX) { |
|---|
| 753 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 754 | n/a | "password is too long."); |
|---|
| 755 | n/a | return NULL; |
|---|
| 756 | n/a | } |
|---|
| 757 | n/a | |
|---|
| 758 | n/a | if (salt->buf == NULL) { |
|---|
| 759 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 760 | n/a | "salt is required"); |
|---|
| 761 | n/a | return NULL; |
|---|
| 762 | n/a | } |
|---|
| 763 | n/a | if (salt->len > INT_MAX) { |
|---|
| 764 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 765 | n/a | "salt is too long."); |
|---|
| 766 | n/a | return NULL; |
|---|
| 767 | n/a | } |
|---|
| 768 | n/a | |
|---|
| 769 | n/a | n = PyLong_AsUnsignedLong(n_obj); |
|---|
| 770 | n/a | if (n == (unsigned long) -1 && PyErr_Occurred()) { |
|---|
| 771 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 772 | n/a | "n is required and must be an unsigned int"); |
|---|
| 773 | n/a | return NULL; |
|---|
| 774 | n/a | } |
|---|
| 775 | n/a | if (n < 2 || n & (n - 1)) { |
|---|
| 776 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 777 | n/a | "n must be a power of 2."); |
|---|
| 778 | n/a | return NULL; |
|---|
| 779 | n/a | } |
|---|
| 780 | n/a | |
|---|
| 781 | n/a | r = PyLong_AsUnsignedLong(r_obj); |
|---|
| 782 | n/a | if (r == (unsigned long) -1 && PyErr_Occurred()) { |
|---|
| 783 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 784 | n/a | "r is required and must be an unsigned int"); |
|---|
| 785 | n/a | return NULL; |
|---|
| 786 | n/a | } |
|---|
| 787 | n/a | |
|---|
| 788 | n/a | p = PyLong_AsUnsignedLong(p_obj); |
|---|
| 789 | n/a | if (p == (unsigned long) -1 && PyErr_Occurred()) { |
|---|
| 790 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 791 | n/a | "p is required and must be an unsigned int"); |
|---|
| 792 | n/a | return NULL; |
|---|
| 793 | n/a | } |
|---|
| 794 | n/a | |
|---|
| 795 | n/a | if (maxmem < 0 || maxmem > INT_MAX) { |
|---|
| 796 | n/a | /* OpenSSL 1.1.0 restricts maxmem to 32MB. It may change in the |
|---|
| 797 | n/a | future. The maxmem constant is private to OpenSSL. */ |
|---|
| 798 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 799 | n/a | "maxmem must be positive and smaller than %d", |
|---|
| 800 | n/a | INT_MAX); |
|---|
| 801 | n/a | return NULL; |
|---|
| 802 | n/a | } |
|---|
| 803 | n/a | |
|---|
| 804 | n/a | if (dklen < 1 || dklen > INT_MAX) { |
|---|
| 805 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 806 | n/a | "dklen must be greater than 0 and smaller than %d", |
|---|
| 807 | n/a | INT_MAX); |
|---|
| 808 | n/a | return NULL; |
|---|
| 809 | n/a | } |
|---|
| 810 | n/a | |
|---|
| 811 | n/a | /* let OpenSSL validate the rest */ |
|---|
| 812 | n/a | retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0); |
|---|
| 813 | n/a | if (!retval) { |
|---|
| 814 | n/a | /* sorry, can't do much better */ |
|---|
| 815 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 816 | n/a | "Invalid paramemter combination for n, r, p, maxmem."); |
|---|
| 817 | n/a | return NULL; |
|---|
| 818 | n/a | } |
|---|
| 819 | n/a | |
|---|
| 820 | n/a | key_obj = PyBytes_FromStringAndSize(NULL, dklen); |
|---|
| 821 | n/a | if (key_obj == NULL) { |
|---|
| 822 | n/a | return NULL; |
|---|
| 823 | n/a | } |
|---|
| 824 | n/a | key = PyBytes_AS_STRING(key_obj); |
|---|
| 825 | n/a | |
|---|
| 826 | n/a | Py_BEGIN_ALLOW_THREADS |
|---|
| 827 | n/a | retval = EVP_PBE_scrypt( |
|---|
| 828 | n/a | (const char*)password->buf, (size_t)password->len, |
|---|
| 829 | n/a | (const unsigned char *)salt->buf, (size_t)salt->len, |
|---|
| 830 | n/a | n, r, p, maxmem, |
|---|
| 831 | n/a | (unsigned char *)key, (size_t)dklen |
|---|
| 832 | n/a | ); |
|---|
| 833 | n/a | Py_END_ALLOW_THREADS |
|---|
| 834 | n/a | |
|---|
| 835 | n/a | if (!retval) { |
|---|
| 836 | n/a | Py_CLEAR(key_obj); |
|---|
| 837 | n/a | _setException(PyExc_ValueError); |
|---|
| 838 | n/a | return NULL; |
|---|
| 839 | n/a | } |
|---|
| 840 | n/a | return key_obj; |
|---|
| 841 | n/a | } |
|---|
| 842 | n/a | #endif |
|---|
| 843 | n/a | |
|---|
| 844 | n/a | /* State for our callback function so that it can accumulate a result. */ |
|---|
| 845 | n/a | typedef struct _internal_name_mapper_state { |
|---|
| 846 | n/a | PyObject *set; |
|---|
| 847 | n/a | int error; |
|---|
| 848 | n/a | } _InternalNameMapperState; |
|---|
| 849 | n/a | |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | /* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */ |
|---|
| 852 | n/a | static void |
|---|
| 853 | n/a | _openssl_hash_name_mapper(const OBJ_NAME *openssl_obj_name, void *arg) |
|---|
| 854 | n/a | { |
|---|
| 855 | n/a | _InternalNameMapperState *state = (_InternalNameMapperState *)arg; |
|---|
| 856 | n/a | PyObject *py_name; |
|---|
| 857 | n/a | |
|---|
| 858 | n/a | assert(state != NULL); |
|---|
| 859 | n/a | if (openssl_obj_name == NULL) |
|---|
| 860 | n/a | return; |
|---|
| 861 | n/a | /* Ignore aliased names, they pollute the list and OpenSSL appears to |
|---|
| 862 | n/a | * have its own definition of alias as the resulting list still |
|---|
| 863 | n/a | * contains duplicate and alternate names for several algorithms. */ |
|---|
| 864 | n/a | if (openssl_obj_name->alias) |
|---|
| 865 | n/a | return; |
|---|
| 866 | n/a | |
|---|
| 867 | n/a | py_name = PyUnicode_FromString(openssl_obj_name->name); |
|---|
| 868 | n/a | if (py_name == NULL) { |
|---|
| 869 | n/a | state->error = 1; |
|---|
| 870 | n/a | } else { |
|---|
| 871 | n/a | if (PySet_Add(state->set, py_name) != 0) { |
|---|
| 872 | n/a | state->error = 1; |
|---|
| 873 | n/a | } |
|---|
| 874 | n/a | Py_DECREF(py_name); |
|---|
| 875 | n/a | } |
|---|
| 876 | n/a | } |
|---|
| 877 | n/a | |
|---|
| 878 | n/a | |
|---|
| 879 | n/a | /* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */ |
|---|
| 880 | n/a | static PyObject* |
|---|
| 881 | n/a | generate_hash_name_list(void) |
|---|
| 882 | n/a | { |
|---|
| 883 | n/a | _InternalNameMapperState state; |
|---|
| 884 | n/a | state.set = PyFrozenSet_New(NULL); |
|---|
| 885 | n/a | if (state.set == NULL) |
|---|
| 886 | n/a | return NULL; |
|---|
| 887 | n/a | state.error = 0; |
|---|
| 888 | n/a | |
|---|
| 889 | n/a | OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, &_openssl_hash_name_mapper, &state); |
|---|
| 890 | n/a | |
|---|
| 891 | n/a | if (state.error) { |
|---|
| 892 | n/a | Py_DECREF(state.set); |
|---|
| 893 | n/a | return NULL; |
|---|
| 894 | n/a | } |
|---|
| 895 | n/a | return state.set; |
|---|
| 896 | n/a | } |
|---|
| 897 | n/a | |
|---|
| 898 | n/a | |
|---|
| 899 | n/a | /* |
|---|
| 900 | n/a | * This macro generates constructor function definitions for specific |
|---|
| 901 | n/a | * hash algorithms. These constructors are much faster than calling |
|---|
| 902 | n/a | * the generic one passing it a python string and are noticeably |
|---|
| 903 | n/a | * faster than calling a python new() wrapper. Thats important for |
|---|
| 904 | n/a | * code that wants to make hashes of a bunch of small strings. |
|---|
| 905 | n/a | */ |
|---|
| 906 | n/a | #define GEN_CONSTRUCTOR(NAME) \ |
|---|
| 907 | n/a | static PyObject * \ |
|---|
| 908 | n/a | EVP_new_ ## NAME (PyObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames) \ |
|---|
| 909 | n/a | { \ |
|---|
| 910 | n/a | PyObject *data_obj = NULL; \ |
|---|
| 911 | n/a | Py_buffer view = { 0 }; \ |
|---|
| 912 | n/a | PyObject *ret_obj; \ |
|---|
| 913 | n/a | \ |
|---|
| 914 | n/a | if (!_PyArg_ParseStack(args, nargs, "|O:" #NAME , &data_obj)) { \ |
|---|
| 915 | n/a | return NULL; \ |
|---|
| 916 | n/a | } \ |
|---|
| 917 | n/a | \ |
|---|
| 918 | n/a | if (!_PyArg_NoStackKeywords(#NAME, kwnames)) { \ |
|---|
| 919 | n/a | return NULL; \ |
|---|
| 920 | n/a | } \ |
|---|
| 921 | n/a | \ |
|---|
| 922 | n/a | if (data_obj) \ |
|---|
| 923 | n/a | GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view); \ |
|---|
| 924 | n/a | \ |
|---|
| 925 | n/a | ret_obj = EVPnew( \ |
|---|
| 926 | n/a | CONST_ ## NAME ## _name_obj, \ |
|---|
| 927 | n/a | NULL, \ |
|---|
| 928 | n/a | CONST_new_ ## NAME ## _ctx_p, \ |
|---|
| 929 | n/a | (unsigned char*)view.buf, \ |
|---|
| 930 | n/a | view.len); \ |
|---|
| 931 | n/a | \ |
|---|
| 932 | n/a | if (data_obj) \ |
|---|
| 933 | n/a | PyBuffer_Release(&view); \ |
|---|
| 934 | n/a | return ret_obj; \ |
|---|
| 935 | n/a | } |
|---|
| 936 | n/a | |
|---|
| 937 | n/a | /* a PyMethodDef structure for the constructor */ |
|---|
| 938 | n/a | #define CONSTRUCTOR_METH_DEF(NAME) \ |
|---|
| 939 | n/a | {"openssl_" #NAME, (PyCFunction)EVP_new_ ## NAME, METH_FASTCALL, \ |
|---|
| 940 | n/a | PyDoc_STR("Returns a " #NAME \ |
|---|
| 941 | n/a | " hash object; optionally initialized with a string") \ |
|---|
| 942 | n/a | } |
|---|
| 943 | n/a | |
|---|
| 944 | n/a | /* used in the init function to setup a constructor: initialize OpenSSL |
|---|
| 945 | n/a | constructor constants if they haven't been initialized already. */ |
|---|
| 946 | n/a | #define INIT_CONSTRUCTOR_CONSTANTS(NAME) do { \ |
|---|
| 947 | n/a | if (CONST_ ## NAME ## _name_obj == NULL) { \ |
|---|
| 948 | n/a | CONST_ ## NAME ## _name_obj = PyUnicode_FromString(#NAME); \ |
|---|
| 949 | n/a | if (EVP_get_digestbyname(#NAME)) { \ |
|---|
| 950 | n/a | CONST_new_ ## NAME ## _ctx_p = EVP_MD_CTX_new(); \ |
|---|
| 951 | n/a | EVP_DigestInit(CONST_new_ ## NAME ## _ctx_p, EVP_get_digestbyname(#NAME)); \ |
|---|
| 952 | n/a | } \ |
|---|
| 953 | n/a | } \ |
|---|
| 954 | n/a | } while (0); |
|---|
| 955 | n/a | |
|---|
| 956 | n/a | GEN_CONSTRUCTOR(md5) |
|---|
| 957 | n/a | GEN_CONSTRUCTOR(sha1) |
|---|
| 958 | n/a | GEN_CONSTRUCTOR(sha224) |
|---|
| 959 | n/a | GEN_CONSTRUCTOR(sha256) |
|---|
| 960 | n/a | GEN_CONSTRUCTOR(sha384) |
|---|
| 961 | n/a | GEN_CONSTRUCTOR(sha512) |
|---|
| 962 | n/a | |
|---|
| 963 | n/a | /* List of functions exported by this module */ |
|---|
| 964 | n/a | |
|---|
| 965 | n/a | static struct PyMethodDef EVP_functions[] = { |
|---|
| 966 | n/a | {"new", (PyCFunction)EVP_new, METH_VARARGS|METH_KEYWORDS, EVP_new__doc__}, |
|---|
| 967 | n/a | #ifdef PY_PBKDF2_HMAC |
|---|
| 968 | n/a | {"pbkdf2_hmac", (PyCFunction)pbkdf2_hmac, METH_VARARGS|METH_KEYWORDS, |
|---|
| 969 | n/a | pbkdf2_hmac__doc__}, |
|---|
| 970 | n/a | #endif |
|---|
| 971 | n/a | _HASHLIB_SCRYPT_METHODDEF |
|---|
| 972 | n/a | CONSTRUCTOR_METH_DEF(md5), |
|---|
| 973 | n/a | CONSTRUCTOR_METH_DEF(sha1), |
|---|
| 974 | n/a | CONSTRUCTOR_METH_DEF(sha224), |
|---|
| 975 | n/a | CONSTRUCTOR_METH_DEF(sha256), |
|---|
| 976 | n/a | CONSTRUCTOR_METH_DEF(sha384), |
|---|
| 977 | n/a | CONSTRUCTOR_METH_DEF(sha512), |
|---|
| 978 | n/a | {NULL, NULL} /* Sentinel */ |
|---|
| 979 | n/a | }; |
|---|
| 980 | n/a | |
|---|
| 981 | n/a | |
|---|
| 982 | n/a | /* Initialize this module. */ |
|---|
| 983 | n/a | |
|---|
| 984 | n/a | |
|---|
| 985 | n/a | static struct PyModuleDef _hashlibmodule = { |
|---|
| 986 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 987 | n/a | "_hashlib", |
|---|
| 988 | n/a | NULL, |
|---|
| 989 | n/a | -1, |
|---|
| 990 | n/a | EVP_functions, |
|---|
| 991 | n/a | NULL, |
|---|
| 992 | n/a | NULL, |
|---|
| 993 | n/a | NULL, |
|---|
| 994 | n/a | NULL |
|---|
| 995 | n/a | }; |
|---|
| 996 | n/a | |
|---|
| 997 | n/a | PyMODINIT_FUNC |
|---|
| 998 | n/a | PyInit__hashlib(void) |
|---|
| 999 | n/a | { |
|---|
| 1000 | n/a | PyObject *m, *openssl_md_meth_names; |
|---|
| 1001 | n/a | |
|---|
| 1002 | n/a | OpenSSL_add_all_digests(); |
|---|
| 1003 | n/a | ERR_load_crypto_strings(); |
|---|
| 1004 | n/a | |
|---|
| 1005 | n/a | /* TODO build EVP_functions openssl_* entries dynamically based |
|---|
| 1006 | n/a | * on what hashes are supported rather than listing many |
|---|
| 1007 | n/a | * but having some be unsupported. Only init appropriate |
|---|
| 1008 | n/a | * constants. */ |
|---|
| 1009 | n/a | |
|---|
| 1010 | n/a | Py_TYPE(&EVPtype) = &PyType_Type; |
|---|
| 1011 | n/a | if (PyType_Ready(&EVPtype) < 0) |
|---|
| 1012 | n/a | return NULL; |
|---|
| 1013 | n/a | |
|---|
| 1014 | n/a | m = PyModule_Create(&_hashlibmodule); |
|---|
| 1015 | n/a | if (m == NULL) |
|---|
| 1016 | n/a | return NULL; |
|---|
| 1017 | n/a | |
|---|
| 1018 | n/a | openssl_md_meth_names = generate_hash_name_list(); |
|---|
| 1019 | n/a | if (openssl_md_meth_names == NULL) { |
|---|
| 1020 | n/a | Py_DECREF(m); |
|---|
| 1021 | n/a | return NULL; |
|---|
| 1022 | n/a | } |
|---|
| 1023 | n/a | if (PyModule_AddObject(m, "openssl_md_meth_names", openssl_md_meth_names)) { |
|---|
| 1024 | n/a | Py_DECREF(m); |
|---|
| 1025 | n/a | return NULL; |
|---|
| 1026 | n/a | } |
|---|
| 1027 | n/a | |
|---|
| 1028 | n/a | Py_INCREF((PyObject *)&EVPtype); |
|---|
| 1029 | n/a | PyModule_AddObject(m, "HASH", (PyObject *)&EVPtype); |
|---|
| 1030 | n/a | |
|---|
| 1031 | n/a | /* these constants are used by the convenience constructors */ |
|---|
| 1032 | n/a | INIT_CONSTRUCTOR_CONSTANTS(md5); |
|---|
| 1033 | n/a | INIT_CONSTRUCTOR_CONSTANTS(sha1); |
|---|
| 1034 | n/a | INIT_CONSTRUCTOR_CONSTANTS(sha224); |
|---|
| 1035 | n/a | INIT_CONSTRUCTOR_CONSTANTS(sha256); |
|---|
| 1036 | n/a | INIT_CONSTRUCTOR_CONSTANTS(sha384); |
|---|
| 1037 | n/a | INIT_CONSTRUCTOR_CONSTANTS(sha512); |
|---|
| 1038 | n/a | return m; |
|---|
| 1039 | n/a | } |
|---|