155714Skris/* ede_cbcm_enc.c */
255714Skris/* Written by Ben Laurie <ben@algroup.co.uk> for the OpenSSL
355714Skris * project 13 Feb 1999.
455714Skris */
555714Skris/* ====================================================================
655714Skris * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
755714Skris *
855714Skris * Redistribution and use in source and binary forms, with or without
955714Skris * modification, are permitted provided that the following conditions
1055714Skris * are met:
1155714Skris *
1255714Skris * 1. Redistributions of source code must retain the above copyright
1355714Skris *    notice, this list of conditions and the following disclaimer.
1455714Skris *
1555714Skris * 2. Redistributions in binary form must reproduce the above copyright
1655714Skris *    notice, this list of conditions and the following disclaimer in
1755714Skris *    the documentation and/or other materials provided with the
1855714Skris *    distribution.
1955714Skris *
2055714Skris * 3. All advertising materials mentioning features or use of this
2155714Skris *    software must display the following acknowledgment:
2255714Skris *    "This product includes software developed by the OpenSSL Project
2355714Skris *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
2455714Skris *
2555714Skris * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
2655714Skris *    endorse or promote products derived from this software without
2755714Skris *    prior written permission. For written permission, please contact
2855714Skris *    licensing@OpenSSL.org.
2955714Skris *
3055714Skris * 5. Products derived from this software may not be called "OpenSSL"
3155714Skris *    nor may "OpenSSL" appear in their names without prior written
3255714Skris *    permission of the OpenSSL Project.
3355714Skris *
3455714Skris * 6. Redistributions of any form whatsoever must retain the following
3555714Skris *    acknowledgment:
3655714Skris *    "This product includes software developed by the OpenSSL Project
3755714Skris *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
3855714Skris *
3955714Skris * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
4055714Skris * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4155714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
4255714Skris * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
4355714Skris * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
4455714Skris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
4555714Skris * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
4655714Skris * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4755714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
4855714Skris * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
4955714Skris * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
5055714Skris * OF THE POSSIBILITY OF SUCH DAMAGE.
5155714Skris * ====================================================================
5255714Skris *
5355714Skris * This product includes cryptographic software written by Eric Young
5455714Skris * (eay@cryptsoft.com).  This product includes software written by Tim
5555714Skris * Hudson (tjh@cryptsoft.com).
5655714Skris *
5755714Skris */
5855714Skris
5955714Skris/*
6055714Skris
6155714SkrisThis is an implementation of Triple DES Cipher Block Chaining with Output
6255714SkrisFeedback Masking, by Coppersmith, Johnson and Matyas, (IBM and Certicom).
6355714Skris
6455714SkrisNote that there is a known attack on this by Biham and Knudsen but it takes
6555714Skrisa lot of work:
6655714Skris
6755714Skrishttp://www.cs.technion.ac.il/users/wwwb/cgi-bin/tr-get.cgi/1998/CS/CS0928.ps.gz
6855714Skris
6955714Skris*/
7055714Skris
71160814Ssimon#include <openssl/opensslconf.h> /* To see if OPENSSL_NO_DESCBCM is defined */
72160814Ssimon
73109998Smarkm#ifndef OPENSSL_NO_DESCBCM
7455714Skris#include "des_locl.h"
7555714Skris
76109998Smarkmvoid DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out,
77109998Smarkm	     long length, DES_key_schedule *ks1, DES_key_schedule *ks2,
78109998Smarkm	     DES_key_schedule *ks3, DES_cblock *ivec1, DES_cblock *ivec2,
7955714Skris	     int enc)
8055714Skris    {
8155714Skris    register DES_LONG tin0,tin1;
8255714Skris    register DES_LONG tout0,tout1,xor0,xor1,m0,m1;
8355714Skris    register long l=length;
8455714Skris    DES_LONG tin[2];
8555714Skris    unsigned char *iv1,*iv2;
8655714Skris
8755714Skris    iv1 = &(*ivec1)[0];
8855714Skris    iv2 = &(*ivec2)[0];
8955714Skris
9055714Skris    if (enc)
9155714Skris	{
9255714Skris	c2l(iv1,m0);
9355714Skris	c2l(iv1,m1);
9455714Skris	c2l(iv2,tout0);
9555714Skris	c2l(iv2,tout1);
9655714Skris	for (l-=8; l>=-7; l-=8)
9755714Skris	    {
9855714Skris	    tin[0]=m0;
9955714Skris	    tin[1]=m1;
100109998Smarkm	    DES_encrypt1(tin,ks3,1);
10155714Skris	    m0=tin[0];
10255714Skris	    m1=tin[1];
10355714Skris
10455714Skris	    if(l < 0)
10555714Skris		{
10655714Skris		c2ln(in,tin0,tin1,l+8);
10755714Skris		}
10855714Skris	    else
10955714Skris		{
11055714Skris		c2l(in,tin0);
11155714Skris		c2l(in,tin1);
11255714Skris		}
11355714Skris	    tin0^=tout0;
11455714Skris	    tin1^=tout1;
11555714Skris
11655714Skris	    tin[0]=tin0;
11755714Skris	    tin[1]=tin1;
118109998Smarkm	    DES_encrypt1(tin,ks1,1);
11955714Skris	    tin[0]^=m0;
12055714Skris	    tin[1]^=m1;
121109998Smarkm	    DES_encrypt1(tin,ks2,0);
12255714Skris	    tin[0]^=m0;
12355714Skris	    tin[1]^=m1;
124109998Smarkm	    DES_encrypt1(tin,ks1,1);
12555714Skris	    tout0=tin[0];
12655714Skris	    tout1=tin[1];
12755714Skris
12855714Skris	    l2c(tout0,out);
12955714Skris	    l2c(tout1,out);
13055714Skris	    }
13155714Skris	iv1=&(*ivec1)[0];
13255714Skris	l2c(m0,iv1);
13355714Skris	l2c(m1,iv1);
13455714Skris
13555714Skris	iv2=&(*ivec2)[0];
13655714Skris	l2c(tout0,iv2);
13755714Skris	l2c(tout1,iv2);
13855714Skris	}
13955714Skris    else
14055714Skris	{
14155714Skris	register DES_LONG t0,t1;
14255714Skris
14355714Skris	c2l(iv1,m0);
14455714Skris	c2l(iv1,m1);
14555714Skris	c2l(iv2,xor0);
14655714Skris	c2l(iv2,xor1);
14755714Skris	for (l-=8; l>=-7; l-=8)
14855714Skris	    {
14955714Skris	    tin[0]=m0;
15055714Skris	    tin[1]=m1;
151109998Smarkm	    DES_encrypt1(tin,ks3,1);
15255714Skris	    m0=tin[0];
15355714Skris	    m1=tin[1];
15455714Skris
15555714Skris	    c2l(in,tin0);
15655714Skris	    c2l(in,tin1);
15755714Skris
15855714Skris	    t0=tin0;
15955714Skris	    t1=tin1;
16055714Skris
16155714Skris	    tin[0]=tin0;
16255714Skris	    tin[1]=tin1;
163109998Smarkm	    DES_encrypt1(tin,ks1,0);
16455714Skris	    tin[0]^=m0;
16555714Skris	    tin[1]^=m1;
166109998Smarkm	    DES_encrypt1(tin,ks2,1);
16755714Skris	    tin[0]^=m0;
16855714Skris	    tin[1]^=m1;
169109998Smarkm	    DES_encrypt1(tin,ks1,0);
17055714Skris	    tout0=tin[0];
17155714Skris	    tout1=tin[1];
17255714Skris
17355714Skris	    tout0^=xor0;
17455714Skris	    tout1^=xor1;
17555714Skris	    if(l < 0)
17655714Skris		{
17755714Skris		l2cn(tout0,tout1,out,l+8);
17855714Skris		}
17955714Skris	    else
18055714Skris		{
18155714Skris		l2c(tout0,out);
18255714Skris		l2c(tout1,out);
18355714Skris		}
18455714Skris	    xor0=t0;
18555714Skris	    xor1=t1;
18655714Skris	    }
18755714Skris
18855714Skris	iv1=&(*ivec1)[0];
18955714Skris	l2c(m0,iv1);
19055714Skris	l2c(m1,iv1);
19155714Skris
19255714Skris	iv2=&(*ivec2)[0];
19355714Skris	l2c(xor0,iv2);
19455714Skris	l2c(xor1,iv2);
19555714Skris	}
19655714Skris    tin0=tin1=tout0=tout1=xor0=xor1=0;
19755714Skris    tin[0]=tin[1]=0;
19855714Skris    }
19955714Skris#endif
200