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.6 2001/01/17 19:37:56 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 |
int *sizes = NULL; |
58 |
int num_of_sizes, i, ok = 0; |
59 |
int key_size; |
60 |
|
61 |
if (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 |
if (sizes!=NULL) free(sizes); |
97 |
|
98 |
if (lenofkey > key_size) { |
99 |
return MCRYPT_KEY_LEN_ERROR; /* error */ |
100 |
} |
101 |
|
102 |
td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td)); |
103 |
memmove(td->keyword_given, key, lenofkey); |
104 |
td->akey = mxcalloc(1, mcrypt_get_size(td)); |
105 |
td->abuf = mxcalloc(1, mcrypt_mode_get_size(td)); |
106 |
|
107 |
init_mcrypt(td, td->abuf, key, key_size, IV); |
108 |
|
109 |
mcrypt_set_key(td, |
110 |
(void *) td->akey, |
111 |
(void *) td->keyword_given, |
112 |
key_size, IV, mcrypt_enc_get_iv_size(td)); |
113 |
|
114 |
if (td->akey == NULL) { |
115 |
internal_end_mcrypt(td); |
116 |
return MCRYPT_UNKNOWN_ERROR; |
117 |
} |
118 |
return 0; |
119 |
} |
120 |
|
121 |
static int internal_end_mcrypt(MCRYPT td) |
122 |
{ |
123 |
mxfree(td->keyword_given, mcrypt_enc_get_key_size(td)); |
124 |
td->keyword_given = NULL; |
125 |
|
126 |
mxfree(td->akey, mcrypt_get_size(td)); |
127 |
td->akey = NULL; |
128 |
|
129 |
end_mcrypt(td, td->abuf); |
130 |
mxfree(td->abuf, mcrypt_mode_get_size(td)); |
131 |
td->abuf = NULL; |
132 |
|
133 |
return 0; |
134 |
} |
135 |
|
136 |
/* Generic - High level functions */ |
137 |
|
138 |
WIN32DLL_DEFINE |
139 |
int mcrypt_generic_init(const MCRYPT td, void *key, int lenofkey, void *IV) |
140 |
{ |
141 |
return internal_init_mcrypt(td, key, lenofkey, IV); |
142 |
} |
143 |
|
144 |
WIN32DLL_DEFINE |
145 |
int mcrypt_generic(MCRYPT td, void *plaintext, int len) |
146 |
{ |
147 |
int x; |
148 |
|
149 |
x = mcrypt(td, td->abuf, plaintext, len); |
150 |
return x; |
151 |
} |
152 |
|
153 |
WIN32DLL_DEFINE |
154 |
int mdecrypt_generic(MCRYPT td, void *ciphertext, int len) |
155 |
{ |
156 |
int x; |
157 |
x = mdecrypt(td, td->abuf, ciphertext, len); |
158 |
return x; |
159 |
} |
160 |
|
161 |
WIN32DLL_DEFINE |
162 |
int mcrypt_generic_end(const MCRYPT td) |
163 |
{ |
164 |
internal_end_mcrypt(td); |
165 |
mcrypt_module_close(td); |
166 |
return 0; |
167 |
} |
168 |
|
169 |
WIN32DLL_DEFINE |
170 |
void mcrypt_perror(int err) |
171 |
{ |
172 |
|
173 |
switch (err) { |
174 |
case MCRYPT_UNKNOWN_ERROR: |
175 |
fprintf(stderr, "Unknown error.\n"); |
176 |
break; |
177 |
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
178 |
fprintf(stderr, |
179 |
"Algorithm incompatible with this mode.\n"); |
180 |
break; |
181 |
case MCRYPT_KEY_LEN_ERROR: |
182 |
fprintf(stderr, "Key length is not legal.\n"); |
183 |
break; |
184 |
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
185 |
fprintf(stderr, "Memory allocation failed.\n"); |
186 |
break; |
187 |
case MCRYPT_UNKNOWN_MODE: |
188 |
fprintf(stderr, "Unknown mode.\n"); |
189 |
break; |
190 |
case MCRYPT_UNKNOWN_ALGORITHM: |
191 |
fprintf(stderr, "Unknown algorithm.\n"); |
192 |
break; |
193 |
|
194 |
} |
195 |
return; |
196 |
} |
197 |
|
198 |
WIN32DLL_DEFINE |
199 |
char* mcrypt_strerror(int err) |
200 |
{ |
201 |
char* cerr=malloc(256); |
202 |
|
203 |
switch (err) { |
204 |
case MCRYPT_UNKNOWN_ERROR: |
205 |
sprintf(cerr, "Unknown error.\n"); |
206 |
break; |
207 |
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
208 |
sprintf(cerr, |
209 |
"Algorithm incompatible with this mode.\n"); |
210 |
break; |
211 |
case MCRYPT_KEY_LEN_ERROR: |
212 |
sprintf(cerr, "Key length is not legal.\n"); |
213 |
break; |
214 |
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
215 |
sprintf(cerr, "Memory allocation failed.\n"); |
216 |
break; |
217 |
case MCRYPT_UNKNOWN_MODE: |
218 |
sprintf(cerr, "Unknown mode.\n"); |
219 |
break; |
220 |
case MCRYPT_UNKNOWN_ALGORITHM: |
221 |
sprintf(cerr, "Unknown algorithm.\n"); |
222 |
break; |
223 |
|
224 |
} |
225 |
return cerr; |
226 |
} |
227 |
|
228 |
WIN32DLL_DEFINE |
229 |
int mcrypt_free(void *ptr) |
230 |
{ |
231 |
free(ptr); |
232 |
return 0; |
233 |
} |