POK
ncbc_enc.c
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Tue Dec 8 15:53:28 2009
15  */
16 
17 /* crypto/des/ncbc_enc.c */
18 /*
19  * #included by:
20  * cbc_enc.c (DES_cbc_encrypt)
21  * des_enc.c (DES_ncbc_encrypt)
22  */
23 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
24  * All rights reserved.
25  *
26  * This package is an SSL implementation written
27  * by Eric Young (eay@cryptsoft.com).
28  * The implementation was written so as to conform with Netscapes SSL.
29  *
30  * This library is free for commercial and non-commercial use as long as
31  * the following conditions are aheared to. The following conditions
32  * apply to all code found in this distribution, be it the RC4, RSA,
33  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
34  * included with this distribution is covered by the same copyright terms
35  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
36  *
37  * Copyright remains Eric Young's, and as such any Copyright notices in
38  * the code are not to be removed.
39  * If this package is used in a product, Eric Young should be given attribution
40  * as the author of the parts of the library used.
41  * This can be in the form of a textual message at program startup or
42  * in documentation (online or textual) provided with the package.
43  *
44  * Redistribution and use in source and binary forms, with or without
45  * modification, are permitted provided that the following conditions
46  * are met:
47  * 1. Redistributions of source code must retain the copyright
48  * notice, this list of conditions and the following disclaimer.
49  * 2. Redistributions in binary form must reproduce the above copyright
50  * notice, this list of conditions and the following disclaimer in the
51  * documentation and/or other materials provided with the distribution.
52  * 3. All advertising materials mentioning features or use of this software
53  * must display the following acknowledgement:
54  * "This product includes cryptographic software written by
55  * Eric Young (eay@cryptsoft.com)"
56  * The word 'cryptographic' can be left out if the rouines from the library
57  * being used are not cryptographic related :-).
58  * 4. If you include any Windows specific code (or a derivative thereof) from
59  * the apps directory (application code) you must include an acknowledgement:
60  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
61  *
62  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
63  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
66  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
67  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
68  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
69  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
70  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
71  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
72  * SUCH DAMAGE.
73  *
74  * The licence and distribution terms for any publically available version or
75  * derivative of this code cannot be changed. i.e. this code cannot simply be
76  * copied and put under another distribution licence
77  * [including the GNU Public Licence.]
78  */
79 
80 #ifdef POK_NEEDS_PROTOCOLS_DES
81 
82 #include "des_locl.h"
83 
84 #ifdef CBC_ENC_C__DONT_UPDATE_IV
85 void DES_cbc_encrypt(const unsigned char *in, unsigned char *out, long length,
86  DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
87 #else
88 void DES_ncbc_encrypt(const unsigned char *in, unsigned char *out, long length,
89  DES_key_schedule *_schedule, DES_cblock *ivec, int enc)
90 #endif
91  {
92  register DES_LONG tin0,tin1;
93  register DES_LONG tout0,tout1,xor0,xor1;
94  register long l=length;
95  DES_LONG tin[2];
96  unsigned char *iv;
97 
98  iv = &(*ivec)[0];
99 
100  if (enc)
101  {
102  c2l(iv,tout0);
103  c2l(iv,tout1);
104  for (l-=8; l>=0; l-=8)
105  {
106  c2l(in,tin0);
107  c2l(in,tin1);
108  tin0^=tout0; tin[0]=tin0;
109  tin1^=tout1; tin[1]=tin1;
110  DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
111  tout0=tin[0]; l2c(tout0,out);
112  tout1=tin[1]; l2c(tout1,out);
113  }
114  if (l != -8)
115  {
116  c2ln(in,tin0,tin1,l+8);
117  tin0^=tout0; tin[0]=tin0;
118  tin1^=tout1; tin[1]=tin1;
119  DES_encrypt1((DES_LONG *)tin,_schedule,DES_ENCRYPT);
120  tout0=tin[0]; l2c(tout0,out);
121  tout1=tin[1]; l2c(tout1,out);
122  }
123 #ifndef CBC_ENC_C__DONT_UPDATE_IV
124  iv = &(*ivec)[0];
125  l2c(tout0,iv);
126  l2c(tout1,iv);
127 #endif
128  }
129  else
130  {
131  c2l(iv,xor0);
132  c2l(iv,xor1);
133  for (l-=8; l>=0; l-=8)
134  {
135  c2l(in,tin0); tin[0]=tin0;
136  c2l(in,tin1); tin[1]=tin1;
137  DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
138  tout0=tin[0]^xor0;
139  tout1=tin[1]^xor1;
140  l2c(tout0,out);
141  l2c(tout1,out);
142  xor0=tin0;
143  xor1=tin1;
144  }
145  if (l != -8)
146  {
147  c2l(in,tin0); tin[0]=tin0;
148  c2l(in,tin1); tin[1]=tin1;
149  DES_encrypt1((DES_LONG *)tin,_schedule,DES_DECRYPT);
150  tout0=tin[0]^xor0;
151  tout1=tin[1]^xor1;
152  l2cn(tout0,tout1,out,l+8);
153 #ifndef CBC_ENC_C__DONT_UPDATE_IV
154  xor0=tin0;
155  xor1=tin1;
156 #endif
157  }
158 #ifndef CBC_ENC_C__DONT_UPDATE_IV
159  iv = &(*ivec)[0];
160  l2c(xor0,iv);
161  l2c(xor1,iv);
162 #endif
163  }
164  tin0=tin1=tout0=tout1=xor0=xor1=0;
165  tin[0]=tin[1]=0;
166  }
167 
168 
169 #endif /* POK_NEEDS_ ...*/