source: roaraudio/libroar/hash.c @ 4877:f204efc45b07

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

added roar_ht_digest2str()

File size: 11.5 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
174static void _bin2hex(char * out, char * in, size_t inlen, int uppercase) {
175 const char * tab = uppercase ? "0123456789ABCDEF" : "0123456789abcdef";
176 unsigned char c;
177 int nib0, nib1;
178
179 for (; inlen; inlen--) {
180  c = *(in++);
181  nib0 = (c & 0x0F) >> 0;
182  nib1 = (c & 0xF0) >> 4;
183//  printf("inlen=%zu, c=%u, nibs={%i, %i}\n", inlen, (unsigned)c, nib1, nib0);
184  *(out++) = tab[nib1];
185  *(out++) = tab[nib0];
186 }
187 *out = 0;
188}
189
190ssize_t      roar_ht_digest2str(char * out, size_t outlen, void * digest, size_t digestlen, int ht) {
191 ssize_t slen = roar_ht_digestlen(ht);
192 union {
193  long long int llsi;
194 } tmp;
195 enum {
196  HEX,
197  TIGER,
198//  BASE64,
199  UUID,
200  INT,
201  GTIN
202 } type = HEX;
203
204 if ( out == NULL || digest == NULL ) {
205  roar_err_set(ROAR_ERROR_FAULT);
206  return -1;
207 }
208
209 if ( !(slen == -1 || digestlen == slen) ) {
210  roar_err_set(ROAR_ERROR_INVAL);
211  return -1;
212 }
213
214 switch (ht) {
215  case ROAR_HT_TIGER: type = TIGER; break;
216  case ROAR_HT_UUID:  type = UUID;  break;
217  case ROAR_HT_CLIENTID:
218  case ROAR_HT_STREAMID:
219  case ROAR_HT_SOURCEID:
220  case ROAR_HT_SAMPLEID:
221  case ROAR_HT_MIXERID:
222  case ROAR_HT_BRIDGEID:
223  case ROAR_HT_LISTENID:
224  case ROAR_HT_ACTIONID:
225  case ROAR_HT_MSGQUEUEID:
226  case ROAR_HT_MSGBUSID:
227    type = INT;
228   break;
229  case ROAR_HT_GTIN8:
230  case ROAR_HT_GTIN13:
231  case ROAR_HT_ISBN10:
232    type = GTIN;
233   break;
234 }
235
236 switch (type) {
237  case HEX:
238    if ( outlen < (digestlen*2 + 1) ) {
239     roar_err_set(ROAR_ERROR_FAULT);
240     return -1;
241    }
242    _bin2hex(out, digest, digestlen, 0);
243   break;
244  case UUID:
245    if ( outlen < 37 || digestlen != 16 ) {
246     roar_err_set(ROAR_ERROR_FAULT);
247     return -1;
248    }
249    _bin2hex(out+ 0, digest+ 0, 4, 0);
250    out[ 8] = '-';
251    _bin2hex(out+ 9, digest+ 4, 2, 0);
252    out[13] = '-';
253    _bin2hex(out+14, digest+ 6, 2, 0);
254    out[18] = '-';
255    _bin2hex(out+19, digest+ 8, 2, 0);
256    out[23] = '-';
257    _bin2hex(out+24, digest+10, 6, 0);
258//    printf("%s\n", out+24);
259   break;
260  case TIGER:
261    if ( outlen < 51 || digestlen != 24 ) {
262     roar_err_set(ROAR_ERROR_FAULT);
263     return -1;
264    }
265    _bin2hex(out+ 0, digest+ 0, 8, 1);
266    out[16] = ' ';
267    _bin2hex(out+17, digest+ 8, 8, 1);
268    out[33] = ' ';
269    _bin2hex(out+34, digest+16, 8, 1);
270   break;
271  case INT:
272    switch (digestlen) {
273     case 1:
274       tmp.llsi = *(char*)digest;
275      break;
276     case 2:
277       tmp.llsi = ROAR_NET2HOST16(*(int16_t*)digest);
278      break;
279     case 4:
280       tmp.llsi = ROAR_NET2HOST32(*(int32_t*)digest);
281      break;
282     default:
283       roar_err_set(ROAR_ERROR_NOSYS);
284       return -1;
285      break;
286    }
287    snprintf(out, outlen-1, "%lli", tmp.llsi);
288   break;
289  default:
290    roar_err_set(ROAR_ERROR_NOSYS);
291    return -1;
292 }
293
294 out[outlen-1] = 0;
295 return strlen(out);
296}
297
298int          roar_ht_is_supported(const int    ht) {
299 roar_crypto_init();
300
301 if ( roar_ht2cmds(ht) != NULL )
302  return 1;
303
304#ifdef ROAR_HAVE_LIBGCRYPT
305 if ( roar_ht2gcrypt_tested(ht) == -1 )
306  return 0;
307
308 return 1;
309#else
310 return 0;
311#endif
312}
313
314struct roar_hash_state * roar_hash_new(int algo) {
315 struct roar_hash_cmds  * cmds = roar_ht2cmds(algo);
316 struct roar_hash_state * self;
317
318 if ( cmds == NULL )
319  return NULL;
320
321 self = roar_mm_malloc(sizeof(struct roar_hash_state));
322
323 if ( self == NULL )
324  return NULL;
325
326 memset(self, 0, sizeof(struct roar_hash_state));
327
328 self->cmds = cmds;
329
330 self->state = roar_mm_malloc(cmds->statelen);
331
332 if ( self->state == NULL ) {
333  roar_mm_free(self);
334  return NULL;
335 }
336
337 memset(self->state, 0, cmds->statelen);
338
339 if ( cmds->init != NULL ) {
340  if ( cmds->init(self->state) == -1 ) {
341   roar_mm_free(self->state);
342   roar_mm_free(self);
343   return NULL;
344  }
345 }
346
347 return self;
348}
349
350int roar_hash_free(struct roar_hash_state * state) {
351 int ret = 0;
352
353 if ( state == NULL )
354  return -1;
355
356 if ( state->cmds->uninit != NULL )
357  ret = state->cmds->uninit(state->state);
358
359 roar_mm_free(state->state);
360 roar_mm_free(state);
361
362 return ret;
363}
364
365int roar_hash_digest(struct roar_hash_state * state, void * digest, size_t * len) {
366 if ( state == NULL )
367  return -1;
368
369 if ( state->cmds->digest == NULL )
370  return -1;
371
372 return state->cmds->digest(state->state, digest, len);
373}
374
375int roar_hash_proc(struct roar_hash_state * state, const void * data, size_t len) {
376 if ( state == NULL )
377  return -1;
378
379 if ( state->cmds->proc == NULL )
380  return -1;
381
382 return state->cmds->proc(state->state, data, len);
383}
384
385int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo) {
386 roar_crypto_init();
387
388 return roar_hash_salted_buffer(digest, data, datalen, algo, NULL, 0);
389}
390
391#ifdef ROAR_HAVE_LIBGCRYPT
392static inline int roar_hash_salted_buffer_gcrypt(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
393 gcry_md_hd_t hdl;
394
395 roar_crypto_init();
396
397 algo = roar_ht2gcrypt_tested(algo);
398 if ( algo == -1 )
399  return -1;
400
401
402 if ( salt == NULL ) {
403  // optimized for unsalted:
404  gcry_md_hash_buffer(algo, digest, data, datalen);
405  return 0;
406 } else {
407  if ( gcry_md_open(&hdl, algo, 0) != 0 )
408   return -1;
409
410  gcry_md_write(hdl, data, datalen);
411  gcry_md_write(hdl, salt, saltlen);
412
413  memcpy(digest, gcry_md_read(hdl, algo), gcry_md_get_algo_dlen(algo));
414
415  gcry_md_close(hdl);
416 }
417
418 return 0;
419}
420#endif
421
422int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
423 struct roar_hash_state * state;
424 size_t len;
425 int ret = 0;
426
427 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);
428
429 if ( digest == NULL || data == NULL )
430  return -1;
431
432 len = roar_ht_digestlen(algo);
433 if ( len == -1 )
434  return -1;
435
436 if ( (state = roar_hash_new(algo)) != NULL ) {
437  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
438
439  if ( roar_hash_proc(state, data, datalen) == -1 )
440   ret = -1;
441
442  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
443
444  if ( saltlen != 0 )
445   if ( roar_hash_proc(state, salt, saltlen) == -1 )
446    ret = -1;
447
448  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
449
450  if ( roar_hash_digest(state, digest, &len) == -1 )
451   ret = -1;
452
453  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
454
455  if ( roar_hash_free(state) == -1 )
456   ret = -1;
457
458  ROAR_DBG("roar_hash_salted_buffer(*): ret=%i", ret);
459
460  return ret;
461 }
462
463 ROAR_DBG("roar_hash_salted_buffer(*): state=%p", state);
464
465#ifdef ROAR_HAVE_LIBGCRYPT
466 return roar_hash_salted_buffer_gcrypt(digest, data, datalen, algo, salt, saltlen);
467#else
468 return -1;
469#endif
470}
471
472//ll
Note: See TracBrowser for help on using the repository browser.