Am 14/09/2011 21:41, schrieb Michael Karcher:
Am Mittwoch, den 14.09.2011, 09:06 +0200 schrieb Thomas Arp:In netmd.cli the random number is used for creating a nonce in this way: unsigned char hostnonce[8] = { 0 }; unsigned char *buf; uint64_t rand; . . rand = (uint64_t)random(); buf = hostnonce; netmd_copy_quadword_to_buffer(&buf, rand); . . As far as i know random() and rand() are 32bit only. What about using "gcry_create_nonce(unsigned char *buffer, size_t length)" from libgcrypt instead? unsigned char hostnonce[8] = { 0 }; . . gcry_create_nonce(hostnonce, sizeof(hostnonce));Yes, go that way. Sounds nice, and we already do depend on libgcrypt. Regards, Michael Karcher
O.K., here is my patch, please review. Regards Thomas
From 78e8e1fc7c5cdac273aa1fcc4a74fb6fec4d4076 Mon Sep 17 00:00:00 2001 From: Thomas Arp <manner.moe@gmx.de> Date: Wed, 14 Sep 2011 23:43:21 +0200 Subject: [PATCH] make libnetmd platform independent and make use of libgcrypt to generate random numbers --- libnetmd/common.c | 2 +- libnetmd/secure.c | 22 ++++++---------------- libnetmd/utils.c | 9 --------- libnetmd/utils.h | 5 ++++- netmdcli/netmdcli.c | 6 +----- 5 files changed, 12 insertions(+), 32 deletions(-) diff --git a/libnetmd/common.c b/libnetmd/common.c index 1f7b660..7e039ed 100644 --- a/libnetmd/common.c +++ b/libnetmd/common.c @@ -62,7 +62,7 @@ static int netmd_poll(libusb_device_handle *dev, unsigned char *buf, int tries) } if (i > 0) { - sleep(1); + usleep(200000); } } diff --git a/libnetmd/secure.c b/libnetmd/secure.c index fce60c5..a940d9d 100644 --- a/libnetmd/secure.c +++ b/libnetmd/secure.c @@ -385,15 +385,6 @@ void netmd_transfer_song_packets(netmd_dev_handle *dev, } } -uint64_t generate_64bit_random() -{ - uint64_t high, low; - high = ((uint64_t)random() & 0xffffffffU) << 32U; - low = ((uint64_t)random() & 0xffffffffU); - - return high + low; -} - netmd_error netmd_prepare_packets(unsigned char* data, size_t data_lenght, netmd_track_packets **packets, size_t *packet_count, @@ -403,11 +394,11 @@ netmd_error netmd_prepare_packets(unsigned char* data, size_t data_lenght, size_t chunksize = 0xffffffffU; netmd_track_packets *last = NULL; netmd_track_packets *next = NULL; - uint64_t rand; gcry_cipher_hd_t key_handle; gcry_cipher_hd_t data_handle; unsigned char iv[8] = { 0 }; + unsigned char rand[8] = { 0 }; netmd_error error = NETMD_NO_ERROR; @@ -418,8 +409,7 @@ netmd_error netmd_prepare_packets(unsigned char* data, size_t data_lenght, /* generate initial iv */ - rand = generate_64bit_random(); - memcpy(iv, &rand, sizeof(rand)); + gcry_create_nonce(iv, sizeof(iv)); *packet_count = 0; while (position < data_lenght) { @@ -450,13 +440,13 @@ netmd_error netmd_prepare_packets(unsigned char* data, size_t data_lenght, } /* generate key */ - rand = generate_64bit_random(); - gcry_cipher_decrypt(key_handle, next->key, 8, &rand, sizeof(rand)); + gcry_randomize(rand, sizeof(rand), GCRY_STRONG_RANDOM); + gcry_cipher_decrypt(key_handle, next->key, 8, rand, sizeof(rand)); /* crypt data */ memcpy(next->iv, iv, 8); gcry_cipher_setiv(data_handle, iv, 8); - gcry_cipher_setkey(data_handle, &rand, sizeof(rand)); + gcry_cipher_setkey(data_handle, rand, sizeof(rand)); gcry_cipher_encrypt(data_handle, next->data, chunksize, data + position, chunksize); memcpy(iv, data + position - 8, 8); @@ -577,7 +567,7 @@ netmd_error netmd_secure_real_recv_track(netmd_dev_handle *dev, uint32_t length, netmd_log(NETMD_LOG_DEBUG, "%.1f%%\n", (double)done/(double)length * 100); } - else if (read != -ETIMEDOUT) { + else if (read != -LIBUSB_ERROR_TIMEOUT) { error = NETMD_USB_ERROR; } } diff --git a/libnetmd/utils.c b/libnetmd/utils.c index 9b12167..0d9e65e 100644 --- a/libnetmd/utils.c +++ b/libnetmd/utils.c @@ -26,15 +26,6 @@ #include "utils.h" #include "log.h" -inline int min(int a,int b) -{ - if (a < b) { - return a; - } - - return b; -} - inline unsigned char proper_to_bcd_single(unsigned char value) { unsigned char high, low; diff --git a/libnetmd/utils.h b/libnetmd/utils.h index a389b03..58996f3 100644 --- a/libnetmd/utils.h +++ b/libnetmd/utils.h @@ -12,7 +12,10 @@ typedef struct { size_t position; } netmd_response; -int min(int a,int b); +#ifndef min + #define min(a,b) ((a)<(b)?(a):(b)) +#endif + unsigned char proper_to_bcd_single(unsigned char value); unsigned char* proper_to_bcd(unsigned int value, unsigned char* target, size_t len); unsigned char bcd_to_proper_single(unsigned char value); diff --git a/netmdcli/netmdcli.c b/netmdcli/netmdcli.c index 55ad3f0..b4e0ee3 100644 --- a/netmdcli/netmdcli.c +++ b/netmdcli/netmdcli.c @@ -426,8 +426,6 @@ int main(int argc, char* argv[]) unsigned char hostnonce[8] = { 0 }; unsigned char devnonce[8] = { 0 }; unsigned char sessionkey[8] = { 0 }; - unsigned char *buf; - uint64_t rand; unsigned char kek[] = { 0x14, 0xe3, 0x83, 0x4e, 0xe2, 0xd3, 0xcc, 0xa5 }; unsigned char contentid[] = { 0x01, 0x0F, 0x50, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x48, 0xA2, 0x8D, @@ -491,9 +489,7 @@ int main(int argc, char* argv[]) } /* exchange nonces */ - rand = (uint64_t)random(); - buf = hostnonce; - netmd_copy_quadword_to_buffer(&buf, rand); + gcry_create_nonce(hostnonce, sizeof(hostnonce)); error = netmd_secure_session_key_exchange(devh, hostnonce, devnonce); puts(netmd_strerror(error)); -- 1.7.6.msysgit.0