/[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.4 - (show annotations)
Mon Sep 18 09:42:54 2000 UTC (23 years, 7 months ago) by nmav
Branch: MAIN
Changes since 1.3: +51 -21 lines
File MIME type: text/plain
updates in the -ldl option. Bugfixes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26