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