New Camellia implementation (replacing previous version)
[openssl.git] / crypto / camellia / cmll_cbc.c
1 /* crypto/camellia/camellia_cbc.c -*- mode:C; c-file-style: "eay" -*- */
2 /* ====================================================================
3  * Copyright (c) 2006 The OpenSSL Project.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer. 
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in
14  *    the documentation and/or other materials provided with the
15  *    distribution.
16  *
17  * 3. All advertising materials mentioning features or use of this
18  *    software must display the following acknowledgment:
19  *    "This product includes software developed by the OpenSSL Project
20  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
21  *
22  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23  *    endorse or promote products derived from this software without
24  *    prior written permission. For written permission, please contact
25  *    openssl-core@openssl.org.
26  *
27  * 5. Products derived from this software may not be called "OpenSSL"
28  *    nor may "OpenSSL" appear in their names without prior written
29  *    permission of the OpenSSL Project.
30  *
31  * 6. Redistributions of any form whatsoever must retain the following
32  *    acknowledgment:
33  *    "This product includes software developed by the OpenSSL Project
34  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
35  *
36  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47  * OF THE POSSIBILITY OF SUCH DAMAGE.
48  * ====================================================================
49  *
50  */
51
52 #ifndef CAMELLIA_DEBUG
53 # ifndef NDEBUG
54 #  define NDEBUG
55 # endif
56 #endif
57 #include <assert.h>
58 #include <stdio.h>
59 #include <string.h>
60
61 #include <openssl/camellia.h>
62 #include "cmll_locl.h"
63
64 void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
65                      const unsigned long length, const CAMELLIA_KEY *key,
66                      unsigned char *ivec, const int enc) {
67
68         unsigned long n;
69         unsigned long len = length;
70         unsigned char tmp[CAMELLIA_BLOCK_SIZE];
71         const unsigned char *iv = ivec;
72         uint32_t t32[UNITSIZE];
73
74
75         assert(in && out && key && ivec);
76         assert((CAMELLIA_ENCRYPT == enc)||(CAMELLIA_DECRYPT == enc));
77
78         if(((size_t)in) % ALIGN == 0
79                 && ((size_t)out) % ALIGN == 0
80                 && ((size_t)ivec) % ALIGN == 0)
81                 {
82                 if (CAMELLIA_ENCRYPT == enc)
83                         {
84                         while (len >= CAMELLIA_BLOCK_SIZE)
85                                 {
86                                 XOR4WORD2((uint32_t *)out,
87                                         (uint32_t *)in, (uint32_t *)iv);
88                                 key->enc(key->rd_key, (uint32_t *)out);
89                                 iv = out;
90                                 len -= CAMELLIA_BLOCK_SIZE;
91                                 in += CAMELLIA_BLOCK_SIZE;
92                                 out += CAMELLIA_BLOCK_SIZE;
93                                 }
94                         if (len)
95                                 {
96                                 for(n=0; n < len; ++n)
97                                         out[n] = in[n] ^ iv[n];
98                                 for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
99                                         out[n] = iv[n];
100                                 key->enc(key->rd_key, (uint32_t *)out);
101                                 iv = out;
102                                 }
103                         memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
104                         }
105                 else if (in != out)
106                         {
107                         while (len >= CAMELLIA_BLOCK_SIZE)
108                                 {
109                                 memcpy(out,in,CAMELLIA_BLOCK_SIZE);
110                                 key->dec(key->rd_key,(uint32_t *)out);
111                                 XOR4WORD((uint32_t *)out, (uint32_t *)iv);
112                                 iv = in;
113                                 len -= CAMELLIA_BLOCK_SIZE;
114                                 in  += CAMELLIA_BLOCK_SIZE;
115                                 out += CAMELLIA_BLOCK_SIZE;
116                                 }
117                         if (len)
118                                 {
119                                 memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
120                                 key->dec(key->rd_key, (uint32_t *)tmp);
121                                 for(n=0; n < len; ++n)
122                                         out[n] = tmp[n] ^ iv[n];
123                                 iv = in;
124                                 }
125                         memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
126                         }
127                 else /* in == out */
128                         {
129                         while (len >= CAMELLIA_BLOCK_SIZE)
130                                 {
131                                 memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
132                                 key->dec(key->rd_key, (uint32_t *)out);
133                                 XOR4WORD((uint32_t *)out, (uint32_t *)ivec);
134                                 memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
135                                 len -= CAMELLIA_BLOCK_SIZE;
136                                 in += CAMELLIA_BLOCK_SIZE;
137                                 out += CAMELLIA_BLOCK_SIZE;
138                                 }
139                         if (len)
140                                 {
141                                 memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
142                                 key->dec(key->rd_key,(uint32_t *)out);
143                                 for(n=0; n < len; ++n)
144                                         out[n] ^= ivec[n];
145                                 for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
146                                         out[n] = tmp[n];
147                                 memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
148                                 }
149                         }
150                 }
151         else /* no aligned */
152                 {
153                 if (CAMELLIA_ENCRYPT == enc)
154                         {
155                         while (len >= CAMELLIA_BLOCK_SIZE)
156                                 {
157                                 for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
158                                         out[n] = in[n] ^ iv[n];
159                                 memcpy(t32, out, CAMELLIA_BLOCK_SIZE);
160                                 key->enc(key->rd_key, t32);
161                                 memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
162                                 iv = out;
163                                 len -= CAMELLIA_BLOCK_SIZE;
164                                 in += CAMELLIA_BLOCK_SIZE;
165                                 out += CAMELLIA_BLOCK_SIZE;
166                                 }
167                         if (len)
168                                 {
169                                 for(n=0; n < len; ++n)
170                                         out[n] = in[n] ^ iv[n];
171                                 for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
172                                         out[n] = iv[n];
173                                 key->enc(key->rd_key, (uint32_t *)out);
174                                 iv = out;
175                                 }
176                         memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
177                         }
178                 else if (in != out)
179                         {
180                         while (len >= CAMELLIA_BLOCK_SIZE)
181                                 {
182                                 memcpy(t32,in,CAMELLIA_BLOCK_SIZE);
183                                 key->dec(key->rd_key,t32);
184                                 memcpy(out,t32,CAMELLIA_BLOCK_SIZE);
185                                 for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
186                                         out[n] ^= iv[n];
187                                 iv = in;
188                                 len -= CAMELLIA_BLOCK_SIZE;
189                                 in  += CAMELLIA_BLOCK_SIZE;
190                                 out += CAMELLIA_BLOCK_SIZE;
191                                 }
192                         if (len)
193                                 {
194                                 memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
195                                 memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
196                                 key->dec(key->rd_key, t32);
197                                 memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
198                                 for(n=0; n < len; ++n)
199                                         out[n] = tmp[n] ^ iv[n];
200                                 iv = in;
201                                 }
202                         memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
203                         }
204                 else
205                         {
206                         while (len >= CAMELLIA_BLOCK_SIZE)
207                                 {
208                                 memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
209                                 memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
210                                 key->dec(key->rd_key, t32);
211                                 memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
212                                 for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
213                                         out[n] ^= ivec[n];
214                                 memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
215                                 len -= CAMELLIA_BLOCK_SIZE;
216                                 in += CAMELLIA_BLOCK_SIZE;
217                                 out += CAMELLIA_BLOCK_SIZE;
218                                 }
219                         if (len)
220                                 {
221                                 memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
222                                 memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
223                                 key->dec(key->rd_key,t32);
224                                 memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
225                                 for(n=0; n < len; ++n)
226                                         out[n] ^= ivec[n];
227                                 for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
228                                         out[n] = tmp[n];
229                                 memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
230                                 }
231                         }
232                 }
233 }
234