sha3module.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. /* SHA3 module
  2. *
  3. * This module provides an interface to the SHA3 algorithm
  4. *
  5. * See below for information about the original code this module was
  6. * based upon. Additional work performed by:
  7. *
  8. * Andrew Kuchling (amk@amk.ca)
  9. * Greg Stein (gstein@lyra.org)
  10. * Trevor Perrin (trevp@trevp.net)
  11. * Gregory P. Smith (greg@krypto.org)
  12. *
  13. * Copyright (C) 2012-2022 Christian Heimes (christian@python.org)
  14. * Licensed to PSF under a Contributor Agreement.
  15. *
  16. */
  17. #ifndef Py_BUILD_CORE_BUILTIN
  18. # define Py_BUILD_CORE_MODULE 1
  19. #endif
  20. #include "Python.h"
  21. #include "pycore_strhex.h" // _Py_strhex()
  22. #include "pycore_typeobject.h" // _PyType_GetModuleState()
  23. #include "hashlib.h"
  24. #define SHA3_MAX_DIGESTSIZE 64 /* 64 Bytes (512 Bits) for 224 to 512 */
  25. typedef struct {
  26. PyTypeObject *sha3_224_type;
  27. PyTypeObject *sha3_256_type;
  28. PyTypeObject *sha3_384_type;
  29. PyTypeObject *sha3_512_type;
  30. PyTypeObject *shake_128_type;
  31. PyTypeObject *shake_256_type;
  32. } SHA3State;
  33. static inline SHA3State*
  34. sha3_get_state(PyObject *module)
  35. {
  36. void *state = PyModule_GetState(module);
  37. assert(state != NULL);
  38. return (SHA3State *)state;
  39. }
  40. /*[clinic input]
  41. module _sha3
  42. class _sha3.sha3_224 "SHA3object *" "&SHA3_224typ"
  43. class _sha3.sha3_256 "SHA3object *" "&SHA3_256typ"
  44. class _sha3.sha3_384 "SHA3object *" "&SHA3_384typ"
  45. class _sha3.sha3_512 "SHA3object *" "&SHA3_512typ"
  46. class _sha3.shake_128 "SHA3object *" "&SHAKE128type"
  47. class _sha3.shake_256 "SHA3object *" "&SHAKE256type"
  48. [clinic start generated code]*/
  49. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b8a53680f370285a]*/
  50. /* The structure for storing SHA3 info */
  51. #include "_hacl/Hacl_Hash_SHA3.h"
  52. typedef struct {
  53. PyObject_HEAD
  54. // Prevents undefined behavior via multiple threads entering the C API.
  55. // The lock will be NULL before threaded access has been enabled.
  56. PyThread_type_lock lock;
  57. Hacl_Hash_SHA3_state_t *hash_state;
  58. } SHA3object;
  59. #include "clinic/sha3module.c.h"
  60. static SHA3object *
  61. newSHA3object(PyTypeObject *type)
  62. {
  63. SHA3object *newobj;
  64. newobj = (SHA3object *)PyObject_New(SHA3object, type);
  65. if (newobj == NULL) {
  66. return NULL;
  67. }
  68. newobj->lock = NULL;
  69. return newobj;
  70. }
  71. static void sha3_update(Hacl_Hash_SHA3_state_t *state, uint8_t *buf, Py_ssize_t len) {
  72. /* Note: we explicitly ignore the error code on the basis that it would take >
  73. * 1 billion years to hash more than 2^64 bytes. */
  74. #if PY_SSIZE_T_MAX > UINT32_MAX
  75. while (len > UINT32_MAX) {
  76. Hacl_Hash_SHA3_update(state, buf, UINT32_MAX);
  77. len -= UINT32_MAX;
  78. buf += UINT32_MAX;
  79. }
  80. #endif
  81. /* Cast to uint32_t is safe: len <= UINT32_MAX at this point. */
  82. Hacl_Hash_SHA3_update(state, buf, (uint32_t) len);
  83. }
  84. /*[clinic input]
  85. @classmethod
  86. _sha3.sha3_224.__new__ as py_sha3_new
  87. data: object(c_default="NULL") = b''
  88. /
  89. *
  90. usedforsecurity: bool = True
  91. Return a new SHA3 hash object.
  92. [clinic start generated code]*/
  93. static PyObject *
  94. py_sha3_new_impl(PyTypeObject *type, PyObject *data, int usedforsecurity)
  95. /*[clinic end generated code: output=90409addc5d5e8b0 input=637e5f8f6a93982a]*/
  96. {
  97. Py_buffer buf = {NULL, NULL};
  98. SHA3State *state = _PyType_GetModuleState(type);
  99. SHA3object *self = newSHA3object(type);
  100. if (self == NULL) {
  101. goto error;
  102. }
  103. assert(state != NULL);
  104. if (type == state->sha3_224_type) {
  105. self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_224);
  106. } else if (type == state->sha3_256_type) {
  107. self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_256);
  108. } else if (type == state->sha3_384_type) {
  109. self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_384);
  110. } else if (type == state->sha3_512_type) {
  111. self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_SHA3_512);
  112. } else if (type == state->shake_128_type) {
  113. self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake128);
  114. } else if (type == state->shake_256_type) {
  115. self->hash_state = Hacl_Hash_SHA3_malloc(Spec_Hash_Definitions_Shake256);
  116. } else {
  117. PyErr_BadInternalCall();
  118. goto error;
  119. }
  120. if (data) {
  121. GET_BUFFER_VIEW_OR_ERROR(data, &buf, goto error);
  122. if (buf.len >= HASHLIB_GIL_MINSIZE) {
  123. /* We do not initialize self->lock here as this is the constructor
  124. * where it is not yet possible to have concurrent access. */
  125. Py_BEGIN_ALLOW_THREADS
  126. sha3_update(self->hash_state, buf.buf, buf.len);
  127. Py_END_ALLOW_THREADS
  128. } else {
  129. sha3_update(self->hash_state, buf.buf, buf.len);
  130. }
  131. }
  132. PyBuffer_Release(&buf);
  133. return (PyObject *)self;
  134. error:
  135. if (self) {
  136. Py_DECREF(self);
  137. }
  138. if (data && buf.obj) {
  139. PyBuffer_Release(&buf);
  140. }
  141. return NULL;
  142. }
  143. /* Internal methods for a hash object */
  144. static void
  145. SHA3_dealloc(SHA3object *self)
  146. {
  147. Hacl_Hash_SHA3_free(self->hash_state);
  148. if (self->lock != NULL) {
  149. PyThread_free_lock(self->lock);
  150. }
  151. PyTypeObject *tp = Py_TYPE(self);
  152. PyObject_Free(self);
  153. Py_DECREF(tp);
  154. }
  155. /* External methods for a hash object */
  156. /*[clinic input]
  157. _sha3.sha3_224.copy
  158. Return a copy of the hash object.
  159. [clinic start generated code]*/
  160. static PyObject *
  161. _sha3_sha3_224_copy_impl(SHA3object *self)
  162. /*[clinic end generated code: output=6c537411ecdcda4c input=93a44aaebea51ba8]*/
  163. {
  164. SHA3object *newobj;
  165. if ((newobj = newSHA3object(Py_TYPE(self))) == NULL) {
  166. return NULL;
  167. }
  168. ENTER_HASHLIB(self);
  169. newobj->hash_state = Hacl_Hash_SHA3_copy(self->hash_state);
  170. LEAVE_HASHLIB(self);
  171. return (PyObject *)newobj;
  172. }
  173. /*[clinic input]
  174. _sha3.sha3_224.digest
  175. Return the digest value as a bytes object.
  176. [clinic start generated code]*/
  177. static PyObject *
  178. _sha3_sha3_224_digest_impl(SHA3object *self)
  179. /*[clinic end generated code: output=fd531842e20b2d5b input=5b2a659536bbd248]*/
  180. {
  181. unsigned char digest[SHA3_MAX_DIGESTSIZE];
  182. // This function errors out if the algorithm is Shake. Here, we know this
  183. // not to be the case, and therefore do not perform error checking.
  184. ENTER_HASHLIB(self);
  185. Hacl_Hash_SHA3_digest(self->hash_state, digest);
  186. LEAVE_HASHLIB(self);
  187. return PyBytes_FromStringAndSize((const char *)digest,
  188. Hacl_Hash_SHA3_hash_len(self->hash_state));
  189. }
  190. /*[clinic input]
  191. _sha3.sha3_224.hexdigest
  192. Return the digest value as a string of hexadecimal digits.
  193. [clinic start generated code]*/
  194. static PyObject *
  195. _sha3_sha3_224_hexdigest_impl(SHA3object *self)
  196. /*[clinic end generated code: output=75ad03257906918d input=2d91bb6e0d114ee3]*/
  197. {
  198. unsigned char digest[SHA3_MAX_DIGESTSIZE];
  199. ENTER_HASHLIB(self);
  200. Hacl_Hash_SHA3_digest(self->hash_state, digest);
  201. LEAVE_HASHLIB(self);
  202. return _Py_strhex((const char *)digest,
  203. Hacl_Hash_SHA3_hash_len(self->hash_state));
  204. }
  205. /*[clinic input]
  206. _sha3.sha3_224.update
  207. data: object
  208. /
  209. Update this hash object's state with the provided bytes-like object.
  210. [clinic start generated code]*/
  211. static PyObject *
  212. _sha3_sha3_224_update(SHA3object *self, PyObject *data)
  213. /*[clinic end generated code: output=d3223352286ed357 input=a887f54dcc4ae227]*/
  214. {
  215. Py_buffer buf;
  216. GET_BUFFER_VIEW_OR_ERROUT(data, &buf);
  217. if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
  218. self->lock = PyThread_allocate_lock();
  219. }
  220. if (self->lock != NULL) {
  221. Py_BEGIN_ALLOW_THREADS
  222. PyThread_acquire_lock(self->lock, 1);
  223. sha3_update(self->hash_state, buf.buf, buf.len);
  224. PyThread_release_lock(self->lock);
  225. Py_END_ALLOW_THREADS
  226. } else {
  227. sha3_update(self->hash_state, buf.buf, buf.len);
  228. }
  229. PyBuffer_Release(&buf);
  230. Py_RETURN_NONE;
  231. }
  232. static PyMethodDef SHA3_methods[] = {
  233. _SHA3_SHA3_224_COPY_METHODDEF
  234. _SHA3_SHA3_224_DIGEST_METHODDEF
  235. _SHA3_SHA3_224_HEXDIGEST_METHODDEF
  236. _SHA3_SHA3_224_UPDATE_METHODDEF
  237. {NULL, NULL} /* sentinel */
  238. };
  239. static PyObject *
  240. SHA3_get_block_size(SHA3object *self, void *closure)
  241. {
  242. uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state);
  243. return PyLong_FromLong(rate);
  244. }
  245. static PyObject *
  246. SHA3_get_name(SHA3object *self, void *closure)
  247. {
  248. PyTypeObject *type = Py_TYPE(self);
  249. SHA3State *state = _PyType_GetModuleState(type);
  250. assert(state != NULL);
  251. if (type == state->sha3_224_type) {
  252. return PyUnicode_FromString("sha3_224");
  253. } else if (type == state->sha3_256_type) {
  254. return PyUnicode_FromString("sha3_256");
  255. } else if (type == state->sha3_384_type) {
  256. return PyUnicode_FromString("sha3_384");
  257. } else if (type == state->sha3_512_type) {
  258. return PyUnicode_FromString("sha3_512");
  259. } else if (type == state->shake_128_type) {
  260. return PyUnicode_FromString("shake_128");
  261. } else if (type == state->shake_256_type) {
  262. return PyUnicode_FromString("shake_256");
  263. } else {
  264. PyErr_BadInternalCall();
  265. return NULL;
  266. }
  267. }
  268. static PyObject *
  269. SHA3_get_digest_size(SHA3object *self, void *closure)
  270. {
  271. // Preserving previous behavior: variable-length algorithms return 0
  272. if (Hacl_Hash_SHA3_is_shake(self->hash_state))
  273. return PyLong_FromLong(0);
  274. else
  275. return PyLong_FromLong(Hacl_Hash_SHA3_hash_len(self->hash_state));
  276. }
  277. static PyObject *
  278. SHA3_get_capacity_bits(SHA3object *self, void *closure)
  279. {
  280. uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state) * 8;
  281. int capacity = 1600 - rate;
  282. return PyLong_FromLong(capacity);
  283. }
  284. static PyObject *
  285. SHA3_get_rate_bits(SHA3object *self, void *closure)
  286. {
  287. uint32_t rate = Hacl_Hash_SHA3_block_len(self->hash_state) * 8;
  288. return PyLong_FromLong(rate);
  289. }
  290. static PyObject *
  291. SHA3_get_suffix(SHA3object *self, void *closure)
  292. {
  293. unsigned char suffix[2] = {0x06, 0};
  294. return PyBytes_FromStringAndSize((const char *)suffix, 1);
  295. }
  296. static PyGetSetDef SHA3_getseters[] = {
  297. {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
  298. {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
  299. {"digest_size", (getter)SHA3_get_digest_size, NULL, NULL, NULL},
  300. {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL},
  301. {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL},
  302. {"_suffix", (getter)SHA3_get_suffix, NULL, NULL, NULL},
  303. {NULL} /* Sentinel */
  304. };
  305. #define SHA3_TYPE_SLOTS(type_slots_obj, type_doc, type_methods, type_getseters) \
  306. static PyType_Slot type_slots_obj[] = { \
  307. {Py_tp_dealloc, SHA3_dealloc}, \
  308. {Py_tp_doc, (char*)type_doc}, \
  309. {Py_tp_methods, type_methods}, \
  310. {Py_tp_getset, type_getseters}, \
  311. {Py_tp_new, py_sha3_new}, \
  312. {0,0} \
  313. }
  314. // Using _PyType_GetModuleState() on these types is safe since they
  315. // cannot be subclassed: it does not have the Py_TPFLAGS_BASETYPE flag.
  316. #define SHA3_TYPE_SPEC(type_spec_obj, type_name, type_slots) \
  317. static PyType_Spec type_spec_obj = { \
  318. .name = "_sha3." type_name, \
  319. .basicsize = sizeof(SHA3object), \
  320. .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE, \
  321. .slots = type_slots \
  322. }
  323. PyDoc_STRVAR(sha3_224__doc__,
  324. "sha3_224([data], *, usedforsecurity=True) -> SHA3 object\n\
  325. \n\
  326. Return a new SHA3 hash object with a hashbit length of 28 bytes.");
  327. PyDoc_STRVAR(sha3_256__doc__,
  328. "sha3_256([data], *, usedforsecurity=True) -> SHA3 object\n\
  329. \n\
  330. Return a new SHA3 hash object with a hashbit length of 32 bytes.");
  331. PyDoc_STRVAR(sha3_384__doc__,
  332. "sha3_384([data], *, usedforsecurity=True) -> SHA3 object\n\
  333. \n\
  334. Return a new SHA3 hash object with a hashbit length of 48 bytes.");
  335. PyDoc_STRVAR(sha3_512__doc__,
  336. "sha3_512([data], *, usedforsecurity=True) -> SHA3 object\n\
  337. \n\
  338. Return a new SHA3 hash object with a hashbit length of 64 bytes.");
  339. SHA3_TYPE_SLOTS(sha3_224_slots, sha3_224__doc__, SHA3_methods, SHA3_getseters);
  340. SHA3_TYPE_SPEC(sha3_224_spec, "sha3_224", sha3_224_slots);
  341. SHA3_TYPE_SLOTS(sha3_256_slots, sha3_256__doc__, SHA3_methods, SHA3_getseters);
  342. SHA3_TYPE_SPEC(sha3_256_spec, "sha3_256", sha3_256_slots);
  343. SHA3_TYPE_SLOTS(sha3_384_slots, sha3_384__doc__, SHA3_methods, SHA3_getseters);
  344. SHA3_TYPE_SPEC(sha3_384_spec, "sha3_384", sha3_384_slots);
  345. SHA3_TYPE_SLOTS(sha3_512_slots, sha3_512__doc__, SHA3_methods, SHA3_getseters);
  346. SHA3_TYPE_SPEC(sha3_512_spec, "sha3_512", sha3_512_slots);
  347. static PyObject *
  348. _SHAKE_digest(SHA3object *self, unsigned long digestlen, int hex)
  349. {
  350. unsigned char *digest = NULL;
  351. PyObject *result = NULL;
  352. if (digestlen >= (1 << 29)) {
  353. PyErr_SetString(PyExc_ValueError, "length is too large");
  354. return NULL;
  355. }
  356. digest = (unsigned char*)PyMem_Malloc(digestlen);
  357. if (digest == NULL) {
  358. return PyErr_NoMemory();
  359. }
  360. /* Get the raw (binary) digest value. The HACL functions errors out if:
  361. * - the algorith is not shake -- not the case here
  362. * - the output length is zero -- we follow the existing behavior and return
  363. * an empty digest, without raising an error */
  364. if (digestlen > 0) {
  365. Hacl_Hash_SHA3_squeeze(self->hash_state, digest, digestlen);
  366. }
  367. if (hex) {
  368. result = _Py_strhex((const char *)digest, digestlen);
  369. } else {
  370. result = PyBytes_FromStringAndSize((const char *)digest,
  371. digestlen);
  372. }
  373. if (digest != NULL) {
  374. PyMem_Free(digest);
  375. }
  376. return result;
  377. }
  378. /*[clinic input]
  379. _sha3.shake_128.digest
  380. length: unsigned_long
  381. /
  382. Return the digest value as a bytes object.
  383. [clinic start generated code]*/
  384. static PyObject *
  385. _sha3_shake_128_digest_impl(SHA3object *self, unsigned long length)
  386. /*[clinic end generated code: output=2313605e2f87bb8f input=418ef6a36d2e6082]*/
  387. {
  388. return _SHAKE_digest(self, length, 0);
  389. }
  390. /*[clinic input]
  391. _sha3.shake_128.hexdigest
  392. length: unsigned_long
  393. /
  394. Return the digest value as a string of hexadecimal digits.
  395. [clinic start generated code]*/
  396. static PyObject *
  397. _sha3_shake_128_hexdigest_impl(SHA3object *self, unsigned long length)
  398. /*[clinic end generated code: output=bf8e2f1e490944a8 input=69fb29b0926ae321]*/
  399. {
  400. return _SHAKE_digest(self, length, 1);
  401. }
  402. static PyObject *
  403. SHAKE_get_digest_size(SHA3object *self, void *closure)
  404. {
  405. return PyLong_FromLong(0);
  406. }
  407. static PyObject *
  408. SHAKE_get_suffix(SHA3object *self, void *closure)
  409. {
  410. unsigned char suffix[2] = {0x1f, 0};
  411. return PyBytes_FromStringAndSize((const char *)suffix, 1);
  412. }
  413. static PyGetSetDef SHAKE_getseters[] = {
  414. {"block_size", (getter)SHA3_get_block_size, NULL, NULL, NULL},
  415. {"name", (getter)SHA3_get_name, NULL, NULL, NULL},
  416. {"digest_size", (getter)SHAKE_get_digest_size, NULL, NULL, NULL},
  417. {"_capacity_bits", (getter)SHA3_get_capacity_bits, NULL, NULL, NULL},
  418. {"_rate_bits", (getter)SHA3_get_rate_bits, NULL, NULL, NULL},
  419. {"_suffix", (getter)SHAKE_get_suffix, NULL, NULL, NULL},
  420. {NULL} /* Sentinel */
  421. };
  422. static PyMethodDef SHAKE_methods[] = {
  423. _SHA3_SHA3_224_COPY_METHODDEF
  424. _SHA3_SHAKE_128_DIGEST_METHODDEF
  425. _SHA3_SHAKE_128_HEXDIGEST_METHODDEF
  426. _SHA3_SHA3_224_UPDATE_METHODDEF
  427. {NULL, NULL} /* sentinel */
  428. };
  429. PyDoc_STRVAR(shake_128__doc__,
  430. "shake_128([data], *, usedforsecurity=True) -> SHAKE object\n\
  431. \n\
  432. Return a new SHAKE hash object.");
  433. PyDoc_STRVAR(shake_256__doc__,
  434. "shake_256([data], *, usedforsecurity=True) -> SHAKE object\n\
  435. \n\
  436. Return a new SHAKE hash object.");
  437. SHA3_TYPE_SLOTS(SHAKE128slots, shake_128__doc__, SHAKE_methods, SHAKE_getseters);
  438. SHA3_TYPE_SPEC(SHAKE128_spec, "shake_128", SHAKE128slots);
  439. SHA3_TYPE_SLOTS(SHAKE256slots, shake_256__doc__, SHAKE_methods, SHAKE_getseters);
  440. SHA3_TYPE_SPEC(SHAKE256_spec, "shake_256", SHAKE256slots);
  441. static int
  442. _sha3_traverse(PyObject *module, visitproc visit, void *arg)
  443. {
  444. SHA3State *state = sha3_get_state(module);
  445. Py_VISIT(state->sha3_224_type);
  446. Py_VISIT(state->sha3_256_type);
  447. Py_VISIT(state->sha3_384_type);
  448. Py_VISIT(state->sha3_512_type);
  449. Py_VISIT(state->shake_128_type);
  450. Py_VISIT(state->shake_256_type);
  451. return 0;
  452. }
  453. static int
  454. _sha3_clear(PyObject *module)
  455. {
  456. SHA3State *state = sha3_get_state(module);
  457. Py_CLEAR(state->sha3_224_type);
  458. Py_CLEAR(state->sha3_256_type);
  459. Py_CLEAR(state->sha3_384_type);
  460. Py_CLEAR(state->sha3_512_type);
  461. Py_CLEAR(state->shake_128_type);
  462. Py_CLEAR(state->shake_256_type);
  463. return 0;
  464. }
  465. static void
  466. _sha3_free(void *module)
  467. {
  468. _sha3_clear((PyObject *)module);
  469. }
  470. static int
  471. _sha3_exec(PyObject *m)
  472. {
  473. SHA3State *st = sha3_get_state(m);
  474. #define init_sha3type(type, typespec) \
  475. do { \
  476. st->type = (PyTypeObject *)PyType_FromModuleAndSpec( \
  477. m, &typespec, NULL); \
  478. if (st->type == NULL) { \
  479. return -1; \
  480. } \
  481. if (PyModule_AddType(m, st->type) < 0) { \
  482. return -1; \
  483. } \
  484. } while(0)
  485. init_sha3type(sha3_224_type, sha3_224_spec);
  486. init_sha3type(sha3_256_type, sha3_256_spec);
  487. init_sha3type(sha3_384_type, sha3_384_spec);
  488. init_sha3type(sha3_512_type, sha3_512_spec);
  489. init_sha3type(shake_128_type, SHAKE128_spec);
  490. init_sha3type(shake_256_type, SHAKE256_spec);
  491. #undef init_sha3type
  492. if (PyModule_AddStringConstant(m, "implementation",
  493. "HACL") < 0) {
  494. return -1;
  495. }
  496. return 0;
  497. }
  498. static PyModuleDef_Slot _sha3_slots[] = {
  499. {Py_mod_exec, _sha3_exec},
  500. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  501. {0, NULL}
  502. };
  503. /* Initialize this module. */
  504. static struct PyModuleDef _sha3module = {
  505. PyModuleDef_HEAD_INIT,
  506. .m_name = "_sha3",
  507. .m_size = sizeof(SHA3State),
  508. .m_slots = _sha3_slots,
  509. .m_traverse = _sha3_traverse,
  510. .m_clear = _sha3_clear,
  511. .m_free = _sha3_free,
  512. };
  513. PyMODINIT_FUNC
  514. PyInit__sha3(void)
  515. {
  516. return PyModuleDef_Init(&_sha3module);
  517. }