/[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.1.1.1 - (show annotations) (vendor branch)
Mon May 22 13:07:53 2000 UTC (23 years, 11 months ago) by nmav
Branch: mcrypt
CVS Tags: start
Changes since 1.1: +0 -0 lines
File MIME type: text/plain

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26