cipher_aes_xts.c 10 KB

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