blake2module.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com>
  3. * Modified for CPython by Christian Heimes <christian@python.org>
  4. *
  5. * To the extent possible under law, the author have dedicated all
  6. * copyright and related and neighboring rights to this software to
  7. * the public domain worldwide. This software is distributed without
  8. * any warranty. http://creativecommons.org/publicdomain/zero/1.0/
  9. */
  10. #ifndef Py_BUILD_CORE_BUILTIN
  11. # define Py_BUILD_CORE_MODULE 1
  12. #endif
  13. #include "Python.h"
  14. #include "blake2module.h"
  15. extern PyType_Spec blake2b_type_spec;
  16. extern PyType_Spec blake2s_type_spec;
  17. PyDoc_STRVAR(blake2mod__doc__,
  18. "_blake2b provides BLAKE2b for hashlib\n"
  19. );
  20. typedef struct {
  21. PyTypeObject* blake2b_type;
  22. PyTypeObject* blake2s_type;
  23. } Blake2State;
  24. static inline Blake2State*
  25. blake2_get_state(PyObject *module)
  26. {
  27. void *state = PyModule_GetState(module);
  28. assert(state != NULL);
  29. return (Blake2State *)state;
  30. }
  31. static struct PyMethodDef blake2mod_functions[] = {
  32. {NULL, NULL}
  33. };
  34. static int
  35. _blake2_traverse(PyObject *module, visitproc visit, void *arg)
  36. {
  37. Blake2State *state = blake2_get_state(module);
  38. Py_VISIT(state->blake2b_type);
  39. Py_VISIT(state->blake2s_type);
  40. return 0;
  41. }
  42. static int
  43. _blake2_clear(PyObject *module)
  44. {
  45. Blake2State *state = blake2_get_state(module);
  46. Py_CLEAR(state->blake2b_type);
  47. Py_CLEAR(state->blake2s_type);
  48. return 0;
  49. }
  50. static void
  51. _blake2_free(void *module)
  52. {
  53. _blake2_clear((PyObject *)module);
  54. }
  55. #define ADD_INT(d, name, value) do { \
  56. PyObject *x = PyLong_FromLong(value); \
  57. if (!x) \
  58. return -1; \
  59. if (PyDict_SetItemString(d, name, x) < 0) { \
  60. Py_DECREF(x); \
  61. return -1; \
  62. } \
  63. Py_DECREF(x); \
  64. } while(0)
  65. #define ADD_INT_CONST(NAME, VALUE) do { \
  66. if (PyModule_AddIntConstant(m, NAME, VALUE) < 0) { \
  67. return -1; \
  68. } \
  69. } while (0)
  70. static int
  71. blake2_exec(PyObject *m)
  72. {
  73. Blake2State* st = blake2_get_state(m);
  74. st->blake2b_type = (PyTypeObject *)PyType_FromModuleAndSpec(
  75. m, &blake2b_type_spec, NULL);
  76. if (NULL == st->blake2b_type)
  77. return -1;
  78. /* BLAKE2b */
  79. if (PyModule_AddType(m, st->blake2b_type) < 0) {
  80. return -1;
  81. }
  82. PyObject *d = st->blake2b_type->tp_dict;
  83. ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES);
  84. ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES);
  85. ADD_INT(d, "MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
  86. ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
  87. ADD_INT_CONST("BLAKE2B_SALT_SIZE", BLAKE2B_SALTBYTES);
  88. ADD_INT_CONST("BLAKE2B_PERSON_SIZE", BLAKE2B_PERSONALBYTES);
  89. ADD_INT_CONST("BLAKE2B_MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
  90. ADD_INT_CONST("BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
  91. /* BLAKE2s */
  92. st->blake2s_type = (PyTypeObject *)PyType_FromModuleAndSpec(
  93. m, &blake2s_type_spec, NULL);
  94. if (NULL == st->blake2s_type)
  95. return -1;
  96. if (PyModule_AddType(m, st->blake2s_type) < 0) {
  97. return -1;
  98. }
  99. d = st->blake2s_type->tp_dict;
  100. ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES);
  101. ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES);
  102. ADD_INT(d, "MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
  103. ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
  104. ADD_INT_CONST("BLAKE2S_SALT_SIZE", BLAKE2S_SALTBYTES);
  105. ADD_INT_CONST("BLAKE2S_PERSON_SIZE", BLAKE2S_PERSONALBYTES);
  106. ADD_INT_CONST("BLAKE2S_MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
  107. ADD_INT_CONST("BLAKE2S_MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
  108. return 0;
  109. }
  110. #undef ADD_INT
  111. #undef ADD_INT_CONST
  112. static PyModuleDef_Slot _blake2_slots[] = {
  113. {Py_mod_exec, blake2_exec},
  114. {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED},
  115. {0, NULL}
  116. };
  117. static struct PyModuleDef blake2_module = {
  118. PyModuleDef_HEAD_INIT,
  119. "_blake2",
  120. .m_doc = blake2mod__doc__,
  121. .m_size = sizeof(Blake2State),
  122. .m_methods = blake2mod_functions,
  123. .m_slots = _blake2_slots,
  124. .m_traverse = _blake2_traverse,
  125. .m_clear = _blake2_clear,
  126. .m_free = _blake2_free,
  127. };
  128. PyMODINIT_FUNC
  129. PyInit__blake2(void)
  130. {
  131. return PyModuleDef_Init(&blake2_module);
  132. }