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.1.1.1 2000/05/22 13:07:52 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 |
#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 |
static int internal_end_mcrypt(MCRYPT td); |
52 |
|
53 |
static 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 |
free(sizes); |
77 |
if (ok == 0) { |
78 |
return MCRYPT_KEY_LEN_ERROR; |
79 |
} |
80 |
|
81 |
td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td)); |
82 |
memmove(td->keyword_given, key, lenofkey); |
83 |
td->akey = mxcalloc(1, mcrypt_get_size(td)); |
84 |
td->abuf = mxcalloc(1, mcrypt_mode_get_size(td)); |
85 |
|
86 |
init_mcrypt(td, td->abuf, key, lenofkey, IV); |
87 |
|
88 |
mcrypt_set_key(td, |
89 |
(void *) td->akey, |
90 |
(void *) td->keyword_given, |
91 |
lenofkey, IV, mcrypt_enc_get_iv_size(td)); |
92 |
|
93 |
if (td->akey == NULL) { |
94 |
internal_end_mcrypt(td); |
95 |
return MCRYPT_UNKNOWN_ERROR; |
96 |
} |
97 |
return 0; |
98 |
} |
99 |
|
100 |
int internal_end_mcrypt(MCRYPT td) |
101 |
{ |
102 |
mxfree(td->keyword_given, mcrypt_enc_get_key_size(td)); |
103 |
td->keyword_given = NULL; |
104 |
|
105 |
mxfree(td->akey, mcrypt_get_size(td)); |
106 |
td->akey = NULL; |
107 |
|
108 |
end_mcrypt(td, td->abuf); |
109 |
mxfree(td->abuf, mcrypt_mode_get_size(td)); |
110 |
td->abuf = NULL; |
111 |
|
112 |
return 0; |
113 |
} |
114 |
|
115 |
/* Generic - High level functions */ |
116 |
|
117 |
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 |
int mcrypt_generic(MCRYPT td, void *plaintext, int len) |
123 |
{ |
124 |
int x; |
125 |
|
126 |
x = mcrypt(td, td->abuf, plaintext, len); |
127 |
return x; |
128 |
} |
129 |
|
130 |
int mdecrypt_generic(MCRYPT td, void *ciphertext, int len) |
131 |
{ |
132 |
int x; |
133 |
x = mdecrypt(td, td->abuf, ciphertext, len); |
134 |
return x; |
135 |
} |
136 |
|
137 |
int mcrypt_generic_end(const MCRYPT td) |
138 |
{ |
139 |
internal_end_mcrypt(td); |
140 |
mcrypt_module_close(td); |
141 |
return 0; |
142 |
} |
143 |
|
144 |
|
145 |
void mcrypt_perror(int err) |
146 |
{ |
147 |
|
148 |
switch (err) { |
149 |
case MCRYPT_UNKNOWN_ERROR: |
150 |
fprintf(stderr, "Unknown error.\n"); |
151 |
break; |
152 |
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
153 |
fprintf(stderr, |
154 |
"Algorithm incompatible with this mode.\n"); |
155 |
break; |
156 |
case MCRYPT_KEY_LEN_ERROR: |
157 |
fprintf(stderr, "Key length is not legal.\n"); |
158 |
break; |
159 |
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
160 |
fprintf(stderr, "Memory allocation failed.\n"); |
161 |
break; |
162 |
case MCRYPT_UNKNOWN_MODE: |
163 |
fprintf(stderr, "Unknown mode.\n"); |
164 |
break; |
165 |
case MCRYPT_UNKNOWN_ALGORITHM: |
166 |
fprintf(stderr, "Unknown algorithm.\n"); |
167 |
break; |
168 |
|
169 |
} |
170 |
return; |
171 |
} |
172 |
|
173 |
int mcrypt_free(void *ptr) |
174 |
{ |
175 |
free(ptr); |
176 |
return 0; |
177 |
} |