Home | History | Annotate | Line # | Download | only in dist
cipher.c revision 1.3.8.1
      1 /*	$NetBSD: cipher.c,v 1.3.8.1 2013/06/23 06:26:14 tls Exp $	*/
      2 /* $OpenBSD: cipher.c,v 1.87 2013/01/26 06:11:05 djm Exp $ */
      3 /*
      4  * Author: Tatu Ylonen <ylo (at) cs.hut.fi>
      5  * Copyright (c) 1995 Tatu Ylonen <ylo (at) cs.hut.fi>, Espoo, Finland
      6  *                    All rights reserved
      7  *
      8  * As far as I am concerned, the code I have written for this software
      9  * can be used freely for any purpose.  Any derived versions of this
     10  * software must be clearly marked as such, and if the derived work is
     11  * incompatible with the protocol description in the RFC file, it must be
     12  * called by a name other than "ssh" or "Secure Shell".
     13  *
     14  *
     15  * Copyright (c) 1999 Niels Provos.  All rights reserved.
     16  * Copyright (c) 1999, 2000 Markus Friedl.  All rights reserved.
     17  *
     18  * Redistribution and use in source and binary forms, with or without
     19  * modification, are permitted provided that the following conditions
     20  * are met:
     21  * 1. Redistributions of source code must retain the above copyright
     22  *    notice, this list of conditions and the following disclaimer.
     23  * 2. Redistributions in binary form must reproduce the above copyright
     24  *    notice, this list of conditions and the following disclaimer in the
     25  *    documentation and/or other materials provided with the distribution.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include "includes.h"
     40 __RCSID("$NetBSD: cipher.c,v 1.3.8.1 2013/06/23 06:26:14 tls Exp $");
     41 #include <sys/types.h>
     42 
     43 #include <openssl/md5.h>
     44 
     45 #include <string.h>
     46 #include <stdarg.h>
     47 
     48 #include "xmalloc.h"
     49 #include "log.h"
     50 #include "cipher.h"
     51 
     52 extern const EVP_CIPHER *evp_ssh1_bf(void);
     53 extern const EVP_CIPHER *evp_ssh1_3des(void);
     54 extern void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
     55 extern const EVP_CIPHER *evp_aes_128_ctr(void);
     56 extern const EVP_CIPHER *evp_aes_ctr_mt(void);
     57 extern void ssh_aes_ctr_iv(EVP_CIPHER_CTX *, int, u_char *, u_int);
     58 
     59 struct Cipher {
     60 	const char	*name;
     61 	int	number;		/* for ssh1 only */
     62 	u_int	block_size;
     63 	u_int	key_len;
     64 	u_int	iv_len;		/* defaults to block_size */
     65 	u_int	auth_len;
     66 	u_int	discard_len;
     67 	u_int	cbc_mode;
     68 	const EVP_CIPHER	*(*evptype)(void);
     69 } ciphers[] = {
     70 	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
     71 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
     72 	{ "3des",	SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
     73 	{ "blowfish",	SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
     74 
     75 	{ "3des-cbc",	SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
     76 	{ "blowfish-cbc",
     77 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
     78 	{ "cast128-cbc",
     79 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
     80 	{ "arcfour",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
     81 	{ "arcfour128",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
     82 	{ "arcfour256",	SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
     83 	{ "aes128-cbc",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
     84 	{ "aes192-cbc",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
     85 	{ "aes256-cbc",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
     86 	{ "rijndael-cbc (at) lysator.liu.se",
     87 			SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
     88 #ifdef AES_CTR_MT
     89 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, evp_aes_ctr_mt },
     90 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, evp_aes_ctr_mt },
     91 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, evp_aes_ctr_mt },
     92 #else
     93 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
     94 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
     95 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
     96 #endif
     97 	{ "aes128-gcm (at) openssh.com",
     98 			SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
     99 	{ "aes256-gcm (at) openssh.com",
    100 			SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
    101 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
    102 };
    103 
    104 /*--*/
    105 
    106 u_int
    107 cipher_blocksize(const Cipher *c)
    108 {
    109 	return (c->block_size);
    110 }
    111 
    112 u_int
    113 cipher_keylen(const Cipher *c)
    114 {
    115 	return (c->key_len);
    116 }
    117 
    118 u_int
    119 cipher_authlen(const Cipher *c)
    120 {
    121 	return (c->auth_len);
    122 }
    123 
    124 u_int
    125 cipher_ivlen(const Cipher *c)
    126 {
    127 	return (c->iv_len ? c->iv_len : c->block_size);
    128 }
    129 
    130 u_int
    131 cipher_get_number(const Cipher *c)
    132 {
    133 	return (c->number);
    134 }
    135 
    136 u_int
    137 cipher_is_cbc(const Cipher *c)
    138 {
    139 	return (c->cbc_mode);
    140 }
    141 
    142 u_int
    143 cipher_mask_ssh1(int client)
    144 {
    145 	u_int mask = 0;
    146 	mask |= 1 << SSH_CIPHER_3DES;		/* Mandatory */
    147 	mask |= 1 << SSH_CIPHER_BLOWFISH;
    148 	if (client) {
    149 		mask |= 1 << SSH_CIPHER_DES;
    150 	}
    151 	return mask;
    152 }
    153 
    154 Cipher *
    155 cipher_by_name(const char *name)
    156 {
    157 	Cipher *c;
    158 	for (c = ciphers; c->name != NULL; c++)
    159 		if (strcmp(c->name, name) == 0)
    160 			return c;
    161 	return NULL;
    162 }
    163 
    164 Cipher *
    165 cipher_by_number(int id)
    166 {
    167 	Cipher *c;
    168 	for (c = ciphers; c->name != NULL; c++)
    169 		if (c->number == id)
    170 			return c;
    171 	return NULL;
    172 }
    173 
    174 #define	CIPHER_SEP	","
    175 int
    176 ciphers_valid(const char *names)
    177 {
    178 	Cipher *c;
    179 	char *cipher_list, *cp;
    180 	char *p;
    181 
    182 	if (names == NULL || strcmp(names, "") == 0)
    183 		return 0;
    184 	cipher_list = cp = xstrdup(names);
    185 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
    186 	    (p = strsep(&cp, CIPHER_SEP))) {
    187 		c = cipher_by_name(p);
    188 		if (c == NULL || (c->number != SSH_CIPHER_SSH2 &&
    189 c->number != SSH_CIPHER_NONE)) {
    190 			debug("bad cipher %s [%s]", p, names);
    191 			xfree(cipher_list);
    192 			return 0;
    193 		} else {
    194 			debug3("cipher ok: %s [%s]", p, names);
    195 		}
    196 	}
    197 	debug3("ciphers ok: [%s]", names);
    198 	xfree(cipher_list);
    199 	return 1;
    200 }
    201 
    202 /*
    203  * Parses the name of the cipher.  Returns the number of the corresponding
    204  * cipher, or -1 on error.
    205  */
    206 
    207 int
    208 cipher_number(const char *name)
    209 {
    210 	Cipher *c;
    211 	if (name == NULL)
    212 		return -1;
    213 	for (c = ciphers; c->name != NULL; c++)
    214 		if (strcasecmp(c->name, name) == 0)
    215 			return c->number;
    216 	return -1;
    217 }
    218 
    219 const char *
    220 cipher_name(int id)
    221 {
    222 	Cipher *c = cipher_by_number(id);
    223 	return (c==NULL) ? "<unknown>" : c->name;
    224 }
    225 
    226 void
    227 cipher_init(CipherContext *cc, Cipher *cipher,
    228     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
    229     int do_encrypt)
    230 {
    231 	static int dowarn = 1;
    232 	const EVP_CIPHER *type;
    233 	int klen;
    234 	u_char *junk, *discard;
    235 
    236 	if (cipher->number == SSH_CIPHER_DES) {
    237 		if (dowarn) {
    238 			error("Warning: use of DES is strongly discouraged "
    239 			    "due to cryptographic weaknesses");
    240 			dowarn = 0;
    241 		}
    242 		if (keylen > 8)
    243 			keylen = 8;
    244 	}
    245 	cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
    246 	cc->encrypt = do_encrypt;
    247 
    248 	if (keylen < cipher->key_len)
    249 		fatal("cipher_init: key length %d is insufficient for %s.",
    250 		    keylen, cipher->name);
    251 	if (iv != NULL && ivlen < cipher_ivlen(cipher))
    252 		fatal("cipher_init: iv length %d is insufficient for %s.",
    253 		    ivlen, cipher->name);
    254 	cc->cipher = cipher;
    255 
    256 	type = (*cipher->evptype)();
    257 
    258 	EVP_CIPHER_CTX_init(&cc->evp);
    259 	if (EVP_CipherInit(&cc->evp, type, NULL, __UNCONST(iv),
    260 	    (do_encrypt == CIPHER_ENCRYPT)) == 0)
    261 		fatal("cipher_init: EVP_CipherInit failed for %s",
    262 		    cipher->name);
    263 	if (cipher_authlen(cipher) &&
    264 	    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
    265 	    -1, __UNCONST(iv)))
    266 		fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s",
    267 		    cipher->name);
    268 	klen = EVP_CIPHER_CTX_key_length(&cc->evp);
    269 	if (klen > 0 && keylen != (u_int)klen) {
    270 		debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
    271 		if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
    272 			fatal("cipher_init: set keylen failed (%d -> %d)",
    273 			    klen, keylen);
    274 	}
    275 	if (EVP_CipherInit(&cc->evp, NULL, __UNCONST(key), NULL, -1) == 0)
    276 		fatal("cipher_init: EVP_CipherInit: set key failed for %s",
    277 		    cipher->name);
    278 
    279 	if (cipher->discard_len > 0) {
    280 		junk = xmalloc(cipher->discard_len);
    281 		discard = xmalloc(cipher->discard_len);
    282 		if (EVP_Cipher(&cc->evp, discard, junk,
    283 		    cipher->discard_len) == 0)
    284 			fatal("evp_crypt: EVP_Cipher failed during discard");
    285 		memset(discard, 0, cipher->discard_len);
    286 		xfree(junk);
    287 		xfree(discard);
    288 	}
    289 }
    290 
    291 /*
    292  * cipher_crypt() operates as following:
    293  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
    294  * Theses bytes are treated as additional authenticated data for
    295  * authenticated encryption modes.
    296  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
    297  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
    298  * This tag is written on encryption and verified on decryption.
    299  * Both 'aadlen' and 'authlen' can be set to 0.
    300  */
    301 void
    302 cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src,
    303     u_int len, u_int aadlen, u_int authlen)
    304 {
    305 	if (authlen) {
    306 		u_char lastiv[1];
    307 
    308 		if (authlen != cipher_authlen(cc->cipher))
    309 			fatal("%s: authlen mismatch %d", __func__, authlen);
    310 		/* increment IV */
    311 		if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
    312 		    1, lastiv))
    313 			fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
    314 		/* set tag on decyption */
    315 		if (!cc->encrypt &&
    316 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
    317 		    authlen, __UNCONST(src + aadlen + len)))
    318 			fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__);
    319 	}
    320 	if (aadlen) {
    321 		if (authlen &&
    322 		    EVP_Cipher(&cc->evp, NULL, (const u_char *)src, aadlen) < 0)
    323 			fatal("%s: EVP_Cipher(aad) failed", __func__);
    324 		memcpy(dest, src, aadlen);
    325 	}
    326 	if (len % cc->cipher->block_size)
    327 		fatal("%s: bad plaintext length %d", __func__, len);
    328 	if (EVP_Cipher(&cc->evp, dest + aadlen, (const u_char *)src + aadlen,
    329 	    len) < 0)
    330 		fatal("%s: EVP_Cipher failed", __func__);
    331 	if (authlen) {
    332 		/* compute tag (on encrypt) or verify tag (on decrypt) */
    333 		if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) {
    334 			if (cc->encrypt)
    335 				fatal("%s: EVP_Cipher(final) failed", __func__);
    336 			else
    337 				fatal("Decryption integrity check failed");
    338 		}
    339 		if (cc->encrypt &&
    340 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
    341 		    authlen, dest + aadlen + len))
    342 			fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__);
    343 	}
    344 }
    345 
    346 void
    347 cipher_cleanup(CipherContext *cc)
    348 {
    349 	if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
    350 		error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
    351 }
    352 
    353 /*
    354  * Selects the cipher, and keys if by computing the MD5 checksum of the
    355  * passphrase and using the resulting 16 bytes as the key.
    356  */
    357 
    358 void
    359 cipher_set_key_string(CipherContext *cc, Cipher *cipher,
    360     const char *passphrase, int do_encrypt)
    361 {
    362 	MD5_CTX md;
    363 	u_char digest[16];
    364 
    365 	MD5_Init(&md);
    366 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
    367 	MD5_Final(digest, &md);
    368 
    369 	cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
    370 
    371 	memset(digest, 0, sizeof(digest));
    372 	memset(&md, 0, sizeof(md));
    373 }
    374 
    375 /*
    376  * Exports an IV from the CipherContext required to export the key
    377  * state back from the unprivileged child to the privileged parent
    378  * process.
    379  */
    380 
    381 int
    382 cipher_get_keyiv_len(const CipherContext *cc)
    383 {
    384 	Cipher *c = cc->cipher;
    385 	int ivlen;
    386 
    387 	if (c->number == SSH_CIPHER_3DES)
    388 		ivlen = 24;
    389 	else
    390 		ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    391 	return (ivlen);
    392 }
    393 
    394 void
    395 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
    396 {
    397 	Cipher *c = cc->cipher;
    398 	int evplen;
    399 
    400 	switch (c->number) {
    401 	case SSH_CIPHER_NONE:
    402 	case SSH_CIPHER_SSH2:
    403 	case SSH_CIPHER_DES:
    404 	case SSH_CIPHER_BLOWFISH:
    405 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    406 		if (evplen <= 0)
    407 			return;
    408 		if ((u_int)evplen != len)
    409 			fatal("%s: wrong iv length %d != %d", __func__,
    410 			    evplen, len);
    411 		if (cipher_authlen(c)) {
    412 			if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
    413 			   len, iv))
    414 			       fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
    415 		} else
    416 			memcpy(iv, cc->evp.iv, len);
    417 		break;
    418 	case SSH_CIPHER_3DES:
    419 		ssh1_3des_iv(&cc->evp, 0, iv, 24);
    420 		break;
    421 	default:
    422 		fatal("%s: bad cipher %d", __func__, c->number);
    423 	}
    424 }
    425 
    426 void
    427 cipher_set_keyiv(CipherContext *cc, u_char *iv)
    428 {
    429 	Cipher *c = cc->cipher;
    430 	int evplen = 0;
    431 
    432 	switch (c->number) {
    433 	case SSH_CIPHER_NONE:
    434 	case SSH_CIPHER_SSH2:
    435 	case SSH_CIPHER_DES:
    436 	case SSH_CIPHER_BLOWFISH:
    437 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    438 		if (evplen == 0)
    439 			return;
    440 		if (cipher_authlen(c)) {
    441 			if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
    442 			    EVP_CTRL_GCM_SET_IV_FIXED, -1, iv))
    443 				fatal("%s: EVP_CTRL_GCM_SET_IV_FIXED failed",
    444 				    __func__);
    445 		} else
    446 			memcpy(cc->evp.iv, iv, evplen);
    447 		break;
    448 	case SSH_CIPHER_3DES:
    449 		ssh1_3des_iv(&cc->evp, 1, iv, 24);
    450 		break;
    451 	default:
    452 		fatal("%s: bad cipher %d", __func__, c->number);
    453 	}
    454 }
    455 
    456 #define EVP_X_STATE(evp)	(evp).cipher_data
    457 #define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
    458 
    459 int
    460 cipher_get_keycontext(const CipherContext *cc, u_char *dat)
    461 {
    462 	Cipher *c = cc->cipher;
    463 	int plen = 0;
    464 
    465 	if (c->evptype == EVP_rc4) {
    466 		plen = EVP_X_STATE_LEN(cc->evp);
    467 		if (dat == NULL)
    468 			return (plen);
    469 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
    470 	}
    471 	return (plen);
    472 }
    473 
    474 void
    475 cipher_set_keycontext(CipherContext *cc, u_char *dat)
    476 {
    477 	Cipher *c = cc->cipher;
    478 	int plen;
    479 
    480 	if (c->evptype == EVP_rc4) {
    481 		plen = EVP_X_STATE_LEN(cc->evp);
    482 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
    483 	}
    484 }
    485