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