Home | History | Annotate | Download | only in _blake2
      1 /*
      2  * Written in 2013 by Dmitry Chestnykh <dmitry (at) codingrobots.com>
      3  * Modified for CPython by Christian Heimes <christian (at) 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
     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_TYPE(&PyBlake2_BLAKE2bType) = &PyType_Type;
     66     if (PyType_Ready(&PyBlake2_BLAKE2bType) < 0) {
     67         return NULL;
     68     }
     69 
     70     Py_INCREF(&PyBlake2_BLAKE2bType);
     71     PyModule_AddObject(m, "blake2b", (PyObject *)&PyBlake2_BLAKE2bType);
     72 
     73     d = PyBlake2_BLAKE2bType.tp_dict;
     74     ADD_INT(d, "SALT_SIZE", BLAKE2B_SALTBYTES);
     75     ADD_INT(d, "PERSON_SIZE", BLAKE2B_PERSONALBYTES);
     76     ADD_INT(d, "MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
     77     ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
     78 
     79     PyModule_AddIntConstant(m, "BLAKE2B_SALT_SIZE", BLAKE2B_SALTBYTES);
     80     PyModule_AddIntConstant(m, "BLAKE2B_PERSON_SIZE", BLAKE2B_PERSONALBYTES);
     81     PyModule_AddIntConstant(m, "BLAKE2B_MAX_KEY_SIZE", BLAKE2B_KEYBYTES);
     82     PyModule_AddIntConstant(m, "BLAKE2B_MAX_DIGEST_SIZE", BLAKE2B_OUTBYTES);
     83 
     84     /* BLAKE2s */
     85     Py_TYPE(&PyBlake2_BLAKE2sType) = &PyType_Type;
     86     if (PyType_Ready(&PyBlake2_BLAKE2sType) < 0) {
     87         return NULL;
     88     }
     89 
     90     Py_INCREF(&PyBlake2_BLAKE2sType);
     91     PyModule_AddObject(m, "blake2s", (PyObject *)&PyBlake2_BLAKE2sType);
     92 
     93     d = PyBlake2_BLAKE2sType.tp_dict;
     94     ADD_INT(d, "SALT_SIZE", BLAKE2S_SALTBYTES);
     95     ADD_INT(d, "PERSON_SIZE", BLAKE2S_PERSONALBYTES);
     96     ADD_INT(d, "MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
     97     ADD_INT(d, "MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
     98 
     99     PyModule_AddIntConstant(m, "BLAKE2S_SALT_SIZE", BLAKE2S_SALTBYTES);
    100     PyModule_AddIntConstant(m, "BLAKE2S_PERSON_SIZE", BLAKE2S_PERSONALBYTES);
    101     PyModule_AddIntConstant(m, "BLAKE2S_MAX_KEY_SIZE", BLAKE2S_KEYBYTES);
    102     PyModule_AddIntConstant(m, "BLAKE2S_MAX_DIGEST_SIZE", BLAKE2S_OUTBYTES);
    103 
    104     return m;
    105 }
    106