+/*----- The NaCl box transform --------------------------------------------*
+ *
+ * This transform is very similar to the NaCl `crypto_secretbox' transform
+ * described in Bernstein, `Cryptography in NaCl', with the difference that,
+ * rather than using XSalsa20, we use either Salsa20/r or ChaChar, because we
+ * have no need of XSalsa20's extended nonce. The default cipher is Salsa20.
+ *
+ * Salsa20 and ChaCha accept a 64-bit nonce. The low 32 bits are the
+ * sequence number, and the high 32 bits are the type, both big-endian.
+ *
+ * +------+------+
+ * | seq | type |
+ * +------+------+
+ * 32 32
+ *
+ * A stream is generated by concatenating the raw output blocks generated
+ * with this nonce and successive counter values starting from zero. The
+ * first 32 bytes of the stream are used as a key for Poly1305: the first 16
+ * bytes are the universal hash key r, and the second 16 bytes are the mask
+ * value s.
+ *
+ * +------+------+ +------...------+
+ * | r | s | | keystream |
+ * +------+------+ +------...------+
+ * 128 128 sz
+ *
+ * The remainder of the stream is XORed with the incoming plaintext to form a
+ * ciphertext with the same length. The ciphertext (only) is then tagged
+ * using Poly1305. The tag, sequence number, and ciphertext are concatenated
+ * in this order, and transmitted.
+ *
+ *
+ * +---...---+------+------...------+
+ * | tag | seq | ciphertext |
+ * +---...---+------+------...------+
+ * 128 32 sz
+ *
+ * Note that there is no need to authenticate the type separately, since it
+ * was used to select the cipher nonce, and hence the Poly1305 key. The
+ * Poly1305 tag length is fixed.
+ */
+
+typedef struct naclbox_algs {
+ bulkalgs _b;
+ const gccipher *c; size_t cksz;
+} naclbox_algs;
+
+typedef struct naclbox_ctx {
+ bulkctx _b;
+ struct { gcipher *c; } d[NDIR];
+} naclbox_ctx;
+
+
+static bulkalgs *naclbox_getalgs(const algswitch *asw, dstr *e,
+ key_file *kf, key *k)
+{
+ naclbox_algs *a = CREATE(naclbox_algs);
+ const char *p;
+ char *qq;
+ unsigned long n;
+
+ /* --- Collect the selected cipher and check that it's supported --- */
+
+ p = key_getattr(kf, k, "cipher");
+ if (!p || strcmp(p, "salsa20") == 0) a->c = &salsa20;
+ else if (strcmp(p, "salsa20/12") == 0) a->c = &salsa2012;
+ else if (strcmp(p, "salsa20/8") == 0) a->c = &salsa208;
+ else if (strcmp(p, "chacha20") == 0) a->c = &chacha20;
+ else if (strcmp(p, "chacha12") == 0) a->c = &chacha12;
+ else if (strcmp(p, "chacha8") == 0) a->c = &chacha8;
+ else {
+ a_format(e, "unknown-cipher", "%s", p, A_END);
+ goto fail;
+ }
+
+ /* --- Collect the selected MAC, and check the tag length --- */
+
+ p = key_getattr(kf, k, "mac");
+ if (!p)
+ ;
+ else if (strncmp(p, "poly1305", 8) != 0 || (p[8] && p[8] != '/')) {
+ a_format(e, "unknown-mac", "%s", p, A_END);
+ goto fail;
+ } else if (p[8] == '/') {
+ n = strtoul(p + 9, &qq, 0);
+ if (*qq) {
+ a_format(e, "bad-tag-length-string", "%s", p + 9, A_END);
+ goto fail;
+ }
+ if (n != 128) {
+ a_format(e, "bad-tag-length", "%lu", n, A_END);
+ goto fail;
+ }
+ }
+
+ return (&a->_b);
+fail:
+ DESTROY(a);
+ return (0);
+}
+
+#ifndef NTRACE
+static void naclbox_tracealgs(const bulkalgs *aa)
+{
+ const naclbox_algs *a = (const naclbox_algs *)aa;
+
+ trace(T_CRYPTO, "crypto: cipher = %s", a->c->name);
+ trace(T_CRYPTO, "crypto: mac = poly1305/128");
+}
+#endif
+
+static int naclbox_checkalgs(bulkalgs *aa, const algswitch *asw, dstr *e)
+{
+ naclbox_algs *a = (naclbox_algs *)aa;
+
+ if ((a->cksz = keysz(asw->hashsz, a->c->keysz)) == 0) {
+ a_format(e, "cipher", "%s", a->c->name,
+ "no-key-size", "%lu", (unsigned long)asw->hashsz,
+ A_END);
+ return (-1);
+ }
+ return (0);
+}
+
+static int naclbox_samealgsp(const bulkalgs *aa, const bulkalgs *bb)
+{
+ const naclbox_algs *a = (const naclbox_algs *)aa,
+ *b = (const naclbox_algs *)bb;
+ return (a->c == b->c);
+}
+
+static void naclbox_alginfo(const bulkalgs *aa, admin *adm)
+{
+ const naclbox_algs *a = (const naclbox_algs *)aa;
+ a_info(adm, "cipher=%s", a->c->name, "cipher-keysz=32", A_END);
+ a_info(adm, "mac=poly1305", "mac-tagsz=16", A_END);
+}
+
+static size_t naclbox_overhead(const bulkalgs *aa)
+ { return (POLY1305_TAGSZ + SEQSZ); }
+
+static size_t naclbox_expsz(const bulkalgs *aa)
+ { return (MEG(2048)); }
+
+static bulkctx *naclbox_genkeys(const bulkalgs *aa, const struct rawkey *rk)
+{
+ const naclbox_algs *a = (const naclbox_algs *)aa;
+ naclbox_ctx *bc = CREATE(naclbox_ctx);
+ octet k[MAXHASHSZ];
+ int i;
+
+ for (i = 0; i < NDIR; i++) {
+ ks_derivekey(k, a->cksz, rk, i, "encryption");
+ bc->d[i].c = GC_INIT(a->c, k, a->cksz);
+ }
+ return (&bc->_b);
+}
+
+typedef struct naclbox_chal {
+ bulkchal _b;
+ gcipher *c;
+} naclbox_chal;
+
+static bulkchal *naclbox_genchal(const bulkalgs *aa)
+{
+ const naclbox_algs *a = (const naclbox_algs *)aa;
+ naclbox_chal *c = CREATE(naclbox_chal);
+ rand_get(RAND_GLOBAL, buf_t, a->cksz);
+ c->c = GC_INIT(a->c, buf_t, a->cksz);
+ IF_TRACING(T_CHAL, {
+ trace(T_CHAL, "chal: generated new challenge key");
+ trace_block(T_CRYPTO, "chal: new key", buf_t, a->cksz);
+ })
+ c->_b.tagsz = 16;
+ return (&c->_b);
+}
+
+static int naclbox_chaltag(bulkchal *bc, const void *m, size_t msz, void *t)
+{
+ naclbox_chal *c = (naclbox_chal *)bc;
+ octet b0[SALSA20_NONCESZ];
+ assert(msz <= sizeof(b0));
+ memcpy(b0, m, msz); memset(b0 + msz, 0, sizeof(b0) - msz);
+ GC_SETIV(c->c, b0);
+ GC_ENCRYPT(c->c, 0, t, c->_b.tagsz);
+ return (0);
+}
+
+static int naclbox_chalvrf(bulkchal *bc, const void *m, size_t msz,
+ const void *t)
+{
+ naclbox_chal *c = (naclbox_chal *)bc;
+ octet b0[SALSA20_NONCESZ], b1[16];
+ assert(msz <= sizeof(b0)); assert(c->_b.tagsz <= sizeof(b1));
+ memcpy(b0, m, msz); memset(b0 + msz, 0, sizeof(b0) - msz);
+ GC_SETIV(c->c, b0);
+ GC_ENCRYPT(c->c, 0, b1, c->_b.tagsz);
+ return (ct_memeq(t, b1, c->_b.tagsz) ? 0 : -1);
+}
+
+static void naclbox_freechal(bulkchal *bc)
+ { naclbox_chal *c = (naclbox_chal *)bc; GC_DESTROY(c->c); DESTROY(c); }
+
+static void naclbox_freealgs(bulkalgs *aa)
+ { naclbox_algs *a = (naclbox_algs *)aa; DESTROY(a); }
+
+static void naclbox_freectx(bulkctx *bbc)
+{
+ naclbox_ctx *bc = (naclbox_ctx *)bbc;
+ int i;
+
+ for (i = 0; i < NDIR; i++) GC_DESTROY(bc->d[i].c);
+ DESTROY(bc);
+}
+
+static int naclbox_encrypt(bulkctx *bbc, unsigned ty,
+ buf *b, buf *bb, uint32 seq)
+{
+ naclbox_ctx *bc = (naclbox_ctx *)bbc;
+ gcipher *c = bc->d[DIR_OUT].c;
+ poly1305_key polyk;
+ poly1305_ctx poly;
+ const octet *p = BCUR(b);
+ size_t sz = BLEFT(b);
+ octet *qmac, *qseq, *qpk;
+
+ /* --- Determine the ciphertext layout --- */
+
+ if (buf_ensure(bb, POLY1305_TAGSZ + SEQSZ + sz)) return (0);
+ qmac = BCUR(bb); qseq = qmac + POLY1305_TAGSZ; qpk = qseq + SEQSZ;
+ BSTEP(bb, POLY1305_TAGSZ + SEQSZ + sz);
+
+ /* --- Construct and set the nonce --- */
+
+ STORE32(qseq, seq);
+ memcpy(buf_u, qseq, SEQSZ); STORE32(buf_u + SEQSZ, ty);
+ GC_SETIV(c, buf_u);
+ TRACE_IV(buf_u, SALSA20_NONCESZ);
+
+ /* --- Determine the MAC key --- */
+
+ GC_ENCRYPT(c, 0, buf_u, POLY1305_KEYSZ + POLY1305_MASKSZ);
+ poly1305_keyinit(&polyk, buf_u, POLY1305_KEYSZ);
+ poly1305_macinit(&poly, &polyk, buf_u + POLY1305_KEYSZ);
+
+ /* --- Encrypt the message --- */
+
+ GC_ENCRYPT(c, p, qpk, sz);
+ TRACE_CT(qpk, sz);
+
+ /* --- Compute the MAC --- */
+
+ poly1305_hash(&poly, qpk, sz);
+ poly1305_done(&poly, qmac);
+ TRACE_MAC(qmac, POLY1305_TAGSZ);
+
+ /* --- We're done --- */
+
+ return (0);
+}
+
+static int naclbox_decrypt(bulkctx *bbc, unsigned ty,
+ buf *b, buf *bb, uint32 *seq)
+{
+ naclbox_ctx *bc = (naclbox_ctx *)bbc;
+ gcipher *c = bc->d[DIR_IN].c;
+ poly1305_key polyk;
+ poly1305_ctx poly;
+ const octet *pmac, *pseq, *ppk;
+ size_t psz = BLEFT(b);
+ size_t sz;
+ octet *q = BCUR(bb);
+
+ /* --- Break up the packet into its components --- */
+
+ if (psz < SEQSZ + POLY1305_TAGSZ) {
+ T( trace(T_KEYSET, "keyset: block too small for keyset"); )
+ return (KSERR_MALFORMED);
+ }
+ sz = psz - SEQSZ - POLY1305_TAGSZ;
+ pmac = BCUR(b); pseq = pmac + POLY1305_TAGSZ; ppk = pseq + SEQSZ;
+
+ /* --- Construct and set the nonce --- */
+
+ memcpy(buf_u, pseq, SEQSZ); STORE32(buf_u + SEQSZ, ty);
+ GC_SETIV(c, buf_u);
+ TRACE_IV(buf_u, SALSA20_NONCESZ);
+
+ /* --- Determine the MAC key --- */
+
+ GC_ENCRYPT(c, 0, buf_u, POLY1305_KEYSZ + POLY1305_MASKSZ);
+ poly1305_keyinit(&polyk, buf_u, POLY1305_KEYSZ);
+ poly1305_macinit(&poly, &polyk, buf_u + POLY1305_KEYSZ);
+
+ /* --- Verify the MAC on the packet --- */
+
+ poly1305_hash(&poly, ppk, sz);
+ poly1305_done(&poly, buf_u);
+ TRACE_MAC(buf_u, POLY1305_TAGSZ);
+ if (!ct_memeq(buf_u, pmac, POLY1305_TAGSZ)) {
+ TRACE_MACERR(pmac, POLY1305_TAGSZ);
+ return (KSERR_DECRYPT);
+ }
+
+ /* --- Decrypt the packet --- */
+
+ GC_DECRYPT(c, ppk, q, sz);
+
+ /* --- Finished --- */
+
+ *seq = LOAD32(pseq);
+ BSTEP(bb, sz);
+ return (0);
+}
+