You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

259 regels
7.9 KiB

  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. #include <openssl/evp.h>
  49. #include <string.h>
  50. #include <openssl/aes.h>
  51. #include <openssl/cipher.h>
  52. #include "../crypto/fipsmodule/modes/internal.h"
  53. typedef struct xts128_context {
  54. AES_KEY *key1, *key2;
  55. block128_f block1, block2;
  56. } XTS128_CONTEXT;
  57. static size_t CRYPTO_xts128_encrypt(const XTS128_CONTEXT *ctx,
  58. const uint8_t iv[16], const uint8_t *inp,
  59. uint8_t *out, size_t len, int enc) {
  60. union {
  61. uint64_t u[2];
  62. uint32_t d[4];
  63. uint8_t c[16];
  64. } tweak, scratch;
  65. unsigned int i;
  66. if (len < 16) return 0;
  67. OPENSSL_memcpy(tweak.c, iv, 16);
  68. (*ctx->block2)(tweak.c, tweak.c, ctx->key2);
  69. if (!enc && (len % 16)) len -= 16;
  70. while (len >= 16) {
  71. #if STRICT_ALIGNMENT
  72. OPENSSL_memcpy(scratch.c, inp, 16);
  73. scratch.u[0] ^= tweak.u[0];
  74. scratch.u[1] ^= tweak.u[1];
  75. #else
  76. scratch.u[0] = ((uint64_t *)inp)[0] ^ tweak.u[0];
  77. scratch.u[1] = ((uint64_t *)inp)[1] ^ tweak.u[1];
  78. #endif
  79. (*ctx->block1)(scratch.c, scratch.c, ctx->key1);
  80. #if STRICT_ALIGNMENT
  81. scratch.u[0] ^= tweak.u[0];
  82. scratch.u[1] ^= tweak.u[1];
  83. OPENSSL_memcpy(out, scratch.c, 16);
  84. #else
  85. ((uint64_t *)out)[0] = scratch.u[0] ^= tweak.u[0];
  86. ((uint64_t *)out)[1] = scratch.u[1] ^= tweak.u[1];
  87. #endif
  88. inp += 16;
  89. out += 16;
  90. len -= 16;
  91. if (len == 0) return 1;
  92. unsigned int carry, res;
  93. res = 0x87 & (((int)tweak.d[3]) >> 31);
  94. carry = (unsigned int)(tweak.u[0] >> 63);
  95. tweak.u[0] = (tweak.u[0] << 1) ^ res;
  96. tweak.u[1] = (tweak.u[1] << 1) | carry;
  97. }
  98. if (enc) {
  99. for (i = 0; i < len; ++i) {
  100. uint8_t c = inp[i];
  101. out[i] = scratch.c[i];
  102. scratch.c[i] = c;
  103. }
  104. scratch.u[0] ^= tweak.u[0];
  105. scratch.u[1] ^= tweak.u[1];
  106. (*ctx->block1)(scratch.c, scratch.c, ctx->key1);
  107. scratch.u[0] ^= tweak.u[0];
  108. scratch.u[1] ^= tweak.u[1];
  109. OPENSSL_memcpy(out - 16, scratch.c, 16);
  110. } else {
  111. union {
  112. uint64_t u[2];
  113. uint8_t c[16];
  114. } tweak1;
  115. unsigned int carry, res;
  116. res = 0x87 & (((int)tweak.d[3]) >> 31);
  117. carry = (unsigned int)(tweak.u[0] >> 63);
  118. tweak1.u[0] = (tweak.u[0] << 1) ^ res;
  119. tweak1.u[1] = (tweak.u[1] << 1) | carry;
  120. #if STRICT_ALIGNMENT
  121. OPENSSL_memcpy(scratch.c, inp, 16);
  122. scratch.u[0] ^= tweak1.u[0];
  123. scratch.u[1] ^= tweak1.u[1];
  124. #else
  125. scratch.u[0] = ((uint64_t *)inp)[0] ^ tweak1.u[0];
  126. scratch.u[1] = ((uint64_t *)inp)[1] ^ tweak1.u[1];
  127. #endif
  128. (*ctx->block1)(scratch.c, scratch.c, ctx->key1);
  129. scratch.u[0] ^= tweak1.u[0];
  130. scratch.u[1] ^= tweak1.u[1];
  131. for (i = 0; i < len; ++i) {
  132. uint8_t c = inp[16 + i];
  133. out[16 + i] = scratch.c[i];
  134. scratch.c[i] = c;
  135. }
  136. scratch.u[0] ^= tweak.u[0];
  137. scratch.u[1] ^= tweak.u[1];
  138. (*ctx->block1)(scratch.c, scratch.c, ctx->key1);
  139. #if STRICT_ALIGNMENT
  140. scratch.u[0] ^= tweak.u[0];
  141. scratch.u[1] ^= tweak.u[1];
  142. OPENSSL_memcpy(out, scratch.c, 16);
  143. #else
  144. ((uint64_t *)out)[0] = scratch.u[0] ^ tweak.u[0];
  145. ((uint64_t *)out)[1] = scratch.u[1] ^ tweak.u[1];
  146. #endif
  147. }
  148. return 1;
  149. }
  150. typedef struct {
  151. union {
  152. double align;
  153. AES_KEY ks;
  154. } ks1, ks2; // AES key schedules to use
  155. XTS128_CONTEXT xts;
  156. } EVP_AES_XTS_CTX;
  157. static int aes_xts_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key,
  158. const uint8_t *iv, int enc) {
  159. EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
  160. if (!iv && !key) {
  161. return 1;
  162. }
  163. if (key) {
  164. // key_len is two AES keys
  165. if (enc) {
  166. AES_set_encrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
  167. xctx->xts.block1 = AES_encrypt;
  168. } else {
  169. AES_set_decrypt_key(key, ctx->key_len * 4, &xctx->ks1.ks);
  170. xctx->xts.block1 = AES_decrypt;
  171. }
  172. AES_set_encrypt_key(key + ctx->key_len / 2,
  173. ctx->key_len * 4, &xctx->ks2.ks);
  174. xctx->xts.block2 = AES_encrypt;
  175. xctx->xts.key1 = &xctx->ks1.ks;
  176. }
  177. if (iv) {
  178. xctx->xts.key2 = &xctx->ks2.ks;
  179. OPENSSL_memcpy(ctx->iv, iv, 16);
  180. }
  181. return 1;
  182. }
  183. static int aes_xts_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out,
  184. const uint8_t *in, size_t len) {
  185. EVP_AES_XTS_CTX *xctx = ctx->cipher_data;
  186. if (!xctx->xts.key1 ||
  187. !xctx->xts.key2 ||
  188. !out ||
  189. !in ||
  190. len < AES_BLOCK_SIZE ||
  191. !CRYPTO_xts128_encrypt(&xctx->xts, ctx->iv, in, out, len, ctx->encrypt)) {
  192. return 0;
  193. }
  194. return 1;
  195. }
  196. static int aes_xts_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr) {
  197. EVP_AES_XTS_CTX *xctx = c->cipher_data;
  198. if (type == EVP_CTRL_COPY) {
  199. EVP_CIPHER_CTX *out = ptr;
  200. EVP_AES_XTS_CTX *xctx_out = out->cipher_data;
  201. if (xctx->xts.key1) {
  202. if (xctx->xts.key1 != &xctx->ks1.ks) {
  203. return 0;
  204. }
  205. xctx_out->xts.key1 = &xctx_out->ks1.ks;
  206. }
  207. if (xctx->xts.key2) {
  208. if (xctx->xts.key2 != &xctx->ks2.ks) {
  209. return 0;
  210. }
  211. xctx_out->xts.key2 = &xctx_out->ks2.ks;
  212. }
  213. return 1;
  214. } else if (type != EVP_CTRL_INIT) {
  215. return -1;
  216. }
  217. // key1 and key2 are used as an indicator both key and IV are set
  218. xctx->xts.key1 = NULL;
  219. xctx->xts.key2 = NULL;
  220. return 1;
  221. }
  222. static const EVP_CIPHER aes_256_xts = {
  223. NID_aes_256_xts, 1 /* block_size */, 64 /* key_size (2 AES keys) */,
  224. 16 /* iv_len */, sizeof(EVP_AES_XTS_CTX),
  225. EVP_CIPH_XTS_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_ALWAYS_CALL_INIT |
  226. EVP_CIPH_CTRL_INIT | EVP_CIPH_CUSTOM_COPY,
  227. NULL /* app_data */, aes_xts_init_key, aes_xts_cipher,
  228. NULL /* cleanup */, aes_xts_ctrl};
  229. const EVP_CIPHER *EVP_aes_256_xts(void) { return &aes_256_xts; }