source: roaraudio/libroar/hash.c @ 4670:8eeb660de23d

Last change on this file since 4670:8eeb660de23d was 4670:8eeb660de23d, checked in by phi, 13 years ago

some statefull general hash api...

File size: 8.0 KB
Line 
1//hash.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010
5 *
6 *  This file is part of libroar a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroar is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 *  NOTE for everyone want's to change something and send patches:
25 *  read README and HACKING! There a addition information on
26 *  the license of this document you need to read before you send
27 *  any patches.
28 *
29 *  NOTE for uses of non-GPL (LGPL,...) software using libesd, libartsc
30 *  or libpulse*:
31 *  The libs libroaresd, libroararts and libroarpulse link this lib
32 *  and are therefore GPL. Because of this it may be illigal to use
33 *  them with any software that uses libesd, libartsc or libpulse*.
34 */
35
36#include "libroar.h"
37
38#ifdef ROAR_HAVE_LIBGCRYPT
39#include <gcrypt.h>
40#endif
41
42struct roar_hash_state {
43 struct roar_hash_cmds * cmds;
44 void * state;
45};
46
47static const struct hashes {
48 const int     id;
49 const char *  name;
50 const ssize_t dlen;
51} _libroar_hashes[] = {
52/*
53grep '^  +HT_' doc/new-cmds | sed 's/ *#(.*)$//; s/^  +HT_//; s/ *=.*$//' | while read n; do printf " {ROAR_HT_%-12s \"%-12s -1   },\n" $n, $n\",; done
54*/
55 {ROAR_HT_NONE,        "NONE",       -1   },
56 {ROAR_HT_MD5,         "MD5",         16  },
57 {ROAR_HT_SHA1,        "SHA1",        20  },
58 {ROAR_HT_RIPEMD160,   "RIPEMD160",   20  },
59 {ROAR_HT_MD2,         "MD2",        -1   },
60 {ROAR_HT_TIGER,       "TIGER",       24  },
61 {ROAR_HT_HAVAL,       "HAVAL",      -1   },
62 {ROAR_HT_SHA256,      "SHA256",      32  },
63 {ROAR_HT_SHA384,      "SHA384",      48  },
64 {ROAR_HT_SHA512,      "SHA512",      64  },
65 {ROAR_HT_SHA224,      "SHA224",      28  },
66 {ROAR_HT_MD4,         "MD4",         16  },
67 {ROAR_HT_CRC32,       "CRC32",       4   },
68 {ROAR_HT_RFC1510,     "RFC1510",     4   },
69 {ROAR_HT_RFC2440,     "RFC2440",     3   },
70 {ROAR_HT_WHIRLPOOL,   "WHIRLPOOL",   64  },
71 {ROAR_HT_UUID,        "UUID",        16  },
72 {ROAR_HT_GTN8,        "GTN8",        1   },
73 {ROAR_HT_GTN16,       "GTN16",       2   },
74 {ROAR_HT_GTN32,       "GTN32",       4   },
75 {ROAR_HT_GTN64,       "GTN64",       8   },
76 {ROAR_HT_CLIENTID,    "CLIENTID",   -1   },
77 {ROAR_HT_STREAMID,    "STREAMID",   -1   },
78 {ROAR_HT_SOURCEID,    "SOURCEID",   -1   },
79 {ROAR_HT_SAMPLEID,    "SAMPLEID",   -1   },
80 {ROAR_HT_MIXERID,     "MIXERID",    -1   },
81 {ROAR_HT_BRIDGEID,    "BRIDGEID",   -1   },
82 {ROAR_HT_LISTENID,    "LISTENID",   -1   },
83 {ROAR_HT_ACTIONID,    "ACTIONID",   -1   },
84 {ROAR_HT_MSGQUEUEID,  "MSGQUEUEID", -1   },
85 {ROAR_HT_MSGBUSID,    "MSGBUSID",   -1   },
86 {ROAR_HT_GTIN8,       "GTIN8",       4   },
87 {ROAR_HT_GTIN13,      "GTIN13",      8   },
88 {ROAR_HT_ISBN10,      "ISBN10",      8   },
89 {ROAR_HT_ISBN13,      "ISBN13",      8   },
90 {-1, NULL}
91};
92
93static struct roar_hash_cmds _libroar_hash_cmds[] = {
94 {ROAR_HT_TIGER, sizeof(struct roar_hash_tiger), 512,
95  (int (*)(void *))roar_hash_tiger_init, (int (*)(void *))roar_hash_tiger_uninit,
96  (int (*)(void *, void *, size_t *))roar_hash_tiger_get_digest,
97  (int (*)(void *, const void *))roar_hash_tiger_proc_block,
98  (int (*)(void *, const void *, size_t))roar_hash_tiger_proc
99 },
100 {-1, -1, -1, NULL, NULL, NULL, NULL, NULL}
101};
102
103static struct roar_hash_cmds * roar_ht2cmds(const int ht) {
104 size_t i;
105
106 for(i = 0; _libroar_hash_cmds[i].algo != -1; i++)
107  if ( _libroar_hash_cmds[i].algo == ht )
108   return &(_libroar_hash_cmds[i]);
109
110 return NULL;
111}
112
113static inline int roar_ht2gcrypt_tested (const int ht) {
114#ifdef ROAR_HAVE_LIBGCRYPT
115 const char * name;
116
117 if ( ht > 512 )
118  return -1;
119
120 // test the algo:
121 name = gcry_md_algo_name(ht);
122
123 if ( name == NULL )
124  return -1;
125
126 if ( *name == 0 )
127  return -1;
128
129 return ht;
130#else
131 return -1;
132#endif
133}
134
135const char * roar_ht2str (const int    ht) {
136 int i;
137
138 for (i = 0; _libroar_hashes[i].id != -1; i++)
139  if ( _libroar_hashes[i].id == ht )
140   return _libroar_hashes[i].name;
141
142 return NULL;
143}
144
145int          roar_str2ht (const char * ht) {
146 int i;
147
148 for (i = 0; _libroar_hashes[i].id != -1; i++)
149  if ( !strcasecmp(_libroar_hashes[i].name, ht) )
150   return _libroar_hashes[i].id;
151
152 return -1;
153}
154
155ssize_t      roar_ht_digestlen (const int    ht) {
156 int i;
157
158 for (i = 0; _libroar_hashes[i].id != -1; i++)
159  if ( _libroar_hashes[i].id == ht )
160   return _libroar_hashes[i].dlen;
161
162 return -1;
163}
164
165int          roar_ht_is_supported(const int    ht) {
166 roar_crypto_init();
167
168 if ( roar_ht2cmds(ht) != NULL )
169  return 1;
170
171#ifdef ROAR_HAVE_LIBGCRYPT
172 if ( roar_ht2gcrypt_tested(ht) == -1 )
173  return 0;
174
175 return 1;
176#else
177 return 0;
178#endif
179}
180
181struct roar_hash_state * roar_hash_new(int algo) {
182 struct roar_hash_cmds  * cmds = roar_ht2cmds(algo);
183 struct roar_hash_state * self;
184
185 if ( cmds == NULL )
186  return NULL;
187
188 self = roar_mm_malloc(sizeof(struct roar_hash_state));
189
190 if ( self == NULL )
191  return NULL;
192
193 memset(self, 0, sizeof(struct roar_hash_state));
194
195 self->cmds = cmds;
196
197 self->state = roar_mm_malloc(cmds->statelen);
198
199 if ( self->state == NULL ) {
200  roar_mm_free(self);
201  return NULL;
202 }
203
204 memset(self->state, 0, cmds->statelen);
205
206 if ( cmds->init != NULL ) {
207  if ( cmds->init(self->state) == -1 ) {
208   roar_mm_free(self->state);
209   roar_mm_free(self);
210   return NULL;
211  }
212 }
213
214 return self;
215}
216
217int roar_hash_free(struct roar_hash_state * state) {
218 int ret = 0;
219
220 if ( state == NULL )
221  return -1;
222
223 if ( state->cmds->uninit != NULL )
224  ret = state->cmds->uninit(state->state);
225
226 roar_mm_free(state->state);
227 roar_mm_free(state);
228
229 return ret;
230}
231
232int roar_hash_digest(struct roar_hash_state * state, void * digest, size_t * len) {
233 if ( state == NULL )
234  return -1;
235
236 if ( state->cmds->digest == NULL )
237  return -1;
238
239 return state->cmds->digest(state->state, digest, len);
240}
241
242int roar_hash_proc(struct roar_hash_state * state, const void * data, size_t len) {
243 if ( state == NULL )
244  return -1;
245
246 if ( state->cmds->proc == NULL )
247  return -1;
248
249 return state->cmds->proc(state->state, data, len);
250}
251
252int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo) {
253 roar_crypto_init();
254
255 return roar_hash_salted_buffer(digest, data, datalen, algo, NULL, 0);
256}
257
258#ifdef ROAR_HAVE_LIBGCRYPT
259static inline int roar_hash_salted_buffer_gcrypt(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
260 gcry_md_hd_t hdl;
261
262 roar_crypto_init();
263
264 algo = roar_ht2gcrypt_tested(algo);
265 if ( algo == -1 )
266  return -1;
267
268
269 if ( salt == NULL ) {
270  // optimized for unsalted:
271  gcry_md_hash_buffer(algo, digest, data, datalen);
272  return 0;
273 } else {
274  if ( gcry_md_open(&hdl, algo, 0) != 0 )
275   return -1;
276
277  gcry_md_write(hdl, data, datalen);
278  gcry_md_write(hdl, salt, saltlen);
279
280  memcpy(digest, gcry_md_read(hdl, algo), gcry_md_get_algo_dlen(algo));
281
282  gcry_md_close(hdl);
283 }
284
285 return 0;
286}
287#endif
288
289int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
290 struct roar_hash_state * state;
291 size_t len;
292 int ret;
293
294 if ( digest == NULL || data == NULL )
295  return -1;
296
297 len = roar_ht_digestlen(algo);
298 if ( len == -1 )
299  return -1;
300
301 if ( (state = roar_hash_new(algo)) != NULL ) {
302  if ( roar_hash_proc(state, data, datalen) == -1 )
303   ret = -1;
304
305  if ( saltlen != 0 )
306   if ( roar_hash_proc(state, salt, saltlen) == -1 )
307    ret = -1;
308
309  if ( roar_hash_digest(state, digest, &len) == -1 )
310   ret = -1;
311
312  if ( roar_hash_free(state) == -1 )
313   ret = -1;
314
315  return ret;
316 }
317
318#ifdef ROAR_HAVE_LIBGCRYPT
319 return roar_hash_salted_buffer_gcrypt(digest, data, datalen, algo, salt, saltlen);
320#else
321 return -1;
322#endif
323}
324
325//ll
Note: See TracBrowser for help on using the repository browser.