djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 1 | /* $OpenBSD: cipher.c,v 1.107 2017/05/07 23:12:57 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 | |
Darren Tucker | 5573171 | 2014-07-21 02:24:59 +1000 | [diff] [blame] | 52 | #include "openbsd-compat/openssl-compat.h" |
| 53 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 54 | |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 55 | struct sshcipher_ctx { |
| 56 | int plaintext; |
| 57 | int encrypt; |
| 58 | EVP_CIPHER_CTX *evp; |
| 59 | struct chachapoly_ctx cp_ctx; /* XXX union with evp? */ |
| 60 | struct aesctr_ctx ac_ctx; /* XXX union with evp? */ |
| 61 | const struct sshcipher *cipher; |
| 62 | }; |
| 63 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 64 | struct sshcipher { |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 65 | char *name; |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 66 | u_int block_size; |
| 67 | u_int key_len; |
Damien Miller | d522c68 | 2013-01-09 16:42:47 +1100 | [diff] [blame] | 68 | u_int iv_len; /* defaults to block_size */ |
| 69 | u_int auth_len; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 70 | u_int flags; |
| 71 | #define CFLAG_CBC (1<<0) |
| 72 | #define CFLAG_CHACHAPOLY (1<<1) |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 73 | #define CFLAG_AESCTR (1<<2) |
| 74 | #define CFLAG_NONE (1<<3) |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 75 | #define CFLAG_INTERNAL CFLAG_NONE /* Don't use "none" for packets */ |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 76 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 77 | const EVP_CIPHER *(*evptype)(void); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 78 | #else |
| 79 | void *ignored; |
| 80 | #endif |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 81 | }; |
| 82 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 83 | static const struct sshcipher ciphers[] = { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 84 | #ifdef WITH_OPENSSL |
djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 85 | { "3des-cbc", 8, 24, 0, 0, CFLAG_CBC, EVP_des_ede3_cbc }, |
| 86 | { "aes128-cbc", 16, 16, 0, 0, CFLAG_CBC, EVP_aes_128_cbc }, |
| 87 | { "aes192-cbc", 16, 24, 0, 0, CFLAG_CBC, EVP_aes_192_cbc }, |
| 88 | { "aes256-cbc", 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc }, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 89 | { "rijndael-cbc@lysator.liu.se", |
djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 90 | 16, 32, 0, 0, CFLAG_CBC, EVP_aes_256_cbc }, |
| 91 | { "aes128-ctr", 16, 16, 0, 0, 0, EVP_aes_128_ctr }, |
| 92 | { "aes192-ctr", 16, 24, 0, 0, 0, EVP_aes_192_ctr }, |
| 93 | { "aes256-ctr", 16, 32, 0, 0, 0, EVP_aes_256_ctr }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 94 | # ifdef OPENSSL_HAVE_EVPGCM |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 95 | { "aes128-gcm@openssh.com", |
djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 96 | 16, 16, 12, 16, 0, EVP_aes_128_gcm }, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 97 | { "aes256-gcm@openssh.com", |
djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 98 | 16, 32, 12, 16, 0, EVP_aes_256_gcm }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 99 | # endif /* OPENSSL_HAVE_EVPGCM */ |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 100 | #else |
djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 101 | { "aes128-ctr", 16, 16, 0, 0, CFLAG_AESCTR, NULL }, |
| 102 | { "aes192-ctr", 16, 24, 0, 0, CFLAG_AESCTR, NULL }, |
| 103 | { "aes256-ctr", 16, 32, 0, 0, CFLAG_AESCTR, NULL }, |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 104 | #endif |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 105 | { "chacha20-poly1305@openssh.com", |
djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 106 | 8, 64, 0, 16, CFLAG_CHACHAPOLY, NULL }, |
| 107 | { "none", 8, 0, 0, 0, CFLAG_NONE, NULL }, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 108 | |
djm@openbsd.org | acaf34f | 2017-05-07 23:12:57 +0000 | [diff] [blame] | 109 | { NULL, 0, 0, 0, 0, 0, NULL } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 110 | }; |
| 111 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 112 | /*--*/ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 114 | /* Returns a comma-separated list of supported ciphers. */ |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 115 | char * |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 116 | cipher_alg_list(char sep, int auth_only) |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 117 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 118 | char *tmp, *ret = NULL; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 119 | size_t nlen, rlen = 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 120 | const struct sshcipher *c; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 121 | |
| 122 | for (c = ciphers; c->name != NULL; c++) { |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 123 | if ((c->flags & CFLAG_INTERNAL) != 0) |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 124 | continue; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 125 | if (auth_only && c->auth_len == 0) |
| 126 | continue; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 127 | if (ret != NULL) |
Damien Miller | 690d989 | 2013-11-08 12:16:49 +1100 | [diff] [blame] | 128 | ret[rlen++] = sep; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 129 | nlen = strlen(c->name); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 130 | if ((tmp = realloc(ret, rlen + nlen + 2)) == NULL) { |
| 131 | free(ret); |
| 132 | return NULL; |
| 133 | } |
| 134 | ret = tmp; |
Damien Miller | ea11119 | 2013-04-23 19:24:32 +1000 | [diff] [blame] | 135 | memcpy(ret + rlen, c->name, nlen + 1); |
| 136 | rlen += nlen; |
| 137 | } |
| 138 | return ret; |
| 139 | } |
| 140 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 141 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 142 | cipher_blocksize(const struct sshcipher *c) |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 143 | { |
| 144 | return (c->block_size); |
| 145 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 146 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 147 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 148 | cipher_keylen(const struct sshcipher *c) |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 149 | { |
| 150 | return (c->key_len); |
| 151 | } |
Ben Lindstrom | 836f0e9 | 2002-06-23 21:21:30 +0000 | [diff] [blame] | 152 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 153 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 154 | cipher_seclen(const struct sshcipher *c) |
Damien Miller | 76eea4a | 2014-01-26 09:37:25 +1100 | [diff] [blame] | 155 | { |
| 156 | if (strcmp("3des-cbc", c->name) == 0) |
| 157 | return 14; |
| 158 | return cipher_keylen(c); |
| 159 | } |
| 160 | |
| 161 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 162 | cipher_authlen(const struct sshcipher *c) |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 163 | { |
| 164 | return (c->auth_len); |
| 165 | } |
| 166 | |
| 167 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 168 | cipher_ivlen(const struct sshcipher *c) |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 169 | { |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 170 | /* |
| 171 | * Default is cipher block size, except for chacha20+poly1305 that |
| 172 | * needs no IV. XXX make iv_len == -1 default? |
| 173 | */ |
| 174 | return (c->iv_len != 0 || (c->flags & CFLAG_CHACHAPOLY) != 0) ? |
| 175 | c->iv_len : c->block_size; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | u_int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 179 | cipher_is_cbc(const struct sshcipher *c) |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 180 | { |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 181 | return (c->flags & CFLAG_CBC) != 0; |
Damien Miller | 13ae44c | 2009-01-28 16:38:41 +1100 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | u_int |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 185 | cipher_ctx_is_plaintext(struct sshcipher_ctx *cc) |
| 186 | { |
| 187 | return cc->plaintext; |
| 188 | } |
| 189 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 190 | const struct sshcipher * |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 191 | cipher_by_name(const char *name) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 192 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 193 | const struct sshcipher *c; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 194 | for (c = ciphers; c->name != NULL; c++) |
Darren Tucker | 660db78 | 2005-01-24 21:57:11 +1100 | [diff] [blame] | 195 | if (strcmp(c->name, name) == 0) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 196 | return c; |
| 197 | return NULL; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 198 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 200 | #define CIPHER_SEP "," |
| 201 | int |
| 202 | ciphers_valid(const char *names) |
| 203 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 204 | const struct sshcipher *c; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 205 | char *cipher_list, *cp; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 206 | char *p; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 207 | |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 208 | if (names == NULL || strcmp(names, "") == 0) |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 209 | return 0; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 210 | if ((cipher_list = cp = strdup(names)) == NULL) |
| 211 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 212 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 213 | (p = strsep(&cp, CIPHER_SEP))) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 214 | c = cipher_by_name(p); |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 215 | if (c == NULL || (c->flags & CFLAG_INTERNAL) != 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 216 | free(cipher_list); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 220 | free(cipher_list); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 221 | return 1; |
| 222 | } |
| 223 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 224 | const char * |
| 225 | cipher_warning_message(const struct sshcipher_ctx *cc) |
| 226 | { |
| 227 | if (cc == NULL || cc->cipher == NULL) |
| 228 | return NULL; |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 229 | /* XXX repurpose for CBC warning */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | int |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 234 | cipher_init(struct sshcipher_ctx **ccp, const struct sshcipher *cipher, |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 235 | 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] | 236 | int do_encrypt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 237 | { |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 238 | struct sshcipher_ctx *cc = NULL; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 239 | int ret = SSH_ERR_INTERNAL_ERROR; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 240 | #ifdef WITH_OPENSSL |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 241 | const EVP_CIPHER *type; |
| 242 | int klen; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 243 | #endif |
| 244 | |
| 245 | *ccp = NULL; |
| 246 | if ((cc = calloc(sizeof(*cc), 1)) == NULL) |
| 247 | return SSH_ERR_ALLOC_FAIL; |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 248 | |
djm@openbsd.org | e77e156 | 2017-05-01 00:03:18 +0000 | [diff] [blame] | 249 | cc->plaintext = (cipher->flags & CFLAG_NONE) != 0; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 250 | cc->encrypt = do_encrypt; |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 251 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 252 | if (keylen < cipher->key_len || |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 253 | (iv != NULL && ivlen < cipher_ivlen(cipher))) { |
| 254 | ret = SSH_ERR_INVALID_ARGUMENT; |
| 255 | goto out; |
| 256 | } |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 257 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 258 | cc->cipher = cipher; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 259 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 260 | ret = chachapoly_init(&cc->cp_ctx, key, keylen); |
| 261 | goto out; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 262 | } |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 263 | if ((cc->cipher->flags & CFLAG_NONE) != 0) { |
| 264 | ret = 0; |
| 265 | goto out; |
| 266 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 267 | #ifndef WITH_OPENSSL |
| 268 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 269 | aesctr_keysetup(&cc->ac_ctx, key, 8 * keylen, 8 * ivlen); |
| 270 | aesctr_ivsetup(&cc->ac_ctx, iv); |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 271 | ret = 0; |
| 272 | goto out; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 273 | } |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 274 | ret = SSH_ERR_INVALID_ARGUMENT; |
| 275 | goto out; |
| 276 | #else /* WITH_OPENSSL */ |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 277 | type = (*cipher->evptype)(); |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 278 | if ((cc->evp = EVP_CIPHER_CTX_new()) == NULL) { |
| 279 | ret = SSH_ERR_ALLOC_FAIL; |
| 280 | goto out; |
| 281 | } |
| 282 | if (EVP_CipherInit(cc->evp, type, NULL, (u_char *)iv, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 283 | (do_encrypt == CIPHER_ENCRYPT)) == 0) { |
| 284 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 285 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 286 | } |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 287 | if (cipher_authlen(cipher) && |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 288 | !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_IV_FIXED, |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 289 | -1, (u_char *)iv)) { |
| 290 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 291 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 292 | } |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 293 | klen = EVP_CIPHER_CTX_key_length(cc->evp); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 294 | if (klen > 0 && keylen != (u_int)klen) { |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 295 | if (EVP_CIPHER_CTX_set_key_length(cc->evp, keylen) == 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 296 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 297 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 298 | } |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 299 | } |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 300 | if (EVP_CipherInit(cc->evp, NULL, (u_char *)key, NULL, -1) == 0) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 301 | ret = SSH_ERR_LIBCRYPTO_ERROR; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 302 | goto out; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 303 | } |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 304 | ret = 0; |
| 305 | #endif /* WITH_OPENSSL */ |
| 306 | out: |
| 307 | if (ret == 0) { |
| 308 | /* success */ |
| 309 | *ccp = cc; |
| 310 | } else { |
| 311 | if (cc != NULL) { |
| 312 | #ifdef WITH_OPENSSL |
| 313 | if (cc->evp != NULL) |
| 314 | EVP_CIPHER_CTX_free(cc->evp); |
| 315 | #endif /* WITH_OPENSSL */ |
| 316 | explicit_bzero(cc, sizeof(*cc)); |
| 317 | free(cc); |
| 318 | } |
| 319 | } |
| 320 | return ret; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 321 | } |
| 322 | |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 323 | /* |
| 324 | * cipher_crypt() operates as following: |
| 325 | * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'. |
| 326 | * Theses bytes are treated as additional authenticated data for |
| 327 | * authenticated encryption modes. |
| 328 | * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'. |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 329 | * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag. |
| 330 | * This tag is written on encryption and verified on decryption. |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 331 | * Both 'aadlen' and 'authlen' can be set to 0. |
| 332 | */ |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 333 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 334 | cipher_crypt(struct sshcipher_ctx *cc, u_int seqnr, u_char *dest, |
| 335 | 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] | 336 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 337 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
| 338 | return chachapoly_crypt(&cc->cp_ctx, seqnr, dest, src, |
| 339 | len, aadlen, authlen, cc->encrypt); |
| 340 | } |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 341 | if ((cc->cipher->flags & CFLAG_NONE) != 0) { |
| 342 | memcpy(dest, src, aadlen + len); |
| 343 | return 0; |
| 344 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 345 | #ifndef WITH_OPENSSL |
| 346 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 347 | if (aadlen) |
| 348 | memcpy(dest, src, aadlen); |
| 349 | aesctr_encrypt_bytes(&cc->ac_ctx, src + aadlen, |
| 350 | dest + aadlen, len); |
| 351 | return 0; |
| 352 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 353 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 354 | #else |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 355 | if (authlen) { |
| 356 | u_char lastiv[1]; |
| 357 | |
| 358 | if (authlen != cipher_authlen(cc->cipher)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 359 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 360 | /* increment IV */ |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 361 | if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 362 | 1, lastiv)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 363 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 364 | /* set tag on decyption */ |
| 365 | if (!cc->encrypt && |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 366 | !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_SET_TAG, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 367 | authlen, (u_char *)src + aadlen + len)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 368 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 369 | } |
| 370 | if (aadlen) { |
| 371 | if (authlen && |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 372 | EVP_Cipher(cc->evp, NULL, (u_char *)src, aadlen) < 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 373 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 374 | memcpy(dest, src, aadlen); |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 375 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 376 | if (len % cc->cipher->block_size) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 377 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 378 | if (EVP_Cipher(cc->evp, dest + aadlen, (u_char *)src + aadlen, |
Damien Miller | af43a7a | 2012-12-12 10:46:31 +1100 | [diff] [blame] | 379 | len) < 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 380 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 381 | if (authlen) { |
| 382 | /* compute tag (on encrypt) or verify tag (on decrypt) */ |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 383 | if (EVP_Cipher(cc->evp, NULL, NULL, 0) < 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 384 | return cc->encrypt ? |
| 385 | SSH_ERR_LIBCRYPTO_ERROR : SSH_ERR_MAC_INVALID; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 386 | if (cc->encrypt && |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 387 | !EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_GET_TAG, |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 388 | authlen, dest + aadlen + len)) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 389 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 1d75abf | 2013-01-09 16:12:19 +1100 | [diff] [blame] | 390 | } |
Damien Miller | bcd00ab | 2013-12-07 10:41:55 +1100 | [diff] [blame] | 391 | return 0; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 392 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 393 | } |
| 394 | |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 395 | /* Extract the packet length, including any decryption necessary beforehand */ |
| 396 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 397 | 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] | 398 | const u_char *cp, u_int len) |
| 399 | { |
| 400 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 401 | return chachapoly_get_length(&cc->cp_ctx, plenp, seqnr, |
| 402 | cp, len); |
| 403 | if (len < 4) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 404 | return SSH_ERR_MESSAGE_INCOMPLETE; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 405 | *plenp = get_u32(cp); |
| 406 | return 0; |
| 407 | } |
| 408 | |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 409 | void |
| 410 | cipher_free(struct sshcipher_ctx *cc) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 411 | { |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 412 | if (cc == NULL) |
| 413 | return; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 414 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 415 | explicit_bzero(&cc->cp_ctx, sizeof(cc->cp_ctx)); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 416 | else if ((cc->cipher->flags & CFLAG_AESCTR) != 0) |
| 417 | explicit_bzero(&cc->ac_ctx, sizeof(cc->ac_ctx)); |
| 418 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 419 | if (cc->evp != NULL) { |
| 420 | EVP_CIPHER_CTX_free(cc->evp); |
| 421 | cc->evp = NULL; |
| 422 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 423 | #endif |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 424 | explicit_bzero(cc, sizeof(*cc)); |
| 425 | free(cc); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 426 | } |
| 427 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 428 | /* |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 429 | * Exports an IV from the sshcipher_ctx required to export the key |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 430 | * state back from the unprivileged child to the privileged parent |
| 431 | * process. |
| 432 | */ |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 433 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 434 | cipher_get_keyiv_len(const struct sshcipher_ctx *cc) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 435 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 436 | const struct sshcipher *c = cc->cipher; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 437 | |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 438 | if ((c->flags & CFLAG_CHACHAPOLY) != 0) |
| 439 | return 0; |
| 440 | else if ((c->flags & CFLAG_AESCTR) != 0) |
| 441 | return sizeof(cc->ac_ctx.ctr); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 442 | #ifdef WITH_OPENSSL |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 443 | return EVP_CIPHER_CTX_iv_length(cc->evp); |
| 444 | #else |
| 445 | return 0; |
| 446 | #endif |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 449 | int |
| 450 | 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] | 451 | { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 452 | const struct sshcipher *c = cc->cipher; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 453 | #ifdef WITH_OPENSSL |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 454 | int evplen; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 455 | #endif |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 456 | |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 457 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) { |
| 458 | if (len != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 459 | return SSH_ERR_INVALID_ARGUMENT; |
| 460 | return 0; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 461 | } |
djm@openbsd.org | 540e891 | 2015-01-14 10:29:45 +0000 | [diff] [blame] | 462 | if ((cc->cipher->flags & CFLAG_AESCTR) != 0) { |
| 463 | if (len != sizeof(cc->ac_ctx.ctr)) |
| 464 | return SSH_ERR_INVALID_ARGUMENT; |
| 465 | memcpy(iv, cc->ac_ctx.ctr, len); |
| 466 | return 0; |
| 467 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 468 | if ((cc->cipher->flags & CFLAG_NONE) != 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 469 | return 0; |
Damien Miller | 0fde8ac | 2013-11-21 14:12:23 +1100 | [diff] [blame] | 470 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 471 | #ifdef WITH_OPENSSL |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 472 | evplen = EVP_CIPHER_CTX_iv_length(cc->evp); |
| 473 | if (evplen == 0) |
| 474 | return 0; |
| 475 | else if (evplen < 0) |
| 476 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 477 | if ((u_int)evplen != len) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 478 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 479 | #ifndef OPENSSL_HAVE_EVPCTR |
| 480 | if (c->evptype == evp_aes_128_ctr) |
| 481 | ssh_aes_ctr_iv(cc->evp, 0, iv, len); |
| 482 | else |
| 483 | #endif |
| 484 | if (cipher_authlen(c)) { |
| 485 | if (!EVP_CIPHER_CTX_ctrl(cc->evp, EVP_CTRL_GCM_IV_GEN, |
| 486 | len, iv)) |
| 487 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 488 | } else |
| 489 | memcpy(iv, cc->evp->iv, len); |
| 490 | #endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 491 | return 0; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 494 | int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 495 | cipher_set_keyiv(struct sshcipher_ctx *cc, const u_char *iv) |
| 496 | { |
| 497 | const struct sshcipher *c = cc->cipher; |
| 498 | #ifdef WITH_OPENSSL |
| 499 | int evplen = 0; |
| 500 | #endif |
| 501 | |
| 502 | if ((cc->cipher->flags & CFLAG_CHACHAPOLY) != 0) |
| 503 | return 0; |
| 504 | if ((cc->cipher->flags & CFLAG_NONE) != 0) |
| 505 | return 0; |
| 506 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 507 | #ifdef WITH_OPENSSL |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 508 | evplen = EVP_CIPHER_CTX_iv_length(cc->evp); |
| 509 | if (evplen <= 0) |
| 510 | return SSH_ERR_LIBCRYPTO_ERROR; |
Damien Miller | 315d2a4 | 2016-10-28 14:34:07 +1100 | [diff] [blame] | 511 | #ifndef OPENSSL_HAVE_EVPCTR |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 512 | /* XXX iv arg is const, but ssh_aes_ctr_iv isn't */ |
| 513 | if (c->evptype == evp_aes_128_ctr) |
| 514 | ssh_aes_ctr_iv(cc->evp, 1, (u_char *)iv, evplen); |
| 515 | else |
Damien Miller | 315d2a4 | 2016-10-28 14:34:07 +1100 | [diff] [blame] | 516 | #endif |
djm@openbsd.org | cdccebd | 2017-04-30 23:15:04 +0000 | [diff] [blame] | 517 | if (cipher_authlen(c)) { |
| 518 | /* XXX iv arg is const, but EVP_CIPHER_CTX_ctrl isn't */ |
| 519 | if (!EVP_CIPHER_CTX_ctrl(cc->evp, |
| 520 | EVP_CTRL_GCM_SET_IV_FIXED, -1, (void *)iv)) |
| 521 | return SSH_ERR_LIBCRYPTO_ERROR; |
| 522 | } else |
| 523 | memcpy(cc->evp->iv, iv, evplen); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 524 | #endif |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | #ifdef WITH_OPENSSL |
djm@openbsd.org | 4706c1d | 2016-08-03 05:41:57 +0000 | [diff] [blame] | 529 | #define EVP_X_STATE(evp) (evp)->cipher_data |
| 530 | #define EVP_X_STATE_LEN(evp) (evp)->cipher->ctx_size |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 531 | #endif |
| 532 | |
| 533 | int |
| 534 | cipher_get_keycontext(const struct sshcipher_ctx *cc, u_char *dat) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 535 | { |
Damien Miller | 832b744 | 2016-07-15 14:45:34 +1000 | [diff] [blame] | 536 | #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 537 | const struct sshcipher *c = cc->cipher; |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 538 | int plen = 0; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 539 | |
Damien Miller | f0a8ded | 2013-02-12 11:00:34 +1100 | [diff] [blame] | 540 | if (c->evptype == EVP_rc4) { |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 541 | plen = EVP_X_STATE_LEN(cc->evp); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 542 | if (dat == NULL) |
Ben Lindstrom | 402c6cc | 2002-06-21 00:43:42 +0000 | [diff] [blame] | 543 | return (plen); |
| 544 | memcpy(dat, EVP_X_STATE(cc->evp), plen); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 545 | } |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 546 | return (plen); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 547 | #else |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 548 | return 0; |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 549 | #endif |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | void |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 553 | cipher_set_keycontext(struct sshcipher_ctx *cc, const u_char *dat) |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 554 | { |
Damien Miller | 832b744 | 2016-07-15 14:45:34 +1000 | [diff] [blame] | 555 | #if defined(WITH_OPENSSL) && !defined(OPENSSL_NO_RC4) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 556 | const struct sshcipher *c = cc->cipher; |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 557 | int plen; |
| 558 | |
Damien Miller | f0a8ded | 2013-02-12 11:00:34 +1100 | [diff] [blame] | 559 | if (c->evptype == EVP_rc4) { |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 560 | plen = EVP_X_STATE_LEN(cc->evp); |
| 561 | memcpy(EVP_X_STATE(cc->evp), dat, plen); |
| 562 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 563 | #endif |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 564 | } |