Add generic crypto module.
[gnulib.git] / lib / gc-gnulib.c
1 /* gc-gl-common.c --- Common gnulib internal crypto interface functions
2  * Copyright (C) 2002, 2003, 2004, 2005  Simon Josefsson
3  *
4  * This file is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published
6  * by the Free Software Foundation; either version 2, 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 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 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdlib.h>
28
29 /* Get prototype. */
30 #include <gc.h>
31
32 /* For randomize. */
33 #include <unistd.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <fcntl.h>
37 #include <errno.h>
38
39 #include <string.h>
40
41 int
42 gc_init (void)
43 {
44   return 0;
45 }
46
47 void
48 gc_done (void)
49 {
50   return;
51 }
52
53 /* Randomness. */
54
55 static int
56 randomize (int level, char *data, size_t datalen)
57 {
58   int fd;
59   const char *device;
60   size_t len = 0;
61   int rc;
62
63   switch (level)
64     {
65     case 0:
66       device = NAME_OF_NONCE_DEVICE;
67       break;
68
69     case 1:
70       device = NAME_OF_PSEUDO_RANDOM_DEVICE;
71       break;
72
73     default:
74       device = NAME_OF_RANDOM_DEVICE;
75       break;
76     }
77
78   fd = open (device, O_RDONLY);
79   if (fd < 0)
80     return GC_RANDOM_ERROR;
81
82   do
83     {
84       ssize_t tmp;
85
86       tmp = read (fd, data, datalen);
87
88       if (tmp < 0)
89         {
90           int save_errno = errno;
91           close (fd);
92           errno = save_errno;
93           return GC_RANDOM_ERROR;
94         }
95
96       len += tmp;
97     }
98   while (len < datalen);
99
100   rc = close (fd);
101   if (rc < 0)
102     return GC_RANDOM_ERROR;
103
104   return GC_OK;
105 }
106
107 int
108 gc_nonce (char *data, size_t datalen)
109 {
110   return randomize (0, data, datalen);
111 }
112
113 int
114 gc_pseudo_random (char *data, size_t datalen)
115 {
116   return randomize (1, data, datalen);
117 }
118
119 int
120 gc_random (char *data, size_t datalen)
121 {
122   return randomize (2, data, datalen);
123 }
124
125 /* Memory allocation. */
126
127 void
128 gc_set_allocators (gc_malloc_t func_malloc,
129                    gc_malloc_t secure_malloc,
130                    gc_secure_check_t secure_check,
131                    gc_realloc_t func_realloc, gc_free_t func_free)
132 {
133   return;
134 }
135
136 #include "md5.h"
137
138 int
139 gc_md5 (const void *in, size_t inlen, void *resbuf)
140 {
141   md5_buffer (in, inlen, resbuf);
142   return 0;
143 }
144
145 #include "hmac.h"
146
147 int
148 gc_hmac_md5 (const void *key, size_t keylen,
149              const void *in, size_t inlen, char *resbuf)
150 {
151   hmac_md5 (key, keylen, in, inlen, resbuf);
152   return 0;
153 }