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"
63 block128_f block1,block2;
66 int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, u64 secno,
67 const unsigned char *inp, unsigned char *out,
70 const union { long one; char little; } is_endian = {1};
71 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch;
74 if (len<16) return -1;
76 if (is_endian.little) {
81 PUTU32(tweak.c,secno);
82 PUTU32(tweak.c+4,secno>>32);
86 (*ctx->block2)(tweak.c,tweak.c,ctx->key2);
88 if (!enc && (len%16)) len-=16;
91 #if defined(STRICT_ALIGNMENT)
92 memcpy(scratch.c,inp,16);
93 scratch.u[0] ^= tweak.u[0];
94 scratch.u[1] ^= tweak.u[1];
96 scratch.u[0] = ((u64*)inp)[0]^tweak.u[0];
97 scratch.u[1] = ((u64*)inp)[1]^tweak.u[1];
99 (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
100 scratch.u[0] ^= tweak.u[0];
101 scratch.u[1] ^= tweak.u[1];
102 memcpy(out,scratch.c,16);
107 if (len==0) return 0;
109 if (is_endian.little) {
110 unsigned int carry,res;
112 res = 0x87&(((int)tweak.d[3])>>31);
113 carry = tweak.u[0]>>63;
114 tweak.u[0] = (tweak.u[0]<<1)^res;
115 tweak.u[1] = (tweak.u[1]<<1)|carry;
118 unsigned int carry,c;
120 for (carry=0,i=0;i<16;++i) {
122 tweak.c[i] = (c<<1)|carry;
125 tweak.c[0] ^= 0x87&(0-carry);
129 for (i=0;i<len;++i) {
131 out[i] = scratch.c[i];
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);
142 union { u64 u[2]; u8 c[16]; } tweak1;
144 if (is_endian.little) {
145 unsigned int carry,res;
147 res = 0x87&(((int)tweak.d[3])>>31);
148 carry = tweak.u[0]>>63;
149 tweak1.u[0] = (tweak.u[0]<<1)^res;
150 tweak1.u[1] = (tweak.u[1]<<1)|carry;
153 unsigned int carry,c;
155 for (carry=0,i=0;i<16;++i) {
157 tweak1.c[i] = (c<<1)|carry;
160 tweak1.c[0] ^= 0x87&(0-carry);
162 #if defined(STRICT_ALIGNMENT)
163 memcpy(scratch.c,inp,16);
164 scratch.u[0] ^= tweak1.u[0];
165 scratch.u[1] ^= tweak1.u[1];
167 scratch.u[0] = ((u64*)inp)[0]^tweak1.u[0];
168 scratch.u[1] = ((u64*)inp)[1]^tweak1.u[1];
170 (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
171 scratch.u[0] ^= tweak1.u[0];
172 scratch.u[1] ^= tweak1.u[1];
174 for (i=0;i<len;++i) {
176 out[16+i] = scratch.c[i];
179 scratch.u[0] ^= tweak.u[0];
180 scratch.u[1] ^= tweak.u[1];
181 (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
182 scratch.u[0] ^= tweak.u[0];
183 scratch.u[1] ^= tweak.u[1];
184 memcpy (out,scratch.c,16);