cbc3_enc.c revision 59191
1341825Sdim/* crypto/des/cbc3_enc.c */
2218887Sdim/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3353358Sdim * All rights reserved.
4353358Sdim *
5353358Sdim * This package is an SSL implementation written
6218887Sdim * by Eric Young (eay@cryptsoft.com).
7218887Sdim * The implementation was written so as to conform with Netscapes SSL.
8218887Sdim *
9218887Sdim * This library is free for commercial and non-commercial use as long as
10218887Sdim * the following conditions are aheared to.  The following conditions
11218887Sdim * apply to all code found in this distribution, be it the RC4, RSA,
12218887Sdim * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13218887Sdim * included with this distribution is covered by the same copyright terms
14280031Sdim * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15280031Sdim *
16218887Sdim * Copyright remains Eric Young's, and as such any Copyright notices in
17218887Sdim * the code are not to be removed.
18341825Sdim * If this package is used in a product, Eric Young should be given attribution
19327952Sdim * as the author of the parts of the library used.
20226633Sdim * This can be in the form of a textual message at program startup or
21309124Sdim * in documentation (online or textual) provided with the package.
22226633Sdim *
23309124Sdim * Redistribution and use in source and binary forms, with or without
24249423Sdim * modification, are permitted provided that the following conditions
25249423Sdim * are met:
26249423Sdim * 1. Redistributions of source code must retain the copyright
27276479Sdim *    notice, this list of conditions and the following disclaimer.
28341825Sdim * 2. Redistributions in binary form must reproduce the above copyright
29218887Sdim *    notice, this list of conditions and the following disclaimer in the
30218887Sdim *    documentation and/or other materials provided with the distribution.
31218887Sdim * 3. All advertising materials mentioning features or use of this software
32341825Sdim *    must display the following acknowledgement:
33341825Sdim *    "This product includes cryptographic software written by
34341825Sdim *     Eric Young (eay@cryptsoft.com)"
35218887Sdim *    The word 'cryptographic' can be left out if the rouines from the library
36218887Sdim *    being used are not cryptographic related :-).
37341825Sdim * 4. If you include any Windows specific code (or a derivative thereof) from
38341825Sdim *    the apps directory (application code) you must include an acknowledgement:
39341825Sdim *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40341825Sdim *
41218887Sdim * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42226633Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43218887Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44218887Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45226633Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46321369Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47321369Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48321369Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49321369Sdim * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50218887Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51226633Sdim * SUCH DAMAGE.
52218887Sdim *
53226633Sdim * The licence and distribution terms for any publically available version or
54296417Sdim * derivative of this code cannot be changed.  i.e. this code cannot simply be
55218887Sdim * copied and put under another distribution licence
56218887Sdim * [including the GNU Public Licence.]
57218887Sdim */
58276479Sdim
59218887Sdim#include "des_locl.h"
60218887Sdim
61218887Sdim/* HAS BUGS! DON'T USE - this is only present for use in des.c */
62276479Sdimvoid des_3cbc_encrypt(des_cblock *input, des_cblock *output, long length,
63309124Sdim	     des_key_schedule ks1, des_key_schedule ks2, des_cblock *iv1,
64218887Sdim	     des_cblock *iv2, int enc)
65276479Sdim	{
66218887Sdim	int off=((int)length-1)/8;
67218887Sdim	long l8=((length+7)/8)*8;
68341825Sdim	des_cblock niv1,niv2;
69296417Sdim
70218887Sdim	if (enc == DES_ENCRYPT)
71218887Sdim		{
72218887Sdim		des_cbc_encrypt((unsigned char*)input,
73234353Sdim				(unsigned char*)output,length,ks1,iv1,enc);
74234353Sdim		if (length >= sizeof(des_cblock))
75218887Sdim			memcpy(niv1,output[off],sizeof(des_cblock));
76226633Sdim		des_cbc_encrypt((unsigned char*)output,
77218887Sdim				(unsigned char*)output,l8,ks2,iv1,!enc);
78218887Sdim		des_cbc_encrypt((unsigned char*)output,
79234353Sdim				(unsigned char*)output,l8,ks1,iv2,enc);
80226633Sdim		if (length >= sizeof(des_cblock))
81218887Sdim			memcpy(niv2,output[off],sizeof(des_cblock));
82218887Sdim		}
83234353Sdim	else
84296417Sdim		{
85296417Sdim		if (length >= sizeof(des_cblock))
86321369Sdim			memcpy(niv2,input[off],sizeof(des_cblock));
87321369Sdim		des_cbc_encrypt((unsigned char*)input,
88321369Sdim				(unsigned char*)output,l8,ks1,iv2,enc);
89321369Sdim		des_cbc_encrypt((unsigned char*)output,
90321369Sdim				(unsigned char*)output,l8,ks2,iv1,!enc);
91321369Sdim		if (length >= sizeof(des_cblock))
92321369Sdim			memcpy(niv1,output[off],sizeof(des_cblock));
93321369Sdim		des_cbc_encrypt((unsigned char*)output,
94321369Sdim				(unsigned char*)output,length,ks1,iv1,enc);
95218887Sdim		}
96226633Sdim	memcpy(*iv1,niv1,sizeof(des_cblock));
97218887Sdim	memcpy(*iv2,niv2,sizeof(des_cblock));
98226633Sdim	}
99218887Sdim
100276479Sdim