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