Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 4 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 5 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 6 | * As far as I am concerned, the code I have written for this software |
| 7 | * can be used freely for any purpose. Any derived versions of this |
| 8 | * software must be clearly marked as such, and if the derived work is |
| 9 | * incompatible with the protocol description in the RFC file, it must be |
| 10 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 11 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 12 | * |
| 13 | * Copyright (c) 1999 Niels Provos. All rights reserved. |
Ben Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 14 | * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 15 | * |
| 16 | * Redistribution and use in source and binary forms, with or without |
| 17 | * modification, are permitted provided that the following conditions |
| 18 | * are met: |
| 19 | * 1. Redistributions of source code must retain the above copyright |
| 20 | * notice, this list of conditions and the following disclaimer. |
| 21 | * 2. Redistributions in binary form must reproduce the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer in the |
| 23 | * documentation and/or other materials provided with the distribution. |
| 24 | * |
| 25 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 26 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 27 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 28 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 30 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 31 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 32 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 33 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 34 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 35 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | |
| 37 | #include "includes.h" |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 38 | RCSID("$OpenBSD: cipher.c,v 1.58 2002/06/04 23:05:49 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 39 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 40 | #include "xmalloc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 41 | #include "log.h" |
| 42 | #include "cipher.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 43 | |
| 44 | #include <openssl/md5.h> |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 45 | |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 46 | #if OPENSSL_VERSION_NUMBER < 0x00906000L |
| 47 | #define SSH_OLD_EVP |
| 48 | #define EVP_CIPHER_CTX_get_app_data(e) ((e)->app_data) |
| 49 | #endif |
| 50 | |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 51 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 52 | #include "rijndael.h" |
| 53 | static const EVP_CIPHER *evp_rijndael(void); |
| 54 | #endif |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 55 | static const EVP_CIPHER *evp_ssh1_3des(void); |
| 56 | static const EVP_CIPHER *evp_ssh1_bf(void); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 57 | |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 58 | struct Cipher { |
| 59 | char *name; |
| 60 | int number; /* for ssh1 only */ |
| 61 | u_int block_size; |
| 62 | u_int key_len; |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 63 | const EVP_CIPHER *(*evptype)(void); |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 64 | } ciphers[] = { |
| 65 | { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null }, |
| 66 | { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc }, |
| 67 | { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des }, |
| 68 | { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf }, |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 69 | |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 70 | { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc }, |
| 71 | { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc }, |
| 72 | { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc }, |
| 73 | { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 }, |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 74 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 75 | { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael }, |
| 76 | { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael }, |
| 77 | { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael }, |
Ben Lindstrom | 8a725a8 | 2002-04-04 22:10:38 +0000 | [diff] [blame] | 78 | { "rijndael-cbc@lysator.liu.se", |
| 79 | SSH_CIPHER_SSH2, 16, 32, evp_rijndael }, |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 80 | #else |
| 81 | { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, EVP_aes_128_cbc }, |
| 82 | { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, EVP_aes_192_cbc }, |
| 83 | { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc }, |
| 84 | { "rijndael-cbc@lysator.liu.se", |
| 85 | SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc }, |
| 86 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 87 | |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 88 | { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | }; |
| 90 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 91 | /*--*/ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 93 | u_int |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 94 | cipher_blocksize(Cipher *c) |
| 95 | { |
| 96 | return (c->block_size); |
| 97 | } |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 98 | u_int |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 99 | cipher_keylen(Cipher *c) |
| 100 | { |
| 101 | return (c->key_len); |
| 102 | } |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 103 | u_int |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 104 | cipher_get_number(Cipher *c) |
| 105 | { |
| 106 | return (c->number); |
| 107 | } |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 108 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 109 | u_int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 110 | cipher_mask_ssh1(int client) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 111 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 112 | u_int mask = 0; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 113 | mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 114 | mask |= 1 << SSH_CIPHER_BLOWFISH; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 115 | if (client) { |
| 116 | mask |= 1 << SSH_CIPHER_DES; |
| 117 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 118 | return mask; |
| 119 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 120 | |
| 121 | Cipher * |
| 122 | cipher_by_name(const char *name) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 123 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 124 | Cipher *c; |
| 125 | for (c = ciphers; c->name != NULL; c++) |
| 126 | if (strcasecmp(c->name, name) == 0) |
| 127 | return c; |
| 128 | return NULL; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 129 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 130 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 131 | Cipher * |
| 132 | cipher_by_number(int id) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 134 | Cipher *c; |
| 135 | for (c = ciphers; c->name != NULL; c++) |
| 136 | if (c->number == id) |
| 137 | return c; |
| 138 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 139 | } |
| 140 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 141 | #define CIPHER_SEP "," |
| 142 | int |
| 143 | ciphers_valid(const char *names) |
| 144 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 145 | Cipher *c; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 146 | char *ciphers, *cp; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 147 | char *p; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 148 | |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 149 | if (names == NULL || strcmp(names, "") == 0) |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 150 | return 0; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 151 | ciphers = cp = xstrdup(names); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 152 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 153 | (p = strsep(&cp, CIPHER_SEP))) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 154 | c = cipher_by_name(p); |
| 155 | if (c == NULL || c->number != SSH_CIPHER_SSH2) { |
| 156 | debug("bad cipher %s [%s]", p, names); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 157 | xfree(ciphers); |
| 158 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 159 | } else { |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 160 | debug3("cipher ok: %s [%s]", p, names); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 161 | } |
| 162 | } |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 163 | debug3("ciphers ok: [%s]", names); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 164 | xfree(ciphers); |
| 165 | return 1; |
| 166 | } |
| 167 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 168 | /* |
| 169 | * Parses the name of the cipher. Returns the number of the corresponding |
| 170 | * cipher, or -1 on error. |
| 171 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 172 | |
| 173 | int |
| 174 | cipher_number(const char *name) |
| 175 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 176 | Cipher *c; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 177 | if (name == NULL) |
| 178 | return -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 179 | c = cipher_by_name(name); |
| 180 | return (c==NULL) ? -1 : c->number; |
| 181 | } |
| 182 | |
| 183 | char * |
| 184 | cipher_name(int id) |
| 185 | { |
| 186 | Cipher *c = cipher_by_number(id); |
| 187 | return (c==NULL) ? "<unknown>" : c->name; |
| 188 | } |
| 189 | |
| 190 | void |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 191 | cipher_init(CipherContext *cc, Cipher *cipher, |
| 192 | const u_char *key, u_int keylen, const u_char *iv, u_int ivlen, |
| 193 | int encrypt) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 194 | { |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 195 | static int dowarn = 1; |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 196 | #ifdef SSH_OLD_EVP |
| 197 | EVP_CIPHER *type; |
| 198 | #else |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 199 | const EVP_CIPHER *type; |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 200 | #endif |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 201 | int klen; |
| 202 | |
| 203 | if (cipher->number == SSH_CIPHER_DES) { |
| 204 | if (dowarn) { |
| 205 | error("Warning: use of DES is strongly discouraged " |
| 206 | "due to cryptographic weaknesses"); |
| 207 | dowarn = 0; |
| 208 | } |
| 209 | if (keylen > 8) |
| 210 | keylen = 8; |
| 211 | } |
| 212 | cc->plaintext = (cipher->number == SSH_CIPHER_NONE); |
| 213 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 214 | if (keylen < cipher->key_len) |
| 215 | fatal("cipher_init: key length %d is insufficient for %s.", |
| 216 | keylen, cipher->name); |
| 217 | if (iv != NULL && ivlen < cipher->block_size) |
| 218 | fatal("cipher_init: iv length %d is insufficient for %s.", |
| 219 | ivlen, cipher->name); |
| 220 | cc->cipher = cipher; |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 221 | |
| 222 | type = (*cipher->evptype)(); |
| 223 | |
| 224 | EVP_CIPHER_CTX_init(&cc->evp); |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 225 | #ifdef SSH_OLD_EVP |
| 226 | if (type->key_len > 0 && type->key_len != keylen) { |
| 227 | debug("cipher_init: set keylen (%d -> %d)", |
| 228 | type->key_len, keylen); |
| 229 | type->key_len = keylen; |
| 230 | } |
| 231 | EVP_CipherInit(&cc->evp, type, (u_char *)key, (u_char *)iv, |
| 232 | (encrypt == CIPHER_ENCRYPT)); |
| 233 | #else |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 234 | if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv, |
| 235 | (encrypt == CIPHER_ENCRYPT)) == 0) |
| 236 | fatal("cipher_init: EVP_CipherInit failed for %s", |
| 237 | cipher->name); |
| 238 | klen = EVP_CIPHER_CTX_key_length(&cc->evp); |
| 239 | if (klen > 0 && keylen != klen) { |
| 240 | debug("cipher_init: set keylen (%d -> %d)", klen, keylen); |
| 241 | if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0) |
| 242 | fatal("cipher_init: set keylen failed (%d -> %d)", |
| 243 | klen, keylen); |
| 244 | } |
| 245 | if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0) |
| 246 | fatal("cipher_init: EVP_CipherInit: set key failed for %s", |
| 247 | cipher->name); |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 248 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | void |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 252 | cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 253 | { |
| 254 | if (len % cc->cipher->block_size) |
| 255 | fatal("cipher_encrypt: bad plaintext length %d", len); |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 256 | #ifdef SSH_OLD_EVP |
| 257 | EVP_Cipher(&cc->evp, dest, (u_char *)src, len); |
| 258 | #else |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 259 | if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0) |
| 260 | fatal("evp_crypt: EVP_Cipher failed"); |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 261 | #endif |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 265 | cipher_cleanup(CipherContext *cc) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 266 | { |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 267 | #ifdef SSH_OLD_EVP |
| 268 | EVP_CIPHER_CTX_cleanup(&cc->evp); |
| 269 | #else |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 270 | if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0) |
| 271 | error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed"); |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 272 | #endif |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 273 | } |
| 274 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 275 | /* |
| 276 | * Selects the cipher, and keys if by computing the MD5 checksum of the |
| 277 | * passphrase and using the resulting 16 bytes as the key. |
| 278 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 279 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 280 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 281 | cipher_set_key_string(CipherContext *cc, Cipher *cipher, |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 282 | const char *passphrase, int encrypt) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 283 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 284 | MD5_CTX md; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 285 | u_char digest[16]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 286 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 287 | MD5_Init(&md); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 288 | MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 289 | MD5_Final(digest, &md); |
| 290 | |
Damien Miller | 963f6b2 | 2002-02-19 15:21:23 +1100 | [diff] [blame] | 291 | cipher_init(cc, cipher, digest, 16, NULL, 0, encrypt); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 292 | |
| 293 | memset(digest, 0, sizeof(digest)); |
| 294 | memset(&md, 0, sizeof(md)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 295 | } |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 296 | |
| 297 | /* Implementations for other non-EVP ciphers */ |
| 298 | |
| 299 | /* |
| 300 | * This is used by SSH1: |
| 301 | * |
| 302 | * What kind of triple DES are these 2 routines? |
| 303 | * |
| 304 | * Why is there a redundant initialization vector? |
| 305 | * |
| 306 | * If only iv3 was used, then, this would till effect have been |
| 307 | * outer-cbc. However, there is also a private iv1 == iv2 which |
| 308 | * perhaps makes differential analysis easier. On the other hand, the |
| 309 | * private iv1 probably makes the CRC-32 attack ineffective. This is a |
| 310 | * result of that there is no longer any known iv1 to use when |
| 311 | * choosing the X block. |
| 312 | */ |
| 313 | struct ssh1_3des_ctx |
| 314 | { |
| 315 | EVP_CIPHER_CTX k1, k2, k3; |
| 316 | }; |
| 317 | static int |
| 318 | ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, |
| 319 | int enc) |
| 320 | { |
| 321 | struct ssh1_3des_ctx *c; |
| 322 | u_char *k1, *k2, *k3; |
| 323 | |
| 324 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
| 325 | c = xmalloc(sizeof(*c)); |
| 326 | EVP_CIPHER_CTX_set_app_data(ctx, c); |
| 327 | } |
| 328 | if (key == NULL) |
| 329 | return (1); |
| 330 | if (enc == -1) |
| 331 | enc = ctx->encrypt; |
| 332 | k1 = k2 = k3 = (u_char *) key; |
| 333 | k2 += 8; |
| 334 | if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) { |
| 335 | if (enc) |
| 336 | k3 += 16; |
| 337 | else |
| 338 | k1 += 16; |
| 339 | } |
| 340 | EVP_CIPHER_CTX_init(&c->k1); |
| 341 | EVP_CIPHER_CTX_init(&c->k2); |
| 342 | EVP_CIPHER_CTX_init(&c->k3); |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 343 | #ifdef SSH_OLD_EVP |
| 344 | EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc); |
| 345 | EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc); |
| 346 | EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc); |
| 347 | #else |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 348 | if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 || |
| 349 | EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 || |
| 350 | EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) { |
| 351 | memset(c, 0, sizeof(*c)); |
| 352 | xfree(c); |
| 353 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
| 354 | return (0); |
| 355 | } |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 356 | #endif |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 357 | return (1); |
| 358 | } |
| 359 | static int |
| 360 | ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len) |
| 361 | { |
| 362 | struct ssh1_3des_ctx *c; |
| 363 | |
| 364 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
| 365 | error("ssh1_3des_cbc: no context"); |
| 366 | return (0); |
| 367 | } |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 368 | #ifdef SSH_OLD_EVP |
| 369 | EVP_Cipher(&c->k1, dest, (u_char *)src, len); |
| 370 | EVP_Cipher(&c->k2, dest, dest, len); |
| 371 | EVP_Cipher(&c->k3, dest, dest, len); |
| 372 | #else |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 373 | if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 || |
| 374 | EVP_Cipher(&c->k2, dest, dest, len) == 0 || |
| 375 | EVP_Cipher(&c->k3, dest, dest, len) == 0) |
| 376 | return (0); |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 377 | #endif |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 378 | return (1); |
| 379 | } |
| 380 | static int |
| 381 | ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx) |
| 382 | { |
| 383 | struct ssh1_3des_ctx *c; |
| 384 | |
| 385 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { |
| 386 | memset(c, 0, sizeof(*c)); |
| 387 | xfree(c); |
| 388 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
| 389 | } |
| 390 | return (1); |
| 391 | } |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 392 | static const EVP_CIPHER * |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 393 | evp_ssh1_3des(void) |
| 394 | { |
| 395 | static EVP_CIPHER ssh1_3des; |
| 396 | |
| 397 | memset(&ssh1_3des, 0, sizeof(EVP_CIPHER)); |
| 398 | ssh1_3des.nid = NID_undef; |
| 399 | ssh1_3des.block_size = 8; |
| 400 | ssh1_3des.iv_len = 0; |
| 401 | ssh1_3des.key_len = 16; |
| 402 | ssh1_3des.init = ssh1_3des_init; |
| 403 | ssh1_3des.cleanup = ssh1_3des_cleanup; |
| 404 | ssh1_3des.do_cipher = ssh1_3des_cbc; |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 405 | #ifndef SSH_OLD_EVP |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 406 | ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH; |
Damien Miller | c7375ac | 2002-03-11 10:51:17 +1100 | [diff] [blame] | 407 | #endif |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 408 | return (&ssh1_3des); |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * SSH1 uses a variation on Blowfish, all bytes must be swapped before |
| 413 | * and after encryption/decryption. Thus the swap_bytes stuff (yuk). |
| 414 | */ |
| 415 | static void |
| 416 | swap_bytes(const u_char *src, u_char *dst, int n) |
| 417 | { |
| 418 | u_char c[4]; |
| 419 | |
| 420 | /* Process 4 bytes every lap. */ |
| 421 | for (n = n / 4; n > 0; n--) { |
| 422 | c[3] = *src++; |
| 423 | c[2] = *src++; |
| 424 | c[1] = *src++; |
| 425 | c[0] = *src++; |
| 426 | |
| 427 | *dst++ = c[0]; |
| 428 | *dst++ = c[1]; |
| 429 | *dst++ = c[2]; |
| 430 | *dst++ = c[3]; |
| 431 | } |
| 432 | } |
| 433 | static int (*orig_bf)(EVP_CIPHER_CTX *, u_char *, const u_char *, u_int) = NULL; |
| 434 | static int |
| 435 | bf_ssh1_cipher(EVP_CIPHER_CTX *ctx, u_char *out, const u_char *in, u_int len) |
| 436 | { |
| 437 | int ret; |
| 438 | |
| 439 | swap_bytes(in, out, len); |
| 440 | ret = (*orig_bf)(ctx, out, out, len); |
| 441 | swap_bytes(out, out, len); |
| 442 | return (ret); |
| 443 | } |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 444 | static const EVP_CIPHER * |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 445 | evp_ssh1_bf(void) |
| 446 | { |
| 447 | static EVP_CIPHER ssh1_bf; |
| 448 | |
| 449 | memcpy(&ssh1_bf, EVP_bf_cbc(), sizeof(EVP_CIPHER)); |
| 450 | orig_bf = ssh1_bf.do_cipher; |
| 451 | ssh1_bf.nid = NID_undef; |
| 452 | ssh1_bf.do_cipher = bf_ssh1_cipher; |
| 453 | ssh1_bf.key_len = 32; |
| 454 | return (&ssh1_bf); |
| 455 | } |
| 456 | |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 457 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 458 | /* RIJNDAEL */ |
| 459 | #define RIJNDAEL_BLOCKSIZE 16 |
| 460 | struct ssh_rijndael_ctx |
| 461 | { |
| 462 | rijndael_ctx r_ctx; |
| 463 | u_char r_iv[RIJNDAEL_BLOCKSIZE]; |
| 464 | }; |
| 465 | |
| 466 | static int |
| 467 | ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, |
| 468 | int enc) |
| 469 | { |
| 470 | struct ssh_rijndael_ctx *c; |
| 471 | |
| 472 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
| 473 | c = xmalloc(sizeof(*c)); |
| 474 | EVP_CIPHER_CTX_set_app_data(ctx, c); |
| 475 | } |
| 476 | if (key != NULL) { |
| 477 | if (enc == -1) |
| 478 | enc = ctx->encrypt; |
| 479 | rijndael_set_key(&c->r_ctx, (u_char *)key, |
| 480 | 8*EVP_CIPHER_CTX_key_length(ctx), enc); |
| 481 | } |
| 482 | if (iv != NULL) |
| 483 | memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE); |
| 484 | return (1); |
| 485 | } |
| 486 | static int |
| 487 | ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, |
| 488 | u_int len) |
| 489 | { |
| 490 | struct ssh_rijndael_ctx *c; |
| 491 | u_char buf[RIJNDAEL_BLOCKSIZE]; |
| 492 | u_char *cprev, *cnow, *plain, *ivp; |
| 493 | int i, j, blocks = len / RIJNDAEL_BLOCKSIZE; |
| 494 | |
| 495 | if (len == 0) |
| 496 | return (1); |
| 497 | if (len % RIJNDAEL_BLOCKSIZE) |
| 498 | fatal("ssh_rijndael_cbc: bad len %d", len); |
| 499 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
| 500 | error("ssh_rijndael_cbc: no context"); |
| 501 | return (0); |
| 502 | } |
| 503 | if (ctx->encrypt) { |
| 504 | cnow = dest; |
| 505 | plain = (u_char *)src; |
| 506 | cprev = c->r_iv; |
| 507 | for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE, |
| 508 | cnow+=RIJNDAEL_BLOCKSIZE) { |
| 509 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) |
| 510 | buf[j] = plain[j] ^ cprev[j]; |
| 511 | rijndael_encrypt(&c->r_ctx, buf, cnow); |
| 512 | cprev = cnow; |
| 513 | } |
| 514 | memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE); |
| 515 | } else { |
| 516 | cnow = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE); |
| 517 | plain = dest+len-RIJNDAEL_BLOCKSIZE; |
| 518 | |
| 519 | memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE); |
| 520 | for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE, |
| 521 | plain-=RIJNDAEL_BLOCKSIZE) { |
| 522 | rijndael_decrypt(&c->r_ctx, cnow, plain); |
Tim Rice | f29a653 | 2002-03-22 13:27:40 -0800 | [diff] [blame] | 523 | ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE; |
| 524 | for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++) |
| 525 | plain[j] ^= ivp[j]; |
| 526 | } |
| 527 | memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE); |
| 528 | } |
| 529 | return (1); |
| 530 | } |
| 531 | static int |
| 532 | ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx) |
| 533 | { |
| 534 | struct ssh_rijndael_ctx *c; |
| 535 | |
| 536 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { |
| 537 | memset(c, 0, sizeof(*c)); |
| 538 | xfree(c); |
| 539 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
| 540 | } |
| 541 | return (1); |
| 542 | } |
Ben Lindstrom | 6a24641 | 2002-06-06 19:48:16 +0000 | [diff] [blame] | 543 | static const EVP_CIPHER * |
Tim Rice | f29a653 | 2002-03-22 13:27:40 -0800 | [diff] [blame] | 544 | evp_rijndael(void) |
| 545 | { |
| 546 | static EVP_CIPHER rijndal_cbc; |
| 547 | |
| 548 | memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER)); |
| 549 | rijndal_cbc.nid = NID_undef; |
| 550 | rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE; |
| 551 | rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE; |
| 552 | rijndal_cbc.key_len = 16; |
| 553 | rijndal_cbc.init = ssh_rijndael_init; |
| 554 | rijndal_cbc.cleanup = ssh_rijndael_cleanup; |
| 555 | rijndal_cbc.do_cipher = ssh_rijndael_cbc; |
| 556 | #ifndef SSH_OLD_EVP |
| 557 | rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH | |
| 558 | EVP_CIPH_ALWAYS_CALL_INIT; |
| 559 | #endif |
| 560 | return (&rijndal_cbc); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 561 | } |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 562 | #endif |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 563 | |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 564 | /* |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 565 | * Exports an IV from the CipherContext required to export the key |
| 566 | * state back from the unprivileged child to the privileged parent |
| 567 | * process. |
| 568 | */ |
| 569 | |
| 570 | int |
| 571 | cipher_get_keyiv_len(CipherContext *cc) |
| 572 | { |
| 573 | Cipher *c = cc->cipher; |
| 574 | int ivlen; |
| 575 | |
| 576 | if (c->number == SSH_CIPHER_3DES) |
| 577 | ivlen = 24; |
| 578 | else |
| 579 | ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp); |
| 580 | return (ivlen); |
| 581 | } |
| 582 | |
| 583 | void |
| 584 | cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len) |
| 585 | { |
| 586 | Cipher *c = cc->cipher; |
| 587 | u_char *civ = NULL; |
| 588 | int evplen; |
| 589 | |
| 590 | switch (c->number) { |
| 591 | case SSH_CIPHER_SSH2: |
| 592 | case SSH_CIPHER_DES: |
| 593 | case SSH_CIPHER_BLOWFISH: |
| 594 | evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); |
| 595 | if (evplen == 0) |
| 596 | return; |
| 597 | if (evplen != len) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 598 | fatal("%s: wrong iv length %d != %d", __func__, |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 599 | evplen, len); |
| 600 | |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 601 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
Ben Lindstrom | 8a725a8 | 2002-04-04 22:10:38 +0000 | [diff] [blame] | 602 | if (c->evptype == evp_rijndael) { |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 603 | struct ssh_rijndael_ctx *aesc; |
| 604 | |
| 605 | aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp); |
| 606 | if (aesc == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 607 | fatal("%s: no rijndael context", __func__); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 608 | civ = aesc->r_iv; |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 609 | } else |
| 610 | #endif |
| 611 | { |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 612 | civ = cc->evp.iv; |
| 613 | } |
| 614 | break; |
| 615 | case SSH_CIPHER_3DES: { |
| 616 | struct ssh1_3des_ctx *desc; |
| 617 | if (len != 24) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 618 | fatal("%s: bad 3des iv length: %d", __func__, len); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 619 | desc = EVP_CIPHER_CTX_get_app_data(&cc->evp); |
| 620 | if (desc == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 621 | fatal("%s: no 3des context", __func__); |
| 622 | debug3("%s: Copying 3DES IV", __func__); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 623 | memcpy(iv, desc->k1.iv, 8); |
| 624 | memcpy(iv + 8, desc->k2.iv, 8); |
| 625 | memcpy(iv + 16, desc->k3.iv, 8); |
| 626 | return; |
| 627 | } |
| 628 | default: |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 629 | fatal("%s: bad cipher %d", __func__, c->number); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 630 | } |
| 631 | memcpy(iv, civ, len); |
| 632 | } |
| 633 | |
| 634 | void |
| 635 | cipher_set_keyiv(CipherContext *cc, u_char *iv) |
| 636 | { |
| 637 | Cipher *c = cc->cipher; |
| 638 | u_char *div = NULL; |
| 639 | int evplen = 0; |
| 640 | |
| 641 | switch (c->number) { |
| 642 | case SSH_CIPHER_SSH2: |
| 643 | case SSH_CIPHER_DES: |
| 644 | case SSH_CIPHER_BLOWFISH: |
| 645 | evplen = EVP_CIPHER_CTX_iv_length(&cc->evp); |
| 646 | if (evplen == 0) |
| 647 | return; |
| 648 | |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 649 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
Ben Lindstrom | 8a725a8 | 2002-04-04 22:10:38 +0000 | [diff] [blame] | 650 | if (c->evptype == evp_rijndael) { |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 651 | struct ssh_rijndael_ctx *aesc; |
| 652 | |
| 653 | aesc = EVP_CIPHER_CTX_get_app_data(&cc->evp); |
| 654 | if (aesc == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 655 | fatal("%s: no rijndael context", __func__); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 656 | div = aesc->r_iv; |
Ben Lindstrom | f088f43 | 2002-06-06 20:50:07 +0000 | [diff] [blame] | 657 | } else |
| 658 | #endif |
| 659 | { |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 660 | div = cc->evp.iv; |
| 661 | } |
| 662 | break; |
| 663 | case SSH_CIPHER_3DES: { |
| 664 | struct ssh1_3des_ctx *desc; |
| 665 | desc = EVP_CIPHER_CTX_get_app_data(&cc->evp); |
| 666 | if (desc == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 667 | fatal("%s: no 3des context", __func__); |
| 668 | debug3("%s: Installed 3DES IV", __func__); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 669 | memcpy(desc->k1.iv, iv, 8); |
| 670 | memcpy(desc->k2.iv, iv + 8, 8); |
| 671 | memcpy(desc->k3.iv, iv + 16, 8); |
| 672 | return; |
Ben Lindstrom | 6328ab3 | 2002-03-22 02:54:23 +0000 | [diff] [blame] | 673 | } |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 674 | default: |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 675 | fatal("%s: bad cipher %d", __func__, c->number); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 676 | } |
| 677 | memcpy(div, iv, evplen); |
| 678 | } |
| 679 | |
| 680 | #if OPENSSL_VERSION_NUMBER < 0x00907000L |
| 681 | #define EVP_X_STATE(evp) &(evp).c |
| 682 | #define EVP_X_STATE_LEN(evp) sizeof((evp).c) |
| 683 | #else |
| 684 | #define EVP_X_STATE(evp) (evp).cipher_data |
| 685 | #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size |
| 686 | #endif |
| 687 | |
| 688 | int |
| 689 | cipher_get_keycontext(CipherContext *cc, u_char *dat) |
| 690 | { |
| 691 | Cipher *c = cc->cipher; |
| 692 | int plen; |
| 693 | |
| 694 | if (c->number == SSH_CIPHER_3DES) { |
| 695 | struct ssh1_3des_ctx *desc; |
| 696 | desc = EVP_CIPHER_CTX_get_app_data(&cc->evp); |
| 697 | if (desc == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 698 | fatal("%s: no 3des context", __func__); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 699 | plen = EVP_X_STATE_LEN(desc->k1); |
| 700 | if (dat == NULL) |
| 701 | return (3*plen); |
| 702 | memcpy(dat, EVP_X_STATE(desc->k1), plen); |
| 703 | memcpy(dat + plen, EVP_X_STATE(desc->k2), plen); |
| 704 | memcpy(dat + 2*plen, EVP_X_STATE(desc->k3), plen); |
| 705 | return (3*plen); |
| 706 | } |
| 707 | |
| 708 | /* Generic EVP */ |
| 709 | plen = EVP_X_STATE_LEN(cc->evp); |
| 710 | if (dat == NULL) |
| 711 | return (plen); |
| 712 | |
| 713 | memcpy(dat, EVP_X_STATE(cc->evp), plen); |
| 714 | return (plen); |
| 715 | } |
| 716 | |
| 717 | void |
| 718 | cipher_set_keycontext(CipherContext *cc, u_char *dat) |
| 719 | { |
| 720 | Cipher *c = cc->cipher; |
| 721 | int plen; |
| 722 | |
| 723 | if (c->number == SSH_CIPHER_3DES) { |
| 724 | struct ssh1_3des_ctx *desc; |
| 725 | desc = EVP_CIPHER_CTX_get_app_data(&cc->evp); |
| 726 | if (desc == NULL) |
Ben Lindstrom | 7d9c38f | 2002-06-06 21:40:51 +0000 | [diff] [blame] | 727 | fatal("%s: no 3des context", __func__); |
Ben Lindstrom | 212faca | 2002-03-22 01:39:44 +0000 | [diff] [blame] | 728 | plen = EVP_X_STATE_LEN(desc->k1); |
| 729 | memcpy(EVP_X_STATE(desc->k1), dat, plen); |
| 730 | memcpy(EVP_X_STATE(desc->k2), dat + plen, plen); |
| 731 | memcpy(EVP_X_STATE(desc->k3), dat + 2*plen, plen); |
| 732 | } else { |
| 733 | plen = EVP_X_STATE_LEN(cc->evp); |
| 734 | memcpy(EVP_X_STATE(cc->evp), dat, plen); |
| 735 | } |
Damien Miller | 21cf4e0 | 2002-02-19 15:26:42 +1100 | [diff] [blame] | 736 | } |