| 1 | n/a | /* |
|---|
| 2 | n/a | * Written in 2013 by Dmitry Chestnykh <dmitry@codingrobots.com> |
|---|
| 3 | n/a | * Modified for CPython by Christian Heimes <christian@python.org> |
|---|
| 4 | n/a | * |
|---|
| 5 | n/a | * To the extent possible under law, the author have dedicated all |
|---|
| 6 | n/a | * copyright and related and neighboring rights to this software to |
|---|
| 7 | n/a | * the public domain worldwide. This software is distributed without |
|---|
| 8 | n/a | * any warranty. http://creativecommons.org/publicdomain/zero/1.0/ |
|---|
| 9 | n/a | */ |
|---|
| 10 | n/a | |
|---|
| 11 | n/a | #include "Python.h" |
|---|
| 12 | n/a | |
|---|
| 13 | n/a | #include "impl/blake2.h" |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | extern PyTypeObject PyBlake2_BLAKE2bType; |
|---|
| 16 | n/a | extern PyTypeObject PyBlake2_BLAKE2sType; |
|---|
| 17 | n/a | |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | PyDoc_STRVAR(blake2mod__doc__, |
|---|
| 20 | n/a | "_blake2b provides BLAKE2b for hashlib\n" |
|---|
| 21 | n/a | ); |
|---|
| 22 | n/a | |
|---|
| 23 | n/a | |
|---|
| 24 | n/a | static struct PyMethodDef blake2mod_functions[] = { |
|---|
| 25 | n/a | {NULL, NULL} |
|---|
| 26 | n/a | }; |
|---|
| 27 | n/a | |
|---|
| 28 | n/a | static struct PyModuleDef blake2_module = { |
|---|
| 29 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 30 | n/a | "_blake2", |
|---|
| 31 | n/a | blake2mod__doc__, |
|---|
| 32 | n/a | -1, |
|---|
| 33 | n/a | blake2mod_functions, |
|---|
| 34 | n/a | NULL, |
|---|
| 35 | n/a | NULL, |
|---|
| 36 | n/a | NULL, |
|---|
| 37 | n/a | NULL |
|---|
| 38 | n/a | }; |
|---|
| 39 | n/a | |
|---|
| 40 | n/a | #define ADD_INT(d, name, value) do { \ |
|---|
| 41 | n/a | PyObject *x = PyLong_FromLong(value); \ |
|---|
| 42 | n/a | if (!x) { \ |
|---|
| 43 | n/a | Py_DECREF(m); \ |
|---|
| 44 | n/a | return NULL; \ |
|---|
| 45 | n/a | } \ |
|---|
| 46 | n/a | if (PyDict_SetItemString(d, name, x) < 0) { \ |
|---|
| 47 | n/a | Py_DECREF(m); \ |
|---|
| 48 | n/a | return NULL; \ |
|---|
| 49 | n/a | } \ |
|---|
| 50 | n/a | Py_DECREF(x); \ |
|---|
| 51 | n/a | } while(0) |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | |
|---|
| 54 | n/a | PyMODINIT_FUNC |
|---|
| 55 | n/a | PyInit__blake2(void) |
|---|
| 56 | n/a | { |
|---|
| 57 | n/a | PyObject *m; |
|---|
| 58 | n/a | PyObject *d; |
|---|
| 59 | n/a | |
|---|
| 60 | n/a | m = PyModule_Create(&blake2_module); |
|---|
| 61 | n/a | if (m == NULL) |
|---|
| 62 | n/a | return NULL; |
|---|
| 63 | n/a | |
|---|
| 64 | n/a | /* BLAKE2b */ |
|---|
| 65 | n/a | Py_TYPE(&PyBlake2_BLAKE2bType) = &PyType_Type; |
|---|
| 66 | n/a | if (PyType_Ready(&PyBlake2_BLAKE2bType) < 0) { |
|---|
| 67 | n/a | return NULL; |
|---|
| 68 | n/a | } |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | Py_INCREF(&PyBlake2_BLAKE2bType); |
|---|
| 71 | n/a | PyModule_AddObject(m, "blake2b", (PyObject *)&PyBlake2_BLAKE2bType); |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | d = PyBlake2_BLAKE2bType.tp_dict; |
|---|
| 74 | n/a | ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES); |
|---|
| 75 | n/a | ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES); |
|---|
| 76 | n/a | ADD_INT(d, "MAX_KEY_SIZE", BLAKE2B_KEYBYTES); |
|---|
| 77 | n/a | ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES); |
|---|
| 78 | n/a | |
|---|
| 79 | n/a | PyModule_AddIntConstant(m, "BLAKE2B_SALT_SIZE", BLAKE2B_SALTBYTES); |
|---|
| 80 | n/a | PyModule_AddIntConstant(m, "BLAKE2B_PERSON_SIZE", BLAKE2B_PERSONALBYTES); |
|---|
| 81 | n/a | PyModule_AddIntConstant(m, "BLAKE2B_MAX_KEY_SIZE", BLAKE2B_KEYBYTES); |
|---|
| 82 | n/a | PyModule_AddIntConstant(m, "BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES); |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | /* BLAKE2s */ |
|---|
| 85 | n/a | Py_TYPE(&PyBlake2_BLAKE2sType) = &PyType_Type; |
|---|
| 86 | n/a | if (PyType_Ready(&PyBlake2_BLAKE2sType) < 0) { |
|---|
| 87 | n/a | return NULL; |
|---|
| 88 | n/a | } |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | Py_INCREF(&PyBlake2_BLAKE2sType); |
|---|
| 91 | n/a | PyModule_AddObject(m, "blake2s", (PyObject *)&PyBlake2_BLAKE2sType); |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | d = PyBlake2_BLAKE2sType.tp_dict; |
|---|
| 94 | n/a | ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES); |
|---|
| 95 | n/a | ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES); |
|---|
| 96 | n/a | ADD_INT(d, "MAX_KEY_SIZE", BLAKE2S_KEYBYTES); |
|---|
| 97 | n/a | ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES); |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | PyModule_AddIntConstant(m, "BLAKE2S_SALT_SIZE", BLAKE2S_SALTBYTES); |
|---|
| 100 | n/a | PyModule_AddIntConstant(m, "BLAKE2S_PERSON_SIZE", BLAKE2S_PERSONALBYTES); |
|---|
| 101 | n/a | PyModule_AddIntConstant(m, "BLAKE2S_MAX_KEY_SIZE", BLAKE2S_KEYBYTES); |
|---|
| 102 | n/a | PyModule_AddIntConstant(m, "BLAKE2S_MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES); |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | return m; |
|---|
| 105 | n/a | } |
|---|