aboutsummaryrefslogtreecommitdiff
path: root/src/daemon/https/lgl/gc-gnulib.c
diff options
context:
space:
mode:
authorlv-426 <oxcafebaby@yahoo.com>2008-06-22 18:20:35 +0000
committerlv-426 <oxcafebaby@yahoo.com>2008-06-22 18:20:35 +0000
commita0339d2458867dbe9485499265641ff205063445 (patch)
tree055b38828b3696520408a32edf81df5bb37400f0 /src/daemon/https/lgl/gc-gnulib.c
parent97c026da05495b83f1511906c2ca027e12ef6cf7 (diff)
downloadlibmicrohttpd-a0339d2458867dbe9485499265641ff205063445.tar.gz
libmicrohttpd-a0339d2458867dbe9485499265641ff205063445.zip
initial GNU TLS import - this should reduce in size considerable
Diffstat (limited to 'src/daemon/https/lgl/gc-gnulib.c')
-rw-r--r--src/daemon/https/lgl/gc-gnulib.c791
1 files changed, 791 insertions, 0 deletions
diff --git a/src/daemon/https/lgl/gc-gnulib.c b/src/daemon/https/lgl/gc-gnulib.c
new file mode 100644
index 00000000..05b12781
--- /dev/null
+++ b/src/daemon/https/lgl/gc-gnulib.c
@@ -0,0 +1,791 @@
1/* gc-gnulib.c --- Common gnulib internal crypto interface functions
2 * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Simon Josefsson
3 *
4 * This file is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2.1, or (at your
7 * option) any later version.
8 *
9 * This file is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with this file; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 * 02110-1301, USA.
18 *
19 */
20
21/* Note: This file is only built if GC uses internal functions. */
22
23#include <config.h>
24
25/* Get prototype. */
26#include "gc.h"
27
28#include <stdlib.h>
29#include <string.h>
30
31/* For randomize. */
32#ifdef GNULIB_GC_RANDOM
33# include <unistd.h>
34# include <sys/types.h>
35# include <sys/stat.h>
36# include <fcntl.h>
37# include <errno.h>
38#endif
39
40/* Hashes. */
41#ifdef GNULIB_GC_MD5
42# include "md5.h"
43#endif
44#ifdef GNULIB_GC_SHA1
45# include "sha1.h"
46#endif
47#if defined(GNULIB_GC_HMAC_MD5) || defined(GNULIB_GC_HMAC_SHA1)
48# include "hmac.h"
49#endif
50
51/* Ciphers. */
52#ifdef GNULIB_GC_ARCFOUR
53# include "arcfour.h"
54#endif
55#ifdef GNULIB_GC_ARCTWO
56# include "arctwo.h"
57#endif
58#ifdef GNULIB_GC_DES
59# include "des.h"
60#endif
61#ifdef GNULIB_GC_RIJNDAEL
62# include "rijndael-api-fst.h"
63#endif
64
65/* The results of open() in this file are not used with fchdir,
66 therefore save some unnecessary work in fchdir.c. */
67#undef open
68#undef close
69
70Gc_rc gc_init(void)
71{
72 return GC_OK;
73}
74
75void gc_done(void)
76{
77 return;
78}
79
80#ifdef GNULIB_GC_RANDOM
81
82/* Randomness. */
83
84static Gc_rc
85randomize (int level, char *data, size_t datalen)
86 {
87 int fd;
88 const char *device;
89 size_t len = 0;
90 int rc;
91
92 switch (level)
93 {
94 case 0:
95 device = NAME_OF_NONCE_DEVICE;
96 break;
97
98 case 1:
99 device = NAME_OF_PSEUDO_RANDOM_DEVICE;
100 break;
101
102 default:
103 device = NAME_OF_RANDOM_DEVICE;
104 break;
105 }
106
107 if (strcmp (device, "no") == 0)
108 return GC_RANDOM_ERROR;
109
110 fd = open (device, O_RDONLY);
111 if (fd < 0)
112 return GC_RANDOM_ERROR;
113
114 do
115 {
116 ssize_t tmp;
117
118 tmp = read (fd, data, datalen);
119
120 if (tmp < 0)
121 {
122 int save_errno = errno;
123 close (fd);
124 errno = save_errno;
125 return GC_RANDOM_ERROR;
126 }
127
128 len += tmp;
129 }
130 while (len < datalen);
131
132 rc = close (fd);
133 if (rc < 0)
134 return GC_RANDOM_ERROR;
135
136 return GC_OK;
137 }
138
139Gc_rc
140gc_nonce (char *data, size_t datalen)
141 {
142 return randomize (0, data, datalen);
143 }
144
145Gc_rc
146gc_pseudo_random (char *data, size_t datalen)
147 {
148 return randomize (1, data, datalen);
149 }
150
151Gc_rc
152gc_random (char *data, size_t datalen)
153 {
154 return randomize (2, data, datalen);
155 }
156
157#endif
158
159/* Memory allocation. */
160void gc_set_allocators(gc_malloc_t func_malloc,
161 gc_malloc_t secure_malloc,
162 gc_secure_check_t secure_check,
163 gc_realloc_t func_realloc,
164 gc_free_t func_free)
165{
166 return;
167}
168
169/* Ciphers. */
170
171typedef struct _gc_cipher_ctx
172 {
173 Gc_cipher alg;
174 Gc_cipher_mode mode;
175#ifdef GNULIB_GC_ARCTWO
176 arctwo_context arctwoContext;
177 char arctwoIV[ARCTWO_BLOCK_SIZE];
178#endif
179#ifdef GNULIB_GC_ARCFOUR
180 arcfour_context arcfourContext;
181#endif
182#ifdef GNULIB_GC_DES
183 gl_des_ctx desContext;
184#endif
185#ifdef GNULIB_GC_RIJNDAEL
186 rijndaelKeyInstance aesEncKey;
187 rijndaelKeyInstance aesDecKey;
188 rijndaelCipherInstance aesContext;
189#endif
190 } _gc_cipher_ctx;
191
192Gc_rc gc_cipher_open(Gc_cipher alg,
193 Gc_cipher_mode mode,
194 gc_cipher_handle * outhandle)
195{
196 _gc_cipher_ctx *ctx;
197 Gc_rc rc = GC_OK;
198
199 ctx = calloc(sizeof (*ctx), 1);
200 if (!ctx)
201 return GC_MALLOC_ERROR;
202
203 ctx->alg = alg;
204 ctx->mode = mode;
205
206 switch (alg)
207 {
208#ifdef GNULIB_GC_ARCTWO
209 case GC_ARCTWO40:
210 switch (mode)
211 {
212 case GC_ECB:
213 case GC_CBC:
214 break;
215
216 default:
217 rc = GC_INVALID_CIPHER;
218 }
219 break;
220#endif
221
222#ifdef GNULIB_GC_ARCFOUR
223 case GC_ARCFOUR128:
224 case GC_ARCFOUR40:
225 switch (mode)
226 {
227 case GC_STREAM:
228 break;
229
230 default:
231 rc = GC_INVALID_CIPHER;
232 }
233 break;
234#endif
235
236#ifdef GNULIB_GC_DES
237 case GC_DES:
238 switch (mode)
239 {
240 case GC_ECB:
241 break;
242
243 default:
244 rc = GC_INVALID_CIPHER;
245 }
246 break;
247#endif
248
249#ifdef GNULIB_GC_RIJNDAEL
250 case GC_AES128:
251 case GC_AES192:
252 case GC_AES256:
253 switch (mode)
254 {
255 case GC_ECB:
256 case GC_CBC:
257 break;
258
259 default:
260 rc = GC_INVALID_CIPHER;
261 }
262 break;
263#endif
264
265 default:
266 rc = GC_INVALID_CIPHER;
267 }
268
269 if (rc == GC_OK)
270 *outhandle = ctx;
271 else
272 free(ctx);
273
274 return rc;
275}
276
277Gc_rc gc_cipher_setkey(gc_cipher_handle handle,
278 size_t keylen,
279 const char *key)
280{
281 _gc_cipher_ctx *ctx = handle;
282
283 switch (ctx->alg)
284 {
285#ifdef GNULIB_GC_ARCTWO
286 case GC_ARCTWO40:
287 arctwo_setkey (&ctx->arctwoContext, keylen, key);
288 break;
289#endif
290
291#ifdef GNULIB_GC_ARCFOUR
292 case GC_ARCFOUR128:
293 case GC_ARCFOUR40:
294 arcfour_setkey (&ctx->arcfourContext, key, keylen);
295 break;
296#endif
297
298#ifdef GNULIB_GC_DES
299 case GC_DES:
300 if (keylen != 8)
301 return GC_INVALID_CIPHER;
302 gl_des_setkey (&ctx->desContext, key);
303 break;
304#endif
305
306#ifdef GNULIB_GC_RIJNDAEL
307 case GC_AES128:
308 case GC_AES192:
309 case GC_AES256:
310 {
311 rijndael_rc rc;
312 size_t i;
313 char keyMaterial[RIJNDAEL_MAX_KEY_SIZE + 1];
314
315 for (i = 0; i < keylen; i++)
316 sprintf (&keyMaterial[2 * i], "%02x", key[i] & 0xFF);
317
318 rc = rijndaelMakeKey (&ctx->aesEncKey, RIJNDAEL_DIR_ENCRYPT,
319 keylen * 8, keyMaterial);
320 if (rc < 0)
321 return GC_INVALID_CIPHER;
322
323 rc = rijndaelMakeKey (&ctx->aesDecKey, RIJNDAEL_DIR_DECRYPT,
324 keylen * 8, keyMaterial);
325 if (rc < 0)
326 return GC_INVALID_CIPHER;
327
328 rc = rijndaelCipherInit (&ctx->aesContext, RIJNDAEL_MODE_ECB, NULL);
329 if (rc < 0)
330 return GC_INVALID_CIPHER;
331 }
332 break;
333#endif
334
335 default:
336 return GC_INVALID_CIPHER;
337 }
338
339 return GC_OK;
340}
341
342Gc_rc gc_cipher_setiv(gc_cipher_handle handle,
343 size_t ivlen,
344 const char *iv)
345{
346 _gc_cipher_ctx *ctx = handle;
347
348 switch (ctx->alg)
349 {
350#ifdef GNULIB_GC_ARCTWO
351 case GC_ARCTWO40:
352 if (ivlen != ARCTWO_BLOCK_SIZE)
353 return GC_INVALID_CIPHER;
354 memcpy (ctx->arctwoIV, iv, ivlen);
355 break;
356#endif
357
358#ifdef GNULIB_GC_RIJNDAEL
359 case GC_AES128:
360 case GC_AES192:
361 case GC_AES256:
362 switch (ctx->mode)
363 {
364 case GC_ECB:
365 /* Doesn't use IV. */
366 break;
367
368 case GC_CBC:
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;
383
384 default:
385 return GC_INVALID_CIPHER;
386 }
387 break;
388#endif
389
390 default:
391 return GC_INVALID_CIPHER;
392 }
393
394 return GC_OK;
395}
396
397Gc_rc gc_cipher_encrypt_inline(gc_cipher_handle handle,
398 size_t len,
399 char *data)
400{
401 _gc_cipher_ctx *ctx = handle;
402
403 switch (ctx->alg)
404 {
405#ifdef GNULIB_GC_ARCTWO
406 case GC_ARCTWO40:
407 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 {
417 size_t i;
418 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
419 data[i] ^= ctx->arctwoIV[i];
420 arctwo_encrypt (&ctx->arctwoContext, data, data,
421 ARCTWO_BLOCK_SIZE);
422 memcpy (ctx->arctwoIV, data, ARCTWO_BLOCK_SIZE);
423 }
424 break;
425
426 default:
427 return GC_INVALID_CIPHER;
428 }
429 break;
430#endif
431
432#ifdef GNULIB_GC_ARCFOUR
433 case GC_ARCFOUR128:
434 case GC_ARCFOUR40:
435 arcfour_stream (&ctx->arcfourContext, data, data, len);
436 break;
437#endif
438
439#ifdef GNULIB_GC_DES
440 case GC_DES:
441 for (; len >= 8; len -= 8, data += 8)
442 gl_des_ecb_encrypt (&ctx->desContext, data, data);
443 break;
444#endif
445
446#ifdef GNULIB_GC_RIJNDAEL
447 case GC_AES128:
448 case GC_AES192:
449 case GC_AES256:
450 {
451 int nblocks;
452
453 nblocks = rijndaelBlockEncrypt (&ctx->aesContext, &ctx->aesEncKey,
454 data, 8 * len, data);
455 if (nblocks < 0)
456 return GC_INVALID_CIPHER;
457 }
458 break;
459#endif
460
461 default:
462 return GC_INVALID_CIPHER;
463 }
464
465 return GC_OK;
466}
467
468Gc_rc gc_cipher_decrypt_inline(gc_cipher_handle handle,
469 size_t len,
470 char *data)
471{
472 _gc_cipher_ctx *ctx = handle;
473
474 switch (ctx->alg)
475 {
476#ifdef GNULIB_GC_ARCTWO
477 case GC_ARCTWO40:
478 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 {
488 char tmpIV[ARCTWO_BLOCK_SIZE];
489 size_t i;
490 memcpy (tmpIV, data, ARCTWO_BLOCK_SIZE);
491 arctwo_decrypt (&ctx->arctwoContext, data, data,
492 ARCTWO_BLOCK_SIZE);
493 for (i = 0; i < ARCTWO_BLOCK_SIZE; i++)
494 data[i] ^= ctx->arctwoIV[i];
495 memcpy (ctx->arctwoIV, tmpIV, ARCTWO_BLOCK_SIZE);
496 }
497 break;
498
499 default:
500 return GC_INVALID_CIPHER;
501 }
502 break;
503#endif
504
505#ifdef GNULIB_GC_ARCFOUR
506 case GC_ARCFOUR128:
507 case GC_ARCFOUR40:
508 arcfour_stream (&ctx->arcfourContext, data, data, len);
509 break;
510#endif
511
512#ifdef GNULIB_GC_DES
513 case GC_DES:
514 for (; len >= 8; len -= 8, data += 8)
515 gl_des_ecb_decrypt (&ctx->desContext, data, data);
516 break;
517#endif
518
519#ifdef GNULIB_GC_RIJNDAEL
520 case GC_AES128:
521 case GC_AES192:
522 case GC_AES256:
523 {
524 int nblocks;
525
526 nblocks = rijndaelBlockDecrypt (&ctx->aesContext, &ctx->aesDecKey,
527 data, 8 * len, data);
528 if (nblocks < 0)
529 return GC_INVALID_CIPHER;
530 }
531 break;
532#endif
533
534 default:
535 return GC_INVALID_CIPHER;
536 }
537
538 return GC_OK;
539}
540
541Gc_rc gc_cipher_close(gc_cipher_handle handle)
542{
543 _gc_cipher_ctx *ctx = handle;
544
545 if (ctx)
546 free(ctx);
547
548 return GC_OK;
549}
550
551/* Hashes. */
552
553#define MAX_DIGEST_SIZE 20
554
555typedef struct _gc_hash_ctx
556 {
557 Gc_hash alg;
558 Gc_hash_mode mode;
559 char hash[MAX_DIGEST_SIZE];
560#ifdef GNULIB_GC_MD5
561 struct md5_ctx md5Context;
562#endif
563#ifdef GNULIB_GC_SHA1
564 struct sha1_ctx sha1Context;
565#endif
566 } _gc_hash_ctx;
567
568Gc_rc gc_hash_open(Gc_hash hash,
569 Gc_hash_mode mode,
570 gc_hash_handle * outhandle)
571{
572 _gc_hash_ctx *ctx;
573 Gc_rc rc = GC_OK;
574
575 ctx = calloc(sizeof (*ctx), 1);
576 if (!ctx)
577 return GC_MALLOC_ERROR;
578
579 ctx->alg = hash;
580 ctx->mode = mode;
581
582 switch (hash)
583 {
584#ifdef GNULIB_GC_MD5
585 case GC_MD5:
586 md5_init_ctx (&ctx->md5Context);
587 break;
588#endif
589
590#ifdef GNULIB_GC_SHA1
591 case GC_SHA1:
592 sha1_init_ctx (&ctx->sha1Context);
593 break;
594#endif
595
596 default:
597 rc = GC_INVALID_HASH;
598 break;
599 }
600
601 switch (mode)
602 {
603 case 0:
604 break;
605
606 default:
607 rc = GC_INVALID_HASH;
608 break;
609 }
610
611 if (rc == GC_OK)
612 *outhandle = ctx;
613 else
614 free(ctx);
615
616 return rc;
617}
618
619Gc_rc gc_hash_clone(gc_hash_handle handle,
620 gc_hash_handle * outhandle)
621{
622 _gc_hash_ctx *in = handle;
623 _gc_hash_ctx *out;
624
625 *outhandle = out = calloc(sizeof (*out), 1);
626 if (!out)
627 return GC_MALLOC_ERROR;
628
629 memcpy(out, in, sizeof (*out));
630
631 return GC_OK;
632}
633
634size_t gc_hash_digest_length(Gc_hash hash)
635{
636 size_t len;
637
638 switch (hash)
639 {
640 case GC_MD2:
641 len = GC_MD2_DIGEST_SIZE;
642 break;
643
644 case GC_MD4:
645 len = GC_MD4_DIGEST_SIZE;
646 break;
647
648 case GC_MD5:
649 len = GC_MD5_DIGEST_SIZE;
650 break;
651
652 case GC_RMD160:
653 len = GC_RMD160_DIGEST_SIZE;
654 break;
655
656 case GC_SHA1:
657 len = GC_SHA1_DIGEST_SIZE;
658 break;
659
660 default:
661 return 0;
662 }
663
664 return len;
665}
666
667void gc_hash_write(gc_hash_handle handle,
668 size_t len,
669 const char *data)
670{
671 _gc_hash_ctx *ctx = handle;
672
673 switch (ctx->alg)
674 {
675#ifdef GNULIB_GC_MD5
676 case GC_MD5:
677 md5_process_bytes (data, len, &ctx->md5Context);
678 break;
679#endif
680
681#ifdef GNULIB_GC_SHA1
682 case GC_SHA1:
683 sha1_process_bytes (data, len, &ctx->sha1Context);
684 break;
685#endif
686
687 default:
688 break;
689 }
690}
691
692const char * gc_hash_read(gc_hash_handle handle)
693{
694 _gc_hash_ctx *ctx = handle;
695 const char *ret= NULL;
696
697 switch (ctx->alg)
698 {
699#ifdef GNULIB_GC_MD5
700 case GC_MD5:
701 md5_finish_ctx (&ctx->md5Context, ctx->hash);
702 ret = ctx->hash;
703 break;
704#endif
705
706#ifdef GNULIB_GC_SHA1
707 case GC_SHA1:
708 sha1_finish_ctx (&ctx->sha1Context, ctx->hash);
709 ret = ctx->hash;
710 break;
711#endif
712
713 default:
714 return NULL;
715 }
716
717 return ret;
718}
719
720void gc_hash_close(gc_hash_handle handle)
721{
722 _gc_hash_ctx *ctx = handle;
723
724 free(ctx);
725}
726
727Gc_rc gc_hash_buffer(Gc_hash hash,
728 const void *in,
729 size_t inlen,
730 char *resbuf)
731{
732 switch (hash)
733 {
734#ifdef GNULIB_GC_MD5
735 case GC_MD5:
736 md5_buffer (in, inlen, resbuf);
737 break;
738#endif
739
740#ifdef GNULIB_GC_SHA1
741 case GC_SHA1:
742 sha1_buffer (in, inlen, resbuf);
743 break;
744#endif
745
746 default:
747 return GC_INVALID_HASH;
748 }
749
750 return GC_OK;
751}
752
753#ifdef GNULIB_GC_MD5
754Gc_rc
755gc_md5 (const void *in, size_t inlen, void *resbuf)
756 {
757 md5_buffer (in, inlen, resbuf);
758 return GC_OK;
759 }
760#endif
761
762#ifdef GNULIB_GC_SHA1
763Gc_rc
764gc_sha1 (const void *in, size_t inlen, void *resbuf)
765 {
766 sha1_buffer (in, inlen, resbuf);
767 return GC_OK;
768 }
769#endif
770
771#ifdef GNULIB_GC_HMAC_MD5
772Gc_rc
773gc_hmac_md5 (const void *key, size_t keylen,
774 const void *in, size_t inlen, char *resbuf)
775 {
776 hmac_md5 (key, keylen, in, inlen, resbuf);
777 return GC_OK;
778 }
779#endif
780
781#ifdef GNULIB_GC_HMAC_SHA1
782Gc_rc gc_hmac_sha1(const void *key,
783 size_t keylen,
784 const void *in,
785 size_t inlen,
786 char *resbuf)
787{
788 hmac_sha1(key, keylen, in, inlen, resbuf);
789 return GC_OK;
790}
791#endif