Remove --classic build entirely
[openssl.git] / crypto / modes / xts128.c
1 /* ====================================================================
2  * Copyright (c) 2011 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 #include <openssl/crypto.h>
51 #include "modes_lcl.h"
52 #include <string.h>
53
54 int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx,
55                           const unsigned char iv[16],
56                           const unsigned char *inp, unsigned char *out,
57                           size_t len, int enc)
58 {
59     const union {
60         long one;
61         char little;
62     } is_endian = {
63         1
64     };
65     union {
66         u64 u[2];
67         u32 d[4];
68         u8 c[16];
69     } tweak, scratch;
70     unsigned int i;
71
72     if (len < 16)
73         return -1;
74
75     memcpy(tweak.c, iv, 16);
76
77     (*ctx->block2) (tweak.c, tweak.c, ctx->key2);
78
79     if (!enc && (len % 16))
80         len -= 16;
81
82     while (len >= 16) {
83 #if defined(STRICT_ALIGNMENT)
84         memcpy(scratch.c, inp, 16);
85         scratch.u[0] ^= tweak.u[0];
86         scratch.u[1] ^= tweak.u[1];
87 #else
88         scratch.u[0] = ((u64 *)inp)[0] ^ tweak.u[0];
89         scratch.u[1] = ((u64 *)inp)[1] ^ tweak.u[1];
90 #endif
91         (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
92 #if defined(STRICT_ALIGNMENT)
93         scratch.u[0] ^= tweak.u[0];
94         scratch.u[1] ^= tweak.u[1];
95         memcpy(out, scratch.c, 16);
96 #else
97         ((u64 *)out)[0] = scratch.u[0] ^= tweak.u[0];
98         ((u64 *)out)[1] = scratch.u[1] ^= tweak.u[1];
99 #endif
100         inp += 16;
101         out += 16;
102         len -= 16;
103
104         if (len == 0)
105             return 0;
106
107         if (is_endian.little) {
108             unsigned int carry, res;
109
110             res = 0x87 & (((int)tweak.d[3]) >> 31);
111             carry = (unsigned int)(tweak.u[0] >> 63);
112             tweak.u[0] = (tweak.u[0] << 1) ^ res;
113             tweak.u[1] = (tweak.u[1] << 1) | carry;
114         } else {
115             size_t c;
116
117             for (c = 0, i = 0; i < 16; ++i) {
118                 /*
119                  * + substitutes for |, because c is 1 bit
120                  */
121                 c += ((size_t)tweak.c[i]) << 1;
122                 tweak.c[i] = (u8)c;
123                 c = c >> 8;
124             }
125             tweak.c[0] ^= (u8)(0x87 & (0 - c));
126         }
127     }
128     if (enc) {
129         for (i = 0; i < len; ++i) {
130             u8 c = inp[i];
131             out[i] = scratch.c[i];
132             scratch.c[i] = c;
133         }
134         scratch.u[0] ^= tweak.u[0];
135         scratch.u[1] ^= tweak.u[1];
136         (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
137         scratch.u[0] ^= tweak.u[0];
138         scratch.u[1] ^= tweak.u[1];
139         memcpy(out - 16, scratch.c, 16);
140     } else {
141         union {
142             u64 u[2];
143             u8 c[16];
144         } tweak1;
145
146         if (is_endian.little) {
147             unsigned int carry, res;
148
149             res = 0x87 & (((int)tweak.d[3]) >> 31);
150             carry = (unsigned int)(tweak.u[0] >> 63);
151             tweak1.u[0] = (tweak.u[0] << 1) ^ res;
152             tweak1.u[1] = (tweak.u[1] << 1) | carry;
153         } else {
154             size_t c;
155
156             for (c = 0, i = 0; i < 16; ++i) {
157                 /*
158                  * + substitutes for |, because c is 1 bit
159                  */
160                 c += ((size_t)tweak.c[i]) << 1;
161                 tweak1.c[i] = (u8)c;
162                 c = c >> 8;
163             }
164             tweak1.c[0] ^= (u8)(0x87 & (0 - c));
165         }
166 #if defined(STRICT_ALIGNMENT)
167         memcpy(scratch.c, inp, 16);
168         scratch.u[0] ^= tweak1.u[0];
169         scratch.u[1] ^= tweak1.u[1];
170 #else
171         scratch.u[0] = ((u64 *)inp)[0] ^ tweak1.u[0];
172         scratch.u[1] = ((u64 *)inp)[1] ^ tweak1.u[1];
173 #endif
174         (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
175         scratch.u[0] ^= tweak1.u[0];
176         scratch.u[1] ^= tweak1.u[1];
177
178         for (i = 0; i < len; ++i) {
179             u8 c = inp[16 + i];
180             out[16 + i] = scratch.c[i];
181             scratch.c[i] = c;
182         }
183         scratch.u[0] ^= tweak.u[0];
184         scratch.u[1] ^= tweak.u[1];
185         (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
186 #if defined(STRICT_ALIGNMENT)
187         scratch.u[0] ^= tweak.u[0];
188         scratch.u[1] ^= tweak.u[1];
189         memcpy(out, scratch.c, 16);
190 #else
191         ((u64 *)out)[0] = scratch.u[0] ^ tweak.u[0];
192         ((u64 *)out)[1] = scratch.u[1] ^ tweak.u[1];
193 #endif
194     }
195
196     return 0;
197 }