1 /* $OpenBSD: cipher.c,v 1.123 2024/08/23 04:51:00 deraadt Exp $ */
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
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".
14 * Copyright (c) 1999 Niels Provos. All rights reserved.
15 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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.
40 #include <sys/types.h>
52 #include "openbsd-compat/openssl-compat.h"
55 #define EVP_CIPHER_CTX void
58 struct sshcipher_ctx
{
62 struct chachapoly_ctx
*cp_ctx
;
63 struct aesctr_ctx ac_ctx
; /* XXX union with evp? */
64 const struct sshcipher
*cipher
;
71 u_int iv_len
; /* defaults to block_size */
74 #define CFLAG_CBC (1<<0)
75 #define CFLAG_CHACHAPOLY (1<<1)
76 #define CFLAG_AESCTR (1<<2)
77 #define CFLAG_NONE (1<<3)
78 #define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */
80 const EVP_CIPHER
*(*evptype
)(void);
86 static const struct sshcipher ciphers
[] = {
88 #ifndef OPENSSL_NO_DES
89 { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC
, EVP_des_ede3_cbc
},
91 { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC
, EVP_aes_128_cbc
},
92 { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC
, EVP_aes_192_cbc
},
93 { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC
, EVP_aes_256_cbc
},
94 { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr
},
95 { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr
},
96 { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr
},
97 { "aes128-gcm@openssh.com",
98 16, 16, 12, 16, 0, EVP_aes_128_gcm
},
99 { "aes256-gcm@openssh.com",
100 16, 32, 12, 16, 0, EVP_aes_256_gcm
},
102 { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR
, NULL
},
103 { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR
, NULL
},
104 { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR
, NULL
},
106 { "chacha20-poly1305@openssh.com",
107 8, 64, 0, 16, CFLAG_CHACHAPOLY
, NULL
},
108 { "none", 8, 0, 0, 0, CFLAG_NONE
, NULL
},
110 { NULL
, 0, 0, 0, 0, 0, NULL
}
115 /* Returns a comma-separated list of supported ciphers. */
117 cipher_alg_list(char sep
, int auth_only
)
119 char *tmp
, *ret
= NULL
;
120 size_t nlen
, rlen
= 0;
121 const struct sshcipher
*c
;
123 for (c
= ciphers
; c
->name
!= NULL
; c
++) {
124 if ((c
->flags
& CFLAG_INTERNAL
) != 0)
126 if (auth_only
&& c
->auth_len
== 0)
130 nlen
= strlen(c
->name
);
131 if ((tmp
= realloc(ret
, rlen
+ nlen
+ 2)) == NULL
) {
136 memcpy(ret
+ rlen
, c
->name
, nlen
+ 1);
143 compression_alg_list(int compression
)
146 return compression
? "zlib@openssh.com,none" :
147 "none,zlib@openssh.com";
154 cipher_blocksize(const struct sshcipher
*c
)
156 return (c
->block_size
);
160 cipher_keylen(const struct sshcipher
*c
)
166 cipher_seclen(const struct sshcipher
*c
)
168 if (strcmp("3des-cbc", c
->name
) == 0)
170 return cipher_keylen(c
);
174 cipher_authlen(const struct sshcipher
*c
)
176 return (c
->auth_len
);
180 cipher_ivlen(const struct sshcipher
*c
)
183 * Default is cipher block size, except for chacha20+poly1305 that
184 * needs no IV. XXX make iv_len == -1 default?
186 return (c
->iv_len
!= 0 || (c
->flags
& CFLAG_CHACHAPOLY
) != 0) ?
187 c
->iv_len
: c
->block_size
;
191 cipher_is_cbc(const struct sshcipher
*c
)
193 return (c
->flags
& CFLAG_CBC
) != 0;
197 cipher_ctx_is_plaintext(struct sshcipher_ctx
*cc
)
199 return cc
->plaintext
;
202 const struct sshcipher
*
203 cipher_by_name(const char *name
)
205 const struct sshcipher
*c
;
206 for (c
= ciphers
; c
->name
!= NULL
; c
++)
207 if (strcmp(c
->name
, name
) == 0)
212 #define CIPHER_SEP ","
214 ciphers_valid(const char *names
)
216 const struct sshcipher
*c
;
217 char *cipher_list
, *cp
;
220 if (names
== NULL
|| strcmp(names
, "") == 0)
222 if ((cipher_list
= cp
= strdup(names
)) == NULL
)
224 for ((p
= strsep(&cp
, CIPHER_SEP
)); p
&& *p
!= '\0';
225 (p
= strsep(&cp
, CIPHER_SEP
))) {
226 c
= cipher_by_name(p
);
227 if (c
== NULL
|| (c
->flags
& CFLAG_INTERNAL
) != 0) {
237 cipher_warning_message(const struct sshcipher_ctx
*cc
)
239 if (cc
== NULL
|| cc
->cipher
== NULL
)
241 /* XXX repurpose for CBC warning */
246 cipher_init(struct sshcipher_ctx
**ccp
, const struct sshcipher
*cipher
,
247 const u_char
*key
, u_int keylen
, const u_char
*iv
, u_int ivlen
,
250 struct sshcipher_ctx
*cc
= NULL
;
251 int ret
= SSH_ERR_INTERNAL_ERROR
;
253 const EVP_CIPHER
*type
;
258 if ((cc
= calloc(1, sizeof(*cc
))) == NULL
)
259 return SSH_ERR_ALLOC_FAIL
;
261 cc
->plaintext
= (cipher
->flags
& CFLAG_NONE
) != 0;
262 cc
->encrypt
= do_encrypt
;
264 if (keylen
< cipher
->key_len
||
265 (iv
!= NULL
&& ivlen
< cipher_ivlen(cipher
))) {
266 ret
= SSH_ERR_INVALID_ARGUMENT
;
271 if ((cc
->cipher
->flags
& CFLAG_CHACHAPOLY
) != 0) {
272 cc
->cp_ctx
= chachapoly_new(key
, keylen
);
273 ret
= cc
->cp_ctx
!= NULL
? 0 : SSH_ERR_INVALID_ARGUMENT
;
276 if ((cc
->cipher
->flags
& CFLAG_NONE
) != 0) {
281 if ((cc
->cipher
->flags
& CFLAG_AESCTR
) != 0) {
282 aesctr_keysetup(&cc
->ac_ctx
, key
, 8 * keylen
, 8 * ivlen
);
283 aesctr_ivsetup(&cc
->ac_ctx
, iv
);
287 ret
= SSH_ERR_INVALID_ARGUMENT
;
289 #else /* WITH_OPENSSL */
290 type
= (*cipher
->evptype
)();
291 if ((cc
->evp
= EVP_CIPHER_CTX_new()) == NULL
) {
292 ret
= SSH_ERR_ALLOC_FAIL
;
295 if (EVP_CipherInit(cc
->evp
, type
, NULL
, (u_char
*)iv
,
296 (do_encrypt
== CIPHER_ENCRYPT
)) == 0) {
297 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
300 if (cipher_authlen(cipher
) &&
301 !EVP_CIPHER_CTX_ctrl(cc
->evp
, EVP_CTRL_GCM_SET_IV_FIXED
,
303 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
306 klen
= EVP_CIPHER_CTX_key_length(cc
->evp
);
307 if (klen
> 0 && keylen
!= (u_int
)klen
) {
308 if (EVP_CIPHER_CTX_set_key_length(cc
->evp
, keylen
) == 0) {
309 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
313 if (EVP_CipherInit(cc
->evp
, NULL
, (u_char
*)key
, NULL
, -1) == 0) {
314 ret
= SSH_ERR_LIBCRYPTO_ERROR
;
318 #endif /* WITH_OPENSSL */
326 EVP_CIPHER_CTX_free(cc
->evp
);
327 #endif /* WITH_OPENSSL */
328 freezero(cc
, sizeof(*cc
));
335 * cipher_crypt() operates as following:
336 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
337 * These bytes are treated as additional authenticated data for
338 * authenticated encryption modes.
339 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
340 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
341 * This tag is written on encryption and verified on decryption.
342 * Both 'aadlen' and 'authlen' can be set to 0.
345 cipher_crypt(struct sshcipher_ctx
*cc
, u_int seqnr
, u_char
*dest
,
346 const u_char
*src
, u_int len
, u_int aadlen
, u_int authlen
)
348 if ((cc
->cipher
->flags
& CFLAG_CHACHAPOLY
) != 0) {
349 return chachapoly_crypt(cc
->cp_ctx
, seqnr
, dest
, src
,
350 len
, aadlen
, authlen
, cc
->encrypt
);
352 if ((cc
->cipher
->flags
& CFLAG_NONE
) != 0) {
353 memcpy(dest
, src
, aadlen
+ len
);
357 if ((cc
->cipher
->flags
& CFLAG_AESCTR
) != 0) {
359 memcpy(dest
, src
, aadlen
);
360 aesctr_encrypt_bytes(&cc
->ac_ctx
, src
+ aadlen
,
364 return SSH_ERR_INVALID_ARGUMENT
;
369 if (authlen
!= cipher_authlen(cc
->cipher
))
370 return SSH_ERR_INVALID_ARGUMENT
;
372 if (!EVP_CIPHER_CTX_ctrl(cc
->evp
, EVP_CTRL_GCM_IV_GEN
,
374 return SSH_ERR_LIBCRYPTO_ERROR
;
375 /* set tag on decryption */
377 !EVP_CIPHER_CTX_ctrl(cc
->evp
, EVP_CTRL_GCM_SET_TAG
,
378 authlen
, (u_char
*)src
+ aadlen
+ len
))
379 return SSH_ERR_LIBCRYPTO_ERROR
;
383 EVP_Cipher(cc
->evp
, NULL
, (u_char
*)src
, aadlen
) < 0)
384 return SSH_ERR_LIBCRYPTO_ERROR
;
385 memcpy(dest
, src
, aadlen
);
387 if (len
% cc
->cipher
->block_size
)
388 return SSH_ERR_INVALID_ARGUMENT
;
389 if (EVP_Cipher(cc
->evp
, dest
+ aadlen
, (u_char
*)src
+ aadlen
,
391 return SSH_ERR_LIBCRYPTO_ERROR
;
393 /* compute tag (on encrypt) or verify tag (on decrypt) */
394 if (EVP_Cipher(cc
->evp
, NULL
, NULL
, 0) < 0)
396 SSH_ERR_LIBCRYPTO_ERROR
: SSH_ERR_MAC_INVALID
;
398 !EVP_CIPHER_CTX_ctrl(cc
->evp
, EVP_CTRL_GCM_GET_TAG
,
399 authlen
, dest
+ aadlen
+ len
))
400 return SSH_ERR_LIBCRYPTO_ERROR
;
406 /* Extract the packet length, including any decryption necessary beforehand */
408 cipher_get_length(struct sshcipher_ctx
*cc
, u_int
*plenp
, u_int seqnr
,
409 const u_char
*cp
, u_int len
)
411 if ((cc
->cipher
->flags
& CFLAG_CHACHAPOLY
) != 0)
412 return chachapoly_get_length(cc
->cp_ctx
, plenp
, seqnr
,
415 return SSH_ERR_MESSAGE_INCOMPLETE
;
416 *plenp
= PEEK_U32(cp
);
421 cipher_free(struct sshcipher_ctx
*cc
)
425 if ((cc
->cipher
->flags
& CFLAG_CHACHAPOLY
) != 0) {
426 chachapoly_free(cc
->cp_ctx
);
428 } else if ((cc
->cipher
->flags
& CFLAG_AESCTR
) != 0)
429 explicit_bzero(&cc
->ac_ctx
, sizeof(cc
->ac_ctx
));
431 EVP_CIPHER_CTX_free(cc
->evp
);
434 freezero(cc
, sizeof(*cc
));
438 cipher_get_keyiv(struct sshcipher_ctx
*cc
, u_char
*iv
, size_t len
)
441 const struct sshcipher
*c
= cc
->cipher
;
445 if ((cc
->cipher
->flags
& CFLAG_CHACHAPOLY
) != 0) {
447 return SSH_ERR_INVALID_ARGUMENT
;
450 if ((cc
->cipher
->flags
& CFLAG_AESCTR
) != 0) {
451 if (len
!= sizeof(cc
->ac_ctx
.ctr
))
452 return SSH_ERR_INVALID_ARGUMENT
;
453 memcpy(iv
, cc
->ac_ctx
.ctr
, len
);
456 if ((cc
->cipher
->flags
& CFLAG_NONE
) != 0)
460 evplen
= EVP_CIPHER_CTX_iv_length(cc
->evp
);
464 return SSH_ERR_LIBCRYPTO_ERROR
;
465 if ((size_t)evplen
!= len
)
466 return SSH_ERR_INVALID_ARGUMENT
;
467 if (cipher_authlen(c
)) {
468 if (!EVP_CIPHER_CTX_ctrl(cc
->evp
, EVP_CTRL_GCM_IV_GEN
,
470 return SSH_ERR_LIBCRYPTO_ERROR
;
471 } else if (!EVP_CIPHER_CTX_get_iv(cc
->evp
, iv
, len
))
472 return SSH_ERR_LIBCRYPTO_ERROR
;
478 cipher_set_keyiv(struct sshcipher_ctx
*cc
, const u_char
*iv
, size_t len
)
481 const struct sshcipher
*c
= cc
->cipher
;
485 if ((cc
->cipher
->flags
& CFLAG_CHACHAPOLY
) != 0)
487 if ((cc
->cipher
->flags
& CFLAG_NONE
) != 0)
491 evplen
= EVP_CIPHER_CTX_iv_length(cc
->evp
);
493 return SSH_ERR_LIBCRYPTO_ERROR
;
494 if ((size_t)evplen
!= len
)
495 return SSH_ERR_INVALID_ARGUMENT
;
496 if (cipher_authlen(c
)) {
497 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */
498 if (!EVP_CIPHER_CTX_ctrl(cc
->evp
,
499 EVP_CTRL_GCM_SET_IV_FIXED
, -1, (void *)iv
))
500 return SSH_ERR_LIBCRYPTO_ERROR
;
501 } else if (!EVP_CIPHER_CTX_set_iv(cc
->evp
, iv
, evplen
))
502 return SSH_ERR_LIBCRYPTO_ERROR
;