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.4 |
/* $Id: mcrypt.c,v 1.3 2000/10/12 21:06:31 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 |
|
|
#ifndef LIBDEFS_H |
40 |
|
|
#define LIBDEFS_H |
41 |
|
|
#include <libdefs.h> |
42 |
|
|
#endif |
43 |
|
|
#include <bzero.h> |
44 |
|
|
#include <xmemory.h> |
45 |
|
|
#include <mcrypt_internal.h> |
46 |
|
|
|
47 |
|
|
#if 0 |
48 |
|
|
static int preloaded_symbols = 0; |
49 |
|
|
#endif |
50 |
|
|
|
51 |
nmav |
1.2 |
static int internal_end_mcrypt(MCRYPT td); |
52 |
nmav |
1.1 |
|
53 |
nmav |
1.2 |
static int internal_init_mcrypt(MCRYPT td, void *key, int lenofkey, void *IV) |
54 |
nmav |
1.1 |
{ |
55 |
|
|
int *sizes = NULL; |
56 |
|
|
int num_of_sizes, i, ok = 0; |
57 |
|
|
if (lenofkey == 0) { |
58 |
|
|
return MCRYPT_KEY_LEN_ERROR; /* error */ |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
sizes = mcrypt_enc_get_supported_key_sizes(td, &num_of_sizes); |
62 |
|
|
if (sizes != NULL) { |
63 |
|
|
for (i = 0; i < num_of_sizes; i++) { |
64 |
|
|
if (lenofkey == sizes[i]) { |
65 |
|
|
ok = 1; |
66 |
|
|
break; |
67 |
|
|
} |
68 |
|
|
} |
69 |
|
|
} else { /* sizes==NULL */ |
70 |
|
|
if (num_of_sizes == 0 |
71 |
|
|
&& lenofkey <= mcrypt_enc_get_key_size(td)) |
72 |
|
|
ok = 1; |
73 |
|
|
} |
74 |
|
|
|
75 |
nmav |
1.2 |
free(sizes); |
76 |
nmav |
1.1 |
if (ok == 0) { |
77 |
|
|
return MCRYPT_KEY_LEN_ERROR; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td)); |
81 |
|
|
memmove(td->keyword_given, key, lenofkey); |
82 |
|
|
td->akey = mxcalloc(1, mcrypt_get_size(td)); |
83 |
|
|
td->abuf = mxcalloc(1, mcrypt_mode_get_size(td)); |
84 |
|
|
|
85 |
|
|
init_mcrypt(td, td->abuf, key, lenofkey, IV); |
86 |
|
|
|
87 |
|
|
mcrypt_set_key(td, |
88 |
|
|
(void *) td->akey, |
89 |
|
|
(void *) td->keyword_given, |
90 |
|
|
lenofkey, IV, mcrypt_enc_get_iv_size(td)); |
91 |
|
|
|
92 |
|
|
if (td->akey == NULL) { |
93 |
|
|
internal_end_mcrypt(td); |
94 |
|
|
return MCRYPT_UNKNOWN_ERROR; |
95 |
|
|
} |
96 |
|
|
return 0; |
97 |
|
|
} |
98 |
|
|
|
99 |
nmav |
1.3 |
static int internal_end_mcrypt(MCRYPT td) |
100 |
nmav |
1.1 |
{ |
101 |
|
|
mxfree(td->keyword_given, mcrypt_enc_get_key_size(td)); |
102 |
|
|
td->keyword_given = NULL; |
103 |
|
|
|
104 |
|
|
mxfree(td->akey, mcrypt_get_size(td)); |
105 |
|
|
td->akey = NULL; |
106 |
|
|
|
107 |
|
|
end_mcrypt(td, td->abuf); |
108 |
|
|
mxfree(td->abuf, mcrypt_mode_get_size(td)); |
109 |
|
|
td->abuf = NULL; |
110 |
|
|
|
111 |
|
|
return 0; |
112 |
|
|
} |
113 |
|
|
|
114 |
|
|
/* Generic - High level functions */ |
115 |
|
|
|
116 |
nmav |
1.3 |
WIN32DLL_DEFINE |
117 |
nmav |
1.1 |
int mcrypt_generic_init(const MCRYPT td, void *key, int lenofkey, void *IV) |
118 |
|
|
{ |
119 |
|
|
return internal_init_mcrypt(td, key, lenofkey, IV); |
120 |
|
|
} |
121 |
|
|
|
122 |
nmav |
1.3 |
WIN32DLL_DEFINE |
123 |
nmav |
1.1 |
int mcrypt_generic(MCRYPT td, void *plaintext, int len) |
124 |
|
|
{ |
125 |
|
|
int x; |
126 |
|
|
|
127 |
|
|
x = mcrypt(td, td->abuf, plaintext, len); |
128 |
|
|
return x; |
129 |
|
|
} |
130 |
|
|
|
131 |
nmav |
1.3 |
WIN32DLL_DEFINE |
132 |
nmav |
1.1 |
int mdecrypt_generic(MCRYPT td, void *ciphertext, int len) |
133 |
|
|
{ |
134 |
|
|
int x; |
135 |
|
|
x = mdecrypt(td, td->abuf, ciphertext, len); |
136 |
|
|
return x; |
137 |
|
|
} |
138 |
|
|
|
139 |
nmav |
1.3 |
WIN32DLL_DEFINE |
140 |
nmav |
1.1 |
int mcrypt_generic_end(const MCRYPT td) |
141 |
|
|
{ |
142 |
|
|
internal_end_mcrypt(td); |
143 |
|
|
mcrypt_module_close(td); |
144 |
|
|
return 0; |
145 |
|
|
} |
146 |
|
|
|
147 |
nmav |
1.3 |
WIN32DLL_DEFINE |
148 |
nmav |
1.1 |
void mcrypt_perror(int err) |
149 |
|
|
{ |
150 |
|
|
|
151 |
|
|
switch (err) { |
152 |
|
|
case MCRYPT_UNKNOWN_ERROR: |
153 |
|
|
fprintf(stderr, "Unknown error.\n"); |
154 |
|
|
break; |
155 |
|
|
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
156 |
|
|
fprintf(stderr, |
157 |
|
|
"Algorithm incompatible with this mode.\n"); |
158 |
|
|
break; |
159 |
|
|
case MCRYPT_KEY_LEN_ERROR: |
160 |
|
|
fprintf(stderr, "Key length is not legal.\n"); |
161 |
|
|
break; |
162 |
|
|
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
163 |
|
|
fprintf(stderr, "Memory allocation failed.\n"); |
164 |
|
|
break; |
165 |
|
|
case MCRYPT_UNKNOWN_MODE: |
166 |
|
|
fprintf(stderr, "Unknown mode.\n"); |
167 |
|
|
break; |
168 |
|
|
case MCRYPT_UNKNOWN_ALGORITHM: |
169 |
|
|
fprintf(stderr, "Unknown algorithm.\n"); |
170 |
|
|
break; |
171 |
|
|
|
172 |
|
|
} |
173 |
|
|
return; |
174 |
nmav |
1.4 |
} |
175 |
|
|
|
176 |
|
|
WIN32DLL_DEFINE |
177 |
|
|
char* mcrypt_strerror(int err) |
178 |
|
|
{ |
179 |
|
|
char* cerr=malloc(256); |
180 |
|
|
|
181 |
|
|
switch (err) { |
182 |
|
|
case MCRYPT_UNKNOWN_ERROR: |
183 |
|
|
sprintf(cerr, "Unknown error.\n"); |
184 |
|
|
break; |
185 |
|
|
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
186 |
|
|
sprintf(cerr, |
187 |
|
|
"Algorithm incompatible with this mode.\n"); |
188 |
|
|
break; |
189 |
|
|
case MCRYPT_KEY_LEN_ERROR: |
190 |
|
|
sprintf(cerr, "Key length is not legal.\n"); |
191 |
|
|
break; |
192 |
|
|
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
193 |
|
|
sprintf(cerr, "Memory allocation failed.\n"); |
194 |
|
|
break; |
195 |
|
|
case MCRYPT_UNKNOWN_MODE: |
196 |
|
|
sprintf(cerr, "Unknown mode.\n"); |
197 |
|
|
break; |
198 |
|
|
case MCRYPT_UNKNOWN_ALGORITHM: |
199 |
|
|
sprintf(cerr, "Unknown algorithm.\n"); |
200 |
|
|
break; |
201 |
|
|
|
202 |
|
|
} |
203 |
|
|
return cerr; |
204 |
nmav |
1.1 |
} |
205 |
|
|
|
206 |
nmav |
1.3 |
WIN32DLL_DEFINE |
207 |
nmav |
1.1 |
int mcrypt_free(void *ptr) |
208 |
|
|
{ |
209 |
|
|
free(ptr); |
210 |
|
|
return 0; |
211 |
|
|
} |