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