155714Skris/* ede_cbcm_enc.c */ 2296465Sdelphij/* 3296465Sdelphij * Written by Ben Laurie <ben@algroup.co.uk> for the OpenSSL project 13 Feb 4296465Sdelphij * 1999. 555714Skris */ 655714Skris/* ==================================================================== 755714Skris * Copyright (c) 1999 The OpenSSL Project. All rights reserved. 855714Skris * 955714Skris * Redistribution and use in source and binary forms, with or without 1055714Skris * modification, are permitted provided that the following conditions 1155714Skris * are met: 1255714Skris * 1355714Skris * 1. Redistributions of source code must retain the above copyright 14296465Sdelphij * notice, this list of conditions and the following disclaimer. 1555714Skris * 1655714Skris * 2. Redistributions in binary form must reproduce the above copyright 1755714Skris * notice, this list of conditions and the following disclaimer in 1855714Skris * the documentation and/or other materials provided with the 1955714Skris * distribution. 2055714Skris * 2155714Skris * 3. All advertising materials mentioning features or use of this 2255714Skris * software must display the following acknowledgment: 2355714Skris * "This product includes software developed by the OpenSSL Project 2455714Skris * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 2555714Skris * 2655714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 2755714Skris * endorse or promote products derived from this software without 2855714Skris * prior written permission. For written permission, please contact 2955714Skris * licensing@OpenSSL.org. 3055714Skris * 3155714Skris * 5. Products derived from this software may not be called "OpenSSL" 3255714Skris * nor may "OpenSSL" appear in their names without prior written 3355714Skris * permission of the OpenSSL Project. 3455714Skris * 3555714Skris * 6. Redistributions of any form whatsoever must retain the following 3655714Skris * acknowledgment: 3755714Skris * "This product includes software developed by the OpenSSL Project 3855714Skris * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 3955714Skris * 4055714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 4155714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4255714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 4355714Skris * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 4455714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 4555714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 4655714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 4755714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 4955714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 5055714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 5155714Skris * OF THE POSSIBILITY OF SUCH DAMAGE. 5255714Skris * ==================================================================== 5355714Skris * 5455714Skris * This product includes cryptographic software written by Eric Young 5555714Skris * (eay@cryptsoft.com). This product includes software written by Tim 5655714Skris * Hudson (tjh@cryptsoft.com). 5755714Skris * 5855714Skris */ 5955714Skris 6055714Skris/* 61296465Sdelphij * 62296465Sdelphij * This is an implementation of Triple DES Cipher Block Chaining with Output 63296465Sdelphij * Feedback Masking, by Coppersmith, Johnson and Matyas, (IBM and Certicom). 64296465Sdelphij * 65296465Sdelphij * Note that there is a known attack on this by Biham and Knudsen but it 66296465Sdelphij * takes a lot of work: 67296465Sdelphij * 68296465Sdelphij * http://www.cs.technion.ac.il/users/wwwb/cgi-bin/tr-get.cgi/1998/CS/CS0928.ps.gz 69296465Sdelphij * 70296465Sdelphij */ 7155714Skris 72160814Ssimon#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_DESCBCM is defined */ 73160814Ssimon 74109998Smarkm#ifndef OPENSSL_NO_DESCBCM 75296465Sdelphij# include "des_locl.h" 7655714Skris 77109998Smarkmvoid DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out, 78296465Sdelphij long length, DES_key_schedule *ks1, 79296465Sdelphij DES_key_schedule *ks2, DES_key_schedule *ks3, 80296465Sdelphij DES_cblock *ivec1, DES_cblock *ivec2, int enc) 81296465Sdelphij{ 82296465Sdelphij register DES_LONG tin0, tin1; 83296465Sdelphij register DES_LONG tout0, tout1, xor0, xor1, m0, m1; 84296465Sdelphij register long l = length; 8555714Skris DES_LONG tin[2]; 86296465Sdelphij unsigned char *iv1, *iv2; 8755714Skris 8855714Skris iv1 = &(*ivec1)[0]; 8955714Skris iv2 = &(*ivec2)[0]; 9055714Skris 91296465Sdelphij if (enc) { 92296465Sdelphij c2l(iv1, m0); 93296465Sdelphij c2l(iv1, m1); 94296465Sdelphij c2l(iv2, tout0); 95296465Sdelphij c2l(iv2, tout1); 96296465Sdelphij for (l -= 8; l >= -7; l -= 8) { 97296465Sdelphij tin[0] = m0; 98296465Sdelphij tin[1] = m1; 99296465Sdelphij DES_encrypt1(tin, ks3, 1); 100296465Sdelphij m0 = tin[0]; 101296465Sdelphij m1 = tin[1]; 10255714Skris 103296465Sdelphij if (l < 0) { 104296465Sdelphij c2ln(in, tin0, tin1, l + 8); 105296465Sdelphij } else { 106296465Sdelphij c2l(in, tin0); 107296465Sdelphij c2l(in, tin1); 108296465Sdelphij } 109296465Sdelphij tin0 ^= tout0; 110296465Sdelphij tin1 ^= tout1; 11155714Skris 112296465Sdelphij tin[0] = tin0; 113296465Sdelphij tin[1] = tin1; 114296465Sdelphij DES_encrypt1(tin, ks1, 1); 115296465Sdelphij tin[0] ^= m0; 116296465Sdelphij tin[1] ^= m1; 117296465Sdelphij DES_encrypt1(tin, ks2, 0); 118296465Sdelphij tin[0] ^= m0; 119296465Sdelphij tin[1] ^= m1; 120296465Sdelphij DES_encrypt1(tin, ks1, 1); 121296465Sdelphij tout0 = tin[0]; 122296465Sdelphij tout1 = tin[1]; 12355714Skris 124296465Sdelphij l2c(tout0, out); 125296465Sdelphij l2c(tout1, out); 126296465Sdelphij } 127296465Sdelphij iv1 = &(*ivec1)[0]; 128296465Sdelphij l2c(m0, iv1); 129296465Sdelphij l2c(m1, iv1); 13055714Skris 131296465Sdelphij iv2 = &(*ivec2)[0]; 132296465Sdelphij l2c(tout0, iv2); 133296465Sdelphij l2c(tout1, iv2); 134296465Sdelphij } else { 135296465Sdelphij register DES_LONG t0, t1; 13655714Skris 137296465Sdelphij c2l(iv1, m0); 138296465Sdelphij c2l(iv1, m1); 139296465Sdelphij c2l(iv2, xor0); 140296465Sdelphij c2l(iv2, xor1); 141296465Sdelphij for (l -= 8; l >= -7; l -= 8) { 142296465Sdelphij tin[0] = m0; 143296465Sdelphij tin[1] = m1; 144296465Sdelphij DES_encrypt1(tin, ks3, 1); 145296465Sdelphij m0 = tin[0]; 146296465Sdelphij m1 = tin[1]; 14755714Skris 148296465Sdelphij c2l(in, tin0); 149296465Sdelphij c2l(in, tin1); 15055714Skris 151296465Sdelphij t0 = tin0; 152296465Sdelphij t1 = tin1; 15355714Skris 154296465Sdelphij tin[0] = tin0; 155296465Sdelphij tin[1] = tin1; 156296465Sdelphij DES_encrypt1(tin, ks1, 0); 157296465Sdelphij tin[0] ^= m0; 158296465Sdelphij tin[1] ^= m1; 159296465Sdelphij DES_encrypt1(tin, ks2, 1); 160296465Sdelphij tin[0] ^= m0; 161296465Sdelphij tin[1] ^= m1; 162296465Sdelphij DES_encrypt1(tin, ks1, 0); 163296465Sdelphij tout0 = tin[0]; 164296465Sdelphij tout1 = tin[1]; 16555714Skris 166296465Sdelphij tout0 ^= xor0; 167296465Sdelphij tout1 ^= xor1; 168296465Sdelphij if (l < 0) { 169296465Sdelphij l2cn(tout0, tout1, out, l + 8); 170296465Sdelphij } else { 171296465Sdelphij l2c(tout0, out); 172296465Sdelphij l2c(tout1, out); 173296465Sdelphij } 174296465Sdelphij xor0 = t0; 175296465Sdelphij xor1 = t1; 176296465Sdelphij } 17755714Skris 178296465Sdelphij iv1 = &(*ivec1)[0]; 179296465Sdelphij l2c(m0, iv1); 180296465Sdelphij l2c(m1, iv1); 18155714Skris 182296465Sdelphij iv2 = &(*ivec2)[0]; 183296465Sdelphij l2c(xor0, iv2); 184296465Sdelphij l2c(xor1, iv2); 18555714Skris } 186296465Sdelphij tin0 = tin1 = tout0 = tout1 = xor0 = xor1 = 0; 187296465Sdelphij tin[0] = tin[1] = 0; 188296465Sdelphij} 18955714Skris#endif 190