_detached versions of crypto_box

This commit is contained in:
Frank Denis 2014-06-26 15:54:03 -07:00
parent 44f7a9f3cb
commit c8dfd0d211
2 changed files with 49 additions and 12 deletions

View File

@ -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);
}

View File

@ -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