source: roaraudio/libroar/hash.c @ 4454:8d5b183324bc

Last change on this file since 4454:8d5b183324bc was 4454:8d5b183324bc, checked in by phi, 14 years ago

implemented a overall crypto init function

File size: 5.4 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 const char * name;
90
91 if ( ht > 512 )
92  return -1;
93
94 // test the algo:
95 name = gcry_md_algo_name(ht);
96
97 if ( name == NULL )
98  return -1;
99
100 if ( *name == 0 )
101  return -1;
102
103 return ht;
104}
105
106const char * roar_ht2str (const int    ht) {
107 int i;
108
109 for (i = 0; _libroar_hashes[i].id != -1; i++)
110  if ( _libroar_hashes[i].id == ht )
111   return _libroar_hashes[i].name;
112
113 return NULL;
114}
115
116int          roar_str2ht (const char * ht) {
117 int i;
118
119 for (i = 0; _libroar_hashes[i].id != -1; i++)
120  if ( !strcasecmp(_libroar_hashes[i].name, ht) )
121   return _libroar_hashes[i].id;
122
123 return -1;
124}
125
126ssize_t      roar_ht_digestlen (const int    ht) {
127 int i;
128
129 for (i = 0; _libroar_hashes[i].id != -1; i++)
130  if ( _libroar_hashes[i].id == ht )
131   return _libroar_hashes[i].dlen;
132
133 return -1;
134}
135
136int          roar_ht_is_supported(const int    ht) {
137 roar_crypto_init();
138
139#ifdef ROAR_HAVE_LIBGCRYPT
140 if ( roar_ht2gcrypt_tested(ht) == -1 )
141  return 0;
142
143 return 1;
144#else
145 return 0;
146#endif
147}
148
149int roar_hash_buffer(void * digest, const void * data, size_t datalen, int algo) {
150 roar_crypto_init();
151
152 return roar_hash_salted_buffer(digest, data, datalen, algo, NULL, 0);
153}
154
155#ifdef ROAR_HAVE_LIBGCRYPT
156static inline int roar_hash_salted_buffer_gcrypt(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
157 gcry_md_hd_t hdl;
158
159 roar_crypto_init();
160
161 algo = roar_ht2gcrypt_tested(algo);
162 if ( algo == -1 )
163  return -1;
164
165
166 if ( salt == NULL ) {
167  // optimized for unsalted:
168  gcry_md_hash_buffer(algo, digest, data, datalen);
169  return 0;
170 } else {
171  if ( gcry_md_open(&hdl, algo, 0) != 0 )
172   return -1;
173
174  gcry_md_write(hdl, data, datalen);
175  gcry_md_write(hdl, salt, saltlen);
176
177  memcpy(digest, gcry_md_read(hdl, algo), gcry_md_get_algo_dlen(algo));
178
179  gcry_md_close(hdl);
180 }
181
182 return 0;
183}
184#endif
185
186int roar_hash_salted_buffer(void * digest, const void * data, size_t datalen, int algo, const void * salt, size_t saltlen) {
187 if ( digest == NULL || data == NULL )
188  return -1;
189
190#ifdef ROAR_HAVE_LIBGCRYPT
191 return roar_hash_salted_buffer_gcrypt(digest, data, datalen, algo, salt, saltlen);
192#else
193 return -1;
194#endif
195}
196
197//ll
Note: See TracBrowser for help on using the repository browser.