xref: /freebsd/crypto/openssh/cipher-chachapoly-libcrypto.c (revision 535af610a4fdace6d50960c0ad9be0597eea7a1b)
1*535af610SEd Maste /* $OpenBSD: cipher-chachapoly-libcrypto.c,v 1.2 2023/07/17 05:26:38 djm Exp $ */
219261079SEd Maste /*
319261079SEd Maste  * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
419261079SEd Maste  *
519261079SEd Maste  * Permission to use, copy, modify, and distribute this software for any
619261079SEd Maste  * purpose with or without fee is hereby granted, provided that the above
719261079SEd Maste  * copyright notice and this permission notice appear in all copies.
819261079SEd Maste  *
919261079SEd Maste  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
1019261079SEd Maste  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1119261079SEd Maste  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
1219261079SEd Maste  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
1319261079SEd Maste  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
1419261079SEd Maste  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
1519261079SEd Maste  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1619261079SEd Maste  */
1719261079SEd Maste 
1819261079SEd Maste #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)
2419261079SEd Maste 
2519261079SEd Maste #include <sys/types.h>
2619261079SEd Maste #include <stdarg.h> /* needed for log.h */
2719261079SEd Maste #include <string.h>
2819261079SEd Maste #include <stdio.h>  /* needed for misc.h */
2919261079SEd Maste 
3019261079SEd Maste #include <openssl/evp.h>
3119261079SEd Maste 
3219261079SEd Maste #include "log.h"
3319261079SEd Maste #include "sshbuf.h"
3419261079SEd Maste #include "ssherr.h"
3519261079SEd Maste #include "cipher-chachapoly.h"
3619261079SEd Maste 
3719261079SEd Maste struct chachapoly_ctx {
3819261079SEd Maste 	EVP_CIPHER_CTX *main_evp, *header_evp;
3919261079SEd Maste };
4019261079SEd Maste 
4119261079SEd Maste struct chachapoly_ctx *
chachapoly_new(const u_char * key,u_int keylen)4219261079SEd Maste chachapoly_new(const u_char *key, u_int keylen)
4319261079SEd Maste {
4419261079SEd Maste 	struct chachapoly_ctx *ctx;
4519261079SEd Maste 
4619261079SEd Maste 	if (keylen != (32 + 32)) /* 2 x 256 bit keys */
4719261079SEd Maste 		return NULL;
4819261079SEd Maste 	if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
4919261079SEd Maste 		return NULL;
5019261079SEd Maste 	if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
5119261079SEd Maste 	    (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
5219261079SEd Maste 		goto fail;
5319261079SEd Maste 	if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
5419261079SEd Maste 		goto fail;
5519261079SEd Maste 	if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
5619261079SEd Maste 		goto fail;
5719261079SEd Maste 	if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
5819261079SEd Maste 		goto fail;
5919261079SEd Maste 	return ctx;
6019261079SEd Maste  fail:
6119261079SEd Maste 	chachapoly_free(ctx);
6219261079SEd Maste 	return NULL;
6319261079SEd Maste }
6419261079SEd Maste 
6519261079SEd Maste void
chachapoly_free(struct chachapoly_ctx * cpctx)6619261079SEd Maste chachapoly_free(struct chachapoly_ctx *cpctx)
6719261079SEd Maste {
6819261079SEd Maste 	if (cpctx == NULL)
6919261079SEd Maste 		return;
7019261079SEd Maste 	EVP_CIPHER_CTX_free(cpctx->main_evp);
7119261079SEd Maste 	EVP_CIPHER_CTX_free(cpctx->header_evp);
7219261079SEd Maste 	freezero(cpctx, sizeof(*cpctx));
7319261079SEd Maste }
7419261079SEd Maste 
7519261079SEd Maste /*
7619261079SEd Maste  * chachapoly_crypt() operates as following:
7719261079SEd Maste  * En/decrypt with header key 'aadlen' bytes from 'src', storing result
7819261079SEd Maste  * to 'dest'. The ciphertext here is treated as additional authenticated
7919261079SEd Maste  * data for MAC calculation.
8019261079SEd Maste  * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
8119261079SEd Maste  * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
8219261079SEd Maste  * tag. This tag is written on encryption and verified on decryption.
8319261079SEd Maste  */
8419261079SEd Maste 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)8519261079SEd Maste chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
8619261079SEd Maste     const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
8719261079SEd Maste {
8819261079SEd Maste 	u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */
8919261079SEd Maste 	int r = SSH_ERR_INTERNAL_ERROR;
9019261079SEd Maste 	u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
9119261079SEd Maste 
9219261079SEd Maste 	/*
9319261079SEd Maste 	 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
9419261079SEd Maste 	 * packet sequence number.
9519261079SEd Maste 	 */
9619261079SEd Maste 	memset(seqbuf, 0, sizeof(seqbuf));
9719261079SEd Maste 	POKE_U64(seqbuf + 8, seqnr);
9819261079SEd Maste 	memset(poly_key, 0, sizeof(poly_key));
9919261079SEd Maste 	if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
10019261079SEd Maste 	    EVP_Cipher(ctx->main_evp, poly_key,
10119261079SEd Maste 	    poly_key, sizeof(poly_key)) < 0) {
10219261079SEd Maste 		r = SSH_ERR_LIBCRYPTO_ERROR;
10319261079SEd Maste 		goto out;
10419261079SEd Maste 	}
10519261079SEd Maste 
10619261079SEd Maste 	/* If decrypting, check tag before anything else */
10719261079SEd Maste 	if (!do_encrypt) {
10819261079SEd Maste 		const u_char *tag = src + aadlen + len;
10919261079SEd Maste 
11019261079SEd Maste 		poly1305_auth(expected_tag, src, aadlen + len, poly_key);
11119261079SEd Maste 		if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
11219261079SEd Maste 			r = SSH_ERR_MAC_INVALID;
11319261079SEd Maste 			goto out;
11419261079SEd Maste 		}
11519261079SEd Maste 	}
11619261079SEd Maste 
11719261079SEd Maste 	/* Crypt additional data */
11819261079SEd Maste 	if (aadlen) {
11919261079SEd Maste 		if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||
12019261079SEd Maste 		    EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) {
12119261079SEd Maste 			r = SSH_ERR_LIBCRYPTO_ERROR;
12219261079SEd Maste 			goto out;
12319261079SEd Maste 		}
12419261079SEd Maste 	}
12519261079SEd Maste 
12619261079SEd Maste 	/* Set Chacha's block counter to 1 */
12719261079SEd Maste 	seqbuf[0] = 1;
12819261079SEd Maste 	if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
12919261079SEd Maste 	    EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) {
13019261079SEd Maste 		r = SSH_ERR_LIBCRYPTO_ERROR;
13119261079SEd Maste 		goto out;
13219261079SEd Maste 	}
13319261079SEd Maste 
13419261079SEd Maste 	/* If encrypting, calculate and append tag */
13519261079SEd Maste 	if (do_encrypt) {
13619261079SEd Maste 		poly1305_auth(dest + aadlen + len, dest, aadlen + len,
13719261079SEd Maste 		    poly_key);
13819261079SEd Maste 	}
13919261079SEd Maste 	r = 0;
14019261079SEd Maste  out:
14119261079SEd Maste 	explicit_bzero(expected_tag, sizeof(expected_tag));
14219261079SEd Maste 	explicit_bzero(seqbuf, sizeof(seqbuf));
14319261079SEd Maste 	explicit_bzero(poly_key, sizeof(poly_key));
14419261079SEd Maste 	return r;
14519261079SEd Maste }
14619261079SEd Maste 
14719261079SEd Maste /* Decrypt and extract the encrypted packet length */
14819261079SEd Maste int
chachapoly_get_length(struct chachapoly_ctx * ctx,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)14919261079SEd Maste chachapoly_get_length(struct chachapoly_ctx *ctx,
15019261079SEd Maste     u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
15119261079SEd Maste {
15219261079SEd Maste 	u_char buf[4], seqbuf[16];
15319261079SEd Maste 
15419261079SEd Maste 	if (len < 4)
15519261079SEd Maste 		return SSH_ERR_MESSAGE_INCOMPLETE;
15619261079SEd Maste 	memset(seqbuf, 0, sizeof(seqbuf));
15719261079SEd Maste 	POKE_U64(seqbuf + 8, seqnr);
15819261079SEd Maste 	if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))
15919261079SEd Maste 		return SSH_ERR_LIBCRYPTO_ERROR;
16019261079SEd Maste 	if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)
16119261079SEd Maste 		return SSH_ERR_LIBCRYPTO_ERROR;
16219261079SEd Maste 	*plenp = PEEK_U32(buf);
16319261079SEd Maste 	return 0;
16419261079SEd Maste }
16519261079SEd Maste #endif /* defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20) */
166