1*c9496f6bSchristos /* ede_cbcm_enc.c */
2*c9496f6bSchristos /* Written by Ben Laurie <ben@algroup.co.uk> for the OpenSSL
3*c9496f6bSchristos * project 13 Feb 1999.
4*c9496f6bSchristos */
5*c9496f6bSchristos /* ====================================================================
6*c9496f6bSchristos * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
7*c9496f6bSchristos *
8*c9496f6bSchristos * Redistribution and use in source and binary forms, with or without
9*c9496f6bSchristos * modification, are permitted provided that the following conditions
10*c9496f6bSchristos * are met:
11*c9496f6bSchristos *
12*c9496f6bSchristos * 1. Redistributions of source code must retain the above copyright
13*c9496f6bSchristos * notice, this list of conditions and the following disclaimer.
14*c9496f6bSchristos *
15*c9496f6bSchristos * 2. Redistributions in binary form must reproduce the above copyright
16*c9496f6bSchristos * notice, this list of conditions and the following disclaimer in
17*c9496f6bSchristos * the documentation and/or other materials provided with the
18*c9496f6bSchristos * distribution.
19*c9496f6bSchristos *
20*c9496f6bSchristos * 3. All advertising materials mentioning features or use of this
21*c9496f6bSchristos * software must display the following acknowledgment:
22*c9496f6bSchristos * "This product includes software developed by the OpenSSL Project
23*c9496f6bSchristos * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*c9496f6bSchristos *
25*c9496f6bSchristos * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*c9496f6bSchristos * endorse or promote products derived from this software without
27*c9496f6bSchristos * prior written permission. For written permission, please contact
28*c9496f6bSchristos * licensing@OpenSSL.org.
29*c9496f6bSchristos *
30*c9496f6bSchristos * 5. Products derived from this software may not be called "OpenSSL"
31*c9496f6bSchristos * nor may "OpenSSL" appear in their names without prior written
32*c9496f6bSchristos * permission of the OpenSSL Project.
33*c9496f6bSchristos *
34*c9496f6bSchristos * 6. Redistributions of any form whatsoever must retain the following
35*c9496f6bSchristos * acknowledgment:
36*c9496f6bSchristos * "This product includes software developed by the OpenSSL Project
37*c9496f6bSchristos * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*c9496f6bSchristos *
39*c9496f6bSchristos * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*c9496f6bSchristos * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*c9496f6bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*c9496f6bSchristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*c9496f6bSchristos * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*c9496f6bSchristos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*c9496f6bSchristos * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*c9496f6bSchristos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*c9496f6bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*c9496f6bSchristos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*c9496f6bSchristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*c9496f6bSchristos * OF THE POSSIBILITY OF SUCH DAMAGE.
51*c9496f6bSchristos * ====================================================================
52*c9496f6bSchristos *
53*c9496f6bSchristos * This product includes cryptographic software written by Eric Young
54*c9496f6bSchristos * (eay@cryptsoft.com). This product includes software written by Tim
55*c9496f6bSchristos * Hudson (tjh@cryptsoft.com).
56*c9496f6bSchristos *
57*c9496f6bSchristos */
58*c9496f6bSchristos
59*c9496f6bSchristos /*
60*c9496f6bSchristos
61*c9496f6bSchristos This is an implementation of Triple DES Cipher Block Chaining with Output
62*c9496f6bSchristos Feedback Masking, by Coppersmith, Johnson and Matyas, (IBM and Certicom).
63*c9496f6bSchristos
64*c9496f6bSchristos Note that there is a known attack on this by Biham and Knudsen but it takes
65*c9496f6bSchristos a lot of work:
66*c9496f6bSchristos
67*c9496f6bSchristos http://www.cs.technion.ac.il/users/wwwb/cgi-bin/tr-get.cgi/1998/CS/CS0928.ps.gz
68*c9496f6bSchristos
69*c9496f6bSchristos */
70*c9496f6bSchristos
71*c9496f6bSchristos #ifndef NO_DESCBCM
72*c9496f6bSchristos #include "des_locl.h"
73*c9496f6bSchristos
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)74*c9496f6bSchristos void des_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out,
75*c9496f6bSchristos long length, des_key_schedule ks1, des_key_schedule ks2,
76*c9496f6bSchristos des_key_schedule ks3, des_cblock *ivec1, des_cblock *ivec2,
77*c9496f6bSchristos int enc)
78*c9496f6bSchristos {
79*c9496f6bSchristos register DES_LONG tin0,tin1;
80*c9496f6bSchristos register DES_LONG tout0,tout1,xor0,xor1,m0,m1;
81*c9496f6bSchristos register long l=length;
82*c9496f6bSchristos DES_LONG tin[2];
83*c9496f6bSchristos unsigned char *iv1,*iv2;
84*c9496f6bSchristos
85*c9496f6bSchristos iv1 = &(*ivec1)[0];
86*c9496f6bSchristos iv2 = &(*ivec2)[0];
87*c9496f6bSchristos
88*c9496f6bSchristos if (enc)
89*c9496f6bSchristos {
90*c9496f6bSchristos c2l(iv1,m0);
91*c9496f6bSchristos c2l(iv1,m1);
92*c9496f6bSchristos c2l(iv2,tout0);
93*c9496f6bSchristos c2l(iv2,tout1);
94*c9496f6bSchristos for (l-=8; l>=-7; l-=8)
95*c9496f6bSchristos {
96*c9496f6bSchristos tin[0]=m0;
97*c9496f6bSchristos tin[1]=m1;
98*c9496f6bSchristos des_encrypt1(tin,ks3,1);
99*c9496f6bSchristos m0=tin[0];
100*c9496f6bSchristos m1=tin[1];
101*c9496f6bSchristos
102*c9496f6bSchristos if(l < 0)
103*c9496f6bSchristos {
104*c9496f6bSchristos c2ln(in,tin0,tin1,l+8);
105*c9496f6bSchristos }
106*c9496f6bSchristos else
107*c9496f6bSchristos {
108*c9496f6bSchristos c2l(in,tin0);
109*c9496f6bSchristos c2l(in,tin1);
110*c9496f6bSchristos }
111*c9496f6bSchristos tin0^=tout0;
112*c9496f6bSchristos tin1^=tout1;
113*c9496f6bSchristos
114*c9496f6bSchristos tin[0]=tin0;
115*c9496f6bSchristos tin[1]=tin1;
116*c9496f6bSchristos des_encrypt1(tin,ks1,1);
117*c9496f6bSchristos tin[0]^=m0;
118*c9496f6bSchristos tin[1]^=m1;
119*c9496f6bSchristos des_encrypt1(tin,ks2,0);
120*c9496f6bSchristos tin[0]^=m0;
121*c9496f6bSchristos tin[1]^=m1;
122*c9496f6bSchristos des_encrypt1(tin,ks1,1);
123*c9496f6bSchristos tout0=tin[0];
124*c9496f6bSchristos tout1=tin[1];
125*c9496f6bSchristos
126*c9496f6bSchristos l2c(tout0,out);
127*c9496f6bSchristos l2c(tout1,out);
128*c9496f6bSchristos }
129*c9496f6bSchristos iv1=&(*ivec1)[0];
130*c9496f6bSchristos l2c(m0,iv1);
131*c9496f6bSchristos l2c(m1,iv1);
132*c9496f6bSchristos
133*c9496f6bSchristos iv2=&(*ivec2)[0];
134*c9496f6bSchristos l2c(tout0,iv2);
135*c9496f6bSchristos l2c(tout1,iv2);
136*c9496f6bSchristos }
137*c9496f6bSchristos else
138*c9496f6bSchristos {
139*c9496f6bSchristos register DES_LONG t0,t1;
140*c9496f6bSchristos
141*c9496f6bSchristos c2l(iv1,m0);
142*c9496f6bSchristos c2l(iv1,m1);
143*c9496f6bSchristos c2l(iv2,xor0);
144*c9496f6bSchristos c2l(iv2,xor1);
145*c9496f6bSchristos for (l-=8; l>=-7; l-=8)
146*c9496f6bSchristos {
147*c9496f6bSchristos tin[0]=m0;
148*c9496f6bSchristos tin[1]=m1;
149*c9496f6bSchristos des_encrypt1(tin,ks3,1);
150*c9496f6bSchristos m0=tin[0];
151*c9496f6bSchristos m1=tin[1];
152*c9496f6bSchristos
153*c9496f6bSchristos c2l(in,tin0);
154*c9496f6bSchristos c2l(in,tin1);
155*c9496f6bSchristos
156*c9496f6bSchristos t0=tin0;
157*c9496f6bSchristos t1=tin1;
158*c9496f6bSchristos
159*c9496f6bSchristos tin[0]=tin0;
160*c9496f6bSchristos tin[1]=tin1;
161*c9496f6bSchristos des_encrypt1(tin,ks1,0);
162*c9496f6bSchristos tin[0]^=m0;
163*c9496f6bSchristos tin[1]^=m1;
164*c9496f6bSchristos des_encrypt1(tin,ks2,1);
165*c9496f6bSchristos tin[0]^=m0;
166*c9496f6bSchristos tin[1]^=m1;
167*c9496f6bSchristos des_encrypt1(tin,ks1,0);
168*c9496f6bSchristos tout0=tin[0];
169*c9496f6bSchristos tout1=tin[1];
170*c9496f6bSchristos
171*c9496f6bSchristos tout0^=xor0;
172*c9496f6bSchristos tout1^=xor1;
173*c9496f6bSchristos if(l < 0)
174*c9496f6bSchristos {
175*c9496f6bSchristos l2cn(tout0,tout1,out,l+8);
176*c9496f6bSchristos }
177*c9496f6bSchristos else
178*c9496f6bSchristos {
179*c9496f6bSchristos l2c(tout0,out);
180*c9496f6bSchristos l2c(tout1,out);
181*c9496f6bSchristos }
182*c9496f6bSchristos xor0=t0;
183*c9496f6bSchristos xor1=t1;
184*c9496f6bSchristos }
185*c9496f6bSchristos
186*c9496f6bSchristos iv1=&(*ivec1)[0];
187*c9496f6bSchristos l2c(m0,iv1);
188*c9496f6bSchristos l2c(m1,iv1);
189*c9496f6bSchristos
190*c9496f6bSchristos iv2=&(*ivec2)[0];
191*c9496f6bSchristos l2c(xor0,iv2);
192*c9496f6bSchristos l2c(xor1,iv2);
193*c9496f6bSchristos }
194*c9496f6bSchristos tin0=tin1=tout0=tout1=xor0=xor1=0;
195*c9496f6bSchristos tin[0]=tin[1]=0;
196*c9496f6bSchristos }
197*c9496f6bSchristos #endif
198