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