source: roaraudio/libroar/hash.c @ 4549:baef66261946

Last change on this file since 4549:baef66261946 was 4549:baef66261946, checked in by phi, 14 years ago

unbreak if no libgcrypt

File size: 5.5 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
42static const struct hashes {
43 const int     id;
44 const char *  name;
45 const ssize_t dlen;
46} _libroar_hashes[] = {
47/*
48grep '^  +HT_' doc/new-cmds | sed 's/ *#(.*)$//; s/^  +HT_//; s/ *=.*$//' | while read n; do printf " {ROAR_HT_%-12s \"%-12s -1   },\n" $n, $n\",; done
49*/
50 {ROAR_HT_NONE,        "NONE",       -1   },
51 {ROAR_HT_MD5,         "MD5",         16  },
52 {ROAR_HT_SHA1,        "SHA1",        20  },
53 {ROAR_HT_RIPEMD160,   "RIPEMD160",   20  },
54 {ROAR_HT_MD2,         "MD2",        -1   },
55 {ROAR_HT_TIGER,       "TIGER",       24  },
56 {ROAR_HT_HAVAL,       "HAVAL",      -1   },
57 {ROAR_HT_SHA256,      "SHA256",      32  },
58 {ROAR_HT_SHA384,      "SHA384",      48  },
59 {ROAR_HT_SHA512,      "SHA512",      64  },
60 {ROAR_HT_SHA224,      "SHA224",      28  },
61 {ROAR_HT_MD4,         "MD4",         16  },
62 {ROAR_HT_CRC32,       "CRC32",       4   },
63 {ROAR_HT_RFC1510,     "RFC1510",     4   },
64 {ROAR_HT_RFC2440,     "RFC2440",     3   },
65 {ROAR_HT_WHIRLPOOL,   "WHIRLPOOL",   64  },
66 {ROAR_HT_UUID,        "UUID",        16  },
67 {ROAR_HT_GTN8,        "GTN8",        1   },
68 {ROAR_HT_GTN16,       "GTN16",       2   },
69 {ROAR_HT_GTN32,       "GTN32",       4   },
70 {ROAR_HT_GTN64,       "GTN64",       8   },
71 {ROAR_HT_CLIENTID,    "CLIENTID",   -1   },
72 {ROAR_HT_STREAMID,    "STREAMID",   -1   },
73 {ROAR_HT_SOURCEID,    "SOURCEID",   -1   },
74 {ROAR_HT_SAMPLEID,    "SAMPLEID",   -1   },
75 {ROAR_HT_MIXERID,     "MIXERID",    -1   },
76 {ROAR_HT_BRIDGEID,    "BRIDGEID",   -1   },
77 {ROAR_HT_LISTENID,    "LISTENID",   -1   },
78 {ROAR_HT_ACTIONID,    "ACTIONID",   -1   },
79 {ROAR_HT_MSGQUEUEID,  "MSGQUEUEID", -1   },
80 {ROAR_HT_MSGBUSID,    "MSGBUSID",   -1   },
81 {ROAR_HT_GTIN8,       "GTIN8",       4   },
82 {ROAR_HT_GTIN13,      "GTIN13",      8   },
83 {ROAR_HT_ISBN10,      "ISBN10",      8   },
84 {ROAR_HT_ISBN13,      "ISBN13",      8   },
85 {-1, NULL}
86};
87
88static inline int roar_ht2gcrypt_tested (const int ht) {
89#ifdef ROAR_HAVE_LIBGCRYPT
90 const char * name;
91
92 if ( ht > 512 )
93  return -1;
94
95 // test the algo:
96 name = gcry_md_algo_name(ht);
97
98 if ( name == NULL )
99  return -1;
100
101 if ( *name == 0 )
102  return -1;
103
104 return ht;
105#else
106 return -1;
107#endif
108}
109
110const char * roar_ht2str (const int    ht) {
111 int i;
112
113 for (i = 0; _libroar_hashes[i].id != -1; i++)
114  if ( _libroar_hashes[i].id == ht )
115   return _libroar_hashes[i].name;
116
117 return NULL;
118}
119
120int          roar_str2ht (const char * ht) {
121 int i;
122
123 for (i = 0; _libroar_hashes[i].id != -1; i++)
124  if ( !strcasecmp(_libroar_hashes[i].name, ht) )
125   return _libroar_hashes[i].id;
126
127 return -1;
128}
129
130ssize_t      roar_ht_digestlen (const int    ht) {
131 int i;
132
133 for (i = 0; _libroar_hashes[i].id != -1; i++)
134  if ( _libroar_hashes[i].id == ht )
135   return _libroar_hashes[i].dlen;
136
137 return -1;
138}
139
140int          roar_ht_is_supported(const int    ht) {
141 roar_crypto_init();
142
143#ifdef ROAR_HAVE_LIBGCRYPT
144 if ( roar_ht2gcrypt_tested(ht) == -1 )
145  return 0;
146
147 return 1;
148#else
149 return 0;
150#endif
151}
152
153int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo) {
154 roar_crypto_init();
155
156 return roar_hash_salted_buffer(digest, data, datalen, algo, NULL, 0);
157}
158
159#ifdef ROAR_HAVE_LIBGCRYPT
160static inline int roar_hash_salted_buffer_gcrypt(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
161 gcry_md_hd_t hdl;
162
163 roar_crypto_init();
164
165 algo = roar_ht2gcrypt_tested(algo);
166 if ( algo == -1 )
167  return -1;
168
169
170 if ( salt == NULL ) {
171  // optimized for unsalted:
172  gcry_md_hash_buffer(algo, digest, data, datalen);
173  return 0;
174 } else {
175  if ( gcry_md_open(&hdl, algo, 0) != 0 )
176   return -1;
177
178  gcry_md_write(hdl, data, datalen);
179  gcry_md_write(hdl, salt, saltlen);
180
181  memcpy(digest, gcry_md_read(hdl, algo), gcry_md_get_algo_dlen(algo));
182
183  gcry_md_close(hdl);
184 }
185
186 return 0;
187}
188#endif
189
190int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
191 if ( digest == NULL || data == NULL )
192  return -1;
193
194#ifdef ROAR_HAVE_LIBGCRYPT
195 return roar_hash_salted_buffer_gcrypt(digest, data, datalen, algo, salt, saltlen);
196#else
197 return -1;
198#endif
199}
200
201//ll
Note: See TracBrowser for help on using the repository browser.