1 /* $OpenBSD: cipher.c,v 1.129 2026/06/19 05:26:04 djm Exp $ */
2 /*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 *
7 * As far as I am concerned, the code I have written for this software
8 * can be used freely for any purpose. Any derived versions of this
9 * software must be clearly marked as such, and if the derived work is
10 * incompatible with the protocol description in the RFC file, it must be
11 * called by a name other than "ssh" or "Secure Shell".
12 *
13 *
14 * Copyright (c) 1999 Niels Provos. All rights reserved.
15 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38 #include "includes.h"
39
40 #include <sys/types.h>
41
42 #include <string.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45
46 #include "cipher.h"
47 #include "misc.h"
48 #include "sshbuf.h"
49 #include "ssherr.h"
50
51 #include "openbsd-compat/openssl-compat.h"
52
53 #ifndef WITH_OPENSSL
54 #define EVP_CIPHER_CTX void
55 #endif
56
57 struct sshcipher_ctx {
58 int plaintext;
59 int encrypt;
60 EVP_CIPHER_CTX *evp;
61 struct chachapoly_ctx *cp_ctx;
62 struct aesctr_ctx ac_ctx; /* XXX union with evp? */
63 const struct sshcipher *cipher;
64 };
65
66 struct sshcipher {
67 char *name;
68 u_int block_size;
69 u_int key_len;
70 u_int iv_len; /* defaults to block_size */
71 u_int auth_len;
72 u_int flags;
73 #define CFLAG_CBC (1<<0)
74 #define CFLAG_CHACHAPOLY (1<<1)
75 #define CFLAG_AESCTR (1<<2)
76 #define CFLAG_NONE (1<<3)
77 #define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
78 #ifdef WITH_OPENSSL
79 const EVP_CIPHER *(*evptype)(void);
80 #else
81 void *ignored;
82 #endif
83 };
84
85 static const struct sshcipher ciphers[] = {
86 #ifdef WITH_OPENSSL
87 #ifndef OPENSSL_NO_DES
88 { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc },
89 #endif
90 { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc },
91 { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc },
92 { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc },
93 { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr },
94 { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr },
95 { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr },
96 { "aes128-gcm@openssh.com",
97 16, 16, 12, 16, 0, EVP_aes_128_gcm },
98 { "aes256-gcm@openssh.com",
99 16, 32, 12, 16, 0, EVP_aes_256_gcm },
100 #else
101 { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL },
102 { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL },
103 { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL },
104 #endif
105 { "chacha20-poly1305@openssh.com",
106 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL },
107 { "none", 8, 0, 0, 0, CFLAG_NONE, NULL },
108
109 { NULL, 0, 0, 0, 0, 0, NULL }
110 };
111
112 /*--*/
113
114 /* Returns a comma-separated list of supported ciphers. */
115 char *
cipher_alg_list(char sep,int auth_only)116 cipher_alg_list(char sep, int auth_only)
117 {
118 char *ret = NULL;
119 const struct sshcipher *c;
120 char sep_str[2] = {sep, '\0'};
121
122 for (c = ciphers; c->name != NULL; c++) {
123 if ((c->flags & CFLAG_INTERNAL) != 0)
124 continue;
125 if (auth_only && c->auth_len == 0)
126 continue;
127 xextendf(&ret, sep_str, "%s", c->name);
128 }
129 return ret;
130 }
131
132 const char *
compression_alg_list(int compression)133 compression_alg_list(int compression)
134 {
135 #ifdef WITH_ZLIB
136 return compression ? "zlib@openssh.com,none" :
137 "none,zlib@openssh.com";
138 #else
139 return "none";
140 #endif
141 }
142
143 u_int
cipher_blocksize(const struct sshcipher * c)144 cipher_blocksize(const struct sshcipher *c)
145 {
146 return (c->block_size);
147 }
148
149 u_int
cipher_keylen(const struct sshcipher * c)150 cipher_keylen(const struct sshcipher *c)
151 {
152 return (c->key_len);
153 }
154
155 u_int
cipher_seclen(const struct sshcipher * c)156 cipher_seclen(const struct sshcipher *c)
157 {
158 if (strcmp("3des-cbc", c->name) == 0)
159 return 14;
160 return cipher_keylen(c);
161 }
162
163 u_int
cipher_authlen(const struct sshcipher * c)164 cipher_authlen(const struct sshcipher *c)
165 {
166 return (c->auth_len);
167 }
168
169 u_int
cipher_ivlen(const struct sshcipher * c)170 cipher_ivlen(const struct sshcipher *c)
171 {
172 /*
173 * Default is cipher block size, except for chacha20+poly1305 that
174 * needs no IV. XXX make iv_len == -1 default?
175 */
176 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ?
177 c->iv_len : c->block_size;
178 }
179
180 u_int
cipher_is_cbc(const struct sshcipher * c)181 cipher_is_cbc(const struct sshcipher *c)
182 {
183 return (c->flags & CFLAG_CBC) != 0;
184 }
185
186 u_int
cipher_is_internal(const struct sshcipher * c)187 cipher_is_internal(const struct sshcipher *c)
188 {
189 return (c->flags & CFLAG_INTERNAL) != 0;
190 }
191
192 u_int
cipher_ctx_is_plaintext(struct sshcipher_ctx * cc)193 cipher_ctx_is_plaintext(struct sshcipher_ctx *cc)
194 {
195 return cc->plaintext;
196 }
197
198 const struct sshcipher *
cipher_by_name(const char * name)199 cipher_by_name(const char *name)
200 {
201 const struct sshcipher *c;
202 for (c = ciphers; c->name != NULL; c++)
203 if (strcmp(c->name, name) == 0)
204 return c;
205 return NULL;
206 }
207
208 #define CIPHER_SEP ","
209 int
ciphers_valid(const char * names)210 ciphers_valid(const char *names)
211 {
212 const struct sshcipher *c;
213 char *cipher_list, *cp;
214 char *p;
215 int found = 0;
216
217 if (names == NULL || strcmp(names, "") == 0)
218 return 0;
219 if ((cipher_list = cp = strdup(names)) == NULL)
220 return 0;
221 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
222 (p = strsep(&cp, CIPHER_SEP))) {
223 c = cipher_by_name(p);
224 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) {
225 free(cipher_list);
226 return 0;
227 } else
228 found = 1;
229 }
230 free(cipher_list);
231 return found;
232 }
233
234 const char *
cipher_warning_message(const struct sshcipher_ctx * cc)235 cipher_warning_message(const struct sshcipher_ctx *cc)
236 {
237 if (cc == NULL || cc->cipher == NULL)
238 return NULL;
239 /* XXX repurpose for CBC warning */
240 return NULL;
241 }
242
243 int
cipher_init(struct sshcipher_ctx ** ccp,const struct sshcipher * cipher,const u_char * key,u_int keylen,const u_char * iv,u_int ivlen,int do_encrypt)244 cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher,
245 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
246 int do_encrypt)
247 {
248 struct sshcipher_ctx *cc = NULL;
249 int ret = SSH_ERR_INTERNAL_ERROR;
250 #ifdef WITH_OPENSSL
251 const EVP_CIPHER *type;
252 int klen;
253 #endif
254
255 *ccp = NULL;
256 if ((cc = calloc(1, sizeof(*cc))) == NULL)
257 return SSH_ERR_ALLOC_FAIL;
258
259 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0;
260 cc->encrypt = do_encrypt;
261
262 if (keylen < cipher->key_len ||
263 (iv != NULL && ivlen < cipher_ivlen(cipher))) {
264 ret = SSH_ERR_INVALID_ARGUMENT;
265 goto out;
266 }
267
268 cc->cipher = cipher;
269 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
270 cc->cp_ctx = chachapoly_new(key, keylen);
271 ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT;
272 goto out;
273 }
274 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
275 ret = 0;
276 goto out;
277 }
278 #ifndef WITH_OPENSSL
279 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
280 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen);
281 aesctr_ivsetup(&cc->ac_ctx, iv);
282 ret = 0;
283 goto out;
284 }
285 ret = SSH_ERR_INVALID_ARGUMENT;
286 goto out;
287 #else /* WITH_OPENSSL */
288 type = (*cipher->evptype)();
289 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) {
290 ret = SSH_ERR_ALLOC_FAIL;
291 goto out;
292 }
293 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv,
294 (do_encrypt == CIPHER_ENCRYPT)) == 0) {
295 ret = SSH_ERR_LIBCRYPTO_ERROR;
296 goto out;
297 }
298 if (cipher_authlen(cipher) &&
299 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
300 -1, (u_char *)iv) <= 0) {
301 ret = SSH_ERR_LIBCRYPTO_ERROR;
302 goto out;
303 }
304 klen = EVP_CIPHER_CTX_key_length(cc->evp);
305 if (klen > 0 && keylen != (u_int)klen) {
306 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) {
307 ret = SSH_ERR_LIBCRYPTO_ERROR;
308 goto out;
309 }
310 }
311 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) {
312 ret = SSH_ERR_LIBCRYPTO_ERROR;
313 goto out;
314 }
315 ret = 0;
316 #endif /* WITH_OPENSSL */
317 out:
318 if (ret == 0) {
319 /* success */
320 *ccp = cc;
321 } else {
322 if (cc != NULL) {
323 #ifdef WITH_OPENSSL
324 EVP_CIPHER_CTX_free(cc->evp);
325 #endif /* WITH_OPENSSL */
326 freezero(cc, sizeof(*cc));
327 }
328 }
329 return ret;
330 }
331
332 /*
333 * cipher_crypt() operates as following:
334 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
335 * These bytes are treated as additional authenticated data for
336 * authenticated encryption modes.
337 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
338 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
339 * This tag is written on encryption and verified on decryption.
340 * Both 'aadlen' and 'authlen' can be set to 0.
341 */
342 int
cipher_crypt(struct sshcipher_ctx * cc,u_int seqnr,u_char * dest,const u_char * src,u_int len,u_int aadlen,u_int authlen)343 cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest,
344 const u_char *src, u_int len, u_int aadlen, u_int authlen)
345 {
346 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
347 return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src,
348 len, aadlen, authlen, cc->encrypt);
349 }
350 if ((cc->cipher->flags & CFLAG_NONE) != 0) {
351 memcpy(dest, src, aadlen + len);
352 return 0;
353 }
354 #ifndef WITH_OPENSSL
355 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
356 if (aadlen)
357 memcpy(dest, src, aadlen);
358 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen,
359 dest + aadlen, len);
360 return 0;
361 }
362 return SSH_ERR_INVALID_ARGUMENT;
363 #else
364 if (authlen) {
365 u_char lastiv[1];
366
367 if (authlen != cipher_authlen(cc->cipher))
368 return SSH_ERR_INVALID_ARGUMENT;
369 /* increment IV */
370 if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN,
371 1, lastiv) <= 0)
372 return SSH_ERR_LIBCRYPTO_ERROR;
373 /* set tag on decryption */
374 if (!cc->encrypt &&
375 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG,
376 authlen, (u_char *)src + aadlen + len) <= 0)
377 return SSH_ERR_LIBCRYPTO_ERROR;
378 }
379 if (aadlen) {
380 if (authlen &&
381 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0)
382 return SSH_ERR_LIBCRYPTO_ERROR;
383 memcpy(dest, src, aadlen);
384 }
385 if (len % cc->cipher->block_size)
386 return SSH_ERR_INVALID_ARGUMENT;
387 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen,
388 len) < 0)
389 return SSH_ERR_LIBCRYPTO_ERROR;
390 if (authlen) {
391 /* compute tag (on encrypt) or verify tag (on decrypt) */
392 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0)
393 return cc->encrypt ?
394 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID;
395 if (cc->encrypt &&
396 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG,
397 authlen, dest + aadlen + len) <= 0)
398 return SSH_ERR_LIBCRYPTO_ERROR;
399 }
400 return 0;
401 #endif
402 }
403
404 /* Extract the packet length, including any decryption necessary beforehand */
405 int
cipher_get_length(struct sshcipher_ctx * cc,u_int * plenp,u_int seqnr,const u_char * cp,u_int len)406 cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr,
407 const u_char *cp, u_int len)
408 {
409 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0)
410 return chachapoly_get_length(cc->cp_ctx, plenp, seqnr,
411 cp, len);
412 if (len < 4)
413 return SSH_ERR_MESSAGE_INCOMPLETE;
414 *plenp = PEEK_U32(cp);
415 return 0;
416 }
417
418 void
cipher_free(struct sshcipher_ctx * cc)419 cipher_free(struct sshcipher_ctx *cc)
420 {
421 if (cc == NULL)
422 return;
423 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
424 chachapoly_free(cc->cp_ctx);
425 cc->cp_ctx = NULL;
426 } else if ((cc->cipher->flags & CFLAG_AESCTR) != 0)
427 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx));
428 #ifdef WITH_OPENSSL
429 EVP_CIPHER_CTX_free(cc->evp);
430 cc->evp = NULL;
431 #endif
432 freezero(cc, sizeof(*cc));
433 }
434
435 int
cipher_get_keyiv(struct sshcipher_ctx * cc,u_char * iv,size_t len)436 cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len)
437 {
438 #ifdef WITH_OPENSSL
439 const struct sshcipher *c = cc->cipher;
440 int evplen;
441 #endif
442
443 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) {
444 if (len != 0)
445 return SSH_ERR_INVALID_ARGUMENT;
446 return 0;
447 }
448 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) {
449 if (len != sizeof(cc->ac_ctx.ctr))
450 return SSH_ERR_INVALID_ARGUMENT;
451 memcpy(iv, cc->ac_ctx.ctr, len);
452 return 0;
453 }
454 if ((cc->cipher->flags & CFLAG_NONE) != 0)
455 return 0;
456
457 #ifdef WITH_OPENSSL
458 evplen = EVP_CIPHER_CTX_iv_length(cc->evp);
459 if (evplen == 0)
460 return 0;
461 else if (evplen < 0)
462 return SSH_ERR_LIBCRYPTO_ERROR;
463 if ((size_t)evplen != len)
464 return SSH_ERR_INVALID_ARGUMENT;
465 if (cipher_authlen(c)) {
466 if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, len,
467 iv) <= 0)
468 return SSH_ERR_LIBCRYPTO_ERROR;
469 } else if (EVP_CIPHER_CTX_get_iv(cc->evp, iv, len) <= 0)
470 return SSH_ERR_LIBCRYPTO_ERROR;
471 #endif
472 return 0;
473 }
474