/[mcrypt]/libmcrypt-nm/lib/mcrypt_extra.c
ViewVC logotype

Contents of /libmcrypt-nm/lib/mcrypt_extra.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Mon May 22 13:08:59 2000 UTC (23 years, 11 months ago) by nmav
Branch: MAIN, mcrypt
CVS Tags: start, HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/plain

1 /* Copyright (C) 1998,1999 Nikos Mavroyanopoulos This library is free
2 * software; you can redistribute it and/or modify it under the terms of the
3 * GNU Library General Public License as published by the Free Software
4 * Foundation; either version 2 of the License, or (at your option) any
5 * later version.
6 *
7 * This library is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * Library General Public License for more details.
11 *
12 * You should have received a copy of the GNU Library General Public
13 * License along with this library; if not, write to the
14 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
15 * Boston, MA 02111-1307, USA.
16 */
17
18 /* $Id: mcrypt_extra.c,v 1.7 1999/10/20 03:38:11 nmav Exp $ */
19
20 #ifndef LIBDEFS_H
21 #define LIBDEFS_H
22 #include <libdefs.h>
23 #endif
24 #include <bzero.h>
25 #include <swap.h>
26 #include <mcrypt_int.h>
27 #include <blowfish.h>
28 #include <3-way.h>
29 #include <des.h>
30 #include <safer.h>
31 #include <tean.h>
32 #include <xmemory.h>
33 #include <rc2.h>
34 #include <loki97.h>
35 #include <rijndael.h>
36 #include <ctype.h>
37
38 #define MCRYPT_ENTRY(name, blksize, keysize, block) \
39 { #name, name, blksize, keysize, block }
40
41 struct mcrypt_algorithm_entry {
42 char *name;
43 int id;
44 size_t blocksize;
45 size_t keysize;
46 int block;
47 };
48
49 static mcrypt_algorithm_entry algorithms[] = {
50 MCRYPT_ENTRY(MCRYPT_BLOWFISH, 8, 56, 1),
51 MCRYPT_ENTRY(MCRYPT_DES, 8, 8, 1),
52 MCRYPT_ENTRY(MCRYPT_TRIPLEDES, 8, 24, 1),
53 MCRYPT_ENTRY(MCRYPT_THREEWAY, 12, 12, 1),
54 MCRYPT_ENTRY(MCRYPT_GOST, 8, 32, 1),
55 MCRYPT_ENTRY(MCRYPT_SAFER_SK64, 8, 8, 1),
56 MCRYPT_ENTRY(MCRYPT_SAFER_SK128, 8, 16, 1),
57 MCRYPT_ENTRY(MCRYPT_CAST_128, 8, 16, 1),
58 MCRYPT_ENTRY(MCRYPT_XTEA, 8, 16, 1),
59 MCRYPT_ENTRY(MCRYPT_RC2, 8, 128, 1),
60 MCRYPT_ENTRY(MCRYPT_TWOFISH, 16, 32, 1),
61 MCRYPT_ENTRY(MCRYPT_CAST_256, 16, 32, 1),
62 MCRYPT_ENTRY(MCRYPT_SAFERPLUS, 16, 32, 1),
63 MCRYPT_ENTRY(MCRYPT_LOKI97, 16, 32, 1),
64 MCRYPT_ENTRY(MCRYPT_SERPENT, 16, 32, 1),
65 MCRYPT_ENTRY(MCRYPT_RIJNDAEL_128, 16, 32, 1),
66 MCRYPT_ENTRY(MCRYPT_ARCFOUR, 1, 256, 0),
67 MCRYPT_ENTRY(MCRYPT_ENIGMA, 1, 13, 0),
68 {0}
69 };
70
71 void tolow( char* str, int size) {
72 int i;
73
74 for(i=0;i<size;i++) {
75 str[i]=tolower(str[i]);
76 }
77 }
78
79 #define MCRYPT_LOOP(b) \
80 mcrypt_algorithm_entry *p; \
81 for(p = algorithms; p->name != NULL; p++) { b ; }
82
83 #define MCRYPT_ALG_LOOP(a) \
84 MCRYPT_LOOP( if(p->id == algorithm) { a; break; } )
85
86
87 #define MCRYPT_MODE_ENTRY(name, block_algo, IV, block) \
88 { #name, name, block_algo, IV, block }
89
90 struct mcrypt_mode_entry {
91 char *name;
92 int id;
93 int block_algo;
94 int IV;
95 int block;
96 };
97
98 static mcrypt_mode_entry modes[] = {
99 MCRYPT_MODE_ENTRY(MCRYPT_ECB, 1, 0, 1),
100 MCRYPT_MODE_ENTRY(MCRYPT_CBC, 1, 1, 1),
101 MCRYPT_MODE_ENTRY(MCRYPT_CFB, 1, 1, 0),
102 MCRYPT_MODE_ENTRY(MCRYPT_OFB, 1, 1, 0),
103 MCRYPT_MODE_ENTRY(MCRYPT_nOFB, 1, 1, 1),
104 MCRYPT_MODE_ENTRY(MCRYPT_STREAM, 0, 0, 0),
105 {0}
106 };
107
108
109 #define MCRYPT_MODE_LOOP(b) \
110 mcrypt_mode_entry *p; \
111 for(p = modes; p->name != NULL; p++) { b ; }
112
113 #define MCRYPT_MLOOP(a) \
114 MCRYPT_MODE_LOOP( if(p->id == mode) { a; break; } )
115
116
117 int mcrypt_mode_has_iv(int mode)
118 {
119 int ret = 0;
120
121 MCRYPT_MLOOP(ret = p->IV);
122 return ret;
123
124 }
125
126
127
128
129 int mcrypt_get_block_size(int algorithm)
130 {
131 size_t ret = 0;
132
133 MCRYPT_ALG_LOOP(ret = p->blocksize);
134 return ret;
135
136 }
137
138 int mcrypt_is_block_algorithm(int algorithm)
139 {
140 size_t ret = 0;
141
142 MCRYPT_ALG_LOOP(ret = p->block);
143 return ret;
144
145 }
146
147 int mcrypt_is_block_algorithm_mode(int mode)
148 {
149 int ret = 0;
150
151 MCRYPT_MLOOP(ret = p->block_algo);
152 return ret;
153
154 }
155
156 int mcrypt_is_block_mode(int mode)
157 {
158 int ret = 0;
159
160 MCRYPT_MLOOP(ret = p->block);
161 return ret;
162
163 }
164
165 int mcrypt_get_key_size(int algorithm)
166 { /* In bytes */
167 size_t ret = 0;
168
169 MCRYPT_ALG_LOOP(ret = p->keysize);
170 return ret;
171
172 }
173
174
175
176 char *mcrypt_get_algorithms_name(int algorithm)
177 {
178 char *ret = NULL;
179 char *pointerTo_;
180
181 /* avoid prefix */
182 MCRYPT_ALG_LOOP(ret = strdup(p->name + sizeof("MCRYPT_") - 1));
183
184
185 if (ret != NULL) {
186 tolow( ret, strlen(ret));
187 pointerTo_ = strchr(ret, '_');
188
189 while (pointerTo_ != NULL) {
190 *pointerTo_ = '-';
191 pointerTo_ = strchr(ret, '_');
192 }
193 }
194 return ret;
195 }
196
197
198
199 int mcrypt_is_ok_algorithm(int algorithm)
200 {
201 char *y = mcrypt_get_algorithms_name(algorithm);
202
203 if (y != NULL) {
204 free(y);
205 return 0;
206 } else {
207 return 1;
208 }
209
210 }
211
212 int mcrypt_is_ok_mode(int mode)
213 { /* if this mode exists returns 0 */
214 char *y = mcrypt_get_modes_name(mode);
215
216 if (y != NULL) {
217 free(y);
218 return 0;
219 } else {
220 return 1;
221 }
222
223 }
224
225
226
227
228 char *mcrypt_get_modes_name(int mode)
229 {
230 char *ret = NULL;
231
232 /* avoid prefix */
233 MCRYPT_MLOOP(ret = strdup(p->name + sizeof("MCRYPT_") - 1));
234 if (ret!=NULL) tolow( ret, strlen(ret));
235
236 return ret;
237 }
238

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26