xts.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * QEMU Crypto XTS cipher mode
  3. *
  4. * Copyright (c) 2015-2016 Red Hat, Inc.
  5. *
  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.1 of the License, or (at your option) any later version.
  10. *
  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.
  15. *
  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/>.
  18. *
  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
  23. *
  24. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/bswap.h"
  27. #include "crypto/xts.h"
  28. typedef union {
  29. uint8_t b[XTS_BLOCK_SIZE];
  30. uint64_t u[2];
  31. } xts_uint128;
  32. static inline void xts_uint128_xor(xts_uint128 *D,
  33. const xts_uint128 *S1,
  34. const xts_uint128 *S2)
  35. {
  36. D->u[0] = S1->u[0] ^ S2->u[0];
  37. D->u[1] = S1->u[1] ^ S2->u[1];
  38. }
  39. static inline void xts_uint128_cpu_to_les(xts_uint128 *v)
  40. {
  41. cpu_to_le64s(&v->u[0]);
  42. cpu_to_le64s(&v->u[1]);
  43. }
  44. static inline void xts_uint128_le_to_cpus(xts_uint128 *v)
  45. {
  46. le64_to_cpus(&v->u[0]);
  47. le64_to_cpus(&v->u[1]);
  48. }
  49. static void xts_mult_x(xts_uint128 *I)
  50. {
  51. uint64_t tt;
  52. xts_uint128_le_to_cpus(I);
  53. tt = I->u[0] >> 63;
  54. I->u[0] <<= 1;
  55. if (I->u[1] >> 63) {
  56. I->u[0] ^= 0x87;
  57. }
  58. I->u[1] <<= 1;
  59. I->u[1] |= tt;
  60. xts_uint128_cpu_to_les(I);
  61. }
  62. /**
  63. * xts_tweak_encdec:
  64. * @param ctxt: the cipher context
  65. * @param func: the cipher function
  66. * @src: buffer providing the input text of XTS_BLOCK_SIZE bytes
  67. * @dst: buffer to output the output text of XTS_BLOCK_SIZE bytes
  68. * @iv: the initialization vector tweak of XTS_BLOCK_SIZE bytes
  69. *
  70. * Encrypt/decrypt data with a tweak
  71. */
  72. static inline void xts_tweak_encdec(const void *ctx,
  73. xts_cipher_func *func,
  74. const xts_uint128 *src,
  75. xts_uint128 *dst,
  76. xts_uint128 *iv)
  77. {
  78. /* tweak encrypt block i */
  79. xts_uint128_xor(dst, src, iv);
  80. func(ctx, XTS_BLOCK_SIZE, dst->b, dst->b);
  81. xts_uint128_xor(dst, dst, iv);
  82. /* LFSR the tweak */
  83. xts_mult_x(iv);
  84. }
  85. void xts_decrypt(const void *datactx,
  86. const void *tweakctx,
  87. xts_cipher_func *encfunc,
  88. xts_cipher_func *decfunc,
  89. uint8_t *iv,
  90. size_t length,
  91. uint8_t *dst,
  92. const uint8_t *src)
  93. {
  94. xts_uint128 PP, CC, T;
  95. unsigned long i, m, mo, lim;
  96. /* get number of blocks */
  97. m = length >> 4;
  98. mo = length & 15;
  99. /* must have at least one full block */
  100. g_assert(m != 0);
  101. if (mo == 0) {
  102. lim = m;
  103. } else {
  104. lim = m - 1;
  105. }
  106. /* encrypt the iv */
  107. encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
  108. if (QEMU_PTR_IS_ALIGNED(src, sizeof(uint64_t)) &&
  109. QEMU_PTR_IS_ALIGNED(dst, sizeof(uint64_t))) {
  110. xts_uint128 *S = (xts_uint128 *)src;
  111. xts_uint128 *D = (xts_uint128 *)dst;
  112. for (i = 0; i < lim; i++, S++, D++) {
  113. xts_tweak_encdec(datactx, decfunc, S, D, &T);
  114. }
  115. } else {
  116. xts_uint128 D;
  117. for (i = 0; i < lim; i++) {
  118. memcpy(&D, src, XTS_BLOCK_SIZE);
  119. xts_tweak_encdec(datactx, decfunc, &D, &D, &T);
  120. memcpy(dst, &D, XTS_BLOCK_SIZE);
  121. src += XTS_BLOCK_SIZE;
  122. dst += XTS_BLOCK_SIZE;
  123. }
  124. }
  125. /* if length is not a multiple of XTS_BLOCK_SIZE then */
  126. if (mo > 0) {
  127. xts_uint128 S, D;
  128. memcpy(&CC, &T, XTS_BLOCK_SIZE);
  129. xts_mult_x(&CC);
  130. /* PP = tweak decrypt block m-1 */
  131. memcpy(&S, src, XTS_BLOCK_SIZE);
  132. xts_tweak_encdec(datactx, decfunc, &S, &PP, &CC);
  133. /* Pm = first length % XTS_BLOCK_SIZE bytes of PP */
  134. for (i = 0; i < mo; i++) {
  135. CC.b[i] = src[XTS_BLOCK_SIZE + i];
  136. dst[XTS_BLOCK_SIZE + i] = PP.b[i];
  137. }
  138. for (; i < XTS_BLOCK_SIZE; i++) {
  139. CC.b[i] = PP.b[i];
  140. }
  141. /* Pm-1 = Tweak uncrypt CC */
  142. xts_tweak_encdec(datactx, decfunc, &CC, &D, &T);
  143. memcpy(dst, &D, XTS_BLOCK_SIZE);
  144. }
  145. /* Decrypt the iv back */
  146. decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
  147. }
  148. void xts_encrypt(const void *datactx,
  149. const void *tweakctx,
  150. xts_cipher_func *encfunc,
  151. xts_cipher_func *decfunc,
  152. uint8_t *iv,
  153. size_t length,
  154. uint8_t *dst,
  155. const uint8_t *src)
  156. {
  157. xts_uint128 PP, CC, T;
  158. unsigned long i, m, mo, lim;
  159. /* get number of blocks */
  160. m = length >> 4;
  161. mo = length & 15;
  162. /* must have at least one full block */
  163. g_assert(m != 0);
  164. if (mo == 0) {
  165. lim = m;
  166. } else {
  167. lim = m - 1;
  168. }
  169. /* encrypt the iv */
  170. encfunc(tweakctx, XTS_BLOCK_SIZE, T.b, iv);
  171. if (QEMU_PTR_IS_ALIGNED(src, sizeof(uint64_t)) &&
  172. QEMU_PTR_IS_ALIGNED(dst, sizeof(uint64_t))) {
  173. xts_uint128 *S = (xts_uint128 *)src;
  174. xts_uint128 *D = (xts_uint128 *)dst;
  175. for (i = 0; i < lim; i++, S++, D++) {
  176. xts_tweak_encdec(datactx, encfunc, S, D, &T);
  177. }
  178. } else {
  179. xts_uint128 D;
  180. for (i = 0; i < lim; i++) {
  181. memcpy(&D, src, XTS_BLOCK_SIZE);
  182. xts_tweak_encdec(datactx, encfunc, &D, &D, &T);
  183. memcpy(dst, &D, XTS_BLOCK_SIZE);
  184. dst += XTS_BLOCK_SIZE;
  185. src += XTS_BLOCK_SIZE;
  186. }
  187. }
  188. /* if length is not a multiple of XTS_BLOCK_SIZE then */
  189. if (mo > 0) {
  190. xts_uint128 S, D;
  191. /* CC = tweak encrypt block m-1 */
  192. memcpy(&S, src, XTS_BLOCK_SIZE);
  193. xts_tweak_encdec(datactx, encfunc, &S, &CC, &T);
  194. /* Cm = first length % XTS_BLOCK_SIZE bytes of CC */
  195. for (i = 0; i < mo; i++) {
  196. PP.b[i] = src[XTS_BLOCK_SIZE + i];
  197. dst[XTS_BLOCK_SIZE + i] = CC.b[i];
  198. }
  199. for (; i < XTS_BLOCK_SIZE; i++) {
  200. PP.b[i] = CC.b[i];
  201. }
  202. /* Cm-1 = Tweak encrypt PP */
  203. xts_tweak_encdec(datactx, encfunc, &PP, &D, &T);
  204. memcpy(dst, &D, XTS_BLOCK_SIZE);
  205. }
  206. /* Decrypt the iv back */
  207. decfunc(tweakctx, XTS_BLOCK_SIZE, iv, T.b);
  208. }