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