1 /* $OpenBSD: cipher-chachapoly-libcrypto.c,v 1.2 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 <openssl/evp.h>
31
32 #include "log.h"
33 #include "sshbuf.h"
34 #include "ssherr.h"
35 #include "cipher-chachapoly.h"
36
37 struct chachapoly_ctx {
38 EVP_CIPHER_CTX *main_evp, *header_evp;
39 };
40
41 struct chachapoly_ctx *
chachapoly_new(const u_char * key,u_int keylen)42 chachapoly_new(const u_char *key, u_int keylen)
43 {
44 struct chachapoly_ctx *ctx;
45
46 if (keylen != (32 + 32)) /* 2 x 256 bit keys */
47 return NULL;
48 if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
49 return NULL;
50 if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
51 (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
52 goto fail;
53 if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
54 goto fail;
55 if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
56 goto fail;
57 if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
58 goto fail;
59 return ctx;
60 fail:
61 chachapoly_free(ctx);
62 return NULL;
63 }
64
65 void
chachapoly_free(struct chachapoly_ctx * cpctx)66 chachapoly_free(struct chachapoly_ctx *cpctx)
67 {
68 if (cpctx == NULL)
69 return;
70 EVP_CIPHER_CTX_free(cpctx->main_evp);
71 EVP_CIPHER_CTX_free(cpctx->header_evp);
72 freezero(cpctx, sizeof(*cpctx));
73 }
74
75 /*
76 * chachapoly_crypt() operates as following:
77 * En/decrypt with header key 'aadlen' bytes from 'src', storing result
78 * to 'dest'. The ciphertext here is treated as additional authenticated
79 * data for MAC calculation.
80 * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use
81 * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication
82 * tag. This tag is written on encryption and verified on decryption.
83 */
84 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)85 chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,
86 const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)
87 {
88 u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */
89 int r = SSH_ERR_INTERNAL_ERROR;
90 u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];
91
92 /*
93 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
94 * packet sequence number.
95 */
96 memset(seqbuf, 0, sizeof(seqbuf));
97 POKE_U64(seqbuf + 8, seqnr);
98 memset(poly_key, 0, sizeof(poly_key));
99 if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
100 EVP_Cipher(ctx->main_evp, poly_key,
101 poly_key, sizeof(poly_key)) < 0) {
102 r = SSH_ERR_LIBCRYPTO_ERROR;
103 goto out;
104 }
105
106 /* If decrypting, check tag before anything else */
107 if (!do_encrypt) {
108 const u_char *tag = src + aadlen + len;
109
110 poly1305_auth(expected_tag, src, aadlen + len, poly_key);
111 if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) {
112 r = SSH_ERR_MAC_INVALID;
113 goto out;
114 }
115 }
116
117 /* Crypt additional data */
118 if (aadlen) {
119 if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||
120 EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) {
121 r = SSH_ERR_LIBCRYPTO_ERROR;
122 goto out;
123 }
124 }
125
126 /* Set Chacha's block counter to 1 */
127 seqbuf[0] = 1;
128 if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||
129 EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) {
130 r = SSH_ERR_LIBCRYPTO_ERROR;
131 goto out;
132 }
133
134 /* If encrypting, calculate and append tag */
135 if (do_encrypt) {
136 poly1305_auth(dest + aadlen + len, dest, aadlen + len,
137 poly_key);
138 }
139 r = 0;
140 out:
141 explicit_bzero(expected_tag, sizeof(expected_tag));
142 explicit_bzero(seqbuf, sizeof(seqbuf));
143 explicit_bzero(poly_key, sizeof(poly_key));
144 return r;
145 }
146
147 /* Decrypt and extract the encrypted packet length */
148 int
chachapoly_get_length(struct chachapoly_ctx * ctx,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)149 chachapoly_get_length(struct chachapoly_ctx *ctx,
150 u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
151 {
152 u_char buf[4], seqbuf[16];
153
154 if (len < 4)
155 return SSH_ERR_MESSAGE_INCOMPLETE;
156 memset(seqbuf, 0, sizeof(seqbuf));
157 POKE_U64(seqbuf + 8, seqnr);
158 if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))
159 return SSH_ERR_LIBCRYPTO_ERROR;
160 if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)
161 return SSH_ERR_LIBCRYPTO_ERROR;
162 *plenp = PEEK_U32(buf);
163 return 0;
164 }
165 #endif /* defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20) */
166