summaryrefslogtreecommitdiff
path: root/src/libsodium/crypto_box
diff options
context:
space:
mode:
Diffstat (limited to 'src/libsodium/crypto_box')
-rw-r--r--src/libsodium/crypto_box/crypto_box.c108
-rw-r--r--src/libsodium/crypto_box/crypto_box_easy.c60
-rw-r--r--src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c41
-rw-r--r--src/libsodium/crypto_box/curve25519xsalsa20poly1305/checksum1
-rw-r--r--src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/after_curve25519xsalsa20poly1305.c22
-rw-r--r--src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h20
-rw-r--r--src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c19
-rw-r--r--src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/box_curve25519xsalsa20poly1305.c27
-rw-r--r--src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c27
-rw-r--r--src/libsodium/crypto_box/try.c195
10 files changed, 520 insertions, 0 deletions
diff --git a/src/libsodium/crypto_box/crypto_box.c b/src/libsodium/crypto_box/crypto_box.c
new file mode 100644
index 0000000..7ae4297
--- /dev/null
+++ b/src/libsodium/crypto_box/crypto_box.c
@@ -0,0 +1,108 @@
+
+#include "crypto_box.h"
+
+size_t
+crypto_box_seedbytes(void)
+{
+ return crypto_box_SEEDBYTES;
+}
+
+size_t
+crypto_box_publickeybytes(void)
+{
+ return crypto_box_PUBLICKEYBYTES;
+}
+
+size_t
+crypto_box_secretkeybytes(void)
+{
+ return crypto_box_SECRETKEYBYTES;
+}
+
+size_t
+crypto_box_beforenmbytes(void)
+{
+ return crypto_box_BEFORENMBYTES;
+}
+
+size_t
+crypto_box_noncebytes(void)
+{
+ return crypto_box_NONCEBYTES;
+}
+
+size_t
+crypto_box_zerobytes(void)
+{
+ return crypto_box_ZEROBYTES;
+}
+
+size_t
+crypto_box_boxzerobytes(void)
+{
+ return crypto_box_BOXZEROBYTES;
+}
+
+size_t
+crypto_box_macbytes(void)
+{
+ return crypto_box_MACBYTES;
+}
+
+const char *
+crypto_box_primitive(void)
+{
+ return crypto_box_PRIMITIVE;
+}
+
+int
+crypto_box_seed_keypair(unsigned char *pk, unsigned char *sk,
+ const unsigned char *seed)
+{
+ return crypto_box_curve25519xsalsa20poly1305_seed_keypair(pk, sk, seed);
+}
+
+int
+crypto_box_keypair(unsigned char *pk, unsigned char *sk)
+{
+ return crypto_box_curve25519xsalsa20poly1305_keypair(pk, sk);
+}
+
+int
+crypto_box_beforenm(unsigned char *k, const unsigned char *pk,
+ const unsigned char *sk)
+{
+ return crypto_box_curve25519xsalsa20poly1305_beforenm(k, pk, sk);
+}
+
+int
+crypto_box_afternm(unsigned char *c, const unsigned char *m,
+ unsigned long long mlen, const unsigned char *n,
+ const unsigned char *k)
+{
+ return crypto_box_curve25519xsalsa20poly1305_afternm(c, m, mlen, n, k);
+}
+
+int
+crypto_box_open_afternm(unsigned char *m, const unsigned char *c,
+ unsigned long long clen, const unsigned char *n,
+ const unsigned char *k)
+{
+ return crypto_box_curve25519xsalsa20poly1305_open_afternm(m, c, clen, n, k);
+}
+
+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)
+{
+ return crypto_box_curve25519xsalsa20poly1305(c, m, mlen, n, pk, sk);
+}
+
+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)
+{
+ return crypto_box_curve25519xsalsa20poly1305_open(m, c, clen, n, pk, sk);
+}
diff --git a/src/libsodium/crypto_box/crypto_box_easy.c b/src/libsodium/crypto_box/crypto_box_easy.c
new file mode 100644
index 0000000..7224f24
--- /dev/null
+++ b/src/libsodium/crypto_box/crypto_box_easy.c
@@ -0,0 +1,60 @@
+
+#include "crypto_box.h"
+#include "crypto_secretbox.h"
+#include "utils.h"
+
+int
+crypto_box_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;
+
+ (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);
+ 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_detached(c + crypto_box_MACBYTES, c, m, mlen, n,
+ pk, sk);
+}
+
+int
+crypto_box_open_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_detached(m, c, mac, clen, n, k);
+ sodium_memzero(k, sizeof k);
+
+ return ret;
+}
+
+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)
+{
+ if (clen < crypto_box_MACBYTES) {
+ return -1;
+ }
+ return crypto_box_open_detached(m, c + crypto_box_MACBYTES, c,
+ clen - crypto_box_MACBYTES,
+ n, pk, sk);
+}
diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c
new file mode 100644
index 0000000..1c002d2
--- /dev/null
+++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/box_curve25519xsalsa20poly1305_api.c
@@ -0,0 +1,41 @@
+#include "crypto_box_curve25519xsalsa20poly1305.h"
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_seedbytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_SEEDBYTES;
+}
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_publickeybytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES;
+}
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_secretkeybytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES;
+}
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_beforenmbytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES;
+}
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_noncebytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_NONCEBYTES;
+}
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_zerobytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_ZEROBYTES;
+}
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_boxzerobytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES;
+}
+
+size_t
+crypto_box_curve25519xsalsa20poly1305_macbytes(void) {
+ return crypto_box_curve25519xsalsa20poly1305_MACBYTES;
+}
diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/checksum b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/checksum
new file mode 100644
index 0000000..56a2008
--- /dev/null
+++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/checksum
@@ -0,0 +1 @@
+5fac7400caabc14a99c5c0bc13fb1df5e468e870382a3a1c
diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/after_curve25519xsalsa20poly1305.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/after_curve25519xsalsa20poly1305.c
new file mode 100644
index 0000000..a830936
--- /dev/null
+++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/after_curve25519xsalsa20poly1305.c
@@ -0,0 +1,22 @@
+#include "api.h"
+#include "crypto_secretbox_xsalsa20poly1305.h"
+
+int crypto_box_afternm(
+ unsigned char *c,
+ const unsigned char *m,unsigned long long mlen,
+ const unsigned char *n,
+ const unsigned char *k
+)
+{
+ return crypto_secretbox_xsalsa20poly1305(c,m,mlen,n,k);
+}
+
+int crypto_box_open_afternm(
+ unsigned char *m,
+ const unsigned char *c,unsigned long long clen,
+ const unsigned char *n,
+ const unsigned char *k
+)
+{
+ return crypto_secretbox_xsalsa20poly1305_open(m,c,clen,n,k);
+}
diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h
new file mode 100644
index 0000000..7f320c6
--- /dev/null
+++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/api.h
@@ -0,0 +1,20 @@
+
+#include "crypto_box_curve25519xsalsa20poly1305.h"
+
+#define crypto_box crypto_box_curve25519xsalsa20poly1305
+#define crypto_box_open crypto_box_curve25519xsalsa20poly1305_open
+#define crypto_box_seed_keypair crypto_box_curve25519xsalsa20poly1305_seed_keypair
+#define crypto_box_keypair crypto_box_curve25519xsalsa20poly1305_keypair
+#define crypto_box_beforenm crypto_box_curve25519xsalsa20poly1305_beforenm
+#define crypto_box_afternm crypto_box_curve25519xsalsa20poly1305_afternm
+#define crypto_box_open_afternm crypto_box_curve25519xsalsa20poly1305_open_afternm
+#define crypto_box_SEEDBYTES crypto_box_curve25519xsalsa20poly1305_SEEDBYTES
+#define crypto_box_PUBLICKEYBYTES crypto_box_curve25519xsalsa20poly1305_PUBLICKEYBYTES
+#define crypto_box_SECRETKEYBYTES crypto_box_curve25519xsalsa20poly1305_SECRETKEYBYTES
+#define crypto_box_BEFORENMBYTES crypto_box_curve25519xsalsa20poly1305_BEFORENMBYTES
+#define crypto_box_NONCEBYTES crypto_box_curve25519xsalsa20poly1305_NONCEBYTES
+#define crypto_box_ZEROBYTES crypto_box_curve25519xsalsa20poly1305_ZEROBYTES
+#define crypto_box_BOXZEROBYTES crypto_box_curve25519xsalsa20poly1305_BOXZEROBYTES
+#define crypto_box_MACBYTES (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
+#define crypto_box_IMPLEMENTATION crypto_box_curve25519xsalsa20poly1305_IMPLEMENTATION
+#define crypto_box_VERSION crypto_box_curve25519xsalsa20poly1305_VERSION
diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c
new file mode 100644
index 0000000..40d4300
--- /dev/null
+++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/before_curve25519xsalsa20poly1305.c
@@ -0,0 +1,19 @@
+#include "api.h"
+#include "crypto_core_hsalsa20.h"
+#include "crypto_scalarmult_curve25519.h"
+
+static const unsigned char sigma[16] = {
+ 'e', 'x', 'p', 'a', 'n', 'd', ' ', '3', '2', '-', 'b', 'y', 't', 'e', ' ', 'k'
+};
+static const unsigned char n[16] = {0};
+
+int crypto_box_beforenm(
+ unsigned char *k,
+ const unsigned char *pk,
+ const unsigned char *sk
+)
+{
+ unsigned char s[32];
+ crypto_scalarmult_curve25519(s,sk,pk);
+ return crypto_core_hsalsa20(k,n,s,sigma);
+}
diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/box_curve25519xsalsa20poly1305.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/box_curve25519xsalsa20poly1305.c
new file mode 100644
index 0000000..4b75ece
--- /dev/null
+++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/box_curve25519xsalsa20poly1305.c
@@ -0,0 +1,27 @@
+#include "api.h"
+
+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
+)
+{
+ unsigned char k[crypto_box_BEFORENMBYTES];
+ crypto_box_beforenm(k,pk,sk);
+ return crypto_box_afternm(c,m,mlen,n,k);
+}
+
+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
+)
+{
+ unsigned char k[crypto_box_BEFORENMBYTES];
+ crypto_box_beforenm(k,pk,sk);
+ return crypto_box_open_afternm(m,c,clen,n,k);
+}
diff --git a/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c
new file mode 100644
index 0000000..88183ea
--- /dev/null
+++ b/src/libsodium/crypto_box/curve25519xsalsa20poly1305/ref/keypair_curve25519xsalsa20poly1305.c
@@ -0,0 +1,27 @@
+#include <string.h>
+
+#include "crypto_hash_sha512.h"
+#include "crypto_scalarmult_curve25519.h"
+#include "api.h"
+#include "randombytes.h"
+
+int crypto_box_seed_keypair(
+ unsigned char *pk,
+ unsigned char *sk,
+ const unsigned char *seed
+)
+{
+ unsigned char hash[64];
+ crypto_hash_sha512(hash,seed,32);
+ memmove(sk,hash,32);
+ return crypto_scalarmult_curve25519_base(pk,sk);
+}
+
+int crypto_box_keypair(
+ unsigned char *pk,
+ unsigned char *sk
+)
+{
+ randombytes(sk,32);
+ return crypto_scalarmult_curve25519_base(pk,sk);
+}
diff --git a/src/libsodium/crypto_box/try.c b/src/libsodium/crypto_box/try.c
new file mode 100644
index 0000000..5f4b7cb
--- /dev/null
+++ b/src/libsodium/crypto_box/try.c
@@ -0,0 +1,195 @@
+/*
+ * crypto_box/try.c version 20090118
+ * D. J. Bernstein
+ * Public domain.
+ */
+
+#include <stdlib.h>
+#include "crypto_box.h"
+#include "utils.h"
+#include "windows/windows-quirks.h"
+
+extern unsigned char *alignedcalloc(unsigned long long);
+
+const char *primitiveimplementation = crypto_box_IMPLEMENTATION;
+
+#define MAXTEST_BYTES 10000
+#define CHECKSUM_BYTES 4096
+#define TUNE_BYTES 1536
+
+static unsigned char *ska;
+static unsigned char *pka;
+static unsigned char *skb;
+static unsigned char *pkb;
+static unsigned char *s;
+static unsigned char *n;
+static unsigned char *m;
+static unsigned char *c;
+static unsigned char *t;
+static unsigned char *ska2;
+static unsigned char *pka2;
+static unsigned char *skb2;
+static unsigned char *pkb2;
+static unsigned char *s2;
+static unsigned char *n2;
+static unsigned char *m2;
+static unsigned char *c2;
+static unsigned char *t2;
+
+#define sklen crypto_box_SECRETKEYBYTES
+#define pklen crypto_box_PUBLICKEYBYTES
+#define nlen crypto_box_NONCEBYTES
+#define slen crypto_box_BEFORENMBYTES
+
+void preallocate(void)
+{
+}
+
+void allocate(void)
+{
+ ska = alignedcalloc(sklen);
+ pka = alignedcalloc(pklen);
+ skb = alignedcalloc(sklen);
+ pkb = alignedcalloc(pklen);
+ n = alignedcalloc(nlen);
+ m = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
+ c = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
+ t = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
+ s = alignedcalloc(slen);
+ ska2 = alignedcalloc(sklen);
+ pka2 = alignedcalloc(pklen);
+ skb2 = alignedcalloc(sklen);
+ pkb2 = alignedcalloc(pklen);
+ n2 = alignedcalloc(nlen);
+ m2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
+ c2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
+ t2 = alignedcalloc(MAXTEST_BYTES + crypto_box_ZEROBYTES);
+ s2 = alignedcalloc(slen);
+}
+
+void predoit(void)
+{
+}
+
+void doit(void)
+{
+ crypto_box(c,m,TUNE_BYTES + crypto_box_ZEROBYTES,n,pka,skb);
+ crypto_box_open(t,c,TUNE_BYTES + crypto_box_ZEROBYTES,n,pkb,ska);
+}
+
+char checksum[nlen * 2 + 1];
+
+const char *checksum_compute(void)
+{
+ long long i;
+ long long j;
+
+ if (crypto_box_keypair(pka,ska) != 0) return "crypto_box_keypair returns nonzero";
+ if (crypto_box_keypair(pkb,skb) != 0) return "crypto_box_keypair returns nonzero";
+
+ for (j = 0;j < crypto_box_ZEROBYTES;++j) m[j] = 0;
+
+ for (i = 0;i < CHECKSUM_BYTES;++i) {
+ long long mlen = i + crypto_box_ZEROBYTES;
+ long long tlen = i + crypto_box_ZEROBYTES;
+ long long clen = i + crypto_box_ZEROBYTES;
+
+ for (j = -16;j < 0;++j) ska[j] = rand();
+ for (j = -16;j < 0;++j) skb[j] = rand();
+ for (j = -16;j < 0;++j) pka[j] = rand();
+ for (j = -16;j < 0;++j) pkb[j] = rand();
+ for (j = -16;j < 0;++j) m[j] = rand();
+ for (j = -16;j < 0;++j) n[j] = rand();
+
+ for (j = sklen;j < sklen + 16;++j) ska[j] = rand();
+ for (j = sklen;j < sklen + 16;++j) skb[j] = rand();
+ for (j = pklen;j < pklen + 16;++j) pka[j] = rand();
+ for (j = pklen;j < pklen + 16;++j) pkb[j] = rand();
+ for (j = mlen;j < mlen + 16;++j) m[j] = rand();
+ for (j = nlen;j < nlen + 16;++j) n[j] = rand();
+
+ for (j = -16;j < sklen + 16;++j) ska2[j] = ska[j];
+ for (j = -16;j < sklen + 16;++j) skb2[j] = skb[j];
+ for (j = -16;j < pklen + 16;++j) pka2[j] = pka[j];
+ for (j = -16;j < pklen + 16;++j) pkb2[j] = pkb[j];
+ for (j = -16;j < mlen + 16;++j) m2[j] = m[j];
+ for (j = -16;j < nlen + 16;++j) n2[j] = n[j];
+ for (j = -16;j < clen + 16;++j) c2[j] = c[j] = rand();
+
+ if (crypto_box(c,m,mlen,n,pkb,ska) != 0) return "crypto_box returns nonzero";
+
+ for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box overwrites m";
+ for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box overwrites n";
+ for (j = -16;j < 0;++j) if (c2[j] != c[j]) return "crypto_box writes before output";
+ for (j = clen;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box writes after output";
+ for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
+ if (c[j] != 0) return "crypto_box does not clear extra bytes";
+
+ for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box overwrites ska";
+ for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box overwrites skb";
+ for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box overwrites pka";
+ for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box overwrites pkb";
+
+ for (j = -16;j < 0;++j) c[j] = rand();
+ for (j = clen;j < clen + 16;++j) c[j] = rand();
+ for (j = -16;j < clen + 16;++j) c2[j] = c[j];
+ for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = rand();
+
+ if (crypto_box_open(t,c,clen,n,pka,skb) != 0) return "crypto_box_open returns nonzero";
+
+ for (j = -16;j < clen + 16;++j) if (c2[j] != c[j]) return "crypto_box_open overwrites c";
+ for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open overwrites n";
+ for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open writes before output";
+ for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open writes after output";
+ for (j = 0;j < crypto_box_ZEROBYTES;++j)
+ if (t[j] != 0) return "crypto_box_open does not clear extra bytes";
+
+ for (j = -16;j < sklen + 16;++j) if (ska2[j] != ska[j]) return "crypto_box_open overwrites ska";
+ for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites skb";
+ for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pka";
+ for (j = -16;j < pklen + 16;++j) if (pkb2[j] != pkb[j]) return "crypto_box_open overwrites pkb";
+
+ for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "plaintext does not match";
+
+ for (j = -16;j < slen + 16;++j) s2[j] = s[j] = rand();
+ if (crypto_box_beforenm(s,pkb,ska) != 0) return "crypto_box_beforenm returns nonzero";
+ for (j = -16;j < pklen + 16;++j) if (pka2[j] != pka[j]) return "crypto_box_open overwrites pk";
+ for (j = -16;j < sklen + 16;++j) if (skb2[j] != skb[j]) return "crypto_box_open overwrites sk";
+ for (j = -16;j < 0;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes before output";
+ for (j = slen;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_beforenm writes after output";
+
+ for (j = -16;j < slen + 16;++j) s2[j] = s[j];
+ for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = rand();
+ if (crypto_box_afternm(t,m,mlen,n,s) != 0) return "crypto_box_afternm returns nonzero";
+ for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_afternm overwrites s";
+ for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_afternm overwrites m";
+ for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_afternm overwrites n";
+ for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes before output";
+ for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_afternm writes after output";
+ for (j = 0;j < crypto_box_BOXZEROBYTES;++j)
+ if (t[j] != 0) return "crypto_box_afternm does not clear extra bytes";
+ for (j = 0;j < mlen;++j) if (t[j] != c[j]) return "crypto_box_afternm does not match crypto_box";
+
+ if (crypto_box_beforenm(s,pka,skb) != 0) return "crypto_box_beforenm returns nonzero";
+
+ for (j = -16;j < tlen + 16;++j) t2[j] = t[j] = rand();
+ if (crypto_box_open_afternm(t,c,clen,n,s) != 0) return "crypto_box_open_afternm returns nonzero";
+ for (j = -16;j < slen + 16;++j) if (s2[j] != s[j]) return "crypto_box_open_afternm overwrites s";
+ for (j = -16;j < mlen + 16;++j) if (m2[j] != m[j]) return "crypto_box_open_afternm overwrites m";
+ for (j = -16;j < nlen + 16;++j) if (n2[j] != n[j]) return "crypto_box_open_afternm overwrites n";
+ for (j = -16;j < 0;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes before output";
+ for (j = tlen;j < tlen + 16;++j) if (t2[j] != t[j]) return "crypto_box_open_afternm writes after output";
+ for (j = 0;j < crypto_box_ZEROBYTES;++j)
+ if (t[j] != 0) return "crypto_box_open_afternm does not clear extra bytes";
+ for (j = 0;j < mlen;++j) if (t[j] != m[j]) return "crypto_box_open_afternm does not match crypto_box_open";
+
+ for (j = 0;j < i;++j) n[j % nlen] ^= c[j + crypto_box_BOXZEROBYTES];
+ if (i == 0) m[crypto_box_ZEROBYTES] = 0;
+ m[i + crypto_box_ZEROBYTES] = m[crypto_box_ZEROBYTES];
+ for (j = 0;j < i;++j) m[j + crypto_box_ZEROBYTES] ^= c[j + crypto_box_BOXZEROBYTES];
+ }
+
+ sodium_bin2hex(checksum, sizeof checksum, n, nlen);
+
+ return 0;
+}