smbd: Use leases_db in lease_match()
[gd/samba-autobuild/.git] / lib / crypto / py_crypto.c
1 /*
2    Unix SMB/CIFS implementation.
3    Samba crypto functions
4
5    Copyright (C) Alexander Bokovoy <ab@samba.org> 2017
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include <Python.h>
22 #include "includes.h"
23 #include "python/py3compat.h"
24 #include "lib/crypto/arcfour.h"
25
26 static PyObject *py_crypto_arcfour_crypt_blob(PyObject *module, PyObject *args, PyObject *kwargs)
27 {
28         DATA_BLOB data, key;
29         PyObject *py_data, *py_key, *result;
30         TALLOC_CTX *ctx;
31
32         if (!PyArg_ParseTuple(args, "OO", &py_data, &py_key))
33                 return NULL;
34
35         if (!PyBytes_Check(py_data)) {
36                 PyErr_Format(PyExc_TypeError, "bytes expected");
37                 return NULL;
38         }
39
40         if (!PyBytes_Check(py_key)) {
41                 PyErr_Format(PyExc_TypeError, "bytes expected");
42                 return NULL;
43         }
44
45         ctx = talloc_new(NULL);
46
47         data.length = PyBytes_Size(py_data);
48         data.data = talloc_memdup(ctx, PyBytes_AsString(py_data), data.length);
49         if (!data.data) {
50                 talloc_free(ctx);
51                 return PyErr_NoMemory();
52         }
53
54         key.data = (uint8_t *)PyBytes_AsString(py_key);
55         key.length = PyBytes_Size(py_key);
56
57         arcfour_crypt_blob(data.data, data.length, &key);
58
59         result = PyBytes_FromStringAndSize((const char*) data.data, data.length);
60         talloc_free(ctx);
61         return result;
62 }
63
64
65 static const char py_crypto_arcfour_crypt_blob_doc[] = "arcfour_crypt_blob(data, key)\n"
66                                          "Encrypt the data with RC4 algorithm using the key";
67
68 static PyMethodDef py_crypto_methods[] = {
69         { "arcfour_crypt_blob", (PyCFunction)py_crypto_arcfour_crypt_blob, METH_VARARGS, py_crypto_arcfour_crypt_blob_doc },
70         { NULL },
71 };
72
73 static struct PyModuleDef moduledef = {
74         PyModuleDef_HEAD_INIT,
75         .m_name = "crypto",
76         .m_doc = "Crypto functions required for SMB",
77         .m_size = -1,
78         .m_methods = py_crypto_methods,
79 };
80
81 MODULE_INIT_FUNC(crypto)
82 {
83         PyObject *m;
84
85         m = PyModule_Create(&moduledef);
86         if (m == NULL)
87                 return NULL;
88
89         return m;
90 }