/[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.18 - (show annotations)
Sun Sep 2 20:11:45 2001 UTC (22 years, 7 months ago) by nmav
Branch: MAIN
Changes since 1.17: +9 -20 lines
File MIME type: text/plain
added option to include modules into the library

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26