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