2 * QEMU Crypto XTS cipher mode
4 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19 * This code is originally derived from public domain / WTFPL code in
20 * LibTomCrypt crytographic library http://libtom.org. The XTS code
21 * was donated by Elliptic Semiconductor Inc (www.ellipticsemi.com)
22 * to the LibTom Projects
26 #include "qemu/osdep.h"
27 #include "crypto/xts.h"
29 static void xts_mult_x(uint8_t *I
)
34 for (x
= t
= 0; x
< 16; x
++) {
36 I
[x
] = ((I
[x
] << 1) | t
) & 0xFF;
47 * @param ctxt: the cipher context
48 * @param func: the cipher function
49 * @src: buffer providing the cipher text of XTS_BLOCK_SIZE bytes
50 * @dst: buffer to output the plain text of XTS_BLOCK_SIZE bytes
51 * @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
53 * Decrypt data with a tweak
55 static void xts_tweak_decrypt(const void *ctx
,
56 xts_cipher_func
*func
,
63 /* tweak encrypt block i */
64 for (x
= 0; x
< XTS_BLOCK_SIZE
; x
++) {
65 dst
[x
] = src
[x
] ^ iv
[x
];
68 func(ctx
, XTS_BLOCK_SIZE
, dst
, dst
);
70 for (x
= 0; x
< XTS_BLOCK_SIZE
; x
++) {
71 dst
[x
] = dst
[x
] ^ iv
[x
];
79 void xts_decrypt(const void *datactx
,
81 xts_cipher_func
*encfunc
,
82 xts_cipher_func
*decfunc
,
88 uint8_t PP
[XTS_BLOCK_SIZE
], CC
[XTS_BLOCK_SIZE
], T
[XTS_BLOCK_SIZE
];
89 unsigned long i
, m
, mo
, lim
;
91 /* get number of blocks */
95 /* must have at least one full block */
105 encfunc(tweakctx
, XTS_BLOCK_SIZE
, T
, iv
);
107 for (i
= 0; i
< lim
; i
++) {
108 xts_tweak_decrypt(datactx
, decfunc
, src
, dst
, T
);
110 src
+= XTS_BLOCK_SIZE
;
111 dst
+= XTS_BLOCK_SIZE
;
114 /* if length is not a multiple of XTS_BLOCK_SIZE then */
116 memcpy(CC
, T
, XTS_BLOCK_SIZE
);
119 /* PP = tweak decrypt block m-1 */
120 xts_tweak_decrypt(datactx
, decfunc
, src
, PP
, CC
);
122 /* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
123 for (i
= 0; i
< mo
; i
++) {
124 CC
[i
] = src
[XTS_BLOCK_SIZE
+ i
];
125 dst
[XTS_BLOCK_SIZE
+ i
] = PP
[i
];
127 for (; i
< XTS_BLOCK_SIZE
; i
++) {
131 /* Pm-1 = Tweak uncrypt CC */
132 xts_tweak_decrypt(datactx
, decfunc
, CC
, dst
, T
);
135 /* Decrypt the iv back */
136 decfunc(tweakctx
, XTS_BLOCK_SIZE
, iv
, T
);
142 * @param ctxt: the cipher context
143 * @param func: the cipher function
144 * @src: buffer providing the plain text of XTS_BLOCK_SIZE bytes
145 * @dst: buffer to output the cipher text of XTS_BLOCK_SIZE bytes
146 * @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
148 * Encrypt data with a tweak
150 static void xts_tweak_encrypt(const void *ctx
,
151 xts_cipher_func
*func
,
158 /* tweak encrypt block i */
159 for (x
= 0; x
< XTS_BLOCK_SIZE
; x
++) {
160 dst
[x
] = src
[x
] ^ iv
[x
];
163 func(ctx
, XTS_BLOCK_SIZE
, dst
, dst
);
165 for (x
= 0; x
< XTS_BLOCK_SIZE
; x
++) {
166 dst
[x
] = dst
[x
] ^ iv
[x
];
174 void xts_encrypt(const void *datactx
,
175 const void *tweakctx
,
176 xts_cipher_func
*encfunc
,
177 xts_cipher_func
*decfunc
,
183 uint8_t PP
[XTS_BLOCK_SIZE
], CC
[XTS_BLOCK_SIZE
], T
[XTS_BLOCK_SIZE
];
184 unsigned long i
, m
, mo
, lim
;
186 /* get number of blocks */
190 /* must have at least one full block */
200 encfunc(tweakctx
, XTS_BLOCK_SIZE
, T
, iv
);
202 for (i
= 0; i
< lim
; i
++) {
203 xts_tweak_encrypt(datactx
, encfunc
, src
, dst
, T
);
205 dst
+= XTS_BLOCK_SIZE
;
206 src
+= XTS_BLOCK_SIZE
;
209 /* if length is not a multiple of XTS_BLOCK_SIZE then */
211 /* CC = tweak encrypt block m-1 */
212 xts_tweak_encrypt(datactx
, encfunc
, src
, CC
, T
);
214 /* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
215 for (i
= 0; i
< mo
; i
++) {
216 PP
[i
] = src
[XTS_BLOCK_SIZE
+ i
];
217 dst
[XTS_BLOCK_SIZE
+ i
] = CC
[i
];
220 for (; i
< XTS_BLOCK_SIZE
; i
++) {
224 /* Cm-1 = Tweak encrypt PP */
225 xts_tweak_encrypt(datactx
, encfunc
, PP
, dst
, T
);
228 /* Decrypt the iv back */
229 decfunc(tweakctx
, XTS_BLOCK_SIZE
, iv
, T
);