diff options
author | lv-426 <oxcafebaby@yahoo.com> | 2008-07-26 03:26:56 +0000 |
---|---|---|
committer | lv-426 <oxcafebaby@yahoo.com> | 2008-07-26 03:26:56 +0000 |
commit | 16abaea132ac90293a868ecb944abce24fd16d9f (patch) | |
tree | 490881a7b7ac6172608c2e9c9ba10d69160551b5 /src/daemon/https/lgl/gc-gnulib.c | |
parent | 14a57219373afd8dd110ee8af89b32767c72c1dc (diff) | |
download | libmicrohttpd-16abaea132ac90293a868ecb944abce24fd16d9f.tar.gz libmicrohttpd-16abaea132ac90293a868ecb944abce24fd16d9f.zip |
'microhttps.h' merge
gnutls version added to configure.ac to enable assersion before running curl tests
GNUTLS symbol renaming
Diffstat (limited to 'src/daemon/https/lgl/gc-gnulib.c')
-rw-r--r-- | src/daemon/https/lgl/gc-gnulib.c | 744 |
1 files changed, 370 insertions, 374 deletions
diff --git a/src/daemon/https/lgl/gc-gnulib.c b/src/daemon/https/lgl/gc-gnulib.c index 05b12781..d1db6d8a 100644 --- a/src/daemon/https/lgl/gc-gnulib.c +++ b/src/daemon/https/lgl/gc-gnulib.c | |||
@@ -67,12 +67,14 @@ | |||
67 | #undef open | 67 | #undef open |
68 | #undef close | 68 | #undef close |
69 | 69 | ||
70 | Gc_rc gc_init(void) | 70 | Gc_rc |
71 | gc_init (void) | ||
71 | { | 72 | { |
72 | return GC_OK; | 73 | return GC_OK; |
73 | } | 74 | } |
74 | 75 | ||
75 | void gc_done(void) | 76 | void |
77 | gc_done (void) | ||
76 | { | 78 | { |
77 | return; | 79 | return; |
78 | } | 80 | } |
@@ -83,85 +85,85 @@ void gc_done(void) | |||
83 | 85 | ||
84 | static Gc_rc | 86 | static Gc_rc |
85 | randomize (int level, char *data, size_t datalen) | 87 | randomize (int level, char *data, size_t datalen) |
86 | { | 88 | { |
87 | int fd; | 89 | int fd; |
88 | const char *device; | 90 | const char *device; |
89 | size_t len = 0; | 91 | size_t len = 0; |
90 | int rc; | 92 | int rc; |
91 | 93 | ||
92 | switch (level) | 94 | switch (level) |
93 | { | 95 | { |
94 | case 0: | 96 | case 0: |
95 | device = NAME_OF_NONCE_DEVICE; | 97 | device = NAME_OF_NONCE_DEVICE; |
96 | break; | 98 | break; |
97 | 99 | ||
98 | case 1: | 100 | case 1: |
99 | device = NAME_OF_PSEUDO_RANDOM_DEVICE; | 101 | device = NAME_OF_PSEUDO_RANDOM_DEVICE; |
100 | break; | 102 | break; |
101 | 103 | ||
102 | default: | 104 | default: |
103 | device = NAME_OF_RANDOM_DEVICE; | 105 | device = NAME_OF_RANDOM_DEVICE; |
104 | break; | 106 | break; |
105 | } | 107 | } |
106 | 108 | ||
107 | if (strcmp (device, "no") == 0) | 109 | if (strcmp (device, "no") == 0) |
108 | return GC_RANDOM_ERROR; | 110 | return GC_RANDOM_ERROR; |
109 | 111 | ||
110 | fd = open (device, O_RDONLY); | 112 | fd = open (device, O_RDONLY); |
111 | if (fd < 0) | 113 | if (fd < 0) |
112 | return GC_RANDOM_ERROR; | 114 | return GC_RANDOM_ERROR; |
113 | 115 | ||
114 | do | 116 | do |
115 | { | 117 | { |
116 | ssize_t tmp; | 118 | ssize_t tmp; |
117 | 119 | ||
118 | tmp = read (fd, data, datalen); | 120 | tmp = read (fd, data, datalen); |
119 | 121 | ||
120 | if (tmp < 0) | 122 | if (tmp < 0) |
121 | { | 123 | { |
122 | int save_errno = errno; | 124 | int save_errno = errno; |
123 | close (fd); | 125 | close (fd); |
124 | errno = save_errno; | 126 | errno = save_errno; |
125 | return GC_RANDOM_ERROR; | 127 | return GC_RANDOM_ERROR; |
126 | } | 128 | } |
127 | 129 | ||
128 | len += tmp; | 130 | len += tmp; |
129 | } | 131 | } |
130 | while (len < datalen); | 132 | while (len < datalen); |
131 | 133 | ||
132 | rc = close (fd); | 134 | rc = close (fd); |
133 | if (rc < 0) | 135 | if (rc < 0) |
134 | return GC_RANDOM_ERROR; | 136 | return GC_RANDOM_ERROR; |
135 | 137 | ||
136 | return GC_OK; | 138 | return GC_OK; |
137 | } | 139 | } |
138 | 140 | ||
139 | Gc_rc | 141 | Gc_rc |
140 | gc_nonce (char *data, size_t datalen) | 142 | gc_nonce (char *data, size_t datalen) |
141 | { | 143 | { |
142 | return randomize (0, data, datalen); | 144 | return randomize (0, data, datalen); |
143 | } | 145 | } |
144 | 146 | ||
145 | Gc_rc | 147 | Gc_rc |
146 | gc_pseudo_random (char *data, size_t datalen) | 148 | gc_pseudo_random (char *data, size_t datalen) |
147 | { | 149 | { |
148 | return randomize (1, data, datalen); | 150 | return randomize (1, data, datalen); |
149 | } | 151 | } |
150 | 152 | ||
151 | Gc_rc | 153 | Gc_rc |
152 | gc_random (char *data, size_t datalen) | 154 | gc_random (char *data, size_t datalen) |
153 | { | 155 | { |
154 | return randomize (2, data, datalen); | 156 | return randomize (2, data, datalen); |
155 | } | 157 | } |
156 | 158 | ||
157 | #endif | 159 | #endif |
158 | 160 | ||
159 | /* Memory allocation. */ | 161 | /* Memory allocation. */ |
160 | void gc_set_allocators(gc_malloc_t func_malloc, | 162 | void |
161 | gc_malloc_t secure_malloc, | 163 | gc_set_allocators (gc_malloc_t func_malloc, |
162 | gc_secure_check_t secure_check, | 164 | gc_malloc_t secure_malloc, |
163 | gc_realloc_t func_realloc, | 165 | gc_secure_check_t secure_check, |
164 | gc_free_t func_free) | 166 | gc_realloc_t func_realloc, gc_free_t func_free) |
165 | { | 167 | { |
166 | return; | 168 | return; |
167 | } | 169 | } |
@@ -169,9 +171,9 @@ void gc_set_allocators(gc_malloc_t func_malloc, | |||
169 | /* Ciphers. */ | 171 | /* Ciphers. */ |
170 | 172 | ||
171 | typedef struct _gc_cipher_ctx | 173 | typedef struct _gc_cipher_ctx |
172 | { | 174 | { |
173 | Gc_cipher alg; | 175 | Gc_cipher alg; |
174 | Gc_cipher_mode mode; | 176 | Gc_cipher_mode mode; |
175 | #ifdef GNULIB_GC_ARCTWO | 177 | #ifdef GNULIB_GC_ARCTWO |
176 | arctwo_context arctwoContext; | 178 | arctwo_context arctwoContext; |
177 | char arctwoIV[ARCTWO_BLOCK_SIZE]; | 179 | char arctwoIV[ARCTWO_BLOCK_SIZE]; |
@@ -187,16 +189,16 @@ typedef struct _gc_cipher_ctx | |||
187 | rijndaelKeyInstance aesDecKey; | 189 | rijndaelKeyInstance aesDecKey; |
188 | rijndaelCipherInstance aesContext; | 190 | rijndaelCipherInstance aesContext; |
189 | #endif | 191 | #endif |
190 | } _gc_cipher_ctx; | 192 | } _gc_cipher_ctx; |
191 | 193 | ||
192 | Gc_rc gc_cipher_open(Gc_cipher alg, | 194 | Gc_rc |
193 | Gc_cipher_mode mode, | 195 | gc_cipher_open (Gc_cipher alg, |
194 | gc_cipher_handle * outhandle) | 196 | Gc_cipher_mode mode, gc_cipher_handle * outhandle) |
195 | { | 197 | { |
196 | _gc_cipher_ctx *ctx; | 198 | _gc_cipher_ctx *ctx; |
197 | Gc_rc rc = GC_OK; | 199 | Gc_rc rc = GC_OK; |
198 | 200 | ||
199 | ctx = calloc(sizeof (*ctx), 1); | 201 | ctx = calloc (sizeof (*ctx), 1); |
200 | if (!ctx) | 202 | if (!ctx) |
201 | return GC_MALLOC_ERROR; | 203 | return GC_MALLOC_ERROR; |
202 | 204 | ||
@@ -206,344 +208,341 @@ Gc_rc gc_cipher_open(Gc_cipher alg, | |||
206 | switch (alg) | 208 | switch (alg) |
207 | { | 209 | { |
208 | #ifdef GNULIB_GC_ARCTWO | 210 | #ifdef GNULIB_GC_ARCTWO |
209 | case GC_ARCTWO40: | 211 | case GC_ARCTWO40: |
210 | switch (mode) | 212 | switch (mode) |
211 | { | 213 | { |
212 | case GC_ECB: | 214 | case GC_ECB: |
213 | case GC_CBC: | 215 | case GC_CBC: |
214 | break; | 216 | break; |
215 | 217 | ||
216 | default: | 218 | default: |
217 | rc = GC_INVALID_CIPHER; | 219 | rc = GC_INVALID_CIPHER; |
218 | } | 220 | } |
219 | break; | 221 | break; |
220 | #endif | 222 | #endif |
221 | 223 | ||
222 | #ifdef GNULIB_GC_ARCFOUR | 224 | #ifdef GNULIB_GC_ARCFOUR |
223 | case GC_ARCFOUR128: | 225 | case GC_ARCFOUR128: |
224 | case GC_ARCFOUR40: | 226 | case GC_ARCFOUR40: |
225 | switch (mode) | 227 | switch (mode) |
226 | { | 228 | { |
227 | case GC_STREAM: | 229 | case GC_STREAM: |
228 | break; | 230 | break; |
229 | 231 | ||
230 | default: | 232 | default: |
231 | rc = GC_INVALID_CIPHER; | 233 | rc = GC_INVALID_CIPHER; |
232 | } | 234 | } |
233 | break; | 235 | break; |
234 | #endif | 236 | #endif |
235 | 237 | ||
236 | #ifdef GNULIB_GC_DES | 238 | #ifdef GNULIB_GC_DES |
237 | case GC_DES: | 239 | case GC_DES: |
238 | switch (mode) | 240 | switch (mode) |
239 | { | 241 | { |
240 | case GC_ECB: | 242 | case GC_ECB: |
241 | break; | 243 | break; |
242 | 244 | ||
243 | default: | 245 | default: |
244 | rc = GC_INVALID_CIPHER; | 246 | rc = GC_INVALID_CIPHER; |
245 | } | 247 | } |
246 | break; | 248 | break; |
247 | #endif | 249 | #endif |
248 | 250 | ||
249 | #ifdef GNULIB_GC_RIJNDAEL | 251 | #ifdef GNULIB_GC_RIJNDAEL |
250 | case GC_AES128: | 252 | case GC_AES128: |
251 | case GC_AES192: | 253 | case GC_AES192: |
252 | case GC_AES256: | 254 | case GC_AES256: |
253 | switch (mode) | 255 | switch (mode) |
254 | { | 256 | { |
255 | case GC_ECB: | 257 | case GC_ECB: |
256 | case GC_CBC: | 258 | case GC_CBC: |
257 | break; | 259 | break; |
258 | 260 | ||
259 | default: | 261 | default: |
260 | rc = GC_INVALID_CIPHER; | 262 | rc = GC_INVALID_CIPHER; |
261 | } | 263 | } |
262 | break; | 264 | break; |
263 | #endif | 265 | #endif |
264 | 266 | ||
265 | default: | 267 | default: |
266 | rc = GC_INVALID_CIPHER; | 268 | rc = GC_INVALID_CIPHER; |
267 | } | 269 | } |
268 | 270 | ||
269 | if (rc == GC_OK) | 271 | if (rc == GC_OK) |
270 | *outhandle = ctx; | 272 | *outhandle = ctx; |
271 | else | 273 | else |
272 | free(ctx); | 274 | free (ctx); |
273 | 275 | ||
274 | return rc; | 276 | return rc; |
275 | } | 277 | } |
276 | 278 | ||
277 | Gc_rc gc_cipher_setkey(gc_cipher_handle handle, | 279 | Gc_rc |
278 | size_t keylen, | 280 | gc_cipher_setkey (gc_cipher_handle handle, size_t keylen, const char *key) |
279 | const char *key) | ||
280 | { | 281 | { |
281 | _gc_cipher_ctx *ctx = handle; | 282 | _gc_cipher_ctx *ctx = handle; |
282 | 283 | ||
283 | switch (ctx->alg) | 284 | switch (ctx->alg) |
284 | { | 285 | { |
285 | #ifdef GNULIB_GC_ARCTWO | 286 | #ifdef GNULIB_GC_ARCTWO |
286 | case GC_ARCTWO40: | 287 | case GC_ARCTWO40: |
287 | arctwo_setkey (&ctx->arctwoContext, keylen, key); | 288 | arctwo_setkey (&ctx->arctwoContext, keylen, key); |
288 | break; | 289 | break; |
289 | #endif | 290 | #endif |
290 | 291 | ||
291 | #ifdef GNULIB_GC_ARCFOUR | 292 | #ifdef GNULIB_GC_ARCFOUR |
292 | case GC_ARCFOUR128: | 293 | case GC_ARCFOUR128: |
293 | case GC_ARCFOUR40: | 294 | case GC_ARCFOUR40: |
294 | arcfour_setkey (&ctx->arcfourContext, key, keylen); | 295 | arcfour_setkey (&ctx->arcfourContext, key, keylen); |
295 | break; | 296 | break; |
296 | #endif | 297 | #endif |
297 | 298 | ||
298 | #ifdef GNULIB_GC_DES | 299 | #ifdef GNULIB_GC_DES |
299 | case GC_DES: | 300 | case GC_DES: |
300 | if (keylen != 8) | 301 | if (keylen != 8) |
301 | return GC_INVALID_CIPHER; | 302 | return GC_INVALID_CIPHER; |
302 | gl_des_setkey (&ctx->desContext, key); | 303 | gl_des_setkey (&ctx->desContext, key); |
303 | break; | 304 | break; |
304 | #endif | 305 | #endif |
305 | 306 | ||
306 | #ifdef GNULIB_GC_RIJNDAEL | 307 | #ifdef GNULIB_GC_RIJNDAEL |
307 | case GC_AES128: | 308 | case GC_AES128: |
308 | case GC_AES192: | 309 | case GC_AES192: |
309 | case GC_AES256: | 310 | case GC_AES256: |
310 | { | 311 | { |
311 | rijndael_rc rc; | 312 | rijndael_rc rc; |
312 | size_t i; | 313 | size_t i; |
313 | char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1]; | 314 | char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1]; |
314 | 315 | ||
315 | for (i = 0; i < keylen; i++) | 316 | for (i = 0; i < keylen; i++) |
316 | sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF); | 317 | sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF); |
317 | 318 | ||
318 | rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT, | 319 | rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT, |
319 | keylen * 8, keyMaterial); | 320 | keylen * 8, keyMaterial); |
320 | if (rc < 0) | 321 | if (rc < 0) |
321 | return GC_INVALID_CIPHER; | 322 | return GC_INVALID_CIPHER; |
322 | 323 | ||
323 | rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT, | 324 | rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT, |
324 | keylen * 8, keyMaterial); | 325 | keylen * 8, keyMaterial); |
325 | if (rc < 0) | 326 | if (rc < 0) |
326 | return GC_INVALID_CIPHER; | 327 | return GC_INVALID_CIPHER; |
327 | 328 | ||
328 | rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL); | 329 | rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL); |
329 | if (rc < 0) | 330 | if (rc < 0) |
330 | return GC_INVALID_CIPHER; | 331 | return GC_INVALID_CIPHER; |
331 | } | 332 | } |
332 | break; | 333 | break; |
333 | #endif | 334 | #endif |
334 | 335 | ||
335 | default: | 336 | default: |
336 | return GC_INVALID_CIPHER; | 337 | return GC_INVALID_CIPHER; |
337 | } | 338 | } |
338 | 339 | ||
339 | return GC_OK; | 340 | return GC_OK; |
340 | } | 341 | } |
341 | 342 | ||
342 | Gc_rc gc_cipher_setiv(gc_cipher_handle handle, | 343 | Gc_rc |
343 | size_t ivlen, | 344 | gc_cipher_setiv (gc_cipher_handle handle, size_t ivlen, const char *iv) |
344 | const char *iv) | ||
345 | { | 345 | { |
346 | _gc_cipher_ctx *ctx = handle; | 346 | _gc_cipher_ctx *ctx = handle; |
347 | 347 | ||
348 | switch (ctx->alg) | 348 | switch (ctx->alg) |
349 | { | 349 | { |
350 | #ifdef GNULIB_GC_ARCTWO | 350 | #ifdef GNULIB_GC_ARCTWO |
351 | case GC_ARCTWO40: | 351 | case GC_ARCTWO40: |
352 | if (ivlen != ARCTWO_BLOCK_SIZE) | 352 | if (ivlen != ARCTWO_BLOCK_SIZE) |
353 | return GC_INVALID_CIPHER; | 353 | return GC_INVALID_CIPHER; |
354 | memcpy (ctx->arctwoIV, iv, ivlen); | 354 | memcpy (ctx->arctwoIV, iv, ivlen); |
355 | break; | 355 | break; |
356 | #endif | 356 | #endif |
357 | 357 | ||
358 | #ifdef GNULIB_GC_RIJNDAEL | 358 | #ifdef GNULIB_GC_RIJNDAEL |
359 | case GC_AES128: | 359 | case GC_AES128: |
360 | case GC_AES192: | 360 | case GC_AES192: |
361 | case GC_AES256: | 361 | case GC_AES256: |
362 | switch (ctx->mode) | 362 | switch (ctx->mode) |
363 | { | ||
364 | case GC_ECB: | ||
365 | /* Doesn't use IV. */ | ||
366 | break; | ||
367 | |||
368 | case GC_CBC: | ||
369 | { | 363 | { |
370 | rijndael_rc rc; | 364 | case GC_ECB: |
371 | size_t i; | 365 | /* Doesn't use IV. */ |
372 | char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1]; | 366 | break; |
373 | 367 | ||
374 | for (i = 0; i < ivlen; i++) | 368 | case GC_CBC: |
375 | sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF); | 369 | { |
370 | rijndael_rc rc; | ||
371 | size_t i; | ||
372 | char ivMaterial[2 * RIJNDAEL_MAX_IV_SIZE + 1]; | ||
373 | |||
374 | for (i = 0; i < ivlen; i++) | ||
375 | sprintf (&ivMaterial[2 * i], "%02x", iv[i] & 0xFF); | ||
376 | |||
377 | rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC, | ||
378 | ivMaterial); | ||
379 | if (rc < 0) | ||
380 | return GC_INVALID_CIPHER; | ||
381 | } | ||
382 | break; | ||
376 | 383 | ||
377 | rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_CBC, | 384 | default: |
378 | ivMaterial); | ||
379 | if (rc < 0) | ||
380 | return GC_INVALID_CIPHER; | 385 | return GC_INVALID_CIPHER; |
381 | } | 386 | } |
382 | break; | 387 | break; |
383 | |||
384 | default: | ||
385 | return GC_INVALID_CIPHER; | ||
386 | } | ||
387 | break; | ||
388 | #endif | 388 | #endif |
389 | 389 | ||
390 | default: | 390 | default: |
391 | return GC_INVALID_CIPHER; | 391 | return GC_INVALID_CIPHER; |
392 | } | 392 | } |
393 | 393 | ||
394 | return GC_OK; | 394 | return GC_OK; |
395 | } | 395 | } |
396 | 396 | ||
397 | Gc_rc gc_cipher_encrypt_inline(gc_cipher_handle handle, | 397 | Gc_rc |
398 | size_t len, | 398 | gc_cipher_encrypt_inline (gc_cipher_handle handle, size_t len, char *data) |
399 | char *data) | ||
400 | { | 399 | { |
401 | _gc_cipher_ctx *ctx = handle; | 400 | _gc_cipher_ctx *ctx = handle; |
402 | 401 | ||
403 | switch (ctx->alg) | 402 | switch (ctx->alg) |
404 | { | 403 | { |
405 | #ifdef GNULIB_GC_ARCTWO | 404 | #ifdef GNULIB_GC_ARCTWO |
406 | case GC_ARCTWO40: | 405 | case GC_ARCTWO40: |
407 | switch (ctx->mode) | 406 | switch (ctx->mode) |
408 | { | ||
409 | case GC_ECB: | ||
410 | arctwo_encrypt (&ctx->arctwoContext, data, data, len); | ||
411 | break; | ||
412 | |||
413 | case GC_CBC: | ||
414 | for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE, | ||
415 | data += ARCTWO_BLOCK_SIZE) | ||
416 | { | 407 | { |
417 | size_t i; | 408 | case GC_ECB: |
418 | for (i = 0; i < ARCTWO_BLOCK_SIZE; i++) | 409 | arctwo_encrypt (&ctx->arctwoContext, data, data, len); |
419 | data[i] ^= ctx->arctwoIV[i]; | 410 | break; |
420 | arctwo_encrypt (&ctx->arctwoContext, data, data, | 411 | |
421 | ARCTWO_BLOCK_SIZE); | 412 | case GC_CBC: |
422 | memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE); | 413 | for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE, |
414 | data += ARCTWO_BLOCK_SIZE) | ||
415 | { | ||
416 | size_t i; | ||
417 | for (i = 0; i < ARCTWO_BLOCK_SIZE; i++) | ||
418 | data[i] ^= ctx->arctwoIV[i]; | ||
419 | arctwo_encrypt (&ctx->arctwoContext, data, data, | ||
420 | ARCTWO_BLOCK_SIZE); | ||
421 | memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE); | ||
422 | } | ||
423 | break; | ||
424 | |||
425 | default: | ||
426 | return GC_INVALID_CIPHER; | ||
423 | } | 427 | } |
424 | break; | 428 | break; |
425 | |||
426 | default: | ||
427 | return GC_INVALID_CIPHER; | ||
428 | } | ||
429 | break; | ||
430 | #endif | 429 | #endif |
431 | 430 | ||
432 | #ifdef GNULIB_GC_ARCFOUR | 431 | #ifdef GNULIB_GC_ARCFOUR |
433 | case GC_ARCFOUR128: | 432 | case GC_ARCFOUR128: |
434 | case GC_ARCFOUR40: | 433 | case GC_ARCFOUR40: |
435 | arcfour_stream (&ctx->arcfourContext, data, data, len); | 434 | arcfour_stream (&ctx->arcfourContext, data, data, len); |
436 | break; | 435 | break; |
437 | #endif | 436 | #endif |
438 | 437 | ||
439 | #ifdef GNULIB_GC_DES | 438 | #ifdef GNULIB_GC_DES |
440 | case GC_DES: | 439 | case GC_DES: |
441 | for (; len >= 8; len -= 8, data += 8) | 440 | for (; len >= 8; len -= 8, data += 8) |
442 | gl_des_ecb_encrypt (&ctx->desContext, data, data); | 441 | gl_des_ecb_encrypt (&ctx->desContext, data, data); |
443 | break; | 442 | break; |
444 | #endif | 443 | #endif |
445 | 444 | ||
446 | #ifdef GNULIB_GC_RIJNDAEL | 445 | #ifdef GNULIB_GC_RIJNDAEL |
447 | case GC_AES128: | 446 | case GC_AES128: |
448 | case GC_AES192: | 447 | case GC_AES192: |
449 | case GC_AES256: | 448 | case GC_AES256: |
450 | { | 449 | { |
451 | int nblocks; | 450 | int nblocks; |
452 | 451 | ||
453 | nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey, | 452 | nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey, |
454 | data, 8 * len, data); | 453 | data, 8 * len, data); |
455 | if (nblocks < 0) | 454 | if (nblocks < 0) |
456 | return GC_INVALID_CIPHER; | 455 | return GC_INVALID_CIPHER; |
457 | } | 456 | } |
458 | break; | 457 | break; |
459 | #endif | 458 | #endif |
460 | 459 | ||
461 | default: | 460 | default: |
462 | return GC_INVALID_CIPHER; | 461 | return GC_INVALID_CIPHER; |
463 | } | 462 | } |
464 | 463 | ||
465 | return GC_OK; | 464 | return GC_OK; |
466 | } | 465 | } |
467 | 466 | ||
468 | Gc_rc gc_cipher_decrypt_inline(gc_cipher_handle handle, | 467 | Gc_rc |
469 | size_t len, | 468 | gc_cipher_decrypt_inline (gc_cipher_handle handle, size_t len, char *data) |
470 | char *data) | ||
471 | { | 469 | { |
472 | _gc_cipher_ctx *ctx = handle; | 470 | _gc_cipher_ctx *ctx = handle; |
473 | 471 | ||
474 | switch (ctx->alg) | 472 | switch (ctx->alg) |
475 | { | 473 | { |
476 | #ifdef GNULIB_GC_ARCTWO | 474 | #ifdef GNULIB_GC_ARCTWO |
477 | case GC_ARCTWO40: | 475 | case GC_ARCTWO40: |
478 | switch (ctx->mode) | 476 | switch (ctx->mode) |
479 | { | ||
480 | case GC_ECB: | ||
481 | arctwo_decrypt (&ctx->arctwoContext, data, data, len); | ||
482 | break; | ||
483 | |||
484 | case GC_CBC: | ||
485 | for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE, | ||
486 | data += ARCTWO_BLOCK_SIZE) | ||
487 | { | 477 | { |
488 | char tmpIV[ARCTWO_BLOCK_SIZE]; | 478 | case GC_ECB: |
489 | size_t i; | 479 | arctwo_decrypt (&ctx->arctwoContext, data, data, len); |
490 | memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE); | 480 | break; |
491 | arctwo_decrypt (&ctx->arctwoContext, data, data, | 481 | |
492 | ARCTWO_BLOCK_SIZE); | 482 | case GC_CBC: |
493 | for (i = 0; i < ARCTWO_BLOCK_SIZE; i++) | 483 | for (; len >= ARCTWO_BLOCK_SIZE; len -= ARCTWO_BLOCK_SIZE, |
494 | data[i] ^= ctx->arctwoIV[i]; | 484 | data += ARCTWO_BLOCK_SIZE) |
495 | memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE); | 485 | { |
486 | char tmpIV[ARCTWO_BLOCK_SIZE]; | ||
487 | size_t i; | ||
488 | memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE); | ||
489 | arctwo_decrypt (&ctx->arctwoContext, data, data, | ||
490 | ARCTWO_BLOCK_SIZE); | ||
491 | for (i = 0; i < ARCTWO_BLOCK_SIZE; i++) | ||
492 | data[i] ^= ctx->arctwoIV[i]; | ||
493 | memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE); | ||
494 | } | ||
495 | break; | ||
496 | |||
497 | default: | ||
498 | return GC_INVALID_CIPHER; | ||
496 | } | 499 | } |
497 | break; | 500 | break; |
498 | |||
499 | default: | ||
500 | return GC_INVALID_CIPHER; | ||
501 | } | ||
502 | break; | ||
503 | #endif | 501 | #endif |
504 | 502 | ||
505 | #ifdef GNULIB_GC_ARCFOUR | 503 | #ifdef GNULIB_GC_ARCFOUR |
506 | case GC_ARCFOUR128: | 504 | case GC_ARCFOUR128: |
507 | case GC_ARCFOUR40: | 505 | case GC_ARCFOUR40: |
508 | arcfour_stream (&ctx->arcfourContext, data, data, len); | 506 | arcfour_stream (&ctx->arcfourContext, data, data, len); |
509 | break; | 507 | break; |
510 | #endif | 508 | #endif |
511 | 509 | ||
512 | #ifdef GNULIB_GC_DES | 510 | #ifdef GNULIB_GC_DES |
513 | case GC_DES: | 511 | case GC_DES: |
514 | for (; len >= 8; len -= 8, data += 8) | 512 | for (; len >= 8; len -= 8, data += 8) |
515 | gl_des_ecb_decrypt (&ctx->desContext, data, data); | 513 | gl_des_ecb_decrypt (&ctx->desContext, data, data); |
516 | break; | 514 | break; |
517 | #endif | 515 | #endif |
518 | 516 | ||
519 | #ifdef GNULIB_GC_RIJNDAEL | 517 | #ifdef GNULIB_GC_RIJNDAEL |
520 | case GC_AES128: | 518 | case GC_AES128: |
521 | case GC_AES192: | 519 | case GC_AES192: |
522 | case GC_AES256: | 520 | case GC_AES256: |
523 | { | 521 | { |
524 | int nblocks; | 522 | int nblocks; |
525 | 523 | ||
526 | nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey, | 524 | nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey, |
527 | data, 8 * len, data); | 525 | data, 8 * len, data); |
528 | if (nblocks < 0) | 526 | if (nblocks < 0) |
529 | return GC_INVALID_CIPHER; | 527 | return GC_INVALID_CIPHER; |
530 | } | 528 | } |
531 | break; | 529 | break; |
532 | #endif | 530 | #endif |
533 | 531 | ||
534 | default: | 532 | default: |
535 | return GC_INVALID_CIPHER; | 533 | return GC_INVALID_CIPHER; |
536 | } | 534 | } |
537 | 535 | ||
538 | return GC_OK; | 536 | return GC_OK; |
539 | } | 537 | } |
540 | 538 | ||
541 | Gc_rc gc_cipher_close(gc_cipher_handle handle) | 539 | Gc_rc |
540 | gc_cipher_close (gc_cipher_handle handle) | ||
542 | { | 541 | { |
543 | _gc_cipher_ctx *ctx = handle; | 542 | _gc_cipher_ctx *ctx = handle; |
544 | 543 | ||
545 | if (ctx) | 544 | if (ctx) |
546 | free(ctx); | 545 | free (ctx); |
547 | 546 | ||
548 | return GC_OK; | 547 | return GC_OK; |
549 | } | 548 | } |
@@ -553,26 +552,25 @@ Gc_rc gc_cipher_close(gc_cipher_handle handle) | |||
553 | #define MAX_DIGEST_SIZE 20 | 552 | #define MAX_DIGEST_SIZE 20 |
554 | 553 | ||
555 | typedef struct _gc_hash_ctx | 554 | typedef struct _gc_hash_ctx |
556 | { | 555 | { |
557 | Gc_hash alg; | 556 | Gc_hash alg; |
558 | Gc_hash_mode mode; | 557 | Gc_hash_mode mode; |
559 | char hash[MAX_DIGEST_SIZE]; | 558 | char hash[MAX_DIGEST_SIZE]; |
560 | #ifdef GNULIB_GC_MD5 | 559 | #ifdef GNULIB_GC_MD5 |
561 | struct md5_ctx md5Context; | 560 | struct md5_ctx md5Context; |
562 | #endif | 561 | #endif |
563 | #ifdef GNULIB_GC_SHA1 | 562 | #ifdef GNULIB_GC_SHA1 |
564 | struct sha1_ctx sha1Context; | 563 | struct sha1_ctx sha1Context; |
565 | #endif | 564 | #endif |
566 | } _gc_hash_ctx; | 565 | } _gc_hash_ctx; |
567 | 566 | ||
568 | Gc_rc gc_hash_open(Gc_hash hash, | 567 | Gc_rc |
569 | Gc_hash_mode mode, | 568 | gc_hash_open (Gc_hash hash, Gc_hash_mode mode, gc_hash_handle * outhandle) |
570 | gc_hash_handle * outhandle) | ||
571 | { | 569 | { |
572 | _gc_hash_ctx *ctx; | 570 | _gc_hash_ctx *ctx; |
573 | Gc_rc rc = GC_OK; | 571 | Gc_rc rc = GC_OK; |
574 | 572 | ||
575 | ctx = calloc(sizeof (*ctx), 1); | 573 | ctx = calloc (sizeof (*ctx), 1); |
576 | if (!ctx) | 574 | if (!ctx) |
577 | return GC_MALLOC_ERROR; | 575 | return GC_MALLOC_ERROR; |
578 | 576 | ||
@@ -582,169 +580,169 @@ Gc_rc gc_hash_open(Gc_hash hash, | |||
582 | switch (hash) | 580 | switch (hash) |
583 | { | 581 | { |
584 | #ifdef GNULIB_GC_MD5 | 582 | #ifdef GNULIB_GC_MD5 |
585 | case GC_MD5: | 583 | case GC_MD5: |
586 | md5_init_ctx (&ctx->md5Context); | 584 | md5_init_ctx (&ctx->md5Context); |
587 | break; | 585 | break; |
588 | #endif | 586 | #endif |
589 | 587 | ||
590 | #ifdef GNULIB_GC_SHA1 | 588 | #ifdef GNULIB_GC_SHA1 |
591 | case GC_SHA1: | 589 | case GC_SHA1: |
592 | sha1_init_ctx (&ctx->sha1Context); | 590 | sha1_init_ctx (&ctx->sha1Context); |
593 | break; | 591 | break; |
594 | #endif | 592 | #endif |
595 | 593 | ||
596 | default: | 594 | default: |
597 | rc = GC_INVALID_HASH; | 595 | rc = GC_INVALID_HASH; |
598 | break; | 596 | break; |
599 | } | 597 | } |
600 | 598 | ||
601 | switch (mode) | 599 | switch (mode) |
602 | { | 600 | { |
603 | case 0: | 601 | case 0: |
604 | break; | 602 | break; |
605 | 603 | ||
606 | default: | 604 | default: |
607 | rc = GC_INVALID_HASH; | 605 | rc = GC_INVALID_HASH; |
608 | break; | 606 | break; |
609 | } | 607 | } |
610 | 608 | ||
611 | if (rc == GC_OK) | 609 | if (rc == GC_OK) |
612 | *outhandle = ctx; | 610 | *outhandle = ctx; |
613 | else | 611 | else |
614 | free(ctx); | 612 | free (ctx); |
615 | 613 | ||
616 | return rc; | 614 | return rc; |
617 | } | 615 | } |
618 | 616 | ||
619 | Gc_rc gc_hash_clone(gc_hash_handle handle, | 617 | Gc_rc |
620 | gc_hash_handle * outhandle) | 618 | gc_hash_clone (gc_hash_handle handle, gc_hash_handle * outhandle) |
621 | { | 619 | { |
622 | _gc_hash_ctx *in = handle; | 620 | _gc_hash_ctx *in = handle; |
623 | _gc_hash_ctx *out; | 621 | _gc_hash_ctx *out; |
624 | 622 | ||
625 | *outhandle = out = calloc(sizeof (*out), 1); | 623 | *outhandle = out = calloc (sizeof (*out), 1); |
626 | if (!out) | 624 | if (!out) |
627 | return GC_MALLOC_ERROR; | 625 | return GC_MALLOC_ERROR; |
628 | 626 | ||
629 | memcpy(out, in, sizeof (*out)); | 627 | memcpy (out, in, sizeof (*out)); |
630 | 628 | ||
631 | return GC_OK; | 629 | return GC_OK; |
632 | } | 630 | } |
633 | 631 | ||
634 | size_t gc_hash_digest_length(Gc_hash hash) | 632 | size_t |
633 | gc_hash_digest_length (Gc_hash hash) | ||
635 | { | 634 | { |
636 | size_t len; | 635 | size_t len; |
637 | 636 | ||
638 | switch (hash) | 637 | switch (hash) |
639 | { | 638 | { |
640 | case GC_MD2: | 639 | case GC_MD2: |
641 | len = GC_MD2_DIGEST_SIZE; | 640 | len = GC_MD2_DIGEST_SIZE; |
642 | break; | 641 | break; |
643 | 642 | ||
644 | case GC_MD4: | 643 | case GC_MD4: |
645 | len = GC_MD4_DIGEST_SIZE; | 644 | len = GC_MD4_DIGEST_SIZE; |
646 | break; | 645 | break; |
647 | 646 | ||
648 | case GC_MD5: | 647 | case GC_MD5: |
649 | len = GC_MD5_DIGEST_SIZE; | 648 | len = GC_MD5_DIGEST_SIZE; |
650 | break; | 649 | break; |
651 | 650 | ||
652 | case GC_RMD160: | 651 | case GC_RMD160: |
653 | len = GC_RMD160_DIGEST_SIZE; | 652 | len = GC_RMD160_DIGEST_SIZE; |
654 | break; | 653 | break; |
655 | 654 | ||
656 | case GC_SHA1: | 655 | case GC_SHA1: |
657 | len = GC_SHA1_DIGEST_SIZE; | 656 | len = GC_SHA1_DIGEST_SIZE; |
658 | break; | 657 | break; |
659 | 658 | ||
660 | default: | 659 | default: |
661 | return 0; | 660 | return 0; |
662 | } | 661 | } |
663 | 662 | ||
664 | return len; | 663 | return len; |
665 | } | 664 | } |
666 | 665 | ||
667 | void gc_hash_write(gc_hash_handle handle, | 666 | void |
668 | size_t len, | 667 | gc_hash_write (gc_hash_handle handle, size_t len, const char *data) |
669 | const char *data) | ||
670 | { | 668 | { |
671 | _gc_hash_ctx *ctx = handle; | 669 | _gc_hash_ctx *ctx = handle; |
672 | 670 | ||
673 | switch (ctx->alg) | 671 | switch (ctx->alg) |
674 | { | 672 | { |
675 | #ifdef GNULIB_GC_MD5 | 673 | #ifdef GNULIB_GC_MD5 |
676 | case GC_MD5: | 674 | case GC_MD5: |
677 | md5_process_bytes (data, len, &ctx->md5Context); | 675 | md5_process_bytes (data, len, &ctx->md5Context); |
678 | break; | 676 | break; |
679 | #endif | 677 | #endif |
680 | 678 | ||
681 | #ifdef GNULIB_GC_SHA1 | 679 | #ifdef GNULIB_GC_SHA1 |
682 | case GC_SHA1: | 680 | case GC_SHA1: |
683 | sha1_process_bytes (data, len, &ctx->sha1Context); | 681 | sha1_process_bytes (data, len, &ctx->sha1Context); |
684 | break; | 682 | break; |
685 | #endif | 683 | #endif |
686 | 684 | ||
687 | default: | 685 | default: |
688 | break; | 686 | break; |
689 | } | 687 | } |
690 | } | 688 | } |
691 | 689 | ||
692 | const char * gc_hash_read(gc_hash_handle handle) | 690 | const char * |
691 | gc_hash_read (gc_hash_handle handle) | ||
693 | { | 692 | { |
694 | _gc_hash_ctx *ctx = handle; | 693 | _gc_hash_ctx *ctx = handle; |
695 | const char *ret= NULL; | 694 | const char *ret = NULL; |
696 | 695 | ||
697 | switch (ctx->alg) | 696 | switch (ctx->alg) |
698 | { | 697 | { |
699 | #ifdef GNULIB_GC_MD5 | 698 | #ifdef GNULIB_GC_MD5 |
700 | case GC_MD5: | 699 | case GC_MD5: |
701 | md5_finish_ctx (&ctx->md5Context, ctx->hash); | 700 | md5_finish_ctx (&ctx->md5Context, ctx->hash); |
702 | ret = ctx->hash; | 701 | ret = ctx->hash; |
703 | break; | 702 | break; |
704 | #endif | 703 | #endif |
705 | 704 | ||
706 | #ifdef GNULIB_GC_SHA1 | 705 | #ifdef GNULIB_GC_SHA1 |
707 | case GC_SHA1: | 706 | case GC_SHA1: |
708 | sha1_finish_ctx (&ctx->sha1Context, ctx->hash); | 707 | sha1_finish_ctx (&ctx->sha1Context, ctx->hash); |
709 | ret = ctx->hash; | 708 | ret = ctx->hash; |
710 | break; | 709 | break; |
711 | #endif | 710 | #endif |
712 | 711 | ||
713 | default: | 712 | default: |
714 | return NULL; | 713 | return NULL; |
715 | } | 714 | } |
716 | 715 | ||
717 | return ret; | 716 | return ret; |
718 | } | 717 | } |
719 | 718 | ||
720 | void gc_hash_close(gc_hash_handle handle) | 719 | void |
720 | gc_hash_close (gc_hash_handle handle) | ||
721 | { | 721 | { |
722 | _gc_hash_ctx *ctx = handle; | 722 | _gc_hash_ctx *ctx = handle; |
723 | 723 | ||
724 | free(ctx); | 724 | free (ctx); |
725 | } | 725 | } |
726 | 726 | ||
727 | Gc_rc gc_hash_buffer(Gc_hash hash, | 727 | Gc_rc |
728 | const void *in, | 728 | gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf) |
729 | size_t inlen, | ||
730 | char *resbuf) | ||
731 | { | 729 | { |
732 | switch (hash) | 730 | switch (hash) |
733 | { | 731 | { |
734 | #ifdef GNULIB_GC_MD5 | 732 | #ifdef GNULIB_GC_MD5 |
735 | case GC_MD5: | 733 | case GC_MD5: |
736 | md5_buffer (in, inlen, resbuf); | 734 | md5_buffer (in, inlen, resbuf); |
737 | break; | 735 | break; |
738 | #endif | 736 | #endif |
739 | 737 | ||
740 | #ifdef GNULIB_GC_SHA1 | 738 | #ifdef GNULIB_GC_SHA1 |
741 | case GC_SHA1: | 739 | case GC_SHA1: |
742 | sha1_buffer (in, inlen, resbuf); | 740 | sha1_buffer (in, inlen, resbuf); |
743 | break; | 741 | break; |
744 | #endif | 742 | #endif |
745 | 743 | ||
746 | default: | 744 | default: |
747 | return GC_INVALID_HASH; | 745 | return GC_INVALID_HASH; |
748 | } | 746 | } |
749 | 747 | ||
750 | return GC_OK; | 748 | return GC_OK; |
@@ -753,39 +751,37 @@ Gc_rc gc_hash_buffer(Gc_hash hash, | |||
753 | #ifdef GNULIB_GC_MD5 | 751 | #ifdef GNULIB_GC_MD5 |
754 | Gc_rc | 752 | Gc_rc |
755 | gc_md5 (const void *in, size_t inlen, void *resbuf) | 753 | gc_md5 (const void *in, size_t inlen, void *resbuf) |
756 | { | 754 | { |
757 | md5_buffer (in, inlen, resbuf); | 755 | md5_buffer (in, inlen, resbuf); |
758 | return GC_OK; | 756 | return GC_OK; |
759 | } | 757 | } |
760 | #endif | 758 | #endif |
761 | 759 | ||
762 | #ifdef GNULIB_GC_SHA1 | 760 | #ifdef GNULIB_GC_SHA1 |
763 | Gc_rc | 761 | Gc_rc |
764 | gc_sha1 (const void *in, size_t inlen, void *resbuf) | 762 | gc_sha1 (const void *in, size_t inlen, void *resbuf) |
765 | { | 763 | { |
766 | sha1_buffer (in, inlen, resbuf); | 764 | sha1_buffer (in, inlen, resbuf); |
767 | return GC_OK; | 765 | return GC_OK; |
768 | } | 766 | } |
769 | #endif | 767 | #endif |
770 | 768 | ||
771 | #ifdef GNULIB_GC_HMAC_MD5 | 769 | #ifdef GNULIB_GC_HMAC_MD5 |
772 | Gc_rc | 770 | Gc_rc |
773 | gc_hmac_md5 (const void *key, size_t keylen, | 771 | gc_hmac_md5 (const void *key, size_t keylen, |
774 | const void *in, size_t inlen, char *resbuf) | 772 | const void *in, size_t inlen, char *resbuf) |
775 | { | 773 | { |
776 | hmac_md5 (key, keylen, in, inlen, resbuf); | 774 | hmac_md5 (key, keylen, in, inlen, resbuf); |
777 | return GC_OK; | 775 | return GC_OK; |
778 | } | 776 | } |
779 | #endif | 777 | #endif |
780 | 778 | ||
781 | #ifdef GNULIB_GC_HMAC_SHA1 | 779 | #ifdef GNULIB_GC_HMAC_SHA1 |
782 | Gc_rc gc_hmac_sha1(const void *key, | 780 | Gc_rc |
783 | size_t keylen, | 781 | gc_hmac_sha1 (const void *key, |
784 | const void *in, | 782 | size_t keylen, const void *in, size_t inlen, char *resbuf) |
785 | size_t inlen, | ||
786 | char *resbuf) | ||
787 | { | 783 | { |
788 | hmac_sha1(key, keylen, in, inlen, resbuf); | 784 | hmac_sha1 (key, keylen, in, inlen, resbuf); |
789 | return GC_OK; | 785 | return GC_OK; |
790 | } | 786 | } |
791 | #endif | 787 | #endif |