diff options
Diffstat (limited to 'toxcore/crypto_core.c')
-rw-r--r-- | toxcore/crypto_core.c | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index a364084a..418edcad 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c | |||
@@ -29,26 +29,16 @@ | |||
29 | 29 | ||
30 | #include "crypto_core.h" | 30 | #include "crypto_core.h" |
31 | 31 | ||
32 | #if crypto_box_PUBLICKEYBYTES != 32 | ||
33 | #error crypto_box_PUBLICKEYBYTES is required to be 32 bytes for public_key_cmp to work, | ||
34 | #endif | ||
32 | 35 | ||
33 | /* Use this instead of memcmp; not vulnerable to timing attacks. | 36 | /* compare 2 public keys of length crypto_box_PUBLICKEYBYTES, not vulnerable to timing attacks. |
34 | returns 0 if both mem locations of length are equal, | 37 | returns 0 if both mem locations of length are equal, |
35 | return -1 if they are not. */ | 38 | return -1 if they are not. */ |
36 | int crypto_cmp(const uint8_t *mem1, const uint8_t *mem2, size_t length) | 39 | int public_key_cmp(const uint8_t *pk1, const uint8_t *pk2) |
37 | { | 40 | { |
38 | if (length == 16) { | 41 | return crypto_verify_32(pk1, pk2); |
39 | return crypto_verify_16(mem1, mem2); | ||
40 | } else if (length == 32) { | ||
41 | return crypto_verify_32(mem1, mem2); | ||
42 | } | ||
43 | |||
44 | unsigned int check = 0; | ||
45 | size_t i; | ||
46 | |||
47 | for (i = 0; i < length; ++i) { | ||
48 | check |= mem1[i] ^ mem2[i]; | ||
49 | } | ||
50 | |||
51 | return (1 & ((check - 1) >> 8)) - 1; | ||
52 | } | 42 | } |
53 | 43 | ||
54 | /* return a random number. | 44 | /* return a random number. |