ssh has moved
[netbsd-mini2440.git] / crypto / dist / ssh / cipher-3des1.c
blobc284aaa06fb39dadaaf826303959f880f3a34122
1 /* $NetBSD$ */
2 /* $OpenBSD: cipher-3des1.c,v 1.6 2006/08/03 03:34:42 deraadt Exp $ */
3 /*
4 * Copyright (c) 2003 Markus Friedl. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "includes.h"
28 __RCSID("$NetBSD$");
30 #include <sys/types.h>
32 #include <openssl/evp.h>
34 #include <string.h>
36 #include "xmalloc.h"
37 #include "log.h"
40 * This is used by SSH1:
42 * What kind of triple DES are these 2 routines?
44 * Why is there a redundant initialization vector?
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.
53 struct ssh1_3des_ctx
55 EVP_CIPHER_CTX k1, k2, k3;
58 const EVP_CIPHER * evp_ssh1_3des(void);
59 void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
61 static int
62 ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
63 int enc)
65 struct ssh1_3des_ctx *c;
66 u_char *k1, *k2, *k3;
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);
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;
84 EVP_CIPHER_CTX_init(&c->k1);
85 EVP_CIPHER_CTX_init(&c->k2);
86 EVP_CIPHER_CTX_init(&c->k3);
87 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
88 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
89 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
90 memset(c, 0, sizeof(*c));
91 xfree(c);
92 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
93 return (0);
95 return (1);
98 static int
99 ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len)
101 struct ssh1_3des_ctx *c;
103 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
104 error("ssh1_3des_cbc: no context");
105 return (0);
107 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
108 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
109 EVP_Cipher(&c->k3, dest, dest, len) == 0)
110 return (0);
111 return (1);
114 static int
115 ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
117 struct ssh1_3des_ctx *c;
119 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
120 EVP_CIPHER_CTX_cleanup(&c->k1);
121 EVP_CIPHER_CTX_cleanup(&c->k2);
122 EVP_CIPHER_CTX_cleanup(&c->k3);
123 memset(c, 0, sizeof(*c));
124 xfree(c);
125 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
127 return (1);
130 void
131 ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
133 struct ssh1_3des_ctx *c;
135 if (len != 24)
136 fatal("%s: bad 3des iv length: %d", __func__, len);
137 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
138 fatal("%s: no 3des context", __func__);
139 if (doset) {
140 debug3("%s: Installed 3DES IV", __func__);
141 memcpy(c->k1.iv, iv, 8);
142 memcpy(c->k2.iv, iv + 8, 8);
143 memcpy(c->k3.iv, iv + 16, 8);
144 } else {
145 debug3("%s: Copying 3DES IV", __func__);
146 memcpy(iv, c->k1.iv, 8);
147 memcpy(iv + 8, c->k2.iv, 8);
148 memcpy(iv + 16, c->k3.iv, 8);
152 const EVP_CIPHER *
153 evp_ssh1_3des(void)
155 static EVP_CIPHER ssh1_3des;
157 memset(&ssh1_3des, 0, sizeof(EVP_CIPHER));
158 ssh1_3des.nid = NID_undef;
159 ssh1_3des.block_size = 8;
160 ssh1_3des.iv_len = 0;
161 ssh1_3des.key_len = 16;
162 ssh1_3des.init = ssh1_3des_init;
163 ssh1_3des.cleanup = ssh1_3des_cleanup;
164 ssh1_3des.do_cipher = ssh1_3des_cbc;
165 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
166 return (&ssh1_3des);