summaryrefslogtreecommitdiff
path: root/toxcore/crypto_core.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2015-04-18 13:13:29 -0400
committerirungentoo <irungentoo@gmail.com>2015-04-18 13:13:29 -0400
commitb4fc0809a7b42ba5d104793e909aecf85951f100 (patch)
tree0e56a65760b37fc5f2323df512f8d21818e1e0c6 /toxcore/crypto_core.c
parent453548f18149594af977dc63c8a1924d8a6bb2e6 (diff)
The only secure compare function currently needed is one to compare 2 public keys.
Diffstat (limited to 'toxcore/crypto_core.c')
-rw-r--r--toxcore/crypto_core.c22
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. */
36int crypto_cmp(const uint8_t *mem1, const uint8_t *mem2, size_t length) 39int 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.