Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 1 | /* $OpenBSD: cipher.c,v 1.99 2014/06/24 01:13:21 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 12 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 13 | * |
| 14 | * Copyright (c) 1999 Niels Provos. All rights reserved. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 15 | * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 42 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 43 | #include <stdarg.h> |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 44 | #include <stdio.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 45 | |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 46 | #include "cipher.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 47 | #include "misc.h" |
| 48 | #include "sshbuf.h" |
| 49 | #include "ssherr.h" |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 50 | #include "digest.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 52 | #ifdef WITH_SSH1 |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 53 | extern const EVP_CIPHER *evp_ssh1_bf(void); |
| 54 | extern const EVP_CIPHER *evp_ssh1_3des(void); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 55 | extern int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 56 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 58 | struct sshcipher { |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 59 | char *name; |
| 60 | int number; /* for ssh1 only */ |
| 61 | u_int block_size; |
| 62 | u_int key_len; |
Damien Miller | d522c68 | 2013-01-09 16:42:47 +1100 | [diff] [blame] | 63 | u_int iv_len; /* defaults to block_size */ |
| 64 | u_int auth_len; |
Damien Miller | 3710f27 | 2005-05-26 12:19:17 +1000 | [diff] [blame] | 65 | u_int discard_len; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 66 | u_int flags; |
| 67 | #define CFLAG_CBC (1<<0) |
| 68 | #define CFLAG_CHACHAPOLY (1<<1) |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 69 | #define CFLAG_AESCTR (1<<2) |
| 70 | #define CFLAG_NONE (1<<3) |
| 71 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 72 | const EVP_CIPHER *(*evptype)(void); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 73 | #else |
| 74 | void *ignored; |
| 75 | #endif |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 76 | }; |
| 77 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 78 | static const struct sshcipher ciphers[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 79 | #ifdef WITH_SSH1 |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 80 | { "des", SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc }, |
| 81 | { "3des", SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des }, |
| 82 | { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 83 | #endif /* WITH_SSH1 */ |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 84 | #ifdef WITH_OPENSSL |
| 85 | { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null }, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 86 | { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc }, |
| 87 | { "blowfish-cbc", |
| 88 | SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc }, |
| 89 | { "cast128-cbc", |
| 90 | SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc }, |
| 91 | { "arcfour", SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 }, |
| 92 | { "arcfour128", SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 }, |
| 93 | { "arcfour256", SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 }, |
| 94 | { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc }, |
| 95 | { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc }, |
| 96 | { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, |
| 97 | { "rijndael-cbc@lysator.liu.se", |
| 98 | SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc }, |
| 99 | { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr }, |
| 100 | { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr }, |
| 101 | { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 102 | # ifdef OPENSSL_HAVE_EVPGCM |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 103 | { "aes128-gcm@openssh.com", |
| 104 | SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm }, |
| 105 | { "aes256-gcm@openssh.com", |
| 106 | SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 107 | # endif /* OPENSSL_HAVE_EVPGCM */ |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 108 | #else /* WITH_OPENSSL */ |
| 109 | { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, CFLAG_AESCTR, NULL }, |
| 110 | { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, CFLAG_AESCTR, NULL }, |
| 111 | { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, CFLAG_AESCTR, NULL }, |
| 112 | { "none", SSH_CIPHER_NONE, 8, 0, 0, 0, 0, CFLAG_NONE, NULL }, |
| 113 | #endif /* WITH_OPENSSL */ |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 114 | { "chacha20-poly1305@openssh.com", |
| 115 | SSH_CIPHER_SSH2, 8, 64, 0, 16, 0, CFLAG_CHACHAPOLY, NULL }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 116 | |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 117 | { NULL, SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 118 | }; |
| 119 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 120 | /*--*/ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 121 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 122 | /* Returns a comma-separated list of supported ciphers. */ |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 123 | char * |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 124 | cipher_alg_list(char sep, int auth_only) |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 125 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 126 | char *tmp, *ret = NULL; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 127 | size_t nlen, rlen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 128 | const struct sshcipher *c; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 129 | |
| 130 | for (c = ciphers; c->name != NULL; c++) { |
| 131 | if (c->number != SSH_CIPHER_SSH2) |
| 132 | continue; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 133 | if (auth_only && c->auth_len == 0) |
| 134 | continue; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 135 | if (ret != NULL) |
Damien Miller | 690d989 | 2013-11-08 12:16:49 +1100 | [diff] [blame] | 136 | ret[rlen++] = sep; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 137 | nlen = strlen(c->name); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 138 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
| 139 | free(ret); |
| 140 | return NULL; |
| 141 | } |
| 142 | ret = tmp; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 143 | memcpy(ret + rlen, c->name, nlen + 1); |
| 144 | rlen += nlen; |
| 145 | } |
| 146 | return ret; |
| 147 | } |
| 148 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 149 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 150 | cipher_blocksize(const struct sshcipher *c) |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 151 | { |
| 152 | return (c->block_size); |
| 153 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 154 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 155 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 156 | cipher_keylen(const struct sshcipher *c) |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 157 | { |
| 158 | return (c->key_len); |
| 159 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 160 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 161 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 162 | cipher_seclen(const struct sshcipher *c) |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 163 | { |
| 164 | if (strcmp("3des-cbc", c->name) == 0) |
| 165 | return 14; |
| 166 | return cipher_keylen(c); |
| 167 | } |
| 168 | |
| 169 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 170 | cipher_authlen(const struct sshcipher *c) |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 171 | { |
| 172 | return (c->auth_len); |
| 173 | } |
| 174 | |
| 175 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 176 | cipher_ivlen(const struct sshcipher *c) |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 177 | { |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 178 | /* |
| 179 | * Default is cipher block size, except for chacha20+poly1305 that |
| 180 | * needs no IV. XXX make iv_len == -1 default? |
| 181 | */ |
| 182 | return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ? |
| 183 | c->iv_len : c->block_size; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 187 | cipher_get_number(const struct sshcipher *c) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 188 | { |
| 189 | return (c->number); |
| 190 | } |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 191 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 192 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 193 | cipher_is_cbc(const struct sshcipher *c) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 194 | { |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 195 | return (c->flags & CFLAG_CBC) != 0; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | u_int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 199 | cipher_mask_ssh1(int client) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 200 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 201 | u_int mask = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 202 | mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 203 | mask |= 1 << SSH_CIPHER_BLOWFISH; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 204 | if (client) { |
| 205 | mask |= 1 << SSH_CIPHER_DES; |
| 206 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 207 | return mask; |
| 208 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 209 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 210 | const struct sshcipher * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 211 | cipher_by_name(const char *name) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 212 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 213 | const struct sshcipher *c; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 214 | for (c = ciphers; c->name != NULL; c++) |
Darren Tucker | 660db78 | 2005-01-24 21:57:11 +1100 | [diff] [blame] | 215 | if (strcmp(c->name, name) == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 216 | return c; |
| 217 | return NULL; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 218 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 219 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 220 | const struct sshcipher * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 221 | cipher_by_number(int id) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 222 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 223 | const struct sshcipher *c; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 224 | for (c = ciphers; c->name != NULL; c++) |
| 225 | if (c->number == id) |
| 226 | return c; |
| 227 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 228 | } |
| 229 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 230 | #define CIPHER_SEP "," |
| 231 | int |
| 232 | ciphers_valid(const char *names) |
| 233 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 234 | const struct sshcipher *c; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 235 | char *cipher_list, *cp; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 236 | char *p; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 237 | |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 238 | if (names == NULL || strcmp(names, "") == 0) |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 239 | return 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 240 | if ((cipher_list = cp = strdup(names)) == NULL) |
| 241 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 242 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 243 | (p = strsep(&cp, CIPHER_SEP))) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 244 | c = cipher_by_name(p); |
| 245 | if (c == NULL || c->number != SSH_CIPHER_SSH2) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 246 | free(cipher_list); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 250 | free(cipher_list); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 251 | return 1; |
| 252 | } |
| 253 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 254 | /* |
| 255 | * Parses the name of the cipher. Returns the number of the corresponding |
| 256 | * cipher, or -1 on error. |
| 257 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 258 | |
| 259 | int |
| 260 | cipher_number(const char *name) |
| 261 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 262 | const struct sshcipher *c; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 263 | if (name == NULL) |
| 264 | return -1; |
Darren Tucker | 660db78 | 2005-01-24 21:57:11 +1100 | [diff] [blame] | 265 | for (c = ciphers; c->name != NULL; c++) |
| 266 | if (strcasecmp(c->name, name) == 0) |
| 267 | return c->number; |
| 268 | return -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | char * |
| 272 | cipher_name(int id) |
| 273 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 274 | const struct sshcipher *c = cipher_by_number(id); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 275 | return (c==NULL) ? "<unknown>" : c->name; |
| 276 | } |
| 277 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 278 | const char * |
| 279 | cipher_warning_message(const struct sshcipher_ctx *cc) |
| 280 | { |
| 281 | if (cc == NULL || cc->cipher == NULL) |
| 282 | return NULL; |
| 283 | if (cc->cipher->number == SSH_CIPHER_DES) |
| 284 | return "use of DES is strongly discouraged due to " |
| 285 | "cryptographic weaknesses"; |
| 286 | return NULL; |
| 287 | } |
| 288 | |
| 289 | int |
| 290 | cipher_init(struct sshcipher_ctx *cc, const struct sshcipher *cipher, |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 291 | const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 292 | int do_encrypt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 293 | { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 294 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 295 | int ret = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 296 | const EVP_CIPHER *type; |
| 297 | int klen; |
Damien Miller | 3710f27 | 2005-05-26 12:19:17 +1000 | [diff] [blame] | 298 | u_char *junk, *discard; |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 299 | |
| 300 | if (cipher->number == SSH_CIPHER_DES) { |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 301 | if (keylen > 8) |
| 302 | keylen = 8; |
| 303 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 304 | #endif |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 305 | cc->plaintext = (cipher->number == SSH_CIPHER_NONE); |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 306 | cc->encrypt = do_encrypt; |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 307 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 308 | if (keylen < cipher->key_len || |
| 309 | (iv != NULL && ivlen < cipher_ivlen(cipher))) |
| 310 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 311 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 312 | cc->cipher = cipher; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 313 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 314 | return chachapoly_init(&cc->cp_ctx, key, keylen); |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 315 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 316 | #ifndef WITH_OPENSSL |
| 317 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 318 | aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen); |
| 319 | aesctr_ivsetup(&cc->ac_ctx, iv); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 320 | return 0; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 321 | } |
| 322 | if ((cc->cipher->flags & CFLAG_NONE) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 323 | return 0; |
| 324 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 325 | #else |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 326 | type = (*cipher->evptype)(); |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 327 | EVP_CIPHER_CTX_init(&cc->evp); |
| 328 | if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 329 | (do_encrypt == CIPHER_ENCRYPT)) == 0) { |
| 330 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 331 | goto bad; |
| 332 | } |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 333 | if (cipher_authlen(cipher) && |
| 334 | !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 335 | -1, (u_char *)iv)) { |
| 336 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 337 | goto bad; |
| 338 | } |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 339 | klen = EVP_CIPHER_CTX_key_length(&cc->evp); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 340 | if (klen > 0 && keylen != (u_int)klen) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 341 | if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) { |
| 342 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 343 | goto bad; |
| 344 | } |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 345 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 346 | if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) { |
| 347 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 348 | goto bad; |
| 349 | } |
Damien Miller | 3710f27 | 2005-05-26 12:19:17 +1000 | [diff] [blame] | 350 | |
Damien Miller | 46d38de | 2005-07-17 17:02:09 +1000 | [diff] [blame] | 351 | if (cipher->discard_len > 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 352 | if ((junk = malloc(cipher->discard_len)) == NULL || |
| 353 | (discard = malloc(cipher->discard_len)) == NULL) { |
| 354 | if (junk != NULL) |
| 355 | free(junk); |
| 356 | ret = SSH_ERR_ALLOC_FAIL; |
| 357 | goto bad; |
| 358 | } |
| 359 | ret = EVP_Cipher(&cc->evp, discard, junk, cipher->discard_len); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 360 | explicit_bzero(discard, cipher->discard_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 361 | free(junk); |
| 362 | free(discard); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 363 | if (ret != 1) { |
| 364 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
| 365 | bad: |
| 366 | EVP_CIPHER_CTX_cleanup(&cc->evp); |
| 367 | return ret; |
| 368 | } |
Damien Miller | 3710f27 | 2005-05-26 12:19:17 +1000 | [diff] [blame] | 369 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 370 | #endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 371 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 372 | } |
| 373 | |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 374 | /* |
| 375 | * cipher_crypt() operates as following: |
| 376 | * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'. |
| 377 | * Theses bytes are treated as additional authenticated data for |
| 378 | * authenticated encryption modes. |
| 379 | * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 380 | * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag. |
| 381 | * This tag is written on encryption and verified on decryption. |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 382 | * Both 'aadlen' and 'authlen' can be set to 0. |
| 383 | */ |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 384 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 385 | cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest, |
| 386 | const u_char *src, u_int len, u_int aadlen, u_int authlen) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 387 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 388 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
| 389 | return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src, |
| 390 | len, aadlen, authlen, cc->encrypt); |
| 391 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 392 | #ifndef WITH_OPENSSL |
| 393 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 394 | if (aadlen) |
| 395 | memcpy(dest, src, aadlen); |
| 396 | aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen, |
| 397 | dest + aadlen, len); |
| 398 | return 0; |
| 399 | } |
| 400 | if ((cc->cipher->flags & CFLAG_NONE) != 0) { |
| 401 | memcpy(dest, src, aadlen + len); |
| 402 | return 0; |
| 403 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 404 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 405 | #else |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 406 | if (authlen) { |
| 407 | u_char lastiv[1]; |
| 408 | |
| 409 | if (authlen != cipher_authlen(cc->cipher)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 410 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 411 | /* increment IV */ |
| 412 | if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN, |
| 413 | 1, lastiv)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 414 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 415 | /* set tag on decyption */ |
| 416 | if (!cc->encrypt && |
| 417 | !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG, |
| 418 | authlen, (u_char *)src + aadlen + len)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 419 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 420 | } |
| 421 | if (aadlen) { |
| 422 | if (authlen && |
| 423 | EVP_Cipher(&cc->evp, NULL, (u_char *)src, aadlen) < 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 424 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 425 | memcpy(dest, src, aadlen); |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 426 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 427 | if (len % cc->cipher->block_size) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 428 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 429 | if (EVP_Cipher(&cc->evp, dest + aadlen, (u_char *)src + aadlen, |
| 430 | len) < 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 431 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 432 | if (authlen) { |
| 433 | /* compute tag (on encrypt) or verify tag (on decrypt) */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 434 | if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) |
| 435 | return cc->encrypt ? |
| 436 | SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 437 | if (cc->encrypt && |
| 438 | !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG, |
| 439 | authlen, dest + aadlen + len)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 440 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 441 | } |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 442 | return 0; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 443 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 444 | } |
| 445 | |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 446 | /* Extract the packet length, including any decryption necessary beforehand */ |
| 447 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 448 | cipher_get_length(struct sshcipher_ctx *cc, u_int *plenp, u_int seqnr, |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 449 | const u_char *cp, u_int len) |
| 450 | { |
| 451 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 452 | return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr, |
| 453 | cp, len); |
| 454 | if (len < 4) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 455 | return SSH_ERR_MESSAGE_INCOMPLETE; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 456 | *plenp = get_u32(cp); |
| 457 | return 0; |
| 458 | } |
| 459 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 460 | int |
| 461 | cipher_cleanup(struct sshcipher_ctx *cc) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 462 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 463 | if (cc == NULL || cc->cipher == NULL) |
| 464 | return 0; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 465 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 466 | explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx)); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 467 | else if ((cc->cipher->flags & CFLAG_AESCTR) != 0) |
| 468 | explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx)); |
| 469 | #ifdef WITH_OPENSSL |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 470 | else if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 471 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 472 | #endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 473 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 474 | } |
| 475 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 476 | /* |
| 477 | * Selects the cipher, and keys if by computing the MD5 checksum of the |
| 478 | * passphrase and using the resulting 16 bytes as the key. |
| 479 | */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 480 | int |
| 481 | cipher_set_key_string(struct sshcipher_ctx *cc, const struct sshcipher *cipher, |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 482 | const char *passphrase, int do_encrypt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 483 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 484 | u_char digest[16]; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 485 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 486 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 487 | if ((r = ssh_digest_memory(SSH_DIGEST_MD5, |
| 488 | passphrase, strlen(passphrase), |
| 489 | digest, sizeof(digest))) != 0) |
| 490 | goto out; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 491 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 492 | r = cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt); |
| 493 | out: |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 494 | explicit_bzero(digest, sizeof(digest)); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 495 | return r; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 496 | } |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 497 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 498 | /* |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 499 | * Exports an IV from the sshcipher_ctx required to export the key |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 500 | * state back from the unprivileged child to the privileged parent |
| 501 | * process. |
| 502 | */ |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 503 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 504 | cipher_get_keyiv_len(const struct sshcipher_ctx *cc) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 505 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 506 | const struct sshcipher *c = cc->cipher; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 507 | int ivlen = 0; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 508 | |
| 509 | if (c->number == SSH_CIPHER_3DES) |
| 510 | ivlen = 24; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 511 | else if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 512 | ivlen = 0; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 513 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 514 | else |
| 515 | ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 516 | #endif /* WITH_OPENSSL */ |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 517 | return (ivlen); |
| 518 | } |
| 519 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 520 | int |
| 521 | cipher_get_keyiv(struct sshcipher_ctx *cc, u_char *iv, u_int len) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 522 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 523 | const struct sshcipher *c = cc->cipher; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 524 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 525 | int evplen; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 526 | #endif |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 527 | |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 528 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
| 529 | if (len != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 530 | return SSH_ERR_INVALID_ARGUMENT; |
| 531 | return 0; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 532 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 533 | if ((cc->cipher->flags & CFLAG_NONE) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 534 | return 0; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 535 | |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 536 | switch (c->number) { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 537 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 538 | case SSH_CIPHER_SSH2: |
| 539 | case SSH_CIPHER_DES: |
| 540 | case SSH_CIPHER_BLOWFISH: |
| 541 | evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); |
| 542 | if (evplen == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 543 | return 0; |
| 544 | else if (evplen < 0) |
| 545 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 546 | if ((u_int)evplen != len) |
| 547 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | eae8874 | 2014-05-27 14:27:02 +1000 | [diff] [blame] | 548 | if (cipher_authlen(c)) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 549 | if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN, |
| 550 | len, iv)) |
| 551 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 552 | } else |
| 553 | memcpy(iv, cc->evp.iv, len); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 554 | break; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 555 | #endif |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 556 | #ifdef WITH_SSH1 |
Damien Miller | a201bb3 | 2003-05-14 13:41:23 +1000 | [diff] [blame] | 557 | case SSH_CIPHER_3DES: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 558 | return ssh1_3des_iv(&cc->evp, 0, iv, 24); |
| 559 | #endif |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 560 | default: |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 561 | return SSH_ERR_INVALID_ARGUMENT; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 562 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 563 | return 0; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 566 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 567 | cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv) |
| 568 | { |
| 569 | const struct sshcipher *c = cc->cipher; |
| 570 | #ifdef WITH_OPENSSL |
| 571 | int evplen = 0; |
| 572 | #endif |
| 573 | |
| 574 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 575 | return 0; |
| 576 | if ((cc->cipher->flags & CFLAG_NONE) != 0) |
| 577 | return 0; |
| 578 | |
| 579 | switch (c->number) { |
| 580 | #ifdef WITH_OPENSSL |
| 581 | case SSH_CIPHER_SSH2: |
| 582 | case SSH_CIPHER_DES: |
| 583 | case SSH_CIPHER_BLOWFISH: |
| 584 | evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); |
| 585 | if (evplen <= 0) |
| 586 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 587 | if (cipher_authlen(c)) { |
| 588 | /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */ |
| 589 | if (!EVP_CIPHER_CTX_ctrl(&cc->evp, |
| 590 | EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv)) |
| 591 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 592 | } else |
| 593 | memcpy(cc->evp.iv, iv, evplen); |
| 594 | break; |
| 595 | #endif |
| 596 | #ifdef WITH_SSH1 |
| 597 | case SSH_CIPHER_3DES: |
| 598 | return ssh1_3des_iv(&cc->evp, 1, (u_char *)iv, 24); |
| 599 | #endif |
| 600 | default: |
| 601 | return SSH_ERR_INVALID_ARGUMENT; |
| 602 | } |
| 603 | return 0; |
| 604 | } |
| 605 | |
| 606 | #ifdef WITH_OPENSSL |
| 607 | #define EVP_X_STATE(evp) (evp).cipher_data |
| 608 | #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size |
| 609 | #endif |
| 610 | |
| 611 | int |
| 612 | cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 613 | { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 614 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 615 | const struct sshcipher *c = cc->cipher; |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 616 | int plen = 0; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 617 | |
Damien Miller | f0a8ded | 2013-02-12 11:00:34 +1100 | [diff] [blame] | 618 | if (c->evptype == EVP_rc4) { |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 619 | plen = EVP_X_STATE_LEN(cc->evp); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 620 | if (dat == NULL) |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 621 | return (plen); |
| 622 | memcpy(dat, EVP_X_STATE(cc->evp), plen); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 623 | } |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 624 | return (plen); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 625 | #else |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 626 | return 0; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 627 | #endif |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | void |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 631 | cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 632 | { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 633 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 634 | const struct sshcipher *c = cc->cipher; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 635 | int plen; |
| 636 | |
Damien Miller | f0a8ded | 2013-02-12 11:00:34 +1100 | [diff] [blame] | 637 | if (c->evptype == EVP_rc4) { |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 638 | plen = EVP_X_STATE_LEN(cc->evp); |
| 639 | memcpy(EVP_X_STATE(cc->evp), dat, plen); |
| 640 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 641 | #endif |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 642 | } |