source: roaraudio/libroar/hash.c @ 4795:cf3e4ecde1f5

Last change on this file since 4795:cf3e4ecde1f5 was 4795:cf3e4ecde1f5, checked in by phi, 13 years ago

Added Adler32 support (Closes: #123) (pr1)

File size: 8.9 KB
Line 
1//hash.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2010-2011
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 uint_least32_t 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 {ROAR_HT_ADLER32,     "ADLER32",     4   },
91 {-1, NULL}
92};
93
94static struct roar_hash_cmds _libroar_hash_cmds[] = {
95 {ROAR_HT_TIGER, sizeof(struct roar_hash_tiger), 512,
96  (int (*)(void *))roar_hash_tiger_init, (int (*)(void *))roar_hash_tiger_uninit,
97  (int (*)(void *, void *, size_t *))roar_hash_tiger_get_digest,
98  (int (*)(void *, const void *))roar_hash_tiger_proc_block,
99  (int (*)(void *, const void *, size_t))roar_hash_tiger_proc
100 },
101 {ROAR_HT_RFC2440, sizeof(uint32_t), -1,
102  roar_hash_crc24_init, NULL,
103  roar_hash_crc24_digest, NULL, roar_hash_crc24_proc
104 },
105 {ROAR_HT_ADLER32, sizeof(uint32_t), -1,
106  roar_hash_adler32_init, NULL,
107  roar_hash_adler32_digest, NULL, roar_hash_adler32_proc
108 },
109 {-1, -1, -1, NULL, NULL, NULL, NULL, NULL}
110};
111
112static struct roar_hash_cmds * roar_ht2cmds(const int ht) {
113 size_t i;
114
115 for(i = 0; _libroar_hash_cmds[i].algo != -1; i++)
116  if ( _libroar_hash_cmds[i].algo == ht )
117   return &(_libroar_hash_cmds[i]);
118
119 return NULL;
120}
121
122static inline int roar_ht2gcrypt_tested (const int ht) {
123#ifdef ROAR_HAVE_LIBGCRYPT
124 const char * name;
125
126 if ( ht > 512 )
127  return -1;
128
129 // test the algo:
130 name = gcry_md_algo_name(ht);
131
132 if ( name == NULL )
133  return -1;
134
135 if ( *name == 0 )
136  return -1;
137
138 return ht;
139#else
140 return -1;
141#endif
142}
143
144const char * roar_ht2str (const int    ht) {
145 int i;
146
147 for (i = 0; _libroar_hashes[i].id != -1; i++)
148  if ( _libroar_hashes[i].id == ht )
149   return _libroar_hashes[i].name;
150
151 return NULL;
152}
153
154int          roar_str2ht (const char * ht) {
155 int i;
156
157 for (i = 0; _libroar_hashes[i].id != -1; i++)
158  if ( !strcasecmp(_libroar_hashes[i].name, ht) )
159   return _libroar_hashes[i].id;
160
161 return -1;
162}
163
164ssize_t      roar_ht_digestlen (const int    ht) {
165 int i;
166
167 for (i = 0; _libroar_hashes[i].id != -1; i++)
168  if ( _libroar_hashes[i].id == ht )
169   return _libroar_hashes[i].dlen;
170
171 return -1;
172}
173
174int          roar_ht_is_supported(const int    ht) {
175 roar_crypto_init();
176
177 if ( roar_ht2cmds(ht) != NULL )
178  return 1;
179
180#ifdef ROAR_HAVE_LIBGCRYPT
181 if ( roar_ht2gcrypt_tested(ht) == -1 )
182  return 0;
183
184 return 1;
185#else
186 return 0;
187#endif
188}
189
190struct roar_hash_state * roar_hash_new(int algo) {
191 struct roar_hash_cmds  * cmds = roar_ht2cmds(algo);
192 struct roar_hash_state * self;
193
194 if ( cmds == NULL )
195  return NULL;
196
197 self = roar_mm_malloc(sizeof(struct roar_hash_state));
198
199 if ( self == NULL )
200  return NULL;
201
202 memset(self, 0, sizeof(struct roar_hash_state));
203
204 self->cmds = cmds;
205
206 self->state = roar_mm_malloc(cmds->statelen);
207
208 if ( self->state == NULL ) {
209  roar_mm_free(self);
210  return NULL;
211 }
212
213 memset(self->state, 0, cmds->statelen);
214
215 if ( cmds->init != NULL ) {
216  if ( cmds->init(self->state) == -1 ) {
217   roar_mm_free(self->state);
218   roar_mm_free(self);
219   return NULL;
220  }
221 }
222
223 return self;
224}
225
226int roar_hash_free(struct roar_hash_state * state) {
227 int ret = 0;
228
229 if ( state == NULL )
230  return -1;
231
232 if ( state->cmds->uninit != NULL )
233  ret = state->cmds->uninit(state->state);
234
235 roar_mm_free(state->state);
236 roar_mm_free(state);
237
238 return ret;
239}
240
241int roar_hash_digest(struct roar_hash_state * state, void * digest, size_t * len) {
242 if ( state == NULL )
243  return -1;
244
245 if ( state->cmds->digest == NULL )
246  return -1;
247
248 return state->cmds->digest(state->state, digest, len);
249}
250
251int roar_hash_proc(struct roar_hash_state * state, const void * data, size_t len) {
252 if ( state == NULL )
253  return -1;
254
255 if ( state->cmds->proc == NULL )
256  return -1;
257
258 return state->cmds->proc(state->state, data, len);
259}
260
261int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo) {
262 roar_crypto_init();
263
264 return roar_hash_salted_buffer(digest, data, datalen, algo, NULL, 0);
265}
266
267#ifdef ROAR_HAVE_LIBGCRYPT
268static inline int roar_hash_salted_buffer_gcrypt(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
269 gcry_md_hd_t hdl;
270
271 roar_crypto_init();
272
273 algo = roar_ht2gcrypt_tested(algo);
274 if ( algo == -1 )
275  return -1;
276
277
278 if ( salt == NULL ) {
279  // optimized for unsalted:
280  gcry_md_hash_buffer(algo, digest, data, datalen);
281  return 0;
282 } else {
283  if ( gcry_md_open(&hdl, algo, 0) != 0 )
284   return -1;
285
286  gcry_md_write(hdl, data, datalen);
287  gcry_md_write(hdl, salt, saltlen);
288
289  memcpy(digest, gcry_md_read(hdl, algo), gcry_md_get_algo_dlen(algo));
290
291  gcry_md_close(hdl);
292 }
293
294 return 0;
295}
296#endif
297
298int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
299 struct roar_hash_state * state;
300 size_t len;
301 int ret = 0;
302
303 ROAR_DBG("roar_hash_salted_buffer(digest=%p, data=%p, datalen=%llu, algo=%i, salt=%p, saltlen=%llu) = ?", digest, data, (unsigned long long int)datalen, algo, salt, (unsigned long long int)saltlen);
304
305 if ( digest == NULL || data == NULL )
306  return -1;
307
308 len = roar_ht_digestlen(algo);
309 if ( len == -1 )
310  return -1;
311
312 if ( (state = roar_hash_new(algo)) != NULL ) {
313  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
314
315  if ( roar_hash_proc(state, data, datalen) == -1 )
316   ret = -1;
317
318  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
319
320  if ( saltlen != 0 )
321   if ( roar_hash_proc(state, salt, saltlen) == -1 )
322    ret = -1;
323
324  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
325
326  if ( roar_hash_digest(state, digest, &len) == -1 )
327   ret = -1;
328
329  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
330
331  if ( roar_hash_free(state) == -1 )
332   ret = -1;
333
334  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
335
336  return ret;
337 }
338
339 ROAR_DBG("roar_hash_salted_buffer(*): state=%p", state);
340
341#ifdef ROAR_HAVE_LIBGCRYPT
342 return roar_hash_salted_buffer_gcrypt(digest, data, datalen, algo, salt, saltlen);
343#else
344 return -1;
345#endif
346}
347
348//ll
Note: See TracBrowser for help on using the repository browser.