2005-08-21 15:45:59 -03:00
|
|
|
/* Module that wraps all OpenSSL hash algorithms */
|
|
|
|
|
|
|
|
/*
|
2010-01-09 14:45:30 -04:00
|
|
|
* Copyright (C) 2005-2010 Gregory P. Smith (greg@krypto.org)
|
2005-08-21 15:45:59 -03:00
|
|
|
* Licensed to PSF under a Contributor Agreement.
|
|
|
|
*
|
|
|
|
* Derived from a skeleton of shamodule.c containing work performed by:
|
|
|
|
*
|
|
|
|
* Andrew Kuchling (amk@amk.ca)
|
|
|
|
* Greg Stein (gstein@lyra.org)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2006-03-01 17:50:07 -04:00
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
#include "Python.h"
|
2009-02-12 03:35:29 -04:00
|
|
|
#include "hashlib.h"
|
2015-04-25 20:42:38 -03:00
|
|
|
#include "pystrhex.h"
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2009-01-08 17:17:16 -04:00
|
|
|
|
2009-05-03 21:45:33 -03:00
|
|
|
/* EVP is the preferred interface to hashing in OpenSSL */
|
|
|
|
#include <openssl/evp.h>
|
2018-01-27 04:53:43 -04:00
|
|
|
#include <openssl/hmac.h>
|
2010-09-06 05:30:23 -03:00
|
|
|
/* We use the object interface to discover what hashes OpenSSL supports. */
|
|
|
|
#include <openssl/objects.h>
|
2013-10-12 19:52:43 -03:00
|
|
|
#include "openssl/err.h"
|
2009-05-03 21:45:33 -03:00
|
|
|
|
2020-04-29 13:04:22 -03:00
|
|
|
#include <openssl/crypto.h> // FIPS_mode()
|
|
|
|
|
2020-05-15 15:55:25 -03:00
|
|
|
#ifndef OPENSSL_THREADS
|
|
|
|
# error "OPENSSL_THREADS is not defined, Python requires thread-safe OpenSSL"
|
|
|
|
#endif
|
|
|
|
|
2019-09-12 11:33:26 -03:00
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
/* OpenSSL < 1.1.0 */
|
|
|
|
#define EVP_MD_CTX_new EVP_MD_CTX_create
|
|
|
|
#define EVP_MD_CTX_free EVP_MD_CTX_destroy
|
2020-05-17 08:49:10 -03:00
|
|
|
|
|
|
|
HMAC_CTX *
|
|
|
|
HMAC_CTX_new(void)
|
|
|
|
{
|
|
|
|
HMAC_CTX *ctx = OPENSSL_malloc(sizeof(HMAC_CTX));
|
|
|
|
if (ctx != NULL) {
|
|
|
|
memset(ctx, 0, sizeof(HMAC_CTX));
|
|
|
|
HMAC_CTX_init(ctx);
|
|
|
|
}
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
HMAC_CTX_free(HMAC_CTX *ctx)
|
|
|
|
{
|
|
|
|
if (ctx != NULL) {
|
|
|
|
HMAC_CTX_cleanup(ctx);
|
|
|
|
OPENSSL_free(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const EVP_MD *
|
|
|
|
HMAC_CTX_get_md(const HMAC_CTX *ctx)
|
|
|
|
{
|
|
|
|
return ctx->md;
|
|
|
|
}
|
2019-09-12 11:33:26 -03:00
|
|
|
#endif
|
|
|
|
|
2009-05-03 21:45:33 -03:00
|
|
|
#define MUNCH_SIZE INT_MAX
|
|
|
|
|
2019-09-14 12:29:54 -03:00
|
|
|
#ifdef NID_sha3_224
|
2019-09-13 10:31:19 -03:00
|
|
|
#define PY_OPENSSL_HAS_SHA3 1
|
|
|
|
#endif
|
|
|
|
|
2019-09-14 12:29:54 -03:00
|
|
|
#if defined(EVP_MD_FLAG_XOF) && defined(NID_shake128)
|
|
|
|
#define PY_OPENSSL_HAS_SHAKE 1
|
|
|
|
#endif
|
|
|
|
|
2019-11-04 10:55:56 -04:00
|
|
|
#if defined(NID_blake2b512) && !defined(OPENSSL_NO_BLAKE2)
|
2019-09-13 10:31:19 -03:00
|
|
|
#define PY_OPENSSL_HAS_BLAKE2 1
|
|
|
|
#endif
|
|
|
|
|
2019-09-25 18:03:30 -03:00
|
|
|
static PyModuleDef _hashlibmodule;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
PyTypeObject *EVPtype;
|
2020-05-17 08:49:10 -03:00
|
|
|
PyTypeObject *HMACtype;
|
2020-05-16 17:27:06 -03:00
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
|
|
|
PyTypeObject *EVPXOFtype;
|
|
|
|
#endif
|
2019-09-25 18:03:30 -03:00
|
|
|
} _hashlibstate;
|
|
|
|
|
2020-03-16 10:15:01 -03:00
|
|
|
static inline _hashlibstate*
|
|
|
|
get_hashlib_state(PyObject *module)
|
|
|
|
{
|
|
|
|
void *state = PyModule_GetState(module);
|
|
|
|
assert(state != NULL);
|
|
|
|
return (_hashlibstate *)state;
|
|
|
|
}
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
2016-09-05 18:19:05 -03:00
|
|
|
EVP_MD_CTX *ctx; /* OpenSSL message digest context */
|
2009-01-08 17:17:16 -04:00
|
|
|
PyThread_type_lock lock; /* OpenSSL context lock */
|
2005-08-21 15:45:59 -03:00
|
|
|
} EVPobject;
|
|
|
|
|
2020-05-17 08:49:10 -03:00
|
|
|
typedef struct {
|
|
|
|
PyObject_HEAD
|
|
|
|
HMAC_CTX *ctx; /* OpenSSL hmac context */
|
|
|
|
PyThread_type_lock lock; /* HMAC context lock */
|
|
|
|
} HMACobject;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
#include "clinic/_hashopenssl.c.h"
|
|
|
|
/*[clinic input]
|
|
|
|
module _hashlib
|
2019-09-25 18:03:30 -03:00
|
|
|
class _hashlib.HASH "EVPobject *" "((_hashlibstate *)PyModule_GetState(module))->EVPtype"
|
2020-05-16 17:27:06 -03:00
|
|
|
class _hashlib.HASHXOF "EVPobject *" "((_hashlibstate *)PyModule_GetState(module))->EVPXOFtype"
|
2020-05-17 08:49:10 -03:00
|
|
|
class _hashlib.HMAC "HMACobject *" "((_hashlibstate *)PyModule_GetState(module))->HMACtype"
|
2018-12-27 09:43:43 -04:00
|
|
|
[clinic start generated code]*/
|
2020-05-17 08:49:10 -03:00
|
|
|
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7df1bcf6f75cb8ef]*/
|
2018-12-27 09:43:43 -04:00
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2016-09-05 18:19:05 -03:00
|
|
|
/* LCOV_EXCL_START */
|
|
|
|
static PyObject *
|
|
|
|
_setException(PyObject *exc)
|
|
|
|
{
|
|
|
|
unsigned long errcode;
|
|
|
|
const char *lib, *func, *reason;
|
|
|
|
|
|
|
|
errcode = ERR_peek_last_error();
|
|
|
|
if (!errcode) {
|
|
|
|
PyErr_SetString(exc, "unknown reasons");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
ERR_clear_error();
|
|
|
|
|
|
|
|
lib = ERR_lib_error_string(errcode);
|
|
|
|
func = ERR_func_error_string(errcode);
|
|
|
|
reason = ERR_reason_error_string(errcode);
|
|
|
|
|
|
|
|
if (lib && func) {
|
|
|
|
PyErr_Format(exc, "[%s: %s] %s", lib, func, reason);
|
|
|
|
}
|
|
|
|
else if (lib) {
|
|
|
|
PyErr_Format(exc, "[%s] %s", lib, reason);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PyErr_SetString(exc, reason);
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* LCOV_EXCL_STOP */
|
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
/* {Py_tp_new, NULL} doesn't block __new__ */
|
|
|
|
static PyObject *
|
|
|
|
_disabled_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
|
|
|
|
{
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"cannot create '%.100s' instances", _PyType_Name(type));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-09-13 10:31:19 -03:00
|
|
|
static PyObject*
|
|
|
|
py_digest_name(const EVP_MD *md)
|
|
|
|
{
|
|
|
|
int nid = EVP_MD_nid(md);
|
|
|
|
const char *name = NULL;
|
|
|
|
|
|
|
|
/* Hard-coded names for well-known hashing algorithms.
|
|
|
|
* OpenSSL uses slightly different names algorithms like SHA3.
|
|
|
|
*/
|
|
|
|
switch (nid) {
|
|
|
|
case NID_md5:
|
|
|
|
name = "md5";
|
|
|
|
break;
|
|
|
|
case NID_sha1:
|
|
|
|
name = "sha1";
|
|
|
|
break;
|
|
|
|
case NID_sha224:
|
|
|
|
name ="sha224";
|
|
|
|
break;
|
|
|
|
case NID_sha256:
|
|
|
|
name ="sha256";
|
|
|
|
break;
|
|
|
|
case NID_sha384:
|
|
|
|
name ="sha384";
|
|
|
|
break;
|
|
|
|
case NID_sha512:
|
|
|
|
name ="sha512";
|
|
|
|
break;
|
|
|
|
#ifdef NID_sha512_224
|
|
|
|
case NID_sha512_224:
|
|
|
|
name ="sha512_224";
|
|
|
|
break;
|
|
|
|
case NID_sha512_256:
|
|
|
|
name ="sha512_256";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef PY_OPENSSL_HAS_SHA3
|
|
|
|
case NID_sha3_224:
|
|
|
|
name ="sha3_224";
|
|
|
|
break;
|
|
|
|
case NID_sha3_256:
|
|
|
|
name ="sha3_256";
|
|
|
|
break;
|
|
|
|
case NID_sha3_384:
|
|
|
|
name ="sha3_384";
|
|
|
|
break;
|
|
|
|
case NID_sha3_512:
|
|
|
|
name ="sha3_512";
|
|
|
|
break;
|
2019-09-14 12:29:54 -03:00
|
|
|
#endif
|
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
2019-09-13 10:31:19 -03:00
|
|
|
case NID_shake128:
|
|
|
|
name ="shake_128";
|
|
|
|
break;
|
|
|
|
case NID_shake256:
|
|
|
|
name ="shake_256";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#ifdef PY_OPENSSL_HAS_BLAKE2
|
|
|
|
case NID_blake2s256:
|
|
|
|
name ="blake2s";
|
|
|
|
break;
|
|
|
|
case NID_blake2b512:
|
|
|
|
name ="blake2b";
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
/* Ignore aliased names and only use long, lowercase name. The aliases
|
|
|
|
* pollute the list and OpenSSL appears to have its own definition of
|
|
|
|
* alias as the resulting list still contains duplicate and alternate
|
|
|
|
* names for several algorithms.
|
|
|
|
*/
|
|
|
|
name = OBJ_nid2ln(nid);
|
|
|
|
if (name == NULL)
|
|
|
|
name = OBJ_nid2sn(nid);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PyUnicode_FromString(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static const EVP_MD*
|
|
|
|
py_digest_by_name(const char *name)
|
|
|
|
{
|
|
|
|
const EVP_MD *digest = EVP_get_digestbyname(name);
|
|
|
|
|
|
|
|
/* OpenSSL uses dash instead of underscore in names of some algorithms
|
|
|
|
* like SHA3 and SHAKE. Detect different spellings. */
|
|
|
|
if (digest == NULL) {
|
2019-09-14 12:29:54 -03:00
|
|
|
if (0) {}
|
2019-09-13 10:31:19 -03:00
|
|
|
#ifdef NID_sha512_224
|
2019-09-14 12:29:54 -03:00
|
|
|
else if (!strcmp(name, "sha512_224") || !strcmp(name, "SHA512_224")) {
|
2019-09-13 10:31:19 -03:00
|
|
|
digest = EVP_sha512_224();
|
|
|
|
}
|
|
|
|
else if (!strcmp(name, "sha512_256") || !strcmp(name, "SHA512_256")) {
|
|
|
|
digest = EVP_sha512_256();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef PY_OPENSSL_HAS_SHA3
|
|
|
|
/* could be sha3_ or shake_, Python never defined upper case */
|
|
|
|
else if (!strcmp(name, "sha3_224")) {
|
|
|
|
digest = EVP_sha3_224();
|
|
|
|
}
|
|
|
|
else if (!strcmp(name, "sha3_256")) {
|
|
|
|
digest = EVP_sha3_256();
|
|
|
|
}
|
|
|
|
else if (!strcmp(name, "sha3_384")) {
|
|
|
|
digest = EVP_sha3_384();
|
|
|
|
}
|
|
|
|
else if (!strcmp(name, "sha3_512")) {
|
|
|
|
digest = EVP_sha3_512();
|
|
|
|
}
|
2019-09-14 12:29:54 -03:00
|
|
|
#endif
|
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
2019-09-13 10:31:19 -03:00
|
|
|
else if (!strcmp(name, "shake_128")) {
|
|
|
|
digest = EVP_shake128();
|
|
|
|
}
|
|
|
|
else if (!strcmp(name, "shake_256")) {
|
|
|
|
digest = EVP_shake256();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef PY_OPENSSL_HAS_BLAKE2
|
|
|
|
else if (!strcmp(name, "blake2s256")) {
|
|
|
|
digest = EVP_blake2s256();
|
|
|
|
}
|
|
|
|
else if (!strcmp(name, "blake2b512")) {
|
|
|
|
digest = EVP_blake2b512();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return digest;
|
|
|
|
}
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
static EVPobject *
|
2020-05-16 17:27:06 -03:00
|
|
|
newEVPobject(PyTypeObject *type)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2020-05-16 17:27:06 -03:00
|
|
|
EVPobject *retval = (EVPobject *)PyObject_New(EVPobject, type);
|
2016-09-05 18:19:05 -03:00
|
|
|
if (retval == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-03-04 11:45:41 -04:00
|
|
|
retval->lock = NULL;
|
|
|
|
|
2016-09-05 18:19:05 -03:00
|
|
|
retval->ctx = EVP_MD_CTX_new();
|
|
|
|
if (retval->ctx == NULL) {
|
2019-03-04 11:45:41 -04:00
|
|
|
Py_DECREF(retval);
|
2016-09-05 18:19:05 -03:00
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2019-09-12 10:30:47 -03:00
|
|
|
static int
|
2009-01-08 17:17:16 -04:00
|
|
|
EVP_hash(EVPobject *self, const void *vp, Py_ssize_t len)
|
|
|
|
{
|
|
|
|
unsigned int process;
|
|
|
|
const unsigned char *cp = (const unsigned char *)vp;
|
|
|
|
while (0 < len) {
|
|
|
|
if (len > (Py_ssize_t)MUNCH_SIZE)
|
|
|
|
process = MUNCH_SIZE;
|
|
|
|
else
|
|
|
|
process = Py_SAFE_DOWNCAST(len, Py_ssize_t, unsigned int);
|
2017-05-24 04:04:38 -03:00
|
|
|
if (!EVP_DigestUpdate(self->ctx, (const void*)cp, process)) {
|
|
|
|
_setException(PyExc_ValueError);
|
2019-09-12 10:30:47 -03:00
|
|
|
return -1;
|
2017-05-24 04:04:38 -03:00
|
|
|
}
|
2009-01-08 17:17:16 -04:00
|
|
|
len -= process;
|
|
|
|
cp += process;
|
|
|
|
}
|
2019-09-12 10:30:47 -03:00
|
|
|
return 0;
|
2009-01-08 17:17:16 -04:00
|
|
|
}
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
/* Internal methods for a hash object */
|
|
|
|
|
|
|
|
static void
|
2009-01-08 17:17:16 -04:00
|
|
|
EVP_dealloc(EVPobject *self)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2019-09-25 18:03:30 -03:00
|
|
|
PyTypeObject *tp = Py_TYPE(self);
|
2009-01-08 17:17:16 -04:00
|
|
|
if (self->lock != NULL)
|
|
|
|
PyThread_free_lock(self->lock);
|
2016-09-05 18:19:05 -03:00
|
|
|
EVP_MD_CTX_free(self->ctx);
|
2009-01-08 17:17:16 -04:00
|
|
|
PyObject_Del(self);
|
2019-09-25 18:03:30 -03:00
|
|
|
Py_DECREF(tp);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2016-09-05 18:19:05 -03:00
|
|
|
static int
|
|
|
|
locked_EVP_MD_CTX_copy(EVP_MD_CTX *new_ctx_p, EVPobject *self)
|
2009-05-03 21:45:33 -03:00
|
|
|
{
|
2016-09-05 18:19:05 -03:00
|
|
|
int result;
|
2009-05-03 21:45:33 -03:00
|
|
|
ENTER_HASHLIB(self);
|
2016-09-05 18:19:05 -03:00
|
|
|
result = EVP_MD_CTX_copy(new_ctx_p, self->ctx);
|
2009-05-03 21:45:33 -03:00
|
|
|
LEAVE_HASHLIB(self);
|
2016-09-05 18:19:05 -03:00
|
|
|
return result;
|
2009-05-03 21:45:33 -03:00
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
/* External methods for a hash object */
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HASH.copy as EVP_copy
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
Return a copy of the hash object.
|
|
|
|
[clinic start generated code]*/
|
2009-05-03 21:45:33 -03:00
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
static PyObject *
|
2018-12-27 09:43:43 -04:00
|
|
|
EVP_copy_impl(EVPobject *self)
|
|
|
|
/*[clinic end generated code: output=b370c21cdb8ca0b4 input=31455b6a3e638069]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
|
|
|
EVPobject *newobj;
|
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
if ((newobj = newEVPobject(Py_TYPE(self))) == NULL)
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
|
|
|
|
2016-09-05 18:19:05 -03:00
|
|
|
if (!locked_EVP_MD_CTX_copy(newobj->ctx, self)) {
|
2019-03-04 11:45:41 -04:00
|
|
|
Py_DECREF(newobj);
|
2016-09-05 18:19:05 -03:00
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
return (PyObject *)newobj;
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HASH.digest as EVP_digest
|
|
|
|
|
|
|
|
Return the digest value as a bytes object.
|
|
|
|
[clinic start generated code]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2018-12-27 09:43:43 -04:00
|
|
|
EVP_digest_impl(EVPobject *self)
|
|
|
|
/*[clinic end generated code: output=0f6a3a0da46dc12d input=03561809a419bf00]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
2016-09-05 18:19:05 -03:00
|
|
|
EVP_MD_CTX *temp_ctx;
|
2005-08-21 15:45:59 -03:00
|
|
|
PyObject *retval;
|
|
|
|
unsigned int digest_size;
|
|
|
|
|
2016-09-05 18:19:05 -03:00
|
|
|
temp_ctx = EVP_MD_CTX_new();
|
|
|
|
if (temp_ctx == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
digest_size = EVP_MD_CTX_size(temp_ctx);
|
2017-05-24 04:04:38 -03:00
|
|
|
if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2008-05-26 10:28:38 -03:00
|
|
|
retval = PyBytes_FromStringAndSize((const char *)digest, digest_size);
|
2016-09-05 18:19:05 -03:00
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
2005-08-21 15:45:59 -03:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HASH.hexdigest as EVP_hexdigest
|
|
|
|
|
|
|
|
Return the digest value as a string of hexadecimal digits.
|
|
|
|
[clinic start generated code]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2018-12-27 09:43:43 -04:00
|
|
|
EVP_hexdigest_impl(EVPobject *self)
|
|
|
|
/*[clinic end generated code: output=18e6decbaf197296 input=aff9cf0e4c741a9a]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
2016-09-05 18:19:05 -03:00
|
|
|
EVP_MD_CTX *temp_ctx;
|
2015-04-25 20:42:38 -03:00
|
|
|
unsigned int digest_size;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2016-09-05 18:19:05 -03:00
|
|
|
temp_ctx = EVP_MD_CTX_new();
|
|
|
|
if (temp_ctx == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
/* Get the raw (binary) digest value */
|
2016-09-05 18:19:05 -03:00
|
|
|
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
digest_size = EVP_MD_CTX_size(temp_ctx);
|
2017-05-24 04:04:38 -03:00
|
|
|
if (!EVP_DigestFinal(temp_ctx, digest, NULL)) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return NULL;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2016-09-05 18:19:05 -03:00
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2018-09-12 16:06:42 -03:00
|
|
|
return _Py_strhex((const char *)digest, (Py_ssize_t)digest_size);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HASH.update as EVP_update
|
|
|
|
|
|
|
|
obj: object
|
|
|
|
/
|
|
|
|
|
|
|
|
Update this hash object's state with the provided string.
|
|
|
|
[clinic start generated code]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2018-12-27 09:43:43 -04:00
|
|
|
EVP_update(EVPobject *self, PyObject *obj)
|
|
|
|
/*[clinic end generated code: output=ec1d55ed2432e966 input=9b30ec848f015501]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2019-09-12 10:30:47 -03:00
|
|
|
int result;
|
2007-09-22 23:00:13 -03:00
|
|
|
Py_buffer view;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2009-02-12 03:35:29 -04:00
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(obj, &view);
|
2009-01-08 17:17:16 -04:00
|
|
|
|
|
|
|
if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
|
|
|
|
self->lock = PyThread_allocate_lock();
|
2009-05-03 21:45:33 -03:00
|
|
|
/* fail? lock = NULL and we fail over to non-threaded code. */
|
2008-09-24 19:53:33 -03:00
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2009-01-08 17:17:16 -04:00
|
|
|
if (self->lock != NULL) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
PyThread_acquire_lock(self->lock, 1);
|
2019-09-12 10:30:47 -03:00
|
|
|
result = EVP_hash(self, view.buf, view.len);
|
2009-01-08 17:17:16 -04:00
|
|
|
PyThread_release_lock(self->lock);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
} else {
|
2019-09-12 10:30:47 -03:00
|
|
|
result = EVP_hash(self, view.buf, view.len);
|
2009-01-08 17:17:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyBuffer_Release(&view);
|
2019-09-12 10:30:47 -03:00
|
|
|
|
|
|
|
if (result == -1)
|
|
|
|
return NULL;
|
2009-01-08 17:17:16 -04:00
|
|
|
Py_RETURN_NONE;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef EVP_methods[] = {
|
2018-12-27 09:43:43 -04:00
|
|
|
EVP_UPDATE_METHODDEF
|
|
|
|
EVP_DIGEST_METHODDEF
|
|
|
|
EVP_HEXDIGEST_METHODDEF
|
|
|
|
EVP_COPY_METHODDEF
|
2009-01-08 17:17:16 -04:00
|
|
|
{NULL, NULL} /* sentinel */
|
2005-08-21 15:45:59 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
EVP_get_block_size(EVPobject *self, void *closure)
|
|
|
|
{
|
2009-01-08 17:17:16 -04:00
|
|
|
long block_size;
|
2016-09-05 18:19:05 -03:00
|
|
|
block_size = EVP_MD_CTX_block_size(self->ctx);
|
2009-01-08 17:17:16 -04:00
|
|
|
return PyLong_FromLong(block_size);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
EVP_get_digest_size(EVPobject *self, void *closure)
|
|
|
|
{
|
2009-01-08 17:17:16 -04:00
|
|
|
long size;
|
2016-09-05 18:19:05 -03:00
|
|
|
size = EVP_MD_CTX_size(self->ctx);
|
2009-01-08 17:17:16 -04:00
|
|
|
return PyLong_FromLong(size);
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2019-09-12 09:42:07 -03:00
|
|
|
static PyObject *
|
|
|
|
EVP_get_name(EVPobject *self, void *closure)
|
|
|
|
{
|
2019-09-13 10:31:19 -03:00
|
|
|
return py_digest_name(EVP_MD_CTX_md(self->ctx));
|
2019-09-12 09:42:07 -03:00
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static PyGetSetDef EVP_getseters[] = {
|
|
|
|
{"digest_size",
|
|
|
|
(getter)EVP_get_digest_size, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
|
|
|
{"block_size",
|
|
|
|
(getter)EVP_get_block_size, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
2019-09-12 09:42:07 -03:00
|
|
|
{"name",
|
|
|
|
(getter)EVP_get_name, NULL,
|
|
|
|
NULL,
|
|
|
|
PyDoc_STR("algorithm name.")},
|
2005-08-21 15:45:59 -03:00
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
2009-01-08 17:17:16 -04:00
|
|
|
EVP_repr(EVPobject *self)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2019-09-12 09:42:07 -03:00
|
|
|
PyObject *name_obj, *repr;
|
2019-09-13 10:31:19 -03:00
|
|
|
name_obj = py_digest_name(EVP_MD_CTX_md(self->ctx));
|
2019-09-12 09:42:07 -03:00
|
|
|
if (!name_obj) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-16 17:27:06 -03:00
|
|
|
repr = PyUnicode_FromFormat("<%U %s object @ %p>",
|
|
|
|
name_obj, Py_TYPE(self)->tp_name, self);
|
2019-09-12 09:42:07 -03:00
|
|
|
Py_DECREF(name_obj);
|
|
|
|
return repr;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2018-12-30 21:54:53 -04:00
|
|
|
PyDoc_STRVAR(hashtype_doc,
|
|
|
|
"HASH(name, string=b\'\')\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"A hash is an object used to calculate a checksum of a string of information.\n"
|
|
|
|
"\n"
|
|
|
|
"Methods:\n"
|
|
|
|
"\n"
|
|
|
|
"update() -- updates the current digest with an additional string\n"
|
|
|
|
"digest() -- return the current digest value\n"
|
|
|
|
"hexdigest() -- return the current digest as a string of hexadecimal digits\n"
|
|
|
|
"copy() -- return a copy of the current hash object\n"
|
|
|
|
"\n"
|
|
|
|
"Attributes:\n"
|
|
|
|
"\n"
|
|
|
|
"name -- the hash algorithm being used by this object\n"
|
|
|
|
"digest_size -- number of bytes in this hashes output");
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2019-09-25 18:03:30 -03:00
|
|
|
static PyType_Slot EVPtype_slots[] = {
|
|
|
|
{Py_tp_dealloc, EVP_dealloc},
|
|
|
|
{Py_tp_repr, EVP_repr},
|
|
|
|
{Py_tp_doc, (char *)hashtype_doc},
|
|
|
|
{Py_tp_methods, EVP_methods},
|
|
|
|
{Py_tp_getset, EVP_getseters},
|
2020-05-16 17:27:06 -03:00
|
|
|
{Py_tp_new, _disabled_new},
|
2019-09-25 18:03:30 -03:00
|
|
|
{0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec EVPtype_spec = {
|
2005-08-21 15:45:59 -03:00
|
|
|
"_hashlib.HASH", /*tp_name*/
|
2009-01-08 17:17:16 -04:00
|
|
|
sizeof(EVPobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
2019-09-25 18:03:30 -03:00
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
|
|
EVPtype_slots
|
2005-08-21 15:45:59 -03:00
|
|
|
};
|
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HASHXOF.digest as EVPXOF_digest
|
|
|
|
|
|
|
|
length: Py_ssize_t
|
|
|
|
|
|
|
|
Return the digest value as a bytes object.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
EVPXOF_digest_impl(EVPobject *self, Py_ssize_t length)
|
|
|
|
/*[clinic end generated code: output=ef9320c23280efad input=816a6537cea3d1db]*/
|
|
|
|
{
|
|
|
|
EVP_MD_CTX *temp_ctx;
|
|
|
|
PyObject *retval = PyBytes_FromStringAndSize(NULL, length);
|
|
|
|
|
|
|
|
if (retval == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp_ctx = EVP_MD_CTX_new();
|
|
|
|
if (temp_ctx == NULL) {
|
|
|
|
Py_DECREF(retval);
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
|
|
|
|
Py_DECREF(retval);
|
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
if (!EVP_DigestFinalXOF(temp_ctx,
|
|
|
|
(unsigned char*)PyBytes_AS_STRING(retval),
|
|
|
|
length)) {
|
|
|
|
Py_DECREF(retval);
|
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HASHXOF.hexdigest as EVPXOF_hexdigest
|
|
|
|
|
|
|
|
length: Py_ssize_t
|
|
|
|
|
|
|
|
Return the digest value as a string of hexadecimal digits.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
EVPXOF_hexdigest_impl(EVPobject *self, Py_ssize_t length)
|
|
|
|
/*[clinic end generated code: output=eb3e6ee7788bf5b2 input=5f9d6a8f269e34df]*/
|
|
|
|
{
|
|
|
|
unsigned char *digest;
|
|
|
|
EVP_MD_CTX *temp_ctx;
|
|
|
|
PyObject *retval;
|
|
|
|
|
|
|
|
digest = (unsigned char*)PyMem_Malloc(length);
|
|
|
|
if (digest == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp_ctx = EVP_MD_CTX_new();
|
|
|
|
if (temp_ctx == NULL) {
|
|
|
|
PyMem_Free(digest);
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the raw (binary) digest value */
|
|
|
|
if (!locked_EVP_MD_CTX_copy(temp_ctx, self)) {
|
|
|
|
PyMem_Free(digest);
|
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
if (!EVP_DigestFinalXOF(temp_ctx, digest, length)) {
|
|
|
|
PyMem_Free(digest);
|
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
EVP_MD_CTX_free(temp_ctx);
|
|
|
|
|
|
|
|
retval = _Py_strhex((const char *)digest, length);
|
|
|
|
PyMem_Free(digest);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef EVPXOF_methods[] = {
|
|
|
|
EVPXOF_DIGEST_METHODDEF
|
|
|
|
EVPXOF_HEXDIGEST_METHODDEF
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
EVPXOF_get_digest_size(EVPobject *self, void *closure)
|
|
|
|
{
|
|
|
|
return PyLong_FromLong(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyGetSetDef EVPXOF_getseters[] = {
|
|
|
|
{"digest_size",
|
|
|
|
(getter)EVPXOF_get_digest_size, NULL,
|
|
|
|
NULL,
|
|
|
|
NULL},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
PyDoc_STRVAR(hashxoftype_doc,
|
|
|
|
"HASHXOF(name, string=b\'\')\n"
|
|
|
|
"--\n"
|
|
|
|
"\n"
|
|
|
|
"A hash is an object used to calculate a checksum of a string of information.\n"
|
|
|
|
"\n"
|
|
|
|
"Methods:\n"
|
|
|
|
"\n"
|
|
|
|
"update() -- updates the current digest with an additional string\n"
|
|
|
|
"digest(length) -- return the current digest value\n"
|
|
|
|
"hexdigest(length) -- return the current digest as a string of hexadecimal digits\n"
|
|
|
|
"copy() -- return a copy of the current hash object\n"
|
|
|
|
"\n"
|
|
|
|
"Attributes:\n"
|
|
|
|
"\n"
|
|
|
|
"name -- the hash algorithm being used by this object\n"
|
|
|
|
"digest_size -- number of bytes in this hashes output");
|
|
|
|
|
|
|
|
static PyType_Slot EVPXOFtype_slots[] = {
|
|
|
|
{Py_tp_doc, (char *)hashxoftype_doc},
|
|
|
|
{Py_tp_methods, EVPXOF_methods},
|
|
|
|
{Py_tp_getset, EVPXOF_getseters},
|
|
|
|
{Py_tp_new, _disabled_new},
|
|
|
|
{0, 0},
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyType_Spec EVPXOFtype_spec = {
|
|
|
|
"_hashlib.HASHXOF", /*tp_name*/
|
|
|
|
sizeof(EVPobject), /*tp_basicsize*/
|
|
|
|
0, /*tp_itemsize*/
|
|
|
|
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
|
|
|
|
EVPXOFtype_slots
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
static PyObject *
|
2020-05-16 17:27:06 -03:00
|
|
|
EVPnew(PyObject *module, const EVP_MD *digest,
|
2019-09-12 21:30:00 -03:00
|
|
|
const unsigned char *cp, Py_ssize_t len, int usedforsecurity)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2019-09-12 10:30:47 -03:00
|
|
|
int result = 0;
|
2005-08-21 15:45:59 -03:00
|
|
|
EVPobject *self;
|
2020-05-16 17:27:06 -03:00
|
|
|
PyTypeObject *type = get_hashlib_state(module)->EVPtype;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2019-09-12 09:42:07 -03:00
|
|
|
if (!digest) {
|
2005-08-21 15:45:59 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError, "unsupported hash type");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
|
|
|
if ((EVP_MD_flags(digest) & EVP_MD_FLAG_XOF) == EVP_MD_FLAG_XOF) {
|
|
|
|
type = get_hashlib_state(module)->EVPXOFtype;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if ((self = newEVPobject(type)) == NULL)
|
2005-08-21 15:45:59 -03:00
|
|
|
return NULL;
|
|
|
|
|
2019-09-12 21:30:00 -03:00
|
|
|
if (!usedforsecurity) {
|
|
|
|
#ifdef EVP_MD_CTX_FLAG_NON_FIPS_ALLOW
|
|
|
|
EVP_MD_CTX_set_flags(self->ctx, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-27 10:03:53 -03:00
|
|
|
if (!EVP_DigestInit_ex(self->ctx, digest, NULL)) {
|
2019-09-12 09:42:07 -03:00
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2008-09-24 19:53:33 -03:00
|
|
|
if (cp && len) {
|
2009-01-08 17:17:16 -04:00
|
|
|
if (len >= HASHLIB_GIL_MINSIZE) {
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2019-09-12 10:30:47 -03:00
|
|
|
result = EVP_hash(self, cp, len);
|
2009-01-08 17:17:16 -04:00
|
|
|
Py_END_ALLOW_THREADS
|
2008-09-24 19:53:33 -03:00
|
|
|
} else {
|
2019-09-12 10:30:47 -03:00
|
|
|
result = EVP_hash(self, cp, len);
|
|
|
|
}
|
|
|
|
if (result == -1) {
|
|
|
|
Py_DECREF(self);
|
|
|
|
return NULL;
|
2008-09-24 19:53:33 -03:00
|
|
|
}
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
return (PyObject *)self;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* The module-level function: new() */
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.new as EVP_new
|
|
|
|
|
|
|
|
name as name_obj: object
|
2019-09-14 06:24:05 -03:00
|
|
|
string as data_obj: object(c_default="NULL") = b''
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2018-12-27 09:43:43 -04:00
|
|
|
|
|
|
|
Return a new hash object using the named algorithm.
|
|
|
|
|
|
|
|
An optional string argument may be provided and will be
|
|
|
|
automatically hashed.
|
|
|
|
|
|
|
|
The MD5 and SHA1 algorithms are always supported.
|
|
|
|
[clinic start generated code]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
EVP_new_impl(PyObject *module, PyObject *name_obj, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
2019-09-14 06:24:05 -03:00
|
|
|
/*[clinic end generated code: output=ddd5053f92dffe90 input=c24554d0337be1b0]*/
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2007-09-22 23:00:13 -03:00
|
|
|
Py_buffer view = { 0 };
|
2007-08-25 23:58:36 -03:00
|
|
|
PyObject *ret_obj;
|
2005-08-21 15:45:59 -03:00
|
|
|
char *name;
|
2020-05-16 17:27:06 -03:00
|
|
|
const EVP_MD *digest = NULL;
|
2005-08-21 15:45:59 -03:00
|
|
|
|
|
|
|
if (!PyArg_Parse(name_obj, "s", &name)) {
|
|
|
|
PyErr_SetString(PyExc_TypeError, "name must be a string");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-08-25 23:58:36 -03:00
|
|
|
if (data_obj)
|
2009-02-12 03:35:29 -04:00
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
|
2007-08-25 23:58:36 -03:00
|
|
|
|
2019-09-13 10:31:19 -03:00
|
|
|
digest = py_digest_by_name(name);
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
ret_obj = EVPnew(module, digest,
|
2019-09-12 21:30:00 -03:00
|
|
|
(unsigned char*)view.buf, view.len,
|
|
|
|
usedforsecurity);
|
2007-08-25 23:58:36 -03:00
|
|
|
|
|
|
|
if (data_obj)
|
2008-08-13 12:53:07 -03:00
|
|
|
PyBuffer_Release(&view);
|
2007-08-25 23:58:36 -03:00
|
|
|
return ret_obj;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|
|
|
|
|
2019-09-12 09:42:07 -03:00
|
|
|
static PyObject*
|
2019-09-12 21:30:00 -03:00
|
|
|
EVP_fast_new(PyObject *module, PyObject *data_obj, const EVP_MD *digest,
|
|
|
|
int usedforsecurity)
|
2019-09-12 09:42:07 -03:00
|
|
|
{
|
|
|
|
Py_buffer view = { 0 };
|
|
|
|
PyObject *ret_obj;
|
|
|
|
|
|
|
|
if (data_obj)
|
|
|
|
GET_BUFFER_VIEW_OR_ERROUT(data_obj, &view);
|
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
ret_obj = EVPnew(module, digest,
|
2019-09-12 21:30:00 -03:00
|
|
|
(unsigned char*)view.buf, view.len,
|
|
|
|
usedforsecurity);
|
2019-09-12 09:42:07 -03:00
|
|
|
|
|
|
|
if (data_obj)
|
|
|
|
PyBuffer_Release(&view);
|
|
|
|
|
|
|
|
return ret_obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_md5
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2019-09-12 09:42:07 -03:00
|
|
|
|
|
|
|
Returns a md5 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_hashlib_openssl_md5_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=87b0186440a44f8c input=990e36d5e689b16e]*/
|
2019-09-12 09:42:07 -03:00
|
|
|
{
|
2019-09-12 21:30:00 -03:00
|
|
|
return EVP_fast_new(module, data_obj, EVP_md5(), usedforsecurity);
|
2019-09-12 09:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha1
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2019-09-12 09:42:07 -03:00
|
|
|
|
|
|
|
Returns a sha1 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_hashlib_openssl_sha1_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=6813024cf690670d input=948f2f4b6deabc10]*/
|
2019-09-12 09:42:07 -03:00
|
|
|
{
|
2019-09-12 21:30:00 -03:00
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha1(), usedforsecurity);
|
2019-09-12 09:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha224
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2019-09-12 09:42:07 -03:00
|
|
|
|
|
|
|
Returns a sha224 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_hashlib_openssl_sha224_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=a2dfe7cc4eb14ebb input=f9272821fadca505]*/
|
2019-09-12 09:42:07 -03:00
|
|
|
{
|
2019-09-12 21:30:00 -03:00
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha224(), usedforsecurity);
|
2019-09-12 09:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha256
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2019-09-12 09:42:07 -03:00
|
|
|
|
|
|
|
Returns a sha256 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_hashlib_openssl_sha256_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=1f874a34870f0a68 input=549fad9d2930d4c5]*/
|
2019-09-12 09:42:07 -03:00
|
|
|
{
|
2019-09-12 21:30:00 -03:00
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha256(), usedforsecurity);
|
2019-09-12 09:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha384
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2019-09-12 09:42:07 -03:00
|
|
|
|
|
|
|
Returns a sha384 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_hashlib_openssl_sha384_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=58529eff9ca457b2 input=48601a6e3bf14ad7]*/
|
2019-09-12 09:42:07 -03:00
|
|
|
{
|
2019-09-12 21:30:00 -03:00
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha384(), usedforsecurity);
|
2019-09-12 09:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha512
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
2019-09-12 21:30:00 -03:00
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
2019-09-12 09:42:07 -03:00
|
|
|
|
|
|
|
Returns a sha512 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2019-09-12 21:30:00 -03:00
|
|
|
_hashlib_openssl_sha512_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=2c744c9e4a40d5f6 input=c5c46a2a817aa98f]*/
|
2019-09-12 09:42:07 -03:00
|
|
|
{
|
2019-09-12 21:30:00 -03:00
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha512(), usedforsecurity);
|
2019-09-12 09:42:07 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-16 17:27:06 -03:00
|
|
|
#ifdef PY_OPENSSL_HAS_SHA3
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha3_224
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
|
|
|
|
|
|
|
Returns a sha3-224 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_openssl_sha3_224_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=144641c1d144b974 input=e3a01b2888916157]*/
|
|
|
|
{
|
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha3_224(), usedforsecurity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha3_256
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
|
|
|
|
|
|
|
Returns a sha3-256 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_openssl_sha3_256_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=c61f1ab772d06668 input=e2908126c1b6deed]*/
|
|
|
|
{
|
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha3_256(), usedforsecurity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha3_384
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
|
|
|
|
|
|
|
Returns a sha3-384 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_openssl_sha3_384_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=f68e4846858cf0ee input=ec0edf5c792f8252]*/
|
|
|
|
{
|
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha3_384(), usedforsecurity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.openssl_sha3_512
|
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
|
|
|
|
|
|
|
Returns a sha3-512 hash object; optionally initialized with a string
|
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_openssl_sha3_512_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=2eede478c159354a input=64e2cc0c094d56f4]*/
|
|
|
|
{
|
|
|
|
return EVP_fast_new(module, data_obj, EVP_sha3_512(), usedforsecurity);
|
|
|
|
}
|
|
|
|
#endif /* PY_OPENSSL_HAS_SHA3 */
|
|
|
|
|
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
|
|
|
/*[clinic input]
|
2020-05-17 13:32:38 -03:00
|
|
|
_hashlib.openssl_shake_128
|
2020-05-16 17:27:06 -03:00
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
|
|
|
|
2020-05-17 13:32:38 -03:00
|
|
|
Returns a shake-128 variable hash object; optionally initialized with a string
|
2020-05-16 17:27:06 -03:00
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2020-05-17 13:32:38 -03:00
|
|
|
_hashlib_openssl_shake_128_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=bc49cdd8ada1fa97 input=6c9d67440eb33ec8]*/
|
2020-05-16 17:27:06 -03:00
|
|
|
{
|
|
|
|
return EVP_fast_new(module, data_obj, EVP_shake128(), usedforsecurity);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
2020-05-17 13:32:38 -03:00
|
|
|
_hashlib.openssl_shake_256
|
2020-05-16 17:27:06 -03:00
|
|
|
|
|
|
|
string as data_obj: object(py_default="b''") = NULL
|
|
|
|
*
|
|
|
|
usedforsecurity: bool = True
|
|
|
|
|
2020-05-17 13:32:38 -03:00
|
|
|
Returns a shake-256 variable hash object; optionally initialized with a string
|
2020-05-16 17:27:06 -03:00
|
|
|
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2020-05-17 13:32:38 -03:00
|
|
|
_hashlib_openssl_shake_256_impl(PyObject *module, PyObject *data_obj,
|
|
|
|
int usedforsecurity)
|
|
|
|
/*[clinic end generated code: output=358d213be8852df7 input=479cbe9fefd4a9f8]*/
|
2020-05-16 17:27:06 -03:00
|
|
|
{
|
|
|
|
return EVP_fast_new(module, data_obj, EVP_shake256(), usedforsecurity);
|
|
|
|
}
|
|
|
|
#endif /* PY_OPENSSL_HAS_SHAKE */
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.pbkdf2_hmac as pbkdf2_hmac
|
|
|
|
|
|
|
|
hash_name: str
|
|
|
|
password: Py_buffer
|
|
|
|
salt: Py_buffer
|
|
|
|
iterations: long
|
|
|
|
dklen as dklen_obj: object = None
|
|
|
|
|
|
|
|
Password based key derivation function 2 (PKCS #5 v2.0) with HMAC as pseudorandom function.
|
|
|
|
[clinic start generated code]*/
|
2013-10-12 19:52:43 -03:00
|
|
|
|
|
|
|
static PyObject *
|
2018-12-27 09:43:43 -04:00
|
|
|
pbkdf2_hmac_impl(PyObject *module, const char *hash_name,
|
|
|
|
Py_buffer *password, Py_buffer *salt, long iterations,
|
|
|
|
PyObject *dklen_obj)
|
|
|
|
/*[clinic end generated code: output=144b76005416599b input=ed3ab0d2d28b5d5c]*/
|
2013-10-12 19:52:43 -03:00
|
|
|
{
|
2018-12-27 09:43:43 -04:00
|
|
|
PyObject *key_obj = NULL;
|
|
|
|
char *key;
|
|
|
|
long dklen;
|
2013-10-12 19:52:43 -03:00
|
|
|
int retval;
|
|
|
|
const EVP_MD *digest;
|
|
|
|
|
2020-05-17 08:49:10 -03:00
|
|
|
digest = py_digest_by_name(hash_name);
|
2013-10-12 19:52:43 -03:00
|
|
|
if (digest == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "unsupported hash type");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
if (password->len > INT_MAX) {
|
2013-10-12 19:52:43 -03:00
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"password is too long.");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
2018-12-27 09:43:43 -04:00
|
|
|
if (salt->len > INT_MAX) {
|
2013-10-12 19:52:43 -03:00
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"salt is too long.");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iterations < 1) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"iteration value must be greater than 0.");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (iterations > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"iteration value is too great.");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dklen_obj == Py_None) {
|
|
|
|
dklen = EVP_MD_size(digest);
|
|
|
|
} else {
|
|
|
|
dklen = PyLong_AsLong(dklen_obj);
|
|
|
|
if ((dklen == -1) && PyErr_Occurred()) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dklen < 1) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"key length must be greater than 0.");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
if (dklen > INT_MAX) {
|
|
|
|
/* INT_MAX is always smaller than dkLen max (2^32 - 1) * hLen */
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"key length is too great.");
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
key_obj = PyBytes_FromStringAndSize(NULL, dklen);
|
|
|
|
if (key_obj == NULL) {
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
key = PyBytes_AS_STRING(key_obj);
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
2018-12-27 09:43:43 -04:00
|
|
|
retval = PKCS5_PBKDF2_HMAC((char*)password->buf, (int)password->len,
|
|
|
|
(unsigned char *)salt->buf, (int)salt->len,
|
2016-09-05 18:19:05 -03:00
|
|
|
iterations, digest, dklen,
|
|
|
|
(unsigned char *)key);
|
2013-10-12 19:52:43 -03:00
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (!retval) {
|
|
|
|
Py_CLEAR(key_obj);
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
return key_obj;
|
|
|
|
}
|
|
|
|
|
2016-09-06 15:22:28 -03:00
|
|
|
#if OPENSSL_VERSION_NUMBER > 0x10100000L && !defined(OPENSSL_NO_SCRYPT) && !defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
#define PY_SCRYPT 1
|
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
/* XXX: Parameters salt, n, r and p should be required keyword-only parameters.
|
|
|
|
They are optional in the Argument Clinic declaration only due to a
|
|
|
|
limitation of PyArg_ParseTupleAndKeywords. */
|
|
|
|
|
2016-09-06 15:22:28 -03:00
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.scrypt
|
|
|
|
|
|
|
|
password: Py_buffer
|
|
|
|
*
|
|
|
|
salt: Py_buffer = None
|
|
|
|
n as n_obj: object(subclass_of='&PyLong_Type') = None
|
|
|
|
r as r_obj: object(subclass_of='&PyLong_Type') = None
|
|
|
|
p as p_obj: object(subclass_of='&PyLong_Type') = None
|
|
|
|
maxmem: long = 0
|
|
|
|
dklen: long = 64
|
|
|
|
|
|
|
|
|
|
|
|
scrypt password-based key derivation function.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
|
|
|
|
PyObject *n_obj, PyObject *r_obj, PyObject *p_obj,
|
|
|
|
long maxmem, long dklen)
|
|
|
|
/*[clinic end generated code: output=14849e2aa2b7b46c input=48a7d63bf3f75c42]*/
|
|
|
|
{
|
|
|
|
PyObject *key_obj = NULL;
|
|
|
|
char *key;
|
|
|
|
int retval;
|
|
|
|
unsigned long n, r, p;
|
|
|
|
|
|
|
|
if (password->len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"password is too long.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (salt->buf == NULL) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"salt is required");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (salt->len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"salt is too long.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
n = PyLong_AsUnsignedLong(n_obj);
|
|
|
|
if (n == (unsigned long) -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"n is required and must be an unsigned int");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (n < 2 || n & (n - 1)) {
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"n must be a power of 2.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = PyLong_AsUnsignedLong(r_obj);
|
|
|
|
if (r == (unsigned long) -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"r is required and must be an unsigned int");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
p = PyLong_AsUnsignedLong(p_obj);
|
|
|
|
if (p == (unsigned long) -1 && PyErr_Occurred()) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"p is required and must be an unsigned int");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (maxmem < 0 || maxmem > INT_MAX) {
|
2017-11-08 18:44:44 -04:00
|
|
|
/* OpenSSL 1.1.0 restricts maxmem to 32 MiB. It may change in the
|
2016-09-06 15:22:28 -03:00
|
|
|
future. The maxmem constant is private to OpenSSL. */
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"maxmem must be positive and smaller than %d",
|
|
|
|
INT_MAX);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dklen < 1 || dklen > INT_MAX) {
|
|
|
|
PyErr_Format(PyExc_ValueError,
|
|
|
|
"dklen must be greater than 0 and smaller than %d",
|
|
|
|
INT_MAX);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* let OpenSSL validate the rest */
|
|
|
|
retval = EVP_PBE_scrypt(NULL, 0, NULL, 0, n, r, p, maxmem, NULL, 0);
|
|
|
|
if (!retval) {
|
|
|
|
/* sorry, can't do much better */
|
|
|
|
PyErr_SetString(PyExc_ValueError,
|
2019-03-06 10:35:35 -04:00
|
|
|
"Invalid parameter combination for n, r, p, maxmem.");
|
2016-09-06 15:22:28 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
key_obj = PyBytes_FromStringAndSize(NULL, dklen);
|
|
|
|
if (key_obj == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
key = PyBytes_AS_STRING(key_obj);
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
retval = EVP_PBE_scrypt(
|
|
|
|
(const char*)password->buf, (size_t)password->len,
|
|
|
|
(const unsigned char *)salt->buf, (size_t)salt->len,
|
|
|
|
n, r, p, maxmem,
|
|
|
|
(unsigned char *)key, (size_t)dklen
|
|
|
|
);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (!retval) {
|
|
|
|
Py_CLEAR(key_obj);
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return key_obj;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-01-27 04:53:43 -04:00
|
|
|
/* Fast HMAC for hmac.digest()
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*[clinic input]
|
2020-05-17 08:49:10 -03:00
|
|
|
_hashlib.hmac_digest as _hashlib_hmac_singleshot
|
2018-01-27 04:53:43 -04:00
|
|
|
|
|
|
|
key: Py_buffer
|
|
|
|
msg: Py_buffer
|
|
|
|
digest: str
|
|
|
|
|
2018-07-31 03:50:16 -03:00
|
|
|
Single-shot HMAC.
|
2018-01-27 04:53:43 -04:00
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
2020-05-17 08:49:10 -03:00
|
|
|
_hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
|
|
|
|
Py_buffer *msg, const char *digest)
|
|
|
|
/*[clinic end generated code: output=15658ede5ab98185 input=019dffc571909a46]*/
|
2018-01-27 04:53:43 -04:00
|
|
|
{
|
|
|
|
unsigned char md[EVP_MAX_MD_SIZE] = {0};
|
|
|
|
unsigned int md_len = 0;
|
|
|
|
unsigned char *result;
|
|
|
|
const EVP_MD *evp;
|
|
|
|
|
2020-05-17 08:49:10 -03:00
|
|
|
evp = py_digest_by_name(digest);
|
2018-01-27 04:53:43 -04:00
|
|
|
if (evp == NULL) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "unsupported hash type");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (key->len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"key is too long.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (msg->len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"msg is too long.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Py_BEGIN_ALLOW_THREADS
|
|
|
|
result = HMAC(
|
|
|
|
evp,
|
|
|
|
(const void*)key->buf, (int)key->len,
|
|
|
|
(const unsigned char*)msg->buf, (int)msg->len,
|
|
|
|
md, &md_len
|
|
|
|
);
|
|
|
|
Py_END_ALLOW_THREADS
|
|
|
|
|
|
|
|
if (result == NULL) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyBytes_FromStringAndSize((const char*)md, md_len);
|
|
|
|
}
|
|
|
|
|
2020-05-17 08:49:10 -03:00
|
|
|
/* OpenSSL-based HMAC implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int _hmac_update(HMACobject*, PyObject*);
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.hmac_new
|
|
|
|
|
|
|
|
key: Py_buffer
|
|
|
|
msg as msg_obj: object(c_default="NULL") = b''
|
|
|
|
digestmod: str(c_default="NULL") = None
|
|
|
|
|
|
|
|
Return a new hmac object.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, PyObject *msg_obj,
|
|
|
|
const char *digestmod)
|
|
|
|
/*[clinic end generated code: output=9a35673be0cbea1b input=a0878868eb190134]*/
|
|
|
|
{
|
|
|
|
PyTypeObject *type = get_hashlib_state(module)->HMACtype;
|
|
|
|
const EVP_MD *digest;
|
|
|
|
HMAC_CTX *ctx = NULL;
|
|
|
|
HMACobject *self = NULL;
|
|
|
|
int r;
|
|
|
|
|
2020-05-19 19:35:51 -03:00
|
|
|
if (key->len > INT_MAX) {
|
|
|
|
PyErr_SetString(PyExc_OverflowError,
|
|
|
|
"key is too long.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-05-17 08:49:10 -03:00
|
|
|
if ((digestmod == NULL) || !strlen(digestmod)) {
|
|
|
|
PyErr_SetString(
|
|
|
|
PyExc_TypeError, "Missing required parameter 'digestmod'.");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
digest = py_digest_by_name(digestmod);
|
|
|
|
if (!digest) {
|
|
|
|
PyErr_SetString(PyExc_ValueError, "unknown hash function");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = HMAC_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
r = HMAC_Init_ex(
|
|
|
|
ctx,
|
|
|
|
(const char*)key->buf,
|
2020-05-19 19:35:51 -03:00
|
|
|
(int)key->len,
|
2020-05-17 08:49:10 -03:00
|
|
|
digest,
|
|
|
|
NULL /*impl*/);
|
|
|
|
if (r == 0) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
self = (HMACobject *)PyObject_New(HMACobject, type);
|
|
|
|
if (self == NULL) {
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
self->ctx = ctx;
|
|
|
|
self->lock = NULL;
|
|
|
|
|
|
|
|
if ((msg_obj != NULL) && (msg_obj != Py_None)) {
|
|
|
|
if (!_hmac_update(self, msg_obj))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (PyObject*)self;
|
|
|
|
|
|
|
|
error:
|
|
|
|
if (ctx) HMAC_CTX_free(ctx);
|
|
|
|
if (self) PyObject_Del(self);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* helper functions */
|
|
|
|
static int
|
|
|
|
locked_HMAC_CTX_copy(HMAC_CTX *new_ctx_p, HMACobject *self)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
ENTER_HASHLIB(self);
|
|
|
|
result = HMAC_CTX_copy(new_ctx_p, self->ctx);
|
|
|
|
LEAVE_HASHLIB(self);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned int
|
|
|
|
_hmac_digest_size(HMACobject *self)
|
|
|
|
{
|
|
|
|
unsigned int digest_size = EVP_MD_size(HMAC_CTX_get_md(self->ctx));
|
|
|
|
assert(digest_size <= EVP_MAX_MD_SIZE);
|
|
|
|
return digest_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_hmac_update(HMACobject *self, PyObject *obj)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
Py_buffer view = {0};
|
|
|
|
|
|
|
|
GET_BUFFER_VIEW_OR_ERROR(obj, &view, return 0);
|
|
|
|
|
|
|
|
if (self->lock == NULL && view.len >= HASHLIB_GIL_MINSIZE) {
|
|
|
|
self->lock = PyThread_allocate_lock();
|
|
|
|
/* fail? lock = NULL and we fail over to non-threaded code. */
|
|
|
|
}
|
|
|
|
|
|
|
|
if (self->lock != NULL) {
|
|
|
|
ENTER_HASHLIB(self);
|
|
|
|
r = HMAC_Update(self->ctx, (const unsigned char*)view.buf, view.len);
|
|
|
|
LEAVE_HASHLIB(self);
|
|
|
|
} else {
|
|
|
|
r = HMAC_Update(self->ctx, (const unsigned char*)view.buf, view.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyBuffer_Release(&view);
|
|
|
|
|
|
|
|
if (r == 0) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HMAC.copy
|
|
|
|
|
|
|
|
Return a copy ("clone") of the HMAC object.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_HMAC_copy_impl(HMACobject *self)
|
|
|
|
/*[clinic end generated code: output=29aa28b452833127 input=e2fa6a05db61a4d6]*/
|
|
|
|
{
|
|
|
|
HMACobject *retval;
|
|
|
|
|
|
|
|
HMAC_CTX *ctx = HMAC_CTX_new();
|
|
|
|
if (ctx == NULL) {
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
if (!locked_HMAC_CTX_copy(ctx, self)) {
|
|
|
|
HMAC_CTX_free(ctx);
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
|
|
|
|
retval = (HMACobject *)PyObject_New(HMACobject, Py_TYPE(self));
|
|
|
|
if (retval == NULL) {
|
|
|
|
HMAC_CTX_free(ctx);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
retval->ctx = ctx;
|
|
|
|
retval->lock = NULL;
|
|
|
|
|
|
|
|
return (PyObject *)retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_hmac_dealloc(HMACobject *self)
|
|
|
|
{
|
|
|
|
PyTypeObject *tp = Py_TYPE(self);
|
|
|
|
if (self->lock != NULL) {
|
|
|
|
PyThread_free_lock(self->lock);
|
|
|
|
}
|
|
|
|
HMAC_CTX_free(self->ctx);
|
|
|
|
PyObject_Del(self);
|
|
|
|
Py_DECREF(tp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hmac_repr(HMACobject *self)
|
|
|
|
{
|
|
|
|
PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx));
|
|
|
|
if (digest_name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *repr = PyUnicode_FromFormat(
|
|
|
|
"<%U HMAC object @ %p>", digest_name, self
|
|
|
|
);
|
|
|
|
Py_DECREF(digest_name);
|
|
|
|
return repr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HMAC.update
|
|
|
|
msg: object
|
|
|
|
|
|
|
|
Update the HMAC object with msg.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_HMAC_update_impl(HMACobject *self, PyObject *msg)
|
|
|
|
/*[clinic end generated code: output=f31f0ace8c625b00 input=1829173bb3cfd4e6]*/
|
|
|
|
{
|
|
|
|
if (!_hmac_update(self, msg)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
Py_RETURN_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
_hmac_digest(HMACobject *self, unsigned char *buf, unsigned int len)
|
|
|
|
{
|
|
|
|
HMAC_CTX *temp_ctx = HMAC_CTX_new();
|
|
|
|
if (temp_ctx == NULL) {
|
|
|
|
PyErr_NoMemory();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!locked_HMAC_CTX_copy(temp_ctx, self)) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
int r = HMAC_Final(temp_ctx, buf, &len);
|
|
|
|
HMAC_CTX_free(temp_ctx);
|
|
|
|
if (r == 0) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HMAC.digest
|
|
|
|
Return the digest of the bytes passed to the update() method so far.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_HMAC_digest_impl(HMACobject *self)
|
|
|
|
/*[clinic end generated code: output=1b1424355af7a41e input=bff07f74da318fb4]*/
|
|
|
|
{
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int digest_size = _hmac_digest_size(self);
|
|
|
|
if (digest_size == 0) {
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
int r = _hmac_digest(self, digest, digest_size);
|
|
|
|
if (r == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return PyBytes_FromStringAndSize((const char *)digest, digest_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.HMAC.hexdigest
|
|
|
|
|
|
|
|
Return hexadecimal digest of the bytes passed to the update() method so far.
|
|
|
|
|
|
|
|
This may be used to exchange the value safely in email or other non-binary
|
|
|
|
environments.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_HMAC_hexdigest_impl(HMACobject *self)
|
|
|
|
/*[clinic end generated code: output=80d825be1eaae6a7 input=5abc42702874ddcf]*/
|
|
|
|
{
|
|
|
|
unsigned char digest[EVP_MAX_MD_SIZE];
|
|
|
|
unsigned int digest_size = _hmac_digest_size(self);
|
|
|
|
if (digest_size == 0) {
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
int r = _hmac_digest(self, digest, digest_size);
|
|
|
|
if (r == 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return _Py_strhex((const char *)digest, digest_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_hmac_get_digest_size(HMACobject *self, void *closure)
|
|
|
|
{
|
|
|
|
unsigned int digest_size = _hmac_digest_size(self);
|
|
|
|
if (digest_size == 0) {
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
return PyLong_FromLong(digest_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_hmac_get_block_size(HMACobject *self, void *closure)
|
|
|
|
{
|
|
|
|
const EVP_MD *md = HMAC_CTX_get_md(self->ctx);
|
|
|
|
if (md == NULL) {
|
|
|
|
return _setException(PyExc_ValueError);
|
|
|
|
}
|
|
|
|
return PyLong_FromLong(EVP_MD_block_size(md));
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject *
|
|
|
|
_hashlib_hmac_get_name(HMACobject *self, void *closure)
|
|
|
|
{
|
|
|
|
PyObject *digest_name = py_digest_name(HMAC_CTX_get_md(self->ctx));
|
|
|
|
if (digest_name == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyObject *name = PyUnicode_FromFormat("hmac-%U", digest_name);
|
|
|
|
Py_DECREF(digest_name);
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef HMAC_methods[] = {
|
|
|
|
_HASHLIB_HMAC_UPDATE_METHODDEF
|
|
|
|
_HASHLIB_HMAC_DIGEST_METHODDEF
|
|
|
|
_HASHLIB_HMAC_HEXDIGEST_METHODDEF
|
|
|
|
_HASHLIB_HMAC_COPY_METHODDEF
|
|
|
|
{NULL, NULL} /* sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
static PyGetSetDef HMAC_getset[] = {
|
|
|
|
{"digest_size", (getter)_hashlib_hmac_get_digest_size, NULL, NULL, NULL},
|
|
|
|
{"block_size", (getter)_hashlib_hmac_get_block_size, NULL, NULL, NULL},
|
|
|
|
{"name", (getter)_hashlib_hmac_get_name, NULL, NULL, NULL},
|
|
|
|
{NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
PyDoc_STRVAR(hmactype_doc,
|
|
|
|
"The object used to calculate HMAC of a message.\n\
|
|
|
|
\n\
|
|
|
|
Methods:\n\
|
|
|
|
\n\
|
|
|
|
update() -- updates the current digest with an additional string\n\
|
|
|
|
digest() -- return the current digest value\n\
|
|
|
|
hexdigest() -- return the current digest as a string of hexadecimal digits\n\
|
|
|
|
copy() -- return a copy of the current hash object\n\
|
|
|
|
\n\
|
|
|
|
Attributes:\n\
|
|
|
|
\n\
|
|
|
|
name -- the name, including the hash algorithm used by this object\n\
|
|
|
|
digest_size -- number of bytes in digest() output\n");
|
|
|
|
|
|
|
|
static PyType_Slot HMACtype_slots[] = {
|
|
|
|
{Py_tp_doc, (char *)hmactype_doc},
|
|
|
|
{Py_tp_repr, (reprfunc)_hmac_repr},
|
|
|
|
{Py_tp_dealloc,(destructor)_hmac_dealloc},
|
|
|
|
{Py_tp_methods, HMAC_methods},
|
|
|
|
{Py_tp_getset, HMAC_getset},
|
|
|
|
{Py_tp_new, _disabled_new},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
PyType_Spec HMACtype_spec = {
|
|
|
|
"_hashlib.HMAC", /* name */
|
|
|
|
sizeof(HMACobject), /* basicsize */
|
|
|
|
.flags = Py_TPFLAGS_DEFAULT,
|
|
|
|
.slots = HMACtype_slots,
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-09-06 05:30:23 -03:00
|
|
|
/* State for our callback function so that it can accumulate a result. */
|
|
|
|
typedef struct _internal_name_mapper_state {
|
|
|
|
PyObject *set;
|
|
|
|
int error;
|
|
|
|
} _InternalNameMapperState;
|
|
|
|
|
|
|
|
|
|
|
|
/* A callback function to pass to OpenSSL's OBJ_NAME_do_all(...) */
|
|
|
|
static void
|
2019-09-13 10:31:19 -03:00
|
|
|
_openssl_hash_name_mapper(const EVP_MD *md, const char *from,
|
|
|
|
const char *to, void *arg)
|
2010-09-06 05:30:23 -03:00
|
|
|
{
|
|
|
|
_InternalNameMapperState *state = (_InternalNameMapperState *)arg;
|
|
|
|
PyObject *py_name;
|
|
|
|
|
|
|
|
assert(state != NULL);
|
2019-09-13 10:31:19 -03:00
|
|
|
if (md == NULL)
|
2010-09-06 05:30:23 -03:00
|
|
|
return;
|
|
|
|
|
2019-09-13 10:31:19 -03:00
|
|
|
py_name = py_digest_name(md);
|
2010-09-06 05:30:23 -03:00
|
|
|
if (py_name == NULL) {
|
|
|
|
state->error = 1;
|
|
|
|
} else {
|
|
|
|
if (PySet_Add(state->set, py_name) != 0) {
|
|
|
|
state->error = 1;
|
|
|
|
}
|
2013-10-29 08:14:55 -03:00
|
|
|
Py_DECREF(py_name);
|
2010-09-06 05:30:23 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Ask OpenSSL for a list of supported ciphers, filling in a Python set. */
|
2020-05-25 05:44:51 -03:00
|
|
|
static int
|
|
|
|
hashlib_md_meth_names(PyObject *module)
|
2010-09-06 05:30:23 -03:00
|
|
|
{
|
2020-05-25 05:44:51 -03:00
|
|
|
_InternalNameMapperState state = {
|
|
|
|
.set = PyFrozenSet_New(NULL),
|
|
|
|
.error = 0
|
|
|
|
};
|
|
|
|
if (state.set == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-06 05:30:23 -03:00
|
|
|
|
2019-09-13 10:31:19 -03:00
|
|
|
EVP_MD_do_all(&_openssl_hash_name_mapper, &state);
|
2010-09-06 05:30:23 -03:00
|
|
|
|
|
|
|
if (state.error) {
|
|
|
|
Py_DECREF(state.set);
|
2020-05-25 05:44:51 -03:00
|
|
|
return -1;
|
2010-09-06 05:30:23 -03:00
|
|
|
}
|
2020-05-25 05:44:51 -03:00
|
|
|
|
|
|
|
if (PyModule_AddObject(module, "openssl_md_meth_names", state.set) < 0) {
|
|
|
|
Py_DECREF(state.set);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2010-09-06 05:30:23 -03:00
|
|
|
}
|
|
|
|
|
2020-04-29 13:04:22 -03:00
|
|
|
/* LibreSSL doesn't support FIPS:
|
|
|
|
https://marc.info/?l=openbsd-misc&m=139819485423701&w=2
|
|
|
|
|
|
|
|
Ted Unangst wrote: "I figured I should mention our current libressl policy
|
|
|
|
wrt FIPS mode. It's gone and it's not coming back." */
|
|
|
|
#ifndef LIBRESSL_VERSION_NUMBER
|
|
|
|
/*[clinic input]
|
|
|
|
_hashlib.get_fips_mode -> int
|
|
|
|
|
|
|
|
Determine the OpenSSL FIPS mode of operation.
|
|
|
|
|
2020-05-15 13:28:05 -03:00
|
|
|
For OpenSSL 3.0.0 and newer it returns the state of the default provider
|
|
|
|
in the default OSSL context. It's not quite the same as FIPS_mode() but good
|
|
|
|
enough for unittests.
|
|
|
|
|
2020-04-29 13:04:22 -03:00
|
|
|
Effectively any non-zero return value indicates FIPS mode;
|
|
|
|
values other than 1 may have additional significance.
|
|
|
|
[clinic start generated code]*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
_hashlib_get_fips_mode_impl(PyObject *module)
|
2020-05-15 13:28:05 -03:00
|
|
|
/*[clinic end generated code: output=87eece1bab4d3fa9 input=2db61538c41c6fef]*/
|
2020-04-29 13:04:22 -03:00
|
|
|
|
|
|
|
{
|
2020-05-15 13:28:05 -03:00
|
|
|
int result;
|
|
|
|
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
|
|
|
|
result = EVP_default_properties_is_fips_enabled(NULL);
|
|
|
|
#else
|
2020-04-29 13:04:22 -03:00
|
|
|
ERR_clear_error();
|
2020-05-15 13:28:05 -03:00
|
|
|
result = FIPS_mode();
|
2020-04-29 13:04:22 -03:00
|
|
|
if (result == 0) {
|
|
|
|
// "If the library was built without support of the FIPS Object Module,
|
|
|
|
// then the function will return 0 with an error code of
|
|
|
|
// CRYPTO_R_FIPS_MODE_NOT_SUPPORTED (0x0f06d065)."
|
|
|
|
// But 0 is also a valid result value.
|
|
|
|
unsigned long errcode = ERR_peek_last_error();
|
|
|
|
if (errcode) {
|
|
|
|
_setException(PyExc_ValueError);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2020-05-15 13:28:05 -03:00
|
|
|
#endif
|
2020-04-29 13:04:22 -03:00
|
|
|
}
|
|
|
|
#endif // !LIBRESSL_VERSION_NUMBER
|
|
|
|
|
|
|
|
|
2005-08-21 15:45:59 -03:00
|
|
|
/* List of functions exported by this module */
|
|
|
|
|
|
|
|
static struct PyMethodDef EVP_functions[] = {
|
2018-12-27 09:43:43 -04:00
|
|
|
EVP_NEW_METHODDEF
|
|
|
|
PBKDF2_HMAC_METHODDEF
|
2016-09-06 15:22:28 -03:00
|
|
|
_HASHLIB_SCRYPT_METHODDEF
|
2020-04-29 13:04:22 -03:00
|
|
|
_HASHLIB_GET_FIPS_MODE_METHODDEF
|
2020-05-17 08:49:10 -03:00
|
|
|
_HASHLIB_HMAC_SINGLESHOT_METHODDEF
|
|
|
|
_HASHLIB_HMAC_NEW_METHODDEF
|
2019-09-12 09:42:07 -03:00
|
|
|
_HASHLIB_OPENSSL_MD5_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA1_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA224_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA256_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA384_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA512_METHODDEF
|
2020-05-16 17:27:06 -03:00
|
|
|
_HASHLIB_OPENSSL_SHA3_224_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA3_256_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA3_384_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHA3_512_METHODDEF
|
2020-05-17 13:32:38 -03:00
|
|
|
_HASHLIB_OPENSSL_SHAKE_128_METHODDEF
|
|
|
|
_HASHLIB_OPENSSL_SHAKE_256_METHODDEF
|
2010-08-11 14:31:17 -03:00
|
|
|
{NULL, NULL} /* Sentinel */
|
2005-08-21 15:45:59 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize this module. */
|
|
|
|
|
2019-09-25 18:03:30 -03:00
|
|
|
static int
|
|
|
|
hashlib_traverse(PyObject *m, visitproc visit, void *arg)
|
|
|
|
{
|
2020-03-16 10:15:01 -03:00
|
|
|
_hashlibstate *state = get_hashlib_state(m);
|
2019-09-25 18:03:30 -03:00
|
|
|
Py_VISIT(state->EVPtype);
|
2020-05-17 08:49:10 -03:00
|
|
|
Py_VISIT(state->HMACtype);
|
2020-05-16 17:27:06 -03:00
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
|
|
|
Py_VISIT(state->EVPXOFtype);
|
|
|
|
#endif
|
2019-09-25 18:03:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hashlib_clear(PyObject *m)
|
|
|
|
{
|
2020-03-16 10:15:01 -03:00
|
|
|
_hashlibstate *state = get_hashlib_state(m);
|
2019-09-25 18:03:30 -03:00
|
|
|
Py_CLEAR(state->EVPtype);
|
2020-05-17 08:49:10 -03:00
|
|
|
Py_CLEAR(state->HMACtype);
|
2020-05-16 17:27:06 -03:00
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
|
|
|
Py_CLEAR(state->EVPXOFtype);
|
|
|
|
#endif
|
2019-09-25 18:03:30 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
hashlib_free(void *m)
|
|
|
|
{
|
|
|
|
hashlib_clear((PyObject *)m);
|
|
|
|
}
|
|
|
|
|
2020-05-25 05:44:51 -03:00
|
|
|
/* Py_mod_exec functions */
|
|
|
|
static int
|
|
|
|
hashlib_openssl_legacy_init(PyObject *module)
|
|
|
|
{
|
|
|
|
#if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
|
|
|
|
/* Load all digest algorithms and initialize cpuid */
|
|
|
|
OPENSSL_add_all_algorithms_noconf();
|
|
|
|
ERR_load_crypto_strings();
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2020-05-25 05:44:51 -03:00
|
|
|
static int
|
|
|
|
hashlib_init_evptype(PyObject *module)
|
|
|
|
{
|
|
|
|
_hashlibstate *state = get_hashlib_state(module);
|
2008-06-11 02:26:20 -03:00
|
|
|
|
2020-05-25 05:44:51 -03:00
|
|
|
state->EVPtype = (PyTypeObject *)PyType_FromSpec(&EVPtype_spec);
|
|
|
|
if (state->EVPtype == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyModule_AddType(module, state->EVPtype) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
hashlib_init_evpxoftype(PyObject *module)
|
2005-08-21 15:45:59 -03:00
|
|
|
{
|
2020-05-16 17:27:06 -03:00
|
|
|
#ifdef PY_OPENSSL_HAS_SHAKE
|
2020-05-25 05:44:51 -03:00
|
|
|
_hashlibstate *state = get_hashlib_state(module);
|
2020-05-16 17:27:06 -03:00
|
|
|
PyObject *bases;
|
2020-05-25 05:44:51 -03:00
|
|
|
|
|
|
|
if (state->EVPtype == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
bases = PyTuple_Pack(1, state->EVPtype);
|
|
|
|
if (bases == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
state->EVPXOFtype = (PyTypeObject *)PyType_FromSpecWithBases(
|
|
|
|
&EVPXOFtype_spec, bases
|
|
|
|
);
|
|
|
|
Py_DECREF(bases);
|
|
|
|
if (state->EVPXOFtype == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyModule_AddType(module, state->EVPXOFtype) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2020-05-16 17:27:06 -03:00
|
|
|
#endif
|
2020-05-25 05:44:51 -03:00
|
|
|
return 0;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-05-25 05:44:51 -03:00
|
|
|
static int
|
|
|
|
hashlib_init_hmactype(PyObject *module)
|
|
|
|
{
|
|
|
|
_hashlibstate *state = get_hashlib_state(module);
|
|
|
|
|
|
|
|
state->HMACtype = (PyTypeObject *)PyType_FromSpec(&HMACtype_spec);
|
|
|
|
if (state->HMACtype == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PyModule_AddType(module, state->HMACtype) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static PyModuleDef_Slot hashlib_slots[] = {
|
|
|
|
/* OpenSSL 1.0.2 and LibreSSL */
|
|
|
|
{Py_mod_exec, hashlib_openssl_legacy_init},
|
|
|
|
{Py_mod_exec, hashlib_init_evptype},
|
|
|
|
{Py_mod_exec, hashlib_init_evpxoftype},
|
|
|
|
{Py_mod_exec, hashlib_init_hmactype},
|
|
|
|
{Py_mod_exec, hashlib_md_meth_names},
|
|
|
|
{0, NULL}
|
|
|
|
};
|
2017-09-05 10:47:11 -03:00
|
|
|
#endif
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2020-05-25 05:44:51 -03:00
|
|
|
static struct PyModuleDef _hashlibmodule = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
|
|
|
.m_name = "_hashlib",
|
|
|
|
.m_doc = "OpenSSL interface for hashlib module",
|
|
|
|
.m_size = sizeof(_hashlibstate),
|
|
|
|
.m_methods = EVP_functions,
|
|
|
|
.m_slots = NULL,
|
|
|
|
.m_traverse = hashlib_traverse,
|
|
|
|
.m_clear = hashlib_clear,
|
|
|
|
.m_free = hashlib_free
|
|
|
|
};
|
|
|
|
|
|
|
|
PyMODINIT_FUNC
|
|
|
|
PyInit__hashlib(void)
|
|
|
|
{
|
|
|
|
PyObject *m = PyState_FindModule(&_hashlibmodule);
|
2019-09-25 18:03:30 -03:00
|
|
|
if (m != NULL) {
|
|
|
|
Py_INCREF(m);
|
|
|
|
return m;
|
|
|
|
}
|
2005-08-21 15:45:59 -03:00
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
m = PyModule_Create(&_hashlibmodule);
|
2020-05-25 05:44:51 -03:00
|
|
|
if (m == NULL) {
|
2019-09-25 18:03:30 -03:00
|
|
|
return NULL;
|
2020-05-16 17:27:06 -03:00
|
|
|
}
|
|
|
|
|
2020-05-25 05:44:51 -03:00
|
|
|
if (hashlib_openssl_legacy_init(m) < 0) {
|
2020-05-17 08:49:10 -03:00
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-25 05:44:51 -03:00
|
|
|
if (hashlib_init_evptype(m) < 0) {
|
2020-05-16 17:27:06 -03:00
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-25 05:44:51 -03:00
|
|
|
if (hashlib_init_evpxoftype(m) < 0) {
|
2020-05-16 17:27:06 -03:00
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-25 05:44:51 -03:00
|
|
|
if (hashlib_init_hmactype(m) < 0) {
|
2010-09-06 05:30:23 -03:00
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-05-25 05:44:51 -03:00
|
|
|
if (hashlib_md_meth_names(m) == -1) {
|
2010-09-06 05:30:23 -03:00
|
|
|
Py_DECREF(m);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-06-11 02:26:20 -03:00
|
|
|
return m;
|
2005-08-21 15:45:59 -03:00
|
|
|
}
|