logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0001-Add-BearSSL-implementation-of-_hashlib.patch (65569B)


  1. From 61ad7f5ec78d4c80a99ac71820d9bc8b62c3fbae Mon Sep 17 00:00:00 2001
  2. From: Michael Forney <mforney@mforney.org>
  3. Date: Sat, 15 May 2021 22:48:13 -0700
  4. Subject: [PATCH] Add BearSSL implementation of _hashlib
  5. ---
  6. Modules/_hashbearssl.c | 1126 +++++++++++++++++++++++++++++++
  7. Modules/clinic/_hashbearssl.c.h | 1113 ++++++++++++++++++++++++++++++
  8. 2 files changed, 2239 insertions(+)
  9. create mode 100644 Modules/_hashbearssl.c
  10. create mode 100644 Modules/clinic/_hashbearssl.c.h
  11. diff --git a/Modules/_hashbearssl.c b/Modules/_hashbearssl.c
  12. new file mode 100644
  13. index 0000000000..2d1a76296d
  14. --- /dev/null
  15. +++ b/Modules/_hashbearssl.c
  16. @@ -0,0 +1,1126 @@
  17. +/* Module that wraps all BearSSL hash algorithms */
  18. +#define PY_SSIZE_T_CLEAN
  19. +#include "Python.h"
  20. +#include "hashlib.h"
  21. +#include "pystrhex.h"
  22. +
  23. +#include <bearssl.h>
  24. +
  25. +static PyModuleDef _hashlibmodule;
  26. +static PyTypeObject Hash_Type;
  27. +static PyTypeObject SHAKE_Type;
  28. +static PyTypeObject HMAC_Type;
  29. +
  30. +typedef struct {
  31. + PyObject_HEAD
  32. + br_hash_compat_context ctx;
  33. + PyThread_type_lock lock;
  34. +} Hash;
  35. +
  36. +typedef struct {
  37. + PyObject_HEAD
  38. + br_hmac_context ctx;
  39. + br_hmac_key_context key;
  40. + PyThread_type_lock lock;
  41. +} HMAC;
  42. +
  43. +typedef struct {
  44. + PyObject_HEAD
  45. + br_shake_context ctx;
  46. + int bits;
  47. + PyThread_type_lock lock;
  48. +} SHAKE;
  49. +
  50. +#include "clinic/_hashbearssl.c.h"
  51. +/*[clinic input]
  52. +module _hashlib
  53. +class _hashlib.Hash "Hash *" "Hash_Type"
  54. +class _hashlib.HMAC "HMAC *" "HMAC_Type"
  55. +class _hashlib.SHAKE "SHAKE *" "SHAKE_Type"
  56. +[clinic start generated code]*/
  57. +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=7a269412ec77c79a]*/
  58. +
  59. +static const br_hash_class *
  60. +py_hash_by_name(const char *name)
  61. +{
  62. + if (strcmp(name, "md5") == 0)
  63. + return &br_md5_vtable;
  64. + else if (strcmp(name, "sha1") == 0)
  65. + return &br_sha1_vtable;
  66. + else if (strcmp(name, "sha224") == 0)
  67. + return &br_sha224_vtable;
  68. + else if (strcmp(name, "sha256") == 0)
  69. + return &br_sha256_vtable;
  70. + else if (strcmp(name, "sha384") == 0)
  71. + return &br_sha384_vtable;
  72. + else if (strcmp(name, "sha512") == 0)
  73. + return &br_sha512_vtable;
  74. + return NULL;
  75. +}
  76. +
  77. +PyDoc_STRVAR(Hash_doc,
  78. +"Hash(name, string=b\'\')\n"
  79. +"--\n"
  80. +"\n"
  81. +"A hash is an object used to calculate a checksum of a string of information.\n"
  82. +"\n"
  83. +"Methods:\n"
  84. +"\n"
  85. +"update() -- updates the current digest with an additional string\n"
  86. +"digest() -- return the current digest value\n"
  87. +"hexdigest() -- return the current digest as a string of hexadecimal digits\n"
  88. +"copy() -- return a copy of the current hash object\n"
  89. +"\n"
  90. +"Attributes:\n"
  91. +"\n"
  92. +"name -- the hash algorithm being used by this object\n"
  93. +"digest_size -- number of bytes in this hashes output");
  94. +
  95. +static void
  96. +Hash_dealloc(Hash *self)
  97. +{
  98. + if (self->lock)
  99. + PyThread_free_lock(self->lock);
  100. + PyObject_Free(self);
  101. +}
  102. +
  103. +/*[clinic input]
  104. +_hashlib.Hash.update as Hash_update
  105. +
  106. + data: Py_buffer
  107. + /
  108. +
  109. +Update this hash object's state with the provided string.
  110. +[clinic start generated code]*/
  111. +
  112. +static PyObject *
  113. +Hash_update_impl(Hash *self, Py_buffer *data)
  114. +/*[clinic end generated code: output=eed11f7503e289a3 input=0547d16ba3981abf]*/
  115. +{
  116. + if (!self->lock && data->len >= HASHLIB_GIL_MINSIZE)
  117. + self->lock = PyThread_allocate_lock();
  118. + if (self->lock) {
  119. + Py_BEGIN_ALLOW_THREADS
  120. + PyThread_acquire_lock(self->lock, 1);
  121. + self->ctx.vtable->update(&self->ctx.vtable, data->buf, data->len);
  122. + PyThread_release_lock(self->lock);
  123. + Py_END_ALLOW_THREADS
  124. + } else {
  125. + self->ctx.vtable->update(&self->ctx.vtable, data->buf, data->len);
  126. + }
  127. + Py_RETURN_NONE;
  128. +}
  129. +
  130. +/*[clinic input]
  131. +_hashlib.Hash.digest as Hash_digest
  132. +
  133. +Return the digest value as a bytes object.
  134. +[clinic start generated code]*/
  135. +
  136. +static PyObject *
  137. +Hash_digest_impl(Hash *self)
  138. +/*[clinic end generated code: output=b3eafdc9f37cf341 input=6d4afcd832edc55a]*/
  139. +{
  140. + char digest[64];
  141. + Py_ssize_t digest_size;
  142. +
  143. + ENTER_HASHLIB(self)
  144. + self->ctx.vtable->out(&self->ctx.vtable, digest);
  145. + LEAVE_HASHLIB(self)
  146. + digest_size = (self->ctx.vtable->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
  147. +
  148. + return PyBytes_FromStringAndSize(digest, digest_size);
  149. +}
  150. +
  151. +/*[clinic input]
  152. +_hashlib.Hash.hexdigest as Hash_hexdigest
  153. +
  154. +Return the digest value as a string of hexadecimal digits.
  155. +[clinic start generated code]*/
  156. +
  157. +static PyObject *
  158. +Hash_hexdigest_impl(Hash *self)
  159. +/*[clinic end generated code: output=eff810494302910a input=6b224236fad6ff65]*/
  160. +{
  161. + char digest[64];
  162. + Py_ssize_t digest_size;
  163. +
  164. + ENTER_HASHLIB(self)
  165. + self->ctx.vtable->out(&self->ctx.vtable, digest);
  166. + LEAVE_HASHLIB(self)
  167. + digest_size = (self->ctx.vtable->desc >> BR_HASHDESC_OUT_OFF) & BR_HASHDESC_OUT_MASK;
  168. +
  169. + return _Py_strhex(digest, digest_size);
  170. +}
  171. +
  172. +/*[clinic input]
  173. +_hashlib.Hash.copy as Hash_copy
  174. +
  175. +Return a copy of the hash object.
  176. +[clinic start generated code]*/
  177. +
  178. +static PyObject *
  179. +Hash_copy_impl(Hash *self)
  180. +/*[clinic end generated code: output=e97863340f55061b input=b830c9d949d08256]*/
  181. +{
  182. + Hash *newobj;
  183. +
  184. + newobj = PyObject_New(Hash, &Hash_Type);
  185. + if (!newobj)
  186. + return NULL;
  187. + newobj->lock = NULL;
  188. + ENTER_HASHLIB(self)
  189. + newobj->ctx = self->ctx;
  190. + LEAVE_HASHLIB(self)
  191. +
  192. + return (PyObject *)newobj;
  193. +}
  194. +
  195. +static PyMethodDef Hash_methods[] = {
  196. + HASH_UPDATE_METHODDEF
  197. + HASH_DIGEST_METHODDEF
  198. + HASH_HEXDIGEST_METHODDEF
  199. + HASH_COPY_METHODDEF
  200. + {0}
  201. +};
  202. +
  203. +static PyObject *
  204. +Hash_get_digest_size(Hash *self, void *closure)
  205. +{
  206. + long digest_size;
  207. +
  208. + digest_size = self->ctx.vtable->desc >> BR_HASHDESC_OUT_OFF & BR_HASHDESC_OUT_MASK;
  209. + return PyLong_FromLong(digest_size);
  210. +}
  211. +
  212. +static PyObject *
  213. +Hash_get_block_size(Hash *self, void *closure)
  214. +{
  215. + long block_size;
  216. +
  217. + block_size = 1 << (self->ctx.vtable->desc >> BR_HASHDESC_LBLEN_OFF & BR_HASHDESC_LBLEN_MASK);
  218. + return PyLong_FromLong(block_size);
  219. +}
  220. +
  221. +static PyObject *
  222. +Hash_get_name(Hash *self, void *closure)
  223. +{
  224. + const char *name = NULL;
  225. +
  226. + switch (self->ctx.vtable->desc >> BR_HASHDESC_ID_OFF & BR_HASHDESC_ID_MASK) {
  227. + case br_md5_ID: name = "md5"; break;
  228. + case br_sha1_ID: name = "sha1"; break;
  229. + case br_sha224_ID: name = "sha224"; break;
  230. + case br_sha256_ID: name = "sha256"; break;
  231. + case br_sha384_ID: name = "sha384"; break;
  232. + case br_sha512_ID: name = "sha512"; break;
  233. + }
  234. +
  235. + return PyUnicode_FromString(name);
  236. +}
  237. +
  238. +static PyGetSetDef Hash_getset[] = {
  239. + {"digest_size", (getter)Hash_get_digest_size, NULL, NULL, NULL},
  240. + {"block_size", (getter)Hash_get_block_size, NULL, NULL, NULL},
  241. + {"name", (getter)Hash_get_name, NULL, NULL, NULL},
  242. + {0}
  243. +};
  244. +
  245. +static PyTypeObject Hash_Type = {
  246. + PyVarObject_HEAD_INIT(NULL, 0)
  247. + .tp_name = "_hashlib.Hash",
  248. + .tp_dealloc = (destructor)Hash_dealloc,
  249. + .tp_doc = Hash_doc,
  250. + .tp_basicsize = sizeof(Hash),
  251. + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
  252. + .tp_methods = Hash_methods,
  253. + .tp_getset = Hash_getset,
  254. +};
  255. +
  256. +PyDoc_STRVAR(SHAKE_doc,
  257. +"SHAKE(name, string=b\'\')\n"
  258. +"--\n"
  259. +"\n"
  260. +"A hash is an object used to calculate a checksum of a string of information.\n"
  261. +"\n"
  262. +"Methods:\n"
  263. +"\n"
  264. +"update() -- updates the current digest with an additional string\n"
  265. +"digest(length) -- return the current digest value\n"
  266. +"hexdigest(length) -- return the current digest as a string of hexadecimal digits\n"
  267. +"copy() -- return a copy of the current hash object\n"
  268. +"\n"
  269. +"Attributes:\n"
  270. +"\n"
  271. +"name -- the hash algorithm being used by this object\n"
  272. +"digest_size -- number of bytes in this hashes output");
  273. +
  274. +static void
  275. +SHAKE_dealloc(SHAKE *self)
  276. +{
  277. + if (self->lock)
  278. + PyThread_free_lock(self->lock);
  279. + PyObject_Free(self);
  280. +}
  281. +
  282. +/*[clinic input]
  283. +_hashlib.SHAKE.update as SHAKE_update
  284. +
  285. + data: Py_buffer
  286. + /
  287. +
  288. +Update this hash object's state with the provided string.
  289. +[clinic start generated code]*/
  290. +
  291. +static PyObject *
  292. +SHAKE_update_impl(SHAKE *self, Py_buffer *data)
  293. +/*[clinic end generated code: output=b7f5cd67459e2fb3 input=87a80642380b615f]*/
  294. +{
  295. + if (!self->lock && data->len >= HASHLIB_GIL_MINSIZE)
  296. + self->lock = PyThread_allocate_lock();
  297. + if (self->lock) {
  298. + Py_BEGIN_ALLOW_THREADS
  299. + PyThread_acquire_lock(self->lock, 1);
  300. + br_shake_inject(&self->ctx, data->buf, data->len);
  301. + PyThread_release_lock(self->lock);
  302. + Py_END_ALLOW_THREADS
  303. + } else {
  304. + br_shake_inject(&self->ctx, data->buf, data->len);
  305. + }
  306. + Py_RETURN_NONE;
  307. +}
  308. +
  309. +/*[clinic input]
  310. +_hashlib.SHAKE.digest as SHAKE_digest
  311. +
  312. + length: Py_ssize_t
  313. +
  314. +Return the digest value as a bytes object.
  315. +[clinic start generated code]*/
  316. +
  317. +static PyObject *
  318. +SHAKE_digest_impl(SHAKE *self, Py_ssize_t length)
  319. +/*[clinic end generated code: output=4235ee593cba0b23 input=9e31587954f5e87a]*/
  320. +{
  321. + br_shake_context ctx;
  322. + PyObject *bytes;
  323. +
  324. + bytes = PyBytes_FromStringAndSize(NULL, length);
  325. + if (!bytes)
  326. + return NULL;
  327. +
  328. + ENTER_HASHLIB(self)
  329. + ctx = self->ctx;
  330. + LEAVE_HASHLIB(self)
  331. + br_shake_flip(&ctx);
  332. + br_shake_produce(&ctx, PyBytes_AS_STRING(bytes), length);
  333. +
  334. + return bytes;
  335. +}
  336. +
  337. +/*[clinic input]
  338. +_hashlib.SHAKE.hexdigest as SHAKE_hexdigest
  339. +
  340. + length: Py_ssize_t
  341. +
  342. +Return the digest value as a string of hexadecimal digits.
  343. +[clinic start generated code]*/
  344. +
  345. +static PyObject *
  346. +SHAKE_hexdigest_impl(SHAKE *self, Py_ssize_t length)
  347. +/*[clinic end generated code: output=fd9a7905a89a1509 input=2c43a9638ca1db5e]*/
  348. +{
  349. + br_shake_context ctx;
  350. + char *bytes;
  351. + PyObject *str;
  352. +
  353. + bytes = PyMem_Malloc(length);
  354. + if (!bytes) {
  355. + PyErr_NoMemory();
  356. + return NULL;
  357. + }
  358. +
  359. + ENTER_HASHLIB(self)
  360. + ctx = self->ctx;
  361. + LEAVE_HASHLIB(self)
  362. + br_shake_flip(&ctx);
  363. + br_shake_produce(&ctx, bytes, length);
  364. +
  365. + str = _Py_strhex(bytes, length);
  366. + PyMem_Free(bytes);
  367. + return str;
  368. +}
  369. +
  370. +/*[clinic input]
  371. +_hashlib.SHAKE.copy as SHAKE_copy
  372. +
  373. +Return a copy of the SHAKE object.
  374. +[clinic start generated code]*/
  375. +
  376. +static PyObject *
  377. +SHAKE_copy_impl(SHAKE *self)
  378. +/*[clinic end generated code: output=19cff23538f29dc6 input=34c8368c5c9e910c]*/
  379. +{
  380. + SHAKE *newobj;
  381. +
  382. + newobj = PyObject_New(SHAKE, &SHAKE_Type);
  383. + if (!newobj)
  384. + return NULL;
  385. + newobj->lock = NULL;
  386. + ENTER_HASHLIB(self)
  387. + newobj->ctx = self->ctx;
  388. + newobj->bits = self->bits;
  389. + LEAVE_HASHLIB(self)
  390. +
  391. + return (PyObject *)newobj;
  392. +}
  393. +
  394. +static PyMethodDef SHAKE_methods[] = {
  395. + SHAKE_UPDATE_METHODDEF
  396. + SHAKE_DIGEST_METHODDEF
  397. + SHAKE_HEXDIGEST_METHODDEF
  398. + SHAKE_COPY_METHODDEF
  399. + {0}
  400. +};
  401. +
  402. +static PyObject *
  403. +SHAKE_get_digest_size(SHAKE *self, void *closure)
  404. +{
  405. + return PyLong_FromLong(0);
  406. +}
  407. +
  408. +static PyObject *
  409. +SHAKE_get_block_size(SHAKE *self, void *closure)
  410. +{
  411. + return PyLong_FromLong(self->ctx.rate);
  412. +}
  413. +
  414. +static PyObject *
  415. +SHAKE_get_name(SHAKE *self, void *closure)
  416. +{
  417. + const char *name = NULL;
  418. +
  419. + switch (self->bits) {
  420. + case 128: name = "shake_128"; break;
  421. + case 256: name = "shake_256"; break;
  422. + }
  423. +
  424. + return PyUnicode_FromString(name);
  425. +}
  426. +
  427. +static PyGetSetDef SHAKE_getset[] = {
  428. + {"digest_size", (getter)SHAKE_get_digest_size, NULL, NULL, NULL},
  429. + {"block_size", (getter)SHAKE_get_block_size, NULL, NULL, NULL},
  430. + {"name", (getter)SHAKE_get_name, NULL, NULL, NULL},
  431. + {0}
  432. +};
  433. +
  434. +static PyTypeObject SHAKE_Type = {
  435. + PyVarObject_HEAD_INIT(NULL, 0)
  436. + .tp_name = "_hashlib.SHAKE",
  437. + .tp_dealloc = (destructor)SHAKE_dealloc,
  438. + .tp_doc = SHAKE_doc,
  439. + .tp_basicsize = sizeof(SHAKE),
  440. + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
  441. + .tp_methods = SHAKE_methods,
  442. + .tp_getset = SHAKE_getset,
  443. +};
  444. +
  445. +PyDoc_STRVAR(HMAC_doc,
  446. +"The object used to calculate HMAC of a message.\n\
  447. +\n\
  448. +Methods:\n\
  449. +\n\
  450. +update() -- updates the current digest with an additional string\n\
  451. +digest() -- return the current digest value\n\
  452. +hexdigest() -- return the current digest as a string of hexadecimal digits\n\
  453. +copy() -- return a copy of the current hash object\n\
  454. +\n\
  455. +Attributes:\n\
  456. +\n\
  457. +name -- the name, including the hash algorithm used by this object\n\
  458. +digest_size -- number of bytes in digest() output\n");
  459. +
  460. +static void
  461. +HMAC_dealloc(HMAC *self)
  462. +{
  463. + if (self->lock)
  464. + PyThread_free_lock(self->lock);
  465. + PyObject_Free(self);
  466. +}
  467. +/*[clinic input]
  468. +_hashlib.HMAC.copy as HMAC_copy
  469. +
  470. +Return a copy of the HMAC object.
  471. +[clinic start generated code]*/
  472. +
  473. +static PyObject *
  474. +HMAC_copy_impl(HMAC *self)
  475. +/*[clinic end generated code: output=6a3595133ec708a5 input=c471e903458ee9a7]*/
  476. +{
  477. + HMAC *newobj;
  478. +
  479. + newobj = PyObject_New(HMAC, &HMAC_Type);
  480. + if (!newobj)
  481. + return NULL;
  482. + newobj->lock = NULL;
  483. + ENTER_HASHLIB(self)
  484. + newobj->ctx = self->ctx;
  485. + newobj->key = self->key;
  486. + LEAVE_HASHLIB(self)
  487. +
  488. + return (PyObject *)newobj;
  489. +}
  490. +
  491. +/*[clinic input]
  492. +_hashlib.HMAC.update as HMAC_update
  493. +
  494. + data: Py_buffer
  495. + /
  496. +
  497. +Update the HMAC object with data.
  498. +[clinic start generated code]*/
  499. +
  500. +static PyObject *
  501. +HMAC_update_impl(HMAC *self, Py_buffer *data)
  502. +/*[clinic end generated code: output=d3520ecffa6cc3ab input=827e77e990887267]*/
  503. +{
  504. + if (!self->lock && data->len >= HASHLIB_GIL_MINSIZE)
  505. + self->lock = PyThread_allocate_lock();
  506. + if (self->lock) {
  507. + Py_BEGIN_ALLOW_THREADS
  508. + PyThread_acquire_lock(self->lock, 1);
  509. + br_hmac_update(&self->ctx, data->buf, data->len);
  510. + PyThread_release_lock(self->lock);
  511. + Py_END_ALLOW_THREADS
  512. + } else {
  513. + br_hmac_update(&self->ctx, data->buf, data->len);
  514. + }
  515. + Py_RETURN_NONE;
  516. +}
  517. +
  518. +/*[clinic input]
  519. +_hashlib.HMAC.digest as HMAC_digest
  520. +
  521. +Return the digest of the bytes passed to the update() method so far.
  522. +[clinic start generated code]*/
  523. +
  524. +static PyObject *
  525. +HMAC_digest_impl(HMAC *self)
  526. +/*[clinic end generated code: output=9853eeca2bdd96df input=d39cb2285b557318]*/
  527. +{
  528. + char digest[64];
  529. + Py_ssize_t digest_size;
  530. +
  531. + ENTER_HASHLIB(self)
  532. + digest_size = br_hmac_out(&self->ctx, digest);
  533. + LEAVE_HASHLIB(self)
  534. +
  535. + return PyBytes_FromStringAndSize(digest, digest_size);
  536. +}
  537. +
  538. +/*[clinic input]
  539. +_hashlib.HMAC.hexdigest as HMAC_hexdigest
  540. +
  541. +Return hexadecimal digest of the bytes passed to the update() method so far.
  542. +
  543. +This may be used to exchange the value safely in email or other non-binary
  544. +environments.
  545. +[clinic start generated code]*/
  546. +
  547. +static PyObject *
  548. +HMAC_hexdigest_impl(HMAC *self)
  549. +/*[clinic end generated code: output=9bb0c7abb6940bab input=2471f22b8dba7433]*/
  550. +{
  551. + char digest[64];
  552. + Py_ssize_t digest_size;
  553. +
  554. + ENTER_HASHLIB(self)
  555. + digest_size = br_hmac_out(&self->ctx, digest);
  556. + LEAVE_HASHLIB(self)
  557. +
  558. + return _Py_strhex(digest, digest_size);
  559. +}
  560. +
  561. +static PyMethodDef HMAC_methods[] = {
  562. + HMAC_UPDATE_METHODDEF
  563. + HMAC_DIGEST_METHODDEF
  564. + HMAC_HEXDIGEST_METHODDEF
  565. + HMAC_COPY_METHODDEF
  566. + {0},
  567. +};
  568. +
  569. +static PyObject *
  570. +HMAC_get_digest_size(HMAC *self, void *closure)
  571. +{
  572. + const br_hash_class *hc;
  573. + long digest_size;
  574. +
  575. + hc = br_hmac_get_digest(&self->ctx);
  576. + digest_size = hc->desc >> BR_HASHDESC_OUT_OFF & BR_HASHDESC_OUT_MASK;
  577. + return PyLong_FromLong(digest_size);
  578. +}
  579. +
  580. +static PyObject *
  581. +HMAC_get_block_size(HMAC *self, void *closure)
  582. +{
  583. + const br_hash_class *hc;
  584. + long block_size;
  585. +
  586. + hc = br_hmac_get_digest(&self->ctx);
  587. + block_size = 1 << (hc->desc >> BR_HASHDESC_LBLEN_OFF & BR_HASHDESC_LBLEN_MASK);
  588. + return PyLong_FromLong(block_size);
  589. +}
  590. +
  591. +static PyObject *
  592. +HMAC_get_name(HMAC *self, void *closure)
  593. +{
  594. + const br_hash_class *hc;
  595. + const char *name = NULL;
  596. +
  597. + hc = br_hmac_get_digest(&self->ctx);
  598. + switch (hc->desc >> BR_HASHDESC_ID_OFF & BR_HASHDESC_ID_MASK) {
  599. + case br_md5_ID: name = "hmac-md5"; break;
  600. + case br_sha1_ID: name = "hmac-sha1"; break;
  601. + case br_sha224_ID: name = "hmac-sha224"; break;
  602. + case br_sha256_ID: name = "hmac-sha256"; break;
  603. + case br_sha384_ID: name = "hmac-sha384"; break;
  604. + case br_sha512_ID: name = "hmac-sha512"; break;
  605. + }
  606. +
  607. + return PyUnicode_FromString(name);
  608. +}
  609. +
  610. +static PyGetSetDef HMAC_getset[] = {
  611. + {"digest_size", (getter)HMAC_get_digest_size, NULL, NULL, NULL},
  612. + {"block_size", (getter)HMAC_get_block_size, NULL, NULL, NULL},
  613. + {"name", (getter)HMAC_get_name, NULL, NULL, NULL},
  614. + {0}
  615. +};
  616. +
  617. +static PyTypeObject HMAC_Type = {
  618. + PyVarObject_HEAD_INIT(NULL, 0)
  619. + .tp_name = "_hashlib.HMAC",
  620. + .tp_dealloc = (destructor)HMAC_dealloc,
  621. + .tp_doc = HMAC_doc,
  622. + .tp_basicsize = sizeof(HMAC),
  623. + .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION | Py_TPFLAGS_IMMUTABLETYPE,
  624. + .tp_methods = HMAC_methods,
  625. + .tp_getset = HMAC_getset,
  626. +};
  627. +
  628. +static PyObject *
  629. +Hash_new_vtable(PyObject *module, const br_hash_class *hc,
  630. + Py_buffer *string, int usedforsecurity)
  631. +{
  632. + Hash *self;
  633. +
  634. + self = PyObject_New(Hash, &Hash_Type);
  635. + if (!self)
  636. + return NULL;
  637. + self->lock = NULL;
  638. + hc->init(&self->ctx.vtable);
  639. +
  640. + if (string->len >= HASHLIB_GIL_MINSIZE) {
  641. + Py_BEGIN_ALLOW_THREADS
  642. + hc->update(&self->ctx.vtable, string->buf, string->len);
  643. + Py_END_ALLOW_THREADS
  644. + } else {
  645. + hc->update(&self->ctx.vtable, string->buf, string->len);
  646. + }
  647. +
  648. + return (PyObject *)self;
  649. +}
  650. +
  651. +static PyObject *
  652. +SHAKE_new(PyObject *module, int bits, Py_buffer *string, int usedforsecurity)
  653. +{
  654. + SHAKE *self;
  655. +
  656. + self = PyObject_New(SHAKE, &SHAKE_Type);
  657. + if (!self)
  658. + return NULL;
  659. + self->lock = NULL;
  660. + self->bits = bits;
  661. + br_shake_init(&self->ctx, bits);
  662. +
  663. + if (string->len >= HASHLIB_GIL_MINSIZE) {
  664. + Py_BEGIN_ALLOW_THREADS
  665. + br_shake_inject(&self->ctx, string->buf, string->len);
  666. + Py_END_ALLOW_THREADS
  667. + } else {
  668. + br_shake_inject(&self->ctx, string->buf, string->len);
  669. + }
  670. +
  671. + return (PyObject *)self;
  672. +}
  673. +
  674. +/*[clinic input]
  675. +_hashlib.new
  676. +
  677. + name: str
  678. + string: Py_buffer = None
  679. + *
  680. + usedforsecurity: bool = True
  681. +
  682. +Return a new hash object using the named algorithm.
  683. +
  684. +An optional string argument may be provided and will be
  685. +automatically hashed.
  686. +
  687. +The MD5 and SHA1 algorithms are always supported.
  688. +[clinic start generated code]*/
  689. +
  690. +static PyObject *
  691. +_hashlib_new_impl(PyObject *module, const char *name, Py_buffer *string,
  692. + int usedforsecurity)
  693. +/*[clinic end generated code: output=5459beb11946eb8d input=c7e3a9928c6af923]*/
  694. +{
  695. + const br_hash_class *hc = NULL;
  696. + int shake = 0;
  697. +
  698. + hc = py_hash_by_name(name);
  699. + if (hc)
  700. + ;
  701. + else if (strcmp(name, "shake_128") == 0)
  702. + shake = 128;
  703. + else if (strcmp(name, "shake_256") == 0)
  704. + shake = 256;
  705. + else {
  706. + PyErr_SetString(PyExc_ValueError, "unsupported hash type");
  707. + return NULL;
  708. + }
  709. +
  710. + if (hc)
  711. + return Hash_new_vtable(module, hc, string, usedforsecurity);
  712. + if (shake)
  713. + return SHAKE_new(module, shake, string, usedforsecurity);
  714. +}
  715. +
  716. +/*[clinic input]
  717. +_hashlib.openssl_md5
  718. +
  719. + string: Py_buffer = None
  720. + *
  721. + usedforsecurity: bool = True
  722. +
  723. +Returns a md5 hash object; optionally initialized with a string.
  724. +[clinic start generated code]*/
  725. +
  726. +static PyObject *
  727. +_hashlib_openssl_md5_impl(PyObject *module, Py_buffer *string,
  728. + int usedforsecurity)
  729. +/*[clinic end generated code: output=bf0df21c3ecd9d85 input=75e59cc38a292e51]*/
  730. +{
  731. + return Hash_new_vtable(module, &br_md5_vtable, string, usedforsecurity);
  732. +}
  733. +
  734. +/*[clinic input]
  735. +_hashlib.openssl_sha1
  736. +
  737. + string: Py_buffer = None
  738. + *
  739. + usedforsecurity: bool = True
  740. +
  741. +Returns a sha1 hash object; optionally initialized with a string.
  742. +[clinic start generated code]*/
  743. +
  744. +static PyObject *
  745. +_hashlib_openssl_sha1_impl(PyObject *module, Py_buffer *string,
  746. + int usedforsecurity)
  747. +/*[clinic end generated code: output=9c468cf3cfdd2e57 input=0f8ad0fa5d988be8]*/
  748. +{
  749. + return Hash_new_vtable(module, &br_sha1_vtable, string, usedforsecurity);
  750. +}
  751. +
  752. +/*[clinic input]
  753. +_hashlib.openssl_sha224
  754. +
  755. + string: Py_buffer = None
  756. + *
  757. + usedforsecurity: bool = True
  758. +
  759. +Returns a sha224 hash object; optionally initialized with a string.
  760. +[clinic start generated code]*/
  761. +
  762. +static PyObject *
  763. +_hashlib_openssl_sha224_impl(PyObject *module, Py_buffer *string,
  764. + int usedforsecurity)
  765. +/*[clinic end generated code: output=9b3fea17aacc8dfe input=21aacab0e6949431]*/
  766. +{
  767. + return Hash_new_vtable(module, &br_sha224_vtable, string, usedforsecurity);
  768. +}
  769. +
  770. +/*[clinic input]
  771. +_hashlib.openssl_sha256
  772. +
  773. + string: Py_buffer = None
  774. + *
  775. + usedforsecurity: bool = True
  776. +
  777. +Returns a sha256 hash object; optionally initialized with a string.
  778. +[clinic start generated code]*/
  779. +
  780. +static PyObject *
  781. +_hashlib_openssl_sha256_impl(PyObject *module, Py_buffer *string,
  782. + int usedforsecurity)
  783. +/*[clinic end generated code: output=2c0585ccc6dfa22a input=81dd291675e0f6c9]*/
  784. +{
  785. + return Hash_new_vtable(module, &br_sha256_vtable, string, usedforsecurity);
  786. +}
  787. +
  788. +/*[clinic input]
  789. +_hashlib.openssl_sha384
  790. +
  791. + string: Py_buffer = None
  792. + *
  793. + usedforsecurity: bool = True
  794. +
  795. +Returns a sha384 hash object; optionally initialized with a string.
  796. +[clinic start generated code]*/
  797. +
  798. +static PyObject *
  799. +_hashlib_openssl_sha384_impl(PyObject *module, Py_buffer *string,
  800. + int usedforsecurity)
  801. +/*[clinic end generated code: output=196d8d7558cfd155 input=eed0b0128dd67969]*/
  802. +{
  803. + return Hash_new_vtable(module, &br_sha384_vtable, string, usedforsecurity);
  804. +}
  805. +
  806. +/*[clinic input]
  807. +_hashlib.openssl_sha512
  808. +
  809. + string: Py_buffer = None
  810. + *
  811. + usedforsecurity: bool = True
  812. +
  813. +Returns a sha512 hash object; optionally initialized with a string.
  814. +[clinic start generated code]*/
  815. +
  816. +static PyObject *
  817. +_hashlib_openssl_sha512_impl(PyObject *module, Py_buffer *string,
  818. + int usedforsecurity)
  819. +/*[clinic end generated code: output=7349b37b20ff6f75 input=da1228b585897043]*/
  820. +{
  821. + return Hash_new_vtable(module, &br_sha512_vtable, string, usedforsecurity);
  822. +}
  823. +
  824. +/*[clinic input]
  825. +_hashlib.openssl_shake_128
  826. +
  827. + string: Py_buffer = None
  828. + *
  829. + usedforsecurity: bool = True
  830. +
  831. +Returns a shake-128 variable hash object; optionally initialized with a string.
  832. +[clinic start generated code]*/
  833. +
  834. +static PyObject *
  835. +_hashlib_openssl_shake_128_impl(PyObject *module, Py_buffer *string,
  836. + int usedforsecurity)
  837. +/*[clinic end generated code: output=e51cc0e4bded887e input=fb6dce24fc91d53d]*/
  838. +{
  839. + return SHAKE_new(module, 128, string, usedforsecurity);
  840. +}
  841. +
  842. +/*[clinic input]
  843. +_hashlib.openssl_shake_256
  844. +
  845. + string: Py_buffer = None
  846. + *
  847. + usedforsecurity: bool = True
  848. +
  849. +Returns a shake-256 variable hash object; optionally initialized with a string.
  850. +[clinic start generated code]*/
  851. +
  852. +static PyObject *
  853. +_hashlib_openssl_shake_256_impl(PyObject *module, Py_buffer *string,
  854. + int usedforsecurity)
  855. +/*[clinic end generated code: output=983a76ff0796751b input=9eb2a133d11e34dc]*/
  856. +{
  857. + return SHAKE_new(module, 256, string, usedforsecurity);
  858. +}
  859. +
  860. +static int
  861. +_tscmp(const unsigned char *a, const unsigned char *b,
  862. + Py_ssize_t len_a, Py_ssize_t len_b)
  863. +{
  864. + /* loop count depends on length of b. Might leak very little timing
  865. + * information if sizes are different.
  866. + */
  867. + Py_ssize_t length = len_b;
  868. + unsigned result = 0;
  869. + size_t i;
  870. +
  871. + if (len_a != length) {
  872. + a = b;
  873. + result = 1;
  874. + }
  875. +
  876. + for (i = 0; i < length; i++)
  877. + result |= (unsigned)a[i] ^ (unsigned)b[i];
  878. +
  879. + return ~(result | -result) >> CHAR_BIT * sizeof(result) - 1;
  880. +}
  881. +
  882. +/*[clinic input]
  883. +_hashlib.compare_digest
  884. +
  885. + a: object
  886. + b: object
  887. + /
  888. +
  889. +Return 'a == b'.
  890. +
  891. +This function uses an approach designed to prevent
  892. +timing analysis, making it appropriate for cryptography.
  893. +
  894. +a and b must both be of the same type: either str (ASCII only),
  895. +or any bytes-like object.
  896. +
  897. +Note: If a and b are of different lengths, or if an error occurs,
  898. +a timing attack could theoretically reveal information about the
  899. +types and lengths of a and b--but not their values.
  900. +[clinic start generated code]*/
  901. +
  902. +static PyObject *
  903. +_hashlib_compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
  904. +/*[clinic end generated code: output=6f1c13927480aed9 input=f9cc6da970b1b1d5]*/
  905. +{
  906. + int rc;
  907. +
  908. + /* ASCII unicode string */
  909. + if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
  910. + if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
  911. + return NULL;
  912. + }
  913. + if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
  914. + PyErr_SetString(PyExc_TypeError,
  915. + "comparing strings with non-ASCII characters is "
  916. + "not supported");
  917. + return NULL;
  918. + }
  919. +
  920. + rc = _tscmp(PyUnicode_DATA(a),
  921. + PyUnicode_DATA(b),
  922. + PyUnicode_GET_LENGTH(a),
  923. + PyUnicode_GET_LENGTH(b));
  924. + }
  925. + /* fallback to buffer interface for bytes, bytesarray and other */
  926. + else {
  927. + Py_buffer view_a;
  928. + Py_buffer view_b;
  929. +
  930. + if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
  931. + PyErr_Format(PyExc_TypeError,
  932. + "unsupported operand types(s) or combination of types: "
  933. + "'%.100s' and '%.100s'",
  934. + Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
  935. + return NULL;
  936. + }
  937. +
  938. + if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
  939. + return NULL;
  940. + }
  941. + if (view_a.ndim > 1) {
  942. + PyErr_SetString(PyExc_BufferError,
  943. + "Buffer must be single dimension");
  944. + PyBuffer_Release(&view_a);
  945. + return NULL;
  946. + }
  947. +
  948. + if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
  949. + PyBuffer_Release(&view_a);
  950. + return NULL;
  951. + }
  952. + if (view_b.ndim > 1) {
  953. + PyErr_SetString(PyExc_BufferError,
  954. + "Buffer must be single dimension");
  955. + PyBuffer_Release(&view_a);
  956. + PyBuffer_Release(&view_b);
  957. + return NULL;
  958. + }
  959. +
  960. + rc = _tscmp((const unsigned char*)view_a.buf,
  961. + (const unsigned char*)view_b.buf,
  962. + view_a.len,
  963. + view_b.len);
  964. +
  965. + PyBuffer_Release(&view_a);
  966. + PyBuffer_Release(&view_b);
  967. + }
  968. +
  969. + return PyBool_FromLong(rc);
  970. +}
  971. +
  972. +static struct PyMethodDef hashlib_methods[] = {
  973. + _HASHLIB_NEW_METHODDEF
  974. + _HASHLIB_HMAC_SINGLESHOT_METHODDEF
  975. + _HASHLIB_HMAC_NEW_METHODDEF
  976. + _HASHLIB_OPENSSL_MD5_METHODDEF
  977. + _HASHLIB_OPENSSL_SHA1_METHODDEF
  978. + _HASHLIB_OPENSSL_SHA224_METHODDEF
  979. + _HASHLIB_OPENSSL_SHA256_METHODDEF
  980. + _HASHLIB_OPENSSL_SHA384_METHODDEF
  981. + _HASHLIB_OPENSSL_SHA512_METHODDEF
  982. + _HASHLIB_OPENSSL_SHAKE_128_METHODDEF
  983. + _HASHLIB_OPENSSL_SHAKE_256_METHODDEF
  984. + _HASHLIB_COMPARE_DIGEST_METHODDEF
  985. + {0}
  986. +};
  987. +
  988. +/* Fast HMAC for hmac.digest() */
  989. +
  990. +/*[clinic input]
  991. +_hashlib.hmac_digest as _hashlib_hmac_singleshot
  992. +
  993. + key: Py_buffer
  994. + msg: Py_buffer
  995. + digest: str
  996. +
  997. +Single-shot HMAC.
  998. +[clinic start generated code]*/
  999. +
  1000. +static PyObject *
  1001. +_hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
  1002. + Py_buffer *msg, const char *digest)
  1003. +/*[clinic end generated code: output=15658ede5ab98185 input=019dffc571909a46]*/
  1004. +{
  1005. + char buf[64];
  1006. + Py_ssize_t buf_len;
  1007. + br_hmac_context ctx;
  1008. + br_hmac_key_context keyctx;
  1009. + const br_hash_class *hc;
  1010. +
  1011. + hc = py_hash_by_name(digest);
  1012. + if (!hc) {
  1013. + PyErr_SetString(PyExc_ValueError, "unsupported hash type");
  1014. + return NULL;
  1015. + }
  1016. + if (msg->len >= HASHLIB_GIL_MINSIZE) {
  1017. + Py_BEGIN_ALLOW_THREADS
  1018. + br_hmac_key_init(&keyctx, hc, key->buf, key->len);
  1019. + br_hmac_init(&ctx, &keyctx, 0);
  1020. + br_hmac_update(&ctx, msg->buf, msg->len);
  1021. + br_hmac_out(&ctx, buf);
  1022. + Py_END_ALLOW_THREADS
  1023. + } else {
  1024. + br_hmac_key_init(&keyctx, hc, key->buf, key->len);
  1025. + br_hmac_init(&ctx, &keyctx, 0);
  1026. + br_hmac_update(&ctx, msg->buf, msg->len);
  1027. + br_hmac_out(&ctx, buf);
  1028. + }
  1029. + buf_len = br_hmac_size(&ctx);
  1030. +
  1031. + return PyBytes_FromStringAndSize(buf, buf_len);
  1032. +}
  1033. +
  1034. +/*[clinic input]
  1035. +_hashlib.hmac_new
  1036. +
  1037. + key: Py_buffer
  1038. + msg: Py_buffer = None
  1039. + digestmod: str(c_default="NULL") = None
  1040. +
  1041. +Return a new hmac object.
  1042. +[clinic start generated code]*/
  1043. +
  1044. +static PyObject *
  1045. +_hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, Py_buffer *msg,
  1046. + const char *digestmod)
  1047. +/*[clinic end generated code: output=5733a8600554bba2 input=b0125f4222a0d06d]*/
  1048. +{
  1049. + HMAC *self;
  1050. + const br_hash_class *hc;
  1051. +
  1052. + printf("hmac new\n");
  1053. +
  1054. + if (!digestmod || !digestmod[0]) {
  1055. + PyErr_SetString(
  1056. + PyExc_TypeError, "missing required parameter 'digestmod'");
  1057. + return NULL;
  1058. + }
  1059. + hc = py_hash_by_name(digestmod);
  1060. + if (!hc) {
  1061. + PyErr_SetString(PyExc_ValueError, "unknown hash function");
  1062. + return NULL;
  1063. + }
  1064. + self = PyObject_New(HMAC, &HMAC_Type);
  1065. + if (!self)
  1066. + return NULL;
  1067. + self->lock = NULL;
  1068. + br_hmac_key_init(&self->key, hc, key->buf, key->len);
  1069. + br_hmac_init(&self->ctx, &self->key, 0);
  1070. +
  1071. + if (msg->len >= HASHLIB_GIL_MINSIZE) {
  1072. + Py_BEGIN_ALLOW_THREADS
  1073. + br_hmac_update(&self->ctx, msg->buf, msg->len);
  1074. + Py_END_ALLOW_THREADS
  1075. + } else {
  1076. + br_hmac_update(&self->ctx, msg->buf, msg->len);
  1077. + }
  1078. +
  1079. + return (PyObject *)self;
  1080. +}
  1081. +
  1082. +static struct PyModuleDef _hashlibmodule = {
  1083. + PyModuleDef_HEAD_INIT,
  1084. + .m_name = "_hashlib",
  1085. + .m_doc = "BearSSL interface for hashlib module",
  1086. + .m_methods = hashlib_methods,
  1087. +};
  1088. +
  1089. +PyMODINIT_FUNC
  1090. +PyInit__hashlib(void)
  1091. +{
  1092. + PyObject *m = NULL, *set = NULL, *name;
  1093. + static const char *const names[] = {
  1094. + "md5",
  1095. + "sha1",
  1096. + "sha224",
  1097. + "sha256",
  1098. + "sha384",
  1099. + "sha512",
  1100. + "shake_128",
  1101. + "shake_256",
  1102. + };
  1103. + int r;
  1104. +
  1105. + m = PyState_FindModule(&_hashlibmodule);
  1106. + if (m) {
  1107. + Py_INCREF(m);
  1108. + return m;
  1109. + }
  1110. + m = PyModule_Create(&_hashlibmodule);
  1111. + if (!m)
  1112. + goto err;
  1113. + if (PyModule_AddType(m, &Hash_Type) < 0)
  1114. + goto err;
  1115. + if (PyModule_AddType(m, &SHAKE_Type) < 0)
  1116. + goto err;
  1117. + if (PyModule_AddType(m, &HMAC_Type) < 0)
  1118. + goto err;
  1119. + set = PyFrozenSet_New(NULL);
  1120. + if (!set)
  1121. + goto err;
  1122. + for (size_t i = 0; i < sizeof(names) / sizeof(names[0]); i++) {
  1123. + name = PyUnicode_FromString(names[i]);
  1124. + if (!name)
  1125. + goto err;
  1126. + r = PySet_Add(set, name);
  1127. + Py_DECREF(name);
  1128. + if (r != 0)
  1129. + goto err;
  1130. + }
  1131. + if (PyModule_AddObject(m, "openssl_md_meth_names", set) < 0)
  1132. + goto err;
  1133. +
  1134. + return m;
  1135. +
  1136. + err:
  1137. + if (set)
  1138. + Py_DECREF(set);
  1139. + if (m)
  1140. + Py_DECREF(m);
  1141. + return NULL;
  1142. +}
  1143. diff --git a/Modules/clinic/_hashbearssl.c.h b/Modules/clinic/_hashbearssl.c.h
  1144. new file mode 100644
  1145. index 0000000000..49bf8f9287
  1146. --- /dev/null
  1147. +++ b/Modules/clinic/_hashbearssl.c.h
  1148. @@ -0,0 +1,1113 @@
  1149. +/*[clinic input]
  1150. +preserve
  1151. +[clinic start generated code]*/
  1152. +
  1153. +PyDoc_STRVAR(Hash_update__doc__,
  1154. +"update($self, data, /)\n"
  1155. +"--\n"
  1156. +"\n"
  1157. +"Update this hash object\'s state with the provided string.");
  1158. +
  1159. +#define HASH_UPDATE_METHODDEF \
  1160. + {"update", (PyCFunction)Hash_update, METH_O, Hash_update__doc__},
  1161. +
  1162. +static PyObject *
  1163. +Hash_update_impl(Hash *self, Py_buffer *data);
  1164. +
  1165. +static PyObject *
  1166. +Hash_update(Hash *self, PyObject *arg)
  1167. +{
  1168. + PyObject *return_value = NULL;
  1169. + Py_buffer data = {NULL, NULL};
  1170. +
  1171. + if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
  1172. + goto exit;
  1173. + }
  1174. + if (!PyBuffer_IsContiguous(&data, 'C')) {
  1175. + _PyArg_BadArgument("update", "argument", "contiguous buffer", arg);
  1176. + goto exit;
  1177. + }
  1178. + return_value = Hash_update_impl(self, &data);
  1179. +
  1180. +exit:
  1181. + /* Cleanup for data */
  1182. + if (data.obj) {
  1183. + PyBuffer_Release(&data);
  1184. + }
  1185. +
  1186. + return return_value;
  1187. +}
  1188. +
  1189. +PyDoc_STRVAR(Hash_digest__doc__,
  1190. +"digest($self, /)\n"
  1191. +"--\n"
  1192. +"\n"
  1193. +"Return the digest value as a bytes object.");
  1194. +
  1195. +#define HASH_DIGEST_METHODDEF \
  1196. + {"digest", (PyCFunction)Hash_digest, METH_NOARGS, Hash_digest__doc__},
  1197. +
  1198. +static PyObject *
  1199. +Hash_digest_impl(Hash *self);
  1200. +
  1201. +static PyObject *
  1202. +Hash_digest(Hash *self, PyObject *Py_UNUSED(ignored))
  1203. +{
  1204. + return Hash_digest_impl(self);
  1205. +}
  1206. +
  1207. +PyDoc_STRVAR(Hash_hexdigest__doc__,
  1208. +"hexdigest($self, /)\n"
  1209. +"--\n"
  1210. +"\n"
  1211. +"Return the digest value as a string of hexadecimal digits.");
  1212. +
  1213. +#define HASH_HEXDIGEST_METHODDEF \
  1214. + {"hexdigest", (PyCFunction)Hash_hexdigest, METH_NOARGS, Hash_hexdigest__doc__},
  1215. +
  1216. +static PyObject *
  1217. +Hash_hexdigest_impl(Hash *self);
  1218. +
  1219. +static PyObject *
  1220. +Hash_hexdigest(Hash *self, PyObject *Py_UNUSED(ignored))
  1221. +{
  1222. + return Hash_hexdigest_impl(self);
  1223. +}
  1224. +
  1225. +PyDoc_STRVAR(Hash_copy__doc__,
  1226. +"copy($self, /)\n"
  1227. +"--\n"
  1228. +"\n"
  1229. +"Return a copy of the hash object.");
  1230. +
  1231. +#define HASH_COPY_METHODDEF \
  1232. + {"copy", (PyCFunction)Hash_copy, METH_NOARGS, Hash_copy__doc__},
  1233. +
  1234. +static PyObject *
  1235. +Hash_copy_impl(Hash *self);
  1236. +
  1237. +static PyObject *
  1238. +Hash_copy(Hash *self, PyObject *Py_UNUSED(ignored))
  1239. +{
  1240. + return Hash_copy_impl(self);
  1241. +}
  1242. +
  1243. +PyDoc_STRVAR(SHAKE_update__doc__,
  1244. +"update($self, data, /)\n"
  1245. +"--\n"
  1246. +"\n"
  1247. +"Update this hash object\'s state with the provided string.");
  1248. +
  1249. +#define SHAKE_UPDATE_METHODDEF \
  1250. + {"update", (PyCFunction)SHAKE_update, METH_O, SHAKE_update__doc__},
  1251. +
  1252. +static PyObject *
  1253. +SHAKE_update_impl(SHAKE *self, Py_buffer *data);
  1254. +
  1255. +static PyObject *
  1256. +SHAKE_update(SHAKE *self, PyObject *arg)
  1257. +{
  1258. + PyObject *return_value = NULL;
  1259. + Py_buffer data = {NULL, NULL};
  1260. +
  1261. + if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
  1262. + goto exit;
  1263. + }
  1264. + if (!PyBuffer_IsContiguous(&data, 'C')) {
  1265. + _PyArg_BadArgument("update", "argument", "contiguous buffer", arg);
  1266. + goto exit;
  1267. + }
  1268. + return_value = SHAKE_update_impl(self, &data);
  1269. +
  1270. +exit:
  1271. + /* Cleanup for data */
  1272. + if (data.obj) {
  1273. + PyBuffer_Release(&data);
  1274. + }
  1275. +
  1276. + return return_value;
  1277. +}
  1278. +
  1279. +PyDoc_STRVAR(SHAKE_digest__doc__,
  1280. +"digest($self, /, length)\n"
  1281. +"--\n"
  1282. +"\n"
  1283. +"Return the digest value as a bytes object.");
  1284. +
  1285. +#define SHAKE_DIGEST_METHODDEF \
  1286. + {"digest", (PyCFunction)(void(*)(void))SHAKE_digest, METH_FASTCALL|METH_KEYWORDS, SHAKE_digest__doc__},
  1287. +
  1288. +static PyObject *
  1289. +SHAKE_digest_impl(SHAKE *self, Py_ssize_t length);
  1290. +
  1291. +static PyObject *
  1292. +SHAKE_digest(SHAKE *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1293. +{
  1294. + PyObject *return_value = NULL;
  1295. + static const char * const _keywords[] = {"length", NULL};
  1296. + static _PyArg_Parser _parser = {NULL, _keywords, "digest", 0};
  1297. + PyObject *argsbuf[1];
  1298. + Py_ssize_t length;
  1299. +
  1300. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
  1301. + if (!args) {
  1302. + goto exit;
  1303. + }
  1304. + if (PyFloat_Check(args[0])) {
  1305. + PyErr_SetString(PyExc_TypeError,
  1306. + "integer argument expected, got float" );
  1307. + goto exit;
  1308. + }
  1309. + {
  1310. + Py_ssize_t ival = -1;
  1311. + PyObject *iobj = PyNumber_Index(args[0]);
  1312. + if (iobj != NULL) {
  1313. + ival = PyLong_AsSsize_t(iobj);
  1314. + Py_DECREF(iobj);
  1315. + }
  1316. + if (ival == -1 && PyErr_Occurred()) {
  1317. + goto exit;
  1318. + }
  1319. + length = ival;
  1320. + }
  1321. + return_value = SHAKE_digest_impl(self, length);
  1322. +
  1323. +exit:
  1324. + return return_value;
  1325. +}
  1326. +
  1327. +PyDoc_STRVAR(SHAKE_hexdigest__doc__,
  1328. +"hexdigest($self, /, length)\n"
  1329. +"--\n"
  1330. +"\n"
  1331. +"Return the digest value as a string of hexadecimal digits.");
  1332. +
  1333. +#define SHAKE_HEXDIGEST_METHODDEF \
  1334. + {"hexdigest", (PyCFunction)(void(*)(void))SHAKE_hexdigest, METH_FASTCALL|METH_KEYWORDS, SHAKE_hexdigest__doc__},
  1335. +
  1336. +static PyObject *
  1337. +SHAKE_hexdigest_impl(SHAKE *self, Py_ssize_t length);
  1338. +
  1339. +static PyObject *
  1340. +SHAKE_hexdigest(SHAKE *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1341. +{
  1342. + PyObject *return_value = NULL;
  1343. + static const char * const _keywords[] = {"length", NULL};
  1344. + static _PyArg_Parser _parser = {NULL, _keywords, "hexdigest", 0};
  1345. + PyObject *argsbuf[1];
  1346. + Py_ssize_t length;
  1347. +
  1348. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
  1349. + if (!args) {
  1350. + goto exit;
  1351. + }
  1352. + if (PyFloat_Check(args[0])) {
  1353. + PyErr_SetString(PyExc_TypeError,
  1354. + "integer argument expected, got float" );
  1355. + goto exit;
  1356. + }
  1357. + {
  1358. + Py_ssize_t ival = -1;
  1359. + PyObject *iobj = PyNumber_Index(args[0]);
  1360. + if (iobj != NULL) {
  1361. + ival = PyLong_AsSsize_t(iobj);
  1362. + Py_DECREF(iobj);
  1363. + }
  1364. + if (ival == -1 && PyErr_Occurred()) {
  1365. + goto exit;
  1366. + }
  1367. + length = ival;
  1368. + }
  1369. + return_value = SHAKE_hexdigest_impl(self, length);
  1370. +
  1371. +exit:
  1372. + return return_value;
  1373. +}
  1374. +
  1375. +PyDoc_STRVAR(SHAKE_copy__doc__,
  1376. +"copy($self, /)\n"
  1377. +"--\n"
  1378. +"\n"
  1379. +"Return a copy of the SHAKE object.");
  1380. +
  1381. +#define SHAKE_COPY_METHODDEF \
  1382. + {"copy", (PyCFunction)SHAKE_copy, METH_NOARGS, SHAKE_copy__doc__},
  1383. +
  1384. +static PyObject *
  1385. +SHAKE_copy_impl(SHAKE *self);
  1386. +
  1387. +static PyObject *
  1388. +SHAKE_copy(SHAKE *self, PyObject *Py_UNUSED(ignored))
  1389. +{
  1390. + return SHAKE_copy_impl(self);
  1391. +}
  1392. +
  1393. +PyDoc_STRVAR(HMAC_copy__doc__,
  1394. +"copy($self, /)\n"
  1395. +"--\n"
  1396. +"\n"
  1397. +"Return a copy of the HMAC object.");
  1398. +
  1399. +#define HMAC_COPY_METHODDEF \
  1400. + {"copy", (PyCFunction)HMAC_copy, METH_NOARGS, HMAC_copy__doc__},
  1401. +
  1402. +static PyObject *
  1403. +HMAC_copy_impl(HMAC *self);
  1404. +
  1405. +static PyObject *
  1406. +HMAC_copy(HMAC *self, PyObject *Py_UNUSED(ignored))
  1407. +{
  1408. + return HMAC_copy_impl(self);
  1409. +}
  1410. +
  1411. +PyDoc_STRVAR(HMAC_update__doc__,
  1412. +"update($self, data, /)\n"
  1413. +"--\n"
  1414. +"\n"
  1415. +"Update the HMAC object with data.");
  1416. +
  1417. +#define HMAC_UPDATE_METHODDEF \
  1418. + {"update", (PyCFunction)HMAC_update, METH_O, HMAC_update__doc__},
  1419. +
  1420. +static PyObject *
  1421. +HMAC_update_impl(HMAC *self, Py_buffer *data);
  1422. +
  1423. +static PyObject *
  1424. +HMAC_update(HMAC *self, PyObject *arg)
  1425. +{
  1426. + PyObject *return_value = NULL;
  1427. + Py_buffer data = {NULL, NULL};
  1428. +
  1429. + if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
  1430. + goto exit;
  1431. + }
  1432. + if (!PyBuffer_IsContiguous(&data, 'C')) {
  1433. + _PyArg_BadArgument("update", "argument", "contiguous buffer", arg);
  1434. + goto exit;
  1435. + }
  1436. + return_value = HMAC_update_impl(self, &data);
  1437. +
  1438. +exit:
  1439. + /* Cleanup for data */
  1440. + if (data.obj) {
  1441. + PyBuffer_Release(&data);
  1442. + }
  1443. +
  1444. + return return_value;
  1445. +}
  1446. +
  1447. +PyDoc_STRVAR(HMAC_digest__doc__,
  1448. +"digest($self, /)\n"
  1449. +"--\n"
  1450. +"\n"
  1451. +"Return the digest of the bytes passed to the update() method so far.");
  1452. +
  1453. +#define HMAC_DIGEST_METHODDEF \
  1454. + {"digest", (PyCFunction)HMAC_digest, METH_NOARGS, HMAC_digest__doc__},
  1455. +
  1456. +static PyObject *
  1457. +HMAC_digest_impl(HMAC *self);
  1458. +
  1459. +static PyObject *
  1460. +HMAC_digest(HMAC *self, PyObject *Py_UNUSED(ignored))
  1461. +{
  1462. + return HMAC_digest_impl(self);
  1463. +}
  1464. +
  1465. +PyDoc_STRVAR(HMAC_hexdigest__doc__,
  1466. +"hexdigest($self, /)\n"
  1467. +"--\n"
  1468. +"\n"
  1469. +"Return hexadecimal digest of the bytes passed to the update() method so far.\n"
  1470. +"\n"
  1471. +"This may be used to exchange the value safely in email or other non-binary\n"
  1472. +"environments.");
  1473. +
  1474. +#define HMAC_HEXDIGEST_METHODDEF \
  1475. + {"hexdigest", (PyCFunction)HMAC_hexdigest, METH_NOARGS, HMAC_hexdigest__doc__},
  1476. +
  1477. +static PyObject *
  1478. +HMAC_hexdigest_impl(HMAC *self);
  1479. +
  1480. +static PyObject *
  1481. +HMAC_hexdigest(HMAC *self, PyObject *Py_UNUSED(ignored))
  1482. +{
  1483. + return HMAC_hexdigest_impl(self);
  1484. +}
  1485. +
  1486. +PyDoc_STRVAR(_hashlib_new__doc__,
  1487. +"new($module, /, name, string=None, *, usedforsecurity=True)\n"
  1488. +"--\n"
  1489. +"\n"
  1490. +"Return a new hash object using the named algorithm.\n"
  1491. +"\n"
  1492. +"An optional string argument may be provided and will be\n"
  1493. +"automatically hashed.\n"
  1494. +"\n"
  1495. +"The MD5 and SHA1 algorithms are always supported.");
  1496. +
  1497. +#define _HASHLIB_NEW_METHODDEF \
  1498. + {"new", (PyCFunction)(void(*)(void))_hashlib_new, METH_FASTCALL|METH_KEYWORDS, _hashlib_new__doc__},
  1499. +
  1500. +static PyObject *
  1501. +_hashlib_new_impl(PyObject *module, const char *name, Py_buffer *string,
  1502. + int usedforsecurity);
  1503. +
  1504. +static PyObject *
  1505. +_hashlib_new(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1506. +{
  1507. + PyObject *return_value = NULL;
  1508. + static const char * const _keywords[] = {"name", "string", "usedforsecurity", NULL};
  1509. + static _PyArg_Parser _parser = {NULL, _keywords, "new", 0};
  1510. + PyObject *argsbuf[3];
  1511. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  1512. + const char *name;
  1513. + Py_buffer string = {NULL, NULL};
  1514. + int usedforsecurity = 1;
  1515. +
  1516. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
  1517. + if (!args) {
  1518. + goto exit;
  1519. + }
  1520. + if (!PyUnicode_Check(args[0])) {
  1521. + _PyArg_BadArgument("new", "argument 'name'", "str", args[0]);
  1522. + goto exit;
  1523. + }
  1524. + Py_ssize_t name_length;
  1525. + name = PyUnicode_AsUTF8AndSize(args[0], &name_length);
  1526. + if (name == NULL) {
  1527. + goto exit;
  1528. + }
  1529. + if (strlen(name) != (size_t)name_length) {
  1530. + PyErr_SetString(PyExc_ValueError, "embedded null character");
  1531. + goto exit;
  1532. + }
  1533. + if (!noptargs) {
  1534. + goto skip_optional_pos;
  1535. + }
  1536. + if (args[1]) {
  1537. + if (PyObject_GetBuffer(args[1], &string, PyBUF_SIMPLE) != 0) {
  1538. + goto exit;
  1539. + }
  1540. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1541. + _PyArg_BadArgument("new", "argument 'string'", "contiguous buffer", args[1]);
  1542. + goto exit;
  1543. + }
  1544. + if (!--noptargs) {
  1545. + goto skip_optional_pos;
  1546. + }
  1547. + }
  1548. +skip_optional_pos:
  1549. + if (!noptargs) {
  1550. + goto skip_optional_kwonly;
  1551. + }
  1552. + usedforsecurity = PyObject_IsTrue(args[2]);
  1553. + if (usedforsecurity < 0) {
  1554. + goto exit;
  1555. + }
  1556. +skip_optional_kwonly:
  1557. + return_value = _hashlib_new_impl(module, name, &string, usedforsecurity);
  1558. +
  1559. +exit:
  1560. + /* Cleanup for string */
  1561. + if (string.obj) {
  1562. + PyBuffer_Release(&string);
  1563. + }
  1564. +
  1565. + return return_value;
  1566. +}
  1567. +
  1568. +PyDoc_STRVAR(_hashlib_openssl_md5__doc__,
  1569. +"openssl_md5($module, /, string=None, *, usedforsecurity=True)\n"
  1570. +"--\n"
  1571. +"\n"
  1572. +"Returns a md5 hash object; optionally initialized with a string.");
  1573. +
  1574. +#define _HASHLIB_OPENSSL_MD5_METHODDEF \
  1575. + {"openssl_md5", (PyCFunction)(void(*)(void))_hashlib_openssl_md5, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_md5__doc__},
  1576. +
  1577. +static PyObject *
  1578. +_hashlib_openssl_md5_impl(PyObject *module, Py_buffer *string,
  1579. + int usedforsecurity);
  1580. +
  1581. +static PyObject *
  1582. +_hashlib_openssl_md5(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1583. +{
  1584. + PyObject *return_value = NULL;
  1585. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  1586. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_md5", 0};
  1587. + PyObject *argsbuf[2];
  1588. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1589. + Py_buffer string = {NULL, NULL};
  1590. + int usedforsecurity = 1;
  1591. +
  1592. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1593. + if (!args) {
  1594. + goto exit;
  1595. + }
  1596. + if (!noptargs) {
  1597. + goto skip_optional_pos;
  1598. + }
  1599. + if (args[0]) {
  1600. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  1601. + goto exit;
  1602. + }
  1603. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1604. + _PyArg_BadArgument("openssl_md5", "argument 'string'", "contiguous buffer", args[0]);
  1605. + goto exit;
  1606. + }
  1607. + if (!--noptargs) {
  1608. + goto skip_optional_pos;
  1609. + }
  1610. + }
  1611. +skip_optional_pos:
  1612. + if (!noptargs) {
  1613. + goto skip_optional_kwonly;
  1614. + }
  1615. + usedforsecurity = PyObject_IsTrue(args[1]);
  1616. + if (usedforsecurity < 0) {
  1617. + goto exit;
  1618. + }
  1619. +skip_optional_kwonly:
  1620. + return_value = _hashlib_openssl_md5_impl(module, &string, usedforsecurity);
  1621. +
  1622. +exit:
  1623. + /* Cleanup for string */
  1624. + if (string.obj) {
  1625. + PyBuffer_Release(&string);
  1626. + }
  1627. +
  1628. + return return_value;
  1629. +}
  1630. +
  1631. +PyDoc_STRVAR(_hashlib_openssl_sha1__doc__,
  1632. +"openssl_sha1($module, /, string=None, *, usedforsecurity=True)\n"
  1633. +"--\n"
  1634. +"\n"
  1635. +"Returns a sha1 hash object; optionally initialized with a string.");
  1636. +
  1637. +#define _HASHLIB_OPENSSL_SHA1_METHODDEF \
  1638. + {"openssl_sha1", (PyCFunction)(void(*)(void))_hashlib_openssl_sha1, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha1__doc__},
  1639. +
  1640. +static PyObject *
  1641. +_hashlib_openssl_sha1_impl(PyObject *module, Py_buffer *string,
  1642. + int usedforsecurity);
  1643. +
  1644. +static PyObject *
  1645. +_hashlib_openssl_sha1(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1646. +{
  1647. + PyObject *return_value = NULL;
  1648. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  1649. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_sha1", 0};
  1650. + PyObject *argsbuf[2];
  1651. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1652. + Py_buffer string = {NULL, NULL};
  1653. + int usedforsecurity = 1;
  1654. +
  1655. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1656. + if (!args) {
  1657. + goto exit;
  1658. + }
  1659. + if (!noptargs) {
  1660. + goto skip_optional_pos;
  1661. + }
  1662. + if (args[0]) {
  1663. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  1664. + goto exit;
  1665. + }
  1666. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1667. + _PyArg_BadArgument("openssl_sha1", "argument 'string'", "contiguous buffer", args[0]);
  1668. + goto exit;
  1669. + }
  1670. + if (!--noptargs) {
  1671. + goto skip_optional_pos;
  1672. + }
  1673. + }
  1674. +skip_optional_pos:
  1675. + if (!noptargs) {
  1676. + goto skip_optional_kwonly;
  1677. + }
  1678. + usedforsecurity = PyObject_IsTrue(args[1]);
  1679. + if (usedforsecurity < 0) {
  1680. + goto exit;
  1681. + }
  1682. +skip_optional_kwonly:
  1683. + return_value = _hashlib_openssl_sha1_impl(module, &string, usedforsecurity);
  1684. +
  1685. +exit:
  1686. + /* Cleanup for string */
  1687. + if (string.obj) {
  1688. + PyBuffer_Release(&string);
  1689. + }
  1690. +
  1691. + return return_value;
  1692. +}
  1693. +
  1694. +PyDoc_STRVAR(_hashlib_openssl_sha224__doc__,
  1695. +"openssl_sha224($module, /, string=None, *, usedforsecurity=True)\n"
  1696. +"--\n"
  1697. +"\n"
  1698. +"Returns a sha224 hash object; optionally initialized with a string.");
  1699. +
  1700. +#define _HASHLIB_OPENSSL_SHA224_METHODDEF \
  1701. + {"openssl_sha224", (PyCFunction)(void(*)(void))_hashlib_openssl_sha224, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha224__doc__},
  1702. +
  1703. +static PyObject *
  1704. +_hashlib_openssl_sha224_impl(PyObject *module, Py_buffer *string,
  1705. + int usedforsecurity);
  1706. +
  1707. +static PyObject *
  1708. +_hashlib_openssl_sha224(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1709. +{
  1710. + PyObject *return_value = NULL;
  1711. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  1712. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_sha224", 0};
  1713. + PyObject *argsbuf[2];
  1714. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1715. + Py_buffer string = {NULL, NULL};
  1716. + int usedforsecurity = 1;
  1717. +
  1718. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1719. + if (!args) {
  1720. + goto exit;
  1721. + }
  1722. + if (!noptargs) {
  1723. + goto skip_optional_pos;
  1724. + }
  1725. + if (args[0]) {
  1726. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  1727. + goto exit;
  1728. + }
  1729. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1730. + _PyArg_BadArgument("openssl_sha224", "argument 'string'", "contiguous buffer", args[0]);
  1731. + goto exit;
  1732. + }
  1733. + if (!--noptargs) {
  1734. + goto skip_optional_pos;
  1735. + }
  1736. + }
  1737. +skip_optional_pos:
  1738. + if (!noptargs) {
  1739. + goto skip_optional_kwonly;
  1740. + }
  1741. + usedforsecurity = PyObject_IsTrue(args[1]);
  1742. + if (usedforsecurity < 0) {
  1743. + goto exit;
  1744. + }
  1745. +skip_optional_kwonly:
  1746. + return_value = _hashlib_openssl_sha224_impl(module, &string, usedforsecurity);
  1747. +
  1748. +exit:
  1749. + /* Cleanup for string */
  1750. + if (string.obj) {
  1751. + PyBuffer_Release(&string);
  1752. + }
  1753. +
  1754. + return return_value;
  1755. +}
  1756. +
  1757. +PyDoc_STRVAR(_hashlib_openssl_sha256__doc__,
  1758. +"openssl_sha256($module, /, string=None, *, usedforsecurity=True)\n"
  1759. +"--\n"
  1760. +"\n"
  1761. +"Returns a sha256 hash object; optionally initialized with a string.");
  1762. +
  1763. +#define _HASHLIB_OPENSSL_SHA256_METHODDEF \
  1764. + {"openssl_sha256", (PyCFunction)(void(*)(void))_hashlib_openssl_sha256, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha256__doc__},
  1765. +
  1766. +static PyObject *
  1767. +_hashlib_openssl_sha256_impl(PyObject *module, Py_buffer *string,
  1768. + int usedforsecurity);
  1769. +
  1770. +static PyObject *
  1771. +_hashlib_openssl_sha256(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1772. +{
  1773. + PyObject *return_value = NULL;
  1774. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  1775. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_sha256", 0};
  1776. + PyObject *argsbuf[2];
  1777. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1778. + Py_buffer string = {NULL, NULL};
  1779. + int usedforsecurity = 1;
  1780. +
  1781. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1782. + if (!args) {
  1783. + goto exit;
  1784. + }
  1785. + if (!noptargs) {
  1786. + goto skip_optional_pos;
  1787. + }
  1788. + if (args[0]) {
  1789. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  1790. + goto exit;
  1791. + }
  1792. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1793. + _PyArg_BadArgument("openssl_sha256", "argument 'string'", "contiguous buffer", args[0]);
  1794. + goto exit;
  1795. + }
  1796. + if (!--noptargs) {
  1797. + goto skip_optional_pos;
  1798. + }
  1799. + }
  1800. +skip_optional_pos:
  1801. + if (!noptargs) {
  1802. + goto skip_optional_kwonly;
  1803. + }
  1804. + usedforsecurity = PyObject_IsTrue(args[1]);
  1805. + if (usedforsecurity < 0) {
  1806. + goto exit;
  1807. + }
  1808. +skip_optional_kwonly:
  1809. + return_value = _hashlib_openssl_sha256_impl(module, &string, usedforsecurity);
  1810. +
  1811. +exit:
  1812. + /* Cleanup for string */
  1813. + if (string.obj) {
  1814. + PyBuffer_Release(&string);
  1815. + }
  1816. +
  1817. + return return_value;
  1818. +}
  1819. +
  1820. +PyDoc_STRVAR(_hashlib_openssl_sha384__doc__,
  1821. +"openssl_sha384($module, /, string=None, *, usedforsecurity=True)\n"
  1822. +"--\n"
  1823. +"\n"
  1824. +"Returns a sha384 hash object; optionally initialized with a string.");
  1825. +
  1826. +#define _HASHLIB_OPENSSL_SHA384_METHODDEF \
  1827. + {"openssl_sha384", (PyCFunction)(void(*)(void))_hashlib_openssl_sha384, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha384__doc__},
  1828. +
  1829. +static PyObject *
  1830. +_hashlib_openssl_sha384_impl(PyObject *module, Py_buffer *string,
  1831. + int usedforsecurity);
  1832. +
  1833. +static PyObject *
  1834. +_hashlib_openssl_sha384(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1835. +{
  1836. + PyObject *return_value = NULL;
  1837. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  1838. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_sha384", 0};
  1839. + PyObject *argsbuf[2];
  1840. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1841. + Py_buffer string = {NULL, NULL};
  1842. + int usedforsecurity = 1;
  1843. +
  1844. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1845. + if (!args) {
  1846. + goto exit;
  1847. + }
  1848. + if (!noptargs) {
  1849. + goto skip_optional_pos;
  1850. + }
  1851. + if (args[0]) {
  1852. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  1853. + goto exit;
  1854. + }
  1855. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1856. + _PyArg_BadArgument("openssl_sha384", "argument 'string'", "contiguous buffer", args[0]);
  1857. + goto exit;
  1858. + }
  1859. + if (!--noptargs) {
  1860. + goto skip_optional_pos;
  1861. + }
  1862. + }
  1863. +skip_optional_pos:
  1864. + if (!noptargs) {
  1865. + goto skip_optional_kwonly;
  1866. + }
  1867. + usedforsecurity = PyObject_IsTrue(args[1]);
  1868. + if (usedforsecurity < 0) {
  1869. + goto exit;
  1870. + }
  1871. +skip_optional_kwonly:
  1872. + return_value = _hashlib_openssl_sha384_impl(module, &string, usedforsecurity);
  1873. +
  1874. +exit:
  1875. + /* Cleanup for string */
  1876. + if (string.obj) {
  1877. + PyBuffer_Release(&string);
  1878. + }
  1879. +
  1880. + return return_value;
  1881. +}
  1882. +
  1883. +PyDoc_STRVAR(_hashlib_openssl_sha512__doc__,
  1884. +"openssl_sha512($module, /, string=None, *, usedforsecurity=True)\n"
  1885. +"--\n"
  1886. +"\n"
  1887. +"Returns a sha512 hash object; optionally initialized with a string.");
  1888. +
  1889. +#define _HASHLIB_OPENSSL_SHA512_METHODDEF \
  1890. + {"openssl_sha512", (PyCFunction)(void(*)(void))_hashlib_openssl_sha512, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_sha512__doc__},
  1891. +
  1892. +static PyObject *
  1893. +_hashlib_openssl_sha512_impl(PyObject *module, Py_buffer *string,
  1894. + int usedforsecurity);
  1895. +
  1896. +static PyObject *
  1897. +_hashlib_openssl_sha512(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1898. +{
  1899. + PyObject *return_value = NULL;
  1900. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  1901. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_sha512", 0};
  1902. + PyObject *argsbuf[2];
  1903. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1904. + Py_buffer string = {NULL, NULL};
  1905. + int usedforsecurity = 1;
  1906. +
  1907. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1908. + if (!args) {
  1909. + goto exit;
  1910. + }
  1911. + if (!noptargs) {
  1912. + goto skip_optional_pos;
  1913. + }
  1914. + if (args[0]) {
  1915. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  1916. + goto exit;
  1917. + }
  1918. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1919. + _PyArg_BadArgument("openssl_sha512", "argument 'string'", "contiguous buffer", args[0]);
  1920. + goto exit;
  1921. + }
  1922. + if (!--noptargs) {
  1923. + goto skip_optional_pos;
  1924. + }
  1925. + }
  1926. +skip_optional_pos:
  1927. + if (!noptargs) {
  1928. + goto skip_optional_kwonly;
  1929. + }
  1930. + usedforsecurity = PyObject_IsTrue(args[1]);
  1931. + if (usedforsecurity < 0) {
  1932. + goto exit;
  1933. + }
  1934. +skip_optional_kwonly:
  1935. + return_value = _hashlib_openssl_sha512_impl(module, &string, usedforsecurity);
  1936. +
  1937. +exit:
  1938. + /* Cleanup for string */
  1939. + if (string.obj) {
  1940. + PyBuffer_Release(&string);
  1941. + }
  1942. +
  1943. + return return_value;
  1944. +}
  1945. +
  1946. +PyDoc_STRVAR(_hashlib_openssl_shake_128__doc__,
  1947. +"openssl_shake_128($module, /, string=None, *, usedforsecurity=True)\n"
  1948. +"--\n"
  1949. +"\n"
  1950. +"Returns a shake-128 variable hash object; optionally initialized with a string.");
  1951. +
  1952. +#define _HASHLIB_OPENSSL_SHAKE_128_METHODDEF \
  1953. + {"openssl_shake_128", (PyCFunction)(void(*)(void))_hashlib_openssl_shake_128, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_shake_128__doc__},
  1954. +
  1955. +static PyObject *
  1956. +_hashlib_openssl_shake_128_impl(PyObject *module, Py_buffer *string,
  1957. + int usedforsecurity);
  1958. +
  1959. +static PyObject *
  1960. +_hashlib_openssl_shake_128(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  1961. +{
  1962. + PyObject *return_value = NULL;
  1963. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  1964. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_shake_128", 0};
  1965. + PyObject *argsbuf[2];
  1966. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  1967. + Py_buffer string = {NULL, NULL};
  1968. + int usedforsecurity = 1;
  1969. +
  1970. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  1971. + if (!args) {
  1972. + goto exit;
  1973. + }
  1974. + if (!noptargs) {
  1975. + goto skip_optional_pos;
  1976. + }
  1977. + if (args[0]) {
  1978. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  1979. + goto exit;
  1980. + }
  1981. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  1982. + _PyArg_BadArgument("openssl_shake_128", "argument 'string'", "contiguous buffer", args[0]);
  1983. + goto exit;
  1984. + }
  1985. + if (!--noptargs) {
  1986. + goto skip_optional_pos;
  1987. + }
  1988. + }
  1989. +skip_optional_pos:
  1990. + if (!noptargs) {
  1991. + goto skip_optional_kwonly;
  1992. + }
  1993. + usedforsecurity = PyObject_IsTrue(args[1]);
  1994. + if (usedforsecurity < 0) {
  1995. + goto exit;
  1996. + }
  1997. +skip_optional_kwonly:
  1998. + return_value = _hashlib_openssl_shake_128_impl(module, &string, usedforsecurity);
  1999. +
  2000. +exit:
  2001. + /* Cleanup for string */
  2002. + if (string.obj) {
  2003. + PyBuffer_Release(&string);
  2004. + }
  2005. +
  2006. + return return_value;
  2007. +}
  2008. +
  2009. +PyDoc_STRVAR(_hashlib_openssl_shake_256__doc__,
  2010. +"openssl_shake_256($module, /, string=None, *, usedforsecurity=True)\n"
  2011. +"--\n"
  2012. +"\n"
  2013. +"Returns a shake-256 variable hash object; optionally initialized with a string.");
  2014. +
  2015. +#define _HASHLIB_OPENSSL_SHAKE_256_METHODDEF \
  2016. + {"openssl_shake_256", (PyCFunction)(void(*)(void))_hashlib_openssl_shake_256, METH_FASTCALL|METH_KEYWORDS, _hashlib_openssl_shake_256__doc__},
  2017. +
  2018. +static PyObject *
  2019. +_hashlib_openssl_shake_256_impl(PyObject *module, Py_buffer *string,
  2020. + int usedforsecurity);
  2021. +
  2022. +static PyObject *
  2023. +_hashlib_openssl_shake_256(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  2024. +{
  2025. + PyObject *return_value = NULL;
  2026. + static const char * const _keywords[] = {"string", "usedforsecurity", NULL};
  2027. + static _PyArg_Parser _parser = {NULL, _keywords, "openssl_shake_256", 0};
  2028. + PyObject *argsbuf[2];
  2029. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
  2030. + Py_buffer string = {NULL, NULL};
  2031. + int usedforsecurity = 1;
  2032. +
  2033. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
  2034. + if (!args) {
  2035. + goto exit;
  2036. + }
  2037. + if (!noptargs) {
  2038. + goto skip_optional_pos;
  2039. + }
  2040. + if (args[0]) {
  2041. + if (PyObject_GetBuffer(args[0], &string, PyBUF_SIMPLE) != 0) {
  2042. + goto exit;
  2043. + }
  2044. + if (!PyBuffer_IsContiguous(&string, 'C')) {
  2045. + _PyArg_BadArgument("openssl_shake_256", "argument 'string'", "contiguous buffer", args[0]);
  2046. + goto exit;
  2047. + }
  2048. + if (!--noptargs) {
  2049. + goto skip_optional_pos;
  2050. + }
  2051. + }
  2052. +skip_optional_pos:
  2053. + if (!noptargs) {
  2054. + goto skip_optional_kwonly;
  2055. + }
  2056. + usedforsecurity = PyObject_IsTrue(args[1]);
  2057. + if (usedforsecurity < 0) {
  2058. + goto exit;
  2059. + }
  2060. +skip_optional_kwonly:
  2061. + return_value = _hashlib_openssl_shake_256_impl(module, &string, usedforsecurity);
  2062. +
  2063. +exit:
  2064. + /* Cleanup for string */
  2065. + if (string.obj) {
  2066. + PyBuffer_Release(&string);
  2067. + }
  2068. +
  2069. + return return_value;
  2070. +}
  2071. +
  2072. +PyDoc_STRVAR(_hashlib_compare_digest__doc__,
  2073. +"compare_digest($module, a, b, /)\n"
  2074. +"--\n"
  2075. +"\n"
  2076. +"Return \'a == b\'.\n"
  2077. +"\n"
  2078. +"This function uses an approach designed to prevent\n"
  2079. +"timing analysis, making it appropriate for cryptography.\n"
  2080. +"\n"
  2081. +"a and b must both be of the same type: either str (ASCII only),\n"
  2082. +"or any bytes-like object.\n"
  2083. +"\n"
  2084. +"Note: If a and b are of different lengths, or if an error occurs,\n"
  2085. +"a timing attack could theoretically reveal information about the\n"
  2086. +"types and lengths of a and b--but not their values.");
  2087. +
  2088. +#define _HASHLIB_COMPARE_DIGEST_METHODDEF \
  2089. + {"compare_digest", (PyCFunction)(void(*)(void))_hashlib_compare_digest, METH_FASTCALL, _hashlib_compare_digest__doc__},
  2090. +
  2091. +static PyObject *
  2092. +_hashlib_compare_digest_impl(PyObject *module, PyObject *a, PyObject *b);
  2093. +
  2094. +static PyObject *
  2095. +_hashlib_compare_digest(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
  2096. +{
  2097. + PyObject *return_value = NULL;
  2098. + PyObject *a;
  2099. + PyObject *b;
  2100. +
  2101. + if (!_PyArg_CheckPositional("compare_digest", nargs, 2, 2)) {
  2102. + goto exit;
  2103. + }
  2104. + a = args[0];
  2105. + b = args[1];
  2106. + return_value = _hashlib_compare_digest_impl(module, a, b);
  2107. +
  2108. +exit:
  2109. + return return_value;
  2110. +}
  2111. +
  2112. +PyDoc_STRVAR(_hashlib_hmac_singleshot__doc__,
  2113. +"hmac_digest($module, /, key, msg, digest)\n"
  2114. +"--\n"
  2115. +"\n"
  2116. +"Single-shot HMAC.");
  2117. +
  2118. +#define _HASHLIB_HMAC_SINGLESHOT_METHODDEF \
  2119. + {"hmac_digest", (PyCFunction)(void(*)(void))_hashlib_hmac_singleshot, METH_FASTCALL|METH_KEYWORDS, _hashlib_hmac_singleshot__doc__},
  2120. +
  2121. +static PyObject *
  2122. +_hashlib_hmac_singleshot_impl(PyObject *module, Py_buffer *key,
  2123. + Py_buffer *msg, const char *digest);
  2124. +
  2125. +static PyObject *
  2126. +_hashlib_hmac_singleshot(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  2127. +{
  2128. + PyObject *return_value = NULL;
  2129. + static const char * const _keywords[] = {"key", "msg", "digest", NULL};
  2130. + static _PyArg_Parser _parser = {NULL, _keywords, "hmac_digest", 0};
  2131. + PyObject *argsbuf[3];
  2132. + Py_buffer key = {NULL, NULL};
  2133. + Py_buffer msg = {NULL, NULL};
  2134. + const char *digest;
  2135. +
  2136. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 3, 3, 0, argsbuf);
  2137. + if (!args) {
  2138. + goto exit;
  2139. + }
  2140. + if (PyObject_GetBuffer(args[0], &key, PyBUF_SIMPLE) != 0) {
  2141. + goto exit;
  2142. + }
  2143. + if (!PyBuffer_IsContiguous(&key, 'C')) {
  2144. + _PyArg_BadArgument("hmac_digest", "argument 'key'", "contiguous buffer", args[0]);
  2145. + goto exit;
  2146. + }
  2147. + if (PyObject_GetBuffer(args[1], &msg, PyBUF_SIMPLE) != 0) {
  2148. + goto exit;
  2149. + }
  2150. + if (!PyBuffer_IsContiguous(&msg, 'C')) {
  2151. + _PyArg_BadArgument("hmac_digest", "argument 'msg'", "contiguous buffer", args[1]);
  2152. + goto exit;
  2153. + }
  2154. + if (!PyUnicode_Check(args[2])) {
  2155. + _PyArg_BadArgument("hmac_digest", "argument 'digest'", "str", args[2]);
  2156. + goto exit;
  2157. + }
  2158. + Py_ssize_t digest_length;
  2159. + digest = PyUnicode_AsUTF8AndSize(args[2], &digest_length);
  2160. + if (digest == NULL) {
  2161. + goto exit;
  2162. + }
  2163. + if (strlen(digest) != (size_t)digest_length) {
  2164. + PyErr_SetString(PyExc_ValueError, "embedded null character");
  2165. + goto exit;
  2166. + }
  2167. + return_value = _hashlib_hmac_singleshot_impl(module, &key, &msg, digest);
  2168. +
  2169. +exit:
  2170. + /* Cleanup for key */
  2171. + if (key.obj) {
  2172. + PyBuffer_Release(&key);
  2173. + }
  2174. + /* Cleanup for msg */
  2175. + if (msg.obj) {
  2176. + PyBuffer_Release(&msg);
  2177. + }
  2178. +
  2179. + return return_value;
  2180. +}
  2181. +
  2182. +PyDoc_STRVAR(_hashlib_hmac_new__doc__,
  2183. +"hmac_new($module, /, key, msg=None, digestmod=None)\n"
  2184. +"--\n"
  2185. +"\n"
  2186. +"Return a new hmac object.");
  2187. +
  2188. +#define _HASHLIB_HMAC_NEW_METHODDEF \
  2189. + {"hmac_new", (PyCFunction)(void(*)(void))_hashlib_hmac_new, METH_FASTCALL|METH_KEYWORDS, _hashlib_hmac_new__doc__},
  2190. +
  2191. +static PyObject *
  2192. +_hashlib_hmac_new_impl(PyObject *module, Py_buffer *key, Py_buffer *msg,
  2193. + const char *digestmod);
  2194. +
  2195. +static PyObject *
  2196. +_hashlib_hmac_new(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
  2197. +{
  2198. + PyObject *return_value = NULL;
  2199. + static const char * const _keywords[] = {"key", "msg", "digestmod", NULL};
  2200. + static _PyArg_Parser _parser = {NULL, _keywords, "hmac_new", 0};
  2201. + PyObject *argsbuf[3];
  2202. + Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
  2203. + Py_buffer key = {NULL, NULL};
  2204. + Py_buffer msg = {NULL, NULL};
  2205. + const char *digestmod = NULL;
  2206. +
  2207. + args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
  2208. + if (!args) {
  2209. + goto exit;
  2210. + }
  2211. + if (PyObject_GetBuffer(args[0], &key, PyBUF_SIMPLE) != 0) {
  2212. + goto exit;
  2213. + }
  2214. + if (!PyBuffer_IsContiguous(&key, 'C')) {
  2215. + _PyArg_BadArgument("hmac_new", "argument 'key'", "contiguous buffer", args[0]);
  2216. + goto exit;
  2217. + }
  2218. + if (!noptargs) {
  2219. + goto skip_optional_pos;
  2220. + }
  2221. + if (args[1]) {
  2222. + if (PyObject_GetBuffer(args[1], &msg, PyBUF_SIMPLE) != 0) {
  2223. + goto exit;
  2224. + }
  2225. + if (!PyBuffer_IsContiguous(&msg, 'C')) {
  2226. + _PyArg_BadArgument("hmac_new", "argument 'msg'", "contiguous buffer", args[1]);
  2227. + goto exit;
  2228. + }
  2229. + if (!--noptargs) {
  2230. + goto skip_optional_pos;
  2231. + }
  2232. + }
  2233. + if (!PyUnicode_Check(args[2])) {
  2234. + _PyArg_BadArgument("hmac_new", "argument 'digestmod'", "str", args[2]);
  2235. + goto exit;
  2236. + }
  2237. + Py_ssize_t digestmod_length;
  2238. + digestmod = PyUnicode_AsUTF8AndSize(args[2], &digestmod_length);
  2239. + if (digestmod == NULL) {
  2240. + goto exit;
  2241. + }
  2242. + if (strlen(digestmod) != (size_t)digestmod_length) {
  2243. + PyErr_SetString(PyExc_ValueError, "embedded null character");
  2244. + goto exit;
  2245. + }
  2246. +skip_optional_pos:
  2247. + return_value = _hashlib_hmac_new_impl(module, &key, &msg, digestmod);
  2248. +
  2249. +exit:
  2250. + /* Cleanup for key */
  2251. + if (key.obj) {
  2252. + PyBuffer_Release(&key);
  2253. + }
  2254. + /* Cleanup for msg */
  2255. + if (msg.obj) {
  2256. + PyBuffer_Release(&msg);
  2257. + }
  2258. +
  2259. + return return_value;
  2260. +}
  2261. +/*[clinic end generated code: output=b4705bad5ece43e9 input=a9049054013a1b77]*/
  2262. --
  2263. 2.32.0