cipher-3des1.c revision 124208
1124208Sdes/* 2124208Sdes * Copyright (c) 2003 Markus Friedl. All rights reserved. 3124208Sdes * 4124208Sdes * Redistribution and use in source and binary forms, with or without 5124208Sdes * modification, are permitted provided that the following conditions 6124208Sdes * are met: 7124208Sdes * 1. Redistributions of source code must retain the above copyright 8124208Sdes * notice, this list of conditions and the following disclaimer. 9124208Sdes * 2. Redistributions in binary form must reproduce the above copyright 10124208Sdes * notice, this list of conditions and the following disclaimer in the 11124208Sdes * documentation and/or other materials provided with the distribution. 12124208Sdes * 13124208Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14124208Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15124208Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16124208Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17124208Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 18124208Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 19124208Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 20124208Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21124208Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22124208Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23124208Sdes */ 24124208Sdes 25124208Sdes#include "includes.h" 26124208SdesRCSID("$OpenBSD: cipher-3des1.c,v 1.1 2003/05/15 03:08:29 markus Exp $"); 27124208Sdes 28124208Sdes#include <openssl/evp.h> 29124208Sdes#include "xmalloc.h" 30124208Sdes#include "log.h" 31124208Sdes 32124208Sdes#if OPENSSL_VERSION_NUMBER < 0x00906000L 33124208Sdes#define SSH_OLD_EVP 34124208Sdes#endif 35124208Sdes 36124208Sdes/* 37124208Sdes * This is used by SSH1: 38124208Sdes * 39124208Sdes * What kind of triple DES are these 2 routines? 40124208Sdes * 41124208Sdes * Why is there a redundant initialization vector? 42124208Sdes * 43124208Sdes * If only iv3 was used, then, this would till effect have been 44124208Sdes * outer-cbc. However, there is also a private iv1 == iv2 which 45124208Sdes * perhaps makes differential analysis easier. On the other hand, the 46124208Sdes * private iv1 probably makes the CRC-32 attack ineffective. This is a 47124208Sdes * result of that there is no longer any known iv1 to use when 48124208Sdes * choosing the X block. 49124208Sdes */ 50124208Sdesstruct ssh1_3des_ctx 51124208Sdes{ 52124208Sdes EVP_CIPHER_CTX k1, k2, k3; 53124208Sdes}; 54124208Sdes 55124208Sdesconst EVP_CIPHER * evp_ssh1_3des(void); 56124208Sdesvoid ssh1_3des_iv(EVP_CIPHER_CTX *, int, u_char *, int); 57124208Sdes 58124208Sdesstatic int 59124208Sdesssh1_3des_init(EVP_CIPHER_CTX *ctx, const u_char *key, const u_char *iv, 60124208Sdes int enc) 61124208Sdes{ 62124208Sdes struct ssh1_3des_ctx *c; 63124208Sdes u_char *k1, *k2, *k3; 64124208Sdes 65124208Sdes if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { 66124208Sdes c = xmalloc(sizeof(*c)); 67124208Sdes EVP_CIPHER_CTX_set_app_data(ctx, c); 68124208Sdes } 69124208Sdes if (key == NULL) 70124208Sdes return (1); 71124208Sdes if (enc == -1) 72124208Sdes enc = ctx->encrypt; 73124208Sdes k1 = k2 = k3 = (u_char *) key; 74124208Sdes k2 += 8; 75124208Sdes if (EVP_CIPHER_CTX_key_length(ctx) >= 16+8) { 76124208Sdes if (enc) 77124208Sdes k3 += 16; 78124208Sdes else 79124208Sdes k1 += 16; 80124208Sdes } 81124208Sdes EVP_CIPHER_CTX_init(&c->k1); 82124208Sdes EVP_CIPHER_CTX_init(&c->k2); 83124208Sdes EVP_CIPHER_CTX_init(&c->k3); 84124208Sdes#ifdef SSH_OLD_EVP 85124208Sdes EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc); 86124208Sdes EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc); 87124208Sdes EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc); 88124208Sdes#else 89124208Sdes if (EVP_CipherInit(&c->k1, EVP_des_cbc(), k1, NULL, enc) == 0 || 90124208Sdes EVP_CipherInit(&c->k2, EVP_des_cbc(), k2, NULL, !enc) == 0 || 91124208Sdes EVP_CipherInit(&c->k3, EVP_des_cbc(), k3, NULL, enc) == 0) { 92124208Sdes memset(c, 0, sizeof(*c)); 93124208Sdes xfree(c); 94124208Sdes EVP_CIPHER_CTX_set_app_data(ctx, NULL); 95124208Sdes return (0); 96124208Sdes } 97124208Sdes#endif 98124208Sdes return (1); 99124208Sdes} 100124208Sdes 101124208Sdesstatic int 102124208Sdesssh1_3des_cbc(EVP_CIPHER_CTX *ctx, u_char *dest, const u_char *src, u_int len) 103124208Sdes{ 104124208Sdes struct ssh1_3des_ctx *c; 105124208Sdes 106124208Sdes if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) == NULL) { 107124208Sdes error("ssh1_3des_cbc: no context"); 108124208Sdes return (0); 109124208Sdes } 110124208Sdes#ifdef SSH_OLD_EVP 111124208Sdes EVP_Cipher(&c->k1, dest, (u_char *)src, len); 112124208Sdes EVP_Cipher(&c->k2, dest, dest, len); 113124208Sdes EVP_Cipher(&c->k3, dest, dest, len); 114124208Sdes#else 115124208Sdes if (EVP_Cipher(&c->k1, dest, (u_char *)src, len) == 0 || 116124208Sdes EVP_Cipher(&c->k2, dest, dest, len) == 0 || 117124208Sdes EVP_Cipher(&c->k3, dest, dest, len) == 0) 118124208Sdes return (0); 119124208Sdes#endif 120124208Sdes return (1); 121124208Sdes} 122124208Sdes 123124208Sdesstatic int 124124208Sdesssh1_3des_cleanup(EVP_CIPHER_CTX *ctx) 125124208Sdes{ 126124208Sdes struct ssh1_3des_ctx *c; 127124208Sdes 128124208Sdes if ((c = EVP_CIPHER_CTX_get_app_data(ctx)) != NULL) { 129124208Sdes memset(c, 0, sizeof(*c)); 130124208Sdes xfree(c); 131124208Sdes EVP_CIPHER_CTX_set_app_data(ctx, NULL); 132124208Sdes } 133124208Sdes return (1); 134124208Sdes} 135124208Sdes 136124208Sdesvoid 137124208Sdesssh1_3des_iv(EVP_CIPHER_CTX *evp, int doset, u_char *iv, int len) 138124208Sdes{ 139124208Sdes struct ssh1_3des_ctx *c; 140124208Sdes 141124208Sdes if (len != 24) 142124208Sdes fatal("%s: bad 3des iv length: %d", __func__, len); 143124208Sdes if ((c = EVP_CIPHER_CTX_get_app_data(evp)) == NULL) 144124208Sdes fatal("%s: no 3des context", __func__); 145124208Sdes if (doset) { 146124208Sdes debug3("%s: Installed 3DES IV", __func__); 147124208Sdes memcpy(c->k1.iv, iv, 8); 148124208Sdes memcpy(c->k2.iv, iv + 8, 8); 149124208Sdes memcpy(c->k3.iv, iv + 16, 8); 150124208Sdes } else { 151124208Sdes debug3("%s: Copying 3DES IV", __func__); 152124208Sdes memcpy(iv, c->k1.iv, 8); 153124208Sdes memcpy(iv + 8, c->k2.iv, 8); 154124208Sdes memcpy(iv + 16, c->k3.iv, 8); 155124208Sdes } 156124208Sdes} 157124208Sdes 158124208Sdesconst EVP_CIPHER * 159124208Sdesevp_ssh1_3des(void) 160124208Sdes{ 161124208Sdes static EVP_CIPHER ssh1_3des; 162124208Sdes 163124208Sdes memset(&ssh1_3des, 0, sizeof(EVP_CIPHER)); 164124208Sdes ssh1_3des.nid = NID_undef; 165124208Sdes ssh1_3des.block_size = 8; 166124208Sdes ssh1_3des.iv_len = 0; 167124208Sdes ssh1_3des.key_len = 16; 168124208Sdes ssh1_3des.init = ssh1_3des_init; 169124208Sdes ssh1_3des.cleanup = ssh1_3des_cleanup; 170124208Sdes ssh1_3des.do_cipher = ssh1_3des_cbc; 171124208Sdes#ifndef SSH_OLD_EVP 172124208Sdes ssh1_3des.flags = EVP_CIPH_CBC_MODE | EVP_CIPH_VARIABLE_LENGTH; 173124208Sdes#endif 174124208Sdes return (&ssh1_3des); 175124208Sdes} 176