xref: /dragonfly/crypto/openssh/cipher-aes.c (revision ba1276ac)
1*ba1276acSMatthew Dillon /*
2*ba1276acSMatthew Dillon  * Copyright (c) 2003 Markus Friedl.  All rights reserved.
3*ba1276acSMatthew Dillon  *
4*ba1276acSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
5*ba1276acSMatthew Dillon  * modification, are permitted provided that the following conditions
6*ba1276acSMatthew Dillon  * are met:
7*ba1276acSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
8*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
9*ba1276acSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
10*ba1276acSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
11*ba1276acSMatthew Dillon  *    documentation and/or other materials provided with the distribution.
12*ba1276acSMatthew Dillon  *
13*ba1276acSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14*ba1276acSMatthew Dillon  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15*ba1276acSMatthew Dillon  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16*ba1276acSMatthew Dillon  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17*ba1276acSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18*ba1276acSMatthew Dillon  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19*ba1276acSMatthew Dillon  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20*ba1276acSMatthew Dillon  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21*ba1276acSMatthew Dillon  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22*ba1276acSMatthew Dillon  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*ba1276acSMatthew Dillon  */
24*ba1276acSMatthew Dillon 
25*ba1276acSMatthew Dillon #include "includes.h"
26*ba1276acSMatthew Dillon 
27*ba1276acSMatthew Dillon /* compatibility with old or broken OpenSSL versions */
28*ba1276acSMatthew Dillon #include "openbsd-compat/openssl-compat.h"
29*ba1276acSMatthew Dillon 
30*ba1276acSMatthew Dillon #ifdef USE_BUILTIN_RIJNDAEL
31*ba1276acSMatthew Dillon #include <sys/types.h>
32*ba1276acSMatthew Dillon 
33*ba1276acSMatthew Dillon #include <openssl/evp.h>
34*ba1276acSMatthew Dillon 
35*ba1276acSMatthew Dillon #include <stdarg.h>
36*ba1276acSMatthew Dillon #include <string.h>
37*ba1276acSMatthew Dillon 
38*ba1276acSMatthew Dillon #include "rijndael.h"
39*ba1276acSMatthew Dillon #include "xmalloc.h"
40*ba1276acSMatthew Dillon #include "log.h"
41*ba1276acSMatthew Dillon 
42*ba1276acSMatthew Dillon #define RIJNDAEL_BLOCKSIZE 16
43*ba1276acSMatthew Dillon struct ssh_rijndael_ctx
44*ba1276acSMatthew Dillon {
45*ba1276acSMatthew Dillon 	rijndael_ctx	r_ctx;
46*ba1276acSMatthew Dillon 	u_char		r_iv[RIJNDAEL_BLOCKSIZE];
47*ba1276acSMatthew Dillon };
48*ba1276acSMatthew Dillon 
49*ba1276acSMatthew Dillon static int
ssh_rijndael_init(EVP_CIPHER_CTX * ctx,const u_char * key,const u_char * iv,int enc)50*ba1276acSMatthew Dillon ssh_rijndael_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
51*ba1276acSMatthew Dillon     int enc)
52*ba1276acSMatthew Dillon {
53*ba1276acSMatthew Dillon 	struct ssh_rijndael_ctx *c;
54*ba1276acSMatthew Dillon 
55*ba1276acSMatthew Dillon 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
56*ba1276acSMatthew Dillon 		c = xmalloc(sizeof(*c));
57*ba1276acSMatthew Dillon 		EVP_CIPHER_CTX_set_app_data(ctx, c);
58*ba1276acSMatthew Dillon 	}
59*ba1276acSMatthew Dillon 	if (key != NULL) {
60*ba1276acSMatthew Dillon 		if (enc == -1)
61*ba1276acSMatthew Dillon 			enc = ctx->encrypt;
62*ba1276acSMatthew Dillon 		rijndael_set_key(&c->r_ctx, (u_char *)key,
63*ba1276acSMatthew Dillon 		    8*EVP_CIPHER_CTX_key_length(ctx), enc);
64*ba1276acSMatthew Dillon 	}
65*ba1276acSMatthew Dillon 	if (iv != NULL)
66*ba1276acSMatthew Dillon 		memcpy(c->r_iv, iv, RIJNDAEL_BLOCKSIZE);
67*ba1276acSMatthew Dillon 	return (1);
68*ba1276acSMatthew Dillon }
69*ba1276acSMatthew Dillon 
70*ba1276acSMatthew Dillon static int
ssh_rijndael_cbc(EVP_CIPHER_CTX * ctx,u_char * dest,const u_char * src,size_t len)71*ba1276acSMatthew Dillon ssh_rijndael_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src,
72*ba1276acSMatthew Dillon     size_t len)
73*ba1276acSMatthew Dillon {
74*ba1276acSMatthew Dillon 	struct ssh_rijndael_ctx *c;
75*ba1276acSMatthew Dillon 	u_char buf[RIJNDAEL_BLOCKSIZE];
76*ba1276acSMatthew Dillon 	u_char *cprev, *cnow, *plain, *ivp;
77*ba1276acSMatthew Dillon 	int i, j, blocks = len / RIJNDAEL_BLOCKSIZE;
78*ba1276acSMatthew Dillon 
79*ba1276acSMatthew Dillon 	if (len == 0)
80*ba1276acSMatthew Dillon 		return (1);
81*ba1276acSMatthew Dillon 	if (len % RIJNDAEL_BLOCKSIZE)
82*ba1276acSMatthew Dillon 		fatal("ssh_rijndael_cbc: bad len %d", len);
83*ba1276acSMatthew Dillon 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
84*ba1276acSMatthew Dillon 		error("ssh_rijndael_cbc: no context");
85*ba1276acSMatthew Dillon 		return (0);
86*ba1276acSMatthew Dillon 	}
87*ba1276acSMatthew Dillon 	if (ctx->encrypt) {
88*ba1276acSMatthew Dillon 		cnow  = dest;
89*ba1276acSMatthew Dillon 		plain = (u_char *)src;
90*ba1276acSMatthew Dillon 		cprev = c->r_iv;
91*ba1276acSMatthew Dillon 		for (i = 0; i < blocks; i++, plain+=RIJNDAEL_BLOCKSIZE,
92*ba1276acSMatthew Dillon 		    cnow+=RIJNDAEL_BLOCKSIZE) {
93*ba1276acSMatthew Dillon 			for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
94*ba1276acSMatthew Dillon 				buf[j] = plain[j] ^ cprev[j];
95*ba1276acSMatthew Dillon 			rijndael_encrypt(&c->r_ctx, buf, cnow);
96*ba1276acSMatthew Dillon 			cprev = cnow;
97*ba1276acSMatthew Dillon 		}
98*ba1276acSMatthew Dillon 		memcpy(c->r_iv, cprev, RIJNDAEL_BLOCKSIZE);
99*ba1276acSMatthew Dillon 	} else {
100*ba1276acSMatthew Dillon 		cnow  = (u_char *) (src+len-RIJNDAEL_BLOCKSIZE);
101*ba1276acSMatthew Dillon 		plain = dest+len-RIJNDAEL_BLOCKSIZE;
102*ba1276acSMatthew Dillon 
103*ba1276acSMatthew Dillon 		memcpy(buf, cnow, RIJNDAEL_BLOCKSIZE);
104*ba1276acSMatthew Dillon 		for (i = blocks; i > 0; i--, cnow-=RIJNDAEL_BLOCKSIZE,
105*ba1276acSMatthew Dillon 		    plain-=RIJNDAEL_BLOCKSIZE) {
106*ba1276acSMatthew Dillon 			rijndael_decrypt(&c->r_ctx, cnow, plain);
107*ba1276acSMatthew Dillon 			ivp = (i == 1) ? c->r_iv : cnow-RIJNDAEL_BLOCKSIZE;
108*ba1276acSMatthew Dillon 			for (j = 0; j < RIJNDAEL_BLOCKSIZE; j++)
109*ba1276acSMatthew Dillon 				plain[j] ^= ivp[j];
110*ba1276acSMatthew Dillon 		}
111*ba1276acSMatthew Dillon 		memcpy(c->r_iv, buf, RIJNDAEL_BLOCKSIZE);
112*ba1276acSMatthew Dillon 	}
113*ba1276acSMatthew Dillon 	return (1);
114*ba1276acSMatthew Dillon }
115*ba1276acSMatthew Dillon 
116*ba1276acSMatthew Dillon static int
ssh_rijndael_cleanup(EVP_CIPHER_CTX * ctx)117*ba1276acSMatthew Dillon ssh_rijndael_cleanup(EVP_CIPHER_CTX *ctx)
118*ba1276acSMatthew Dillon {
119*ba1276acSMatthew Dillon 	struct ssh_rijndael_ctx *c;
120*ba1276acSMatthew Dillon 
121*ba1276acSMatthew Dillon 	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
122*ba1276acSMatthew Dillon 		memset(c, 0, sizeof(*c));
123*ba1276acSMatthew Dillon 		free(c);
124*ba1276acSMatthew Dillon 		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
125*ba1276acSMatthew Dillon 	}
126*ba1276acSMatthew Dillon 	return (1);
127*ba1276acSMatthew Dillon }
128*ba1276acSMatthew Dillon 
129*ba1276acSMatthew Dillon void
ssh_rijndael_iv(EVP_CIPHER_CTX * evp,int doset,u_char * iv,u_int len)130*ba1276acSMatthew Dillon ssh_rijndael_iv(EVP_CIPHER_CTX *evp, int doset, u_char * iv, u_int len)
131*ba1276acSMatthew Dillon {
132*ba1276acSMatthew Dillon 	struct ssh_rijndael_ctx *c;
133*ba1276acSMatthew Dillon 
134*ba1276acSMatthew Dillon 	if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
135*ba1276acSMatthew Dillon 		fatal("ssh_rijndael_iv: no context");
136*ba1276acSMatthew Dillon 	if (doset)
137*ba1276acSMatthew Dillon 		memcpy(c->r_iv, iv, len);
138*ba1276acSMatthew Dillon 	else
139*ba1276acSMatthew Dillon 		memcpy(iv, c->r_iv, len);
140*ba1276acSMatthew Dillon }
141*ba1276acSMatthew Dillon 
142*ba1276acSMatthew Dillon const EVP_CIPHER *
evp_rijndael(void)143*ba1276acSMatthew Dillon evp_rijndael(void)
144*ba1276acSMatthew Dillon {
145*ba1276acSMatthew Dillon 	static EVP_CIPHER rijndal_cbc;
146*ba1276acSMatthew Dillon 
147*ba1276acSMatthew Dillon 	memset(&rijndal_cbc, 0, sizeof(EVP_CIPHER));
148*ba1276acSMatthew Dillon 	rijndal_cbc.nid = NID_undef;
149*ba1276acSMatthew Dillon 	rijndal_cbc.block_size = RIJNDAEL_BLOCKSIZE;
150*ba1276acSMatthew Dillon 	rijndal_cbc.iv_len = RIJNDAEL_BLOCKSIZE;
151*ba1276acSMatthew Dillon 	rijndal_cbc.key_len = 16;
152*ba1276acSMatthew Dillon 	rijndal_cbc.init = ssh_rijndael_init;
153*ba1276acSMatthew Dillon 	rijndal_cbc.cleanup = ssh_rijndael_cleanup;
154*ba1276acSMatthew Dillon 	rijndal_cbc.do_cipher = ssh_rijndael_cbc;
155*ba1276acSMatthew Dillon #ifndef SSH_OLD_EVP
156*ba1276acSMatthew Dillon 	rijndal_cbc.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH |
157*ba1276acSMatthew Dillon 	    EVP_CIPH_ALWAYS_CALL_INIT | EVP_CIPH_CUSTOM_IV;
158*ba1276acSMatthew Dillon #endif
159*ba1276acSMatthew Dillon 	return (&rijndal_cbc);
160*ba1276acSMatthew Dillon }
161*ba1276acSMatthew Dillon #endif /* USE_BUILTIN_RIJNDAEL */
162