xref: /openssh-portable/cipher-aes.c (revision f60845fd)
13a3261ffSDamien Miller /*
23a3261ffSDamien Miller  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
33a3261ffSDamien Miller  *
43a3261ffSDamien Miller  * Redistribution and use in source and binary forms, with or without
53a3261ffSDamien Miller  * modification, are permitted provided that the following conditions
63a3261ffSDamien Miller  * are met:
73a3261ffSDamien Miller  * 1. Redistributions of source code must retain the above copyright
83a3261ffSDamien Miller  *    notice, this list of conditions and the following disclaimer.
93a3261ffSDamien Miller  * 2. Redistributions in binary form must reproduce the above copyright
103a3261ffSDamien Miller  *    notice, this list of conditions and the following disclaimer in the
113a3261ffSDamien Miller  *    documentation and/or other materials provided with the distribution.
123a3261ffSDamien Miller  *
133a3261ffSDamien Miller  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
143a3261ffSDamien Miller  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
153a3261ffSDamien Miller  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
163a3261ffSDamien Miller  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
173a3261ffSDamien Miller  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
183a3261ffSDamien Miller  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
193a3261ffSDamien Miller  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
203a3261ffSDamien Miller  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
213a3261ffSDamien Miller  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
223a3261ffSDamien Miller  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
233a3261ffSDamien Miller  */
243a3261ffSDamien Miller 
253a3261ffSDamien Miller #include "includes.h"
26129d0bb6SDarren Tucker 
27129d0bb6SDarren Tucker /* compatibility with old or broken OpenSSL versions */
28129d0bb6SDarren Tucker #include "openbsd-compat/openssl-compat.h"
29129d0bb6SDarren Tucker 
30129d0bb6SDarren Tucker #ifdef USE_BUILTIN_RIJNDAEL
31ded319ccSDamien Miller #include <sys/types.h>
323a3261ffSDamien Miller 
333a3261ffSDamien Miller #include <openssl/evp.h>
34ded319ccSDamien Miller 
35ded319ccSDamien Miller #include <stdarg.h>
365d8a9aceSDarren Tucker #include <string.h>
37ded319ccSDamien Miller 
383a3261ffSDamien Miller #include "rijndael.h"
393a3261ffSDamien Miller #include "xmalloc.h"
403a3261ffSDamien Miller #include "log.h"
413a3261ffSDamien Miller 
423a3261ffSDamien Miller #define RIJNDAEL_BLOCKSIZE 16
433a3261ffSDamien Miller struct ssh_rijndael_ctx
443a3261ffSDamien Miller {
453a3261ffSDamien Miller 	rijndael_ctx	r_ctx;
463a3261ffSDamien Miller 	u_char		r_iv[RIJNDAEL_BLOCKSIZE];
473a3261ffSDamien Miller };
483a3261ffSDamien Miller 
493a3261ffSDamien Miller static int
ssh_rijndael_init(EVP_CIPHER_CTX * ctx,const u_char * key,const u_char * iv,int enc)503a3261ffSDamien Miller ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
513a3261ffSDamien Miller     int enc)
523a3261ffSDamien Miller {
533a3261ffSDamien Miller 	struct ssh_rijndael_ctx *c;
543a3261ffSDamien Miller 
553a3261ffSDamien Miller 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
563a3261ffSDamien Miller 		c = xmalloc(sizeof(*c));
573a3261ffSDamien Miller 		EVP_CIPHER_CTX_set_app_data(ctx, c);
583a3261ffSDamien Miller 	}
593a3261ffSDamien Miller 	if (key != NULL) {
603a3261ffSDamien Miller 		if (enc == -1)
613a3261ffSDamien Miller 			enc = ctx->encrypt;
623a3261ffSDamien Miller 		rijndael_set_key(&c->r_ctx, (u_char *)key,
633a3261ffSDamien Miller 		    8*EVP_CIPHER_CTX_key_length(ctx), enc);
643a3261ffSDamien Miller 	}
653a3261ffSDamien Miller 	if (iv != NULL)
663a3261ffSDamien Miller 		memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
673a3261ffSDamien Miller 	return (1);
683a3261ffSDamien Miller }
693a3261ffSDamien Miller 
703a3261ffSDamien Miller static int
ssh_rijndael_cbc(EVP_CIPHER_CTX * ctx,u_char * dest,const u_char * src,LIBCRYPTO_EVP_INL_TYPE len)713a3261ffSDamien Miller ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
729a3d0dc0SDamien Miller     LIBCRYPTO_EVP_INL_TYPE len)
733a3261ffSDamien Miller {
743a3261ffSDamien Miller 	struct ssh_rijndael_ctx *c;
753a3261ffSDamien Miller 	u_char buf[RIJNDAEL_BLOCKSIZE];
763a3261ffSDamien Miller 	u_char *cprev, *cnow, *plain, *ivp;
773a3261ffSDamien Miller 	int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
783a3261ffSDamien Miller 
793a3261ffSDamien Miller 	if (len == 0)
803a3261ffSDamien Miller 		return (1);
813a3261ffSDamien Miller 	if (len % RIJNDAEL_BLOCKSIZE)
823a3261ffSDamien Miller 		fatal("ssh_rijndael_cbc: bad len %d", len);
833a3261ffSDamien Miller 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
843a3261ffSDamien Miller 		error("ssh_rijndael_cbc: no context");
853a3261ffSDamien Miller 		return (0);
863a3261ffSDamien Miller 	}
873a3261ffSDamien Miller 	if (ctx->encrypt) {
883a3261ffSDamien Miller 		cnow  = dest;
893a3261ffSDamien Miller 		plain = (u_char *)src;
903a3261ffSDamien Miller 		cprev = c->r_iv;
913a3261ffSDamien Miller 		for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
923a3261ffSDamien Miller 		    cnow+=RIJNDAEL_BLOCKSIZE) {
933a3261ffSDamien Miller 			for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
943a3261ffSDamien Miller 				buf[j] = plain[j] ^ cprev[j];
953a3261ffSDamien Miller 			rijndael_encrypt(&c->r_ctx, buf, cnow);
963a3261ffSDamien Miller 			cprev = cnow;
973a3261ffSDamien Miller 		}
983a3261ffSDamien Miller 		memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
993a3261ffSDamien Miller 	} else {
1003a3261ffSDamien Miller 		cnow  = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
1013a3261ffSDamien Miller 		plain = dest+len-RIJNDAEL_BLOCKSIZE;
1023a3261ffSDamien Miller 
1033a3261ffSDamien Miller 		memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
1043a3261ffSDamien Miller 		for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
1053a3261ffSDamien Miller 		    plain-=RIJNDAEL_BLOCKSIZE) {
1063a3261ffSDamien Miller 			rijndael_decrypt(&c->r_ctx, cnow, plain);
1073a3261ffSDamien Miller 			ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
1083a3261ffSDamien Miller 			for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
1093a3261ffSDamien Miller 				plain[j] ^= ivp[j];
1103a3261ffSDamien Miller 		}
1113a3261ffSDamien Miller 		memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
1123a3261ffSDamien Miller 	}
1133a3261ffSDamien Miller 	return (1);
1143a3261ffSDamien Miller }
1153a3261ffSDamien Miller 
1163a3261ffSDamien Miller static int
ssh_rijndael_cleanup(EVP_CIPHER_CTX * ctx)1173a3261ffSDamien Miller ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
1183a3261ffSDamien Miller {
1193a3261ffSDamien Miller 	struct ssh_rijndael_ctx *c;
1203a3261ffSDamien Miller 
1213a3261ffSDamien Miller 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
1223a3261ffSDamien Miller 		memset(c, 0, sizeof(*c));
123*f60845fdSDarren Tucker 		free(c);
1243a3261ffSDamien Miller 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
1253a3261ffSDamien Miller 	}
1263a3261ffSDamien Miller 	return (1);
1273a3261ffSDamien Miller }
1283a3261ffSDamien Miller 
1293a3261ffSDamien Miller void
ssh_rijndael_iv(EVP_CIPHER_CTX * evp,int doset,u_char * iv,u_int len)1303a3261ffSDamien Miller ssh_rijndael_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len)
1313a3261ffSDamien Miller {
1323a3261ffSDamien Miller 	struct ssh_rijndael_ctx *c;
1333a3261ffSDamien Miller 
1343a3261ffSDamien Miller 	if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
1353a3261ffSDamien Miller 		fatal("ssh_rijndael_iv: no context");
1363a3261ffSDamien Miller 	if (doset)
1373a3261ffSDamien Miller 		memcpy(c->r_iv, iv, len);
1383a3261ffSDamien Miller 	else
1393a3261ffSDamien Miller 		memcpy(iv, c->r_iv, len);
1403a3261ffSDamien Miller }
1413a3261ffSDamien Miller 
1423a3261ffSDamien Miller const EVP_CIPHER *
evp_rijndael(void)1433a3261ffSDamien Miller evp_rijndael(void)
1443a3261ffSDamien Miller {
1453a3261ffSDamien Miller 	static EVP_CIPHER rijndal_cbc;
1463a3261ffSDamien Miller 
1473a3261ffSDamien Miller 	memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
1483a3261ffSDamien Miller 	rijndal_cbc.nid = NID_undef;
1493a3261ffSDamien Miller 	rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
1503a3261ffSDamien Miller 	rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
1513a3261ffSDamien Miller 	rijndal_cbc.key_len = 16;
1523a3261ffSDamien Miller 	rijndal_cbc.init = ssh_rijndael_init;
1533a3261ffSDamien Miller 	rijndal_cbc.cleanup = ssh_rijndael_cleanup;
1543a3261ffSDamien Miller 	rijndal_cbc.do_cipher = ssh_rijndael_cbc;
1553a3261ffSDamien Miller #ifndef SSH_OLD_EVP
1563a3261ffSDamien Miller 	rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
1573a3261ffSDamien Miller 	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
1583a3261ffSDamien Miller #endif
1593a3261ffSDamien Miller 	return (&rijndal_cbc);
1603a3261ffSDamien Miller }
161129d0bb6SDarren Tucker #endif /* USE_BUILTIN_RIJNDAEL */
162