Home | History | Annotate | Line # | Download | only in dist
cipher.c revision 1.5
      1 /*	$NetBSD: cipher.c,v 1.5 2013/11/08 19:18:24 christos Exp $	*/
      2 /* $OpenBSD: cipher.c,v 1.89 2013/05/17 00:13:13 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.5 2013/11/08 19:18:24 christos 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 };
     70 
     71 static const struct Cipher ciphers[] = {
     72 	{ "none",	SSH_CIPHER_NONE, 8, 0, 0, 0, 0, 0, EVP_enc_null },
     73 	{ "des",	SSH_CIPHER_DES, 8, 8, 0, 0, 0, 1, EVP_des_cbc },
     74 	{ "3des",	SSH_CIPHER_3DES, 8, 16, 0, 0, 0, 1, evp_ssh1_3des },
     75 	{ "blowfish",	SSH_CIPHER_BLOWFISH, 8, 32, 0, 0, 0, 1, evp_ssh1_bf },
     76 
     77 	{ "3des-cbc",	SSH_CIPHER_SSH2, 8, 24, 0, 0, 0, 1, EVP_des_ede3_cbc },
     78 	{ "blowfish-cbc",
     79 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_bf_cbc },
     80 	{ "cast128-cbc",
     81 			SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 1, EVP_cast5_cbc },
     82 	{ "arcfour",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 0, 0, EVP_rc4 },
     83 	{ "arcfour128",	SSH_CIPHER_SSH2, 8, 16, 0, 0, 1536, 0, EVP_rc4 },
     84 	{ "arcfour256",	SSH_CIPHER_SSH2, 8, 32, 0, 0, 1536, 0, EVP_rc4 },
     85 	{ "aes128-cbc",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 1, EVP_aes_128_cbc },
     86 	{ "aes192-cbc",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 1, EVP_aes_192_cbc },
     87 	{ "aes256-cbc",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
     88 	{ "rijndael-cbc (at) lysator.liu.se",
     89 			SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 1, EVP_aes_256_cbc },
     90 #ifdef AES_CTR_MT
     91 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, evp_aes_ctr_mt },
     92 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, evp_aes_ctr_mt },
     93 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, evp_aes_ctr_mt },
     94 #else
     95 	{ "aes128-ctr",	SSH_CIPHER_SSH2, 16, 16, 0, 0, 0, 0, EVP_aes_128_ctr },
     96 	{ "aes192-ctr",	SSH_CIPHER_SSH2, 16, 24, 0, 0, 0, 0, EVP_aes_192_ctr },
     97 	{ "aes256-ctr",	SSH_CIPHER_SSH2, 16, 32, 0, 0, 0, 0, EVP_aes_256_ctr },
     98 #endif
     99 	{ "aes128-gcm (at) openssh.com",
    100 			SSH_CIPHER_SSH2, 16, 16, 12, 16, 0, 0, EVP_aes_128_gcm },
    101 	{ "aes256-gcm (at) openssh.com",
    102 			SSH_CIPHER_SSH2, 16, 32, 12, 16, 0, 0, EVP_aes_256_gcm },
    103 	{ NULL,		SSH_CIPHER_INVALID, 0, 0, 0, 0, 0, 0, NULL }
    104 };
    105 
    106 /*--*/
    107 
    108 /* Returns a comma-separated list of supported ciphers. */
    109 char *
    110 cipher_alg_list(void)
    111 {
    112 	char *ret = NULL;
    113 	size_t nlen, rlen = 0;
    114 	const Cipher *c;
    115 
    116 	for (c = ciphers; c->name != NULL; c++) {
    117 		if (c->number != SSH_CIPHER_SSH2)
    118 			continue;
    119 		if (ret != NULL)
    120 			ret[rlen++] = '\n';
    121 		nlen = strlen(c->name);
    122 		ret = xrealloc(ret, 1, rlen + nlen + 2);
    123 		memcpy(ret + rlen, c->name, nlen + 1);
    124 		rlen += nlen;
    125 	}
    126 	return ret;
    127 }
    128 
    129 u_int
    130 cipher_blocksize(const Cipher *c)
    131 {
    132 	return (c->block_size);
    133 }
    134 
    135 u_int
    136 cipher_keylen(const Cipher *c)
    137 {
    138 	return (c->key_len);
    139 }
    140 
    141 u_int
    142 cipher_authlen(const Cipher *c)
    143 {
    144 	return (c->auth_len);
    145 }
    146 
    147 u_int
    148 cipher_ivlen(const Cipher *c)
    149 {
    150 	return (c->iv_len ? c->iv_len : c->block_size);
    151 }
    152 
    153 u_int
    154 cipher_get_number(const Cipher *c)
    155 {
    156 	return (c->number);
    157 }
    158 
    159 u_int
    160 cipher_is_cbc(const Cipher *c)
    161 {
    162 	return (c->cbc_mode);
    163 }
    164 
    165 u_int
    166 cipher_mask_ssh1(int client)
    167 {
    168 	u_int mask = 0;
    169 	mask |= 1 << SSH_CIPHER_3DES;		/* Mandatory */
    170 	mask |= 1 << SSH_CIPHER_BLOWFISH;
    171 	if (client) {
    172 		mask |= 1 << SSH_CIPHER_DES;
    173 	}
    174 	return mask;
    175 }
    176 
    177 const Cipher *
    178 cipher_by_name(const char *name)
    179 {
    180 	const Cipher *c;
    181 	for (c = ciphers; c->name != NULL; c++)
    182 		if (strcmp(c->name, name) == 0)
    183 			return c;
    184 	return NULL;
    185 }
    186 
    187 const Cipher *
    188 cipher_by_number(int id)
    189 {
    190 	const Cipher *c;
    191 	for (c = ciphers; c->name != NULL; c++)
    192 		if (c->number == id)
    193 			return c;
    194 	return NULL;
    195 }
    196 
    197 #define	CIPHER_SEP	","
    198 int
    199 ciphers_valid(const char *names)
    200 {
    201 	const Cipher *c;
    202 	char *cipher_list, *cp;
    203 	char *p;
    204 
    205 	if (names == NULL || strcmp(names, "") == 0)
    206 		return 0;
    207 	cipher_list = cp = xstrdup(names);
    208 	for ((p = strsep(&cp, CIPHER_SEP)); p && *p != '\0';
    209 	    (p = strsep(&cp, CIPHER_SEP))) {
    210 		c = cipher_by_name(p);
    211 		if (c == NULL || (c->number != SSH_CIPHER_SSH2 &&
    212 c->number != SSH_CIPHER_NONE)) {
    213 			debug("bad cipher %s [%s]", p, names);
    214 			free(cipher_list);
    215 			return 0;
    216 		} else {
    217 			debug3("cipher ok: %s [%s]", p, names);
    218 		}
    219 	}
    220 	debug3("ciphers ok: [%s]", names);
    221 	free(cipher_list);
    222 	return 1;
    223 }
    224 
    225 /*
    226  * Parses the name of the cipher.  Returns the number of the corresponding
    227  * cipher, or -1 on error.
    228  */
    229 
    230 int
    231 cipher_number(const char *name)
    232 {
    233 	const Cipher *c;
    234 	if (name == NULL)
    235 		return -1;
    236 	for (c = ciphers; c->name != NULL; c++)
    237 		if (strcasecmp(c->name, name) == 0)
    238 			return c->number;
    239 	return -1;
    240 }
    241 
    242 const char *
    243 cipher_name(int id)
    244 {
    245 	const Cipher *c = cipher_by_number(id);
    246 	return (c==NULL) ? "<unknown>" : c->name;
    247 }
    248 
    249 void
    250 cipher_init(CipherContext *cc, const Cipher *cipher,
    251     const u_char *key, u_int keylen, const u_char *iv, u_int ivlen,
    252     int do_encrypt)
    253 {
    254 	static int dowarn = 1;
    255 	const EVP_CIPHER *type;
    256 	int klen;
    257 	u_char *junk, *discard;
    258 
    259 	if (cipher->number == SSH_CIPHER_DES) {
    260 		if (dowarn) {
    261 			error("Warning: use of DES is strongly discouraged "
    262 			    "due to cryptographic weaknesses");
    263 			dowarn = 0;
    264 		}
    265 		if (keylen > 8)
    266 			keylen = 8;
    267 	}
    268 	cc->plaintext = (cipher->number == SSH_CIPHER_NONE);
    269 	cc->encrypt = do_encrypt;
    270 
    271 	if (keylen < cipher->key_len)
    272 		fatal("cipher_init: key length %d is insufficient for %s.",
    273 		    keylen, cipher->name);
    274 	if (iv != NULL && ivlen < cipher_ivlen(cipher))
    275 		fatal("cipher_init: iv length %d is insufficient for %s.",
    276 		    ivlen, cipher->name);
    277 	cc->cipher = cipher;
    278 
    279 	type = (*cipher->evptype)();
    280 
    281 	EVP_CIPHER_CTX_init(&cc->evp);
    282 	if (EVP_CipherInit(&cc->evp, type, NULL, __UNCONST(iv),
    283 	    (do_encrypt == CIPHER_ENCRYPT)) == 0)
    284 		fatal("cipher_init: EVP_CipherInit failed for %s",
    285 		    cipher->name);
    286 	if (cipher_authlen(cipher) &&
    287 	    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_IV_FIXED,
    288 	    -1, __UNCONST(iv)))
    289 		fatal("cipher_init: EVP_CTRL_GCM_SET_IV_FIXED failed for %s",
    290 		    cipher->name);
    291 	klen = EVP_CIPHER_CTX_key_length(&cc->evp);
    292 	if (klen > 0 && keylen != (u_int)klen) {
    293 		debug2("cipher_init: set keylen (%d -> %d)", klen, keylen);
    294 		if (EVP_CIPHER_CTX_set_key_length(&cc->evp, keylen) == 0)
    295 			fatal("cipher_init: set keylen failed (%d -> %d)",
    296 			    klen, keylen);
    297 	}
    298 	if (EVP_CipherInit(&cc->evp, NULL, __UNCONST(key), NULL, -1) == 0)
    299 		fatal("cipher_init: EVP_CipherInit: set key failed for %s",
    300 		    cipher->name);
    301 
    302 	if (cipher->discard_len > 0) {
    303 		junk = xmalloc(cipher->discard_len);
    304 		discard = xmalloc(cipher->discard_len);
    305 		if (EVP_Cipher(&cc->evp, discard, junk,
    306 		    cipher->discard_len) == 0)
    307 			fatal("evp_crypt: EVP_Cipher failed during discard");
    308 		memset(discard, 0, cipher->discard_len);
    309 		free(junk);
    310 		free(discard);
    311 	}
    312 }
    313 
    314 /*
    315  * cipher_crypt() operates as following:
    316  * Copy 'aadlen' bytes (without en/decryption) from 'src' to 'dest'.
    317  * Theses bytes are treated as additional authenticated data for
    318  * authenticated encryption modes.
    319  * En/Decrypt 'len' bytes at offset 'aadlen' from 'src' to 'dest'.
    320  * Use 'authlen' bytes at offset 'len'+'aadlen' as the authentication tag.
    321  * This tag is written on encryption and verified on decryption.
    322  * Both 'aadlen' and 'authlen' can be set to 0.
    323  */
    324 void
    325 cipher_crypt(CipherContext *cc, u_char *dest, const u_char *src,
    326     u_int len, u_int aadlen, u_int authlen)
    327 {
    328 	if (authlen) {
    329 		u_char lastiv[1];
    330 
    331 		if (authlen != cipher_authlen(cc->cipher))
    332 			fatal("%s: authlen mismatch %d", __func__, authlen);
    333 		/* increment IV */
    334 		if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
    335 		    1, lastiv))
    336 			fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
    337 		/* set tag on decyption */
    338 		if (!cc->encrypt &&
    339 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_SET_TAG,
    340 		    authlen, __UNCONST(src + aadlen + len)))
    341 			fatal("%s: EVP_CTRL_GCM_SET_TAG", __func__);
    342 	}
    343 	if (aadlen) {
    344 		if (authlen &&
    345 		    EVP_Cipher(&cc->evp, NULL, (const u_char *)src, aadlen) < 0)
    346 			fatal("%s: EVP_Cipher(aad) failed", __func__);
    347 		memcpy(dest, src, aadlen);
    348 	}
    349 	if (len % cc->cipher->block_size)
    350 		fatal("%s: bad plaintext length %d", __func__, len);
    351 	if (EVP_Cipher(&cc->evp, dest + aadlen, (const u_char *)src + aadlen,
    352 	    len) < 0)
    353 		fatal("%s: EVP_Cipher failed", __func__);
    354 	if (authlen) {
    355 		/* compute tag (on encrypt) or verify tag (on decrypt) */
    356 		if (EVP_Cipher(&cc->evp, NULL, NULL, 0) < 0) {
    357 			if (cc->encrypt)
    358 				fatal("%s: EVP_Cipher(final) failed", __func__);
    359 			else
    360 				fatal("Decryption integrity check failed");
    361 		}
    362 		if (cc->encrypt &&
    363 		    !EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_GET_TAG,
    364 		    authlen, dest + aadlen + len))
    365 			fatal("%s: EVP_CTRL_GCM_GET_TAG", __func__);
    366 	}
    367 }
    368 
    369 void
    370 cipher_cleanup(CipherContext *cc)
    371 {
    372 	if (EVP_CIPHER_CTX_cleanup(&cc->evp) == 0)
    373 		error("cipher_cleanup: EVP_CIPHER_CTX_cleanup failed");
    374 }
    375 
    376 /*
    377  * Selects the cipher, and keys if by computing the MD5 checksum of the
    378  * passphrase and using the resulting 16 bytes as the key.
    379  */
    380 
    381 void
    382 cipher_set_key_string(CipherContext *cc, const Cipher *cipher,
    383     const char *passphrase, int do_encrypt)
    384 {
    385 	MD5_CTX md;
    386 	u_char digest[16];
    387 
    388 	MD5_Init(&md);
    389 	MD5_Update(&md, (const u_char *)passphrase, strlen(passphrase));
    390 	MD5_Final(digest, &md);
    391 
    392 	cipher_init(cc, cipher, digest, 16, NULL, 0, do_encrypt);
    393 
    394 	memset(digest, 0, sizeof(digest));
    395 	memset(&md, 0, sizeof(md));
    396 }
    397 
    398 /*
    399  * Exports an IV from the CipherContext required to export the key
    400  * state back from the unprivileged child to the privileged parent
    401  * process.
    402  */
    403 
    404 int
    405 cipher_get_keyiv_len(const CipherContext *cc)
    406 {
    407 	const Cipher *c = cc->cipher;
    408 	int ivlen;
    409 
    410 	if (c->number == SSH_CIPHER_3DES)
    411 		ivlen = 24;
    412 	else
    413 		ivlen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    414 	return (ivlen);
    415 }
    416 
    417 void
    418 cipher_get_keyiv(CipherContext *cc, u_char *iv, u_int len)
    419 {
    420 	const Cipher *c = cc->cipher;
    421 	int evplen;
    422 
    423 	switch (c->number) {
    424 	case SSH_CIPHER_NONE:
    425 	case SSH_CIPHER_SSH2:
    426 	case SSH_CIPHER_DES:
    427 	case SSH_CIPHER_BLOWFISH:
    428 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    429 		if (evplen <= 0)
    430 			return;
    431 		if ((u_int)evplen != len)
    432 			fatal("%s: wrong iv length %d != %d", __func__,
    433 			    evplen, len);
    434 		if (cipher_authlen(c)) {
    435 			if (!EVP_CIPHER_CTX_ctrl(&cc->evp, EVP_CTRL_GCM_IV_GEN,
    436 			   len, iv))
    437 			       fatal("%s: EVP_CTRL_GCM_IV_GEN", __func__);
    438 		} else
    439 			memcpy(iv, cc->evp.iv, len);
    440 		break;
    441 	case SSH_CIPHER_3DES:
    442 		ssh1_3des_iv(&cc->evp, 0, iv, 24);
    443 		break;
    444 	default:
    445 		fatal("%s: bad cipher %d", __func__, c->number);
    446 	}
    447 }
    448 
    449 void
    450 cipher_set_keyiv(CipherContext *cc, u_char *iv)
    451 {
    452 	const Cipher *c = cc->cipher;
    453 	int evplen = 0;
    454 
    455 	switch (c->number) {
    456 	case SSH_CIPHER_NONE:
    457 	case SSH_CIPHER_SSH2:
    458 	case SSH_CIPHER_DES:
    459 	case SSH_CIPHER_BLOWFISH:
    460 		evplen = EVP_CIPHER_CTX_iv_length(&cc->evp);
    461 		if (evplen == 0)
    462 			return;
    463 		if (cipher_authlen(c)) {
    464 			if (!EVP_CIPHER_CTX_ctrl(&cc->evp,
    465 			    EVP_CTRL_GCM_SET_IV_FIXED, -1, iv))
    466 				fatal("%s: EVP_CTRL_GCM_SET_IV_FIXED failed",
    467 				    __func__);
    468 		} else
    469 			memcpy(cc->evp.iv, iv, evplen);
    470 		break;
    471 	case SSH_CIPHER_3DES:
    472 		ssh1_3des_iv(&cc->evp, 1, iv, 24);
    473 		break;
    474 	default:
    475 		fatal("%s: bad cipher %d", __func__, c->number);
    476 	}
    477 }
    478 
    479 #define EVP_X_STATE(evp)	(evp).cipher_data
    480 #define EVP_X_STATE_LEN(evp)	(evp).cipher->ctx_size
    481 
    482 int
    483 cipher_get_keycontext(const CipherContext *cc, u_char *dat)
    484 {
    485 	const Cipher *c = cc->cipher;
    486 	int plen = 0;
    487 
    488 	if (c->evptype == EVP_rc4) {
    489 		plen = EVP_X_STATE_LEN(cc->evp);
    490 		if (dat == NULL)
    491 			return (plen);
    492 		memcpy(dat, EVP_X_STATE(cc->evp), plen);
    493 	}
    494 	return (plen);
    495 }
    496 
    497 void
    498 cipher_set_keycontext(CipherContext *cc, u_char *dat)
    499 {
    500 	const Cipher *c = cc->cipher;
    501 	int plen;
    502 
    503 	if (c->evptype == EVP_rc4) {
    504 		plen = EVP_X_STATE_LEN(cc->evp);
    505 		memcpy(EVP_X_STATE(cc->evp), dat, plen);
    506 	}
    507 }
    508