Annotation of src/usr.bin/ssh/cipher.c, Revision 1.65.2.2
1.1 deraadt 1: /*
1.30 deraadt 2: * Author: Tatu Ylonen <ylo@cs.hut.fi>
3: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4: * All rights reserved
1.26 markus 5: *
1.30 deraadt 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".
1.26 markus 11: *
12: *
1.30 deraadt 13: * Copyright (c) 1999 Niels Provos. All rights reserved.
1.46 markus 14: * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
1.26 markus 15: *
1.30 deraadt 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.
1.26 markus 24: *
1.30 deraadt 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.
1.17 deraadt 35: */
1.1 deraadt 36:
37: #include "includes.h"
1.65.2.2! brad 38: RCSID("$OpenBSD: cipher.c,v 1.71 2004/07/28 09:40:29 markus Exp $");
1.1 deraadt 39:
1.24 markus 40: #include "xmalloc.h"
1.42 markus 41: #include "log.h"
42: #include "cipher.h"
1.8 deraadt 43:
1.25 markus 44: #include <openssl/md5.h>
1.57 markus 45:
46: #if OPENSSL_VERSION_NUMBER < 0x00907000L
1.64 markus 47: extern const EVP_CIPHER *evp_rijndael(void);
48: extern void ssh_rijndael_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
1.57 markus 49: #endif
1.64 markus 50: extern const EVP_CIPHER *evp_ssh1_bf(void);
51: extern const EVP_CIPHER *evp_ssh1_3des(void);
52: extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
1.65 markus 53: extern const EVP_CIPHER *evp_aes_128_ctr(void);
54: extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
1.65.2.1 brad 55: #define EVP_acss NULL
1.1 deraadt 56:
1.51 markus 57: struct Cipher {
58: char *name;
59: int number; /* for ssh1 only */
60: u_int block_size;
61: u_int key_len;
1.56 markus 62: const EVP_CIPHER *(*evptype)(void);
1.52 markus 63: } ciphers[] = {
1.65.2.2! brad 64: { "none", SSH_CIPHER_NONE, 8, 0, EVP_enc_null },
! 65: { "des", SSH_CIPHER_DES, 8, 8, EVP_des_cbc },
! 66: { "3des", SSH_CIPHER_3DES, 8, 16, evp_ssh1_3des },
! 67: { "blowfish", SSH_CIPHER_BLOWFISH, 8, 32, evp_ssh1_bf },
! 68:
! 69: { "3des-cbc", SSH_CIPHER_SSH2, 8, 24, EVP_des_ede3_cbc },
! 70: { "blowfish-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_bf_cbc },
! 71: { "cast128-cbc", SSH_CIPHER_SSH2, 8, 16, EVP_cast5_cbc },
! 72: { "arcfour", SSH_CIPHER_SSH2, 8, 16, EVP_rc4 },
1.57 markus 73: #if OPENSSL_VERSION_NUMBER < 0x00907000L
1.65.2.2! brad 74: { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, evp_rijndael },
! 75: { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, evp_rijndael },
! 76: { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
1.55 markus 77: { "rijndael-cbc@lysator.liu.se",
78: SSH_CIPHER_SSH2, 16, 32, evp_rijndael },
1.57 markus 79: #else
80: { "aes128-cbc", SSH_CIPHER_SSH2, 16, 16, EVP_aes_128_cbc },
81: { "aes192-cbc", SSH_CIPHER_SSH2, 16, 24, EVP_aes_192_cbc },
82: { "aes256-cbc", SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
83: { "rijndael-cbc@lysator.liu.se",
84: SSH_CIPHER_SSH2, 16, 32, EVP_aes_256_cbc },
85: #endif
1.65.2.2! brad 86: { "aes128-ctr", SSH_CIPHER_SSH2, 16, 16, evp_aes_128_ctr },
! 87: { "aes192-ctr", SSH_CIPHER_SSH2, 16, 24, evp_aes_128_ctr },
! 88: { "aes256-ctr", SSH_CIPHER_SSH2, 16, 32, evp_aes_128_ctr },
1.65.2.1 brad 89: { "acss@openssh.org", SSH_CIPHER_SSH2, 16, 5, EVP_acss },
1.47 markus 90:
1.65.2.2! brad 91: { NULL, SSH_CIPHER_INVALID, 0, 0, NULL }
1.32 markus 92: };
93:
94: /*--*/
1.1 deraadt 95:
1.54 markus 96: u_int
1.65.2.1 brad 97: cipher_blocksize(const Cipher *c)
1.51 markus 98: {
99: return (c->block_size);
100: }
1.60 deraadt 101:
1.54 markus 102: u_int
1.65.2.1 brad 103: cipher_keylen(const Cipher *c)
1.51 markus 104: {
105: return (c->key_len);
106: }
1.60 deraadt 107:
1.54 markus 108: u_int
1.65.2.1 brad 109: cipher_get_number(const Cipher *c)
1.53 markus 110: {
111: return (c->number);
112: }
1.51 markus 113:
1.41 markus 114: u_int
1.34 markus 115: cipher_mask_ssh1(int client)
1.1 deraadt 116: {
1.41 markus 117: u_int mask = 0;
1.48 deraadt 118: mask |= 1 << SSH_CIPHER_3DES; /* Mandatory */
1.34 markus 119: mask |= 1 << SSH_CIPHER_BLOWFISH;
120: if (client) {
121: mask |= 1 << SSH_CIPHER_DES;
1.32 markus 122: }
123: return mask;
1.1 deraadt 124: }
125:
1.32 markus 126: Cipher *
127: cipher_by_name(const char *name)
128: {
129: Cipher *c;
130: for (c = ciphers; c->name != NULL; c++)
131: if (strcasecmp(c->name, name) == 0)
132: return c;
133: return NULL;
134: }
135:
136: Cipher *
137: cipher_by_number(int id)
138: {
139: Cipher *c;
140: for (c = ciphers; c->name != NULL; c++)
141: if (c->number == id)
142: return c;
143: return NULL;
144: }
1.24 markus 145:
146: #define CIPHER_SEP ","
147: int
148: ciphers_valid(const char *names)
149: {
1.32 markus 150: Cipher *c;
1.65.2.2! brad 151: char *cipher_list, *cp;
1.24 markus 152: char *p;
153:
1.27 markus 154: if (names == NULL || strcmp(names, "") == 0)
1.24 markus 155: return 0;
1.65.2.2! brad 156: cipher_list = cp = xstrdup(names);
1.32 markus 157: for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
1.48 deraadt 158: (p = strsep(&cp, CIPHER_SEP))) {
1.32 markus 159: c = cipher_by_name(p);
160: if (c == NULL || c->number != SSH_CIPHER_SSH2) {
161: debug("bad cipher %s [%s]", p, names);
1.65.2.2! brad 162: xfree(cipher_list);
1.24 markus 163: return 0;
1.32 markus 164: } else {
1.36 markus 165: debug3("cipher ok: %s [%s]", p, names);
1.24 markus 166: }
167: }
1.36 markus 168: debug3("ciphers ok: [%s]", names);
1.65.2.2! brad 169: xfree(cipher_list);
1.24 markus 170: return 1;
171: }
172:
1.18 markus 173: /*
174: * Parses the name of the cipher. Returns the number of the corresponding
175: * cipher, or -1 on error.
176: */
1.1 deraadt 177:
1.4 provos 178: int
179: cipher_number(const char *name)
1.1 deraadt 180: {
1.32 markus 181: Cipher *c;
1.27 markus 182: if (name == NULL)
183: return -1;
1.32 markus 184: c = cipher_by_name(name);
185: return (c==NULL) ? -1 : c->number;
1.1 deraadt 186: }
187:
1.32 markus 188: char *
189: cipher_name(int id)
1.1 deraadt 190: {
1.32 markus 191: Cipher *c = cipher_by_number(id);
192: return (c==NULL) ? "<unknown>" : c->name;
1.1 deraadt 193: }
194:
1.26 markus 195: void
1.52 markus 196: cipher_init(CipherContext *cc, Cipher *cipher,
197: const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
1.65.2.2! brad 198: int do_encrypt)
1.16 markus 199: {
1.52 markus 200: static int dowarn = 1;
201: const EVP_CIPHER *type;
202: int klen;
203:
204: if (cipher->number == SSH_CIPHER_DES) {
205: if (dowarn) {
206: error("Warning: use of DES is strongly discouraged "
207: "due to cryptographic weaknesses");
208: dowarn = 0;
209: }
210: if (keylen > 8)
211: keylen = 8;
212: }
213: cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
214:
1.32 markus 215: if (keylen < cipher->key_len)
216: fatal("cipher_init: key length %d is insufficient for %s.",
217: keylen, cipher->name);
218: if (iv != NULL && ivlen < cipher->block_size)
219: fatal("cipher_init: iv length %d is insufficient for %s.",
220: ivlen, cipher->name);
221: cc->cipher = cipher;
1.52 markus 222:
223: type = (*cipher->evptype)();
224:
225: EVP_CIPHER_CTX_init(&cc->evp);
226: if (EVP_CipherInit(&cc->evp, type, NULL, (u_char *)iv,
1.65.2.2! brad 227: (do_encrypt == CIPHER_ENCRYPT)) == 0)
1.52 markus 228: fatal("cipher_init: EVP_CipherInit failed for %s",
229: cipher->name);
230: klen = EVP_CIPHER_CTX_key_length(&cc->evp);
231: if (klen > 0 && keylen != klen) {
1.62 markus 232: debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
1.52 markus 233: if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
234: fatal("cipher_init: set keylen failed (%d -> %d)",
235: klen, keylen);
236: }
237: if (EVP_CipherInit(&cc->evp, NULL, (u_char *)key, NULL, -1) == 0)
238: fatal("cipher_init: EVP_CipherInit: set key failed for %s",
239: cipher->name);
1.1 deraadt 240: }
1.21 markus 241:
1.26 markus 242: void
1.51 markus 243: cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src, u_int len)
1.32 markus 244: {
245: if (len % cc->cipher->block_size)
246: fatal("cipher_encrypt: bad plaintext length %d", len);
1.52 markus 247: if (EVP_Cipher(&cc->evp, dest, (u_char *)src, len) == 0)
248: fatal("evp_crypt: EVP_Cipher failed");
1.21 markus 249: }
250:
1.26 markus 251: void
1.51 markus 252: cipher_cleanup(CipherContext *cc)
1.16 markus 253: {
1.52 markus 254: if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
255: error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
1.16 markus 256: }
1.1 deraadt 257:
1.32 markus 258: /*
259: * Selects the cipher, and keys if by computing the MD5 checksum of the
260: * passphrase and using the resulting 16 bytes as the key.
261: */
1.1 deraadt 262:
1.26 markus 263: void
1.32 markus 264: cipher_set_key_string(CipherContext *cc, Cipher *cipher,
1.65.2.2! brad 265: const char *passphrase, int do_encrypt)
1.16 markus 266: {
1.32 markus 267: MD5_CTX md;
1.41 markus 268: u_char digest[16];
1.32 markus 269:
270: MD5_Init(&md);
271: MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
272: MD5_Final(digest, &md);
1.16 markus 273:
1.65.2.2! brad 274: cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
1.16 markus 275:
1.32 markus 276: memset(digest, 0, sizeof(digest));
277: memset(&md, 0, sizeof(md));
1.52 markus 278: }
1.53 markus 279:
1.54 markus 280: /*
1.53 markus 281: * Exports an IV from the CipherContext required to export the key
282: * state back from the unprivileged child to the privileged parent
283: * process.
284: */
285:
286: int
1.65.2.1 brad 287: cipher_get_keyiv_len(const CipherContext *cc)
1.53 markus 288: {
289: Cipher *c = cc->cipher;
290: int ivlen;
291:
292: if (c->number == SSH_CIPHER_3DES)
293: ivlen = 24;
294: else
295: ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
296: return (ivlen);
297: }
298:
299: void
300: cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
301: {
302: Cipher *c = cc->cipher;
303: int evplen;
304:
305: switch (c->number) {
306: case SSH_CIPHER_SSH2:
307: case SSH_CIPHER_DES:
308: case SSH_CIPHER_BLOWFISH:
309: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
310: if (evplen == 0)
311: return;
312: if (evplen != len)
1.58 markus 313: fatal("%s: wrong iv length %d != %d", __func__,
1.53 markus 314: evplen, len);
1.57 markus 315: #if OPENSSL_VERSION_NUMBER < 0x00907000L
1.63 markus 316: if (c->evptype == evp_rijndael)
317: ssh_rijndael_iv(&cc->evp, 0, iv, len);
318: else
1.57 markus 319: #endif
1.65 markus 320: if (c->evptype == evp_aes_128_ctr)
321: ssh_aes_ctr_iv(&cc->evp, 0, iv, len);
322: else
1.63 markus 323: memcpy(iv, cc->evp.iv, len);
324: break;
325: case SSH_CIPHER_3DES:
326: ssh1_3des_iv(&cc->evp, 0, iv, 24);
1.53 markus 327: break;
328: default:
1.58 markus 329: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 330: }
331: }
332:
333: void
334: cipher_set_keyiv(CipherContext *cc, u_char *iv)
335: {
336: Cipher *c = cc->cipher;
337: int evplen = 0;
338:
339: switch (c->number) {
340: case SSH_CIPHER_SSH2:
341: case SSH_CIPHER_DES:
342: case SSH_CIPHER_BLOWFISH:
343: evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
344: if (evplen == 0)
345: return;
1.57 markus 346: #if OPENSSL_VERSION_NUMBER < 0x00907000L
1.63 markus 347: if (c->evptype == evp_rijndael)
348: ssh_rijndael_iv(&cc->evp, 1, iv, evplen);
349: else
1.57 markus 350: #endif
1.65 markus 351: if (c->evptype == evp_aes_128_ctr)
352: ssh_aes_ctr_iv(&cc->evp, 1, iv, evplen);
353: else
1.63 markus 354: memcpy(cc->evp.iv, iv, evplen);
355: break;
356: case SSH_CIPHER_3DES:
357: ssh1_3des_iv(&cc->evp, 1, iv, 24);
1.53 markus 358: break;
359: default:
1.58 markus 360: fatal("%s: bad cipher %d", __func__, c->number);
1.53 markus 361: }
362: }
363:
364: #if OPENSSL_VERSION_NUMBER < 0x00907000L
365: #define EVP_X_STATE(evp) &(evp).c
366: #define EVP_X_STATE_LEN(evp) sizeof((evp).c)
367: #else
368: #define EVP_X_STATE(evp) (evp).cipher_data
369: #define EVP_X_STATE_LEN(evp) (evp).cipher->ctx_size
370: #endif
371:
372: int
1.65.2.1 brad 373: cipher_get_keycontext(const CipherContext *cc, u_char *dat)
1.53 markus 374: {
375: Cipher *c = cc->cipher;
1.59 markus 376: int plen = 0;
1.53 markus 377:
1.65.2.1 brad 378: if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.59 markus 379: plen = EVP_X_STATE_LEN(cc->evp);
1.53 markus 380: if (dat == NULL)
1.59 markus 381: return (plen);
382: memcpy(dat, EVP_X_STATE(cc->evp), plen);
1.53 markus 383: }
384: return (plen);
385: }
386:
387: void
388: cipher_set_keycontext(CipherContext *cc, u_char *dat)
389: {
390: Cipher *c = cc->cipher;
391: int plen;
392:
1.65.2.1 brad 393: if (c->evptype == EVP_rc4 || c->evptype == EVP_acss) {
1.53 markus 394: plen = EVP_X_STATE_LEN(cc->evp);
395: memcpy(EVP_X_STATE(cc->evp), dat, plen);
396: }
1.1 deraadt 397: }