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

Annotation of /libmcrypt/lib/mcrypt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations)
Thu May 17 18:58:20 2001 UTC (22 years, 11 months ago) by nmav
Branch: MAIN
CVS Tags: libmcrypt_2_4_15, mcrypt_2_4_12, mcrypt_2_4_13
Changes since 1.11: +9 -6 lines
File MIME type: text/plain
fixes to work with the new libltdl

1 nmav 1.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 nmav 1.12 /* $Id: mcrypt.c,v 1.11 2001/02/01 10:34:51 nmav Exp $ */
21 nmav 1.1
22     /* Changed by Steve Underwood 1999/12/10 to allow an arbitrary number of
23     * streams of encryption. Currently the resulting code is probably not
24     * thread safe, but as far as I could tell the previous code wasn't
25     * either. This version has brute force locking in a lot of places, but
26     * it has not been tested in a multi-threaded manner.
27     * The key locking issue is that the table of encryption streams could
28     * be moved when it is extended. Any address pre-calculated, or in
29     * calculation at the time of the reallocation would be screwed.
30     * This won't happen often, but requires lots of locks - PITA!
31     */
32    
33     /* Changed again at 1999/12/15 to correct the thread safeness. Now it
34     * seems to be thread safe. Brute force locking was removed and
35     * locks per thread were introduced.
36     * --nikos
37     */
38    
39 nmav 1.5 /* The above comments are too old! */
40    
41 nmav 1.1 #ifndef LIBDEFS_H
42     #define LIBDEFS_H
43     #include <libdefs.h>
44     #endif
45     #include <bzero.h>
46     #include <xmemory.h>
47     #include <mcrypt_internal.h>
48    
49     #if 0
50     static int preloaded_symbols = 0;
51     #endif
52    
53 nmav 1.2 static int internal_end_mcrypt(MCRYPT td);
54 nmav 1.1
55 nmav 1.2 static int internal_init_mcrypt(MCRYPT td, void *key, int lenofkey, void *IV)
56 nmav 1.1 {
57     int *sizes = NULL;
58     int num_of_sizes, i, ok = 0;
59 nmav 1.9 int key_size = mcrypt_enc_get_key_size(td);
60    
61     if (lenofkey > key_size || lenofkey==0) {
62     return MCRYPT_KEY_LEN_ERROR; /* error */
63 nmav 1.1 }
64 nmav 1.6
65 nmav 1.1 sizes = mcrypt_enc_get_supported_key_sizes(td, &num_of_sizes);
66     if (sizes != NULL) {
67     for (i = 0; i < num_of_sizes; i++) {
68     if (lenofkey == sizes[i]) {
69     ok = 1;
70     break;
71     }
72     }
73     } else { /* sizes==NULL */
74     if (num_of_sizes == 0
75     && lenofkey <= mcrypt_enc_get_key_size(td))
76     ok = 1;
77     }
78    
79 nmav 1.5
80     if (ok == 0) { /* not supported key size */
81     key_size = mcrypt_enc_get_key_size(td);
82     if (sizes != NULL) {
83     for (i = 0; i < num_of_sizes; i++) {
84     if (lenofkey <= sizes[i]) {
85     key_size = sizes[i];
86     break;
87     }
88     }
89 nmav 1.7 } else { /* well every key size is supported! */
90     key_size = lenofkey;
91 nmav 1.5 }
92     } else {
93     key_size = lenofkey;
94 nmav 1.6 }
95 nmav 1.7
96     if (sizes!=NULL) free(sizes);
97 nmav 1.6
98 nmav 1.1 td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td));
99 nmav 1.9 if (td->keyword_given==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR;
100    
101 nmav 1.1 memmove(td->keyword_given, key, lenofkey);
102 nmav 1.12 i = mcrypt_get_size(td);
103     td->akey = mxcalloc(1, i);
104 nmav 1.9 if (td->akey==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR;
105    
106 nmav 1.12 i = mcrypt_mode_get_size(td);
107     if (i > 0) {
108     td->abuf = mxcalloc(1, i);
109     if (td->abuf==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR;
110     }
111 nmav 1.5 init_mcrypt(td, td->abuf, key, key_size, IV);
112 nmav 1.1
113 nmav 1.8 ok = mcrypt_set_key(td,
114 nmav 1.1 (void *) td->akey,
115     (void *) td->keyword_given,
116 nmav 1.11 key_size, IV, IV!=NULL ? mcrypt_enc_get_iv_size(td) : 0);
117 nmav 1.8
118     if (ok!=0) return MCRYPT_UNKNOWN_ERROR; /* algorithm error */
119 nmav 1.1
120     if (td->akey == NULL) {
121     internal_end_mcrypt(td);
122     return MCRYPT_UNKNOWN_ERROR;
123     }
124     return 0;
125     }
126    
127 nmav 1.3 static int internal_end_mcrypt(MCRYPT td)
128 nmav 1.1 {
129     mxfree(td->keyword_given, mcrypt_enc_get_key_size(td));
130     td->keyword_given = NULL;
131    
132     mxfree(td->akey, mcrypt_get_size(td));
133     td->akey = NULL;
134    
135     end_mcrypt(td, td->abuf);
136 nmav 1.12 if (td->abuf!=NULL) mxfree(td->abuf, mcrypt_mode_get_size(td));
137 nmav 1.1 td->abuf = NULL;
138    
139     return 0;
140     }
141    
142     /* Generic - High level functions */
143    
144 nmav 1.3 WIN32DLL_DEFINE
145 nmav 1.1 int mcrypt_generic_init(const MCRYPT td, void *key, int lenofkey, void *IV)
146     {
147     return internal_init_mcrypt(td, key, lenofkey, IV);
148     }
149    
150 nmav 1.3 WIN32DLL_DEFINE
151 nmav 1.1 int mcrypt_generic(MCRYPT td, void *plaintext, int len)
152     {
153     int x;
154    
155     x = mcrypt(td, td->abuf, plaintext, len);
156     return x;
157     }
158    
159 nmav 1.3 WIN32DLL_DEFINE
160 nmav 1.1 int mdecrypt_generic(MCRYPT td, void *ciphertext, int len)
161     {
162     int x;
163     x = mdecrypt(td, td->abuf, ciphertext, len);
164     return x;
165     }
166    
167 nmav 1.3 WIN32DLL_DEFINE
168 nmav 1.1 int mcrypt_generic_end(const MCRYPT td)
169     {
170     internal_end_mcrypt(td);
171     mcrypt_module_close(td);
172 nmav 1.10 return 0;
173     }
174    
175     WIN32DLL_DEFINE
176     int mcrypt_generic_deinit(const MCRYPT td)
177     {
178     internal_end_mcrypt(td);
179 nmav 1.1 return 0;
180     }
181    
182 nmav 1.3 WIN32DLL_DEFINE
183 nmav 1.1 void mcrypt_perror(int err)
184     {
185    
186     switch (err) {
187     case MCRYPT_UNKNOWN_ERROR:
188     fprintf(stderr, "Unknown error.\n");
189     break;
190     case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY:
191     fprintf(stderr,
192     "Algorithm incompatible with this mode.\n");
193     break;
194     case MCRYPT_KEY_LEN_ERROR:
195     fprintf(stderr, "Key length is not legal.\n");
196     break;
197     case MCRYPT_MEMORY_ALLOCATION_ERROR:
198     fprintf(stderr, "Memory allocation failed.\n");
199     break;
200     case MCRYPT_UNKNOWN_MODE:
201     fprintf(stderr, "Unknown mode.\n");
202     break;
203     case MCRYPT_UNKNOWN_ALGORITHM:
204     fprintf(stderr, "Unknown algorithm.\n");
205     break;
206    
207     }
208     return;
209 nmav 1.4 }
210    
211     WIN32DLL_DEFINE
212     char* mcrypt_strerror(int err)
213     {
214     char* cerr=malloc(256);
215 nmav 1.9 if (cerr==NULL) return NULL;
216 nmav 1.4
217     switch (err) {
218     case MCRYPT_UNKNOWN_ERROR:
219     sprintf(cerr, "Unknown error.\n");
220     break;
221     case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY:
222     sprintf(cerr,
223     "Algorithm incompatible with this mode.\n");
224     break;
225     case MCRYPT_KEY_LEN_ERROR:
226     sprintf(cerr, "Key length is not legal.\n");
227     break;
228     case MCRYPT_MEMORY_ALLOCATION_ERROR:
229     sprintf(cerr, "Memory allocation failed.\n");
230     break;
231     case MCRYPT_UNKNOWN_MODE:
232     sprintf(cerr, "Unknown mode.\n");
233     break;
234     case MCRYPT_UNKNOWN_ALGORITHM:
235     sprintf(cerr, "Unknown algorithm.\n");
236     break;
237    
238     }
239     return cerr;
240 nmav 1.1 }
241    
242 nmav 1.3 WIN32DLL_DEFINE
243 nmav 1.1 int mcrypt_free(void *ptr)
244     {
245     free(ptr);
246     return 0;
247     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26