cipher_aes_xts.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "cipher_aes_xts.h"
  10. #include "prov/implementations.h"
  11. #include "prov/providercommonerr.h"
  12. /* TODO (3.0) Figure out what flags need to be set */
  13. #define AES_XTS_FLAGS (EVP_CIPH_CUSTOM_IV \
  14. | EVP_CIPH_ALWAYS_CALL_INIT \
  15. | EVP_CIPH_CTRL_INIT \
  16. | EVP_CIPH_CUSTOM_COPY)
  17. #define AES_XTS_IV_BITS 128
  18. #define AES_XTS_BLOCK_BITS 8
  19. /* forward declarations */
  20. static OSSL_OP_cipher_encrypt_init_fn aes_xts_einit;
  21. static OSSL_OP_cipher_decrypt_init_fn aes_xts_dinit;
  22. static OSSL_OP_cipher_update_fn aes_xts_stream_update;
  23. static OSSL_OP_cipher_final_fn aes_xts_stream_final;
  24. static OSSL_OP_cipher_cipher_fn aes_xts_cipher;
  25. static OSSL_OP_cipher_freectx_fn aes_xts_freectx;
  26. static OSSL_OP_cipher_dupctx_fn aes_xts_dupctx;
  27. static OSSL_OP_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
  28. static OSSL_OP_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
  29. /*
  30. * Verify that the two keys are different.
  31. *
  32. * This addresses the vulnerability described in Rogaway's
  33. * September 2004 paper:
  34. *
  35. * "Efficient Instantiations of Tweakable Blockciphers and
  36. * Refinements to Modes OCB and PMAC".
  37. * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
  38. *
  39. * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
  40. * that:
  41. * "The check for Key_1 != Key_2 shall be done at any place
  42. * BEFORE using the keys in the XTS-AES algorithm to process
  43. * data with them."
  44. */
  45. static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
  46. int enc)
  47. {
  48. if ((!allow_insecure_decrypt || enc)
  49. && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
  50. ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
  51. return 0;
  52. }
  53. return 1;
  54. }
  55. /*-
  56. * Provider dispatch functions
  57. */
  58. static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
  59. const unsigned char *iv, size_t ivlen, int enc)
  60. {
  61. PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
  62. PROV_CIPHER_CTX *ctx = &xctx->base;
  63. ctx->enc = enc;
  64. if (iv != NULL) {
  65. if (!cipher_generic_initiv(vctx, iv, ivlen))
  66. return 0;
  67. }
  68. if (key != NULL) {
  69. if (keylen != ctx->keylen) {
  70. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  71. return 0;
  72. }
  73. if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
  74. return 0;
  75. return ctx->hw->init(ctx, key, keylen);
  76. }
  77. return 1;
  78. }
  79. static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
  80. const unsigned char *iv, size_t ivlen)
  81. {
  82. return aes_xts_init(vctx, key, keylen, iv, ivlen, 1);
  83. }
  84. static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
  85. const unsigned char *iv, size_t ivlen)
  86. {
  87. return aes_xts_init(vctx, key, keylen, iv, ivlen, 0);
  88. }
  89. static void *aes_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
  90. size_t kbits, size_t blkbits, size_t ivbits)
  91. {
  92. PROV_AES_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  93. if (ctx != NULL) {
  94. cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode, flags,
  95. PROV_CIPHER_HW_aes_xts(kbits), NULL);
  96. }
  97. return ctx;
  98. }
  99. static void aes_xts_freectx(void *vctx)
  100. {
  101. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  102. OPENSSL_clear_free(ctx, sizeof(*ctx));
  103. }
  104. static void *aes_xts_dupctx(void *vctx)
  105. {
  106. PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
  107. PROV_AES_XTS_CTX *ret = NULL;
  108. if (in->xts.key1 != NULL) {
  109. if (in->xts.key1 != &in->ks1)
  110. return NULL;
  111. }
  112. if (in->xts.key2 != NULL) {
  113. if (in->xts.key2 != &in->ks2)
  114. return NULL;
  115. }
  116. ret = OPENSSL_malloc(sizeof(*ret));
  117. if (ret == NULL) {
  118. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  119. return NULL;
  120. }
  121. in->base.hw->copyctx(&ret->base, &in->base);
  122. return ret;
  123. }
  124. static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
  125. size_t outsize, const unsigned char *in, size_t inl)
  126. {
  127. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  128. if (ctx->xts.key1 == NULL
  129. || ctx->xts.key2 == NULL
  130. || !ctx->base.iv_set
  131. || out == NULL
  132. || in == NULL
  133. || inl < AES_BLOCK_SIZE)
  134. return 0;
  135. /*
  136. * Impose a limit of 2^20 blocks per data unit as specified by
  137. * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
  138. * indicated that this was a SHOULD NOT rather than a MUST NOT.
  139. * NIST SP 800-38E mandates the same limit.
  140. */
  141. if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
  142. ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
  143. return 0;
  144. }
  145. if (ctx->stream != NULL)
  146. (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
  147. else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
  148. ctx->base.enc))
  149. return 0;
  150. *outl = inl;
  151. return 1;
  152. }
  153. static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
  154. size_t outsize, const unsigned char *in,
  155. size_t inl)
  156. {
  157. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  158. if (outsize < inl) {
  159. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  160. return 0;
  161. }
  162. if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
  163. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  164. return 0;
  165. }
  166. return 1;
  167. }
  168. static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
  169. size_t outsize)
  170. {
  171. *outl = 0;
  172. return 1;
  173. }
  174. static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
  175. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  176. OSSL_PARAM_END
  177. };
  178. static const OSSL_PARAM *aes_xts_settable_ctx_params(void)
  179. {
  180. return aes_xts_known_settable_ctx_params;
  181. }
  182. static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  183. {
  184. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  185. const OSSL_PARAM *p;
  186. /*
  187. * TODO(3.0) We need a general solution for handling missing parameters
  188. * inside set_params and get_params methods.
  189. */
  190. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  191. if (p != NULL) {
  192. size_t keylen;
  193. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  194. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  195. return 0;
  196. }
  197. /* The key length can not be modified for xts mode */
  198. if (keylen != ctx->keylen)
  199. return 0;
  200. }
  201. return 1;
  202. }
  203. #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
  204. static OSSL_OP_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
  205. static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  206. { \
  207. return cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  208. flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
  209. AES_XTS_IV_BITS); \
  210. } \
  211. static OSSL_OP_cipher_newctx_fn aes_##kbits##_xts_newctx; \
  212. static void *aes_##kbits##_xts_newctx(void *provctx) \
  213. { \
  214. return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
  215. AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
  216. } \
  217. const OSSL_DISPATCH aes##kbits##xts_functions[] = { \
  218. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
  219. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
  220. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
  221. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
  222. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
  223. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
  224. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
  225. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
  226. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  227. (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
  228. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  229. (void (*)(void))cipher_generic_gettable_params }, \
  230. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  231. (void (*)(void))cipher_generic_get_ctx_params }, \
  232. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  233. (void (*)(void))cipher_generic_gettable_ctx_params }, \
  234. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  235. (void (*)(void))aes_xts_set_ctx_params }, \
  236. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  237. (void (*)(void))aes_xts_settable_ctx_params }, \
  238. { 0, NULL } \
  239. }
  240. IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
  241. IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);