1 /* $OpenBSD: cipher-chachapoly.c,v 1.10 2023/07/17 05:26:38 djm Exp $ */
2 /*
3 * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
4 *
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.
8 *
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.
16 */
17
18 #include "includes.h"
19 #ifdef WITH_OPENSSL
20 #include "openbsd-compat/openssl-compat.h"
21 #endif
22
23 #if !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20)
24
25 #include <sys/types.h>
26 #include <stdarg.h> /* needed for log.h */
27 #include <string.h>
28 #include <stdio.h> /* needed for misc.h */
29
30 #include "log.h"
31 #include "sshbuf.h"
32 #include "ssherr.h"
33 #include "cipher-chachapoly.h"
34
35 struct chachapoly_ctx {
36 struct chacha_ctx main_ctx, header_ctx;
37 };
38
39 struct chachapoly_ctx *
chachapoly_new(const u_char * key,u_int keylen)40 chachapoly_new(const u_char *key, u_int keylen)
41 {
42 struct chachapoly_ctx *ctx;
43
44 if (keylen != (32 + 32)) /* 2 x 256 bit keys */
45 return NULL;
46 if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
47 return NULL;
48 chacha_keysetup(&ctx->main_ctx, key, 256);
49 chacha_keysetup(&ctx->header_ctx, key + 32, 256);
50 return ctx;
51 }
52
53 void
chachapoly_free(struct chachapoly_ctx * cpctx)54 chachapoly_free(struct chachapoly_ctx *cpctx)
55 {
56 freezero(cpctx, sizeof(*cpctx));
57 }
58
59 /*
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.
67 */
68 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)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)
71 {
72 u_char seqbuf[8];
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;
76
77 /*
78 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
79 * packet sequence number.
80 */
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));
86
87 /* If decrypting, check tag before anything else */
88 if (!do_encrypt) {
89 const u_char *tag = src + aadlen + len;
90
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;
94 goto out;
95 }
96 }
97
98 /* Crypt additional data */
99 if (aadlen) {
100 chacha_ivsetup(&ctx->header_ctx, seqbuf, NULL);
101 chacha_encrypt_bytes(&ctx->header_ctx, src, dest, aadlen);
102 }
103
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,
107 dest + aadlen, len);
108
109 /* If encrypting, calculate and append tag */
110 if (do_encrypt) {
111 poly1305_auth(dest + aadlen + len, dest, aadlen + len,
112 poly_key);
113 }
114 r = 0;
115 out:
116 explicit_bzero(expected_tag, sizeof(expected_tag));
117 explicit_bzero(seqbuf, sizeof(seqbuf));
118 explicit_bzero(poly_key, sizeof(poly_key));
119 return r;
120 }
121
122 /* Decrypt and extract the encrypted packet length */
123 int
chachapoly_get_length(struct chachapoly_ctx * ctx,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)124 chachapoly_get_length(struct chachapoly_ctx *ctx,
125 u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
126 {
127 u_char buf[4], seqbuf[8];
128
129 if (len < 4)
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);
135 return 0;
136 }
137
138 #endif /* !defined(HAVE_EVP_CHACHA20) || defined(HAVE_BROKEN_CHACHA20) */
139