| 1 |
// Copyright (c) 2011 The OpenSSL Project. All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/evp.h> |
| 5 |
|
| 6 |
#include <string.h> |
| 7 |
|
| 8 |
#include <openssl/aes.h> |
| 9 |
#include <openssl/cipher.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
|
| 12 |
#include "internal.h" |
| 13 |
#include "../../internal.h" |
| 14 |
|
| 15 |
size_t CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx, |
| 16 |
const uint8_t iv[16], const uint8_t *inp, |
| 17 |
uint8_t *out, size_t len, int enc) { |
| 18 |
union { |
| 19 |
uint64_t u[2]; |
| 20 |
uint8_t c[16]; |
| 21 |
} tweak, scratch; |
| 22 |
unsigned int i; |
| 23 |
|
| 24 |
if (len < 16) return 0; |
| 25 |
|
| 26 |
OPENSSL_memcpy(tweak.c, iv, 16); |
| 27 |
|
| 28 |
(*ctx->block2)(tweak.c, tweak.c, ctx->key2); |
| 29 |
|
| 30 |
if (!enc && (len % 16)) len -= 16; |
| 31 |
|
| 32 |
while (len >= 16) { |
| 33 |
OPENSSL_memcpy(scratch.c, inp, 16); |
| 34 |
scratch.u[0] ^= tweak.u[0]; |
| 35 |
scratch.u[1] ^= tweak.u[1]; |
| 36 |
(*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 37 |
scratch.u[0] ^= tweak.u[0]; |
| 38 |
scratch.u[1] ^= tweak.u[1]; |
| 39 |
OPENSSL_memcpy(out, scratch.c, 16); |
| 40 |
inp += 16; |
| 41 |
out += 16; |
| 42 |
len -= 16; |
| 43 |
|
| 44 |
if (len == 0) return 1; |
| 45 |
|
| 46 |
unsigned int carry, res; |
| 47 |
|
| 48 |
#if defined(OPENSSL_BIG_ENDIAN) |
| 49 |
uint64_t tweak_u0, tweak_u1; |
| 50 |
tweak_u0 = CRYPTO_load_u64_le(&tweak.u[0]); |
| 51 |
tweak_u1 = CRYPTO_load_u64_le(&tweak.u[1]); |
| 52 |
res = 0x87 & (((int64_t)tweak_u1) >> 63); |
| 53 |
carry = (unsigned int)(tweak_u0 >> 63); |
| 54 |
tweak_u0 = (tweak_u0 << 1) ^ res; |
| 55 |
tweak_u1 = (tweak_u1 << 1) | carry; |
| 56 |
CRYPTO_store_u64_le(&tweak.u[0], tweak_u0); |
| 57 |
CRYPTO_store_u64_le(&tweak.u[1], tweak_u1); |
| 58 |
#else |
| 59 |
res = 0x87 & (((int64_t)tweak.u[1]) >> 63); |
| 60 |
carry = (unsigned int)(tweak.u[0] >> 63); |
| 61 |
tweak.u[0] = (tweak.u[0] << 1) ^ res; |
| 62 |
tweak.u[1] = (tweak.u[1] << 1) | carry; |
| 63 |
#endif |
| 64 |
} |
| 65 |
if (enc) { |
| 66 |
for (i = 0; i < len; ++i) { |
| 67 |
uint8_t c = inp[i]; |
| 68 |
out[i] = scratch.c[i]; |
| 69 |
scratch.c[i] = c; |
| 70 |
} |
| 71 |
scratch.u[0] ^= tweak.u[0]; |
| 72 |
scratch.u[1] ^= tweak.u[1]; |
| 73 |
(*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 74 |
scratch.u[0] ^= tweak.u[0]; |
| 75 |
scratch.u[1] ^= tweak.u[1]; |
| 76 |
OPENSSL_memcpy(out - 16, scratch.c, 16); |
| 77 |
} else { |
| 78 |
union { |
| 79 |
uint64_t u[2]; |
| 80 |
uint8_t c[16]; |
| 81 |
} tweak1; |
| 82 |
|
| 83 |
unsigned int carry, res; |
| 84 |
|
| 85 |
#if defined(OPENSSL_BIG_ENDIAN) |
| 86 |
uint64_t tweak_u0, tweak_u1; |
| 87 |
tweak_u0 = CRYPTO_load_u64_le(&tweak.u[0]); |
| 88 |
tweak_u1 = CRYPTO_load_u64_le(&tweak.u[1]); |
| 89 |
res = 0x87 & (((int64_t)tweak_u1) >> 63); |
| 90 |
carry = (unsigned int)(tweak_u0 >> 63); |
| 91 |
tweak_u0 = (tweak_u0 << 1) ^ res; |
| 92 |
tweak_u1 = (tweak_u1 << 1) | carry; |
| 93 |
CRYPTO_store_u64_le(&tweak1.u[0], tweak_u0); |
| 94 |
CRYPTO_store_u64_le(&tweak1.u[1], tweak_u1); |
| 95 |
#else |
| 96 |
res = 0x87 & (((int64_t)tweak.u[1]) >> 63); |
| 97 |
carry = (unsigned int)(tweak.u[0] >> 63); |
| 98 |
tweak1.u[0] = (tweak.u[0] << 1) ^ res; |
| 99 |
tweak1.u[1] = (tweak.u[1] << 1) | carry; |
| 100 |
#endif |
| 101 |
OPENSSL_memcpy(scratch.c, inp, 16); |
| 102 |
scratch.u[0] ^= tweak1.u[0]; |
| 103 |
scratch.u[1] ^= tweak1.u[1]; |
| 104 |
(*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 105 |
scratch.u[0] ^= tweak1.u[0]; |
| 106 |
scratch.u[1] ^= tweak1.u[1]; |
| 107 |
|
| 108 |
for (i = 0; i < len; ++i) { |
| 109 |
uint8_t c = inp[16 + i]; |
| 110 |
out[16 + i] = scratch.c[i]; |
| 111 |
scratch.c[i] = c; |
| 112 |
} |
| 113 |
scratch.u[0] ^= tweak.u[0]; |
| 114 |
scratch.u[1] ^= tweak.u[1]; |
| 115 |
(*ctx->block1)(scratch.c, scratch.c, ctx->key1); |
| 116 |
scratch.u[0] ^= tweak.u[0]; |
| 117 |
scratch.u[1] ^= tweak.u[1]; |
| 118 |
OPENSSL_memcpy(out, scratch.c, 16); |
| 119 |
} |
| 120 |
|
| 121 |
return 1; |
| 122 |
} |