From: Andre Noll Date: Mon, 28 Feb 2011 23:28:27 +0000 (+0100) Subject: Move sha1.[ch] to crypt.[ch] and make crypto API independent of sha1. X-Git-Tag: v0.4.7~14^2~4 X-Git-Url: http://git.tuebingen.mpg.de/?p=paraslash.git;a=commitdiff_plain;h=e7593d7c153bfca2131b18621fb24ae18c1c0cb3;hp=a5ec1a6f44b149c0b018c1da8c1703109c5bdc8c Move sha1.[ch] to crypt.[ch] and make crypto API independent of sha1. This patch moves all public functions related to sha1 to crypt.c. This allows to remove sha1.c, sha1.h and hash.h. It also removes the HASH_TYPE define and avoids the word "sha1" in the public API and its callers. The former sha1_hash() is now called hash_function(). --- diff --git a/afs.h b/afs.h index 7a93183e..f54aa87c 100644 --- a/afs.h +++ b/afs.h @@ -7,7 +7,6 @@ /** \file afs.h Exported symbols of the audio file selector. */ #include -#include "hash.h" /** Audio file selector data stored in the audio file table. */ struct afs_info { @@ -119,8 +118,8 @@ struct ls_data { char *path; /** The score value (if -a was given). */ long score; - /** The sha1 hash of audio file. */ - HASH_TYPE *hash; + /** The hash value of audio file data. */ + unsigned char *hash; }; /** Data about the current audio file, passed from afs to server. */ diff --git a/aft.c b/aft.c index 9911b7fb..bcfe7e7d 100644 --- a/aft.c +++ b/aft.c @@ -238,7 +238,7 @@ enum audio_file_table_columns { */ static int aft_hash_compare(const struct osl_object *obj1, const struct osl_object *obj2) { - return hash_compare((HASH_TYPE *)obj1->data, (HASH_TYPE *)obj2->data); + return hash_compare((unsigned char *)obj1->data, (unsigned char *)obj2->data); } static struct osl_column_description aft_cols[] = { @@ -485,7 +485,7 @@ int aft_get_row_of_path(const char *path, struct osl_row **row) * * \return Standard. */ -static int aft_get_row_of_hash(HASH_TYPE *hash, struct osl_row **row) +static int aft_get_row_of_hash(unsigned char *hash, struct osl_row **row) { const struct osl_object obj = {.data = hash, .size = HASH_SIZE}; return osl(osl_get_row(audio_file_table, AFTCOL_HASH, &obj, row)); @@ -605,7 +605,7 @@ static int get_hash_object_of_aft_row(const struct osl_row *row, * \return The return value of the underlying call to * get_hash_object_of_aft_row(). */ -static int get_hash_of_row(const struct osl_row *row, HASH_TYPE **hash) +static int get_hash_of_row(const struct osl_row *row, unsigned char **hash) { struct osl_object obj; int ret = get_hash_object_of_aft_row(row, &obj); @@ -1026,7 +1026,7 @@ out: static int make_status_items(struct audio_file_data *afd, struct afs_info *afsi, char *path, long score, - HASH_TYPE *hash) + unsigned char *hash) { struct ls_data d = { .afhi = afd->afhi, @@ -1079,7 +1079,7 @@ static int make_status_items(struct audio_file_data *afd, int open_and_update_audio_file(struct osl_row *aft_row, long score, struct audio_file_data *afd) { - HASH_TYPE *aft_hash, file_hash[HASH_SIZE]; + unsigned char *aft_hash, file_hash[HASH_SIZE]; struct osl_object afsi_obj; struct afs_info old_afsi, new_afsi; int ret = get_hash_of_row(aft_row, &aft_hash); @@ -1526,7 +1526,7 @@ int audio_file_loop(void *private_data, osl_rbtree_loop_func *func) func)); } -static struct osl_row *find_hash_sister(HASH_TYPE *hash) +static struct osl_row *find_hash_sister(unsigned char *hash) { const struct osl_object obj = {.data = hash, .size = HASH_SIZE}; struct osl_row *row; @@ -1558,7 +1558,7 @@ enum com_add_buffer_offsets { * It's OK to call this with afhi == NULL. In this case, the audio format * handler info won't be stored in the buffer. */ -static void save_add_callback_buffer(HASH_TYPE *hash, const char *path, +static void save_add_callback_buffer(unsigned char *hash, const char *path, struct afh_info *afhi, uint32_t flags, uint8_t audio_format_num, struct osl_object *obj) { @@ -1661,7 +1661,7 @@ static void com_add_callback(int fd, const struct osl_object *query) struct osl_row *pb, *aft_row; struct osl_row *hs; struct osl_object objs[NUM_AFT_COLUMNS]; - HASH_TYPE *hash; + unsigned char *hash; char asc[2 * HASH_SIZE + 1]; int ret; char afsi_buf[AFSI_SIZE]; @@ -1671,7 +1671,7 @@ static void com_add_callback(int fd, const struct osl_object *query) .max_size_handler = pass_buffer_as_shm, .private_data = &fd}; uint16_t afhi_offset, chunks_offset; - hash = (HASH_TYPE *)buf + CAB_HASH_OFFSET; + hash = (unsigned char *)buf + CAB_HASH_OFFSET; hash_to_asc(hash, asc);; objs[AFTCOL_HASH].data = buf + CAB_HASH_OFFSET; objs[AFTCOL_HASH].size = HASH_SIZE; @@ -1736,7 +1736,7 @@ static void com_add_callback(int fd, const struct osl_object *query) objs[AFTCOL_CHUNKS].size = query->size - chunks_offset; if (pb && !hs) { /* update pb's hash */ char old_asc[2 * HASH_SIZE + 1]; - HASH_TYPE *old_hash; + unsigned char *old_hash; ret = get_hash_of_row(pb, &old_hash); if (ret < 0) goto out; @@ -1813,7 +1813,7 @@ static void path_brother_callback(int fd, const struct osl_object *query) static void hash_sister_callback(int fd, const struct osl_object *query) { - HASH_TYPE *hash = query->data; + unsigned char *hash = query->data; struct osl_row *hash_sister; hash_sister = find_hash_sister(hash); @@ -1837,7 +1837,7 @@ static int add_one_audio_file(const char *path, void *private_data) struct afh_info afhi, *afhi_ptr = NULL; struct osl_row *pb = NULL, *hs = NULL; /* path brother/hash sister */ struct osl_object map, obj = {.data = NULL}, query; - HASH_TYPE hash[HASH_SIZE]; + unsigned char hash[HASH_SIZE]; ret = guess_audio_format(path); if (ret < 0 && !(pad->flags & ADD_FLAG_ALL)) diff --git a/client_common.c b/client_common.c index b5d34ed7..c7ea3b4c 100644 --- a/client_common.c +++ b/client_common.c @@ -21,7 +21,6 @@ #include "string.h" #include "client.cmdline.h" #include "client.h" -#include "hash.h" #include "buffer_tree.h" /** The size of the receiving buffer. */ @@ -182,7 +181,7 @@ static void client_post_select(struct sched *s, struct task *t) /* decrypted challenge/session key buffer */ unsigned char crypt_buf[1024]; /* the SHA1 of the decrypted challenge */ - unsigned char challenge_sha1[HASH_SIZE]; + unsigned char challenge_hash[HASH_SIZE]; ret = client_recv_buffer(ct, &s->rfds, buf, sizeof(buf), &n); if (ret < 0 || n == 0) @@ -192,13 +191,13 @@ static void client_post_select(struct sched *s, struct task *t) (unsigned char *)buf, n); if (ret < 0) goto out; - sha1_hash((char *)crypt_buf, CHALLENGE_SIZE, challenge_sha1); + hash_function((char *)crypt_buf, CHALLENGE_SIZE, challenge_hash); ct->scc.send = sc_new(crypt_buf + CHALLENGE_SIZE, SESSION_KEY_LEN); ct->scc.recv = sc_new(crypt_buf + CHALLENGE_SIZE + SESSION_KEY_LEN, SESSION_KEY_LEN); - hash_to_asc(challenge_sha1, buf); + hash_to_asc(challenge_hash, buf); PARA_INFO_LOG("--> %s\n", buf); - ret = send_bin_buffer(ct->scc.fd, (char *)challenge_sha1, + ret = send_bin_buffer(ct->scc.fd, (char *)challenge_hash, HASH_SIZE); if (ret < 0) goto out; diff --git a/command.c b/command.c index ece87e16..22a71444 100644 --- a/command.c +++ b/command.c @@ -730,7 +730,7 @@ __noreturn void handle_connect(int fd, const char *peername) int ret, argc; char buf[4096]; unsigned char rand_buf[CHALLENGE_SIZE + 2 * SESSION_KEY_LEN]; - unsigned char challenge_sha1[HASH_SIZE]; + unsigned char challenge_hash[HASH_SIZE]; struct user *u; struct server_command *cmd = NULL; char **argv = NULL; @@ -795,14 +795,14 @@ __noreturn void handle_connect(int fd, const char *peername) if (!u) goto net_err; /* - * The correct response is the sha1 of the first CHALLENGE_SIZE bytes + * The correct response is the hash of the first CHALLENGE_SIZE bytes * of the random data. */ ret = -E_BAD_AUTH; if (numbytes != HASH_SIZE) goto net_err; - sha1_hash((char *)rand_buf, CHALLENGE_SIZE, challenge_sha1); - if (memcmp(challenge_sha1, buf, HASH_SIZE)) + hash_function((char *)rand_buf, CHALLENGE_SIZE, challenge_hash); + if (memcmp(challenge_hash, buf, HASH_SIZE)) goto net_err; /* auth successful */ alarm(0); diff --git a/configure.ac b/configure.ac index e8b2118c..48f2958f 100644 --- a/configure.ac +++ b/configure.ac @@ -87,7 +87,7 @@ AC_DEFUN([add_cmdline],[$(for i in $@; do printf "${i}.cmdline "; done)]) all_errlist_objs="server mp3_afh afh_common vss command net string signal time daemon stat crypt http_send close_on_fork ipc acl afh fade amp_filter dccp_send fd user_list chunk_queue afs aft mood score attribute blob ringbuffer -playlist sha1 sched audiod grab_client filter_common wav_filter compress_filter +playlist sched audiod grab_client filter_common wav_filter compress_filter http_recv dccp_recv recv_common write_common file_write audiod_command client_common recv stdout filter stdin audioc write client exec send_common ggo udp_recv udp_send color fec fecdec_filter prebuffer_filter mm @@ -118,7 +118,7 @@ audiod_cmdline_objs="add_cmdline(audiod compress_filter http_recv dccp_recv file audiod_errlist_objs="audiod signal string daemon stat net time grab_client filter_common wav_filter compress_filter amp_filter http_recv dccp_recv recv_common fd sched write_common file_write audiod_command crypt fecdec_filter - client_common ggo udp_recv color fec prebuffer_filter sha1 audiod_command_list + client_common ggo udp_recv color fec prebuffer_filter audiod_command_list bitstream imdct wma_common wmadec_filter buffer_tree" audiod_ldflags="-lm" audiod_audio_formats="wma" @@ -131,7 +131,7 @@ server_cmdline_objs="add_cmdline(server)" server_errlist_objs="server afh_common mp3_afh vss command net string signal time daemon crypt http_send close_on_fork mm ipc dccp_send fd user_list chunk_queue afs aft mood score attribute - blob playlist sha1 sched acl send_common udp_send color fec + blob playlist sched acl send_common udp_send color fec server_command_list afs_command_list wma_afh wma_common" server_ldflags="" server_audio_formats="mp3 wma" @@ -145,7 +145,7 @@ default_writer="FILE_WRITE" client_cmdline_objs="add_cmdline(client)" client_errlist_objs="client net string crypt fd sched stdin stdout time - client_common sha1 buffer_tree" + client_common buffer_tree" client_ldflags="" gui_cmdline_objs="add_cmdline(gui)" diff --git a/crypt.c b/crypt.c index 99804470..f3bfed3b 100644 --- a/crypt.c +++ b/crypt.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "para.h" #include "error.h" @@ -364,3 +365,22 @@ int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size) *buf = '\0'; return n; } + +/** + * Compute the hash of the given input data. + * + * \param data Pointer to the data to compute the hash value from. + * \param len The length of \a data in bytes. + * \param hash Result pointer. + * + * \a hash must point to an area at least \p HASH_SIZE bytes large. + * + * \sa sha(3), openssl(1). + * */ +void hash_function(const char *data, unsigned long len, unsigned char *hash) +{ + SHA_CTX c; + SHA1_Init(&c); + SHA1_Update(&c, data, len); + SHA1_Final(hash, &c); +} diff --git a/crypt.h b/crypt.h index 0e0695e5..21abe41f 100644 --- a/crypt.h +++ b/crypt.h @@ -60,3 +60,52 @@ int sc_recv_buffer(struct stream_cipher_context *scc, char *buf, size_t size); #define LOAD_PRIVATE_KEY 1 #define CHALLENGE_SIZE 64 /** \endcond **/ + +/** Size of the hash value in bytes. */ +#define HASH_SIZE 20 + +void hash_function(const char *data, unsigned long len, unsigned char *hash); + +/** + * Compare two hashes. + * + * \param h1 Pointer to the first hash value. + * \param h2 Pointer to the second hash value. + * + * \return 1, -1, or zero, depending on whether \a h1 is greater than, + * less than or equal to h2, respectively. + */ +_static_inline_ int hash_compare(unsigned char *h1, unsigned char *h2) +{ + int i; + + for (i = 0; i < HASH_SIZE; i++) { + if (h1[i] < h2[i]) + return -1; + if (h1[i] > h2[i]) + return 1; + } + return 0; +} + +/** + * Convert a hash value to ascii format. + * + * \param hash the hash value. + * \param asc Result pointer. + * + * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which + * will be filled by the function with the ascii representation of the hash + * value given by \a hash, and a terminating \p NULL byte. + */ +_static_inline_ void hash_to_asc(unsigned char *hash, char *asc) +{ + int i; + const char hexchar[] = "0123456789abcdef"; + + for (i = 0; i < HASH_SIZE; i++) { + asc[2 * i] = hexchar[hash[i] >> 4]; + asc[2 * i + 1] = hexchar[hash[i] & 0xf]; + } + asc[2 * HASH_SIZE] = '\0'; +} diff --git a/error.h b/error.h index de5dacf2..564f1339 100644 --- a/error.h +++ b/error.h @@ -19,7 +19,6 @@ DEFINE_ERRLIST_OBJECT_ENUM; #define GUI_ERRORS #define RINGBUFFER_ERRORS #define SCORE_ERRORS -#define SHA1_ERRORS #define RBTREE_ERRORS #define RECV_ERRORS #define IPC_ERRORS diff --git a/hash.h b/hash.h deleted file mode 100644 index 6dfddbed..00000000 --- a/hash.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (C) 2007-2011 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ - -/** \file hash.h Inline functions for hash values. */ - -/** hash arrays are always unsigned char. */ -#define HASH_TYPE unsigned char - -#include "sha1.h" -/** We use openssl's sha1 implementation. */ -#define hash_function sha1_hash - -/** - * Compare two hashes. - * - * \param h1 Pointer to the first hash value. - * \param h2 Pointer to the second hash value. - * - * \return 1, -1, or zero, depending on whether \a h1 is greater than, - * less than or equal to h2, respectively. - */ -_static_inline_ int hash_compare(HASH_TYPE *h1, HASH_TYPE *h2) -{ - int i; - - for (i = 0; i < HASH_SIZE; i++) { - if (h1[i] < h2[i]) - return -1; - if (h1[i] > h2[i]) - return 1; - } - return 0; -} - -/** - * Convert a hash value to ascii format. - * - * \param hash the hash value. - * \param asc Result pointer. - * - * \a asc must point to an area of at least 2 * \p HASH_SIZE + 1 bytes which - * will be filled by the function with the ascii representation of the hash - * value given by \a hash, and a terminating \p NULL byte. - */ -_static_inline_ void hash_to_asc(HASH_TYPE *hash, char *asc) -{ - int i; - const char hexchar[] = "0123456789abcdef"; - - for (i = 0; i < HASH_SIZE; i++) { - asc[2 * i] = hexchar[hash[i] >> 4]; - asc[2 * i + 1] = hexchar[hash[i] & 0xf]; - } - asc[2 * HASH_SIZE] = '\0'; -} diff --git a/server.c b/server.c index 0f2967b3..9f237ad7 100644 --- a/server.c +++ b/server.c @@ -57,8 +57,7 @@ * * - Doubly linked lists: \ref list.h, * - Ring buffer: \ref ringbuffer.c, \ref ringbuffer.h, - * - Hashing: \ref hash.h, \ref sha1.h, \ref sha1.c, - * - Crypto: \ref crypt.c. + * - Crypto: \ref crypt.c, \ref crypt.h. * - Forward error correction: \ref fec.c. */ diff --git a/sha1.c b/sha1.c deleted file mode 100644 index b0e955ec..00000000 --- a/sha1.c +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (C) 2007-2011 Andre Noll - * - * Licensed under the GPL v2. For licencing details see COPYING. - */ - -/** \file sha1.c Secure Hash Algorithm, provided by openssl. */ - -#include "para.h" -#include - -/** - * Compute the sha1 hash. - * - * \param data Pointer to the data to compute the hash value from. - * \param len The length of \a data in bytes. - * \param sha1 Result pointer. - * - * \a sha1 must point to an area at least 20 bytes large. - * - * \sa sha(3), openssl(1). - * */ -void sha1_hash(const char *data, unsigned long len, unsigned char *sha1) -{ - SHA_CTX c; - SHA1_Init(&c); - SHA1_Update(&c, data, len); - SHA1_Final(sha1, &c); -} diff --git a/sha1.h b/sha1.h deleted file mode 100644 index 4d21733e..00000000 --- a/sha1.h +++ /dev/null @@ -1,5 +0,0 @@ -/** \file sha1.h Secure Hash Algorithm prototype */ - -/** Size of the hash value in bytes. */ -#define HASH_SIZE 20 -void sha1_hash(const char *data, unsigned long len, unsigned char *sha1);