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 |
|
|
/* $Id: mcrypt.c,v 1.25 2000/03/31 16:45:28 nikos 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 |
|
|
#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 |
|
|
int internal_end_mcrypt(MCRYPT td); |
52 |
|
|
|
53 |
|
|
int internal_init_mcrypt(MCRYPT td, void *key, int lenofkey, void *IV) |
54 |
|
|
{ |
55 |
|
|
int *sizes = NULL; |
56 |
|
|
int num_of_sizes, i, ok = 0; |
57 |
|
|
|
58 |
|
|
if (lenofkey == 0) { |
59 |
|
|
return MCRYPT_KEY_LEN_ERROR; /* error */ |
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
sizes = mcrypt_enc_get_supported_key_sizes(td, &num_of_sizes); |
63 |
|
|
if (sizes != NULL) { |
64 |
|
|
for (i = 0; i < num_of_sizes; i++) { |
65 |
|
|
if (lenofkey == sizes[i]) { |
66 |
|
|
ok = 1; |
67 |
|
|
break; |
68 |
|
|
} |
69 |
|
|
} |
70 |
|
|
} else { /* sizes==NULL */ |
71 |
|
|
if (num_of_sizes == 0 |
72 |
|
|
&& lenofkey <= mcrypt_enc_get_key_size(td)) |
73 |
|
|
ok = 1; |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
if (ok == 0) { |
77 |
|
|
free(sizes); |
78 |
|
|
return MCRYPT_KEY_LEN_ERROR; |
79 |
|
|
} |
80 |
|
|
free(sizes); |
81 |
|
|
|
82 |
|
|
td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td)); |
83 |
|
|
memmove(td->keyword_given, key, lenofkey); |
84 |
|
|
td->akey = mxcalloc(1, mcrypt_get_size(td)); |
85 |
|
|
td->abuf = mxcalloc(1, mcrypt_mode_get_size(td)); |
86 |
|
|
|
87 |
|
|
init_mcrypt(td, td->abuf, key, lenofkey, IV); |
88 |
|
|
|
89 |
|
|
mcrypt_set_key(td, |
90 |
|
|
(void *) td->akey, |
91 |
|
|
(void *) td->keyword_given, |
92 |
|
|
lenofkey, IV, mcrypt_enc_get_iv_size(td)); |
93 |
|
|
|
94 |
|
|
if (td->akey == NULL) { |
95 |
|
|
internal_end_mcrypt(td); |
96 |
|
|
return MCRYPT_UNKNOWN_ERROR; |
97 |
|
|
} |
98 |
|
|
return 0; |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
int internal_end_mcrypt(MCRYPT td) |
102 |
|
|
{ |
103 |
|
|
mxfree(td->keyword_given, mcrypt_enc_get_key_size(td)); |
104 |
|
|
td->keyword_given = NULL; |
105 |
|
|
|
106 |
|
|
mxfree(td->akey, mcrypt_get_size(td)); |
107 |
|
|
td->akey = NULL; |
108 |
|
|
|
109 |
|
|
end_mcrypt(td, td->abuf); |
110 |
|
|
mxfree(td->abuf, mcrypt_mode_get_size(td)); |
111 |
|
|
td->abuf = NULL; |
112 |
|
|
|
113 |
|
|
return 0; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* Generic - High level functions */ |
117 |
|
|
|
118 |
|
|
int mcrypt_generic_init(const MCRYPT td, void *key, int lenofkey, void *IV) |
119 |
|
|
{ |
120 |
|
|
return internal_init_mcrypt(td, key, lenofkey, IV); |
121 |
|
|
} |
122 |
|
|
|
123 |
|
|
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 |
|
|
int mdecrypt_generic(MCRYPT td, void *ciphertext, int len) |
132 |
|
|
{ |
133 |
|
|
int x; |
134 |
|
|
x = mdecrypt(td, td->abuf, ciphertext, len); |
135 |
|
|
return x; |
136 |
|
|
} |
137 |
|
|
|
138 |
|
|
int mcrypt_generic_end(const MCRYPT td) |
139 |
|
|
{ |
140 |
|
|
internal_end_mcrypt(td); |
141 |
|
|
mcrypt_module_close(td); |
142 |
|
|
return 0; |
143 |
|
|
} |
144 |
|
|
|
145 |
|
|
|
146 |
|
|
void mcrypt_perror(int err) |
147 |
|
|
{ |
148 |
|
|
|
149 |
|
|
switch (err) { |
150 |
|
|
case MCRYPT_UNKNOWN_ERROR: |
151 |
|
|
fprintf(stderr, "Unknown error.\n"); |
152 |
|
|
break; |
153 |
|
|
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
154 |
|
|
fprintf(stderr, |
155 |
|
|
"Algorithm incompatible with this mode.\n"); |
156 |
|
|
break; |
157 |
|
|
case MCRYPT_KEY_LEN_ERROR: |
158 |
|
|
fprintf(stderr, "Key length is not legal.\n"); |
159 |
|
|
break; |
160 |
|
|
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
161 |
|
|
fprintf(stderr, "Memory allocation failed.\n"); |
162 |
|
|
break; |
163 |
|
|
case MCRYPT_UNKNOWN_MODE: |
164 |
|
|
fprintf(stderr, "Unknown mode.\n"); |
165 |
|
|
break; |
166 |
|
|
case MCRYPT_UNKNOWN_ALGORITHM: |
167 |
|
|
fprintf(stderr, "Unknown algorithm.\n"); |
168 |
|
|
break; |
169 |
|
|
|
170 |
|
|
} |
171 |
|
|
return; |
172 |
|
|
} |
173 |
|
|
|
174 |
|
|
int mcrypt_free(void *ptr) |
175 |
|
|
{ |
176 |
|
|
free(ptr); |
177 |
|
|
return 0; |
178 |
|
|
} |