54 |
|
|
55 |
static int internal_init_mcrypt(MCRYPT td, void *key, int lenofkey, void *IV) |
static int internal_init_mcrypt(MCRYPT td, void *key, int lenofkey, void *IV) |
56 |
{ |
{ |
57 |
int *sizes = NULL; |
const int *sizes = NULL; |
58 |
int num_of_sizes, i, ok = 0; |
int num_of_sizes, i, ok = 0; |
59 |
int key_size = mcrypt_enc_get_key_size(td); |
int key_size = mcrypt_enc_get_key_size(td); |
60 |
|
|
93 |
key_size = lenofkey; |
key_size = lenofkey; |
94 |
} |
} |
95 |
|
|
|
if (sizes!=NULL) free(sizes); |
|
|
|
|
96 |
td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td)); |
td->keyword_given = mxcalloc(1, mcrypt_enc_get_key_size(td)); |
97 |
if (td->keyword_given==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR; |
if (td->keyword_given==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR; |
98 |
|
|
99 |
memmove(td->keyword_given, key, lenofkey); |
memmove(td->keyword_given, key, lenofkey); |
100 |
i = mcrypt_get_size(td); |
i = mcrypt_get_size(td); |
101 |
td->akey = mxcalloc(1, i); |
td->akey = mxcalloc(1, i); |
102 |
if (td->akey==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR; |
if (td->akey==NULL) { |
103 |
|
free(td->keyword_given); |
104 |
|
return MCRYPT_MEMORY_ALLOCATION_ERROR; |
105 |
|
} |
106 |
i = mcrypt_mode_get_size(td); |
i = mcrypt_mode_get_size(td); |
107 |
if (i > 0) { |
if (i > 0) { |
108 |
td->abuf = mxcalloc(1, i); |
td->abuf = mxcalloc(1, i); |
109 |
if (td->abuf==NULL) return MCRYPT_MEMORY_ALLOCATION_ERROR; |
if (td->abuf==NULL) { |
110 |
|
free(td->keyword_given); |
111 |
|
free(td->akey); |
112 |
|
return MCRYPT_MEMORY_ALLOCATION_ERROR; |
113 |
|
} |
114 |
|
} |
115 |
|
ok = init_mcrypt(td, td->abuf, key, key_size, IV); |
116 |
|
if (ok!=0) { |
117 |
|
free(td->keyword_given); |
118 |
|
free(td->akey); |
119 |
|
free(td->abuf); |
120 |
|
return MCRYPT_UNKNOWN_ERROR; /* algorithm error */ |
121 |
} |
} |
|
init_mcrypt(td, td->abuf, key, key_size, IV); |
|
122 |
|
|
123 |
ok = mcrypt_set_key(td, |
ok = mcrypt_set_key(td, |
124 |
(void *) td->akey, |
(void *) td->akey, |
125 |
(void *) td->keyword_given, |
(void *) td->keyword_given, |
126 |
key_size, IV, IV!=NULL ? mcrypt_enc_get_iv_size(td) : 0); |
key_size, IV, IV!=NULL ? mcrypt_enc_get_iv_size(td) : 0); |
127 |
|
|
128 |
if (ok!=0) return MCRYPT_UNKNOWN_ERROR; /* algorithm error */ |
if (ok!=0) { |
|
|
|
|
if (td->akey == NULL) { |
|
129 |
internal_end_mcrypt(td); |
internal_end_mcrypt(td); |
130 |
return MCRYPT_UNKNOWN_ERROR; |
return MCRYPT_UNKNOWN_ERROR; /* algorithm error */ |
131 |
} |
} |
132 |
|
|
133 |
return 0; |
return 0; |
134 |
} |
} |
135 |
|
|
218 |
} |
} |
219 |
|
|
220 |
WIN32DLL_DEFINE |
WIN32DLL_DEFINE |
221 |
char* mcrypt_strerror(int err) |
const char* mcrypt_strerror(int err) |
222 |
{ |
{ |
|
char* cerr=malloc(256); |
|
|
if (cerr==NULL) return NULL; |
|
223 |
|
|
224 |
switch (err) { |
switch (err) { |
225 |
case MCRYPT_UNKNOWN_ERROR: |
case MCRYPT_UNKNOWN_ERROR: |
226 |
sprintf(cerr, "Unknown error.\n"); |
return "Unknown error.\n"; |
227 |
break; |
break; |
228 |
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
case MCRYPT_ALGORITHM_MODE_INCOMPATIBILITY: |
229 |
sprintf(cerr, |
return "Algorithm incompatible with this mode.\n"; |
|
"Algorithm incompatible with this mode.\n"); |
|
230 |
break; |
break; |
231 |
case MCRYPT_KEY_LEN_ERROR: |
case MCRYPT_KEY_LEN_ERROR: |
232 |
sprintf(cerr, "Key length is not legal.\n"); |
return "Key length is not legal.\n"; |
233 |
break; |
break; |
234 |
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
case MCRYPT_MEMORY_ALLOCATION_ERROR: |
235 |
sprintf(cerr, "Memory allocation failed.\n"); |
return "Memory allocation failed.\n"; |
236 |
break; |
break; |
237 |
case MCRYPT_UNKNOWN_MODE: |
case MCRYPT_UNKNOWN_MODE: |
238 |
sprintf(cerr, "Unknown mode.\n"); |
return "Unknown mode.\n"; |
239 |
break; |
break; |
240 |
case MCRYPT_UNKNOWN_ALGORITHM: |
case MCRYPT_UNKNOWN_ALGORITHM: |
241 |
sprintf(cerr, "Unknown algorithm.\n"); |
return "Unknown algorithm.\n"; |
242 |
break; |
break; |
243 |
|
|
244 |
} |
} |
245 |
return cerr; |
return NULL; |
246 |
} |
} |
247 |
|
|
248 |
WIN32DLL_DEFINE |
WIN32DLL_DEFINE |