/[mcrypt]/libmcrypt/lib/mcrypt.c
ViewVC logotype

Annotation of /libmcrypt/lib/mcrypt.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Thu Oct 12 21:06:31 2000 UTC (23 years, 6 months ago) by nmav
Branch: MAIN
Changes since 1.2: +8 -3 lines
File MIME type: text/plain
Added defines and some function emulation needed in WIN32. Thanks goes
to Dimitris Souflis (dsouflis@altera.gr)

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.3 /* $Id: mcrypt.c,v 1.2 2000/09/04 13:02:11 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    
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 nmav 1.2 free(sizes);
77 nmav 1.1 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 nmav 1.3 static int internal_end_mcrypt(MCRYPT td)
101 nmav 1.1 {
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 nmav 1.3 WIN32DLL_DEFINE
118 nmav 1.1 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 nmav 1.3 WIN32DLL_DEFINE
124 nmav 1.1 int mcrypt_generic(MCRYPT td, void *plaintext, int len)
125     {
126     int x;
127    
128     x = mcrypt(td, td->abuf, plaintext, len);
129     return x;
130     }
131    
132 nmav 1.3 WIN32DLL_DEFINE
133 nmav 1.1 int mdecrypt_generic(MCRYPT td, void *ciphertext, int len)
134     {
135     int x;
136     x = mdecrypt(td, td->abuf, ciphertext, len);
137     return x;
138     }
139    
140 nmav 1.3 WIN32DLL_DEFINE
141 nmav 1.1 int mcrypt_generic_end(const MCRYPT td)
142     {
143     internal_end_mcrypt(td);
144     mcrypt_module_close(td);
145     return 0;
146     }
147    
148 nmav 1.3 WIN32DLL_DEFINE
149 nmav 1.1 void mcrypt_perror(int err)
150     {
151    
152     switch (err) {
153     case MCRYPT_UNKNOWN_ERROR:
154     fprintf(stderr, "Unknown error.\n");
155     break;
156     case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY:
157     fprintf(stderr,
158     "Algorithm incompatible with this mode.\n");
159     break;
160     case MCRYPT_KEY_LEN_ERROR:
161     fprintf(stderr, "Key length is not legal.\n");
162     break;
163     case MCRYPT_MEMORY_ALLOCATION_ERROR:
164     fprintf(stderr, "Memory allocation failed.\n");
165     break;
166     case MCRYPT_UNKNOWN_MODE:
167     fprintf(stderr, "Unknown mode.\n");
168     break;
169     case MCRYPT_UNKNOWN_ALGORITHM:
170     fprintf(stderr, "Unknown algorithm.\n");
171     break;
172    
173     }
174     return;
175     }
176    
177 nmav 1.3 WIN32DLL_DEFINE
178 nmav 1.1 int mcrypt_free(void *ptr)
179     {
180     free(ptr);
181     return 0;
182     }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26