blob: 9fcc2785a3fb53faea703cf80f8212bf87dc2766 [file] [log] [blame]
markus@openbsd.org60c2c4e2015-01-14 10:24:42 +00001/* $OpenBSD: cipher-3des1.c,v 1.12 2015/01/14 10:24:42 markus Exp $ */
Damien Miller3a3261f2003-05-15 13:37:19 +10002/*
3 * Copyright (c) 2003 Markus Friedl. All rights reserved.
4 *
markus@openbsd.org60c2c4e2015-01-14 10:24:42 +00005 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
Damien Miller3a3261f2003-05-15 13:37:19 +10008 *
9 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
11 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
12 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
13 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
14 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
18 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 */
20
21#include "includes.h"
Damien Miller3a3261f2003-05-15 13:37:19 +100022
Darren Tuckerd9246402016-10-28 13:38:19 +110023#ifdef WITH_SSH1
24
Damien Millerd7834352006-08-05 12:39:39 +100025#include <sys/types.h>
markus@openbsd.org60c2c4e2015-01-14 10:24:42 +000026#include <string.h>
Damien Miller3a3261f2003-05-15 13:37:19 +100027#include <openssl/evp.h>
Damien Millere3476ed2006-07-24 14:13:33 +100028
Damien Miller86687062014-07-02 15:28:02 +100029#include "ssherr.h"
Damien Miller5c3a5582003-09-23 22:12:38 +100030
Damien Miller3a3261f2003-05-15 13:37:19 +100031/*
32 * This is used by SSH1:
33 *
34 * What kind of triple DES are these 2 routines?
35 *
36 * Why is there a redundant initialization vector?
37 *
38 * If only iv3 was used, then, this would till effect have been
39 * outer-cbc. However, there is also a private iv1 == iv2 which
40 * perhaps makes differential analysis easier. On the other hand, the
41 * private iv1 probably makes the CRC-32 attack ineffective. This is a
42 * result of that there is no longer any known iv1 to use when
43 * choosing the X block.
44 */
45struct ssh1_3des_ctx
46{
47 EVP_CIPHER_CTX k1, k2, k3;
48};
49
50const EVP_CIPHER * evp_ssh1_3des(void);
Damien Miller86687062014-07-02 15:28:02 +100051int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
Damien Miller3a3261f2003-05-15 13:37:19 +100052
53static int
54ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
55 int enc)
56{
57 struct ssh1_3des_ctx *c;
58 u_char *k1, *k2, *k3;
59
60 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
Damien Miller86687062014-07-02 15:28:02 +100061 if ((c = calloc(1, sizeof(*c))) == NULL)
62 return 0;
Damien Miller3a3261f2003-05-15 13:37:19 +100063 EVP_CIPHER_CTX_set_app_data(ctx, c);
64 }
65 if (key == NULL)
Damien Miller86687062014-07-02 15:28:02 +100066 return 1;
Damien Miller3a3261f2003-05-15 13:37:19 +100067 if (enc == -1)
68 enc = ctx->encrypt;
69 k1 = k2 = k3 = (u_char *) key;
70 k2 += 8;
71 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
72 if (enc)
73 k3 += 16;
74 else
75 k1 += 16;
76 }
77 EVP_CIPHER_CTX_init(&c->k1);
78 EVP_CIPHER_CTX_init(&c->k2);
79 EVP_CIPHER_CTX_init(&c->k3);
Damien Miller3a3261f2003-05-15 13:37:19 +100080 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
81 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
82 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
Damien Millera5103f42014-02-04 11:20:14 +110083 explicit_bzero(c, sizeof(*c));
Darren Tuckera627d422013-06-02 07:31:17 +100084 free(c);
Damien Miller3a3261f2003-05-15 13:37:19 +100085 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
Damien Miller86687062014-07-02 15:28:02 +100086 return 0;
Damien Miller3a3261f2003-05-15 13:37:19 +100087 }
Damien Miller86687062014-07-02 15:28:02 +100088 return 1;
Damien Miller3a3261f2003-05-15 13:37:19 +100089}
90
91static int
Damien Miller86687062014-07-02 15:28:02 +100092ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, size_t len)
Damien Miller3a3261f2003-05-15 13:37:19 +100093{
94 struct ssh1_3des_ctx *c;
95
Damien Miller86687062014-07-02 15:28:02 +100096 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
97 return 0;
Damien Miller3a3261f2003-05-15 13:37:19 +100098 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
99 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
100 EVP_Cipher(&c->k3, dest, dest, len) == 0)
Damien Miller86687062014-07-02 15:28:02 +1000101 return 0;
102 return 1;
Damien Miller3a3261f2003-05-15 13:37:19 +1000103}
104
105static int
106ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
107{
108 struct ssh1_3des_ctx *c;
109
110 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
Darren Tuckera32e19c2003-12-31 11:36:00 +1100111 EVP_CIPHER_CTX_cleanup(&c->k1);
112 EVP_CIPHER_CTX_cleanup(&c->k2);
113 EVP_CIPHER_CTX_cleanup(&c->k3);
Damien Millera5103f42014-02-04 11:20:14 +1100114 explicit_bzero(c, sizeof(*c));
Darren Tuckera627d422013-06-02 07:31:17 +1000115 free(c);
Damien Miller3a3261f2003-05-15 13:37:19 +1000116 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
117 }
Damien Miller86687062014-07-02 15:28:02 +1000118 return 1;
Damien Miller3a3261f2003-05-15 13:37:19 +1000119}
120
Damien Miller86687062014-07-02 15:28:02 +1000121int
Damien Miller3a3261f2003-05-15 13:37:19 +1000122ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
123{
124 struct ssh1_3des_ctx *c;
125
126 if (len != 24)
Damien Miller86687062014-07-02 15:28:02 +1000127 return SSH_ERR_INVALID_ARGUMENT;
Damien Miller3a3261f2003-05-15 13:37:19 +1000128 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
Damien Miller86687062014-07-02 15:28:02 +1000129 return SSH_ERR_INTERNAL_ERROR;
Damien Miller3a3261f2003-05-15 13:37:19 +1000130 if (doset) {
Damien Miller3a3261f2003-05-15 13:37:19 +1000131 memcpy(c->k1.iv, iv, 8);
132 memcpy(c->k2.iv, iv + 8, 8);
133 memcpy(c->k3.iv, iv + 16, 8);
134 } else {
Damien Miller3a3261f2003-05-15 13:37:19 +1000135 memcpy(iv, c->k1.iv, 8);
136 memcpy(iv + 8, c->k2.iv, 8);
137 memcpy(iv + 16, c->k3.iv, 8);
138 }
Damien Miller86687062014-07-02 15:28:02 +1000139 return 0;
Damien Miller3a3261f2003-05-15 13:37:19 +1000140}
141
142const EVP_CIPHER *
143evp_ssh1_3des(void)
144{
145 static EVP_CIPHER ssh1_3des;
146
markus@openbsd.org60c2c4e2015-01-14 10:24:42 +0000147 memset(&ssh1_3des, 0, sizeof(ssh1_3des));
Damien Miller3a3261f2003-05-15 13:37:19 +1000148 ssh1_3des.nid = NID_undef;
149 ssh1_3des.block_size = 8;
150 ssh1_3des.iv_len = 0;
151 ssh1_3des.key_len = 16;
152 ssh1_3des.init = ssh1_3des_init;
153 ssh1_3des.cleanup = ssh1_3des_cleanup;
154 ssh1_3des.do_cipher = ssh1_3des_cbc;
Damien Miller3a3261f2003-05-15 13:37:19 +1000155 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
Damien Miller86687062014-07-02 15:28:02 +1000156 return &ssh1_3des;
Damien Miller3a3261f2003-05-15 13:37:19 +1000157}
Darren Tuckerd9246402016-10-28 13:38:19 +1100158#endif /* WITH_SSH1 */