xts128.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /* ====================================================================
  2. * Copyright (c) 2011 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. * ====================================================================
  48. */
  49. #include <openssl/crypto.h>
  50. #include "modes_lcl.h"
  51. #include <string.h>
  52. #ifndef MODES_DEBUG
  53. # ifndef NDEBUG
  54. # define NDEBUG
  55. # endif
  56. #endif
  57. #include <assert.h>
  58. int CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx,
  59. const unsigned char iv[16],
  60. const unsigned char *inp, unsigned char *out,
  61. size_t len, int enc)
  62. {
  63. const union {
  64. long one;
  65. char little;
  66. } is_endian = {
  67. 1
  68. };
  69. union {
  70. u64 u[2];
  71. u32 d[4];
  72. u8 c[16];
  73. } tweak, scratch;
  74. unsigned int i;
  75. if (len < 16)
  76. return -1;
  77. memcpy(tweak.c, iv, 16);
  78. (*ctx->block2) (tweak.c, tweak.c, ctx->key2);
  79. if (!enc && (len % 16))
  80. len -= 16;
  81. while (len >= 16) {
  82. #if defined(STRICT_ALIGNMENT)
  83. memcpy(scratch.c, inp, 16);
  84. scratch.u[0] ^= tweak.u[0];
  85. scratch.u[1] ^= tweak.u[1];
  86. #else
  87. scratch.u[0] = ((u64 *)inp)[0] ^ tweak.u[0];
  88. scratch.u[1] = ((u64 *)inp)[1] ^ tweak.u[1];
  89. #endif
  90. (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
  91. #if defined(STRICT_ALIGNMENT)
  92. scratch.u[0] ^= tweak.u[0];
  93. scratch.u[1] ^= tweak.u[1];
  94. memcpy(out, scratch.c, 16);
  95. #else
  96. ((u64 *)out)[0] = scratch.u[0] ^= tweak.u[0];
  97. ((u64 *)out)[1] = scratch.u[1] ^= tweak.u[1];
  98. #endif
  99. inp += 16;
  100. out += 16;
  101. len -= 16;
  102. if (len == 0)
  103. return 0;
  104. if (is_endian.little) {
  105. unsigned int carry, res;
  106. res = 0x87 & (((int)tweak.d[3]) >> 31);
  107. carry = (unsigned int)(tweak.u[0] >> 63);
  108. tweak.u[0] = (tweak.u[0] << 1) ^ res;
  109. tweak.u[1] = (tweak.u[1] << 1) | carry;
  110. } else {
  111. size_t c;
  112. for (c = 0, i = 0; i < 16; ++i) {
  113. /*
  114. * + substitutes for |, because c is 1 bit
  115. */
  116. c += ((size_t)tweak.c[i]) << 1;
  117. tweak.c[i] = (u8)c;
  118. c = c >> 8;
  119. }
  120. tweak.c[0] ^= (u8)(0x87 & (0 - c));
  121. }
  122. }
  123. if (enc) {
  124. for (i = 0; i < len; ++i) {
  125. u8 c = inp[i];
  126. out[i] = scratch.c[i];
  127. scratch.c[i] = c;
  128. }
  129. scratch.u[0] ^= tweak.u[0];
  130. scratch.u[1] ^= tweak.u[1];
  131. (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
  132. scratch.u[0] ^= tweak.u[0];
  133. scratch.u[1] ^= tweak.u[1];
  134. memcpy(out - 16, scratch.c, 16);
  135. } else {
  136. union {
  137. u64 u[2];
  138. u8 c[16];
  139. } tweak1;
  140. if (is_endian.little) {
  141. unsigned int carry, res;
  142. res = 0x87 & (((int)tweak.d[3]) >> 31);
  143. carry = (unsigned int)(tweak.u[0] >> 63);
  144. tweak1.u[0] = (tweak.u[0] << 1) ^ res;
  145. tweak1.u[1] = (tweak.u[1] << 1) | carry;
  146. } else {
  147. size_t c;
  148. for (c = 0, i = 0; i < 16; ++i) {
  149. /*
  150. * + substitutes for |, because c is 1 bit
  151. */
  152. c += ((size_t)tweak.c[i]) << 1;
  153. tweak1.c[i] = (u8)c;
  154. c = c >> 8;
  155. }
  156. tweak1.c[0] ^= (u8)(0x87 & (0 - c));
  157. }
  158. #if defined(STRICT_ALIGNMENT)
  159. memcpy(scratch.c, inp, 16);
  160. scratch.u[0] ^= tweak1.u[0];
  161. scratch.u[1] ^= tweak1.u[1];
  162. #else
  163. scratch.u[0] = ((u64 *)inp)[0] ^ tweak1.u[0];
  164. scratch.u[1] = ((u64 *)inp)[1] ^ tweak1.u[1];
  165. #endif
  166. (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
  167. scratch.u[0] ^= tweak1.u[0];
  168. scratch.u[1] ^= tweak1.u[1];
  169. for (i = 0; i < len; ++i) {
  170. u8 c = inp[16 + i];
  171. out[16 + i] = scratch.c[i];
  172. scratch.c[i] = c;
  173. }
  174. scratch.u[0] ^= tweak.u[0];
  175. scratch.u[1] ^= tweak.u[1];
  176. (*ctx->block1) (scratch.c, scratch.c, ctx->key1);
  177. #if defined(STRICT_ALIGNMENT)
  178. scratch.u[0] ^= tweak.u[0];
  179. scratch.u[1] ^= tweak.u[1];
  180. memcpy(out, scratch.c, 16);
  181. #else
  182. ((u64 *)out)[0] = scratch.u[0] ^ tweak.u[0];
  183. ((u64 *)out)[1] = scratch.u[1] ^ tweak.u[1];
  184. #endif
  185. }
  186. return 0;
  187. }