From c8dfd0d211993a5b408cdde22f8eacf0b243b387 Mon Sep 17 00:00:00 2001 From: Frank Denis Date: Thu, 26 Jun 2014 15:54:03 -0700 Subject: [PATCH] _detached versions of crypto_box --- src/libsodium/crypto_box/crypto_box_easy.c | 48 ++++++++++++++++------ src/libsodium/include/sodium/crypto_box.h | 13 ++++++ 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/src/libsodium/crypto_box/crypto_box_easy.c b/src/libsodium/crypto_box/crypto_box_easy.c index a01b4961..70648c09 100644 --- a/src/libsodium/crypto_box/crypto_box_easy.c +++ b/src/libsodium/crypto_box/crypto_box_easy.c @@ -4,9 +4,10 @@ #include "utils.h" int -crypto_box_easy(unsigned char *c, const unsigned char *m, - unsigned long long mlen, const unsigned char *n, - const unsigned char *pk, const unsigned char *sk) +crypto_box_easy_detached(unsigned char *c, unsigned char *mac, + const unsigned char *m, unsigned long long mlen, + const unsigned char *n, const unsigned char *pk, + const unsigned char *sk) { unsigned char k[crypto_box_BEFORENMBYTES]; int ret; @@ -14,7 +15,32 @@ crypto_box_easy(unsigned char *c, const unsigned char *m, (void) sizeof(int[crypto_box_BEFORENMBYTES >= crypto_secretbox_KEYBYTES ? 1 : -1]); crypto_box_beforenm(k, pk, sk); - ret = crypto_secretbox_easy(c, m, mlen, n, k); + ret = crypto_secretbox_easy_detached(c, mac, m, mlen, n, k); + sodium_memzero(k, sizeof k); + + return ret; +} + +int +crypto_box_easy(unsigned char *c, const unsigned char *m, + unsigned long long mlen, const unsigned char *n, + const unsigned char *pk, const unsigned char *sk) +{ + return crypto_box_easy_detached(c + crypto_box_MACBYTES, c, m, mlen, n, + pk, sk); +} + +int +crypto_box_open_easy_detached(unsigned char *m, const unsigned char *c, + const unsigned char *mac, + unsigned long long clen, const unsigned char *n, + const unsigned char *pk, const unsigned char *sk) +{ + unsigned char k[crypto_box_BEFORENMBYTES]; + int ret; + + crypto_box_beforenm(k, pk, sk); + ret = crypto_secretbox_open_easy_detached(m, c, mac, clen, n, k); sodium_memzero(k, sizeof k); return ret; @@ -25,12 +51,10 @@ crypto_box_open_easy(unsigned char *m, const unsigned char *c, unsigned long long clen, const unsigned char *n, const unsigned char *pk, const unsigned char *sk) { - unsigned char k[crypto_box_BEFORENMBYTES]; - int ret; - - crypto_box_beforenm(k, pk, sk); - ret = crypto_secretbox_open_easy(m, c, clen, n, k); - sodium_memzero(k, sizeof k); - - return ret; + if (clen < crypto_box_MACBYTES) { + return -1; + } + return crypto_box_open_easy_detached(m, c + crypto_box_MACBYTES, c, + clen - crypto_box_MACBYTES, + n, pk, sk); } diff --git a/src/libsodium/include/sodium/crypto_box.h b/src/libsodium/include/sodium/crypto_box.h index 9c1a64bf..17f7b516 100644 --- a/src/libsodium/include/sodium/crypto_box.h +++ b/src/libsodium/include/sodium/crypto_box.h @@ -97,6 +97,19 @@ int crypto_box_open_easy(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_detached(unsigned char *c, unsigned char *mac, + 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_easy_detached(unsigned char *m, const unsigned char *c, + const unsigned char *mac, + unsigned long long clen, + const unsigned char *n, + const unsigned char *pk, + const unsigned char *sk); #ifdef __cplusplus } #endif