jcs's openbsd hax
openbsd
at jcs 489 lines 13 kB view raw
1/* $OpenBSD: cipher.c,v 1.126 2026/02/14 00:18:34 jsg 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 <sys/types.h> 39 40#include <string.h> 41#include <stdarg.h> 42#include <stdio.h> 43 44#include "cipher.h" 45#include "misc.h" 46#include "sshbuf.h" 47#include "ssherr.h" 48 49#ifndef WITH_OPENSSL 50#define EVP_CIPHER_CTX void 51#endif 52 53struct sshcipher_ctx { 54 int plaintext; 55 int encrypt; 56 EVP_CIPHER_CTX *evp; 57 struct chachapoly_ctx *cp_ctx; 58 struct aesctr_ctx ac_ctx; /* XXX union with evp? */ 59 const struct sshcipher *cipher; 60}; 61 62struct sshcipher { 63 char *name; 64 u_int block_size; 65 u_int key_len; 66 u_int iv_len; /* defaults to block_size */ 67 u_int auth_len; 68 u_int flags; 69#define CFLAG_CBC (1<<0) 70#define CFLAG_CHACHAPOLY (1<<1) 71#define CFLAG_AESCTR (1<<2) 72#define CFLAG_NONE (1<<3) 73#define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */ 74#ifdef WITH_OPENSSL 75 const EVP_CIPHER *(*evptype)(void); 76#else 77 void *ignored; 78#endif 79}; 80 81static const struct sshcipher ciphers[] = { 82#ifdef WITH_OPENSSL 83 { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc }, 84 { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc }, 85 { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc }, 86 { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc }, 87 { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr }, 88 { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr }, 89 { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr }, 90 { "aes128-gcm@openssh.com", 91 16, 16, 12, 16, 0, EVP_aes_128_gcm }, 92 { "aes256-gcm@openssh.com", 93 16, 32, 12, 16, 0, EVP_aes_256_gcm }, 94#else 95 { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL }, 96 { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL }, 97 { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL }, 98#endif 99 { "chacha20-poly1305@openssh.com", 100 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL }, 101 { "none", 8, 0, 0, 0, CFLAG_NONE, NULL }, 102 103 { NULL, 0, 0, 0, 0, 0, NULL } 104}; 105 106/*--*/ 107 108/* Returns a comma-separated list of supported ciphers. */ 109char * 110cipher_alg_list(char sep, int auth_only) 111{ 112 char *ret = NULL; 113 const struct sshcipher *c; 114 char sep_str[2] = {sep, '\0'}; 115 116 for (c = ciphers; c->name != NULL; c++) { 117 if ((c->flags & CFLAG_INTERNAL) != 0) 118 continue; 119 if (auth_only && c->auth_len == 0) 120 continue; 121 xextendf(&ret, sep_str, "%s", c->name); 122 } 123 return ret; 124} 125 126const char * 127compression_alg_list(int compression) 128{ 129#ifdef WITH_ZLIB 130 return compression ? "zlib@openssh.com,none" : 131 "none,zlib@openssh.com"; 132#else 133 return "none"; 134#endif 135} 136 137u_int 138cipher_blocksize(const struct sshcipher *c) 139{ 140 return (c->block_size); 141} 142 143u_int 144cipher_keylen(const struct sshcipher *c) 145{ 146 return (c->key_len); 147} 148 149u_int 150cipher_seclen(const struct sshcipher *c) 151{ 152 if (strcmp("3des-cbc", c->name) == 0) 153 return 14; 154 return cipher_keylen(c); 155} 156 157u_int 158cipher_authlen(const struct sshcipher *c) 159{ 160 return (c->auth_len); 161} 162 163u_int 164cipher_ivlen(const struct sshcipher *c) 165{ 166 /* 167 * Default is cipher block size, except for chacha20+poly1305 that 168 * needs no IV. XXX make iv_len == -1 default? 169 */ 170 return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ? 171 c->iv_len : c->block_size; 172} 173 174u_int 175cipher_is_cbc(const struct sshcipher *c) 176{ 177 return (c->flags & CFLAG_CBC) != 0; 178} 179 180u_int 181cipher_ctx_is_plaintext(struct sshcipher_ctx *cc) 182{ 183 return cc->plaintext; 184} 185 186const struct sshcipher * 187cipher_by_name(const char *name) 188{ 189 const struct sshcipher *c; 190 for (c = ciphers; c->name != NULL; c++) 191 if (strcmp(c->name, name) == 0) 192 return c; 193 return NULL; 194} 195 196#define CIPHER_SEP "," 197int 198ciphers_valid(const char *names) 199{ 200 const struct sshcipher *c; 201 char *cipher_list, *cp; 202 char *p; 203 204 if (names == NULL || strcmp(names, "") == 0) 205 return 0; 206 if ((cipher_list = cp = strdup(names)) == NULL) 207 return 0; 208 for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; 209 (p = strsep(&cp, CIPHER_SEP))) { 210 c = cipher_by_name(p); 211 if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) { 212 free(cipher_list); 213 return 0; 214 } 215 } 216 free(cipher_list); 217 return 1; 218} 219 220const char * 221cipher_warning_message(const struct sshcipher_ctx *cc) 222{ 223 if (cc == NULL || cc->cipher == NULL) 224 return NULL; 225 /* XXX repurpose for CBC warning */ 226 return NULL; 227} 228 229int 230cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher, 231 const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, 232 int do_encrypt) 233{ 234 struct sshcipher_ctx *cc = NULL; 235 int ret = SSH_ERR_INTERNAL_ERROR; 236#ifdef WITH_OPENSSL 237 const EVP_CIPHER *type; 238 int klen; 239#endif 240 241 *ccp = NULL; 242 if ((cc = calloc(1, sizeof(*cc))) == NULL) 243 return SSH_ERR_ALLOC_FAIL; 244 245 cc->plaintext = (cipher->flags & CFLAG_NONE) != 0; 246 cc->encrypt = do_encrypt; 247 248 if (keylen < cipher->key_len || 249 (iv != NULL && ivlen < cipher_ivlen(cipher))) { 250 ret = SSH_ERR_INVALID_ARGUMENT; 251 goto out; 252 } 253 254 cc->cipher = cipher; 255 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 256 cc->cp_ctx = chachapoly_new(key, keylen); 257 ret = cc->cp_ctx != NULL ? 0 : SSH_ERR_INVALID_ARGUMENT; 258 goto out; 259 } 260 if ((cc->cipher->flags & CFLAG_NONE) != 0) { 261 ret = 0; 262 goto out; 263 } 264#ifndef WITH_OPENSSL 265 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 266 aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen); 267 aesctr_ivsetup(&cc->ac_ctx, iv); 268 ret = 0; 269 goto out; 270 } 271 ret = SSH_ERR_INVALID_ARGUMENT; 272 goto out; 273#else /* WITH_OPENSSL */ 274 type = (*cipher->evptype)(); 275 if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) { 276 ret = SSH_ERR_ALLOC_FAIL; 277 goto out; 278 } 279 if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv, 280 (do_encrypt == CIPHER_ENCRYPT)) == 0) { 281 ret = SSH_ERR_LIBCRYPTO_ERROR; 282 goto out; 283 } 284 if (cipher_authlen(cipher) && 285 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED, 286 -1, (u_char *)iv) <= 0) { 287 ret = SSH_ERR_LIBCRYPTO_ERROR; 288 goto out; 289 } 290 klen = EVP_CIPHER_CTX_key_length(cc->evp); 291 if (klen > 0 && keylen != (u_int)klen) { 292 if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) { 293 ret = SSH_ERR_LIBCRYPTO_ERROR; 294 goto out; 295 } 296 } 297 if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) { 298 ret = SSH_ERR_LIBCRYPTO_ERROR; 299 goto out; 300 } 301 ret = 0; 302#endif /* WITH_OPENSSL */ 303 out: 304 if (ret == 0) { 305 /* success */ 306 *ccp = cc; 307 } else { 308 if (cc != NULL) { 309#ifdef WITH_OPENSSL 310 EVP_CIPHER_CTX_free(cc->evp); 311#endif /* WITH_OPENSSL */ 312 freezero(cc, sizeof(*cc)); 313 } 314 } 315 return ret; 316} 317 318/* 319 * cipher_crypt() operates as following: 320 * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'. 321 * These bytes are treated as additional authenticated data for 322 * authenticated encryption modes. 323 * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. 324 * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag. 325 * This tag is written on encryption and verified on decryption. 326 * Both 'aadlen' and 'authlen' can be set to 0. 327 */ 328int 329cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest, 330 const u_char *src, u_int len, u_int aadlen, u_int authlen) 331{ 332 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 333 return chachapoly_crypt(cc->cp_ctx, seqnr, dest, src, 334 len, aadlen, authlen, cc->encrypt); 335 } 336 if ((cc->cipher->flags & CFLAG_NONE) != 0) { 337 memcpy(dest, src, aadlen + len); 338 return 0; 339 } 340#ifndef WITH_OPENSSL 341 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 342 if (aadlen) 343 memcpy(dest, src, aadlen); 344 aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen, 345 dest + aadlen, len); 346 return 0; 347 } 348 return SSH_ERR_INVALID_ARGUMENT; 349#else 350 if (authlen) { 351 u_char lastiv[1]; 352 353 if (authlen != cipher_authlen(cc->cipher)) 354 return SSH_ERR_INVALID_ARGUMENT; 355 /* increment IV */ 356 if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, 357 1, lastiv) <= 0) 358 return SSH_ERR_LIBCRYPTO_ERROR; 359 /* set tag on decryption */ 360 if (!cc->encrypt && 361 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG, 362 authlen, (u_char *)src + aadlen + len) <= 0) 363 return SSH_ERR_LIBCRYPTO_ERROR; 364 } 365 if (aadlen) { 366 if (authlen && 367 EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0) 368 return SSH_ERR_LIBCRYPTO_ERROR; 369 memcpy(dest, src, aadlen); 370 } 371 if (len % cc->cipher->block_size) 372 return SSH_ERR_INVALID_ARGUMENT; 373 if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen, 374 len) < 0) 375 return SSH_ERR_LIBCRYPTO_ERROR; 376 if (authlen) { 377 /* compute tag (on encrypt) or verify tag (on decrypt) */ 378 if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0) 379 return cc->encrypt ? 380 SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID; 381 if (cc->encrypt && 382 EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG, 383 authlen, dest + aadlen + len) <= 0) 384 return SSH_ERR_LIBCRYPTO_ERROR; 385 } 386 return 0; 387#endif 388} 389 390/* Extract the packet length, including any decryption necessary beforehand */ 391int 392cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr, 393 const u_char *cp, u_int len) 394{ 395 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 396 return chachapoly_get_length(cc->cp_ctx, plenp, seqnr, 397 cp, len); 398 if (len < 4) 399 return SSH_ERR_MESSAGE_INCOMPLETE; 400 *plenp = PEEK_U32(cp); 401 return 0; 402} 403 404void 405cipher_free(struct sshcipher_ctx *cc) 406{ 407 if (cc == NULL) 408 return; 409 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 410 chachapoly_free(cc->cp_ctx); 411 cc->cp_ctx = NULL; 412 } else if ((cc->cipher->flags & CFLAG_AESCTR) != 0) 413 explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx)); 414#ifdef WITH_OPENSSL 415 EVP_CIPHER_CTX_free(cc->evp); 416 cc->evp = NULL; 417#endif 418 freezero(cc, sizeof(*cc)); 419} 420 421int 422cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, size_t len) 423{ 424#ifdef WITH_OPENSSL 425 const struct sshcipher *c = cc->cipher; 426 int evplen; 427#endif 428 429 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { 430 if (len != 0) 431 return SSH_ERR_INVALID_ARGUMENT; 432 return 0; 433 } 434 if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { 435 if (len != sizeof(cc->ac_ctx.ctr)) 436 return SSH_ERR_INVALID_ARGUMENT; 437 memcpy(iv, cc->ac_ctx.ctr, len); 438 return 0; 439 } 440 if ((cc->cipher->flags & CFLAG_NONE) != 0) 441 return 0; 442 443#ifdef WITH_OPENSSL 444 evplen = EVP_CIPHER_CTX_iv_length(cc->evp); 445 if (evplen == 0) 446 return 0; 447 else if (evplen < 0) 448 return SSH_ERR_LIBCRYPTO_ERROR; 449 if ((size_t)evplen != len) 450 return SSH_ERR_INVALID_ARGUMENT; 451 if (cipher_authlen(c)) { 452 if (EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, len, 453 iv) <= 0) 454 return SSH_ERR_LIBCRYPTO_ERROR; 455 } else if (EVP_CIPHER_CTX_get_iv(cc->evp, iv, len) <= 0) 456 return SSH_ERR_LIBCRYPTO_ERROR; 457#endif 458 return 0; 459} 460 461int 462cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv, size_t len) 463{ 464#ifdef WITH_OPENSSL 465 const struct sshcipher *c = cc->cipher; 466 int evplen = 0; 467#endif 468 469 if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) 470 return 0; 471 if ((cc->cipher->flags & CFLAG_NONE) != 0) 472 return 0; 473 474#ifdef WITH_OPENSSL 475 evplen = EVP_CIPHER_CTX_iv_length(cc->evp); 476 if (evplen <= 0) 477 return SSH_ERR_LIBCRYPTO_ERROR; 478 if ((size_t)evplen != len) 479 return SSH_ERR_INVALID_ARGUMENT; 480 if (cipher_authlen(c)) { 481 /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */ 482 if (EVP_CIPHER_CTX_ctrl(cc->evp, 483 EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv) <= 0) 484 return SSH_ERR_LIBCRYPTO_ERROR; 485 } else if (!EVP_CIPHER_CTX_set_iv(cc->evp, iv, evplen)) 486 return SSH_ERR_LIBCRYPTO_ERROR; 487#endif 488 return 0; 489}