]> andersk Git - openssh.git/blame - cipher-3des1.c
- stevesk@cvs.openbsd.org 2006/08/01 23:36:12
[openssh.git] / cipher-3des1.c
CommitLineData
00146caa 1/* $OpenBSD: cipher-3des1.c,v 1.5 2006/07/22 20:48:22 stevesk Exp $ */
3b6e3da9 2/*
3 * Copyright (c) 2003 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
3b6e3da9 27
28#include <openssl/evp.h>
00146caa 29
30#include <string.h>
31
3b6e3da9 32#include "xmalloc.h"
33#include "log.h"
34
0a23d79f 35#if OPENSSL_VERSION_NUMBER < 0x00906000L
36#define SSH_OLD_EVP
37#endif
38
3b6e3da9 39/*
40 * This is used by SSH1:
41 *
42 * What kind of triple DES are these 2 routines?
43 *
44 * Why is there a redundant initialization vector?
45 *
46 * If only iv3 was used, then, this would till effect have been
47 * outer-cbc. However, there is also a private iv1 == iv2 which
48 * perhaps makes differential analysis easier. On the other hand, the
49 * private iv1 probably makes the CRC-32 attack ineffective. This is a
50 * result of that there is no longer any known iv1 to use when
51 * choosing the X block.
52 */
53struct ssh1_3des_ctx
54{
55 EVP_CIPHER_CTX k1, k2, k3;
56};
57
58const EVP_CIPHER * evp_ssh1_3des(void);
59void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
60
61static int
62ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
63 int enc)
64{
65 struct ssh1_3des_ctx *c;
66 u_char *k1, *k2, *k3;
67
68 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
69 c = xmalloc(sizeof(*c));
70 EVP_CIPHER_CTX_set_app_data(ctx, c);
71 }
72 if (key == NULL)
73 return (1);
74 if (enc == -1)
75 enc = ctx->encrypt;
76 k1 = k2 = k3 = (u_char *) key;
77 k2 += 8;
78 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
79 if (enc)
80 k3 += 16;
81 else
82 k1 += 16;
83 }
84 EVP_CIPHER_CTX_init(&c->k1);
85 EVP_CIPHER_CTX_init(&c->k2);
86 EVP_CIPHER_CTX_init(&c->k3);
87#ifdef SSH_OLD_EVP
88 EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
89 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
90 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
91#else
92 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
93 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
94 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
95 memset(c, 0, sizeof(*c));
96 xfree(c);
97 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
98 return (0);
99 }
100#endif
101 return (1);
102}
103
104static int
105ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
106{
107 struct ssh1_3des_ctx *c;
108
109 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
110 error("ssh1_3des_cbc: no context");
111 return (0);
112 }
113#ifdef SSH_OLD_EVP
114 EVP_Cipher(&c->k1, dest, (u_char *)src, len);
115 EVP_Cipher(&c->k2, dest, dest, len);
116 EVP_Cipher(&c->k3, dest, dest, len);
117#else
118 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
119 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
120 EVP_Cipher(&c->k3, dest, dest, len) == 0)
121 return (0);
122#endif
123 return (1);
124}
125
126static int
127ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
128{
129 struct ssh1_3des_ctx *c;
130
131 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
1dd5f021 132 EVP_CIPHER_CTX_cleanup(&c->k1);
133 EVP_CIPHER_CTX_cleanup(&c->k2);
134 EVP_CIPHER_CTX_cleanup(&c->k3);
3b6e3da9 135 memset(c, 0, sizeof(*c));
136 xfree(c);
137 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
138 }
139 return (1);
140}
141
142void
143ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
144{
145 struct ssh1_3des_ctx *c;
146
147 if (len != 24)
148 fatal("%s: bad 3des iv length: %d", __func__, len);
149 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
150 fatal("%s: no 3des context", __func__);
151 if (doset) {
152 debug3("%s: Installed 3DES IV", __func__);
153 memcpy(c->k1.iv, iv, 8);
154 memcpy(c->k2.iv, iv + 8, 8);
155 memcpy(c->k3.iv, iv + 16, 8);
156 } else {
157 debug3("%s: Copying 3DES IV", __func__);
158 memcpy(iv, c->k1.iv, 8);
159 memcpy(iv + 8, c->k2.iv, 8);
160 memcpy(iv + 16, c->k3.iv, 8);
161 }
162}
163
164const EVP_CIPHER *
165evp_ssh1_3des(void)
166{
167 static EVP_CIPHER ssh1_3des;
168
169 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
170 ssh1_3des.nid = NID_undef;
171 ssh1_3des.block_size = 8;
172 ssh1_3des.iv_len = 0;
173 ssh1_3des.key_len = 16;
174 ssh1_3des.init = ssh1_3des_init;
175 ssh1_3des.cleanup = ssh1_3des_cleanup;
176 ssh1_3des.do_cipher = ssh1_3des_cbc;
177#ifndef SSH_OLD_EVP
178 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
179#endif
180 return (&ssh1_3des);
181}
This page took 0.166026 seconds and 5 git commands to generate.