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