xref: /onnv-gate/usr/src/common/openssl/crypto/des/ede_cbcm_enc.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* ede_cbcm_enc.c */
20Sstevel@tonic-gate /* Written by Ben Laurie <ben@algroup.co.uk> for the OpenSSL
30Sstevel@tonic-gate  * project 13 Feb 1999.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate 
610Sstevel@tonic-gate This is an implementation of Triple DES Cipher Block Chaining with Output
620Sstevel@tonic-gate Feedback Masking, by Coppersmith, Johnson and Matyas, (IBM and Certicom).
630Sstevel@tonic-gate 
640Sstevel@tonic-gate Note that there is a known attack on this by Biham and Knudsen but it takes
650Sstevel@tonic-gate a lot of work:
660Sstevel@tonic-gate 
670Sstevel@tonic-gate http://www.cs.technion.ac.il/users/wwwb/cgi-bin/tr-get.cgi/1998/CS/CS0928.ps.gz
680Sstevel@tonic-gate 
690Sstevel@tonic-gate */
700Sstevel@tonic-gate 
71*2139Sjp161948 #include <openssl/opensslconf.h> /* To see if OPENSSL_NO_DESCBCM is defined */
72*2139Sjp161948 
730Sstevel@tonic-gate #ifndef OPENSSL_NO_DESCBCM
740Sstevel@tonic-gate #include "des_locl.h"
750Sstevel@tonic-gate 
DES_ede3_cbcm_encrypt(const unsigned char * in,unsigned char * out,long length,DES_key_schedule * ks1,DES_key_schedule * ks2,DES_key_schedule * ks3,DES_cblock * ivec1,DES_cblock * ivec2,int enc)760Sstevel@tonic-gate void DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out,
770Sstevel@tonic-gate 	     long length, DES_key_schedule *ks1, DES_key_schedule *ks2,
780Sstevel@tonic-gate 	     DES_key_schedule *ks3, DES_cblock *ivec1, DES_cblock *ivec2,
790Sstevel@tonic-gate 	     int enc)
800Sstevel@tonic-gate     {
810Sstevel@tonic-gate     register DES_LONG tin0,tin1;
820Sstevel@tonic-gate     register DES_LONG tout0,tout1,xor0,xor1,m0,m1;
830Sstevel@tonic-gate     register long l=length;
840Sstevel@tonic-gate     DES_LONG tin[2];
850Sstevel@tonic-gate     unsigned char *iv1,*iv2;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate     iv1 = &(*ivec1)[0];
880Sstevel@tonic-gate     iv2 = &(*ivec2)[0];
890Sstevel@tonic-gate 
900Sstevel@tonic-gate     if (enc)
910Sstevel@tonic-gate 	{
920Sstevel@tonic-gate 	c2l(iv1,m0);
930Sstevel@tonic-gate 	c2l(iv1,m1);
940Sstevel@tonic-gate 	c2l(iv2,tout0);
950Sstevel@tonic-gate 	c2l(iv2,tout1);
960Sstevel@tonic-gate 	for (l-=8; l>=-7; l-=8)
970Sstevel@tonic-gate 	    {
980Sstevel@tonic-gate 	    tin[0]=m0;
990Sstevel@tonic-gate 	    tin[1]=m1;
1000Sstevel@tonic-gate 	    DES_encrypt1(tin,ks3,1);
1010Sstevel@tonic-gate 	    m0=tin[0];
1020Sstevel@tonic-gate 	    m1=tin[1];
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	    if(l < 0)
1050Sstevel@tonic-gate 		{
1060Sstevel@tonic-gate 		c2ln(in,tin0,tin1,l+8);
1070Sstevel@tonic-gate 		}
1080Sstevel@tonic-gate 	    else
1090Sstevel@tonic-gate 		{
1100Sstevel@tonic-gate 		c2l(in,tin0);
1110Sstevel@tonic-gate 		c2l(in,tin1);
1120Sstevel@tonic-gate 		}
1130Sstevel@tonic-gate 	    tin0^=tout0;
1140Sstevel@tonic-gate 	    tin1^=tout1;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	    tin[0]=tin0;
1170Sstevel@tonic-gate 	    tin[1]=tin1;
1180Sstevel@tonic-gate 	    DES_encrypt1(tin,ks1,1);
1190Sstevel@tonic-gate 	    tin[0]^=m0;
1200Sstevel@tonic-gate 	    tin[1]^=m1;
1210Sstevel@tonic-gate 	    DES_encrypt1(tin,ks2,0);
1220Sstevel@tonic-gate 	    tin[0]^=m0;
1230Sstevel@tonic-gate 	    tin[1]^=m1;
1240Sstevel@tonic-gate 	    DES_encrypt1(tin,ks1,1);
1250Sstevel@tonic-gate 	    tout0=tin[0];
1260Sstevel@tonic-gate 	    tout1=tin[1];
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	    l2c(tout0,out);
1290Sstevel@tonic-gate 	    l2c(tout1,out);
1300Sstevel@tonic-gate 	    }
1310Sstevel@tonic-gate 	iv1=&(*ivec1)[0];
1320Sstevel@tonic-gate 	l2c(m0,iv1);
1330Sstevel@tonic-gate 	l2c(m1,iv1);
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	iv2=&(*ivec2)[0];
1360Sstevel@tonic-gate 	l2c(tout0,iv2);
1370Sstevel@tonic-gate 	l2c(tout1,iv2);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate     else
1400Sstevel@tonic-gate 	{
1410Sstevel@tonic-gate 	register DES_LONG t0,t1;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	c2l(iv1,m0);
1440Sstevel@tonic-gate 	c2l(iv1,m1);
1450Sstevel@tonic-gate 	c2l(iv2,xor0);
1460Sstevel@tonic-gate 	c2l(iv2,xor1);
1470Sstevel@tonic-gate 	for (l-=8; l>=-7; l-=8)
1480Sstevel@tonic-gate 	    {
1490Sstevel@tonic-gate 	    tin[0]=m0;
1500Sstevel@tonic-gate 	    tin[1]=m1;
1510Sstevel@tonic-gate 	    DES_encrypt1(tin,ks3,1);
1520Sstevel@tonic-gate 	    m0=tin[0];
1530Sstevel@tonic-gate 	    m1=tin[1];
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	    c2l(in,tin0);
1560Sstevel@tonic-gate 	    c2l(in,tin1);
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	    t0=tin0;
1590Sstevel@tonic-gate 	    t1=tin1;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	    tin[0]=tin0;
1620Sstevel@tonic-gate 	    tin[1]=tin1;
1630Sstevel@tonic-gate 	    DES_encrypt1(tin,ks1,0);
1640Sstevel@tonic-gate 	    tin[0]^=m0;
1650Sstevel@tonic-gate 	    tin[1]^=m1;
1660Sstevel@tonic-gate 	    DES_encrypt1(tin,ks2,1);
1670Sstevel@tonic-gate 	    tin[0]^=m0;
1680Sstevel@tonic-gate 	    tin[1]^=m1;
1690Sstevel@tonic-gate 	    DES_encrypt1(tin,ks1,0);
1700Sstevel@tonic-gate 	    tout0=tin[0];
1710Sstevel@tonic-gate 	    tout1=tin[1];
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	    tout0^=xor0;
1740Sstevel@tonic-gate 	    tout1^=xor1;
1750Sstevel@tonic-gate 	    if(l < 0)
1760Sstevel@tonic-gate 		{
1770Sstevel@tonic-gate 		l2cn(tout0,tout1,out,l+8);
1780Sstevel@tonic-gate 		}
1790Sstevel@tonic-gate 	    else
1800Sstevel@tonic-gate 		{
1810Sstevel@tonic-gate 		l2c(tout0,out);
1820Sstevel@tonic-gate 		l2c(tout1,out);
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 	    xor0=t0;
1850Sstevel@tonic-gate 	    xor1=t1;
1860Sstevel@tonic-gate 	    }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	iv1=&(*ivec1)[0];
1890Sstevel@tonic-gate 	l2c(m0,iv1);
1900Sstevel@tonic-gate 	l2c(m1,iv1);
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 	iv2=&(*ivec2)[0];
1930Sstevel@tonic-gate 	l2c(xor0,iv2);
1940Sstevel@tonic-gate 	l2c(xor1,iv2);
1950Sstevel@tonic-gate 	}
1960Sstevel@tonic-gate     tin0=tin1=tout0=tout1=xor0=xor1=0;
1970Sstevel@tonic-gate     tin[0]=tin[1]=0;
1980Sstevel@tonic-gate     }
1990Sstevel@tonic-gate #endif
200