blake2b_impl.c.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*[clinic input]
  2. preserve
  3. [clinic start generated code]*/
  4. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  5. # include "pycore_gc.h" // PyGC_Head
  6. # include "pycore_runtime.h" // _Py_ID()
  7. #endif
  8. PyDoc_STRVAR(py_blake2b_new__doc__,
  9. "blake2b(data=b\'\', /, *, digest_size=_blake2.blake2b.MAX_DIGEST_SIZE,\n"
  10. " key=b\'\', salt=b\'\', person=b\'\', fanout=1, depth=1, leaf_size=0,\n"
  11. " node_offset=0, node_depth=0, inner_size=0, last_node=False,\n"
  12. " usedforsecurity=True)\n"
  13. "--\n"
  14. "\n"
  15. "Return a new BLAKE2b hash object.");
  16. static PyObject *
  17. py_blake2b_new_impl(PyTypeObject *type, PyObject *data, int digest_size,
  18. Py_buffer *key, Py_buffer *salt, Py_buffer *person,
  19. int fanout, int depth, unsigned long leaf_size,
  20. unsigned long long node_offset, int node_depth,
  21. int inner_size, int last_node, int usedforsecurity);
  22. static PyObject *
  23. py_blake2b_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
  24. {
  25. PyObject *return_value = NULL;
  26. #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
  27. #define NUM_KEYWORDS 12
  28. static struct {
  29. PyGC_Head _this_is_not_used;
  30. PyObject_VAR_HEAD
  31. PyObject *ob_item[NUM_KEYWORDS];
  32. } _kwtuple = {
  33. .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
  34. .ob_item = { &_Py_ID(digest_size), &_Py_ID(key), &_Py_ID(salt), &_Py_ID(person), &_Py_ID(fanout), &_Py_ID(depth), &_Py_ID(leaf_size), &_Py_ID(node_offset), &_Py_ID(node_depth), &_Py_ID(inner_size), &_Py_ID(last_node), &_Py_ID(usedforsecurity), },
  35. };
  36. #undef NUM_KEYWORDS
  37. #define KWTUPLE (&_kwtuple.ob_base.ob_base)
  38. #else // !Py_BUILD_CORE
  39. # define KWTUPLE NULL
  40. #endif // !Py_BUILD_CORE
  41. static const char * const _keywords[] = {"", "digest_size", "key", "salt", "person", "fanout", "depth", "leaf_size", "node_offset", "node_depth", "inner_size", "last_node", "usedforsecurity", NULL};
  42. static _PyArg_Parser _parser = {
  43. .keywords = _keywords,
  44. .fname = "blake2b",
  45. .kwtuple = KWTUPLE,
  46. };
  47. #undef KWTUPLE
  48. PyObject *argsbuf[13];
  49. PyObject * const *fastargs;
  50. Py_ssize_t nargs = PyTuple_GET_SIZE(args);
  51. Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 0;
  52. PyObject *data = NULL;
  53. int digest_size = BLAKE2B_OUTBYTES;
  54. Py_buffer key = {NULL, NULL};
  55. Py_buffer salt = {NULL, NULL};
  56. Py_buffer person = {NULL, NULL};
  57. int fanout = 1;
  58. int depth = 1;
  59. unsigned long leaf_size = 0;
  60. unsigned long long node_offset = 0;
  61. int node_depth = 0;
  62. int inner_size = 0;
  63. int last_node = 0;
  64. int usedforsecurity = 1;
  65. fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 0, 1, 0, argsbuf);
  66. if (!fastargs) {
  67. goto exit;
  68. }
  69. if (nargs < 1) {
  70. goto skip_optional_posonly;
  71. }
  72. noptargs--;
  73. data = fastargs[0];
  74. skip_optional_posonly:
  75. if (!noptargs) {
  76. goto skip_optional_kwonly;
  77. }
  78. if (fastargs[1]) {
  79. digest_size = _PyLong_AsInt(fastargs[1]);
  80. if (digest_size == -1 && PyErr_Occurred()) {
  81. goto exit;
  82. }
  83. if (!--noptargs) {
  84. goto skip_optional_kwonly;
  85. }
  86. }
  87. if (fastargs[2]) {
  88. if (PyObject_GetBuffer(fastargs[2], &key, PyBUF_SIMPLE) != 0) {
  89. goto exit;
  90. }
  91. if (!PyBuffer_IsContiguous(&key, 'C')) {
  92. _PyArg_BadArgument("blake2b", "argument 'key'", "contiguous buffer", fastargs[2]);
  93. goto exit;
  94. }
  95. if (!--noptargs) {
  96. goto skip_optional_kwonly;
  97. }
  98. }
  99. if (fastargs[3]) {
  100. if (PyObject_GetBuffer(fastargs[3], &salt, PyBUF_SIMPLE) != 0) {
  101. goto exit;
  102. }
  103. if (!PyBuffer_IsContiguous(&salt, 'C')) {
  104. _PyArg_BadArgument("blake2b", "argument 'salt'", "contiguous buffer", fastargs[3]);
  105. goto exit;
  106. }
  107. if (!--noptargs) {
  108. goto skip_optional_kwonly;
  109. }
  110. }
  111. if (fastargs[4]) {
  112. if (PyObject_GetBuffer(fastargs[4], &person, PyBUF_SIMPLE) != 0) {
  113. goto exit;
  114. }
  115. if (!PyBuffer_IsContiguous(&person, 'C')) {
  116. _PyArg_BadArgument("blake2b", "argument 'person'", "contiguous buffer", fastargs[4]);
  117. goto exit;
  118. }
  119. if (!--noptargs) {
  120. goto skip_optional_kwonly;
  121. }
  122. }
  123. if (fastargs[5]) {
  124. fanout = _PyLong_AsInt(fastargs[5]);
  125. if (fanout == -1 && PyErr_Occurred()) {
  126. goto exit;
  127. }
  128. if (!--noptargs) {
  129. goto skip_optional_kwonly;
  130. }
  131. }
  132. if (fastargs[6]) {
  133. depth = _PyLong_AsInt(fastargs[6]);
  134. if (depth == -1 && PyErr_Occurred()) {
  135. goto exit;
  136. }
  137. if (!--noptargs) {
  138. goto skip_optional_kwonly;
  139. }
  140. }
  141. if (fastargs[7]) {
  142. if (!_PyLong_UnsignedLong_Converter(fastargs[7], &leaf_size)) {
  143. goto exit;
  144. }
  145. if (!--noptargs) {
  146. goto skip_optional_kwonly;
  147. }
  148. }
  149. if (fastargs[8]) {
  150. if (!_PyLong_UnsignedLongLong_Converter(fastargs[8], &node_offset)) {
  151. goto exit;
  152. }
  153. if (!--noptargs) {
  154. goto skip_optional_kwonly;
  155. }
  156. }
  157. if (fastargs[9]) {
  158. node_depth = _PyLong_AsInt(fastargs[9]);
  159. if (node_depth == -1 && PyErr_Occurred()) {
  160. goto exit;
  161. }
  162. if (!--noptargs) {
  163. goto skip_optional_kwonly;
  164. }
  165. }
  166. if (fastargs[10]) {
  167. inner_size = _PyLong_AsInt(fastargs[10]);
  168. if (inner_size == -1 && PyErr_Occurred()) {
  169. goto exit;
  170. }
  171. if (!--noptargs) {
  172. goto skip_optional_kwonly;
  173. }
  174. }
  175. if (fastargs[11]) {
  176. last_node = PyObject_IsTrue(fastargs[11]);
  177. if (last_node < 0) {
  178. goto exit;
  179. }
  180. if (!--noptargs) {
  181. goto skip_optional_kwonly;
  182. }
  183. }
  184. usedforsecurity = PyObject_IsTrue(fastargs[12]);
  185. if (usedforsecurity < 0) {
  186. goto exit;
  187. }
  188. skip_optional_kwonly:
  189. return_value = py_blake2b_new_impl(type, data, digest_size, &key, &salt, &person, fanout, depth, leaf_size, node_offset, node_depth, inner_size, last_node, usedforsecurity);
  190. exit:
  191. /* Cleanup for key */
  192. if (key.obj) {
  193. PyBuffer_Release(&key);
  194. }
  195. /* Cleanup for salt */
  196. if (salt.obj) {
  197. PyBuffer_Release(&salt);
  198. }
  199. /* Cleanup for person */
  200. if (person.obj) {
  201. PyBuffer_Release(&person);
  202. }
  203. return return_value;
  204. }
  205. PyDoc_STRVAR(_blake2_blake2b_copy__doc__,
  206. "copy($self, /)\n"
  207. "--\n"
  208. "\n"
  209. "Return a copy of the hash object.");
  210. #define _BLAKE2_BLAKE2B_COPY_METHODDEF \
  211. {"copy", (PyCFunction)_blake2_blake2b_copy, METH_NOARGS, _blake2_blake2b_copy__doc__},
  212. static PyObject *
  213. _blake2_blake2b_copy_impl(BLAKE2bObject *self);
  214. static PyObject *
  215. _blake2_blake2b_copy(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
  216. {
  217. return _blake2_blake2b_copy_impl(self);
  218. }
  219. PyDoc_STRVAR(_blake2_blake2b_update__doc__,
  220. "update($self, data, /)\n"
  221. "--\n"
  222. "\n"
  223. "Update this hash object\'s state with the provided bytes-like object.");
  224. #define _BLAKE2_BLAKE2B_UPDATE_METHODDEF \
  225. {"update", (PyCFunction)_blake2_blake2b_update, METH_O, _blake2_blake2b_update__doc__},
  226. PyDoc_STRVAR(_blake2_blake2b_digest__doc__,
  227. "digest($self, /)\n"
  228. "--\n"
  229. "\n"
  230. "Return the digest value as a bytes object.");
  231. #define _BLAKE2_BLAKE2B_DIGEST_METHODDEF \
  232. {"digest", (PyCFunction)_blake2_blake2b_digest, METH_NOARGS, _blake2_blake2b_digest__doc__},
  233. static PyObject *
  234. _blake2_blake2b_digest_impl(BLAKE2bObject *self);
  235. static PyObject *
  236. _blake2_blake2b_digest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
  237. {
  238. return _blake2_blake2b_digest_impl(self);
  239. }
  240. PyDoc_STRVAR(_blake2_blake2b_hexdigest__doc__,
  241. "hexdigest($self, /)\n"
  242. "--\n"
  243. "\n"
  244. "Return the digest value as a string of hexadecimal digits.");
  245. #define _BLAKE2_BLAKE2B_HEXDIGEST_METHODDEF \
  246. {"hexdigest", (PyCFunction)_blake2_blake2b_hexdigest, METH_NOARGS, _blake2_blake2b_hexdigest__doc__},
  247. static PyObject *
  248. _blake2_blake2b_hexdigest_impl(BLAKE2bObject *self);
  249. static PyObject *
  250. _blake2_blake2b_hexdigest(BLAKE2bObject *self, PyObject *Py_UNUSED(ignored))
  251. {
  252. return _blake2_blake2b_hexdigest_impl(self);
  253. }
  254. /*[clinic end generated code: output=996b4fe396824797 input=a9049054013a1b77]*/