cipher-3des1.c revision 162852
1162852Sdes/* $OpenBSD: cipher-3des1.c,v 1.6 2006/08/03 03:34:42 deraadt Exp $ */
2124208Sdes/*
3124208Sdes * Copyright (c) 2003 Markus Friedl.  All rights reserved.
4124208Sdes *
5124208Sdes * Redistribution and use in source and binary forms, with or without
6124208Sdes * modification, are permitted provided that the following conditions
7124208Sdes * are met:
8124208Sdes * 1. Redistributions of source code must retain the above copyright
9124208Sdes *    notice, this list of conditions and the following disclaimer.
10124208Sdes * 2. Redistributions in binary form must reproduce the above copyright
11124208Sdes *    notice, this list of conditions and the following disclaimer in the
12124208Sdes *    documentation and/or other materials provided with the distribution.
13124208Sdes *
14124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24124208Sdes */
25124208Sdes
26124208Sdes#include "includes.h"
27124208Sdes
28162852Sdes#include <sys/types.h>
29162852Sdes
30124208Sdes#include <openssl/evp.h>
31162852Sdes
32162852Sdes#include <stdarg.h>
33162852Sdes#include <string.h>
34162852Sdes
35124208Sdes#include "xmalloc.h"
36124208Sdes#include "log.h"
37124208Sdes
38124208Sdes#if OPENSSL_VERSION_NUMBER < 0x00906000L
39124208Sdes#define SSH_OLD_EVP
40124208Sdes#endif
41124208Sdes
42124208Sdes/*
43124208Sdes * This is used by SSH1:
44124208Sdes *
45124208Sdes * What kind of triple DES are these 2 routines?
46124208Sdes *
47124208Sdes * Why is there a redundant initialization vector?
48124208Sdes *
49124208Sdes * If only iv3 was used, then, this would till effect have been
50124208Sdes * outer-cbc. However, there is also a private iv1 == iv2 which
51124208Sdes * perhaps makes differential analysis easier. On the other hand, the
52124208Sdes * private iv1 probably makes the CRC-32 attack ineffective. This is a
53124208Sdes * result of that there is no longer any known iv1 to use when
54124208Sdes * choosing the X block.
55124208Sdes */
56124208Sdesstruct ssh1_3des_ctx
57124208Sdes{
58124208Sdes	EVP_CIPHER_CTX	k1, k2, k3;
59124208Sdes};
60124208Sdes
61124208Sdesconst EVP_CIPHER * evp_ssh1_3des(void);
62124208Sdesvoid ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
63124208Sdes
64124208Sdesstatic int
65124208Sdesssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
66124208Sdes    int enc)
67124208Sdes{
68124208Sdes	struct ssh1_3des_ctx *c;
69124208Sdes	u_char *k1, *k2, *k3;
70124208Sdes
71124208Sdes	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
72124208Sdes		c = xmalloc(sizeof(*c));
73124208Sdes		EVP_CIPHER_CTX_set_app_data(ctx, c);
74124208Sdes	}
75124208Sdes	if (key == NULL)
76124208Sdes		return (1);
77124208Sdes	if (enc == -1)
78124208Sdes		enc = ctx->encrypt;
79124208Sdes	k1 = k2 = k3 = (u_char *) key;
80124208Sdes	k2 += 8;
81124208Sdes	if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
82124208Sdes		if (enc)
83124208Sdes			k3 += 16;
84124208Sdes		else
85124208Sdes			k1 += 16;
86124208Sdes	}
87124208Sdes	EVP_CIPHER_CTX_init(&c->k1);
88124208Sdes	EVP_CIPHER_CTX_init(&c->k2);
89124208Sdes	EVP_CIPHER_CTX_init(&c->k3);
90124208Sdes#ifdef SSH_OLD_EVP
91124208Sdes	EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
92124208Sdes	EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
93124208Sdes	EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
94124208Sdes#else
95124208Sdes	if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
96124208Sdes	    EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
97124208Sdes	    EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
98124208Sdes		memset(c, 0, sizeof(*c));
99124208Sdes		xfree(c);
100124208Sdes		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
101124208Sdes		return (0);
102124208Sdes	}
103124208Sdes#endif
104124208Sdes	return (1);
105124208Sdes}
106124208Sdes
107124208Sdesstatic int
108124208Sdesssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
109124208Sdes{
110124208Sdes	struct ssh1_3des_ctx *c;
111124208Sdes
112124208Sdes	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
113124208Sdes		error("ssh1_3des_cbc: no context");
114124208Sdes		return (0);
115124208Sdes	}
116124208Sdes#ifdef SSH_OLD_EVP
117124208Sdes	EVP_Cipher(&c->k1, dest, (u_char *)src, len);
118124208Sdes	EVP_Cipher(&c->k2, dest, dest, len);
119124208Sdes	EVP_Cipher(&c->k3, dest, dest, len);
120124208Sdes#else
121124208Sdes	if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
122124208Sdes	    EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
123124208Sdes	    EVP_Cipher(&c->k3, dest, dest, len) == 0)
124124208Sdes		return (0);
125124208Sdes#endif
126124208Sdes	return (1);
127124208Sdes}
128124208Sdes
129124208Sdesstatic int
130124208Sdesssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
131124208Sdes{
132124208Sdes	struct ssh1_3des_ctx *c;
133124208Sdes
134124208Sdes	if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
135126274Sdes		EVP_CIPHER_CTX_cleanup(&c->k1);
136126274Sdes		EVP_CIPHER_CTX_cleanup(&c->k2);
137126274Sdes		EVP_CIPHER_CTX_cleanup(&c->k3);
138124208Sdes		memset(c, 0, sizeof(*c));
139124208Sdes		xfree(c);
140124208Sdes		EVP_CIPHER_CTX_set_app_data(ctx, NULL);
141124208Sdes	}
142124208Sdes	return (1);
143124208Sdes}
144124208Sdes
145124208Sdesvoid
146124208Sdesssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
147124208Sdes{
148124208Sdes	struct ssh1_3des_ctx *c;
149124208Sdes
150124208Sdes	if (len != 24)
151124208Sdes		fatal("%s: bad 3des iv length: %d", __func__, len);
152124208Sdes	if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
153124208Sdes		fatal("%s: no 3des context", __func__);
154124208Sdes	if (doset) {
155124208Sdes		debug3("%s: Installed 3DES IV", __func__);
156124208Sdes		memcpy(c->k1.iv, iv, 8);
157124208Sdes		memcpy(c->k2.iv, iv + 8, 8);
158124208Sdes		memcpy(c->k3.iv, iv + 16, 8);
159124208Sdes	} else {
160124208Sdes		debug3("%s: Copying 3DES IV", __func__);
161124208Sdes		memcpy(iv, c->k1.iv, 8);
162124208Sdes		memcpy(iv + 8, c->k2.iv, 8);
163124208Sdes		memcpy(iv + 16, c->k3.iv, 8);
164124208Sdes	}
165124208Sdes}
166124208Sdes
167124208Sdesconst EVP_CIPHER *
168124208Sdesevp_ssh1_3des(void)
169124208Sdes{
170124208Sdes	static EVP_CIPHER ssh1_3des;
171124208Sdes
172124208Sdes	memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
173124208Sdes	ssh1_3des.nid = NID_undef;
174124208Sdes	ssh1_3des.block_size = 8;
175124208Sdes	ssh1_3des.iv_len = 0;
176124208Sdes	ssh1_3des.key_len = 16;
177124208Sdes	ssh1_3des.init = ssh1_3des_init;
178124208Sdes	ssh1_3des.cleanup = ssh1_3des_cleanup;
179124208Sdes	ssh1_3des.do_cipher = ssh1_3des_cbc;
180124208Sdes#ifndef SSH_OLD_EVP
181124208Sdes	ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
182124208Sdes#endif
183124208Sdes	return (&ssh1_3des);
184124208Sdes}
185