Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 1 | /* $OpenBSD: cipher-3des1.c,v 1.9 2013/11/08 00:39:15 djm Exp $ */ |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 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" |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 27 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 30 | #include <openssl/evp.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 31 | |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 32 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 33 | #include <string.h> |
| 34 | |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 35 | #include "xmalloc.h" |
| 36 | #include "log.h" |
| 37 | |
Darren Tucker | da05f48 | 2007-03-13 18:50:04 +1100 | [diff] [blame] | 38 | #include "openbsd-compat/openssl-compat.h" |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 39 | |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 40 | /* |
| 41 | * This is used by SSH1: |
| 42 | * |
| 43 | * What kind of triple DES are these 2 routines? |
| 44 | * |
| 45 | * Why is there a redundant initialization vector? |
| 46 | * |
| 47 | * If only iv3 was used, then, this would till effect have been |
| 48 | * outer-cbc. However, there is also a private iv1 == iv2 which |
| 49 | * perhaps makes differential analysis easier. On the other hand, the |
| 50 | * private iv1 probably makes the CRC-32 attack ineffective. This is a |
| 51 | * result of that there is no longer any known iv1 to use when |
| 52 | * choosing the X block. |
| 53 | */ |
| 54 | struct ssh1_3des_ctx |
| 55 | { |
| 56 | EVP_CIPHER_CTX k1, k2, k3; |
| 57 | }; |
| 58 | |
| 59 | const EVP_CIPHER * evp_ssh1_3des(void); |
| 60 | void ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int); |
| 61 | |
| 62 | static int |
| 63 | ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, |
| 64 | int enc) |
| 65 | { |
| 66 | struct ssh1_3des_ctx *c; |
| 67 | u_char *k1, *k2, *k3; |
| 68 | |
| 69 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
Damien Miller | 6c81fee | 2013-11-08 12:19:55 +1100 | [diff] [blame] | 70 | c = xcalloc(1, sizeof(*c)); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 71 | EVP_CIPHER_CTX_set_app_data(ctx, c); |
| 72 | } |
| 73 | if (key == NULL) |
| 74 | return (1); |
| 75 | if (enc == -1) |
| 76 | enc = ctx->encrypt; |
| 77 | k1 = k2 = k3 = (u_char *) key; |
| 78 | k2 += 8; |
| 79 | if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) { |
| 80 | if (enc) |
| 81 | k3 += 16; |
| 82 | else |
| 83 | k1 += 16; |
| 84 | } |
| 85 | EVP_CIPHER_CTX_init(&c->k1); |
| 86 | EVP_CIPHER_CTX_init(&c->k2); |
| 87 | EVP_CIPHER_CTX_init(&c->k3); |
| 88 | #ifdef SSH_OLD_EVP |
| 89 | EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc); |
| 90 | EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc); |
| 91 | EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc); |
| 92 | #else |
| 93 | if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 || |
| 94 | EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 || |
| 95 | EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) { |
| 96 | memset(c, 0, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 97 | free(c); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 98 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
| 99 | return (0); |
| 100 | } |
| 101 | #endif |
| 102 | return (1); |
| 103 | } |
| 104 | |
| 105 | static int |
Damien Miller | 9a3d0dc | 2010-10-07 22:06:42 +1100 | [diff] [blame] | 106 | ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, |
| 107 | LIBCRYPTO_EVP_INL_TYPE len) |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 108 | { |
| 109 | struct ssh1_3des_ctx *c; |
| 110 | |
| 111 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
| 112 | error("ssh1_3des_cbc: no context"); |
| 113 | return (0); |
| 114 | } |
| 115 | #ifdef SSH_OLD_EVP |
| 116 | EVP_Cipher(&c->k1, dest, (u_char *)src, len); |
| 117 | EVP_Cipher(&c->k2, dest, dest, len); |
| 118 | EVP_Cipher(&c->k3, dest, dest, len); |
| 119 | #else |
| 120 | if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 || |
| 121 | EVP_Cipher(&c->k2, dest, dest, len) == 0 || |
| 122 | EVP_Cipher(&c->k3, dest, dest, len) == 0) |
| 123 | return (0); |
| 124 | #endif |
| 125 | return (1); |
| 126 | } |
| 127 | |
| 128 | static int |
| 129 | ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx) |
| 130 | { |
| 131 | struct ssh1_3des_ctx *c; |
| 132 | |
| 133 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { |
Darren Tucker | a32e19c | 2003-12-31 11:36:00 +1100 | [diff] [blame] | 134 | EVP_CIPHER_CTX_cleanup(&c->k1); |
| 135 | EVP_CIPHER_CTX_cleanup(&c->k2); |
| 136 | EVP_CIPHER_CTX_cleanup(&c->k3); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 137 | memset(c, 0, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 138 | free(c); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 139 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
| 140 | } |
| 141 | return (1); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len) |
| 146 | { |
| 147 | struct ssh1_3des_ctx *c; |
| 148 | |
| 149 | if (len != 24) |
| 150 | fatal("%s: bad 3des iv length: %d", __func__, len); |
| 151 | if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL) |
| 152 | fatal("%s: no 3des context", __func__); |
| 153 | if (doset) { |
| 154 | debug3("%s: Installed 3DES IV", __func__); |
| 155 | memcpy(c->k1.iv, iv, 8); |
| 156 | memcpy(c->k2.iv, iv + 8, 8); |
| 157 | memcpy(c->k3.iv, iv + 16, 8); |
| 158 | } else { |
| 159 | debug3("%s: Copying 3DES IV", __func__); |
| 160 | memcpy(iv, c->k1.iv, 8); |
| 161 | memcpy(iv + 8, c->k2.iv, 8); |
| 162 | memcpy(iv + 16, c->k3.iv, 8); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | const EVP_CIPHER * |
| 167 | evp_ssh1_3des(void) |
| 168 | { |
| 169 | static EVP_CIPHER ssh1_3des; |
| 170 | |
| 171 | memset(&ssh1_3des, 0, sizeof(EVP_CIPHER)); |
| 172 | ssh1_3des.nid = NID_undef; |
| 173 | ssh1_3des.block_size = 8; |
| 174 | ssh1_3des.iv_len = 0; |
| 175 | ssh1_3des.key_len = 16; |
| 176 | ssh1_3des.init = ssh1_3des_init; |
| 177 | ssh1_3des.cleanup = ssh1_3des_cleanup; |
| 178 | ssh1_3des.do_cipher = ssh1_3des_cbc; |
| 179 | #ifndef SSH_OLD_EVP |
| 180 | ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH; |
| 181 | #endif |
| 182 | return (&ssh1_3des); |
| 183 | } |