blob: 6a0f1f37b1dd3adc6a2e9b39b6655de3c2ce1a99 [file] [log] [blame]
Adam Langleyd0592972015-03-30 14:49:51 -07001/* $OpenBSD: cipher-3des1.c,v 1.12 2015/01/14 10:24:42 markus Exp $ */
Greg Hartmanbd77cf72015-02-25 13:21:06 -08002/*
3 * Copyright (c) 2003 Markus Friedl. All rights reserved.
4 *
Adam Langleyd0592972015-03-30 14:49:51 -07005 * 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.
Greg Hartmanbd77cf72015-02-25 13:21:06 -08008 *
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"
22
23#include <sys/types.h>
Adam Langleyd0592972015-03-30 14:49:51 -070024#include <string.h>
Greg Hartmanbd77cf72015-02-25 13:21:06 -080025#include <openssl/evp.h>
26
Adam Langleyd0592972015-03-30 14:49:51 -070027#include "ssherr.h"
Greg Hartmanbd77cf72015-02-25 13:21:06 -080028
29/*
30 * This is used by SSH1:
31 *
32 * What kind of triple DES are these 2 routines?
33 *
34 * Why is there a redundant initialization vector?
35 *
36 * If only iv3 was used, then, this would till effect have been
37 * outer-cbc. However, there is also a private iv1 == iv2 which
38 * perhaps makes differential analysis easier. On the other hand, the
39 * private iv1 probably makes the CRC-32 attack ineffective. This is a
40 * result of that there is no longer any known iv1 to use when
41 * choosing the X block.
42 */
43struct ssh1_3des_ctx
44{
45 EVP_CIPHER_CTX k1, k2, k3;
46};
47
48const EVP_CIPHER * evp_ssh1_3des(void);
Adam Langleyd0592972015-03-30 14:49:51 -070049int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int);
Greg Hartmanbd77cf72015-02-25 13:21:06 -080050
51static int
52ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv,
53 int enc)
54{
55 struct ssh1_3des_ctx *c;
56 u_char *k1, *k2, *k3;
57
58 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) {
Adam Langleyd0592972015-03-30 14:49:51 -070059 if ((c = calloc(1, sizeof(*c))) == NULL)
60 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080061 EVP_CIPHER_CTX_set_app_data(ctx, c);
62 }
63 if (key == NULL)
Adam Langleyd0592972015-03-30 14:49:51 -070064 return 1;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080065 if (enc == -1)
66 enc = ctx->encrypt;
67 k1 = k2 = k3 = (u_char *) key;
68 k2 += 8;
69 if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) {
70 if (enc)
71 k3 += 16;
72 else
73 k1 += 16;
74 }
75 EVP_CIPHER_CTX_init(&c->k1);
76 EVP_CIPHER_CTX_init(&c->k2);
77 EVP_CIPHER_CTX_init(&c->k3);
Greg Hartmanbd77cf72015-02-25 13:21:06 -080078 if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 ||
79 EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 ||
80 EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) {
Adam Langleyd0592972015-03-30 14:49:51 -070081 explicit_bzero(c, sizeof(*c));
82 free(c);
Greg Hartmanbd77cf72015-02-25 13:21:06 -080083 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
Adam Langleyd0592972015-03-30 14:49:51 -070084 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080085 }
Adam Langleyd0592972015-03-30 14:49:51 -070086 return 1;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080087}
88
89static int
Adam Langleyd0592972015-03-30 14:49:51 -070090ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, size_t len)
Greg Hartmanbd77cf72015-02-25 13:21:06 -080091{
92 struct ssh1_3des_ctx *c;
93
Adam Langleyd0592972015-03-30 14:49:51 -070094 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL)
95 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -080096 if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 ||
97 EVP_Cipher(&c->k2, dest, dest, len) == 0 ||
98 EVP_Cipher(&c->k3, dest, dest, len) == 0)
Adam Langleyd0592972015-03-30 14:49:51 -070099 return 0;
100 return 1;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800101}
102
103static int
104ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx)
105{
106 struct ssh1_3des_ctx *c;
107
108 if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) {
109 EVP_CIPHER_CTX_cleanup(&c->k1);
110 EVP_CIPHER_CTX_cleanup(&c->k2);
111 EVP_CIPHER_CTX_cleanup(&c->k3);
Adam Langleyd0592972015-03-30 14:49:51 -0700112 explicit_bzero(c, sizeof(*c));
113 free(c);
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800114 EVP_CIPHER_CTX_set_app_data(ctx, NULL);
115 }
Adam Langleyd0592972015-03-30 14:49:51 -0700116 return 1;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800117}
118
Adam Langleyd0592972015-03-30 14:49:51 -0700119int
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800120ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len)
121{
122 struct ssh1_3des_ctx *c;
123
124 if (len != 24)
Adam Langleyd0592972015-03-30 14:49:51 -0700125 return SSH_ERR_INVALID_ARGUMENT;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800126 if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL)
Adam Langleyd0592972015-03-30 14:49:51 -0700127 return SSH_ERR_INTERNAL_ERROR;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800128 if (doset) {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800129 memcpy(c->k1.iv, iv, 8);
130 memcpy(c->k2.iv, iv + 8, 8);
131 memcpy(c->k3.iv, iv + 16, 8);
132 } else {
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800133 memcpy(iv, c->k1.iv, 8);
134 memcpy(iv + 8, c->k2.iv, 8);
135 memcpy(iv + 16, c->k3.iv, 8);
136 }
Adam Langleyd0592972015-03-30 14:49:51 -0700137 return 0;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800138}
139
140const EVP_CIPHER *
141evp_ssh1_3des(void)
142{
143 static EVP_CIPHER ssh1_3des;
144
Adam Langleyd0592972015-03-30 14:49:51 -0700145 memset(&ssh1_3des, 0, sizeof(ssh1_3des));
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800146 ssh1_3des.nid = NID_undef;
147 ssh1_3des.block_size = 8;
148 ssh1_3des.iv_len = 0;
149 ssh1_3des.key_len = 16;
150 ssh1_3des.init = ssh1_3des_init;
151 ssh1_3des.cleanup = ssh1_3des_cleanup;
152 ssh1_3des.do_cipher = ssh1_3des_cbc;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800153 ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH;
Adam Langleyd0592972015-03-30 14:49:51 -0700154 return &ssh1_3des;
Greg Hartmanbd77cf72015-02-25 13:21:06 -0800155}