File: pyfcrypto.c

package info (click to toggle)
libfcrypto 0~20240414-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,592 kB
  • sloc: ansic: 15,069; sh: 6,189; makefile: 399; python: 306; cpp: 297; sed: 16
file content (325 lines) | stat: -rw-r--r-- 7,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 * Python bindings module for libfcrypto (pyfcrypto)
 *
 * Copyright (C) 2017-2024, Joachim Metz <joachim.metz@gmail.com>
 *
 * Refer to AUTHORS for acknowledgements.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <common.h>
#include <narrow_string.h>
#include <types.h>

#if defined( HAVE_STDLIB_H ) || defined( HAVE_WINAPI )
#include <stdlib.h>
#endif

#include "pyfcrypto.h"
#include "pyfcrypto_blowfish_context.h"
#include "pyfcrypto_des3_context.h"
#include "pyfcrypto_crypt.h"
#include "pyfcrypto_crypt_modes.h"
#include "pyfcrypto_error.h"
#include "pyfcrypto_libcerror.h"
#include "pyfcrypto_libfcrypto.h"
#include "pyfcrypto_python.h"
#include "pyfcrypto_rc4_context.h"
#include "pyfcrypto_serpent_context.h"
#include "pyfcrypto_unused.h"

/* The pyfcrypto module methods
 */
PyMethodDef pyfcrypto_module_methods[] = {
	{ "get_version",
	  (PyCFunction) pyfcrypto_get_version,
	  METH_NOARGS,
	  "get_version() -> String\n"
	  "\n"
	  "Retrieves the version." },

	{ "crypt_blowfish_cbc",
	  (PyCFunction) pyfcrypto_crypt_blowfish_cbc,
	  METH_VARARGS | METH_KEYWORDS,
	  "crypt_blowfish_cbc(context, mode, initialization_vector, data) -> Bytes\n"
	  "\n"
	  "De- or encrypts a block of data using Blowfish-CBC." },

	{ "crypt_blowfish_ecb",
	  (PyCFunction) pyfcrypto_crypt_blowfish_ecb,
	  METH_VARARGS | METH_KEYWORDS,
	  "crypt_blowfish_ecb(context, mode, data) -> Bytes\n"
	  "\n"
	  "De- or encrypts a block of data using Blowfish-ECB." },

	/* TODO remove deprecated crypt_des3 */

	{ "crypt_des3",
	  (PyCFunction) pyfcrypto_crypt_des3,
	  METH_VARARGS | METH_KEYWORDS,
	  "crypt_des3(context, mode, data) -> Bytes\n"
	  "\n"
	  "De- or encrypts a block of data using DES3." },

	{ "crypt_des3_cbc",
	  (PyCFunction) pyfcrypto_crypt_des3_cbc,
	  METH_VARARGS | METH_KEYWORDS,
	  "crypt_des3_cbc(context, mode, initialization_vector, data) -> Bytes\n"
	  "\n"
	  "De- or encrypts a block of data using DES3-CBC." },

	{ "crypt_des3_ecb",
	  (PyCFunction) pyfcrypto_crypt_des3_ecb,
	  METH_VARARGS | METH_KEYWORDS,
	  "crypt_des3_csb(context, mode, data) -> Bytes\n"
	  "\n"
	  "De- or encrypts a block of data using DES3-ECB." },

	{ "crypt_rc4",
	  (PyCFunction) pyfcrypto_crypt_rc4,
	  METH_VARARGS | METH_KEYWORDS,
	  "crypt_rc4(context, data) -> Bytes\n"
	  "\n"
	  "De- or encrypts a block of data using RC4." },

	/* TODO add crypt_serpent_cbc */

	{ "crypt_serpent_ecb",
	  (PyCFunction) pyfcrypto_crypt_serpent_ecb,
	  METH_VARARGS | METH_KEYWORDS,
	  "crypt_serpent_ecb(context, mode, data) -> Bytes\n"
	  "\n"
	  "De- or encrypts a block of data using Serpent-ECB." },

	/* Sentinel */
	{ NULL, NULL, 0, NULL }
};

/* Retrieves the pyfcrypto/libfcrypto version
 * Returns a Python object if successful or NULL on error
 */
PyObject *pyfcrypto_get_version(
           PyObject *self PYFCRYPTO_ATTRIBUTE_UNUSED,
           PyObject *arguments PYFCRYPTO_ATTRIBUTE_UNUSED )
{
	const char *version_string   = NULL;
	size_t version_string_length = 0;

	PYFCRYPTO_UNREFERENCED_PARAMETER( self )
	PYFCRYPTO_UNREFERENCED_PARAMETER( arguments )

	Py_BEGIN_ALLOW_THREADS

	version_string = libfcrypto_get_version();

	Py_END_ALLOW_THREADS

	version_string_length = narrow_string_length(
	                         version_string );

	/* Pass the string length to PyUnicode_DecodeUTF8
	 * otherwise it makes the end of string character is part
	 * of the string
	 */
	return( PyUnicode_DecodeUTF8(
	         version_string,
	         (Py_ssize_t) version_string_length,
	         NULL ) );
}

