From ce29c8e7ec91d95167b2dea3aee9fd1ae1aac254 Mon Sep 17 00:00:00 2001 From: iphydf Date: Mon, 19 Dec 2016 02:47:42 +0000 Subject: Wrap all sodium/nacl functions in crypto_core.c. --- toxcore/crypto_core.c | 95 +++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 89 insertions(+), 6 deletions(-) (limited to 'toxcore/crypto_core.c') diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index a71b3cb8..d4f7c562 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -29,8 +29,53 @@ #include "crypto_core.h" -#if crypto_box_PUBLICKEYBYTES != 32 -#error crypto_box_PUBLICKEYBYTES is required to be 32 bytes for public_key_cmp to work, +#ifndef VANILLA_NACL +/* We use libsodium by default. */ +#include +#else +#include +#include +#include +#include +#include +#include +#include +#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) +/* I know */ +#define sodium_memcmp(a, b, c) memcmp(a, b, c) +#define sodium_memzero(a, c) memset(a, 0, c) +#endif + +#if CRYPTO_PUBLIC_KEY_SIZE != crypto_box_PUBLICKEYBYTES +#error CRYPTO_PUBLIC_KEY_SIZE should be equal to crypto_box_PUBLICKEYBYTES +#endif + +#if CRYPTO_SECRET_KEY_SIZE != crypto_box_SECRETKEYBYTES +#error CRYPTO_SECRET_KEY_SIZE should be equal to crypto_box_SECRETKEYBYTES +#endif + +#if CRYPTO_SHARED_KEY_SIZE != crypto_box_BEFORENMBYTES +#error CRYPTO_SHARED_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES +#endif + +#if CRYPTO_SYMMETRIC_KEY_SIZE != crypto_box_BEFORENMBYTES +#error CRYPTO_SYMMETRIC_KEY_SIZE should be equal to crypto_box_BEFORENMBYTES +#endif + +#if CRYPTO_MAC_SIZE != crypto_box_MACBYTES +#error CRYPTO_MAC_SIZE should be equal to crypto_box_MACBYTES +#endif + +#if CRYPTO_NONCE_SIZE != crypto_box_NONCEBYTES +#error CRYPTO_NONCE_SIZE should be equal to crypto_box_NONCEBYTES +#endif + +#if CRYPTO_SHA256_SIZE != crypto_hash_sha256_BYTES +#error CRYPTO_SHA256_SIZE should be equal to crypto_hash_sha256_BYTES +#endif + +#if CRYPTO_SHA512_SIZE != crypto_hash_sha512_BYTES +#error CRYPTO_SHA512_SIZE should be equal to crypto_hash_sha512_BYTES #endif /* compare 2 public keys of length crypto_box_PUBLICKEYBYTES, not vulnerable to timing attacks. @@ -38,6 +83,9 @@ return -1 if they are not. */ int public_key_cmp(const uint8_t *pk1, const uint8_t *pk2) { +#if crypto_box_PUBLICKEYBYTES != 32 +#error crypto_box_PUBLICKEYBYTES is required to be 32 bytes for public_key_cmp to work, +#endif return crypto_verify_32(pk1, pk2); } @@ -135,7 +183,7 @@ int encrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uin uint8_t k[crypto_box_BEFORENMBYTES]; encrypt_precompute(public_key, secret_key, k); int ret = encrypt_data_symmetric(k, nonce, plain, length, encrypted); - sodium_memzero(k, sizeof k); + crypto_memzero(k, sizeof k); return ret; } @@ -149,7 +197,7 @@ int decrypt_data(const uint8_t *public_key, const uint8_t *secret_key, const uin uint8_t k[crypto_box_BEFORENMBYTES]; encrypt_precompute(public_key, secret_key, k); int ret = decrypt_data_symmetric(k, nonce, encrypted, length, plain); - sodium_memzero(k, sizeof k); + crypto_memzero(k, sizeof k); return ret; } @@ -204,8 +252,43 @@ void random_nonce(uint8_t *nonce) randombytes(nonce, crypto_box_NONCEBYTES); } -/* Fill a key crypto_box_KEYBYTES big with random bytes */ +/* Fill a key CRYPTO_SYMMETRIC_KEY_SIZE big with random bytes */ void new_symmetric_key(uint8_t *key) { - randombytes(key, crypto_box_KEYBYTES); + randombytes(key, CRYPTO_SYMMETRIC_KEY_SIZE); +} + +int32_t crypto_new_keypair(uint8_t *public_key, uint8_t *secret_key) +{ + return crypto_box_keypair(public_key, secret_key); +} + +void crypto_derive_public_key(uint8_t *public_key, uint8_t *secret_key) +{ + crypto_scalarmult_curve25519_base(public_key, secret_key); +} + +void crypto_sha256(uint8_t *hash, const uint8_t *data, size_t length) +{ + crypto_hash_sha256(hash, data, length); +} + +void crypto_sha512(uint8_t *hash, const uint8_t *data, size_t length) +{ + crypto_hash_sha512(hash, data, length); +} + +void crypto_memzero(void *data, size_t length) +{ + sodium_memzero(data, length); +} + +int32_t crypto_memcmp(const void *p1, const void *p2, size_t length) +{ + return sodium_memcmp(p1, p2, length); +} + +void random_bytes(uint8_t *data, size_t length) +{ + randombytes(data, length); } -- cgit v1.2.3