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
11 #include "Python.h"
12
13 #include "impl/blake2.h"
14
15 extern PyTypeObject PyBlake2_BLAKE2bType;
16 extern PyTypeObject PyBlake2_BLAKE2sType;
17
18
19 PyDoc_STRVAR(blake2mod__doc__,
20 "_blake2b provides BLAKE2b for hashlib\n"
21 );
22
23
24 static struct PyMethodDef blake2mod_functions[] = {
25 {NULL, NULL}
26 };
27
28 static struct PyModuleDef blake2_module = {
29 PyModuleDef_HEAD_INIT,
30 "_blake2",
31 blake2mod__doc__,
32 -1,
33 blake2mod_functions,
34 NULL,
35 NULL,
36 NULL,
37 NULL
38 };
39
40 #define ADD_INT(d, name, value) do { \
41 PyObject *x = PyLong_FromLong(value); \
42 if (!x) { \
43 Py_DECREF(m); \
44 return NULL; \
45 } \
46 if (PyDict_SetItemString(d, name, x) < 0) { \
47 Py_DECREF(m); \
48 return NULL; \
49 } \
50 Py_DECREF(x); \
51 } while(0)
52
53
54 PyMODINIT_FUNC
PyInit__blake2(void)55 PyInit__blake2(void)
56 {
57 PyObject *m;
58 PyObject *d;
59
60 m = PyModule_Create(&blake2_module);
61 if (m == NULL)
62 return NULL;
63
64 /* BLAKE2b */
65 Py_SET_TYPE(&PyBlake2_BLAKE2bType, &PyType_Type);
66 if (PyModule_AddType(m, &PyBlake2_BLAKE2bType) < 0) {
67 return NULL;
68 }
69
70 d = PyBlake2_BLAKE2bType.tp_dict;
71 ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES);
72 ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES);
73 ADD_INT(d, "MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
74 ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
75
76 PyModule_AddIntConstant(m, "BLAKE2B_SALT_SIZE", BLAKE2B_SALTBYTES);
77 PyModule_AddIntConstant(m, "BLAKE2B_PERSON_SIZE", BLAKE2B_PERSONALBYTES);
78 PyModule_AddIntConstant(m, "BLAKE2B_MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
79 PyModule_AddIntConstant(m, "BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
80
81 /* BLAKE2s */
82 Py_SET_TYPE(&PyBlake2_BLAKE2sType, &PyType_Type);
83 if (PyModule_AddType(m, &PyBlake2_BLAKE2sType) < 0) {
84 return NULL;
85 }
86
87 d = PyBlake2_BLAKE2sType.tp_dict;
88 ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES);
89 ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES);
90 ADD_INT(d, "MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
91 ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
92
93 PyModule_AddIntConstant(m, "BLAKE2S_SALT_SIZE", BLAKE2S_SALTBYTES);
94 PyModule_AddIntConstant(m, "BLAKE2S_PERSON_SIZE", BLAKE2S_PERSONALBYTES);
95 PyModule_AddIntConstant(m, "BLAKE2S_MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
96 PyModule_AddIntConstant(m, "BLAKE2S_MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
97
98 return m;
99 }
100