* gc.h, gc-gnulib.c, gc-libgcrypt.c: Use Gc_rc for return types,
[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 /* Get prototype. */
28 #include <gc.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 /* For randomize. */
34 #include <unistd.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <errno.h>
39
40 #ifdef GC_USE_MD5
41 # include "md5.h"
42 #endif
43 #ifdef GC_USE_SHA1
44 # include "sha1.h"
45 #endif
46 #ifdef GC_USE_HMAC_MD5
47 # include "hmac.h"
48 #endif
49
50 Gc_rc
51 gc_init (void)
52 {
53   return GC_OK;
54 }
55
56 void
57 gc_done (void)
58 {
59   return;
60 }
61
62 /* Randomness. */
63
64 static Gc_rc
65 randomize (int level, char *data, size_t datalen)
66 {
67   int fd;
68   const char *device;
69   size_t len = 0;
70   int rc;
71
72   switch (level)
73     {
74     case 0:
75       device = NAME_OF_NONCE_DEVICE;
76       break;
77
78     case 1:
79       device = NAME_OF_PSEUDO_RANDOM_DEVICE;
80       break;
81
82     default:
83       device = NAME_OF_RANDOM_DEVICE;
84       break;
85     }
86
87   fd = open (device, O_RDONLY);
88   if (fd < 0)
89     return GC_RANDOM_ERROR;
90
91   do
92     {
93       ssize_t tmp;
94
95       tmp = read (fd, data, datalen);
96
97       if (tmp < 0)
98         {
99           int save_errno = errno;
100           close (fd);
101           errno = save_errno;
102           return GC_RANDOM_ERROR;
103         }
104
105       len += tmp;
106     }
107   while (len < datalen);
108
109   rc = close (fd);
110   if (rc < 0)
111     return GC_RANDOM_ERROR;
112
113   return GC_OK;
114 }
115
116 Gc_rc
117 gc_nonce (char *data, size_t datalen)
118 {
119   return randomize (0, data, datalen);
120 }
121
122 Gc_rc
123 gc_pseudo_random (char *data, size_t datalen)
124 {
125   return randomize (1, data, datalen);
126 }
127
128 Gc_rc
129 gc_random (char *data, size_t datalen)
130 {
131   return randomize (2, data, datalen);
132 }
133
134 /* Memory allocation. */
135
136 void
137 gc_set_allocators (gc_malloc_t func_malloc,
138                    gc_malloc_t secure_malloc,
139                    gc_secure_check_t secure_check,
140                    gc_realloc_t func_realloc, gc_free_t func_free)
141 {
142   return;
143 }
144
145 /* Hashes. */
146
147 Gc_rc
148 gc_hash_buffer (Gc_hash hash, const void *in, size_t inlen, char *resbuf)
149 {
150   switch (hash)
151     {
152 #ifdef GC_USE_MD5
153     case GC_MD5:
154       md5_buffer (in, inlen, resbuf);
155       break;
156 #endif
157
158 #ifdef GC_USE_SHA1
159     case GC_SHA1:
160       sha1_buffer (in, inlen, resbuf);
161       break;
162 #endif
163
164     default:
165       return GC_INVALID_HASH;
166     }
167
168   return GC_OK;
169 }
170
171 #ifdef GC_USE_MD5
172 Gc_rc
173 gc_md5 (const void *in, size_t inlen, void *resbuf)
174 {
175   md5_buffer (in, inlen, resbuf);
176   return GC_OK;
177 }
178 #endif
179
180 #ifdef GC_USE_SHA1
181 Gc_rc
182 gc_sha1 (const void *in, size_t inlen, void *resbuf)
183 {
184   sha1_buffer (in, inlen, resbuf);
185   return GC_OK;
186 }
187 #endif
188
189 #ifdef GC_USE_HMAC_MD5
190 Gc_rc
191 gc_hmac_md5 (const void *key, size_t keylen,
192              const void *in, size_t inlen, char *resbuf)
193 {
194   hmac_md5 (key, keylen, in, inlen, resbuf);
195   return GC_OK;
196 }
197 #endif
198
199 #ifdef GC_USE_HMAC_SHA1
200 Gc_rc
201 gc_hmac_sha1 (const void *key, size_t keylen,
202               const void *in, size_t inlen, char *resbuf)
203 {
204   hmac_sha1 (key, keylen, in, inlen, resbuf);
205   return GC_OK;
206 }
207 #endif