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. |
| 14 | * Copyright (c) 1999,2000 Markus Friedl. All rights reserved. |
| 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 | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 38 | RCSID("$OpenBSD: cipher.c,v 1.42 2001/01/21 19:05:46 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 | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 45 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 46 | |
| 47 | /* no encryption */ |
| 48 | void |
| 49 | none_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 50 | { |
| 51 | } |
| 52 | void |
| 53 | none_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) |
| 54 | { |
| 55 | } |
| 56 | void |
| 57 | none_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 58 | { |
| 59 | memcpy(dest, src, len); |
| 60 | } |
| 61 | |
| 62 | /* DES */ |
| 63 | void |
| 64 | des_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 65 | { |
| 66 | static int dowarn = 1; |
| 67 | if (dowarn) { |
| 68 | error("Warning: use of DES is strongly discouraged " |
| 69 | "due to cryptographic weaknesses"); |
| 70 | dowarn = 0; |
| 71 | } |
| 72 | des_set_key((void *)key, cc->u.des.key); |
| 73 | } |
| 74 | void |
| 75 | des_ssh1_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) |
| 76 | { |
| 77 | memset(cc->u.des.iv, 0, sizeof(cc->u.des.iv)); |
| 78 | } |
| 79 | void |
| 80 | des_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 81 | { |
| 82 | des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv, |
| 83 | DES_ENCRYPT); |
| 84 | } |
| 85 | void |
| 86 | des_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 87 | { |
| 88 | des_ncbc_encrypt(src, dest, len, cc->u.des.key, &cc->u.des.iv, |
| 89 | DES_DECRYPT); |
| 90 | } |
| 91 | |
| 92 | /* 3DES */ |
| 93 | void |
| 94 | des3_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 95 | { |
| 96 | des_set_key((void *) key, cc->u.des3.key1); |
| 97 | des_set_key((void *) (key+8), cc->u.des3.key2); |
| 98 | des_set_key((void *) (key+16), cc->u.des3.key3); |
| 99 | } |
| 100 | void |
| 101 | des3_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) |
| 102 | { |
| 103 | memset(cc->u.des3.iv2, 0, sizeof(cc->u.des3.iv2)); |
| 104 | memset(cc->u.des3.iv3, 0, sizeof(cc->u.des3.iv3)); |
| 105 | if (iv == NULL) |
| 106 | return; |
| 107 | memcpy(cc->u.des3.iv3, (char *)iv, 8); |
| 108 | } |
| 109 | void |
| 110 | des3_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 111 | { |
| 112 | des_ede3_cbc_encrypt(src, dest, len, |
| 113 | cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3, |
| 114 | &cc->u.des3.iv3, DES_ENCRYPT); |
| 115 | } |
| 116 | void |
| 117 | des3_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 118 | { |
| 119 | des_ede3_cbc_encrypt(src, dest, len, |
| 120 | cc->u.des3.key1, cc->u.des3.key2, cc->u.des3.key3, |
| 121 | &cc->u.des3.iv3, DES_DECRYPT); |
| 122 | } |
| 123 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 124 | /* |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 125 | * This is used by SSH1: |
| 126 | * |
| 127 | * What kind of triple DES are these 2 routines? |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 128 | * |
| 129 | * Why is there a redundant initialization vector? |
| 130 | * |
| 131 | * If only iv3 was used, then, this would till effect have been |
| 132 | * outer-cbc. However, there is also a private iv1 == iv2 which |
| 133 | * perhaps makes differential analysis easier. On the other hand, the |
| 134 | * private iv1 probably makes the CRC-32 attack ineffective. This is a |
| 135 | * result of that there is no longer any known iv1 to use when |
| 136 | * choosing the X block. |
| 137 | */ |
| 138 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 139 | des3_ssh1_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 140 | { |
| 141 | des_set_key((void *) key, cc->u.des3.key1); |
| 142 | des_set_key((void *) (key+8), cc->u.des3.key2); |
| 143 | if (keylen <= 16) |
| 144 | des_set_key((void *) key, cc->u.des3.key3); |
| 145 | else |
| 146 | des_set_key((void *) (key+16), cc->u.des3.key3); |
| 147 | } |
| 148 | void |
| 149 | des3_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 150 | u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 152 | des_cblock iv1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 153 | des_cblock *iv2 = &cc->u.des3.iv2; |
| 154 | des_cblock *iv3 = &cc->u.des3.iv3; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 156 | memcpy(&iv1, iv2, 8); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 157 | |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 158 | des_ncbc_encrypt(src, dest, len, cc->u.des3.key1, &iv1, DES_ENCRYPT); |
| 159 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_DECRYPT); |
| 160 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key3, iv3, DES_ENCRYPT); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 163 | des3_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 164 | u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 165 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 166 | des_cblock iv1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 167 | des_cblock *iv2 = &cc->u.des3.iv2; |
| 168 | des_cblock *iv3 = &cc->u.des3.iv3; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 169 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 170 | memcpy(&iv1, iv2, 8); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | |
Ben Lindstrom | c72745a | 2000-12-02 19:03:54 +0000 | [diff] [blame] | 172 | des_ncbc_encrypt(src, dest, len, cc->u.des3.key3, iv3, DES_DECRYPT); |
| 173 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key2, iv2, DES_ENCRYPT); |
| 174 | des_ncbc_encrypt(dest, dest, len, cc->u.des3.key1, &iv1, DES_DECRYPT); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 175 | } |
| 176 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 177 | /* Blowfish */ |
| 178 | void |
| 179 | blowfish_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 180 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 181 | BF_set_key(&cc->u.bf.key, keylen, (u_char *)key); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 182 | } |
| 183 | void |
| 184 | blowfish_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) |
| 185 | { |
| 186 | if (iv == NULL) |
| 187 | memset(cc->u.bf.iv, 0, 8); |
| 188 | else |
| 189 | memcpy(cc->u.bf.iv, (char *)iv, 8); |
| 190 | } |
| 191 | void |
| 192 | blowfish_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 193 | u_int len) |
| 194 | { |
| 195 | BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv, |
| 196 | BF_ENCRYPT); |
| 197 | } |
| 198 | void |
| 199 | blowfish_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 200 | u_int len) |
| 201 | { |
| 202 | BF_cbc_encrypt((void *)src, dest, len, &cc->u.bf.key, cc->u.bf.iv, |
| 203 | BF_DECRYPT); |
| 204 | } |
| 205 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 206 | /* |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 207 | * SSH1 uses a variation on Blowfish, all bytes must be swapped before |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 208 | * and after encryption/decryption. Thus the swap_bytes stuff (yuk). |
| 209 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | static void |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 211 | swap_bytes(const u_char *src, u_char *dst, int n) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 212 | { |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 213 | char c[4]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 214 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 215 | /* Process 4 bytes every lap. */ |
| 216 | for (n = n / 4; n > 0; n--) { |
| 217 | c[3] = *src++; |
| 218 | c[2] = *src++; |
| 219 | c[1] = *src++; |
| 220 | c[0] = *src++; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 221 | |
Damien Miller | 69b69aa | 2000-10-28 14:19:58 +1100 | [diff] [blame] | 222 | *dst++ = c[0]; |
| 223 | *dst++ = c[1]; |
| 224 | *dst++ = c[2]; |
| 225 | *dst++ = c[3]; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 226 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 227 | } |
| 228 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 229 | void |
| 230 | blowfish_ssh1_encrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 231 | u_int len) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 232 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 233 | swap_bytes(src, dest, len); |
| 234 | BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv, |
| 235 | BF_ENCRYPT); |
| 236 | swap_bytes(dest, dest, len); |
| 237 | } |
| 238 | void |
| 239 | blowfish_ssh1_decrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 240 | u_int len) |
| 241 | { |
| 242 | swap_bytes(src, dest, len); |
| 243 | BF_cbc_encrypt((void *)dest, dest, len, &cc->u.bf.key, cc->u.bf.iv, |
| 244 | BF_DECRYPT); |
| 245 | swap_bytes(dest, dest, len); |
| 246 | } |
| 247 | |
| 248 | /* alleged rc4 */ |
| 249 | void |
| 250 | arcfour_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 251 | { |
| 252 | RC4_set_key(&cc->u.rc4, keylen, (u_char *)key); |
| 253 | } |
| 254 | void |
| 255 | arcfour_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 256 | { |
| 257 | RC4(&cc->u.rc4, len, (u_char *)src, dest); |
| 258 | } |
| 259 | |
| 260 | /* CAST */ |
| 261 | void |
| 262 | cast_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 263 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 264 | CAST_set_key(&cc->u.cast.key, keylen, (u_char *) key); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 265 | } |
| 266 | void |
| 267 | cast_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) |
| 268 | { |
| 269 | if (iv == NULL) |
| 270 | fatal("no IV for %s.", cc->cipher->name); |
| 271 | memcpy(cc->u.cast.iv, (char *)iv, 8); |
| 272 | } |
| 273 | void |
| 274 | cast_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 275 | { |
| 276 | CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv, |
| 277 | CAST_ENCRYPT); |
| 278 | } |
| 279 | void |
| 280 | cast_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 281 | { |
| 282 | CAST_cbc_encrypt(src, dest, len, &cc->u.cast.key, cc->u.cast.iv, |
| 283 | CAST_DECRYPT); |
| 284 | } |
| 285 | |
| 286 | /* RIJNDAEL */ |
| 287 | |
| 288 | #define RIJNDAEL_BLOCKSIZE 16 |
| 289 | void |
| 290 | rijndael_setkey(CipherContext *cc, const u_char *key, u_int keylen) |
| 291 | { |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 292 | rijndael_set_key(&cc->u.rijndael.enc, (u4byte *)key, 8*keylen, 1); |
| 293 | rijndael_set_key(&cc->u.rijndael.dec, (u4byte *)key, 8*keylen, 0); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 294 | } |
| 295 | void |
| 296 | rijndael_setiv(CipherContext *cc, const u_char *iv, u_int ivlen) |
| 297 | { |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 298 | if (iv == NULL) |
| 299 | fatal("no IV for %s.", cc->cipher->name); |
| 300 | memcpy((u_char *)cc->u.rijndael.iv, iv, RIJNDAEL_BLOCKSIZE); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 301 | } |
| 302 | void |
| 303 | rijndael_cbc_encrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 304 | u_int len) |
| 305 | { |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 306 | rijndael_ctx *ctx = &cc->u.rijndael.enc; |
| 307 | u4byte *iv = cc->u.rijndael.iv; |
| 308 | u4byte in[4]; |
| 309 | u4byte *cprev, *cnow, *plain; |
| 310 | int i, blocks = len / RIJNDAEL_BLOCKSIZE; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 311 | if (len == 0) |
| 312 | return; |
| 313 | if (len % RIJNDAEL_BLOCKSIZE) |
| 314 | fatal("rijndael_cbc_encrypt: bad len %d", len); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 315 | cnow = (u4byte*) dest; |
| 316 | plain = (u4byte*) src; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 317 | cprev = iv; |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 318 | for(i = 0; i < blocks; i++, plain+=4, cnow+=4) { |
| 319 | in[0] = plain[0] ^ cprev[0]; |
| 320 | in[1] = plain[1] ^ cprev[1]; |
| 321 | in[2] = plain[2] ^ cprev[2]; |
| 322 | in[3] = plain[3] ^ cprev[3]; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 323 | rijndael_encrypt(ctx, in, cnow); |
| 324 | cprev = cnow; |
| 325 | } |
| 326 | memcpy(iv, cprev, RIJNDAEL_BLOCKSIZE); |
| 327 | } |
| 328 | |
| 329 | void |
| 330 | rijndael_cbc_decrypt(CipherContext *cc, u_char *dest, const u_char *src, |
| 331 | u_int len) |
| 332 | { |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 333 | rijndael_ctx *ctx = &cc->u.rijndael.dec; |
| 334 | u4byte *iv = cc->u.rijndael.iv; |
| 335 | u4byte ivsaved[4]; |
| 336 | u4byte *cnow = (u4byte*) (src+len-RIJNDAEL_BLOCKSIZE); |
| 337 | u4byte *plain = (u4byte*) (dest+len-RIJNDAEL_BLOCKSIZE); |
| 338 | u4byte *ivp; |
| 339 | int i, blocks = len / RIJNDAEL_BLOCKSIZE; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 340 | if (len == 0) |
| 341 | return; |
| 342 | if (len % RIJNDAEL_BLOCKSIZE) |
| 343 | fatal("rijndael_cbc_decrypt: bad len %d", len); |
| 344 | memcpy(ivsaved, cnow, RIJNDAEL_BLOCKSIZE); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 345 | for(i = blocks; i > 0; i--, cnow-=4, plain-=4) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 346 | rijndael_decrypt(ctx, cnow, plain); |
Ben Lindstrom | fa1b3d0 | 2000-12-10 01:55:37 +0000 | [diff] [blame] | 347 | ivp = (i == 1) ? iv : cnow-4; |
| 348 | plain[0] ^= ivp[0]; |
| 349 | plain[1] ^= ivp[1]; |
| 350 | plain[2] ^= ivp[2]; |
| 351 | plain[3] ^= ivp[3]; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 352 | } |
| 353 | memcpy(iv, ivsaved, RIJNDAEL_BLOCKSIZE); |
| 354 | } |
| 355 | |
| 356 | Cipher ciphers[] = { |
| 357 | { "none", |
| 358 | SSH_CIPHER_NONE, 8, 0, |
| 359 | none_setkey, none_setiv, |
| 360 | none_crypt, none_crypt }, |
| 361 | { "des", |
| 362 | SSH_CIPHER_DES, 8, 8, |
| 363 | des_ssh1_setkey, des_ssh1_setiv, |
| 364 | des_ssh1_encrypt, des_ssh1_decrypt }, |
| 365 | { "3des", |
| 366 | SSH_CIPHER_3DES, 8, 16, |
| 367 | des3_ssh1_setkey, des3_setiv, |
| 368 | des3_ssh1_encrypt, des3_ssh1_decrypt }, |
| 369 | { "blowfish", |
| 370 | SSH_CIPHER_BLOWFISH, 8, 16, |
| 371 | blowfish_setkey, blowfish_setiv, |
| 372 | blowfish_ssh1_encrypt, blowfish_ssh1_decrypt }, |
| 373 | |
| 374 | { "3des-cbc", |
| 375 | SSH_CIPHER_SSH2, 8, 24, |
| 376 | des3_setkey, des3_setiv, |
| 377 | des3_cbc_encrypt, des3_cbc_decrypt }, |
| 378 | { "blowfish-cbc", |
| 379 | SSH_CIPHER_SSH2, 8, 16, |
| 380 | blowfish_setkey, blowfish_setiv, |
| 381 | blowfish_cbc_encrypt, blowfish_cbc_decrypt }, |
| 382 | { "cast128-cbc", |
| 383 | SSH_CIPHER_SSH2, 8, 16, |
| 384 | cast_setkey, cast_setiv, |
| 385 | cast_cbc_encrypt, cast_cbc_decrypt }, |
| 386 | { "arcfour", |
| 387 | SSH_CIPHER_SSH2, 8, 16, |
| 388 | arcfour_setkey, none_setiv, |
| 389 | arcfour_crypt, arcfour_crypt }, |
| 390 | { "aes128-cbc", |
| 391 | SSH_CIPHER_SSH2, 16, 16, |
| 392 | rijndael_setkey, rijndael_setiv, |
| 393 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, |
| 394 | { "aes192-cbc", |
| 395 | SSH_CIPHER_SSH2, 16, 24, |
| 396 | rijndael_setkey, rijndael_setiv, |
| 397 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, |
| 398 | { "aes256-cbc", |
| 399 | SSH_CIPHER_SSH2, 16, 32, |
| 400 | rijndael_setkey, rijndael_setiv, |
| 401 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, |
| 402 | { "rijndael128-cbc", |
| 403 | SSH_CIPHER_SSH2, 16, 16, |
| 404 | rijndael_setkey, rijndael_setiv, |
| 405 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, |
| 406 | { "rijndael192-cbc", |
| 407 | SSH_CIPHER_SSH2, 16, 24, |
| 408 | rijndael_setkey, rijndael_setiv, |
| 409 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, |
| 410 | { "rijndael256-cbc", |
| 411 | SSH_CIPHER_SSH2, 16, 32, |
| 412 | rijndael_setkey, rijndael_setiv, |
| 413 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, |
| 414 | { "rijndael-cbc@lysator.liu.se", |
| 415 | SSH_CIPHER_SSH2, 16, 32, |
| 416 | rijndael_setkey, rijndael_setiv, |
| 417 | rijndael_cbc_encrypt, rijndael_cbc_decrypt }, |
| 418 | { NULL, SSH_CIPHER_ILLEGAL, 0, 0, NULL, NULL, NULL, NULL } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 419 | }; |
| 420 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 421 | /*--*/ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 422 | |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 423 | u_int |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 424 | cipher_mask_ssh1(int client) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 425 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 426 | u_int mask = 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 427 | mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 428 | mask |= 1 << SSH_CIPHER_BLOWFISH; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 429 | if (client) { |
| 430 | mask |= 1 << SSH_CIPHER_DES; |
| 431 | } |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 432 | return mask; |
| 433 | } |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 434 | |
| 435 | Cipher * |
| 436 | cipher_by_name(const char *name) |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 437 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 438 | Cipher *c; |
| 439 | for (c = ciphers; c->name != NULL; c++) |
| 440 | if (strcasecmp(c->name, name) == 0) |
| 441 | return c; |
| 442 | return NULL; |
Damien Miller | 1383bd8 | 2000-04-06 12:32:37 +1000 | [diff] [blame] | 443 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 444 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 445 | Cipher * |
| 446 | cipher_by_number(int id) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 447 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 448 | Cipher *c; |
| 449 | for (c = ciphers; c->name != NULL; c++) |
| 450 | if (c->number == id) |
| 451 | return c; |
| 452 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 453 | } |
| 454 | |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 455 | #define CIPHER_SEP "," |
| 456 | int |
| 457 | ciphers_valid(const char *names) |
| 458 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 459 | Cipher *c; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 460 | char *ciphers, *cp; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 461 | char *p; |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 462 | |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 463 | if (names == NULL || strcmp(names, "") == 0) |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 464 | return 0; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 465 | ciphers = cp = xstrdup(names); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 466 | for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0'; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 467 | (p = strsep(&cp, CIPHER_SEP))) { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 468 | c = cipher_by_name(p); |
| 469 | if (c == NULL || c->number != SSH_CIPHER_SSH2) { |
| 470 | debug("bad cipher %s [%s]", p, names); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 471 | xfree(ciphers); |
| 472 | return 0; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 473 | } else { |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 474 | debug3("cipher ok: %s [%s]", p, names); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 475 | } |
| 476 | } |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 477 | debug3("ciphers ok: [%s]", names); |
Damien Miller | 7892879 | 2000-04-12 20:17:38 +1000 | [diff] [blame] | 478 | xfree(ciphers); |
| 479 | return 1; |
| 480 | } |
| 481 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 482 | /* |
| 483 | * Parses the name of the cipher. Returns the number of the corresponding |
| 484 | * cipher, or -1 on error. |
| 485 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 486 | |
| 487 | int |
| 488 | cipher_number(const char *name) |
| 489 | { |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 490 | Cipher *c; |
Damien Miller | b1715dc | 2000-05-30 13:44:51 +1000 | [diff] [blame] | 491 | if (name == NULL) |
| 492 | return -1; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 493 | c = cipher_by_name(name); |
| 494 | return (c==NULL) ? -1 : c->number; |
| 495 | } |
| 496 | |
| 497 | char * |
| 498 | cipher_name(int id) |
| 499 | { |
| 500 | Cipher *c = cipher_by_number(id); |
| 501 | return (c==NULL) ? "<unknown>" : c->name; |
| 502 | } |
| 503 | |
| 504 | void |
| 505 | cipher_init(CipherContext *cc, Cipher *cipher, |
| 506 | const u_char *key, u_int keylen, const u_char *iv, u_int ivlen) |
| 507 | { |
| 508 | if (keylen < cipher->key_len) |
| 509 | fatal("cipher_init: key length %d is insufficient for %s.", |
| 510 | keylen, cipher->name); |
| 511 | if (iv != NULL && ivlen < cipher->block_size) |
| 512 | fatal("cipher_init: iv length %d is insufficient for %s.", |
| 513 | ivlen, cipher->name); |
| 514 | cc->cipher = cipher; |
| 515 | cipher->setkey(cc, key, keylen); |
| 516 | cipher->setiv(cc, iv, ivlen); |
| 517 | } |
| 518 | |
| 519 | void |
| 520 | cipher_encrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 521 | { |
| 522 | if (len % cc->cipher->block_size) |
| 523 | fatal("cipher_encrypt: bad plaintext length %d", len); |
| 524 | cc->cipher->encrypt(cc, dest, src, len); |
| 525 | } |
| 526 | |
| 527 | void |
| 528 | cipher_decrypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len) |
| 529 | { |
| 530 | if (len % cc->cipher->block_size) |
| 531 | fatal("cipher_decrypt: bad ciphertext length %d", len); |
| 532 | cc->cipher->decrypt(cc, dest, src, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 533 | } |
| 534 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 535 | /* |
| 536 | * Selects the cipher, and keys if by computing the MD5 checksum of the |
| 537 | * passphrase and using the resulting 16 bytes as the key. |
| 538 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 539 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 540 | void |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 541 | cipher_set_key_string(CipherContext *cc, Cipher *cipher, |
| 542 | const char *passphrase) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 543 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 544 | MD5_CTX md; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 545 | u_char digest[16]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 546 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 547 | MD5_Init(&md); |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 548 | MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 549 | MD5_Final(digest, &md); |
| 550 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 551 | cipher_init(cc, cipher, digest, 16, NULL, 0); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 552 | |
| 553 | memset(digest, 0, sizeof(digest)); |
| 554 | memset(&md, 0, sizeof(md)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 555 | } |