sha1module.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* SHA1 module */
  2. /* This module provides an interface to the SHA1 algorithm */
  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. Copyright (C) 2005-2007 Gregory P. Smith (greg@krypto.org)
  9. Licensed to PSF under a Contributor Agreement.
  10. */
  11. /* SHA1 objects */
  12. #ifndef Py_BUILD_CORE_BUILTIN
  13. # define Py_BUILD_CORE_MODULE 1
  14. #endif
  15. #include "Python.h"
  16. #include "hashlib.h"
  17. #include "pycore_strhex.h" // _Py_strhex()
  18. #include "pycore_typeobject.h" // _PyType_GetModuleState()
  19. /*[clinic input]
  20. module _sha1
  21. class SHA1Type "SHA1object *" "&PyType_Type"
  22. [clinic start generated code]*/
  23. /*[clinic end generated code: output=da39a3ee5e6b4b0d input=3dc9a20d1becb759]*/
  24. /* Some useful types */
  25. #if SIZEOF_INT == 4
  26. typedef unsigned int SHA1_INT32; /* 32-bit integer */
  27. typedef long long SHA1_INT64; /* 64-bit integer */
  28. #else
  29. /* not defined. compilation will die. */
  30. #endif
  31. /* The SHA1 block size and message digest sizes, in bytes */
  32. #define SHA1_BLOCKSIZE 64
  33. #define SHA1_DIGESTSIZE 20
  34. #include "_hacl/Hacl_Hash_SHA1.h"
  35. typedef struct {
  36. PyObject_HEAD
  37. // Prevents undefined behavior via multiple threads entering the C API.
  38. // The lock will be NULL before threaded access has been enabled.
  39. PyThread_type_lock lock;
  40. Hacl_Streaming_SHA1_state *hash_state;
  41. } SHA1object;
  42. #include "clinic/sha1module.c.h"
  43. typedef struct {
  44. PyTypeObject* sha1_type;
  45. } SHA1State;
  46. static inline SHA1State*
  47. sha1_get_state(PyObject *module)
  48. {
  49. void *state = PyModule_GetState(module);
  50. assert(state != NULL);
  51. return (SHA1State *)state;
  52. }
  53. static SHA1object *
  54. newSHA1object(SHA1State *st)
  55. {
  56. SHA1object *sha = (SHA1object *)PyObject_GC_New(SHA1object, st->sha1_type);
  57. sha->lock = NULL;
  58. PyObject_GC_Track(sha);
  59. return sha;
  60. }
  61. /* Internal methods for a hash object */
  62. static int
  63. SHA1_traverse(PyObject *ptr, visitproc visit, void *arg)
  64. {
  65. Py_VISIT(Py_TYPE(ptr));
  66. return 0;
  67. }
  68. static void
  69. SHA1_dealloc(SHA1object *ptr)
  70. {
  71. Hacl_Streaming_SHA1_legacy_free(ptr->hash_state);
  72. if (ptr->lock != NULL) {
  73. PyThread_free_lock(ptr->lock);
  74. }
  75. PyTypeObject *tp = Py_TYPE(ptr);
  76. PyObject_GC_UnTrack(ptr);
  77. PyObject_GC_Del(ptr);
  78. Py_DECREF(tp);
  79. }
  80. /* External methods for a hash object */
  81. /*[clinic input]
  82. SHA1Type.copy
  83. cls: defining_class
  84. Return a copy of the hash object.
  85. [clinic start generated code]*/
  86. static PyObject *
  87. SHA1Type_copy_impl(SHA1object *self, PyTypeObject *cls)
  88. /*[clinic end generated code: output=b32d4461ce8bc7a7 input=6c22e66fcc34c58e]*/
  89. {
  90. SHA1State *st = _PyType_GetModuleState(cls);
  91. SHA1object *newobj;
  92. if ((newobj = newSHA1object(st)) == NULL)
  93. return NULL;
  94. ENTER_HASHLIB(self);
  95. newobj->hash_state = Hacl_Streaming_SHA1_legacy_copy(self->hash_state);
  96. LEAVE_HASHLIB(self);
  97. return (PyObject *)newobj;
  98. }
  99. /*[clinic input]
  100. SHA1Type.digest
  101. Return the digest value as a bytes object.
  102. [clinic start generated code]*/
  103. static PyObject *
  104. SHA1Type_digest_impl(SHA1object *self)
  105. /*[clinic end generated code: output=2f05302a7aa2b5cb input=13824b35407444bd]*/
  106. {
  107. unsigned char digest[SHA1_DIGESTSIZE];
  108. ENTER_HASHLIB(self);
  109. Hacl_Streaming_SHA1_legacy_finish(self->hash_state, digest);
  110. LEAVE_HASHLIB(self);
  111. return PyBytes_FromStringAndSize((const char *)digest, SHA1_DIGESTSIZE);
  112. }
  113. /*[clinic input]
  114. SHA1Type.hexdigest
  115. Return the digest value as a string of hexadecimal digits.
  116. [clinic start generated code]*/
  117. static PyObject *
  118. SHA1Type_hexdigest_impl(SHA1object *self)
  119. /*[clinic end generated code: output=4161fd71e68c6659 input=97691055c0c74ab0]*/
  120. {
  121. unsigned char digest[SHA1_DIGESTSIZE];
  122. ENTER_HASHLIB(self);
  123. Hacl_Streaming_SHA1_legacy_finish(self->hash_state, digest);
  124. LEAVE_HASHLIB(self);
  125. return _Py_strhex((const char *)digest, SHA1_DIGESTSIZE);
  126. }
  127. static void update(Hacl_Streaming_SHA1_state *state, uint8_t *buf, Py_ssize_t len) {
  128. #if PY_SSIZE_T_MAX > UINT32_MAX
  129. while (len > UINT32_MAX) {
  130. Hacl_Streaming_SHA1_legacy_update(state, buf, UINT32_MAX);
  131. len -= UINT32_MAX;
  132. buf += UINT32_MAX;
  133. }
  134. #endif
  135. Hacl_Streaming_SHA1_legacy_update(state, buf, (uint32_t) len);
  136. }
  137. /*[clinic input]
  138. SHA1Type.update
  139. obj: object
  140. /
  141. Update this hash object's state with the provided string.
  142. [clinic start generated code]*/
  143. static PyObject *
  144. SHA1Type_update(SHA1object *self, PyObject *obj)
  145. /*[clinic end generated code: output=d9902f0e5015e9ae input=aad8e07812edbba3]*/
  146. {
  147. Py_buffer buf;
  148. GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
  149. if (self->lock == NULL && buf.len >= HASHLIB_GIL_MINSIZE) {
  150. self->lock = PyThread_allocate_lock();
  151. }
  152. if (self->lock != NULL) {
  153. Py_BEGIN_ALLOW_THREADS
  154. PyThread_acquire_lock(self->lock, 1);
  155. update(self->hash_state, buf.buf, buf.len);
  156. PyThread_release_lock(self->lock);
  157. Py_END_ALLOW_THREADS
  158. } else {
  159. update(self->hash_state, buf.buf, buf.len);
  160. }
  161. PyBuffer_Release(&buf);
  162. Py_RETURN_NONE;
  163. }
  164. static PyMethodDef SHA1_methods[] = {
  165. SHA1TYPE_COPY_METHODDEF
  166. SHA1TYPE_DIGEST_METHODDEF
  167. SHA1TYPE_HEXDIGEST_METHODDEF
  168. SHA1TYPE_UPDATE_METHODDEF
  169. {NULL, NULL} /* sentinel */
  170. };
  171. static PyObject *
  172. SHA1_get_block_size(PyObject *self, void *closure)
  173. {
  174. return PyLong_FromLong(SHA1_BLOCKSIZE);
  175. }
  176. static PyObject *
  177. SHA1_get_name(PyObject *self, void *closure)
  178. {
  179. return PyUnicode_FromStringAndSize("sha1", 4);
  180. }
  181. static PyObject *
  182. sha1_get_digest_size(PyObject *self, void *closure)
  183. {
  184. return PyLong_FromLong(SHA1_DIGESTSIZE);
  185. }
  186. static PyGetSetDef SHA1_getseters[] = {
  187. {"block_size",
  188. (getter)SHA1_get_block_size, NULL,
  189. NULL,
  190. NULL},
  191. {"name",
  192. (getter)SHA1_get_name, NULL,
  193. NULL,
  194. NULL},
  195. {"digest_size",
  196. (getter)sha1_get_digest_size, NULL,
  197. NULL,
  198. NULL},
  199. {NULL} /* Sentinel */
  200. };
  201. static PyType_Slot sha1_type_slots[] = {
  202. {Py_tp_dealloc, SHA1_dealloc},
  203. {Py_tp_methods, SHA1_methods},
  204. {Py_tp_getset, SHA1_getseters},
  205. {Py_tp_traverse, SHA1_traverse},
  206. {0,0}
  207. };
  208. static PyType_Spec sha1_type_spec = {
  209. .name = "_sha1.sha1",
  210. .basicsize = sizeof(SHA1object),
  211. .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION |
  212. Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_GC),
  213. .slots = sha1_type_slots
  214. };
  215. /* The single module-level function: new() */
  216. /*[clinic input]
  217. _sha1.sha1
  218. string: object(c_default="NULL") = b''
  219. *
  220. usedforsecurity: bool = True
  221. Return a new SHA1 hash object; optionally initialized with a string.
  222. [clinic start generated code]*/
  223. static PyObject *
  224. _sha1_sha1_impl(PyObject *module, PyObject *string, int usedforsecurity)
  225. /*[clinic end generated code: output=6f8b3af05126e18e input=bd54b68e2bf36a8a]*/
  226. {
  227. SHA1object *new;
  228. Py_buffer buf;
  229. if (string)
  230. GET_BUFFER_VIEW_OR_ERROUT(string, &buf);
  231. SHA1State *st = sha1_get_state(module);
  232. if ((new = newSHA1object(st)) == NULL) {
  233. if (string)
  234. PyBuffer_Release(&buf);
  235. return NULL;
  236. }
  237. new->hash_state = Hacl_Streaming_SHA1_legacy_create_in();
  238. if (PyErr_Occurred()) {
  239. Py_DECREF(new);
  240. if (string)
  241. PyBuffer_Release(&buf);
  242. return NULL;
  243. }
  244. if (string) {
  245. if (buf.len >= HASHLIB_GIL_MINSIZE) {
  246. /* We do not initialize self->lock here as this is the constructor
  247. * where it is not yet possible to have concurrent access. */
  248. Py_BEGIN_ALLOW_THREADS
  249. update(new->hash_state, buf.buf, buf.len);
  250. Py_END_ALLOW_THREADS
  251. } else {
  252. update(new->hash_state, buf.buf, buf.len);
  253. }
  254. PyBuffer_Release(&buf);
  255. }
  256. return (PyObject *)new;
  257. }
  258. /* List of functions exported by this module */
  259. static struct PyMethodDef SHA1_functions[] = {
  260. _SHA1_SHA1_METHODDEF
  261. {NULL, NULL} /* Sentinel */
  262. };
  263. static int
  264. _sha1_traverse(PyObject *module, visitproc visit, void *arg)
  265. {
  266. SHA1State *state = sha1_get_state(module);
  267. Py_VISIT(state->sha1_type);
  268. return 0;
  269. }
  270. static int
  271. _sha1_clear(PyObject *module)
  272. {
  273. SHA1State *state = sha1_get_state(module);
  274. Py_CLEAR(state->sha1_type);
  275. return 0;
  276. }
  277. static void
  278. _sha1_free(void *module)
  279. {
  280. _sha1_clear((PyObject *)module);
  281. }
  282. static int
  283. _sha1_exec(PyObject *module)
  284. {
  285. SHA1State* st = sha1_get_state(module);
  286. st->sha1_type = (PyTypeObject *)PyType_FromModuleAndSpec(
  287. module, &sha1_type_spec, NULL);
  288. if (st->sha1_type == NULL) {
  289. return -1;
  290. }
  291. Py_INCREF(st->sha1_type);
  292. if (PyModule_AddObject(module,
  293. "SHA1Type",
  294. (PyObject *)st->sha1_type) < 0) {
  295. Py_DECREF(st->sha1_type);
  296. return -1;
  297. }
  298. return 0;
  299. }
  300. /* Initialize this module. */
  301. static PyModuleDef_Slot _sha1_slots[] = {
  302. {Py_mod_exec, _sha1_exec},
  303. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  304. {0, NULL}
  305. };
  306. static struct PyModuleDef _sha1module = {
  307. PyModuleDef_HEAD_INIT,
  308. .m_name = "_sha1",
  309. .m_size = sizeof(SHA1State),
  310. .m_methods = SHA1_functions,
  311. .m_slots = _sha1_slots,
  312. .m_traverse = _sha1_traverse,
  313. .m_clear = _sha1_clear,
  314. .m_free = _sha1_free
  315. };
  316. PyMODINIT_FUNC
  317. PyInit__sha1(void)
  318. {
  319. return PyModuleDef_Init(&_sha1module);
  320. }