1 /* $OpenBSD: cipher-chachapoly.c,v 1.10 2023/07/17 05:26:38 djm Exp $ */
3 * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include "openbsd-compat/openssl-compat.h"
23 #if !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20)
25 #include <sys/types.h>
26 #include <stdarg.h> /* needed for log.h */
28 #include <stdio.h> /* needed for misc.h */
33 #include "cipher-chachapoly.h"
35 struct chachapoly_ctx {
36 struct chacha_ctx main_ctx, header_ctx;
39 struct chachapoly_ctx *
40 chachapoly_new(const u_char *key, u_int keylen)
42 struct chachapoly_ctx *ctx;
44 if (keylen != (32 + 32)) /* 2 x 256 bit keys */
46 if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
48 chacha_keysetup(&ctx->main_ctx, key, 256);
49 chacha_keysetup(&ctx->header_ctx, key + 32, 256);
54 chachapoly_free(struct chachapoly_ctx *cpctx)
56 freezero(cpctx, sizeof(*cpctx));
60 * chachapoly_crypt() operates as following:
61 * En/decrypt with header key 'aadlen' bytes from 'src', storing result
62 * to 'dest'. The ciphertext here is treated as additional authenticated
63 * data for MAC calculation.
64 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
65 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
66 * tag. This tag is written on encryption and verified on decryption.
69 chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
70 const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
73 const u_char one[8] = { 1, 0, 0, 0, 0, 0, 0, 0 }; /* NB little-endian */
74 u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
75 int r = SSH_ERR_INTERNAL_ERROR;
78 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
79 * packet sequence number.
81 memset(poly_key, 0, sizeof(poly_key));
82 POKE_U64(seqbuf, seqnr);
83 chacha_ivsetup(&ctx->main_ctx, seqbuf, NULL);
84 chacha_encrypt_bytes(&ctx->main_ctx,
85 poly_key, poly_key, sizeof(poly_key));
87 /* If decrypting, check tag before anything else */
89 const u_char *tag = src + aadlen + len;
91 poly1305_auth(expected_tag, src, aadlen + len, poly_key);
92 if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
93 r = SSH_ERR_MAC_INVALID;
98 /* Crypt additional data */
100 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
101 chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen);
104 /* Set Chacha's block counter to 1 */
105 chacha_ivsetup(&ctx->main_ctx, seqbuf, one);
106 chacha_encrypt_bytes(&ctx->main_ctx, src + aadlen,
109 /* If encrypting, calculate and append tag */
111 poly1305_auth(dest + aadlen + len, dest, aadlen + len,
116 explicit_bzero(expected_tag, sizeof(expected_tag));
117 explicit_bzero(seqbuf, sizeof(seqbuf));
118 explicit_bzero(poly_key, sizeof(poly_key));
122 /* Decrypt and extract the encrypted packet length */
124 chachapoly_get_length(struct chachapoly_ctx *ctx,
125 u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
127 u_char buf[4], seqbuf[8];
130 return SSH_ERR_MESSAGE_INCOMPLETE;
131 POKE_U64(seqbuf, seqnr);
132 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
133 chacha_encrypt_bytes(&ctx->header_ctx, cp, buf, 4);
134 *plenp = PEEK_U32(buf);
138 #endif /* !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20) */