1 /* ====================================================================
2 * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
30 * 6. Redistributions of any form whatsoever must retain the following
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
50 #include <openssl/crypto.h>
51 #include "modes_lcl.h"
61 int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx,
62 const unsigned char iv[16],
63 const unsigned char *inp, unsigned char *out,
82 memcpy(tweak.c, iv, 16);
84 (*ctx->block2) (tweak.c, tweak.c, ctx->key2);
86 if (!enc && (len % 16))
90 #if defined(STRICT_ALIGNMENT)
91 memcpy(scratch.c, inp, 16);
92 scratch.u[0] ^= tweak.u[0];
93 scratch.u[1] ^= tweak.u[1];
95 scratch.u[0] = ((u64 *)inp)[0] ^ tweak.u[0];
96 scratch.u[1] = ((u64 *)inp)[1] ^ tweak.u[1];
98 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
99 #if defined(STRICT_ALIGNMENT)
100 scratch.u[0] ^= tweak.u[0];
101 scratch.u[1] ^= tweak.u[1];
102 memcpy(out, scratch.c, 16);
104 ((u64 *)out)[0] = scratch.u[0] ^= tweak.u[0];
105 ((u64 *)out)[1] = scratch.u[1] ^= tweak.u[1];
114 if (is_endian.little) {
115 unsigned int carry, res;
117 res = 0x87 & (((int)tweak.d[3]) >> 31);
118 carry = (unsigned int)(tweak.u[0] >> 63);
119 tweak.u[0] = (tweak.u[0] << 1) ^ res;
120 tweak.u[1] = (tweak.u[1] << 1) | carry;
124 for (c = 0, i = 0; i < 16; ++i) {
126 * + substitutes for |, because c is 1 bit
128 c += ((size_t)tweak.c[i]) << 1;
132 tweak.c[0] ^= (u8)(0x87 & (0 - c));
136 for (i = 0; i < len; ++i) {
138 out[i] = scratch.c[i];
141 scratch.u[0] ^= tweak.u[0];
142 scratch.u[1] ^= tweak.u[1];
143 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
144 scratch.u[0] ^= tweak.u[0];
145 scratch.u[1] ^= tweak.u[1];
146 memcpy(out - 16, scratch.c, 16);
153 if (is_endian.little) {
154 unsigned int carry, res;
156 res = 0x87 & (((int)tweak.d[3]) >> 31);
157 carry = (unsigned int)(tweak.u[0] >> 63);
158 tweak1.u[0] = (tweak.u[0] << 1) ^ res;
159 tweak1.u[1] = (tweak.u[1] << 1) | carry;
163 for (c = 0, i = 0; i < 16; ++i) {
165 * + substitutes for |, because c is 1 bit
167 c += ((size_t)tweak.c[i]) << 1;
171 tweak1.c[0] ^= (u8)(0x87 & (0 - c));
173 #if defined(STRICT_ALIGNMENT)
174 memcpy(scratch.c, inp, 16);
175 scratch.u[0] ^= tweak1.u[0];
176 scratch.u[1] ^= tweak1.u[1];
178 scratch.u[0] = ((u64 *)inp)[0] ^ tweak1.u[0];
179 scratch.u[1] = ((u64 *)inp)[1] ^ tweak1.u[1];
181 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
182 scratch.u[0] ^= tweak1.u[0];
183 scratch.u[1] ^= tweak1.u[1];
185 for (i = 0; i < len; ++i) {
187 out[16 + i] = scratch.c[i];
190 scratch.u[0] ^= tweak.u[0];
191 scratch.u[1] ^= tweak.u[1];
192 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
193 #if defined(STRICT_ALIGNMENT)
194 scratch.u[0] ^= tweak.u[0];
195 scratch.u[1] ^= tweak.u[1];
196 memcpy(out, scratch.c, 16);
198 ((u64 *)out)[0] = scratch.u[0] ^ tweak.u[0];
199 ((u64 *)out)[1] = scratch.u[1] ^ tweak.u[1];