2 * Copyright 2011-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/crypto.h>
12 #include "internal/endian.h"
13 #include "crypto/modes.h"
15 #ifndef STRICT_ALIGNMENT
17 typedef u64 u64_a1 __attribute((__aligned__(1)));
23 int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx,
24 const unsigned char iv[16],
25 const unsigned char *inp, unsigned char *out,
39 memcpy(tweak.c, iv, 16);
41 (*ctx->block2) (tweak.c, tweak.c, ctx->key2);
43 if (!enc && (len % 16))
47 #if defined(STRICT_ALIGNMENT)
48 memcpy(scratch.c, inp, 16);
49 scratch.u[0] ^= tweak.u[0];
50 scratch.u[1] ^= tweak.u[1];
52 scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak.u[0];
53 scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak.u[1];
55 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
56 #if defined(STRICT_ALIGNMENT)
57 scratch.u[0] ^= tweak.u[0];
58 scratch.u[1] ^= tweak.u[1];
59 memcpy(out, scratch.c, 16);
61 ((u64_a1 *)out)[0] = scratch.u[0] ^= tweak.u[0];
62 ((u64_a1 *)out)[1] = scratch.u[1] ^= tweak.u[1];
71 if (IS_LITTLE_ENDIAN) {
72 unsigned int carry, res;
74 res = 0x87 & (((int)tweak.d[3]) >> 31);
75 carry = (unsigned int)(tweak.u[0] >> 63);
76 tweak.u[0] = (tweak.u[0] << 1) ^ res;
77 tweak.u[1] = (tweak.u[1] << 1) | carry;
81 for (c = 0, i = 0; i < 16; ++i) {
83 * + substitutes for |, because c is 1 bit
85 c += ((size_t)tweak.c[i]) << 1;
89 tweak.c[0] ^= (u8)(0x87 & (0 - c));
93 for (i = 0; i < len; ++i) {
95 out[i] = scratch.c[i];
98 scratch.u[0] ^= tweak.u[0];
99 scratch.u[1] ^= tweak.u[1];
100 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
101 scratch.u[0] ^= tweak.u[0];
102 scratch.u[1] ^= tweak.u[1];
103 memcpy(out - 16, scratch.c, 16);
110 if (IS_LITTLE_ENDIAN) {
111 unsigned int carry, res;
113 res = 0x87 & (((int)tweak.d[3]) >> 31);
114 carry = (unsigned int)(tweak.u[0] >> 63);
115 tweak1.u[0] = (tweak.u[0] << 1) ^ res;
116 tweak1.u[1] = (tweak.u[1] << 1) | carry;
120 for (c = 0, i = 0; i < 16; ++i) {
122 * + substitutes for |, because c is 1 bit
124 c += ((size_t)tweak.c[i]) << 1;
128 tweak1.c[0] ^= (u8)(0x87 & (0 - c));
130 #if defined(STRICT_ALIGNMENT)
131 memcpy(scratch.c, inp, 16);
132 scratch.u[0] ^= tweak1.u[0];
133 scratch.u[1] ^= tweak1.u[1];
135 scratch.u[0] = ((u64_a1 *)inp)[0] ^ tweak1.u[0];
136 scratch.u[1] = ((u64_a1 *)inp)[1] ^ tweak1.u[1];
138 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
139 scratch.u[0] ^= tweak1.u[0];
140 scratch.u[1] ^= tweak1.u[1];
142 for (i = 0; i < len; ++i) {
144 out[16 + i] = scratch.c[i];
147 scratch.u[0] ^= tweak.u[0];
148 scratch.u[1] ^= tweak.u[1];
149 (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
150 #if defined(STRICT_ALIGNMENT)
151 scratch.u[0] ^= tweak.u[0];
152 scratch.u[1] ^= tweak.u[1];
153 memcpy(out, scratch.c, 16);
155 ((u64_a1 *)out)[0] = scratch.u[0] ^ tweak.u[0];
156 ((u64_a1 *)out)[1] = scratch.u[1] ^ tweak.u[1];