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