Switch xorg to modular, so that it fits with the prebuilt packages.
[dragonfly.git] / crypto / openssh-4 / cipher-3des1.c
blobfc16e20d7e790762ec7af1e73918e68577d28f0c
1 /* $OpenBSD: cipher-3des1.c,v 1.6 2006/08/03 03:34:42 deraadt Exp $ */
2 /*
3 * Copyright (c) 2003 Markus Friedl. All rights reserved.
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.
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.
26 #include "includes.h"
28 #include <sys/types.h>
30 #include <openssl/evp.h>
32 #include <stdarg.h>
33 #include <string.h>
35 #include "xmalloc.h"
36 #include "log.h"
38 #if OPENSSL_VERSION_NUMBER < 0x00906000L
39 #define SSH_OLD_EVP
40 #endif
43 * This is used by SSH1:
45 * What kind of triple DES are these 2 routines?
47 * Why is there a redundant initialization vector?
49 * If only iv3 was used, then, this would till effect have been
50 * outer-cbc. However, there is also a private iv1 == iv2 which
51 * perhaps makes differential analysis easier. On the other hand, the
52 * private iv1 probably makes the CRC-32 attack ineffective. This is a
53 * result of that there is no longer any known iv1 to use when
54 * choosing the X block.
56 struct ssh1_3des_ctx
58 EVP_CIPHER_CTX k1, k2, k3;
61 const EVP_CIPHER * evp_ssh1_3des(void);
62 void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
64 static int
65 ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
66 int enc)
68 struct ssh1_3des_ctx *c;
69 u_char *k1, *k2, *k3;
71 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
72 c = xmalloc(sizeof(*c));
73 EVP_CIPHER_CTX_set_app_data(ctx, c);
75 if (key == NULL)
76 return (1);
77 if (enc == -1)
78 enc = ctx->encrypt;
79 k1 = k2 = k3 = (u_char *) key;
80 k2 += 8;
81 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
82 if (enc)
83 k3 += 16;
84 else
85 k1 += 16;
87 EVP_CIPHER_CTX_init(&c->k1);
88 EVP_CIPHER_CTX_init(&c->k2);
89 EVP_CIPHER_CTX_init(&c->k3);
90 #ifdef SSH_OLD_EVP
91 EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc);
92 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc);
93 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc);
94 #else
95 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
96 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
97 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
98 memset(c, 0, sizeof(*c));
99 xfree(c);
100 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
101 return (0);
103 #endif
104 return (1);
107 static int
108 ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
110 struct ssh1_3des_ctx *c;
112 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
113 error("ssh1_3des_cbc: no context");
114 return (0);
116 #ifdef SSH_OLD_EVP
117 EVP_Cipher(&c->k1, dest, (u_char *)src, len);
118 EVP_Cipher(&c->k2, dest, dest, len);
119 EVP_Cipher(&c->k3, dest, dest, len);
120 #else
121 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
122 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
123 EVP_Cipher(&c->k3, dest, dest, len) == 0)
124 return (0);
125 #endif
126 return (1);
129 static int
130 ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
132 struct ssh1_3des_ctx *c;
134 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
135 EVP_CIPHER_CTX_cleanup(&c->k1);
136 EVP_CIPHER_CTX_cleanup(&c->k2);
137 EVP_CIPHER_CTX_cleanup(&c->k3);
138 memset(c, 0, sizeof(*c));
139 xfree(c);
140 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
142 return (1);
145 void
146 ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
148 struct ssh1_3des_ctx *c;
150 if (len != 24)
151 fatal("%s: bad 3des iv length: %d", __func__, len);
152 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
153 fatal("%s: no 3des context", __func__);
154 if (doset) {
155 debug3("%s: Installed 3DES IV", __func__);
156 memcpy(c->k1.iv, iv, 8);
157 memcpy(c->k2.iv, iv + 8, 8);
158 memcpy(c->k3.iv, iv + 16, 8);
159 } else {
160 debug3("%s: Copying 3DES IV", __func__);
161 memcpy(iv, c->k1.iv, 8);
162 memcpy(iv + 8, c->k2.iv, 8);
163 memcpy(iv + 16, c->k3.iv, 8);
167 const EVP_CIPHER *
168 evp_ssh1_3des(void)
170 static EVP_CIPHER ssh1_3des;
172 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
173 ssh1_3des.nid = NID_undef;
174 ssh1_3des.block_size = 8;
175 ssh1_3des.iv_len = 0;
176 ssh1_3des.key_len = 16;
177 ssh1_3des.init = ssh1_3des_init;
178 ssh1_3des.cleanup = ssh1_3des_cleanup;
179 ssh1_3des.do_cipher = ssh1_3des_cbc;
180 #ifndef SSH_OLD_EVP
181 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
182 #endif
183 return (&ssh1_3des);