blob: f815e8ae524e786439da56a5f59fa940ef3f5732 [file] [log] [blame]
Damien Miller3a3261f2003-05-15 13:37:19 +10001/*
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"
Darren Tuckera32e19c2003-12-31 11:36:00 +110026RCSID("$OpenBSD: cipher-3des1.c,v 1.2 2003/12/22 20:29:55 markus Exp $");
Damien Miller3a3261f2003-05-15 13:37:19 +100027
28#include <openssl/evp.h>
29#include "xmalloc.h"
30#include "log.h"
31
Damien Miller5c3a5582003-09-23 22:12:38 +100032#if OPENSSL_VERSION_NUMBER < 0x00906000L
33#define SSH_OLD_EVP
34#endif
35
Damien Miller3a3261f2003-05-15 13:37:19 +100036/*
37 * This is used by SSH1:
38 *
39 * What kind of triple DES are these 2 routines?
40 *
41 * Why is there a redundant initialization vector?
42 *
43 * If only iv3 was used, then, this would till effect have been
44 * outer-cbc. However, there is also a private iv1 == iv2 which
45 * perhaps makes differential analysis easier. On the other hand, the
46 * private iv1 probably makes the CRC-32 attack ineffective. This is a
47 * result of that there is no longer any known iv1 to use when
48 * choosing the X block.
49 */
50struct ssh1_3des_ctx
51{
52 EVP_CIPHER_CTX k1, k2, k3;
53};
54
55const EVP_CIPHER * evp_ssh1_3des(void);
56void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
57
58static int
59ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
60 int enc)
61{
62 struct ssh1_3des_ctx *c;
63 u_char *k1, *k2, *k3;
64
65 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
66 c = xmalloc(sizeof(*c));
67 EVP_CIPHER_CTX_set_app_data(ctx, c);
68 }
69 if (key == NULL)
70 return (1);
71 if (enc == -1)
72 enc = ctx->encrypt;
73 k1 = k2 = k3 = (u_char *) key;
74 k2 += 8;
75 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
76 if (enc)
77 k3 += 16;
78 else
79 k1 += 16;
80 }
81 EVP_CIPHER_CTX_init(&c->k1);
82 EVP_CIPHER_CTX_init(&c->k2);
83 EVP_CIPHER_CTX_init(&c->k3);
84#ifdef SSH_OLD_EVP
85 EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
86 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
87 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
88#else
89 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
90 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
91 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
92 memset(c, 0, sizeof(*c));
93 xfree(c);
94 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
95 return (0);
96 }
97#endif
98 return (1);
99}
100
101static int
102ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
103{
104 struct ssh1_3des_ctx *c;
105
106 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
107 error("ssh1_3des_cbc: no context");
108 return (0);
109 }
110#ifdef SSH_OLD_EVP
111 EVP_Cipher(&c->k1, dest, (u_char *)src, len);
112 EVP_Cipher(&c->k2, dest, dest, len);
113 EVP_Cipher(&c->k3, dest, dest, len);
114#else
115 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
116 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
117 EVP_Cipher(&c->k3, dest, dest, len) == 0)
118 return (0);
119#endif
120 return (1);
121}
122
123static int
124ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
125{
126 struct ssh1_3des_ctx *c;
127
128 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
Darren Tuckera32e19c2003-12-31 11:36:00 +1100129 EVP_CIPHER_CTX_cleanup(&c->k1);
130 EVP_CIPHER_CTX_cleanup(&c->k2);
131 EVP_CIPHER_CTX_cleanup(&c->k3);
Damien Miller3a3261f2003-05-15 13:37:19 +1000132 memset(c, 0, sizeof(*c));
133 xfree(c);
134 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
135 }
136 return (1);
137}
138
139void
140ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
141{
142 struct ssh1_3des_ctx *c;
143
144 if (len != 24)
145 fatal("%s: bad 3des iv length: %d", __func__, len);
146 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
147 fatal("%s: no 3des context", __func__);
148 if (doset) {
149 debug3("%s: Installed 3DES IV", __func__);
150 memcpy(c->k1.iv, iv, 8);
151 memcpy(c->k2.iv, iv + 8, 8);
152 memcpy(c->k3.iv, iv + 16, 8);
153 } else {
154 debug3("%s: Copying 3DES IV", __func__);
155 memcpy(iv, c->k1.iv, 8);
156 memcpy(iv + 8, c->k2.iv, 8);
157 memcpy(iv + 16, c->k3.iv, 8);
158 }
159}
160
161const EVP_CIPHER *
162evp_ssh1_3des(void)
163{
164 static EVP_CIPHER ssh1_3des;
165
166 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
167 ssh1_3des.nid = NID_undef;
168 ssh1_3des.block_size = 8;
169 ssh1_3des.iv_len = 0;
170 ssh1_3des.key_len = 16;
171 ssh1_3des.init = ssh1_3des_init;
172 ssh1_3des.cleanup = ssh1_3des_cleanup;
173 ssh1_3des.do_cipher = ssh1_3des_cbc;
174#ifndef SSH_OLD_EVP
175 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
176#endif
177 return (&ssh1_3des);
178}