1 /* $OpenBSD: cipher-chachapoly-libcrypto.c,v 1.2 2023/07/17 05:26:38 djm Exp $ */
3 * Copyright (c) 2013 Damien Miller <djm@mindrot.org>
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.
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.
20 #include "openbsd-compat/openssl-compat.h"
23 #if defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20)
25 #include <sys/types.h>
26 #include <stdarg.h> /* needed for log.h */
28 #include <stdio.h> /* needed for misc.h */
30 #include <openssl/evp.h>
35 #include "cipher-chachapoly.h"
37 struct chachapoly_ctx {
38 EVP_CIPHER_CTX *main_evp, *header_evp;
41 struct chachapoly_ctx *
42 chachapoly_new(const u_char *key, u_int keylen)
44 struct chachapoly_ctx *ctx;
46 if (keylen != (32 + 32)) /* 2 x 256 bit keys */
48 if ((ctx = calloc(1, sizeof(*ctx))) == NULL)
50 if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||
51 (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)
53 if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))
55 if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))
57 if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)
66 chachapoly_free(struct chachapoly_ctx *cpctx)
70 EVP_CIPHER_CTX_free(cpctx->main_evp);
71 EVP_CIPHER_CTX_free(cpctx->header_evp);
72 freezero(cpctx, sizeof(*cpctx));
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.
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)
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];
93 * Run ChaCha20 once to generate the Poly1305 key. The IV is the
94 * packet sequence number.
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;
106 /* If decrypting, check tag before anything else */
108 const u_char *tag = src + aadlen + len;
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;
117 /* Crypt additional data */
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;
126 /* Set Chacha's block counter to 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;
134 /* If encrypting, calculate and append tag */
136 poly1305_auth(dest + aadlen + len, dest, aadlen + len,
141 explicit_bzero(expected_tag, sizeof(expected_tag));
142 explicit_bzero(seqbuf, sizeof(seqbuf));
143 explicit_bzero(poly_key, sizeof(poly_key));
147 /* Decrypt and extract the encrypted packet length */
149 chachapoly_get_length(struct chachapoly_ctx *ctx,
150 u_int *plenp, u_int seqnr, const u_char *cp, u_int len)
152 u_char buf[4], seqbuf[16];
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);
165 #endif /* defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20) */