| 1 |
/* |
| 2 |
* Written in 2013 by Dmitry Chestnykh <[email protected]> |
| 3 |
* Modified for CPython by Christian Heimes <[email protected]> |
| 4 |
* |
| 5 |
* To the extent possible under law, the author have dedicated all |
| 6 |
* copyright and related and neighboring rights to this software to |
| 7 |
* the public domain worldwide. This software is distributed without |
| 8 |
* any warranty. http://creativecommons.org/publicdomain/zero/1.0/ |
| 9 |
*/ |
| 10 |
|
| 11 |
/* WARNING: autogenerated file! |
| 12 |
* |
| 13 |
* The blake2s_impl.c is autogenerated from blake2b_impl.c. |
| 14 |
*/ |
| 15 |
|
| 16 |
#ifndef Py_BUILD_CORE_BUILTIN |
| 17 |
# define Py_BUILD_CORE_MODULE 1 |
| 18 |
#endif |
| 19 |
|
| 20 |
#include "Python.h" |
| 21 |
#include "pycore_strhex.h" // _Py_strhex() |
| 22 |
|
| 23 |
#include <stdbool.h> |
| 24 |
|
| 25 |
#include "../hashlib.h" |
| 26 |
#include "blake2module.h" |
| 27 |
|
| 28 |
#ifndef HAVE_LIBB2 |
| 29 |
/* pure SSE2 implementation is very slow, so only use the more optimized SSSE3+ |
| 30 |
* https://bugs.python.org/issue31834 */ |
| 31 |
#if defined(__SSSE3__) || defined(__SSE4_1__) || defined(__AVX__) || defined(__XOP__) |
| 32 |
#include "impl/blake2b.c" |
| 33 |
#else |
| 34 |
#include "impl/blake2b-ref.c" |
| 35 |
#endif |
| 36 |
#endif // !HAVE_LIBB2 |
| 37 |
|
| 38 |
#define HAVE_BLAKE2B 1 |
| 39 |
|
| 40 |
extern PyType_Spec blake2b_type_spec; |
| 41 |
|
| 42 |
|
| 43 |
typedef struct { |
| 44 |
PyObject_HEAD |
| 45 |
blake2b_param param; |
| 46 |
blake2b_state state; |
| 47 |
bool use_mutex; |
| 48 |
PyMutex mutex; |
| 49 |
} BLAKE2bObject; |
| 50 |
|
| 51 |
#include "clinic/blake2b_impl.c.h" |
| 52 |
|
| 53 |
/*[clinic input] |
| 54 |
module _blake2 |
| 55 |
class _blake2.blake2b "BLAKE2bObject *" "&PyBlake2_BLAKE2bType" |
| 56 |
[clinic start generated code]*/ |
| 57 |
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d47b0527b39c673f]*/ |
| 58 |
|
| 59 |
|
| 60 |
static BLAKE2bObject * |
| 61 |
new_BLAKE2bObject(PyTypeObject *type) |
| 62 |
{ |
| 63 |
BLAKE2bObject *self; |
| 64 |
self = (BLAKE2bObject *)type->tp_alloc(type, 0); |
| 65 |
if (self == NULL) { |
| 66 |
return NULL; |
| 67 |
} |
| 68 |
HASHLIB_INIT_MUTEX(self); |
| 69 |
|
| 70 |
return self; |
| 71 |
} |
| 72 |
|
| 73 |
/*[clinic input] |
| 74 |
@classmethod |
| 75 |
_blake2.blake2b.__new__ as py_blake2b_new |
| 76 |
data as data_obj: object(c_default="NULL") = b'' |
| 77 |
* |
| 78 |
digest_size: int(c_default="BLAKE2B_OUTBYTES") = _blake2.blake2b.MAX_DIGEST_SIZE |
| 79 |
key: Py_buffer(c_default="NULL", py_default="b''") = None |
| 80 |
salt: Py_buffer(c_default="NULL", py_default="b''") = None |
| 81 |
person: Py_buffer(c_default="NULL", py_default="b''") = None |
| 82 |
fanout: int = 1 |
| 83 |
depth: int = 1 |
| 84 |
leaf_size: unsigned_long = 0 |
| 85 |
node_offset: unsigned_long_long = 0 |
| 86 |
node_depth: int = 0 |
| 87 |
inner_size: int = 0 |
| 88 |
last_node: bool = False |
| 89 |
usedforsecurity: bool = True |
| 90 |
string: object(c_default="NULL") = None |
| 91 |
|
| 92 |
Return a new BLAKE2b hash object. |
| 93 |
[clinic start generated code]*/ |
| 94 |
|
| 95 |
static PyObject * |
| 96 |
py_blake2b_new_impl(PyTypeObject *type, PyObject *data_obj, int digest_size, |
| 97 |
Py_buffer *key, Py_buffer *salt, Py_buffer *person, |
| 98 |
int fanout, int depth, unsigned long leaf_size, |
| 99 |
unsigned long long node_offset, int node_depth, |
| 100 |
int inner_size, int last_node, int usedforsecurity, |
| 101 |
PyObject *string) |
| 102 |
/*[clinic end generated code: output=de64bd850606b6a0 input=a876354eae7e3c39]*/ |
| 103 |
{ |
| 104 |
BLAKE2bObject *self = NULL; |
| 105 |
PyObject *data; |
| 106 |
Py_buffer buf; |
| 107 |
if (_Py_hashlib_data_argument(&data, data_obj, string) < 0) { |
| 108 |
return NULL; |
| 109 |
} |
| 110 |
|
| 111 |
self = new_BLAKE2bObject(type); |
| 112 |
if (self == NULL) { |
| 113 |
goto error; |
| 114 |
} |
| 115 |
|
| 116 |
/* Zero parameter block. */ |
| 117 |
memset(&self->param, 0, sizeof(self->param)); |
| 118 |
|
| 119 |
/* Set digest size. */ |
| 120 |
if (digest_size <= 0 || digest_size > BLAKE2B_OUTBYTES) { |
| 121 |
PyErr_Format(PyExc_ValueError, |
| 122 |
"digest_size must be between 1 and %d bytes", |
| 123 |
BLAKE2B_OUTBYTES); |
| 124 |
goto error; |
| 125 |
} |
| 126 |
self->param.digest_length = digest_size; |
| 127 |
|
| 128 |
/* Set salt parameter. */ |
| 129 |
if ((salt->obj != NULL) && salt->len) { |
| 130 |
if (salt->len > BLAKE2B_SALTBYTES) { |
| 131 |
PyErr_Format(PyExc_ValueError, |
| 132 |
"maximum salt length is %d bytes", |
| 133 |
BLAKE2B_SALTBYTES); |
| 134 |
goto error; |
| 135 |
} |
| 136 |
memcpy(self->param.salt, salt->buf, salt->len); |
| 137 |
} |
| 138 |
|
| 139 |
/* Set personalization parameter. */ |
| 140 |
if ((person->obj != NULL) && person->len) { |
| 141 |
if (person->len > BLAKE2B_PERSONALBYTES) { |
| 142 |
PyErr_Format(PyExc_ValueError, |
| 143 |
"maximum person length is %d bytes", |
| 144 |
BLAKE2B_PERSONALBYTES); |
| 145 |
goto error; |
| 146 |
} |
| 147 |
memcpy(self->param.personal, person->buf, person->len); |
| 148 |
} |
| 149 |
|
| 150 |
/* Set tree parameters. */ |
| 151 |
if (fanout < 0 || fanout > 255) { |
| 152 |
PyErr_SetString(PyExc_ValueError, |
| 153 |
"fanout must be between 0 and 255"); |
| 154 |
goto error; |
| 155 |
} |
| 156 |
self->param.fanout = (uint8_t)fanout; |
| 157 |
|
| 158 |
if (depth <= 0 || depth > 255) { |
| 159 |
PyErr_SetString(PyExc_ValueError, |
| 160 |
"depth must be between 1 and 255"); |
| 161 |
goto error; |
| 162 |
} |
| 163 |
self->param.depth = (uint8_t)depth; |
| 164 |
|
| 165 |
if (leaf_size > 0xFFFFFFFFU) { |
| 166 |
PyErr_SetString(PyExc_OverflowError, "leaf_size is too large"); |
| 167 |
goto error; |
| 168 |
} |
| 169 |
// NB: Simple assignment here would be incorrect on big endian platforms. |
| 170 |
store32(&(self->param.leaf_length), leaf_size); |
| 171 |
|
| 172 |
#ifdef HAVE_BLAKE2S |
| 173 |
if (node_offset > 0xFFFFFFFFFFFFULL) { |
| 174 |
/* maximum 2**48 - 1 */ |
| 175 |
PyErr_SetString(PyExc_OverflowError, "node_offset is too large"); |
| 176 |
goto error; |
| 177 |
} |
| 178 |
store48(&(self->param.node_offset), node_offset); |
| 179 |
#else |
| 180 |
// NB: Simple assignment here would be incorrect on big endian platforms. |
| 181 |
store64(&(self->param.node_offset), node_offset); |
| 182 |
#endif |
| 183 |
|
| 184 |
if (node_depth < 0 || node_depth > 255) { |
| 185 |
PyErr_SetString(PyExc_ValueError, |
| 186 |
"node_depth must be between 0 and 255"); |
| 187 |
goto error; |
| 188 |
} |
| 189 |
self->param.node_depth = node_depth; |
| 190 |
|
| 191 |
if (inner_size < 0 || inner_size > BLAKE2B_OUTBYTES) { |
| 192 |
PyErr_Format(PyExc_ValueError, |
| 193 |
"inner_size must be between 0 and is %d", |
| 194 |
BLAKE2B_OUTBYTES); |
| 195 |
goto error; |
| 196 |
} |
| 197 |
self->param.inner_length = inner_size; |
| 198 |
|
| 199 |
/* Set key length. */ |
| 200 |
if ((key->obj != NULL) && key->len) { |
| 201 |
if (key->len > BLAKE2B_KEYBYTES) { |
| 202 |
PyErr_Format(PyExc_ValueError, |
| 203 |
"maximum key length is %d bytes", |
| 204 |
BLAKE2B_KEYBYTES); |
| 205 |
goto error; |
| 206 |
} |
| 207 |
self->param.key_length = (uint8_t)key->len; |
| 208 |
} |
| 209 |
|
| 210 |
/* Initialize hash state. */ |
| 211 |
if (blake2b_init_param(&self->state, &self->param) < 0) { |
| 212 |
PyErr_SetString(PyExc_RuntimeError, |
| 213 |
"error initializing hash state"); |
| 214 |
goto error; |
| 215 |
} |
| 216 |
|
| 217 |
/* Set last node flag (must come after initialization). */ |
| 218 |
self->state.last_node = last_node; |
| 219 |
|
| 220 |
/* Process key block if any. */ |
| 221 |
if (self->param.key_length) { |
| 222 |
uint8_t block[BLAKE2B_BLOCKBYTES]; |
| 223 |
memset(block, 0, sizeof(block)); |
| 224 |
memcpy(block, key->buf, key->len); |
| 225 |
blake2b_update(&self->state, block, sizeof(block)); |
| 226 |
secure_zero_memory(block, sizeof(block)); |
| 227 |
} |
| 228 |
|
| 229 |
/* Process initial data if any. */ |
| 230 |
if (data != NULL) { |
| 231 |
GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error); |
| 232 |
|
| 233 |
if (buf.len >= HASHLIB_GIL_MINSIZE) { |
| 234 |
Py_BEGIN_ALLOW_THREADS |
| 235 |
blake2b_update(&self->state, buf.buf, buf.len); |
| 236 |
Py_END_ALLOW_THREADS |
| 237 |
} else { |
| 238 |
blake2b_update(&self->state, buf.buf, buf.len); |
| 239 |
} |
| 240 |
PyBuffer_Release(&buf); |
| 241 |
} |
| 242 |
|
| 243 |
return (PyObject *)self; |
| 244 |
|
| 245 |
error: |
| 246 |
if (self != NULL) { |
| 247 |
Py_DECREF(self); |
| 248 |
} |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
|
| 252 |
/*[clinic input] |
| 253 |
_blake2.blake2b.copy |
| 254 |
|
| 255 |
Return a copy of the hash object. |
| 256 |
[clinic start generated code]*/ |
| 257 |
|
| 258 |
static PyObject * |
| 259 |
_blake2_blake2b_copy_impl(BLAKE2bObject *self) |
| 260 |
/*[clinic end generated code: output=ff6acee5f93656ae input=e383c2d199fd8a2e]*/ |
| 261 |
{ |
| 262 |
BLAKE2bObject *cpy; |
| 263 |
|
| 264 |
if ((cpy = new_BLAKE2bObject(Py_TYPE(self))) == NULL) |
| 265 |
return NULL; |
| 266 |
|
| 267 |
ENTER_HASHLIB(self); |
| 268 |
cpy->param = self->param; |
| 269 |
cpy->state = self->state; |
| 270 |
LEAVE_HASHLIB(self); |
| 271 |
return (PyObject *)cpy; |
| 272 |
} |
| 273 |
|
| 274 |
/*[clinic input] |
| 275 |
_blake2.blake2b.update |
| 276 |
|
| 277 |
data: object |
| 278 |
/ |
| 279 |
|
| 280 |
Update this hash object's state with the provided bytes-like object. |
| 281 |
[clinic start generated code]*/ |
| 282 |
|
| 283 |
static PyObject * |
| 284 |
_blake2_blake2b_update(BLAKE2bObject *self, PyObject *data) |
| 285 |
/*[clinic end generated code: output=010dfcbe22654359 input=ffc4aa6a6a225d31]*/ |
| 286 |
{ |
| 287 |
Py_buffer buf; |
| 288 |
|
| 289 |
GET_BUFFER_VIEW_OR_ERROUT(data, &buf); |
| 290 |
|
| 291 |
if (!self->use_mutex && buf.len >= HASHLIB_GIL_MINSIZE) { |
| 292 |
self->use_mutex = true; |
| 293 |
} |
| 294 |
if (self->use_mutex) { |
| 295 |
Py_BEGIN_ALLOW_THREADS |
| 296 |
PyMutex_Lock(&self->mutex); |
| 297 |
blake2b_update(&self->state, buf.buf, buf.len); |
| 298 |
PyMutex_Unlock(&self->mutex); |
| 299 |
Py_END_ALLOW_THREADS |
| 300 |
} else { |
| 301 |
blake2b_update(&self->state, buf.buf, buf.len); |
| 302 |
} |
| 303 |
|
| 304 |
PyBuffer_Release(&buf); |
| 305 |
|
| 306 |
Py_RETURN_NONE; |
| 307 |
} |
| 308 |
|
| 309 |
/*[clinic input] |
| 310 |
_blake2.blake2b.digest |
| 311 |
|
| 312 |
Return the digest value as a bytes object. |
| 313 |
[clinic start generated code]*/ |
| 314 |
|
| 315 |
static PyObject * |
| 316 |
_blake2_blake2b_digest_impl(BLAKE2bObject *self) |
| 317 |
/*[clinic end generated code: output=a5864660f4bfc61a input=7d21659e9c5fff02]*/ |
| 318 |
{ |
| 319 |
uint8_t digest[BLAKE2B_OUTBYTES]; |
| 320 |
blake2b_state state_cpy; |
| 321 |
|
| 322 |
ENTER_HASHLIB(self); |
| 323 |
state_cpy = self->state; |
| 324 |
blake2b_final(&state_cpy, digest, self->param.digest_length); |
| 325 |
LEAVE_HASHLIB(self); |
| 326 |
return PyBytes_FromStringAndSize((const char *)digest, |
| 327 |
self->param.digest_length); |
| 328 |
} |
| 329 |
|
| 330 |
/*[clinic input] |
| 331 |
_blake2.blake2b.hexdigest |
| 332 |
|
| 333 |
Return the digest value as a string of hexadecimal digits. |
| 334 |
[clinic start generated code]*/ |
| 335 |
|
| 336 |
static PyObject * |
| 337 |
_blake2_blake2b_hexdigest_impl(BLAKE2bObject *self) |
| 338 |
/*[clinic end generated code: output=b5598a87d8794a60 input=76930f6946351f56]*/ |
| 339 |
{ |
| 340 |
uint8_t digest[BLAKE2B_OUTBYTES]; |
| 341 |
blake2b_state state_cpy; |
| 342 |
|
| 343 |
ENTER_HASHLIB(self); |
| 344 |
state_cpy = self->state; |
| 345 |
blake2b_final(&state_cpy, digest, self->param.digest_length); |
| 346 |
LEAVE_HASHLIB(self); |
| 347 |
return _Py_strhex((const char *)digest, self->param.digest_length); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
static PyMethodDef py_blake2b_methods[] = { |
| 352 |
_BLAKE2_BLAKE2B_COPY_METHODDEF |
| 353 |
_BLAKE2_BLAKE2B_DIGEST_METHODDEF |
| 354 |
_BLAKE2_BLAKE2B_HEXDIGEST_METHODDEF |
| 355 |
_BLAKE2_BLAKE2B_UPDATE_METHODDEF |
| 356 |
{NULL, NULL} |
| 357 |
}; |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
static PyObject * |
| 362 |
py_blake2b_get_name(BLAKE2bObject *self, void *closure) |
| 363 |
{ |
| 364 |
return PyUnicode_FromString("blake2b"); |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static PyObject * |
| 370 |
py_blake2b_get_block_size(BLAKE2bObject *self, void *closure) |
| 371 |
{ |
| 372 |
return PyLong_FromLong(BLAKE2B_BLOCKBYTES); |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
static PyObject * |
| 378 |
py_blake2b_get_digest_size(BLAKE2bObject *self, void *closure) |
| 379 |
{ |
| 380 |
return PyLong_FromLong(self->param.digest_length); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
static PyGetSetDef py_blake2b_getsetters[] = { |
| 385 |
{"name", (getter)py_blake2b_get_name, |
| 386 |
NULL, NULL, NULL}, |
| 387 |
{"block_size", (getter)py_blake2b_get_block_size, |
| 388 |
NULL, NULL, NULL}, |
| 389 |
{"digest_size", (getter)py_blake2b_get_digest_size, |
| 390 |
NULL, NULL, NULL}, |
| 391 |
{NULL} |
| 392 |
}; |
| 393 |
|
| 394 |
|
| 395 |
static void |
| 396 |
py_blake2b_dealloc(PyObject *self) |
| 397 |
{ |
| 398 |
BLAKE2bObject *obj = (BLAKE2bObject *)self; |
| 399 |
|
| 400 |
/* Try not to leave state in memory. */ |
| 401 |
secure_zero_memory(&obj->param, sizeof(obj->param)); |
| 402 |
secure_zero_memory(&obj->state, sizeof(obj->state)); |
| 403 |
|
| 404 |
PyTypeObject *type = Py_TYPE(self); |
| 405 |
PyObject_Free(self); |
| 406 |
Py_DECREF(type); |
| 407 |
} |
| 408 |
|
| 409 |
static PyType_Slot blake2b_type_slots[] = { |
| 410 |
{Py_tp_dealloc, py_blake2b_dealloc}, |
| 411 |
{Py_tp_doc, (char *)py_blake2b_new__doc__}, |
| 412 |
{Py_tp_methods, py_blake2b_methods}, |
| 413 |
{Py_tp_getset, py_blake2b_getsetters}, |
| 414 |
{Py_tp_new, py_blake2b_new}, |
| 415 |
{0,0} |
| 416 |
}; |
| 417 |
|
| 418 |
PyType_Spec blake2b_type_spec = { |
| 419 |
.name = "_blake2.blake2b", |
| 420 |
.basicsize = sizeof(BLAKE2bObject), |
| 421 |
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, |
| 422 |
.slots = blake2b_type_slots |
| 423 |
}; |