From aaf5fbf2e53a33b18d8ea9bdf2c6f73d7acc8c3e Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Tue, 6 Jan 2015 17:52:42 +0100 Subject: [PATCH] + precomputed interface for crypto_box() --- builds/msvc/version.h | 2 +- configure.ac | 4 +- src/libsodium/crypto_box/crypto_box_easy.c | 46 +++++++++- src/libsodium/include/sodium/crypto_box.h | 97 ++++++++++++++-------- 4 files changed, 108 insertions(+), 41 deletions(-) diff --git a/builds/msvc/version.h b/builds/msvc/version.h index e5740fe6..46672531 100644 --- a/builds/msvc/version.h +++ b/builds/msvc/version.h @@ -7,7 +7,7 @@ #define SODIUM_VERSION_STRING "1.0.1" #define SODIUM_LIBRARY_VERSION_MAJOR 7 -#define SODIUM_LIBRARY_VERSION_MINOR 3 +#define SODIUM_LIBRARY_VERSION_MINOR 4 #ifdef __cplusplus extern "C" { diff --git a/configure.ac b/configure.ac index 8d7caaef..acabe40d 100644 --- a/configure.ac +++ b/configure.ac @@ -17,9 +17,9 @@ ISODATE=`date +%Y-%m-%d` AC_SUBST(ISODATE) SODIUM_LIBRARY_VERSION_MAJOR=7 -SODIUM_LIBRARY_VERSION_MINOR=3 +SODIUM_LIBRARY_VERSION_MINOR=4 DLL_VERSION=6 -SODIUM_LIBRARY_VERSION=13:3:0 +SODIUM_LIBRARY_VERSION=14:0:1 # | | | # +------+ | +---+ # | | | diff --git a/src/libsodium/crypto_box/crypto_box_easy.c b/src/libsodium/crypto_box/crypto_box_easy.c index 42ae102a..a4066c5c 100644 --- a/src/libsodium/crypto_box/crypto_box_easy.c +++ b/src/libsodium/crypto_box/crypto_box_easy.c @@ -7,6 +7,14 @@ #include "crypto_secretbox.h" #include "utils.h" +int +crypto_box_detached_afternm(unsigned char *c, unsigned char *mac, + const unsigned char *m, unsigned long long mlen, + const unsigned char *n, const unsigned char *k) +{ + return crypto_secretbox_detached(c, mac, m, mlen, n, k); +} + int crypto_box_detached(unsigned char *c, unsigned char *mac, const unsigned char *m, unsigned long long mlen, @@ -19,12 +27,24 @@ crypto_box_detached(unsigned char *c, unsigned char *mac, (void) sizeof(int[crypto_box_BEFORENMBYTES >= crypto_secretbox_KEYBYTES ? 1 : -1]); crypto_box_beforenm(k, pk, sk); - ret = crypto_secretbox_detached(c, mac, m, mlen, n, k); + ret = crypto_box_detached_afternm(c, mac, m, mlen, n, k); sodium_memzero(k, sizeof k); return ret; } +int +crypto_box_easy_afternm(unsigned char *c, const unsigned char *m, + unsigned long long mlen, const unsigned char *n, + const unsigned char *k) +{ + if (mlen > SIZE_MAX - crypto_box_MACBYTES) { + return -1; + } + return crypto_box_detached_afternm(c + crypto_box_MACBYTES, c, m, mlen, n, + k); +} + int crypto_box_easy(unsigned char *c, const unsigned char *m, unsigned long long mlen, const unsigned char *n, @@ -37,6 +57,15 @@ crypto_box_easy(unsigned char *c, const unsigned char *m, pk, sk); } +int +crypto_box_open_detached_afternm(unsigned char *m, const unsigned char *c, + const unsigned char *mac, + unsigned long long clen, const unsigned char *n, + const unsigned char *k) +{ + return crypto_secretbox_open_detached(m, c, mac, clen, n, k); +} + int crypto_box_open_detached(unsigned char *m, const unsigned char *c, const unsigned char *mac, @@ -47,12 +76,25 @@ crypto_box_open_detached(unsigned char *m, const unsigned char *c, int ret; crypto_box_beforenm(k, pk, sk); - ret = crypto_secretbox_open_detached(m, c, mac, clen, n, k); + ret = crypto_box_open_detached_afternm(m, c, mac, clen, n, k); sodium_memzero(k, sizeof k); return ret; } +int +crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c, + unsigned long long clen, const unsigned char *n, + const unsigned char *k) +{ + if (clen < crypto_box_MACBYTES) { + return -1; + } + return crypto_box_open_detached_afternm(m, c + crypto_box_MACBYTES, c, + clen - crypto_box_MACBYTES, + n, k); +} + int crypto_box_open_easy(unsigned char *m, const unsigned char *c, unsigned long long clen, const unsigned char *n, diff --git a/src/libsodium/include/sodium/crypto_box.h b/src/libsodium/include/sodium/crypto_box.h index 35207820..cf29f6c1 100644 --- a/src/libsodium/include/sodium/crypto_box.h +++ b/src/libsodium/include/sodium/crypto_box.h @@ -32,22 +32,10 @@ size_t crypto_box_publickeybytes(void); SODIUM_EXPORT size_t crypto_box_secretkeybytes(void); -#define crypto_box_BEFORENMBYTES crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES -SODIUM_EXPORT -size_t crypto_box_beforenmbytes(void); - #define crypto_box_NONCEBYTES crypto_box_curve25519xsalsa20poly1305_NONCEBYTES SODIUM_EXPORT size_t crypto_box_noncebytes(void); -#define crypto_box_ZEROBYTES crypto_box_curve25519xsalsa20poly1305_ZEROBYTES -SODIUM_EXPORT -size_t crypto_box_zerobytes(void); - -#define crypto_box_BOXZEROBYTES crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES -SODIUM_EXPORT -size_t crypto_box_boxzerobytes(void); - #define crypto_box_MACBYTES crypto_box_curve25519xsalsa20poly1305_MACBYTES SODIUM_EXPORT size_t crypto_box_macbytes(void); @@ -63,30 +51,6 @@ int crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk, SODIUM_EXPORT int crypto_box_keypair(unsigned char *pk, unsigned char *sk); -SODIUM_EXPORT -int crypto_box_beforenm(unsigned char *k, const unsigned char *pk, - const unsigned char *sk); - -SODIUM_EXPORT -int crypto_box_afternm(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *k); - -SODIUM_EXPORT -int crypto_box_open_afternm(unsigned char *m, const unsigned char *c, - unsigned long long clen, const unsigned char *n, - const unsigned char *k); - -SODIUM_EXPORT -int crypto_box(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *pk, const unsigned char *sk); - -SODIUM_EXPORT -int crypto_box_open(unsigned char *m, const unsigned char *c, - unsigned long long clen, const unsigned char *n, - const unsigned char *pk, const unsigned char *sk); - SODIUM_EXPORT int crypto_box_easy(unsigned char *c, const unsigned char *m, unsigned long long mlen, const unsigned char *n, @@ -110,6 +74,67 @@ int crypto_box_open_detached(unsigned char *m, const unsigned char *c, const unsigned char *n, const unsigned char *pk, const unsigned char *sk); + +/* -- Precomputation interface -- */ + +#define crypto_box_BEFORENMBYTES crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES +SODIUM_EXPORT +size_t crypto_box_beforenmbytes(void); + +SODIUM_EXPORT +int crypto_box_beforenm(unsigned char *k, const unsigned char *pk, + const unsigned char *sk); + +SODIUM_EXPORT +int crypto_box_easy_afternm(unsigned char *c, const unsigned char *m, + unsigned long long mlen, const unsigned char *n, + const unsigned char *k); + +SODIUM_EXPORT +int crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c, + unsigned long long clen, const unsigned char *n, + const unsigned char *k); + +SODIUM_EXPORT +int crypto_box_detached_afternm(unsigned char *c, unsigned char *mac, + const unsigned char *m, unsigned long long mlen, + const unsigned char *n, const unsigned char *k); + +SODIUM_EXPORT +int crypto_box_open_detached_afternm(unsigned char *m, const unsigned char *c, + const unsigned char *mac, + unsigned long long clen, const unsigned char *n, + const unsigned char *k); + +/* -- Compatibility layer with NaCl -- */ + +#define crypto_box_ZEROBYTES crypto_box_curve25519xsalsa20poly1305_ZEROBYTES +SODIUM_EXPORT +size_t crypto_box_zerobytes(void); + +#define crypto_box_BOXZEROBYTES crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES +SODIUM_EXPORT +size_t crypto_box_boxzerobytes(void); + +SODIUM_EXPORT +int crypto_box(unsigned char *c, const unsigned char *m, + unsigned long long mlen, const unsigned char *n, + const unsigned char *pk, const unsigned char *sk); + +SODIUM_EXPORT +int crypto_box_open(unsigned char *m, const unsigned char *c, + unsigned long long clen, const unsigned char *n, + const unsigned char *pk, const unsigned char *sk); + +SODIUM_EXPORT +int crypto_box_afternm(unsigned char *c, const unsigned char *m, + unsigned long long mlen, const unsigned char *n, + const unsigned char *k); + +SODIUM_EXPORT +int crypto_box_open_afternm(unsigned char *m, const unsigned char *c, + unsigned long long clen, const unsigned char *n, + const unsigned char *k); #ifdef __cplusplus } #endif