]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/modes/xts128.c
Provisional AES XTS support.
[thirdparty/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 #ifndef MODES_DEBUG
55 # ifndef NDEBUG
56 # define NDEBUG
57 # endif
58 #endif
59 #include <assert.h>
60
61 int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, const unsigned char *iv,
62 const unsigned char *inp, unsigned char *out,
63 size_t len, int enc)
64 {
65 const union { long one; char little; } is_endian = {1};
66 union { u64 u[2]; u32 d[4]; u8 c[16]; } tweak, scratch;
67 unsigned int i;
68
69 if (len<16) return -1;
70
71 memcpy(tweak.c, iv, 16);
72
73 (*ctx->block2)(tweak.c,tweak.c,ctx->key2);
74
75 if (!enc && (len%16)) len-=16;
76
77 while (len>=16) {
78 #if defined(STRICT_ALIGNMENT)
79 memcpy(scratch.c,inp,16);
80 scratch.u[0] ^= tweak.u[0];
81 scratch.u[1] ^= tweak.u[1];
82 #else
83 scratch.u[0] = ((u64*)inp)[0]^tweak.u[0];
84 scratch.u[1] = ((u64*)inp)[1]^tweak.u[1];
85 #endif
86 (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
87 scratch.u[0] ^= tweak.u[0];
88 scratch.u[1] ^= tweak.u[1];
89 memcpy(out,scratch.c,16);
90 inp += 16;
91 out += 16;
92 len -= 16;
93
94 if (len==0) return 0;
95
96 if (is_endian.little) {
97 unsigned int carry,res;
98
99 res = 0x87&(((int)tweak.d[3])>>31);
100 carry = tweak.u[0]>>63;
101 tweak.u[0] = (tweak.u[0]<<1)^res;
102 tweak.u[1] = (tweak.u[1]<<1)|carry;
103 }
104 else {
105 unsigned int carry,c;
106
107 for (carry=0,i=0;i<16;++i) {
108 c = tweak.c[i];
109 tweak.c[i] = (c<<1)|carry;
110 carry = c>>7;
111 }
112 tweak.c[0] ^= 0x87&(0-carry);
113 }
114 }
115 if (enc) {
116 for (i=0;i<len;++i) {
117 u8 c = inp[i];
118 out[i] = scratch.c[i];
119 scratch.c[i] = c;
120 }
121 scratch.u[0] ^= tweak.u[0];
122 scratch.u[1] ^= tweak.u[1];
123 (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
124 scratch.u[0] ^= tweak.u[0];
125 scratch.u[1] ^= tweak.u[1];
126 memcpy(out-16,scratch.c,16);
127 }
128 else {
129 union { u64 u[2]; u8 c[16]; } tweak1;
130
131 if (is_endian.little) {
132 unsigned int carry,res;
133
134 res = 0x87&(((int)tweak.d[3])>>31);
135 carry = tweak.u[0]>>63;
136 tweak1.u[0] = (tweak.u[0]<<1)^res;
137 tweak1.u[1] = (tweak.u[1]<<1)|carry;
138 }
139 else {
140 unsigned int carry,c;
141
142 for (carry=0,i=0;i<16;++i) {
143 c = tweak.c[i];
144 tweak1.c[i] = (c<<1)|carry;
145 carry = c>>7;
146 }
147 tweak1.c[0] ^= 0x87&(0-carry);
148 }
149 #if defined(STRICT_ALIGNMENT)
150 memcpy(scratch.c,inp,16);
151 scratch.u[0] ^= tweak1.u[0];
152 scratch.u[1] ^= tweak1.u[1];
153 #else
154 scratch.u[0] = ((u64*)inp)[0]^tweak1.u[0];
155 scratch.u[1] = ((u64*)inp)[1]^tweak1.u[1];
156 #endif
157 (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
158 scratch.u[0] ^= tweak1.u[0];
159 scratch.u[1] ^= tweak1.u[1];
160
161 for (i=0;i<len;++i) {
162 u8 c = inp[16+i];
163 out[16+i] = scratch.c[i];
164 scratch.c[i] = c;
165 }
166 scratch.u[0] ^= tweak.u[0];
167 scratch.u[1] ^= tweak.u[1];
168 (*ctx->block1)(scratch.c,scratch.c,ctx->key1);
169 scratch.u[0] ^= tweak.u[0];
170 scratch.u[1] ^= tweak.u[1];
171 memcpy (out,scratch.c,16);
172 }
173
174 return 0;
175 }