zerocopy crypto_box_easy() and crypto_box_open_easy()
This commit is contained in:
parent
865a0719fc
commit
d983bbe860
@ -1,11 +1,6 @@
|
|||||||
|
|
||||||
#include <assert.h>
|
|
||||||
#include <limits.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include "crypto_box.h"
|
#include "crypto_box.h"
|
||||||
|
#include "crypto_secretbox.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -13,42 +8,16 @@ crypto_box_easy(unsigned char *c, const unsigned char *m,
|
|||||||
unsigned long long mlen, const unsigned char *n,
|
unsigned long long mlen, const unsigned char *n,
|
||||||
const unsigned char *pk, const unsigned char *sk)
|
const unsigned char *pk, const unsigned char *sk)
|
||||||
{
|
{
|
||||||
unsigned char *c_boxed;
|
unsigned char k[crypto_box_BEFORENMBYTES];
|
||||||
unsigned char *m_boxed;
|
int ret;
|
||||||
size_t c_boxed_len;
|
|
||||||
size_t m_boxed_len;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
if (mlen > SIZE_MAX - crypto_box_ZEROBYTES) {
|
(void) sizeof(int[crypto_box_BEFORENMBYTES >=
|
||||||
return -1;
|
crypto_secretbox_KEYBYTES ? 1 : -1]);
|
||||||
}
|
crypto_box_beforenm(k, pk, sk);
|
||||||
(void) sizeof(char[crypto_box_ZEROBYTES >=
|
ret = crypto_secretbox_easy(c, m, mlen, n, k);
|
||||||
crypto_box_BOXZEROBYTES ? 1 : -1]);
|
sodium_memzero(k, sizeof k);
|
||||||
m_boxed_len = (size_t) mlen + crypto_box_ZEROBYTES;
|
|
||||||
if ((m_boxed = (unsigned char *) malloc((size_t) m_boxed_len)) == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
c_boxed_len = (size_t) mlen + crypto_box_ZEROBYTES;
|
|
||||||
if ((c_boxed = (unsigned char *) malloc(c_boxed_len)) == NULL) {
|
|
||||||
free(m_boxed);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
memset(m_boxed, 0, crypto_box_ZEROBYTES);
|
|
||||||
sodium_mlock(m_boxed, m_boxed_len);
|
|
||||||
memcpy(m_boxed + crypto_box_ZEROBYTES, m, mlen);
|
|
||||||
rc = crypto_box(c_boxed, m_boxed, m_boxed_len, n, pk, sk);
|
|
||||||
sodium_munlock(m_boxed, m_boxed_len);
|
|
||||||
free(m_boxed);
|
|
||||||
if (rc != 0) {
|
|
||||||
free(c_boxed);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
assert(m_boxed_len - crypto_box_BOXZEROBYTES ==
|
|
||||||
mlen + crypto_box_MACBYTES);
|
|
||||||
memcpy(c, c_boxed + crypto_box_BOXZEROBYTES, mlen + crypto_box_MACBYTES);
|
|
||||||
free(c_boxed);
|
|
||||||
|
|
||||||
return 0;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -56,39 +25,12 @@ crypto_box_open_easy(unsigned char *m, const unsigned char *c,
|
|||||||
unsigned long long clen, const unsigned char *n,
|
unsigned long long clen, const unsigned char *n,
|
||||||
const unsigned char *pk, const unsigned char *sk)
|
const unsigned char *pk, const unsigned char *sk)
|
||||||
{
|
{
|
||||||
unsigned char *c_boxed;
|
unsigned char k[crypto_box_BEFORENMBYTES];
|
||||||
unsigned char *m_boxed;
|
int ret;
|
||||||
size_t c_boxed_len;
|
|
||||||
size_t m_boxed_len;
|
|
||||||
int rc;
|
|
||||||
|
|
||||||
if (clen < crypto_box_MACBYTES ||
|
crypto_box_beforenm(k, pk, sk);
|
||||||
clen > SIZE_MAX - crypto_box_BOXZEROBYTES) {
|
ret = crypto_secretbox_open_easy(m, c, clen, n, k);
|
||||||
return -1;
|
sodium_memzero(k, sizeof k);
|
||||||
}
|
|
||||||
c_boxed_len = clen + crypto_box_BOXZEROBYTES;
|
|
||||||
if ((c_boxed = (unsigned char *) malloc(c_boxed_len)) == NULL) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
memset(c_boxed, 0, crypto_box_BOXZEROBYTES);
|
|
||||||
memcpy(c_boxed + crypto_box_BOXZEROBYTES, c, clen);
|
|
||||||
m_boxed_len = crypto_box_ZEROBYTES + (clen - crypto_box_MACBYTES);
|
|
||||||
if ((m_boxed = (unsigned char *) malloc(m_boxed_len)) == NULL) {
|
|
||||||
free(c_boxed);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
sodium_mlock(m_boxed, m_boxed_len);
|
|
||||||
rc = crypto_box_open(m_boxed, c_boxed,
|
|
||||||
(unsigned long long) c_boxed_len, n, pk, sk);
|
|
||||||
free(c_boxed);
|
|
||||||
if (rc != 0) {
|
|
||||||
sodium_munlock(m_boxed, m_boxed_len);
|
|
||||||
free(m_boxed);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
memcpy(m, m_boxed + crypto_box_ZEROBYTES, clen - crypto_box_MACBYTES);
|
|
||||||
sodium_munlock(m_boxed, m_boxed_len);
|
|
||||||
free(m_boxed);
|
|
||||||
|
|
||||||
return 0;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user