2016-02-11 17:00:17 +03:00
|
|
|
/*
|
|
|
|
* QEMU Crypto XTS cipher mode
|
|
|
|
*
|
|
|
|
* Copyright (c) 2015-2016 Red Hat, Inc.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
2019-02-13 18:54:59 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2016-02-11 17:00:17 +03:00
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This code is originally derived from public domain / WTFPL code in
|
|
|
|
* LibTomCrypt crytographic library http://libtom.org. The XTS code
|
|
|
|
* was donated by Elliptic Semiconductor Inc (www.ellipticsemi.com)
|
|
|
|
* to the LibTom Projects
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2018-10-09 12:55:14 +03:00
|
|
|
#include "qemu/bswap.h"
|
2016-02-11 17:00:17 +03:00
|
|
|
#include "crypto/xts.h"
|
|
|
|
|
2018-10-09 12:45:41 +03:00
|
|
|
typedef union {
|
|
|
|
uint8_t b[XTS_BLOCK_SIZE];
|
|
|
|
uint64_t u[2];
|
|
|
|
} xts_uint128;
|
|
|
|
|
2018-10-09 12:50:43 +03:00
|
|
|
static inline void xts_uint128_xor(xts_uint128 *D,
|
|
|
|
const xts_uint128 *S1,
|
|
|
|
const xts_uint128 *S2)
|
|
|
|
{
|
|
|
|
D->u[0] = S1->u[0] ^ S2->u[0];
|
|
|
|
D->u[1] = S1->u[1] ^ S2->u[1];
|
|
|
|
}
|
2018-10-09 12:45:41 +03:00
|
|
|
|
2018-10-09 12:55:14 +03:00
|
|
|
static inline void xts_uint128_cpu_to_les(xts_uint128 *v)
|
2016-02-11 17:00:17 +03:00
|
|
|
{
|
2018-10-09 12:55:14 +03:00
|
|
|
cpu_to_le64s(&v->u[0]);
|
|
|
|
cpu_to_le64s(&v->u[1]);
|
|
|
|
}
|
2016-02-11 17:00:17 +03:00
|
|
|
|
2018-10-09 12:55:14 +03:00
|
|
|
static inline void xts_uint128_le_to_cpus(xts_uint128 *v)
|
|
|
|
{
|
|
|
|
le64_to_cpus(&v->u[0]);
|
|
|
|
le64_to_cpus(&v->u[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void xts_mult_x(xts_uint128 *I)
|
|
|
|
{
|
|
|
|
uint64_t tt;
|
|
|
|
|
|
|
|
xts_uint128_le_to_cpus(I);
|
|
|
|
|
|
|
|
tt = I->u[0] >> 63;
|
|
|
|
I->u[0] <<= 1;
|
|
|
|
|
|
|
|
if (I->u[1] >> 63) {
|
|
|
|
I->u[0] ^= 0x87;
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
2018-10-09 12:55:14 +03:00
|
|
|
I->u[1] <<= 1;
|
|
|
|
I->u[1] |= tt;
|
|
|
|
|
|
|
|
xts_uint128_cpu_to_les(I);
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2018-10-08 16:13:28 +03:00
|
|
|
* xts_tweak_encdec:
|
2016-02-11 17:00:17 +03:00
|
|
|
* @param ctxt: the cipher context
|
|
|
|
* @param func: the cipher function
|
2018-10-08 16:13:28 +03:00
|
|
|
* @src: buffer providing the input text of XTS_BLOCK_SIZE bytes
|
|
|
|
* @dst: buffer to output the output text of XTS_BLOCK_SIZE bytes
|
2016-02-11 17:00:17 +03:00
|
|
|
* @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
|
|
|
|
*
|
2018-10-08 16:13:28 +03:00
|
|
|
* Encrypt/decrypt data with a tweak
|
2016-02-11 17:00:17 +03:00
|
|
|
*/
|
2018-10-09 12:50:43 +03:00
|
|
|
static inline void xts_tweak_encdec(const void *ctx,
|
|
|
|
xts_cipher_func *func,
|
|
|
|
const xts_uint128 *src,
|
|
|
|
xts_uint128 *dst,
|
|
|
|
xts_uint128 *iv)
|
2016-02-11 17:00:17 +03:00
|
|
|
{
|
|
|
|
/* tweak encrypt block i */
|
2018-10-09 12:50:43 +03:00
|
|
|
xts_uint128_xor(dst, src, iv);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
2018-10-09 12:50:43 +03:00
|
|
|
func(ctx, XTS_BLOCK_SIZE, dst->b, dst->b);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
2018-10-09 12:50:43 +03:00
|
|
|
xts_uint128_xor(dst, dst, iv);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
|
|
|
/* LFSR the tweak */
|
2018-10-09 12:55:14 +03:00
|
|
|
xts_mult_x(iv);
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void xts_decrypt(const void *datactx,
|
|
|
|
const void *tweakctx,
|
|
|
|
xts_cipher_func *encfunc,
|
|
|
|
xts_cipher_func *decfunc,
|
|
|
|
uint8_t *iv,
|
|
|
|
size_t length,
|
|
|
|
uint8_t *dst,
|
|
|
|
const uint8_t *src)
|
|
|
|
{
|
2018-10-09 12:45:41 +03:00
|
|
|
xts_uint128 PP, CC, T;
|
2016-02-11 17:00:17 +03:00
|
|
|
unsigned long i, m, mo, lim;
|
|
|
|
|
|
|
|
/* get number of blocks */
|
|
|
|
m = length >> 4;
|
|
|
|
mo = length & 15;
|
|
|
|
|
|
|
|
/* must have at least one full block */
|
|
|
|
g_assert(m != 0);
|
|
|
|
|
|
|
|
if (mo == 0) {
|
|
|
|
lim = m;
|
|
|
|
} else {
|
|
|
|
lim = m - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* encrypt the iv */
|
2018-10-09 12:45:41 +03:00
|
|
|
encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
2018-10-09 12:50:43 +03:00
|
|
|
if (QEMU_PTR_IS_ALIGNED(src, sizeof(uint64_t)) &&
|
|
|
|
QEMU_PTR_IS_ALIGNED(dst, sizeof(uint64_t))) {
|
|
|
|
xts_uint128 *S = (xts_uint128 *)src;
|
|
|
|
xts_uint128 *D = (xts_uint128 *)dst;
|
|
|
|
for (i = 0; i < lim; i++, S++, D++) {
|
|
|
|
xts_tweak_encdec(datactx, decfunc, S, D, &T);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xts_uint128 D;
|
|
|
|
|
|
|
|
for (i = 0; i < lim; i++) {
|
|
|
|
memcpy(&D, src, XTS_BLOCK_SIZE);
|
|
|
|
xts_tweak_encdec(datactx, decfunc, &D, &D, &T);
|
|
|
|
memcpy(dst, &D, XTS_BLOCK_SIZE);
|
|
|
|
src += XTS_BLOCK_SIZE;
|
|
|
|
dst += XTS_BLOCK_SIZE;
|
|
|
|
}
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if length is not a multiple of XTS_BLOCK_SIZE then */
|
|
|
|
if (mo > 0) {
|
2018-10-09 12:50:43 +03:00
|
|
|
xts_uint128 S, D;
|
2018-10-09 12:45:41 +03:00
|
|
|
memcpy(&CC, &T, XTS_BLOCK_SIZE);
|
2018-10-09 12:55:14 +03:00
|
|
|
xts_mult_x(&CC);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
|
|
|
/* PP = tweak decrypt block m-1 */
|
2018-10-09 12:50:43 +03:00
|
|
|
memcpy(&S, src, XTS_BLOCK_SIZE);
|
|
|
|
xts_tweak_encdec(datactx, decfunc, &S, &PP, &CC);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
|
|
|
/* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
|
|
|
|
for (i = 0; i < mo; i++) {
|
2018-10-09 12:45:41 +03:00
|
|
|
CC.b[i] = src[XTS_BLOCK_SIZE + i];
|
|
|
|
dst[XTS_BLOCK_SIZE + i] = PP.b[i];
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
for (; i < XTS_BLOCK_SIZE; i++) {
|
2018-10-09 12:45:41 +03:00
|
|
|
CC.b[i] = PP.b[i];
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Pm-1 = Tweak uncrypt CC */
|
2018-10-09 12:50:43 +03:00
|
|
|
xts_tweak_encdec(datactx, decfunc, &CC, &D, &T);
|
|
|
|
memcpy(dst, &D, XTS_BLOCK_SIZE);
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt the iv back */
|
2018-10-09 12:45:41 +03:00
|
|
|
decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void xts_encrypt(const void *datactx,
|
|
|
|
const void *tweakctx,
|
|
|
|
xts_cipher_func *encfunc,
|
|
|
|
xts_cipher_func *decfunc,
|
|
|
|
uint8_t *iv,
|
|
|
|
size_t length,
|
|
|
|
uint8_t *dst,
|
|
|
|
const uint8_t *src)
|
|
|
|
{
|
2018-10-09 12:45:41 +03:00
|
|
|
xts_uint128 PP, CC, T;
|
2016-02-11 17:00:17 +03:00
|
|
|
unsigned long i, m, mo, lim;
|
|
|
|
|
|
|
|
/* get number of blocks */
|
|
|
|
m = length >> 4;
|
|
|
|
mo = length & 15;
|
|
|
|
|
|
|
|
/* must have at least one full block */
|
|
|
|
g_assert(m != 0);
|
|
|
|
|
|
|
|
if (mo == 0) {
|
|
|
|
lim = m;
|
|
|
|
} else {
|
|
|
|
lim = m - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* encrypt the iv */
|
2018-10-09 12:45:41 +03:00
|
|
|
encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
2018-10-09 12:50:43 +03:00
|
|
|
if (QEMU_PTR_IS_ALIGNED(src, sizeof(uint64_t)) &&
|
|
|
|
QEMU_PTR_IS_ALIGNED(dst, sizeof(uint64_t))) {
|
|
|
|
xts_uint128 *S = (xts_uint128 *)src;
|
|
|
|
xts_uint128 *D = (xts_uint128 *)dst;
|
|
|
|
for (i = 0; i < lim; i++, S++, D++) {
|
|
|
|
xts_tweak_encdec(datactx, encfunc, S, D, &T);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
xts_uint128 D;
|
|
|
|
|
|
|
|
for (i = 0; i < lim; i++) {
|
|
|
|
memcpy(&D, src, XTS_BLOCK_SIZE);
|
|
|
|
xts_tweak_encdec(datactx, encfunc, &D, &D, &T);
|
|
|
|
memcpy(dst, &D, XTS_BLOCK_SIZE);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
2018-10-09 12:50:43 +03:00
|
|
|
dst += XTS_BLOCK_SIZE;
|
|
|
|
src += XTS_BLOCK_SIZE;
|
|
|
|
}
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* if length is not a multiple of XTS_BLOCK_SIZE then */
|
|
|
|
if (mo > 0) {
|
2018-10-09 12:50:43 +03:00
|
|
|
xts_uint128 S, D;
|
2016-02-11 17:00:17 +03:00
|
|
|
/* CC = tweak encrypt block m-1 */
|
2018-10-09 12:50:43 +03:00
|
|
|
memcpy(&S, src, XTS_BLOCK_SIZE);
|
|
|
|
xts_tweak_encdec(datactx, encfunc, &S, &CC, &T);
|
2016-02-11 17:00:17 +03:00
|
|
|
|
|
|
|
/* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
|
|
|
|
for (i = 0; i < mo; i++) {
|
2018-10-09 12:45:41 +03:00
|
|
|
PP.b[i] = src[XTS_BLOCK_SIZE + i];
|
|
|
|
dst[XTS_BLOCK_SIZE + i] = CC.b[i];
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (; i < XTS_BLOCK_SIZE; i++) {
|
2018-10-09 12:45:41 +03:00
|
|
|
PP.b[i] = CC.b[i];
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cm-1 = Tweak encrypt PP */
|
2018-10-09 12:50:43 +03:00
|
|
|
xts_tweak_encdec(datactx, encfunc, &PP, &D, &T);
|
|
|
|
memcpy(dst, &D, XTS_BLOCK_SIZE);
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Decrypt the iv back */
|
2018-10-09 12:45:41 +03:00
|
|
|
decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
|
2016-02-11 17:00:17 +03:00
|
|
|
}
|