#if PY_MAJOR_VERSION >= 3

/* The pyfcrypto module definition
 */
PyModuleDef pyfcrypto_module_definition = {
	PyModuleDef_HEAD_INIT,

	/* m_name */
	"pyfcrypto",
	/* m_doc */
	"Python libfcrypto module (pyfcrypto).",
	/* m_size */
	-1,
	/* m_methods */
	pyfcrypto_module_methods,
	/* m_reload */
	NULL,
	/* m_traverse */
	NULL,
	/* m_clear */
	NULL,
	/* m_free */
	NULL,
};

#endif /* PY_MAJOR_VERSION >= 3 */

/* Initializes the pyfcrypto module
 */
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_pyfcrypto(
                void )
#else
PyMODINIT_FUNC initpyfcrypto(
                void )
#endif
{
	PyObject *module           = NULL;
	PyGILState_STATE gil_state = 0;

#if defined( HAVE_DEBUG_OUTPUT )
	libfcrypto_notify_set_stream(
	 stderr,
	 NULL );
	libfcrypto_notify_set_verbose(
	 1 );
#endif

	/* Create the module
	 * This function must be called before grabbing the GIL
	 * otherwise the module will segfault on a version mismatch
	 */
#if PY_MAJOR_VERSION >= 3
	module = PyModule_Create(
	          &pyfcrypto_module_definition );
#else
	module = Py_InitModule3(
	          "pyfcrypto",
	          pyfcrypto_module_methods,
	          "Python libfcrypto module (pyfcrypto)." );
#endif
	if( module == NULL )
	{
#if PY_MAJOR_VERSION >= 3
		return( NULL );
#else
		return;
#endif
	}
#if PY_VERSION_HEX < 0x03070000
	PyEval_InitThreads();
#endif
	gil_state = PyGILState_Ensure();

	/* Setup the crypt modes type object
	 */
	pyfcrypto_crypt_modes_type_object.tp_new = PyType_GenericNew;

	if( pyfcrypto_crypt_modes_init_type(
	     &pyfcrypto_crypt_modes_type_object ) != 1 )
	{
		goto on_error;
	}
	if( PyType_Ready(
	     &pyfcrypto_crypt_modes_type_object ) < 0 )
	{
		goto on_error;
	}
	Py_IncRef(
	 (PyObject *) &pyfcrypto_crypt_modes_type_object );

	PyModule_AddObject(
	 module,
	 "crypt_modes",
	 (PyObject *) &pyfcrypto_crypt_modes_type_object );

	/* Setup the Blowfish context type object
	 */
	pyfcrypto_blowfish_context_type_object.tp_new = PyType_GenericNew;

	if( PyType_Ready(
	     &pyfcrypto_blowfish_context_type_object ) < 0 )
	{
		goto on_error;
	}
	Py_IncRef(
	 (PyObject *) &pyfcrypto_blowfish_context_type_object );

	PyModule_AddObject(
	 module,
	 "blowfish_context",
	 (PyObject *) &pyfcrypto_blowfish_context_type_object );

	/* Setup the DES3 context type object
	 */
	pyfcrypto_des3_context_type_object.tp_new = PyType_GenericNew;

	if( PyType_Ready(
	     &pyfcrypto_des3_context_type_object ) < 0 )
	{
		goto on_error;
	}
	Py_IncRef(
	 (PyObject *) &pyfcrypto_des3_context_type_object );

	PyModule_AddObject(
	 module,
	 "des3_context",
	 (PyObject *) &pyfcrypto_des3_context_type_object );

	/* Setup the RC4 context type object
	 */
	pyfcrypto_rc4_context_type_object.tp_new = PyType_GenericNew;

	if( PyType_Ready(
	     &pyfcrypto_rc4_context_type_object ) < 0 )
	{
		goto on_error;
	}
	Py_IncRef(
	 (PyObject *) &pyfcrypto_rc4_context_type_object );

	PyModule_AddObject(
	 module,
	 "rc4_context",
	 (PyObject *) &pyfcrypto_rc4_context_type_object );

	/* Setup the Serpent context type object
	 */
	pyfcrypto_serpent_context_type_object.tp_new = PyType_GenericNew;

	if( PyType_Ready(
	     &pyfcrypto_serpent_context_type_object ) < 0 )
	{
		goto on_error;
	}
	Py_IncRef(
	 (PyObject *) &pyfcrypto_serpent_context_type_object );

	PyModule_AddObject(
	 module,
	 "serpent_context",
	 (PyObject *) &pyfcrypto_serpent_context_type_object );

	PyGILState_Release(
	 gil_state );

#if PY_MAJOR_VERSION >= 3
	return( module );
#else
	return;
#endif

on_error:
	PyGILState_Release(
	 gil_state );

#if PY_MAJOR_VERSION >= 3
	return( NULL );
#else
	return;
#endif
}