/src/hpn-ssh/cipher-chachapoly-libcrypto.c
Line  | Count  | Source (jump to first uncovered line)  | 
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  |  |  | 
38  |  | /* using the EVP_MAC interface for poly1305 is significantly  | 
39  |  |  * faster than the version bundled with OpenSSH. However,  | 
40  |  |  * this interface is only available in OpenSSL 3.0+  | 
41  |  |  * -cjr 10/21/2022 */  | 
42  |  | struct chachapoly_ctx { | 
43  |  |   EVP_CIPHER_CTX *main_evp, *header_evp;  | 
44  |  | #ifdef OPENSSL_HAVE_POLY_EVP  | 
45  |  |   EVP_MAC_CTX    *poly_ctx;  | 
46  |  | #else  | 
47  |  |   char           *poly_ctx;  | 
48  |  | #endif  | 
49  |  | };  | 
50  |  |  | 
51  |  | struct chachapoly_ctx *  | 
52  |  | chachapoly_new(const u_char *key, u_int keylen)  | 
53  | 0  | { | 
54  | 0  |   struct chachapoly_ctx *ctx;  | 
55  |  | 
  | 
56  | 0  |   if (keylen != (32 + 32)) /* 2 x 256 bit keys */  | 
57  | 0  |     return NULL;  | 
58  | 0  |   if ((ctx = calloc(1, sizeof(*ctx))) == NULL)  | 
59  | 0  |     return NULL;  | 
60  | 0  |   if ((ctx->main_evp = EVP_CIPHER_CTX_new()) == NULL ||  | 
61  | 0  |       (ctx->header_evp = EVP_CIPHER_CTX_new()) == NULL)  | 
62  | 0  |     goto fail;  | 
63  | 0  |   if (!EVP_CipherInit(ctx->main_evp, EVP_chacha20(), key, NULL, 1))  | 
64  | 0  |     goto fail;  | 
65  | 0  |   if (!EVP_CipherInit(ctx->header_evp, EVP_chacha20(), key + 32, NULL, 1))  | 
66  | 0  |     goto fail;  | 
67  | 0  |   if (EVP_CIPHER_CTX_iv_length(ctx->header_evp) != 16)  | 
68  | 0  |     goto fail;  | 
69  |  | #ifdef OPENSSL_HAVE_POLY_EVP  | 
70  |  |   EVP_MAC *mac = NULL;  | 
71  |  |   if ((mac = EVP_MAC_fetch(NULL, "POLY1305", NULL)) == NULL)  | 
72  |  |     goto fail;  | 
73  |  |   if ((ctx->poly_ctx = EVP_MAC_CTX_new(mac)) == NULL)  | 
74  |  |     goto fail;  | 
75  |  | #else  | 
76  | 0  |   ctx->poly_ctx = NULL;  | 
77  | 0  | #endif  | 
78  | 0  |   return ctx;  | 
79  | 0  |  fail:  | 
80  | 0  |   chachapoly_free(ctx);  | 
81  | 0  |   return NULL;  | 
82  | 0  | }  | 
83  |  |  | 
84  |  | void  | 
85  |  | chachapoly_free(struct chachapoly_ctx *cpctx)  | 
86  | 0  | { | 
87  | 0  |   if (cpctx == NULL)  | 
88  | 0  |     return;  | 
89  | 0  |   EVP_CIPHER_CTX_free(cpctx->main_evp);  | 
90  | 0  |   EVP_CIPHER_CTX_free(cpctx->header_evp);  | 
91  |  | #ifdef OPENSSL_HAVE_POLY_EVP  | 
92  |  |   EVP_MAC_CTX_free(cpctx->poly_ctx);  | 
93  |  | #endif  | 
94  | 0  |   freezero(cpctx, sizeof(*cpctx));  | 
95  | 0  | }  | 
96  |  |  | 
97  |  | /*  | 
98  |  |  * chachapoly_crypt() operates as following:  | 
99  |  |  * En/decrypt with header key 'aadlen' bytes from 'src', storing result  | 
100  |  |  * to 'dest'. The ciphertext here is treated as additional authenticated  | 
101  |  |  * data for MAC calculation.  | 
102  |  |  * En/decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. Use  | 
103  |  |  * POLY1305_TAGLEN bytes at offset 'len'+'aadlen' as the authentication  | 
104  |  |  * tag. This tag is written on encryption and verified on decryption.  | 
105  |  |  */  | 
106  |  | int  | 
107  |  | chachapoly_crypt(struct chachapoly_ctx *ctx, u_int seqnr, u_char *dest,  | 
108  |  |     const u_char *src, u_int len, u_int aadlen, u_int authlen, int do_encrypt)  | 
109  | 0  | { | 
110  | 0  |   u_char seqbuf[16]; /* layout: u64 counter || u64 seqno */  | 
111  | 0  |   int r = SSH_ERR_INTERNAL_ERROR;  | 
112  | 0  |   u_char expected_tag[POLY1305_TAGLEN], poly_key[POLY1305_KEYLEN];  | 
113  |  |  | 
114  |  |   /*  | 
115  |  |    * Run ChaCha20 once to generate the Poly1305 key. The IV is the  | 
116  |  |    * packet sequence number.  | 
117  |  |    */  | 
118  | 0  |   memset(seqbuf, 0, sizeof(seqbuf));  | 
119  | 0  |   POKE_U64(seqbuf + 8, seqnr);  | 
120  | 0  |   memset(poly_key, 0, sizeof(poly_key));  | 
121  | 0  |   if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||  | 
122  | 0  |       EVP_Cipher(ctx->main_evp, poly_key,  | 
123  | 0  |       poly_key, sizeof(poly_key)) < 0) { | 
124  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
125  | 0  |     goto out;  | 
126  | 0  |   }  | 
127  |  |  | 
128  |  |   /* If decrypting, check tag before anything else */  | 
129  | 0  |   if (!do_encrypt) { | 
130  | 0  |     const u_char *tag = src + aadlen + len;  | 
131  | 0  |     poly1305_auth(ctx->poly_ctx, expected_tag, src, aadlen + len, poly_key);  | 
132  | 0  |     if (timingsafe_bcmp(expected_tag, tag, POLY1305_TAGLEN) != 0) { | 
133  | 0  |       r = SSH_ERR_MAC_INVALID;  | 
134  | 0  |       goto out;  | 
135  | 0  |     }  | 
136  | 0  |   }  | 
137  |  |  | 
138  |  |   /* Crypt additional data */  | 
139  | 0  |   if (aadlen) { | 
140  | 0  |     if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 1) ||  | 
141  | 0  |         EVP_Cipher(ctx->header_evp, dest, src, aadlen) < 0) { | 
142  | 0  |       r = SSH_ERR_LIBCRYPTO_ERROR;  | 
143  | 0  |       goto out;  | 
144  | 0  |     }  | 
145  | 0  |   }  | 
146  |  |  | 
147  |  |   /* Set Chacha's block counter to 1 */  | 
148  | 0  |   seqbuf[0] = 1;  | 
149  | 0  |   if (!EVP_CipherInit(ctx->main_evp, NULL, NULL, seqbuf, 1) ||  | 
150  | 0  |       EVP_Cipher(ctx->main_evp, dest + aadlen, src + aadlen, len) < 0) { | 
151  | 0  |     r = SSH_ERR_LIBCRYPTO_ERROR;  | 
152  | 0  |     goto out;  | 
153  | 0  |   }  | 
154  |  |  | 
155  |  |   /* If encrypting, calculate and append tag */  | 
156  | 0  |   if (do_encrypt) { | 
157  | 0  |     poly1305_auth(ctx->poly_ctx, dest + aadlen + len, dest, aadlen + len,  | 
158  | 0  |       poly_key);  | 
159  | 0  |   }  | 
160  | 0  |   r = 0;  | 
161  | 0  |  out:  | 
162  | 0  |   explicit_bzero(expected_tag, sizeof(expected_tag));  | 
163  | 0  |   explicit_bzero(seqbuf, sizeof(seqbuf));  | 
164  | 0  |   explicit_bzero(poly_key, sizeof(poly_key));  | 
165  | 0  |   return r;  | 
166  | 0  | }  | 
167  |  |  | 
168  |  | /* Decrypt and extract the encrypted packet length */  | 
169  |  | int  | 
170  |  | chachapoly_get_length(struct chachapoly_ctx *ctx,  | 
171  |  |     u_int *plenp, u_int seqnr, const u_char *cp, u_int len)  | 
172  | 0  | { | 
173  | 0  |   u_char buf[4], seqbuf[16];  | 
174  |  | 
  | 
175  | 0  |   if (len < 4)  | 
176  | 0  |     return SSH_ERR_MESSAGE_INCOMPLETE;  | 
177  | 0  |   memset(seqbuf, 0, sizeof(seqbuf));  | 
178  | 0  |   POKE_U64(seqbuf + 8, seqnr);  | 
179  | 0  |   if (!EVP_CipherInit(ctx->header_evp, NULL, NULL, seqbuf, 0))  | 
180  | 0  |     return SSH_ERR_LIBCRYPTO_ERROR;  | 
181  | 0  |   if (EVP_Cipher(ctx->header_evp, buf, (u_char *)cp, sizeof(buf)) < 0)  | 
182  | 0  |     return SSH_ERR_LIBCRYPTO_ERROR;  | 
183  | 0  |   *plenp = PEEK_U32(buf);  | 
184  | 0  |   return 0;  | 
185  | 0  | }  | 
186  |  | #endif /* defined(HAVE_EVP_CHACHA20) && !defined(HAVE_BROKEN_CHACHA20) */  |