Damien Miller | 3ff92ba | 2014-07-02 15:33:09 +1000 | [diff] [blame] | 1 | /* $OpenBSD: cipher-3des1.c,v 1.11 2014/07/02 04:59:06 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 | |
| 32 | #include <string.h> |
| 33 | |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 34 | #include "xmalloc.h" |
| 35 | #include "log.h" |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 36 | #include "ssherr.h" |
Damien Miller | 5c3a558 | 2003-09-23 22:12:38 +1000 | [diff] [blame] | 37 | |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 38 | /* |
| 39 | * This is used by SSH1: |
| 40 | * |
| 41 | * What kind of triple DES are these 2 routines? |
| 42 | * |
| 43 | * Why is there a redundant initialization vector? |
| 44 | * |
| 45 | * If only iv3 was used, then, this would till effect have been |
| 46 | * outer-cbc. However, there is also a private iv1 == iv2 which |
| 47 | * perhaps makes differential analysis easier. On the other hand, the |
| 48 | * private iv1 probably makes the CRC-32 attack ineffective. This is a |
| 49 | * result of that there is no longer any known iv1 to use when |
| 50 | * choosing the X block. |
| 51 | */ |
| 52 | struct ssh1_3des_ctx |
| 53 | { |
| 54 | EVP_CIPHER_CTX k1, k2, k3; |
| 55 | }; |
| 56 | |
| 57 | const EVP_CIPHER * evp_ssh1_3des(void); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 58 | int ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 59 | |
| 60 | static int |
| 61 | ssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, |
| 62 | int enc) |
| 63 | { |
| 64 | struct ssh1_3des_ctx *c; |
| 65 | u_char *k1, *k2, *k3; |
| 66 | |
| 67 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 68 | if ((c = calloc(1, sizeof(*c))) == NULL) |
| 69 | return 0; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 70 | EVP_CIPHER_CTX_set_app_data(ctx, c); |
| 71 | } |
| 72 | if (key == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 73 | return 1; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 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; |
| 83 | } |
| 84 | EVP_CIPHER_CTX_init(&c->k1); |
| 85 | EVP_CIPHER_CTX_init(&c->k2); |
| 86 | EVP_CIPHER_CTX_init(&c->k3); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 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) { |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 90 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 91 | free(c); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 92 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 93 | return 0; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 94 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 95 | return 1; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static int |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 99 | ssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, size_t len) |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 100 | { |
| 101 | struct ssh1_3des_ctx *c; |
| 102 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 103 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) |
| 104 | return 0; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 105 | if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 || |
| 106 | EVP_Cipher(&c->k2, dest, dest, len) == 0 || |
| 107 | EVP_Cipher(&c->k3, dest, dest, len) == 0) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 108 | return 0; |
| 109 | return 1; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static int |
| 113 | ssh1_3des_cleanup(EVP_CIPHER_CTX *ctx) |
| 114 | { |
| 115 | struct ssh1_3des_ctx *c; |
| 116 | |
| 117 | if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { |
Darren Tucker | a32e19c | 2003-12-31 11:36:00 +1100 | [diff] [blame] | 118 | EVP_CIPHER_CTX_cleanup(&c->k1); |
| 119 | EVP_CIPHER_CTX_cleanup(&c->k2); |
| 120 | EVP_CIPHER_CTX_cleanup(&c->k3); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 121 | explicit_bzero(c, sizeof(*c)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 122 | free(c); |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 123 | EVP_CIPHER_CTX_set_app_data(ctx, NULL); |
| 124 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 125 | return 1; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 126 | } |
| 127 | |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 128 | int |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 129 | ssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len) |
| 130 | { |
| 131 | struct ssh1_3des_ctx *c; |
| 132 | |
| 133 | if (len != 24) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 134 | return SSH_ERR_INVALID_ARGUMENT; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 135 | if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL) |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 136 | return SSH_ERR_INTERNAL_ERROR; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 137 | if (doset) { |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 138 | memcpy(c->k1.iv, iv, 8); |
| 139 | memcpy(c->k2.iv, iv + 8, 8); |
| 140 | memcpy(c->k3.iv, iv + 16, 8); |
| 141 | } else { |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 142 | memcpy(iv, c->k1.iv, 8); |
| 143 | memcpy(iv + 8, c->k2.iv, 8); |
| 144 | memcpy(iv + 16, c->k3.iv, 8); |
| 145 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 146 | return 0; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | const EVP_CIPHER * |
| 150 | evp_ssh1_3des(void) |
| 151 | { |
| 152 | static EVP_CIPHER ssh1_3des; |
| 153 | |
| 154 | memset(&ssh1_3des, 0, sizeof(EVP_CIPHER)); |
| 155 | ssh1_3des.nid = NID_undef; |
| 156 | ssh1_3des.block_size = 8; |
| 157 | ssh1_3des.iv_len = 0; |
| 158 | ssh1_3des.key_len = 16; |
| 159 | ssh1_3des.init = ssh1_3des_init; |
| 160 | ssh1_3des.cleanup = ssh1_3des_cleanup; |
| 161 | ssh1_3des.do_cipher = ssh1_3des_cbc; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 162 | ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH; |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 163 | return &ssh1_3des; |
Damien Miller | 3a3261f | 2003-05-15 13:37:19 +1000 | [diff] [blame] | 164 | } |