summaryrefslogtreecommitdiff
path: root/toxcore/crypto_core.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/crypto_core.c')
-rw-r--r--toxcore/crypto_core.c25
1 files changed, 10 insertions, 15 deletions
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c
index 6e8b747a..3f3e7b48 100644
--- a/toxcore/crypto_core.c
+++ b/toxcore/crypto_core.c
@@ -29,30 +29,25 @@
29 29
30#include "crypto_core.h" 30#include "crypto_core.h"
31 31
32/* Use this instead of memcmp; not vulnerable to timing attacks. */
33uint8_t crypto_iszero(uint8_t *mem, uint32_t length)
34{
35 uint8_t check = 0;
36 uint32_t i;
37
38 for (i = 0; i < length; ++i) {
39 check |= mem[i];
40 }
41
42 return check; // We return zero if mem is made out of zeroes.
43}
44 32
45/* Use this instead of memcmp; not vulnerable to timing attacks. 33/* Use this instead of memcmp; not vulnerable to timing attacks.
46 returns 0 if both mem locations of length are equal. */ 34 returns 0 if both mem locations of length are equal,
35 return -1 if they are not. */
47unsigned int crypto_cmp(uint8_t *mem1, uint8_t *mem2, uint32_t length) 36unsigned int crypto_cmp(uint8_t *mem1, uint8_t *mem2, uint32_t length)
48{ 37{
49 unsigned int i, check = 0;; 38 if (length == 16) {
39 return crypto_verify_16(mem1, mem2);
40 } else if (length == 32) {
41 return crypto_verify_32(mem1, mem2);
42 }
43
44 unsigned int i, check = 0;
50 45
51 for (i = 0; i < length; ++i) { 46 for (i = 0; i < length; ++i) {
52 check |= mem1[i] ^ mem2[i]; 47 check |= mem1[i] ^ mem2[i];
53 } 48 }
54 49
55 return check; 50 return (1 & ((check - 1) >> 8)) - 1;
56} 51}
57 52
58/* Precomputes the shared key from their public_key and our secret_key. 53/* Precomputes the shared key from their public_key and our secret_key.