Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 1 | /* $OpenBSD: cipher.c,v 1.102 2016/08/03 05:41:57 djm Exp $ */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 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 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 42 | #include <string.h> |
| 43 | #include <stdarg.h> |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 44 | #include <stdio.h> |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 45 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 46 | #include "cipher.h" |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 47 | #include "misc.h" |
| 48 | #include "sshbuf.h" |
| 49 | #include "ssherr.h" |
| 50 | #include "digest.h" |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 51 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 52 | #include "openbsd-compat/openssl-compat.h" |
| 53 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 54 | #ifdef WITH_SSH1 |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 55 | extern const EVP_CIPHER *evp_ssh1_bf(void); |
| 56 | extern const EVP_CIPHER *evp_ssh1_3des(void); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 57 | extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int); |
| 58 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 59 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 60 | struct sshcipher_ctx { |
| 61 | int plaintext; |
| 62 | int encrypt; |
| 63 | EVP_CIPHER_CTX *evp; |
| 64 | struct chachapoly_ctx cp_ctx; /* XXX union with evp? */ |
| 65 | struct aesctr_ctx ac_ctx; /* XXX union with evp? */ |
| 66 | const struct sshcipher *cipher; |
| 67 | }; |
| 68 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 69 | struct sshcipher { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 70 | char *name; |
| 71 | int number; /* for ssh1 only */ |
| 72 | u_int block_size; |
| 73 | u_int key_len; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 74 | u_int iv_len; /* defaults to block_size */ |
| 75 | u_int auth_len; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 76 | u_int discard_len; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 77 | u_int flags; |
| 78 | #define CFLAG_CBC (1<<0) |
| 79 | #define CFLAG_CHACHAPOLY (1<<1) |
| 80 | #define CFLAG_AESCTR (1<<2) |
| 81 | #define CFLAG_NONE (1<<3) |
| 82 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 83 | const EVP_CIPHER *(*evptype)(void); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 84 | #else |
| 85 | void *ignored; |
| 86 | #endif |
| 87 | }; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 88 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 89 | static const struct sshcipher ciphers[] = { |
| 90 | #ifdef WITH_SSH1 |
| 91 | { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc }, |
| 92 | { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des }, |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 93 | # ifndef OPENSSL_NO_BF |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 94 | { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf }, |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 95 | # endif /* OPENSSL_NO_BF */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 96 | #endif /* WITH_SSH1 */ |
| 97 | #ifdef WITH_OPENSSL |
| 98 | { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null }, |
| 99 | #if !defined(ANDROID) |
| 100 | { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc }, |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 101 | # endif /* ANDROID */ |
| 102 | # ifndef OPENSSL_NO_BF |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 103 | { "blowfish-cbc", |
| 104 | SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc }, |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 105 | # endif /* OPENSSL_NO_BF */ |
| 106 | # ifndef OPENSSL_NO_CAST |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 107 | { "cast128-cbc", |
| 108 | SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc }, |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 109 | # endif /* OPENSSL_NO_CAST */ |
| 110 | # ifndef OPENSSL_NO_RC4 |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 111 | { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 }, |
| 112 | { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 }, |
| 113 | { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 }, |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 114 | # endif /* OPENSSL_NO_RC4 */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 115 | { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc }, |
| 116 | { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc }, |
| 117 | { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 118 | { "rijndael-cbc@lysator.liu.se", |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 119 | SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, |
| 120 | { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr }, |
| 121 | #if !defined(ANDROID) |
| 122 | { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr }, |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 123 | #endif |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 124 | { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr }, |
| 125 | # ifdef OPENSSL_HAVE_EVPGCM |
| 126 | { "aes128-gcm@openssh.com", |
| 127 | SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm }, |
| 128 | { "aes256-gcm@openssh.com", |
| 129 | SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm }, |
| 130 | # endif /* OPENSSL_HAVE_EVPGCM */ |
| 131 | #else /* WITH_OPENSSL */ |
| 132 | { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL }, |
| 133 | { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL }, |
| 134 | { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL }, |
| 135 | { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL }, |
| 136 | #endif /* WITH_OPENSSL */ |
| 137 | { "chacha20-poly1305@openssh.com", |
| 138 | SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL }, |
| 139 | |
| 140 | { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 141 | }; |
| 142 | |
| 143 | /*--*/ |
| 144 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 145 | /* Returns a comma-separated list of supported ciphers. */ |
| 146 | char * |
| 147 | cipher_alg_list(char sep, int auth_only) |
| 148 | { |
| 149 | char *tmp, *ret = NULL; |
| 150 | size_t nlen, rlen = 0; |
| 151 | const struct sshcipher *c; |
| 152 | |
| 153 | for (c = ciphers; c->name != NULL; c++) { |
| 154 | if (c->number != SSH_CIPHER_SSH2) |
| 155 | continue; |
| 156 | if (auth_only && c->auth_len == 0) |
| 157 | continue; |
| 158 | if (ret != NULL) |
| 159 | ret[rlen++] = sep; |
| 160 | nlen = strlen(c->name); |
| 161 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
| 162 | free(ret); |
| 163 | return NULL; |
| 164 | } |
| 165 | ret = tmp; |
| 166 | memcpy(ret + rlen, c->name, nlen + 1); |
| 167 | rlen += nlen; |
| 168 | } |
| 169 | return ret; |
| 170 | } |
| 171 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 172 | u_int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 173 | cipher_blocksize(const struct sshcipher *c) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 174 | { |
| 175 | return (c->block_size); |
| 176 | } |
| 177 | |
| 178 | u_int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 179 | cipher_keylen(const struct sshcipher *c) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 180 | { |
| 181 | return (c->key_len); |
| 182 | } |
| 183 | |
| 184 | u_int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 185 | cipher_seclen(const struct sshcipher *c) |
| 186 | { |
| 187 | if (strcmp("3des-cbc", c->name) == 0) |
| 188 | return 14; |
| 189 | return cipher_keylen(c); |
| 190 | } |
| 191 | |
| 192 | u_int |
| 193 | cipher_authlen(const struct sshcipher *c) |
| 194 | { |
| 195 | return (c->auth_len); |
| 196 | } |
| 197 | |
| 198 | u_int |
| 199 | cipher_ivlen(const struct sshcipher *c) |
| 200 | { |
| 201 | /* |
| 202 | * Default is cipher block size, except for chacha20+poly1305 that |
| 203 | * needs no IV. XXX make iv_len == -1 default? |
| 204 | */ |
| 205 | return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ? |
| 206 | c->iv_len : c->block_size; |
| 207 | } |
| 208 | |
| 209 | u_int |
| 210 | cipher_get_number(const struct sshcipher *c) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 211 | { |
| 212 | return (c->number); |
| 213 | } |
| 214 | |
| 215 | u_int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 216 | cipher_is_cbc(const struct sshcipher *c) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 217 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 218 | return (c->flags & CFLAG_CBC) != 0; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | u_int |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 222 | cipher_ctx_is_plaintext(struct sshcipher_ctx *cc) |
| 223 | { |
| 224 | return cc->plaintext; |
| 225 | } |
| 226 | |
| 227 | u_int |
| 228 | cipher_ctx_get_number(struct sshcipher_ctx *cc) |
| 229 | { |
| 230 | return cc->cipher->number; |
| 231 | } |
| 232 | |
| 233 | u_int |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 234 | cipher_mask_ssh1(int client) |
| 235 | { |
| 236 | u_int mask = 0; |
| 237 | mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ |
| 238 | mask |= 1 << SSH_CIPHER_BLOWFISH; |
| 239 | if (client) { |
| 240 | mask |= 1 << SSH_CIPHER_DES; |
| 241 | } |
| 242 | return mask; |
| 243 | } |
| 244 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 245 | const struct sshcipher * |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 246 | cipher_by_name(const char *name) |
| 247 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 248 | const struct sshcipher *c; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 249 | for (c = ciphers; c->name != NULL; c++) |
| 250 | if (strcmp(c->name, name) == 0) |
| 251 | return c; |
| 252 | return NULL; |
| 253 | } |
| 254 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 255 | const struct sshcipher * |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 256 | cipher_by_number(int id) |
| 257 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 258 | const struct sshcipher *c; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 259 | for (c = ciphers; c->name != NULL; c++) |
| 260 | if (c->number == id) |
| 261 | return c; |
| 262 | return NULL; |
| 263 | } |
| 264 | |
| 265 | #define CIPHER_SEP "," |
| 266 | int |
| 267 | ciphers_valid(const char *names) |
| 268 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 269 | const struct sshcipher *c; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 270 | char *cipher_list, *cp; |
| 271 | char *p; |
| 272 | |
| 273 | if (names == NULL || strcmp(names, "") == 0) |
| 274 | return 0; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 275 | if ((cipher_list = cp = strdup(names)) == NULL) |
| 276 | return 0; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 277 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; |
| 278 | (p = strsep(&cp, CIPHER_SEP))) { |
| 279 | c = cipher_by_name(p); |
| 280 | if (c == NULL || c->number != SSH_CIPHER_SSH2) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 281 | free(cipher_list); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 282 | return 0; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 283 | } |
| 284 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 285 | free(cipher_list); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 286 | return 1; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Parses the name of the cipher. Returns the number of the corresponding |
| 291 | * cipher, or -1 on error. |
| 292 | */ |
| 293 | |
| 294 | int |
| 295 | cipher_number(const char *name) |
| 296 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 297 | const struct sshcipher *c; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 298 | if (name == NULL) |
| 299 | return -1; |
| 300 | for (c = ciphers; c->name != NULL; c++) |
| 301 | if (strcasecmp(c->name, name) == 0) |
| 302 | return c->number; |
| 303 | return -1; |
| 304 | } |
| 305 | |
| 306 | char * |
| 307 | cipher_name(int id) |
| 308 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 309 | const struct sshcipher *c = cipher_by_number(id); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 310 | return (c==NULL) ? "<unknown>" : c->name; |
| 311 | } |
| 312 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 313 | const char * |
| 314 | cipher_warning_message(const struct sshcipher_ctx *cc) |
| 315 | { |
| 316 | if (cc == NULL || cc->cipher == NULL) |
| 317 | return NULL; |
| 318 | if (cc->cipher->number == SSH_CIPHER_DES) |
| 319 | return "use of DES is strongly discouraged due to " |
| 320 | "cryptographic weaknesses"; |
| 321 | return NULL; |
| 322 | } |
| 323 | |
| 324 | int |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 325 | cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher, |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 326 | const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, |
| 327 | int do_encrypt) |
| 328 | { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 329 | struct sshcipher_ctx *cc = NULL; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 330 | int ret = SSH_ERR_INTERNAL_ERROR; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 331 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 332 | const EVP_CIPHER *type; |
| 333 | int klen; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 334 | u_char *junk, *discard; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 335 | #endif |
| 336 | |
| 337 | *ccp = NULL; |
| 338 | if ((cc = calloc(sizeof(*cc), 1)) == NULL) |
| 339 | return SSH_ERR_ALLOC_FAIL; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 340 | |
| 341 | if (cipher->number == SSH_CIPHER_DES) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 342 | if (keylen > 8) |
| 343 | keylen = 8; |
| 344 | } |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 345 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 346 | cc->plaintext = (cipher->number == SSH_CIPHER_NONE); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 347 | cc->encrypt = do_encrypt; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 348 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 349 | if (keylen < cipher->key_len || |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 350 | (iv != NULL && ivlen < cipher_ivlen(cipher))) { |
| 351 | ret = SSH_ERR_INVALID_ARGUMENT; |
| 352 | goto out; |
| 353 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 354 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 355 | cc->cipher = cipher; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 356 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 357 | ret = chachapoly_init(&cc->cp_ctx, key, keylen); |
| 358 | goto out; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 359 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 360 | #ifndef WITH_OPENSSL |
| 361 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 362 | aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen); |
| 363 | aesctr_ivsetup(&cc->ac_ctx, iv); |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 364 | ret = 0; |
| 365 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 366 | } |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 367 | if ((cc->cipher->flags & CFLAG_NONE) != 0) { |
| 368 | ret = 0; |
| 369 | goto out; |
| 370 | } |
| 371 | ret = SSH_ERR_INVALID_ARGUMENT; |
| 372 | goto out; |
| 373 | #else /* WITH_OPENSSL */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 374 | type = (*cipher->evptype)(); |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 375 | if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) { |
| 376 | ret = SSH_ERR_ALLOC_FAIL; |
| 377 | goto out; |
| 378 | } |
| 379 | if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 380 | (do_encrypt == CIPHER_ENCRYPT)) == 0) { |
| 381 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 382 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 383 | } |
| 384 | if (cipher_authlen(cipher) && |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 385 | !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 386 | -1, (u_char *)iv)) { |
| 387 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 388 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 389 | } |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 390 | klen = EVP_CIPHER_CTX_key_length(cc->evp); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 391 | if (klen > 0 && keylen != (u_int)klen) { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 392 | if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 393 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 394 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 395 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 396 | } |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 397 | if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 398 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 399 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 400 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 401 | |
| 402 | if (cipher->discard_len > 0) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 403 | if ((junk = malloc(cipher->discard_len)) == NULL || |
| 404 | (discard = malloc(cipher->discard_len)) == NULL) { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 405 | free(junk); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 406 | ret = SSH_ERR_ALLOC_FAIL; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 407 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 408 | } |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 409 | ret = EVP_Cipher(cc->evp, discard, junk, cipher->discard_len); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 410 | explicit_bzero(discard, cipher->discard_len); |
| 411 | free(junk); |
| 412 | free(discard); |
| 413 | if (ret != 1) { |
| 414 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 415 | goto out; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 416 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 417 | } |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 418 | ret = 0; |
| 419 | #endif /* WITH_OPENSSL */ |
| 420 | out: |
| 421 | if (ret == 0) { |
| 422 | /* success */ |
| 423 | *ccp = cc; |
| 424 | } else { |
| 425 | if (cc != NULL) { |
| 426 | #ifdef WITH_OPENSSL |
| 427 | if (cc->evp != NULL) |
| 428 | EVP_CIPHER_CTX_free(cc->evp); |
| 429 | #endif /* WITH_OPENSSL */ |
| 430 | explicit_bzero(cc, sizeof(*cc)); |
| 431 | free(cc); |
| 432 | } |
| 433 | } |
| 434 | return ret; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 437 | /* |
| 438 | * cipher_crypt() operates as following: |
| 439 | * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'. |
| 440 | * Theses bytes are treated as additional authenticated data for |
| 441 | * authenticated encryption modes. |
| 442 | * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. |
| 443 | * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag. |
| 444 | * This tag is written on encryption and verified on decryption. |
| 445 | * Both 'aadlen' and 'authlen' can be set to 0. |
| 446 | */ |
| 447 | int |
| 448 | cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest, |
| 449 | const u_char *src, u_int len, u_int aadlen, u_int authlen) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 450 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 451 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
| 452 | return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src, |
| 453 | len, aadlen, authlen, cc->encrypt); |
| 454 | } |
| 455 | #ifndef WITH_OPENSSL |
| 456 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 457 | if (aadlen) |
| 458 | memcpy(dest, src, aadlen); |
| 459 | aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen, |
| 460 | dest + aadlen, len); |
| 461 | return 0; |
| 462 | } |
| 463 | if ((cc->cipher->flags & CFLAG_NONE) != 0) { |
| 464 | memcpy(dest, src, aadlen + len); |
| 465 | return 0; |
| 466 | } |
| 467 | return SSH_ERR_INVALID_ARGUMENT; |
| 468 | #else |
| 469 | if (authlen) { |
| 470 | u_char lastiv[1]; |
| 471 | |
| 472 | if (authlen != cipher_authlen(cc->cipher)) |
| 473 | return SSH_ERR_INVALID_ARGUMENT; |
| 474 | /* increment IV */ |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 475 | if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 476 | 1, lastiv)) |
| 477 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 478 | /* set tag on decyption */ |
| 479 | if (!cc->encrypt && |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 480 | !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 481 | authlen, (u_char *)src + aadlen + len)) |
| 482 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 483 | } |
| 484 | if (aadlen) { |
| 485 | if (authlen && |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 486 | EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 487 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 488 | memcpy(dest, src, aadlen); |
| 489 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 490 | if (len % cc->cipher->block_size) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 491 | return SSH_ERR_INVALID_ARGUMENT; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 492 | if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 493 | len) < 0) |
| 494 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 495 | if (authlen) { |
| 496 | /* compute tag (on encrypt) or verify tag (on decrypt) */ |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 497 | if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 498 | return cc->encrypt ? |
| 499 | SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID; |
| 500 | if (cc->encrypt && |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 501 | !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 502 | authlen, dest + aadlen + len)) |
| 503 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 504 | } |
| 505 | return 0; |
| 506 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 507 | } |
| 508 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 509 | /* Extract the packet length, including any decryption necessary beforehand */ |
| 510 | int |
| 511 | cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr, |
| 512 | const u_char *cp, u_int len) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 513 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 514 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 515 | return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr, |
| 516 | cp, len); |
| 517 | if (len < 4) |
| 518 | return SSH_ERR_MESSAGE_INCOMPLETE; |
| 519 | *plenp = get_u32(cp); |
| 520 | return 0; |
| 521 | } |
| 522 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 523 | void |
| 524 | cipher_free(struct sshcipher_ctx *cc) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 525 | { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 526 | if (cc == NULL) |
| 527 | return; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 528 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 529 | explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx)); |
| 530 | else if ((cc->cipher->flags & CFLAG_AESCTR) != 0) |
| 531 | explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx)); |
| 532 | #ifdef WITH_OPENSSL |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 533 | if (cc->evp != NULL) { |
| 534 | EVP_CIPHER_CTX_free(cc->evp); |
| 535 | cc->evp = NULL; |
| 536 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 537 | #endif |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 538 | explicit_bzero(cc, sizeof(*cc)); |
| 539 | free(cc); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Selects the cipher, and keys if by computing the MD5 checksum of the |
| 544 | * passphrase and using the resulting 16 bytes as the key. |
| 545 | */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 546 | int |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 547 | cipher_set_key_string(struct sshcipher_ctx **ccp, |
| 548 | const struct sshcipher *cipher, const char *passphrase, int do_encrypt) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 549 | { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 550 | u_char digest[16]; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 551 | int r = SSH_ERR_INTERNAL_ERROR; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 552 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 553 | if ((r = ssh_digest_memory(SSH_DIGEST_MD5, |
| 554 | passphrase, strlen(passphrase), |
| 555 | digest, sizeof(digest))) != 0) |
| 556 | goto out; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 557 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 558 | r = cipher_init(ccp, cipher, digest, 16, NULL, 0, do_encrypt); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 559 | out: |
| 560 | explicit_bzero(digest, sizeof(digest)); |
| 561 | return r; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | /* |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 565 | * Exports an IV from the sshcipher_ctx required to export the key |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 566 | * state back from the unprivileged child to the privileged parent |
| 567 | * process. |
| 568 | */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 569 | int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 570 | cipher_get_keyiv_len(const struct sshcipher_ctx *cc) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 571 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 572 | const struct sshcipher *c = cc->cipher; |
| 573 | int ivlen = 0; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 574 | |
| 575 | if (c->number == SSH_CIPHER_3DES) |
| 576 | ivlen = 24; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 577 | else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 578 | ivlen = 0; |
| 579 | else if ((cc->cipher->flags & CFLAG_AESCTR) != 0) |
| 580 | ivlen = sizeof(cc->ac_ctx.ctr); |
| 581 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 582 | else |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 583 | ivlen = EVP_CIPHER_CTX_iv_length(cc->evp); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 584 | #endif /* WITH_OPENSSL */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 585 | return (ivlen); |
| 586 | } |
| 587 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 588 | int |
| 589 | cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 590 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 591 | const struct sshcipher *c = cc->cipher; |
| 592 | #ifdef WITH_OPENSSL |
| 593 | int evplen; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 594 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 595 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 596 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
| 597 | if (len != 0) |
| 598 | return SSH_ERR_INVALID_ARGUMENT; |
| 599 | return 0; |
| 600 | } |
| 601 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 602 | if (len != sizeof(cc->ac_ctx.ctr)) |
| 603 | return SSH_ERR_INVALID_ARGUMENT; |
| 604 | memcpy(iv, cc->ac_ctx.ctr, len); |
| 605 | return 0; |
| 606 | } |
| 607 | if ((cc->cipher->flags & CFLAG_NONE) != 0) |
| 608 | return 0; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 609 | |
| 610 | switch (c->number) { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 611 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 612 | case SSH_CIPHER_SSH2: |
| 613 | case SSH_CIPHER_DES: |
| 614 | case SSH_CIPHER_BLOWFISH: |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 615 | evplen = EVP_CIPHER_CTX_iv_length(cc->evp); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 616 | if (evplen == 0) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 617 | return 0; |
| 618 | else if (evplen < 0) |
| 619 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 620 | if ((u_int)evplen != len) |
| 621 | return SSH_ERR_INVALID_ARGUMENT; |
| 622 | #ifndef OPENSSL_HAVE_EVPCTR |
| 623 | if (c->evptype == evp_aes_128_ctr) |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 624 | ssh_aes_ctr_iv(cc->evp, 0, iv, len); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 625 | else |
| 626 | #endif |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 627 | if (cipher_authlen(c)) { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 628 | if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 629 | len, iv)) |
| 630 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 631 | } else |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 632 | memcpy(iv, cc->evp->iv, len); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 633 | break; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 634 | #endif |
| 635 | #ifdef WITH_SSH1 |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 636 | case SSH_CIPHER_3DES: |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 637 | return ssh1_3des_iv(cc->evp, 0, iv, 24); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 638 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 639 | default: |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 640 | return SSH_ERR_INVALID_ARGUMENT; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 641 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 642 | return 0; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 645 | int |
| 646 | cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv) |
| 647 | { |
| 648 | const struct sshcipher *c = cc->cipher; |
| 649 | #ifdef WITH_OPENSSL |
| 650 | int evplen = 0; |
| 651 | #endif |
| 652 | |
| 653 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 654 | return 0; |
| 655 | if ((cc->cipher->flags & CFLAG_NONE) != 0) |
| 656 | return 0; |
| 657 | |
| 658 | switch (c->number) { |
| 659 | #ifdef WITH_OPENSSL |
| 660 | case SSH_CIPHER_SSH2: |
| 661 | case SSH_CIPHER_DES: |
| 662 | case SSH_CIPHER_BLOWFISH: |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 663 | evplen = EVP_CIPHER_CTX_iv_length(cc->evp); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 664 | if (evplen <= 0) |
| 665 | return SSH_ERR_LIBCRYPTO_ERROR; |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 666 | #ifndef OPENSSL_HAVE_EVPCTR |
| 667 | /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */ |
| 668 | if (c->evptype == evp_aes_128_ctr) |
| 669 | ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen); |
| 670 | else |
| 671 | #endif |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 672 | if (cipher_authlen(c)) { |
| 673 | /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */ |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 674 | if (!EVP_CIPHER_CTX_ctrl(cc->evp, |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 675 | EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv)) |
| 676 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 677 | } else |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 678 | memcpy(cc->evp->iv, iv, evplen); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 679 | break; |
| 680 | #endif |
| 681 | #ifdef WITH_SSH1 |
| 682 | case SSH_CIPHER_3DES: |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 683 | return ssh1_3des_iv(cc->evp, 1, (u_char *)iv, 24); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 684 | #endif |
| 685 | default: |
| 686 | return SSH_ERR_INVALID_ARGUMENT; |
| 687 | } |
| 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | #ifdef WITH_OPENSSL |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 692 | #define EVP_X_STATE(evp) (evp)->cipher_data |
| 693 | #define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 694 | #endif |
| 695 | |
| 696 | int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 697 | cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 698 | { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 699 | #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 700 | const struct sshcipher *c = cc->cipher; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 701 | int plen = 0; |
| 702 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 703 | if (c->evptype == EVP_rc4) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 704 | plen = EVP_X_STATE_LEN(cc->evp); |
| 705 | if (dat == NULL) |
| 706 | return (plen); |
| 707 | memcpy(dat, EVP_X_STATE(cc->evp), plen); |
| 708 | } |
| 709 | return (plen); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 710 | #else |
| 711 | return 0; |
| 712 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | void |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 716 | cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 717 | { |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 718 | #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 719 | const struct sshcipher *c = cc->cipher; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 720 | int plen; |
| 721 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 722 | if (c->evptype == EVP_rc4) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 723 | plen = EVP_X_STATE_LEN(cc->evp); |
| 724 | memcpy(EVP_X_STATE(cc->evp), dat, plen); |
| 725 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 726 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 727 | } |