1*535af610SEd Maste /* $OpenBSD: cipher-chachapoly.c,v 1.10 2023/07/17 05:26:38 djm Exp $ */
2f7167e0eSDag-Erling Smørgrav /*
3f7167e0eSDag-Erling Smørgrav * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
4f7167e0eSDag-Erling Smørgrav *
5f7167e0eSDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any
6f7167e0eSDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above
7f7167e0eSDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies.
8f7167e0eSDag-Erling Smørgrav *
9f7167e0eSDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10f7167e0eSDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11f7167e0eSDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12f7167e0eSDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13f7167e0eSDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14f7167e0eSDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15f7167e0eSDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16f7167e0eSDag-Erling Smørgrav */
17f7167e0eSDag-Erling Smørgrav
18f7167e0eSDag-Erling Smørgrav #include "includes.h"
1919261079SEd Maste #ifdef WITH_OPENSSL
2019261079SEd Maste #include "openbsd-compat/openssl-compat.h"
2119261079SEd Maste #endif
2219261079SEd Maste
2319261079SEd Maste #if !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20)
24f7167e0eSDag-Erling Smørgrav
25f7167e0eSDag-Erling Smørgrav #include <sys/types.h>
26f7167e0eSDag-Erling Smørgrav #include <stdarg.h> /* needed for log.h */
27f7167e0eSDag-Erling Smørgrav #include <string.h>
28f7167e0eSDag-Erling Smørgrav #include <stdio.h> /* needed for misc.h */
29f7167e0eSDag-Erling Smørgrav
30f7167e0eSDag-Erling Smørgrav #include "log.h"
31a0ee8cc6SDag-Erling Smørgrav #include "sshbuf.h"
32a0ee8cc6SDag-Erling Smørgrav #include "ssherr.h"
33f7167e0eSDag-Erling Smørgrav #include "cipher-chachapoly.h"
34f7167e0eSDag-Erling Smørgrav
3519261079SEd Maste struct chachapoly_ctx {
3619261079SEd Maste struct chacha_ctx main_ctx, header_ctx;
3719261079SEd Maste };
3819261079SEd Maste
3919261079SEd Maste struct chachapoly_ctx *
chachapoly_new(const u_char * key,u_int keylen)4019261079SEd Maste chachapoly_new(const u_char *key, u_int keylen)
41f7167e0eSDag-Erling Smørgrav {
4219261079SEd Maste struct chachapoly_ctx *ctx;
4319261079SEd Maste
44f7167e0eSDag-Erling Smørgrav if (keylen != (32 + 32)) /* 2 x 256 bit keys */
4519261079SEd Maste return NULL;
4619261079SEd Maste if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
4719261079SEd Maste return NULL;
48f7167e0eSDag-Erling Smørgrav chacha_keysetup(&ctx->main_ctx, key, 256);
49f7167e0eSDag-Erling Smørgrav chacha_keysetup(&ctx->header_ctx, key + 32, 256);
5019261079SEd Maste return ctx;
5119261079SEd Maste }
5219261079SEd Maste
5319261079SEd Maste void
chachapoly_free(struct chachapoly_ctx * cpctx)5419261079SEd Maste chachapoly_free(struct chachapoly_ctx *cpctx)
5519261079SEd Maste {
5619261079SEd Maste freezero(cpctx, sizeof(*cpctx));
57f7167e0eSDag-Erling Smørgrav }
58f7167e0eSDag-Erling Smørgrav
59f7167e0eSDag-Erling Smørgrav /*
60f7167e0eSDag-Erling Smørgrav * chachapoly_crypt() operates as following:
61f7167e0eSDag-Erling Smørgrav * En/decrypt with header key 'aadlen' bytes from 'src', storing result
62f7167e0eSDag-Erling Smørgrav * to 'dest'. The ciphertext here is treated as additional authenticated
63f7167e0eSDag-Erling Smørgrav * data for MAC calculation.
64f7167e0eSDag-Erling Smørgrav * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
65f7167e0eSDag-Erling Smørgrav * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
66f7167e0eSDag-Erling Smørgrav * tag. This tag is written on encryption and verified on decryption.
67f7167e0eSDag-Erling Smørgrav */
68f7167e0eSDag-Erling Smørgrav int
chachapoly_crypt(struct chachapoly_ctx * ctx,u_int seqnr,u_char * dest,const u_char * src,u_int len,u_int aadlen,u_int authlen,int do_encrypt)69f7167e0eSDag-Erling Smørgrav chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
70f7167e0eSDag-Erling Smørgrav const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
71f7167e0eSDag-Erling Smørgrav {
72f7167e0eSDag-Erling Smørgrav u_char seqbuf[8];
73f7167e0eSDag-Erling Smørgrav const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */
74f7167e0eSDag-Erling Smørgrav u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
75a0ee8cc6SDag-Erling Smørgrav int r = SSH_ERR_INTERNAL_ERROR;
76f7167e0eSDag-Erling Smørgrav
77f7167e0eSDag-Erling Smørgrav /*
78f7167e0eSDag-Erling Smørgrav * Run ChaCha20 once to generate the Poly1305 key. The IV is the
79f7167e0eSDag-Erling Smørgrav * packet sequence number.
80f7167e0eSDag-Erling Smørgrav */
81b83788ffSDag-Erling Smørgrav memset(poly_key, 0, sizeof(poly_key));
82a0ee8cc6SDag-Erling Smørgrav POKE_U64(seqbuf, seqnr);
83f7167e0eSDag-Erling Smørgrav chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
84f7167e0eSDag-Erling Smørgrav chacha_encrypt_bytes(&ctx->main_ctx,
85f7167e0eSDag-Erling Smørgrav poly_key, poly_key, sizeof(poly_key));
86f7167e0eSDag-Erling Smørgrav
87f7167e0eSDag-Erling Smørgrav /* If decrypting, check tag before anything else */
88f7167e0eSDag-Erling Smørgrav if (!do_encrypt) {
89f7167e0eSDag-Erling Smørgrav const u_char *tag = src + aadlen + len;
90f7167e0eSDag-Erling Smørgrav
91f7167e0eSDag-Erling Smørgrav poly1305_auth(expected_tag, src, aadlen + len, poly_key);
92a0ee8cc6SDag-Erling Smørgrav if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
93a0ee8cc6SDag-Erling Smørgrav r = SSH_ERR_MAC_INVALID;
94f7167e0eSDag-Erling Smørgrav goto out;
95f7167e0eSDag-Erling Smørgrav }
96a0ee8cc6SDag-Erling Smørgrav }
97a0ee8cc6SDag-Erling Smørgrav
98f7167e0eSDag-Erling Smørgrav /* Crypt additional data */
99f7167e0eSDag-Erling Smørgrav if (aadlen) {
100f7167e0eSDag-Erling Smørgrav chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
101f7167e0eSDag-Erling Smørgrav chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen);
102f7167e0eSDag-Erling Smørgrav }
103a0ee8cc6SDag-Erling Smørgrav
104a0ee8cc6SDag-Erling Smørgrav /* Set Chacha's block counter to 1 */
105a0ee8cc6SDag-Erling Smørgrav chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
106f7167e0eSDag-Erling Smørgrav chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen,
107f7167e0eSDag-Erling Smørgrav dest + aadlen, len);
108f7167e0eSDag-Erling Smørgrav
109f7167e0eSDag-Erling Smørgrav /* If encrypting, calculate and append tag */
110f7167e0eSDag-Erling Smørgrav if (do_encrypt) {
111f7167e0eSDag-Erling Smørgrav poly1305_auth(dest + aadlen + len, dest, aadlen + len,
112f7167e0eSDag-Erling Smørgrav poly_key);
113f7167e0eSDag-Erling Smørgrav }
114f7167e0eSDag-Erling Smørgrav r = 0;
115f7167e0eSDag-Erling Smørgrav out:
116b83788ffSDag-Erling Smørgrav explicit_bzero(expected_tag, sizeof(expected_tag));
117b83788ffSDag-Erling Smørgrav explicit_bzero(seqbuf, sizeof(seqbuf));
118b83788ffSDag-Erling Smørgrav explicit_bzero(poly_key, sizeof(poly_key));
119f7167e0eSDag-Erling Smørgrav return r;
120f7167e0eSDag-Erling Smørgrav }
121f7167e0eSDag-Erling Smørgrav
122f7167e0eSDag-Erling Smørgrav /* Decrypt and extract the encrypted packet length */
123f7167e0eSDag-Erling Smørgrav int
chachapoly_get_length(struct chachapoly_ctx * ctx,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)124f7167e0eSDag-Erling Smørgrav chachapoly_get_length(struct chachapoly_ctx *ctx,
125f7167e0eSDag-Erling Smørgrav u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
126f7167e0eSDag-Erling Smørgrav {
127f7167e0eSDag-Erling Smørgrav u_char buf[4], seqbuf[8];
128f7167e0eSDag-Erling Smørgrav
129f7167e0eSDag-Erling Smørgrav if (len < 4)
130a0ee8cc6SDag-Erling Smørgrav return SSH_ERR_MESSAGE_INCOMPLETE;
131a0ee8cc6SDag-Erling Smørgrav POKE_U64(seqbuf, seqnr);
132f7167e0eSDag-Erling Smørgrav chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
133f7167e0eSDag-Erling Smørgrav chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4);
134a0ee8cc6SDag-Erling Smørgrav *plenp = PEEK_U32(buf);
135f7167e0eSDag-Erling Smørgrav return 0;
136f7167e0eSDag-Erling Smørgrav }
13719261079SEd Maste
13819261079SEd Maste #endif /* !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20) */
139