/[mcrypt]/libmcrypt/lib/mcrypt_modules.c
ViewVC logotype

Contents of /libmcrypt/lib/mcrypt_modules.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.16 - (show annotations)
Fri May 18 10:54:14 2001 UTC (22 years, 10 months ago) by nmav
Branch: MAIN
CVS Tags: libmcrypt_2_4_15, mcrypt_2_4_13
Changes since 1.15: +2 -2 lines
File MIME type: text/plain
updated api.

1 /*
2 * Copyright (C) 1998,1999,2000 Nikos Mavroyanopoulos
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Library General Public License as published
6 * by the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 #ifndef LIBDEFS_H
21 #define LIBDEFS_H
22 #include <libdefs.h>
23 #endif
24 #include <bzero.h>
25 #include <mcrypt_internal.h>
26 #include <xmemory.h>
27
28 #ifndef DEBUG
29 # define fputs(x, y) 1
30 #endif
31
32 WIN32DLL_DEFINE
33 int mcrypt_module_close(MCRYPT td)
34 {
35
36 lt_dlclose(td->algorithm_handle);
37 lt_dlclose(td->mode_handle);
38 if (lt_dlexit() != 0) {
39 return MCRYPT_UNKNOWN_ERROR;
40 }
41
42 td->algorithm_handle = NULL;
43 td->mode_handle = NULL;
44
45 td->m_encrypt = NULL;
46 td->a_encrypt = NULL;
47 td->a_decrypt = NULL;
48 td->m_decrypt = NULL;
49
50 free(td);
51
52 return 0;
53 }
54
55 #ifndef USE_LTDL
56 WIN32DLL_DEFINE
57 void *mcrypt_dlopen_ext ( const char *filename) {
58 void *ret;
59 char full_path[1054];
60
61 if (strlen(filename)>sizeof(full_path)) return NULL;
62
63 strcpy(full_path, filename);
64 ret = dlopen(full_path, RTLD_LAZY);
65
66 if (ret==NULL) {
67 #ifndef WIN32
68 strcat(full_path, ".so");
69 #else
70 strcat(full_path, ".dll");
71 #endif
72 ret = dlopen(full_path, RTLD_LAZY);
73 }
74 return ret;
75
76 }
77 #endif
78
79 WIN32DLL_DEFINE
80 void *mcrypt_dlopen ( const char* a_directory, const char *m_directory, const char *filename) {
81 void* ret=NULL;
82
83 #ifdef USE_LTDL
84 char paths[1526];
85
86 memset( paths, 0, 1024);
87 if (a_directory != NULL) {
88 strncat( paths, a_directory, 512);
89 strcat( paths, ":");
90 }
91 if (m_directory != NULL) {
92 strncat( paths, m_directory, 512);
93 strcat( paths, ":");
94 }
95 strncat( paths, LIBDIR, 512);
96
97 lt_dlsetsearchpath(paths);
98
99 ret = lt_dlopenext(filename);
100
101 return ret;
102
103 #else
104 char full_path[1050];
105
106 if (a_directory!=NULL) {
107 strncpy(full_path, a_directory, 512);
108 strcat(full_path, "/");
109 strncat( full_path, filename, 512);
110 ret = mcrypt_dlopen_ext(full_path);
111 }
112 if (ret==NULL) {
113 if (m_directory!=NULL) {
114 strncpy(full_path, m_directory, 512);
115 strcat(full_path, "/");
116 strncat( full_path, filename, 510);
117 ret = mcrypt_dlopen_ext(full_path);
118 }
119 }
120 if (ret==NULL) {
121 if (LIBDIR!=NULL) {
122 strncpy(full_path, LIBDIR, 512);
123 strcat(full_path, "/");
124 strncat( full_path, filename, 510);
125 ret = mcrypt_dlopen_ext(full_path);
126 }
127 }
128 return ret;
129 #endif
130
131 }
132
133 WIN32DLL_DEFINE
134 MCRYPT mcrypt_module_open(char *algorithm,
135 char *a_directory, char *mode, char *m_directory)
136 {
137 MCRYPT td;
138 const char* err;
139
140 td = calloc(1, sizeof(CRYPT_STREAM));
141 if (td==NULL) return MCRYPT_FAILED;
142
143 if (lt_dlinit() != 0) {
144 return MCRYPT_FAILED;
145 }
146
147 td->algorithm_handle = mcrypt_dlopen(a_directory, m_directory, algorithm);
148 if (td->algorithm_handle == NULL) {
149 err=lt_dlerror();
150 if (err!=NULL) fputs( err, stderr);
151 fputs("\n", stderr);
152 return MCRYPT_FAILED;
153 }
154
155 td->mode_handle = mcrypt_dlopen(a_directory, m_directory, mode);
156
157 if (td->mode_handle == NULL) {
158 err=lt_dlerror();
159 if (err!=NULL) fputs( err, stderr);
160 lt_dlclose(td->algorithm_handle);
161 return MCRYPT_FAILED;
162 }
163
164 td->a_encrypt = lt_dlsym(td->algorithm_handle, "_mcrypt_encrypt");
165 td->a_decrypt = lt_dlsym(td->algorithm_handle, "_mcrypt_decrypt");
166 td->m_encrypt = lt_dlsym(td->mode_handle, "_mcrypt");
167 td->m_decrypt = lt_dlsym(td->mode_handle, "_mdecrypt");
168 td->a_block_size =
169 lt_dlsym(td->algorithm_handle, "_mcrypt_get_block_size");
170
171 if (mcrypt_enc_is_block_algorithm_mode(td) !=
172 mcrypt_enc_is_block_algorithm(td)) {
173 mcrypt_module_close(td);
174 return MCRYPT_FAILED;
175 }
176
177 return td;
178 }
179
180
181
182 /* Modules' frontends */
183
184 WIN32DLL_DEFINE
185 int mcrypt_get_size(MCRYPT td)
186 {
187 int (*_mcrypt_get_size) (void);
188
189 const char *error;
190
191 _mcrypt_get_size = lt_dlsym(td->algorithm_handle, "_mcrypt_get_size");
192 if (_mcrypt_get_size == NULL) {
193 error = lt_dlerror();
194 if (error!=NULL) fputs(error, stderr);
195 fputs("\n", stderr);
196 return MCRYPT_UNKNOWN_ERROR;
197 }
198 return _mcrypt_get_size();
199 }
200
201 WIN32DLL_DEFINE
202 int mcrypt_mode_get_size(MCRYPT td)
203 {
204 int (*_mcrypt_get_size) (void);
205
206 const char *error;
207
208 _mcrypt_get_size = lt_dlsym(td->mode_handle, "_mcrypt_mode_get_size");
209 if (_mcrypt_get_size == NULL) {
210 error = lt_dlerror();
211 if (error!=NULL) fputs(error, stderr);
212 fputs("\n", stderr);
213 return MCRYPT_UNKNOWN_ERROR;
214 }
215 return _mcrypt_get_size();
216 }
217
218 WIN32DLL_DEFINE
219 int mcrypt_set_key(MCRYPT td, void *a, void *b, int c, void *d, int e)
220 {
221 int (*__mcrypt_set_key_stream) (void *, void *, int, void *, int);
222 int (*__mcrypt_set_key_block) (void *, void *, int);
223 const char *error;
224
225 if (mcrypt_enc_is_block_algorithm(td) == 0) {
226 /* stream */
227 __mcrypt_set_key_stream = lt_dlsym(td->algorithm_handle, "_mcrypt_set_key");
228 if (__mcrypt_set_key_stream == NULL) {
229 error = lt_dlerror();
230 if (error!=NULL) fputs(error, stderr);
231 fputs("\n", stderr);
232 return -2;
233 }
234 return __mcrypt_set_key_stream(a, b, c, d, e);
235 } else {
236 __mcrypt_set_key_block = lt_dlsym(td->algorithm_handle, "_mcrypt_set_key");
237 if (__mcrypt_set_key_block == NULL) {
238 error = lt_dlerror();
239 if (error!=NULL) fputs(error, stderr);
240 fputs("\n", stderr);
241 return -2;
242 }
243 return __mcrypt_set_key_block(a, b, c);
244 }
245 }
246
247 WIN32DLL_DEFINE
248 int mcrypt_enc_set_state(MCRYPT td, void *iv, int size)
249 {
250 int (*__mcrypt_set_state) (void *, void *, int);
251 const char *error;
252
253 __mcrypt_set_state = lt_dlsym(td->mode_handle, "_mcrypt_set_state");
254 if (__mcrypt_set_state==NULL) {
255 error = lt_dlerror();
256 if (error!=NULL) fputs(error, stderr);
257 fputs("\n", stderr);
258 return MCRYPT_UNKNOWN_ERROR;
259 }
260 return __mcrypt_set_state(td->abuf, iv, size);
261 }
262
263 WIN32DLL_DEFINE
264 int mcrypt_enc_get_block_size(MCRYPT td)
265 {
266 int (*_mcrypt_get_block_size) (void);
267
268 _mcrypt_get_block_size = td->a_block_size;
269 return _mcrypt_get_block_size();
270 }
271
272 WIN32DLL_DEFINE
273 int mcrypt_get_algo_iv_size(MCRYPT td)
274 {
275 int (*_mcrypt_get_algo_iv_size) (void);
276
277 _mcrypt_get_algo_iv_size = lt_dlsym(td->algorithm_handle, "_mcrypt_get_algo_iv_size");
278 return _mcrypt_get_algo_iv_size();
279 }
280
281 WIN32DLL_DEFINE
282 int mcrypt_enc_get_iv_size(MCRYPT td)
283 {
284 if (mcrypt_enc_is_block_algorithm_mode(td) == 1) {
285 return mcrypt_enc_get_block_size(td);
286 } else {
287 return mcrypt_get_algo_iv_size(td);
288 }
289 }
290
291 WIN32DLL_DEFINE
292 int mcrypt_enc_get_key_size(MCRYPT td)
293 {
294 int (*_mcrypt_get_key_size) (void);
295
296 _mcrypt_get_key_size = lt_dlsym(td->algorithm_handle, "_mcrypt_get_key_size");
297 return _mcrypt_get_key_size();
298 }
299
300 WIN32DLL_DEFINE
301 int *mcrypt_enc_get_supported_key_sizes(MCRYPT td, int *len)
302 {
303 int *(*_mcrypt_get_key_sizes) (int *);
304 int *size;
305
306 _mcrypt_get_key_sizes =
307 lt_dlsym(td->algorithm_handle, "_mcrypt_get_supported_key_sizes");
308 size = _mcrypt_get_key_sizes(len);
309
310 return size;
311 }
312
313 WIN32DLL_DEFINE
314 int mcrypt_enc_is_block_algorithm(MCRYPT td)
315 {
316 int (*_is_block_algorithm) (void);
317
318 _is_block_algorithm = lt_dlsym(td->algorithm_handle, "_is_block_algorithm");
319 return _is_block_algorithm();
320 }
321
322 WIN32DLL_DEFINE
323 char *mcrypt_enc_get_algorithms_name(MCRYPT td)
324 {
325 char *(*_mcrypt_get_algorithms_name) (void);
326
327 _mcrypt_get_algorithms_name =
328 lt_dlsym(td->algorithm_handle, "_mcrypt_get_algorithms_name");
329 return _mcrypt_get_algorithms_name();
330 }
331
332 WIN32DLL_DEFINE
333 int init_mcrypt(MCRYPT td, void *buf, void *a, int b, void *c)
334 {
335 int (*_init_mcrypt) (void *, void *, int, void *, int);
336
337 _init_mcrypt = lt_dlsym(td->mode_handle, "_init_mcrypt");
338 return _init_mcrypt(buf, a, b, c, mcrypt_enc_get_block_size(td));
339 }
340
341 WIN32DLL_DEFINE
342 int end_mcrypt(MCRYPT td, void *buf)
343 {
344 int (*_end_mcrypt) (void *);
345
346 _end_mcrypt = lt_dlsym(td->mode_handle, "_end_mcrypt");
347 return _end_mcrypt(buf);
348 }
349
350 WIN32DLL_DEFINE
351 int mcrypt(MCRYPT td, void *buf, void *a, int b)
352 {
353 int (*_mcrypt) (void *, void *, int, int, void *, void *, void*);
354 _mcrypt = td->m_encrypt;
355
356 return _mcrypt(buf, a, b, mcrypt_enc_get_block_size(td), td->akey,
357 td->a_encrypt, td->a_decrypt);
358 }
359
360 WIN32DLL_DEFINE
361 int mdecrypt(MCRYPT td, void *buf, void *a, int b)
362 {
363 int (*_mdecrypt) (void *, void *, int, int, void *, void *, void*);
364
365 _mdecrypt = td->m_decrypt;
366 return _mdecrypt(buf, a, b, mcrypt_enc_get_block_size(td),
367 td->akey, td->a_encrypt, td->a_decrypt);
368 }
369
370 WIN32DLL_DEFINE
371 char *mcrypt_enc_get_modes_name(MCRYPT td)
372 {
373 char *(*_mcrypt_get_modes_name) (void);
374
375 _mcrypt_get_modes_name = lt_dlsym(td->mode_handle, "_mcrypt_get_modes_name");
376 return _mcrypt_get_modes_name();
377 }
378
379 WIN32DLL_DEFINE
380 int mcrypt_enc_is_block_mode(MCRYPT td)
381 {
382 int (*_is_block_mode) (void);
383
384 _is_block_mode = lt_dlsym(td->mode_handle, "_is_block_mode");
385 return _is_block_mode();
386 }
387
388 WIN32DLL_DEFINE
389 int mcrypt_enc_mode_has_iv(MCRYPT td)
390 {
391 int (*_has_iv) (void);
392
393 _has_iv = lt_dlsym(td->mode_handle, "_has_iv");
394 return _has_iv();
395 }
396
397 WIN32DLL_DEFINE
398 int mcrypt_enc_is_block_algorithm_mode(MCRYPT td)
399 {
400 int (*_is_a_block_mode) (void);
401
402 _is_a_block_mode = lt_dlsym(td->mode_handle, "_is_block_algorithm_mode");
403 return _is_a_block_mode();
404 }
405
406 WIN32DLL_DEFINE
407 int mcrypt_enc_self_test(MCRYPT td)
408 {
409 int (*_self_test) (void);
410
411 _self_test = lt_dlsym(td->algorithm_handle, "_mcrypt_self_test");
412
413 return _self_test();
414 }
415
416 WIN32DLL_DEFINE
417 int mcrypt_module_self_test(char *algorithm, char *a_directory)
418 {
419 int i;
420 lt_dlhandle _handle;
421 int (*_self_test) (void);
422 const char* error;
423
424 if (lt_dlinit() != 0) {
425 return MCRYPT_UNKNOWN_ERROR;
426 }
427
428 _handle = mcrypt_dlopen(a_directory, NULL, algorithm);
429
430 if (!_handle) {
431 error=lt_dlerror();
432 if (error!=NULL) fputs( error, stderr);
433 fputs("\n", stderr);
434 lt_dlexit();
435 return MCRYPT_UNKNOWN_ERROR;
436 }
437
438 _self_test = lt_dlsym(_handle, "_mcrypt_self_test");
439
440 i = _self_test();
441
442 lt_dlclose(_handle);
443 if (lt_dlexit() != 0) {
444 return MCRYPT_UNKNOWN_ERROR;
445 }
446
447 return i;
448 }
449
450 WIN32DLL_DEFINE
451 int mcrypt_module_algorithm_version(char *algorithm, char *a_directory)
452 {
453 int i;
454 lt_dlhandle _handle;
455 int (*_version) (void);
456 const char* error;
457
458 if (lt_dlinit() != 0) {
459 return MCRYPT_UNKNOWN_ERROR;
460 }
461
462 _handle = mcrypt_dlopen(a_directory, NULL, algorithm);
463 if (!_handle) {
464 error=lt_dlerror();
465 if (error!=NULL) fputs( error, stderr);
466 fputs("\n", stderr);
467 return MCRYPT_UNKNOWN_ERROR;
468 }
469
470 _version = lt_dlsym(_handle, "_mcrypt_algorithm_version");
471
472 i = _version();
473
474 lt_dlclose(_handle);
475 if (lt_dlexit() != 0) {
476 return MCRYPT_UNKNOWN_ERROR;
477 }
478
479 return i;
480 }
481
482 WIN32DLL_DEFINE
483 int mcrypt_module_mode_version(char *mode, char *m_directory)
484 {
485 int i;
486 lt_dlhandle _handle;
487 int (*_version) (void);
488 const char* error;
489
490 if (lt_dlinit() != 0) {
491 return MCRYPT_UNKNOWN_ERROR;
492 }
493
494 _handle = mcrypt_dlopen(m_directory, NULL, mode);
495 if (!_handle) {
496 error=lt_dlerror();
497 if(error!=NULL) fputs(error, stderr);
498 fputs("\n", stderr);
499 return MCRYPT_UNKNOWN_ERROR;
500 }
501
502 _version = lt_dlsym(_handle, "_mcrypt_mode_version");
503
504 i = _version();
505
506 lt_dlclose(_handle);
507 if (lt_dlexit() != 0) {
508 return MCRYPT_UNKNOWN_ERROR;
509 }
510
511 return i;
512 }
513
514 WIN32DLL_DEFINE
515 int mcrypt_module_is_block_algorithm(char *algorithm, char *a_directory)
516 {
517 int i;
518 lt_dlhandle _handle;
519 int (*_is_block_algorithm) (void);
520 const char* error;
521
522 if (lt_dlinit() != 0) {
523 return MCRYPT_UNKNOWN_ERROR;
524 }
525
526 _handle = mcrypt_dlopen(a_directory, NULL, algorithm);
527
528 if (!_handle) {
529 error=lt_dlerror();
530 if(error!=NULL) fputs( error, stderr);
531 fputs("\n", stderr);
532 return MCRYPT_UNKNOWN_ERROR;
533 }
534
535 _is_block_algorithm = lt_dlsym(_handle, "_is_block_algorithm");
536
537 i = _is_block_algorithm();
538
539 lt_dlclose(_handle);
540 if (lt_dlexit() != 0) {
541 return MCRYPT_UNKNOWN_ERROR;
542 }
543
544 return i;
545 }
546
547 WIN32DLL_DEFINE
548 int mcrypt_module_is_block_algorithm_mode(char *mode, char *m_directory)
549 {
550 int i;
551 lt_dlhandle _handle;
552 int (*_is_a_block_mode) (void);
553 const char* error;
554
555 if (lt_dlinit() != 0) {
556 return MCRYPT_UNKNOWN_ERROR;
557 }
558
559 _handle = mcrypt_dlopen(m_directory, NULL, mode);
560 if (!_handle) {
561 error=lt_dlerror();
562 if(error!=NULL) fputs( error, stderr);
563 fputs("\n", stderr);
564 return MCRYPT_UNKNOWN_ERROR;
565 }
566
567 _is_a_block_mode = lt_dlsym(_handle, "_is_block_algorithm_mode");
568
569 i = _is_a_block_mode();
570
571 lt_dlclose(_handle);
572 if (lt_dlexit() != 0) {
573 return MCRYPT_UNKNOWN_ERROR;
574 }
575
576 return i;
577 }
578
579 WIN32DLL_DEFINE
580 int mcrypt_module_is_block_mode(char *mode, char *m_directory)
581 {
582 int i;
583 lt_dlhandle _handle;
584 int (*_is_block_mode) (void);
585 const char* error;
586
587 if (lt_dlinit() != 0) {
588 return MCRYPT_UNKNOWN_ERROR;
589 }
590
591 _handle = mcrypt_dlopen(m_directory, NULL, mode);
592 if (!_handle) {
593 error=lt_dlerror();
594 if(error!=NULL) fputs( error, stderr);
595 fputs("\n", stderr);
596 return MCRYPT_UNKNOWN_ERROR;
597 }
598
599 _is_block_mode = lt_dlsym(_handle, "_is_block_mode");
600
601 i = _is_block_mode();
602
603 lt_dlclose(_handle);
604 if (lt_dlexit() != 0) {
605 return MCRYPT_UNKNOWN_ERROR;
606 }
607
608 return i;
609 }
610
611 WIN32DLL_DEFINE
612 int mcrypt_module_get_algo_block_size(char *algorithm, char *a_directory)
613 {
614 int i;
615 lt_dlhandle _handle;
616 int (*_get_block_size) (void);
617 const char* error;
618
619 if (lt_dlinit() != 0) {
620 return MCRYPT_UNKNOWN_ERROR;
621 }
622
623 _handle = mcrypt_dlopen(a_directory, NULL, algorithm);
624 if (!_handle) {
625 error=lt_dlerror();
626 if(error!=NULL) fputs( error, stderr);
627 fputs("\n", stderr);
628 return MCRYPT_UNKNOWN_ERROR;
629 }
630
631 _get_block_size = lt_dlsym(_handle, "_mcrypt_get_block_size");
632
633 i = _get_block_size();
634
635 lt_dlclose(_handle);
636 if (lt_dlexit() != 0) {
637 return MCRYPT_UNKNOWN_ERROR;
638 }
639
640 return i;
641 }
642
643 WIN32DLL_DEFINE
644 int mcrypt_module_get_algo_key_size(char *algorithm, char *a_directory)
645 {
646 int i;
647 lt_dlhandle _handle;
648 int (*_get_key_size) (void);
649 const char* error;
650
651 if (lt_dlinit() != 0) {
652 return MCRYPT_UNKNOWN_ERROR;
653 }
654
655 _handle = mcrypt_dlopen(a_directory, NULL, algorithm);
656 if (!_handle) {
657 error=lt_dlerror();
658 if(error!=NULL) fputs( error, stderr);
659 fputs("\n", stderr);
660 return MCRYPT_UNKNOWN_ERROR;
661 }
662
663 _get_key_size = lt_dlsym(_handle, "_mcrypt_get_key_size");
664
665 i = _get_key_size();
666
667 lt_dlclose(_handle);
668 if (lt_dlexit() != 0) {
669 return MCRYPT_UNKNOWN_ERROR;
670 }
671
672 return i;
673 }
674
675 WIN32DLL_DEFINE
676 int *mcrypt_module_get_algo_supported_key_sizes(char *algorithm,
677 char *a_directory,
678 int *len)
679 {
680 lt_dlhandle _handle;
681 int *(*_mcrypt_get_key_sizes) (int *);
682 int *size;
683 const char* error;
684
685 if (lt_dlinit() != 0) {
686 *len = 0;
687 return NULL;
688 }
689
690 _handle = mcrypt_dlopen(a_directory, NULL, algorithm);
691 if (!_handle) {
692 error=lt_dlerror();
693 if(error!=NULL) fputs( error, stderr);
694 fputs("\n", stderr);
695 *len = 0;
696 return NULL;
697 }
698
699 _mcrypt_get_key_sizes =
700 lt_dlsym(_handle, "_mcrypt_get_supported_key_sizes");
701
702 size = _mcrypt_get_key_sizes(len);
703
704 lt_dlclose(_handle);
705 if (lt_dlexit() != 0) {
706 return NULL;
707 }
708
709 return size;
710 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26