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 |
/* $Id: mcrypt.c,v 1.12 2001/05/17 18:58:20 nmav Exp $ */ |
21 |
|
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 |
/* The above comments are too old! */ |
40 |
|
41 |
#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 |
static int internal_end_mcrypt(MCRYPT td); |
54 |
|
55 |
static int internal_init_mcrypt(MCRYPT td, void *key, int lenofkey, void *IV) |
56 |
{ |
57 |
const int *sizes = NULL; |
58 |
int num_of_sizes, i, ok = 0; |
59 |
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 |
} |
64 |
|
65 |
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 |
|
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 |
} else { /* well every key size is supported! */ |
90 |
key_size = lenofkey; |
91 |
} |
92 |
} else { |
93 |
key_size = lenofkey; |
94 |
} |
95 |
|
96 |
td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td)); |
97 |
if (td->keyword_given==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR; |
98 |
|
99 |
memmove(td->keyword_given, key, lenofkey); |
100 |
i = mcrypt_get_size(td); |
101 |
td->akey = mxcalloc(1, i); |
102 |
if (td->akey==NULL) { |
103 |
free(td->keyword_given); |
104 |
return MCRYPT_MEMORY_ALLOCATION_ERROR; |
105 |
} |
106 |
i = mcrypt_mode_get_size(td); |
107 |
if (i > 0) { |
108 |
td->abuf = mxcalloc(1, i); |
109 |
if (td->abuf==NULL) { |
110 |
free(td->keyword_given); |
111 |
free(td->akey); |
112 |
return MCRYPT_MEMORY_ALLOCATION_ERROR; |
113 |
} |
114 |
} |
115 |
ok = init_mcrypt(td, td->abuf, key, key_size, IV); |
116 |
if (ok!=0) { |
117 |
free(td->keyword_given); |
118 |
free(td->akey); |
119 |
free(td->abuf); |
120 |
return MCRYPT_UNKNOWN_ERROR; /* algorithm error */ |
121 |
} |
122 |
|
123 |
ok = mcrypt_set_key(td, |
124 |
(void *) td->akey, |
125 |
(void *) td->keyword_given, |
126 |
key_size, IV, IV!=NULL ? mcrypt_enc_get_iv_size(td) : 0); |
127 |
|
128 |
if (ok!=0) { |
129 |
internal_end_mcrypt(td); |
130 |
return MCRYPT_UNKNOWN_ERROR; /* algorithm error */ |
131 |
} |
132 |
|
133 |
return 0; |
134 |
} |
135 |
|
136 |
static int internal_end_mcrypt(MCRYPT td) |
137 |
{ |
138 |
mxfree(td->keyword_given, mcrypt_enc_get_key_size(td)); |
139 |
td->keyword_given = NULL; |
140 |
|
141 |
mxfree(td->akey, mcrypt_get_size(td)); |
142 |
td->akey = NULL; |
143 |
|
144 |
end_mcrypt(td, td->abuf); |
145 |
if (td->abuf!=NULL) mxfree(td->abuf, mcrypt_mode_get_size(td)); |
146 |
td->abuf = NULL; |
147 |
|
148 |
return 0; |
149 |
} |
150 |
|
151 |
/* Generic - High level functions */ |
152 |
|
153 |
WIN32DLL_DEFINE |
154 |
int mcrypt_generic_init(const MCRYPT td, void *key, int lenofkey, void *IV) |
155 |
{ |
156 |
return internal_init_mcrypt(td, key, lenofkey, IV); |
157 |
} |
158 |
|
159 |
WIN32DLL_DEFINE |
160 |
int mcrypt_generic(MCRYPT td, void *plaintext, int len) |
161 |
{ |
162 |
int x; |
163 |
|
164 |
x = mcrypt(td, td->abuf, plaintext, len); |
165 |
return x; |
166 |
} |
167 |
|
168 |
WIN32DLL_DEFINE |
169 |
int mdecrypt_generic(MCRYPT td, void *ciphertext, int len) |
170 |
{ |
171 |
int x; |
172 |
x = mdecrypt(td, td->abuf, ciphertext, len); |
173 |
return x; |
174 |
} |
175 |
|
176 |
WIN32DLL_DEFINE |
177 |
int mcrypt_generic_end(const MCRYPT td) |
178 |
{ |
179 |
internal_end_mcrypt(td); |
180 |
mcrypt_module_close(td); |
181 |
return 0; |
182 |
} |
183 |
|
184 |
WIN32DLL_DEFINE |
185 |
int mcrypt_generic_deinit(const MCRYPT td) |
186 |
{ |
187 |
internal_end_mcrypt(td); |
188 |
return 0; |
189 |
} |
190 |
|
191 |
WIN32DLL_DEFINE |
192 |
void mcrypt_perror(int err) |
193 |
{ |
194 |
|
195 |
switch (err) { |
196 |
case MCRYPT_UNKNOWN_ERROR: |
197 |
fprintf(stderr, "Unknown error.\n"); |
198 |
break; |
199 |
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
200 |
fprintf(stderr, |
201 |
"Algorithm incompatible with this mode.\n"); |
202 |
break; |
203 |
case MCRYPT_KEY_LEN_ERROR: |
204 |
fprintf(stderr, "Key length is not legal.\n"); |
205 |
break; |
206 |
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
207 |
fprintf(stderr, "Memory allocation failed.\n"); |
208 |
break; |
209 |
case MCRYPT_UNKNOWN_MODE: |
210 |
fprintf(stderr, "Unknown mode.\n"); |
211 |
break; |
212 |
case MCRYPT_UNKNOWN_ALGORITHM: |
213 |
fprintf(stderr, "Unknown algorithm.\n"); |
214 |
break; |
215 |
|
216 |
} |
217 |
return; |
218 |
} |
219 |
|
220 |
WIN32DLL_DEFINE |
221 |
const char* mcrypt_strerror(int err) |
222 |
{ |
223 |
|
224 |
switch (err) { |
225 |
case MCRYPT_UNKNOWN_ERROR: |
226 |
return "Unknown error.\n"; |
227 |
break; |
228 |
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
229 |
return "Algorithm incompatible with this mode.\n"; |
230 |
break; |
231 |
case MCRYPT_KEY_LEN_ERROR: |
232 |
return "Key length is not legal.\n"; |
233 |
break; |
234 |
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
235 |
return "Memory allocation failed.\n"; |
236 |
break; |
237 |
case MCRYPT_UNKNOWN_MODE: |
238 |
return "Unknown mode.\n"; |
239 |
break; |
240 |
case MCRYPT_UNKNOWN_ALGORITHM: |
241 |
return "Unknown algorithm.\n"; |
242 |
break; |
243 |
|
244 |
} |
245 |
return NULL; |
246 |
} |
247 |
|
248 |
WIN32DLL_DEFINE |
249 |
int mcrypt_free(void *ptr) |
250 |
{ |
251 |
free(ptr); |
252 |
return 0; |
253 |
} |