From c999fb6462af9ca64b4ba70b4daa228331d98e20 Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 21 Jul 2013 18:35:12 -0230 Subject: Implemented a faster algorithm for generating nonces --- core/net_crypto.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/core/net_crypto.c b/core/net_crypto.c index 270c969a..cbca250b 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -138,14 +138,18 @@ void increment_nonce(uint8_t * nonce) } } -/* fill the given nonce with random bytes. - TODO: make this more optimized */ +/* fill the given nonce with random bytes. */ void random_nonce(uint8_t * nonce) { - uint32_t i; - for(i = 0; i < crypto_box_NONCEBYTES; ++i) - { - nonce[i] = random_int() % 256; + uint32_t i, j, r, m = crypto_box_NONCEBYTES / 3, ind = 0; + for(i = 0; i < m; ++i) + { + r = ranom_int(); + for (j = 0; j < 3; j++) { + nonce[ind] = r % 1000 % 256; + r /= 1000; + ++ind; + } } } -- cgit v1.2.3 From 9b634504a647083753f54472151f81906b290f8b Mon Sep 17 00:00:00 2001 From: Zack Date: Sun, 21 Jul 2013 18:40:24 -0230 Subject: Fixed an embarassing typo --- core/net_crypto.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/net_crypto.c b/core/net_crypto.c index cbca250b..26102a02 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -144,8 +144,9 @@ void random_nonce(uint8_t * nonce) uint32_t i, j, r, m = crypto_box_NONCEBYTES / 3, ind = 0; for(i = 0; i < m; ++i) { - r = ranom_int(); - for (j = 0; j < 3; j++) { + r = random_int(); + for (j = 0; j < 3; j++) + { nonce[ind] = r % 1000 % 256; r /= 1000; ++ind; -- cgit v1.2.3 From 99c3426cbcb91a1b3509671c11ebffb1778057e6 Mon Sep 17 00:00:00 2001 From: redwire Date: Sun, 21 Jul 2013 21:13:04 -0230 Subject: Going with irungentoo's suggestion and using memcpy instead of sectioning with arithmetic --- core/net_crypto.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/core/net_crypto.c b/core/net_crypto.c index 26102a02..166adafb 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -141,16 +141,11 @@ void increment_nonce(uint8_t * nonce) /* fill the given nonce with random bytes. */ void random_nonce(uint8_t * nonce) { - uint32_t i, j, r, m = crypto_box_NONCEBYTES / 3, ind = 0; - for(i = 0; i < m; ++i) + uint32_t i, temp; + for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { - r = random_int(); - for (j = 0; j < 3; j++) - { - nonce[ind] = r % 1000 % 256; - r /= 1000; - ++ind; - } + uint32_t temp = random_int(); + memcpy(nonce + 4 * i, &temp, 4); } } -- cgit v1.2.3 From b16013b86be5063dfe32a59cf04febebed0a6d35 Mon Sep 17 00:00:00 2001 From: redwire Date: Sun, 21 Jul 2013 21:23:21 -0230 Subject: Fixed temp redeclaration --- core/net_crypto.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/net_crypto.c b/core/net_crypto.c index 166adafb..bdde7063 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c @@ -144,7 +144,7 @@ void random_nonce(uint8_t * nonce) uint32_t i, temp; for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { - uint32_t temp = random_int(); + temp = random_int(); memcpy(nonce + 4 * i, &temp, 4); } } -- cgit v1.2.3