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