sha2module.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /* SHA2 module */
  2. /* This provides an interface to NIST's SHA2 224, 256, 384, & 512 Algorithms */
  3. /* See below for information about the original code this module was
  4. based upon. Additional work performed by:
  5. Andrew Kuchling (amk@amk.ca)
  6. Greg Stein (gstein@lyra.org)
  7. Trevor Perrin (trevp@trevp.net)
  8. Jonathan Protzenko (jonathan@protzenko.fr)
  9. Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
  10. Licensed to PSF under a Contributor Agreement.
  11. */
  12. /* SHA objects */
  13. #ifndef Py_BUILD_CORE_BUILTIN
  14. # define Py_BUILD_CORE_MODULE 1
  15. #endif
  16. #include "Python.h"
  17. #include "pycore_bitutils.h" // _Py_bswap32()
  18. #include "pycore_moduleobject.h" // _PyModule_GetState()
  19. #include "pycore_typeobject.h" // _PyType_GetModuleState()
  20. #include "pycore_strhex.h" // _Py_strhex()
  21. #include "structmember.h" // PyMemberDef
  22. #include "hashlib.h"
  23. /*[clinic input]
  24. module _sha2
  25. class SHA256Type "SHA256object *" "&PyType_Type"
  26. class SHA512Type "SHA512object *" "&PyType_Type"
  27. [clinic start generated code]*/
  28. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5315a7b611c9afc]*/
  29. /* The SHA block sizes and maximum message digest sizes, in bytes */
  30. #define SHA256_BLOCKSIZE 64
  31. #define SHA256_DIGESTSIZE 32
  32. #define SHA512_BLOCKSIZE 128
  33. #define SHA512_DIGESTSIZE 64
  34. /* Our SHA2 implementations defer to the HACL* verified library. */
  35. #include "_hacl/Hacl_Hash_SHA2.h"
  36. // TODO: Get rid of int digestsize in favor of Hacl state info?
  37. typedef struct {
  38. PyObject_HEAD
  39. int digestsize;
  40. // Prevents undefined behavior via multiple threads entering the C API.
  41. // The lock will be NULL before threaded access has been enabled.
  42. PyThread_type_lock lock;
  43. Hacl_Hash_SHA2_state_t_256 *state;
  44. } SHA256object;
  45. typedef struct {
  46. PyObject_HEAD
  47. int digestsize;
  48. // Prevents undefined behavior via multiple threads entering the C API.
  49. // The lock will be NULL before threaded access has been enabled.
  50. PyThread_type_lock lock;
  51. Hacl_Hash_SHA2_state_t_512 *state;
  52. } SHA512object;
  53. #include "clinic/sha2module.c.h"
  54. /* We shall use run-time type information in the remainder of this module to
  55. * tell apart SHA2-224 and SHA2-256 */
  56. typedef struct {
  57. PyTypeObject* sha224_type;
  58. PyTypeObject* sha256_type;
  59. PyTypeObject* sha384_type;
  60. PyTypeObject* sha512_type;
  61. } sha2_state;
  62. static inline sha2_state*
  63. sha2_get_state(PyObject *module)
  64. {
  65. void *state = _PyModule_GetState(module);
  66. assert(state != NULL);
  67. return (sha2_state *)state;
  68. }
  69. static void SHA256copy(SHA256object *src, SHA256object *dest)
  70. {
  71. dest->digestsize = src->digestsize;
  72. dest->state = Hacl_Hash_SHA2_copy_256(src->state);
  73. }
  74. static void SHA512copy(SHA512object *src, SHA512object *dest)
  75. {
  76. dest->digestsize = src->digestsize;
  77. dest->state = Hacl_Hash_SHA2_copy_512(src->state);
  78. }
  79. static SHA256object *
  80. newSHA224object(sha2_state *state)
  81. {
  82. SHA256object *sha = (SHA256object *)PyObject_GC_New(
  83. SHA256object, state->sha224_type);
  84. if (!sha) {
  85. return NULL;
  86. }
  87. sha->lock = NULL;
  88. PyObject_GC_Track(sha);
  89. return sha;
  90. }
  91. static SHA256object *
  92. newSHA256object(sha2_state *state)
  93. {
  94. SHA256object *sha = (SHA256object *)PyObject_GC_New(
  95. SHA256object, state->sha256_type);
  96. if (!sha) {
  97. return NULL;
  98. }
  99. sha->lock = NULL;
  100. PyObject_GC_Track(sha);
  101. return sha;
  102. }
  103. static SHA512object *
  104. newSHA384object(sha2_state *state)
  105. {
  106. SHA512object *sha = (SHA512object *)PyObject_GC_New(
  107. SHA512object, state->sha384_type);
  108. if (!sha) {
  109. return NULL;
  110. }
  111. sha->lock = NULL;
  112. PyObject_GC_Track(sha);
  113. return sha;
  114. }
  115. static SHA512object *
  116. newSHA512object(sha2_state *state)
  117. {
  118. SHA512object *sha = (SHA512object *)PyObject_GC_New(
  119. SHA512object, state->sha512_type);
  120. if (!sha) {
  121. return NULL;
  122. }
  123. sha->lock = NULL;
  124. PyObject_GC_Track(sha);
  125. return sha;
  126. }
  127. /* Internal methods for our hash objects. */
  128. static int
  129. SHA2_traverse(PyObject *ptr, visitproc visit, void *arg)
  130. {
  131. Py_VISIT(Py_TYPE(ptr));
  132. return 0;
  133. }
  134. static void
  135. SHA256_dealloc(SHA256object *ptr)
  136. {
  137. Hacl_Hash_SHA2_free_256(ptr->state);
  138. if (ptr->lock != NULL) {
  139. PyThread_free_lock(ptr->lock);
  140. }
  141. PyTypeObject *tp = Py_TYPE(ptr);
  142. PyObject_GC_UnTrack(ptr);
  143. PyObject_GC_Del(ptr);
  144. Py_DECREF(tp);
  145. }
  146. static void
  147. SHA512_dealloc(SHA512object *ptr)
  148. {
  149. Hacl_Hash_SHA2_free_512(ptr->state);
  150. if (ptr->lock != NULL) {
  151. PyThread_free_lock(ptr->lock);
  152. }
  153. PyTypeObject *tp = Py_TYPE(ptr);
  154. PyObject_GC_UnTrack(ptr);
  155. PyObject_GC_Del(ptr);
  156. Py_DECREF(tp);
  157. }
  158. /* HACL* takes a uint32_t for the length of its parameter, but Py_ssize_t can be
  159. * 64 bits so we loop in <4gig chunks when needed. */
  160. static void update_256(Hacl_Hash_SHA2_state_t_256 *state, uint8_t *buf, Py_ssize_t len) {
  161. /* Note: we explicitly ignore the error code on the basis that it would take >
  162. * 1 billion years to overflow the maximum admissible length for SHA2-256
  163. * (namely, 2^61-1 bytes). */
  164. #if PY_SSIZE_T_MAX > UINT32_MAX
  165. while (len > UINT32_MAX) {
  166. Hacl_Hash_SHA2_update_256(state, buf, UINT32_MAX);
  167. len -= UINT32_MAX;
  168. buf += UINT32_MAX;
  169. }
  170. #endif
  171. /* Cast to uint32_t is safe: len <= UINT32_MAX at this point. */
  172. Hacl_Hash_SHA2_update_256(state, buf, (uint32_t) len);
  173. }
  174. static void update_512(Hacl_Hash_SHA2_state_t_512 *state, uint8_t *buf, Py_ssize_t len) {
  175. /* Note: we explicitly ignore the error code on the basis that it would take >
  176. * 1 billion years to overflow the maximum admissible length for this API
  177. * (namely, 2^64-1 bytes). */
  178. #if PY_SSIZE_T_MAX > UINT32_MAX
  179. while (len > UINT32_MAX) {
  180. Hacl_Hash_SHA2_update_512(state, buf, UINT32_MAX);
  181. len -= UINT32_MAX;
  182. buf += UINT32_MAX;
  183. }
  184. #endif
  185. /* Cast to uint32_t is safe: len <= UINT32_MAX at this point. */
  186. Hacl_Hash_SHA2_update_512(state, buf, (uint32_t) len);
  187. }
  188. /* External methods for our hash objects */
  189. /*[clinic input]
  190. SHA256Type.copy
  191. cls:defining_class
  192. Return a copy of the hash object.
  193. [clinic start generated code]*/
  194. static PyObject *
  195. SHA256Type_copy_impl(SHA256object *self, PyTypeObject *cls)
  196. /*[clinic end generated code: output=fabd515577805cd3 input=3137146fcb88e212]*/
  197. {
  198. SHA256object *newobj;
  199. sha2_state *state = _PyType_GetModuleState(cls);
  200. if (Py_IS_TYPE(self, state->sha256_type)) {
  201. if ((newobj = newSHA256object(state)) == NULL) {
  202. return NULL;
  203. }
  204. } else {
  205. if ((newobj = newSHA224object(state)) == NULL) {
  206. return NULL;
  207. }
  208. }
  209. ENTER_HASHLIB(self);
  210. SHA256copy(self, newobj);
  211. LEAVE_HASHLIB(self);
  212. return (PyObject *)newobj;
  213. }
  214. /*[clinic input]
  215. SHA512Type.copy
  216. cls: defining_class
  217. Return a copy of the hash object.
  218. [clinic start generated code]*/
  219. static PyObject *
  220. SHA512Type_copy_impl(SHA512object *self, PyTypeObject *cls)
  221. /*[clinic end generated code: output=66d2a8ef20de8302 input=f673a18f66527c90]*/
  222. {
  223. SHA512object *newobj;
  224. sha2_state *state = _PyType_GetModuleState(cls);
  225. if (Py_IS_TYPE((PyObject*)self, state->sha512_type)) {
  226. if ((newobj = newSHA512object(state)) == NULL) {
  227. return NULL;
  228. }
  229. }
  230. else {
  231. if ((newobj = newSHA384object(state)) == NULL) {
  232. return NULL;
  233. }
  234. }
  235. ENTER_HASHLIB(self);
  236. SHA512copy(self, newobj);
  237. LEAVE_HASHLIB(self);
  238. return (PyObject *)newobj;
  239. }
  240. /*[clinic input]
  241. SHA256Type.digest
  242. Return the digest value as a bytes object.
  243. [clinic start generated code]*/
  244. static PyObject *
  245. SHA256Type_digest_impl(SHA256object *self)
  246. /*[clinic end generated code: output=3a2e3997a98ee792 input=f1f4cfea5cbde35c]*/
  247. {
  248. uint8_t digest[SHA256_DIGESTSIZE];
  249. assert(self->digestsize <= SHA256_DIGESTSIZE);
  250. ENTER_HASHLIB(self);
  251. // HACL* performs copies under the hood so that self->state remains valid
  252. // after this call.
  253. Hacl_Hash_SHA2_digest_256(self->state, digest);
  254. LEAVE_HASHLIB(self);
  255. return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
  256. }
  257. /*[clinic input]
  258. SHA512Type.digest
  259. Return the digest value as a bytes object.
  260. [clinic start generated code]*/
  261. static PyObject *
  262. SHA512Type_digest_impl(SHA512object *self)
  263. /*[clinic end generated code: output=dd8c6320070458e0 input=f6470dd359071f4b]*/
  264. {
  265. uint8_t digest[SHA512_DIGESTSIZE];
  266. assert(self->digestsize <= SHA512_DIGESTSIZE);
  267. ENTER_HASHLIB(self);
  268. // HACL* performs copies under the hood so that self->state remains valid
  269. // after this call.
  270. Hacl_Hash_SHA2_digest_512(self->state, digest);
  271. LEAVE_HASHLIB(self);
  272. return PyBytes_FromStringAndSize((const char *)digest, self->digestsize);
  273. }
  274. /*[clinic input]
  275. SHA256Type.hexdigest
  276. Return the digest value as a string of hexadecimal digits.
  277. [clinic start generated code]*/
  278. static PyObject *
  279. SHA256Type_hexdigest_impl(SHA256object *self)
  280. /*[clinic end generated code: output=96cb68996a780ab3 input=0cc4c714693010d1]*/
  281. {
  282. uint8_t digest[SHA256_DIGESTSIZE];
  283. assert(self->digestsize <= SHA256_DIGESTSIZE);
  284. ENTER_HASHLIB(self);
  285. Hacl_Hash_SHA2_digest_256(self->state, digest);
  286. LEAVE_HASHLIB(self);
  287. return _Py_strhex((const char *)digest, self->digestsize);
  288. }
  289. /*[clinic input]
  290. SHA512Type.hexdigest
  291. Return the digest value as a string of hexadecimal digits.
  292. [clinic start generated code]*/
  293. static PyObject *
  294. SHA512Type_hexdigest_impl(SHA512object *self)
  295. /*[clinic end generated code: output=cbd6f844aba1fe7c input=498b877b25cbe0a2]*/
  296. {
  297. uint8_t digest[SHA512_DIGESTSIZE];
  298. assert(self->digestsize <= SHA512_DIGESTSIZE);
  299. ENTER_HASHLIB(self);
  300. Hacl_Hash_SHA2_digest_512(self->state, digest);
  301. LEAVE_HASHLIB(self);
  302. return _Py_strhex((const char *)digest, self->digestsize);
  303. }
  304. /*[clinic input]
  305. SHA256Type.update
  306. obj: object
  307. /
  308. Update this hash object's state with the provided string.
  309. [clinic start generated code]*/
  310. static PyObject *
  311. SHA256Type_update(SHA256object *self, PyObject *obj)
  312. /*[clinic end generated code: output=1b240f965ddbd8c6 input=b2d449d5b30f0f5a]*/
  313. {
  314. Py_buffer buf;
  315. GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
  316. if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
  317. self->lock = PyThread_allocate_lock();
  318. }
  319. if (self->lock != NULL) {
  320. Py_BEGIN_ALLOW_THREADS
  321. PyThread_acquire_lock(self->lock, 1);
  322. update_256(self->state, buf.buf, buf.len);
  323. PyThread_release_lock(self->lock);
  324. Py_END_ALLOW_THREADS
  325. } else {
  326. update_256(self->state, buf.buf, buf.len);
  327. }
  328. PyBuffer_Release(&buf);
  329. Py_RETURN_NONE;
  330. }
  331. /*[clinic input]
  332. SHA512Type.update
  333. obj: object
  334. /
  335. Update this hash object's state with the provided string.
  336. [clinic start generated code]*/
  337. static PyObject *
  338. SHA512Type_update(SHA512object *self, PyObject *obj)
  339. /*[clinic end generated code: output=745f51057a985884 input=ded2b46656566283]*/
  340. {
  341. Py_buffer buf;
  342. GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
  343. if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
  344. self->lock = PyThread_allocate_lock();
  345. }
  346. if (self->lock != NULL) {
  347. Py_BEGIN_ALLOW_THREADS
  348. PyThread_acquire_lock(self->lock, 1);
  349. update_512(self->state, buf.buf, buf.len);
  350. PyThread_release_lock(self->lock);
  351. Py_END_ALLOW_THREADS
  352. } else {
  353. update_512(self->state, buf.buf, buf.len);
  354. }
  355. PyBuffer_Release(&buf);
  356. Py_RETURN_NONE;
  357. }
  358. static PyMethodDef SHA256_methods[] = {
  359. SHA256TYPE_COPY_METHODDEF
  360. SHA256TYPE_DIGEST_METHODDEF
  361. SHA256TYPE_HEXDIGEST_METHODDEF
  362. SHA256TYPE_UPDATE_METHODDEF
  363. {NULL, NULL} /* sentinel */
  364. };
  365. static PyMethodDef SHA512_methods[] = {
  366. SHA512TYPE_COPY_METHODDEF
  367. SHA512TYPE_DIGEST_METHODDEF
  368. SHA512TYPE_HEXDIGEST_METHODDEF
  369. SHA512TYPE_UPDATE_METHODDEF
  370. {NULL, NULL} /* sentinel */
  371. };
  372. static PyObject *
  373. SHA256_get_block_size(PyObject *self, void *closure)
  374. {
  375. return PyLong_FromLong(SHA256_BLOCKSIZE);
  376. }
  377. static PyObject *
  378. SHA512_get_block_size(PyObject *self, void *closure)
  379. {
  380. return PyLong_FromLong(SHA512_BLOCKSIZE);
  381. }
  382. static PyObject *
  383. SHA256_get_digest_size(SHA256object *self, void *closure)
  384. {
  385. return PyLong_FromLong(self->digestsize);
  386. }
  387. static PyObject *
  388. SHA512_get_digest_size(SHA512object *self, void *closure)
  389. {
  390. return PyLong_FromLong(self->digestsize);
  391. }
  392. static PyObject *
  393. SHA256_get_name(SHA256object *self, void *closure)
  394. {
  395. if (self->digestsize == 28) {
  396. return PyUnicode_FromStringAndSize("sha224", 6);
  397. }
  398. return PyUnicode_FromStringAndSize("sha256", 6);
  399. }
  400. static PyObject *
  401. SHA512_get_name(SHA512object *self, void *closure)
  402. {
  403. if (self->digestsize == 64) {
  404. return PyUnicode_FromStringAndSize("sha512", 6);
  405. }
  406. return PyUnicode_FromStringAndSize("sha384", 6);
  407. }
  408. static PyGetSetDef SHA256_getseters[] = {
  409. {"block_size",
  410. (getter)SHA256_get_block_size, NULL,
  411. NULL,
  412. NULL},
  413. {"name",
  414. (getter)SHA256_get_name, NULL,
  415. NULL,
  416. NULL},
  417. {"digest_size",
  418. (getter)SHA256_get_digest_size, NULL,
  419. NULL,
  420. NULL},
  421. {NULL} /* Sentinel */
  422. };
  423. static PyGetSetDef SHA512_getseters[] = {
  424. {"block_size",
  425. (getter)SHA512_get_block_size, NULL,
  426. NULL,
  427. NULL},
  428. {"name",
  429. (getter)SHA512_get_name, NULL,
  430. NULL,
  431. NULL},
  432. {"digest_size",
  433. (getter)SHA512_get_digest_size, NULL,
  434. NULL,
  435. NULL},
  436. {NULL} /* Sentinel */
  437. };
  438. static PyType_Slot sha256_types_slots[] = {
  439. {Py_tp_dealloc, SHA256_dealloc},
  440. {Py_tp_methods, SHA256_methods},
  441. {Py_tp_getset, SHA256_getseters},
  442. {Py_tp_traverse, SHA2_traverse},
  443. {0,0}
  444. };
  445. static PyType_Slot sha512_type_slots[] = {
  446. {Py_tp_dealloc, SHA512_dealloc},
  447. {Py_tp_methods, SHA512_methods},
  448. {Py_tp_getset, SHA512_getseters},
  449. {Py_tp_traverse, SHA2_traverse},
  450. {0,0}
  451. };
  452. // Using _PyType_GetModuleState() on these types is safe since they
  453. // cannot be subclassed: they don't have the Py_TPFLAGS_BASETYPE flag.
  454. static PyType_Spec sha224_type_spec = {
  455. .name = "_sha2.SHA224Type",
  456. .basicsize = sizeof(SHA256object),
  457. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
  458. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
  459. .slots = sha256_types_slots
  460. };
  461. static PyType_Spec sha256_type_spec = {
  462. .name = "_sha2.SHA256Type",
  463. .basicsize = sizeof(SHA256object),
  464. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
  465. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
  466. .slots = sha256_types_slots
  467. };
  468. static PyType_Spec sha384_type_spec = {
  469. .name = "_sha2.SHA384Type",
  470. .basicsize = sizeof(SHA512object),
  471. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
  472. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
  473. .slots = sha512_type_slots
  474. };
  475. static PyType_Spec sha512_type_spec = {
  476. .name = "_sha2.SHA512Type",
  477. .basicsize = sizeof(SHA512object),
  478. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
  479. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
  480. .slots = sha512_type_slots
  481. };
  482. /* The module-level constructors. */
  483. /*[clinic input]
  484. _sha2.sha256
  485. string: object(c_default="NULL") = b''
  486. *
  487. usedforsecurity: bool = True
  488. Return a new SHA-256 hash object; optionally initialized with a string.
  489. [clinic start generated code]*/
  490. static PyObject *
  491. _sha2_sha256_impl(PyObject *module, PyObject *string, int usedforsecurity)
  492. /*[clinic end generated code: output=243c9dd289931f87 input=6249da1de607280a]*/
  493. {
  494. Py_buffer buf;
  495. if (string) {
  496. GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
  497. }
  498. sha2_state *state = sha2_get_state(module);
  499. SHA256object *new;
  500. if ((new = newSHA256object(state)) == NULL) {
  501. if (string) {
  502. PyBuffer_Release(&buf);
  503. }
  504. return NULL;
  505. }
  506. new->state = Hacl_Hash_SHA2_malloc_256();
  507. new->digestsize = 32;
  508. if (PyErr_Occurred()) {
  509. Py_DECREF(new);
  510. if (string) {
  511. PyBuffer_Release(&buf);
  512. }
  513. return NULL;
  514. }
  515. if (string) {
  516. if (buf.len >= HASHLIB_GIL_MINSIZE) {
  517. /* We do not initialize self->lock here as this is the constructor
  518. * where it is not yet possible to have concurrent access. */
  519. Py_BEGIN_ALLOW_THREADS
  520. update_256(new->state, buf.buf, buf.len);
  521. Py_END_ALLOW_THREADS
  522. } else {
  523. update_256(new->state, buf.buf, buf.len);
  524. }
  525. PyBuffer_Release(&buf);
  526. }
  527. return (PyObject *)new;
  528. }
  529. /*[clinic input]
  530. _sha2.sha224
  531. string: object(c_default="NULL") = b''
  532. *
  533. usedforsecurity: bool = True
  534. Return a new SHA-224 hash object; optionally initialized with a string.
  535. [clinic start generated code]*/
  536. static PyObject *
  537. _sha2_sha224_impl(PyObject *module, PyObject *string, int usedforsecurity)
  538. /*[clinic end generated code: output=68191f232e4a3843 input=c42bcba47fd7d2b7]*/
  539. {
  540. Py_buffer buf;
  541. if (string) {
  542. GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
  543. }
  544. sha2_state *state = sha2_get_state(module);
  545. SHA256object *new;
  546. if ((new = newSHA224object(state)) == NULL) {
  547. if (string) {
  548. PyBuffer_Release(&buf);
  549. }
  550. return NULL;
  551. }
  552. new->state = Hacl_Hash_SHA2_malloc_224();
  553. new->digestsize = 28;
  554. if (PyErr_Occurred()) {
  555. Py_DECREF(new);
  556. if (string) {
  557. PyBuffer_Release(&buf);
  558. }
  559. return NULL;
  560. }
  561. if (string) {
  562. if (buf.len >= HASHLIB_GIL_MINSIZE) {
  563. /* We do not initialize self->lock here as this is the constructor
  564. * where it is not yet possible to have concurrent access. */
  565. Py_BEGIN_ALLOW_THREADS
  566. update_256(new->state, buf.buf, buf.len);
  567. Py_END_ALLOW_THREADS
  568. } else {
  569. update_256(new->state, buf.buf, buf.len);
  570. }
  571. PyBuffer_Release(&buf);
  572. }
  573. return (PyObject *)new;
  574. }
  575. /*[clinic input]
  576. _sha2.sha512
  577. string: object(c_default="NULL") = b''
  578. *
  579. usedforsecurity: bool = True
  580. Return a new SHA-512 hash object; optionally initialized with a string.
  581. [clinic start generated code]*/
  582. static PyObject *
  583. _sha2_sha512_impl(PyObject *module, PyObject *string, int usedforsecurity)
  584. /*[clinic end generated code: output=d55c8996eca214d7 input=0576ae2a6ebfad25]*/
  585. {
  586. SHA512object *new;
  587. Py_buffer buf;
  588. sha2_state *state = sha2_get_state(module);
  589. if (string)
  590. GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
  591. if ((new = newSHA512object(state)) == NULL) {
  592. if (string)
  593. PyBuffer_Release(&buf);
  594. return NULL;
  595. }
  596. new->state = Hacl_Hash_SHA2_malloc_512();
  597. new->digestsize = 64;
  598. if (PyErr_Occurred()) {
  599. Py_DECREF(new);
  600. if (string)
  601. PyBuffer_Release(&buf);
  602. return NULL;
  603. }
  604. if (string) {
  605. if (buf.len >= HASHLIB_GIL_MINSIZE) {
  606. /* We do not initialize self->lock here as this is the constructor
  607. * where it is not yet possible to have concurrent access. */
  608. Py_BEGIN_ALLOW_THREADS
  609. update_512(new->state, buf.buf, buf.len);
  610. Py_END_ALLOW_THREADS
  611. } else {
  612. update_512(new->state, buf.buf, buf.len);
  613. }
  614. PyBuffer_Release(&buf);
  615. }
  616. return (PyObject *)new;
  617. }
  618. /*[clinic input]
  619. _sha2.sha384
  620. string: object(c_default="NULL") = b''
  621. *
  622. usedforsecurity: bool = True
  623. Return a new SHA-384 hash object; optionally initialized with a string.
  624. [clinic start generated code]*/
  625. static PyObject *
  626. _sha2_sha384_impl(PyObject *module, PyObject *string, int usedforsecurity)
  627. /*[clinic end generated code: output=b29a0d81d51d1368 input=4e9199d8de0d2f9b]*/
  628. {
  629. SHA512object *new;
  630. Py_buffer buf;
  631. sha2_state *state = sha2_get_state(module);
  632. if (string)
  633. GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
  634. if ((new = newSHA384object(state)) == NULL) {
  635. if (string)
  636. PyBuffer_Release(&buf);
  637. return NULL;
  638. }
  639. new->state = Hacl_Hash_SHA2_malloc_384();
  640. new->digestsize = 48;
  641. if (PyErr_Occurred()) {
  642. Py_DECREF(new);
  643. if (string)
  644. PyBuffer_Release(&buf);
  645. return NULL;
  646. }
  647. if (string) {
  648. if (buf.len >= HASHLIB_GIL_MINSIZE) {
  649. /* We do not initialize self->lock here as this is the constructor
  650. * where it is not yet possible to have concurrent access. */
  651. Py_BEGIN_ALLOW_THREADS
  652. update_512(new->state, buf.buf, buf.len);
  653. Py_END_ALLOW_THREADS
  654. } else {
  655. update_512(new->state, buf.buf, buf.len);
  656. }
  657. PyBuffer_Release(&buf);
  658. }
  659. return (PyObject *)new;
  660. }
  661. /* List of functions exported by this module */
  662. static struct PyMethodDef SHA2_functions[] = {
  663. _SHA2_SHA256_METHODDEF
  664. _SHA2_SHA224_METHODDEF
  665. _SHA2_SHA512_METHODDEF
  666. _SHA2_SHA384_METHODDEF
  667. {NULL, NULL} /* Sentinel */
  668. };
  669. static int
  670. _sha2_traverse(PyObject *module, visitproc visit, void *arg)
  671. {
  672. sha2_state *state = sha2_get_state(module);
  673. Py_VISIT(state->sha224_type);
  674. Py_VISIT(state->sha256_type);
  675. Py_VISIT(state->sha384_type);
  676. Py_VISIT(state->sha512_type);
  677. return 0;
  678. }
  679. static int
  680. _sha2_clear(PyObject *module)
  681. {
  682. sha2_state *state = sha2_get_state(module);
  683. Py_CLEAR(state->sha224_type);
  684. Py_CLEAR(state->sha256_type);
  685. Py_CLEAR(state->sha384_type);
  686. Py_CLEAR(state->sha512_type);
  687. return 0;
  688. }
  689. static void
  690. _sha2_free(void *module)
  691. {
  692. _sha2_clear((PyObject *)module);
  693. }
  694. /* Initialize this module. */
  695. static int sha2_exec(PyObject *module)
  696. {
  697. sha2_state *state = sha2_get_state(module);
  698. state->sha224_type = (PyTypeObject *)PyType_FromModuleAndSpec(
  699. module, &sha224_type_spec, NULL);
  700. if (state->sha224_type == NULL) {
  701. return -1;
  702. }
  703. state->sha256_type = (PyTypeObject *)PyType_FromModuleAndSpec(
  704. module, &sha256_type_spec, NULL);
  705. if (state->sha256_type == NULL) {
  706. return -1;
  707. }
  708. state->sha384_type = (PyTypeObject *)PyType_FromModuleAndSpec(
  709. module, &sha384_type_spec, NULL);
  710. if (state->sha384_type == NULL) {
  711. return -1;
  712. }
  713. state->sha512_type = (PyTypeObject *)PyType_FromModuleAndSpec(
  714. module, &sha512_type_spec, NULL);
  715. if (state->sha512_type == NULL) {
  716. return -1;
  717. }
  718. if (PyModule_AddType(module, state->sha224_type) < 0) {
  719. return -1;
  720. }
  721. if (PyModule_AddType(module, state->sha256_type) < 0) {
  722. return -1;
  723. }
  724. if (PyModule_AddType(module, state->sha384_type) < 0) {
  725. return -1;
  726. }
  727. if (PyModule_AddType(module, state->sha512_type) < 0) {
  728. return -1;
  729. }
  730. return 0;
  731. }
  732. static PyModuleDef_Slot _sha2_slots[] = {
  733. {Py_mod_exec, sha2_exec},
  734. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  735. {0, NULL}
  736. };
  737. static struct PyModuleDef _sha2module = {
  738. PyModuleDef_HEAD_INIT,
  739. .m_name = "_sha2",
  740. .m_size = sizeof(sha2_state),
  741. .m_methods = SHA2_functions,
  742. .m_slots = _sha2_slots,
  743. .m_traverse = _sha2_traverse,
  744. .m_clear = _sha2_clear,
  745. .m_free = _sha2_free
  746. };
  747. PyMODINIT_FUNC
  748. PyInit__sha2(void)
  749. {
  750. return PyModuleDef_Init(&_sha2module);
  751. }