cipher_aes_xts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * Copyright 2019-2023 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. /*
  10. * AES low level APIs are deprecated for public use, but still ok for internal
  11. * use where we're using them to implement the higher level EVP interface, as is
  12. * the case here.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <openssl/proverr.h>
  16. #include "cipher_aes_xts.h"
  17. #include "prov/implementations.h"
  18. #include "prov/providercommon.h"
  19. #define AES_XTS_FLAGS PROV_CIPHER_FLAG_CUSTOM_IV
  20. #define AES_XTS_IV_BITS 128
  21. #define AES_XTS_BLOCK_BITS 8
  22. /* forward declarations */
  23. static OSSL_FUNC_cipher_encrypt_init_fn aes_xts_einit;
  24. static OSSL_FUNC_cipher_decrypt_init_fn aes_xts_dinit;
  25. static OSSL_FUNC_cipher_update_fn aes_xts_stream_update;
  26. static OSSL_FUNC_cipher_final_fn aes_xts_stream_final;
  27. static OSSL_FUNC_cipher_cipher_fn aes_xts_cipher;
  28. static OSSL_FUNC_cipher_freectx_fn aes_xts_freectx;
  29. static OSSL_FUNC_cipher_dupctx_fn aes_xts_dupctx;
  30. static OSSL_FUNC_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
  31. static OSSL_FUNC_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
  32. /*
  33. * Verify that the two keys are different.
  34. *
  35. * This addresses the vulnerability described in Rogaway's
  36. * September 2004 paper:
  37. *
  38. * "Efficient Instantiations of Tweakable Blockciphers and
  39. * Refinements to Modes OCB and PMAC".
  40. * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
  41. *
  42. * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
  43. * that:
  44. * "The check for Key_1 != Key_2 shall be done at any place
  45. * BEFORE using the keys in the XTS-AES algorithm to process
  46. * data with them."
  47. */
  48. static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
  49. int enc)
  50. {
  51. if ((!ossl_aes_xts_allow_insecure_decrypt || enc)
  52. && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
  53. ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
  54. return 0;
  55. }
  56. return 1;
  57. }
  58. /*-
  59. * Provider dispatch functions
  60. */
  61. static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
  62. const unsigned char *iv, size_t ivlen,
  63. const OSSL_PARAM params[], int enc)
  64. {
  65. PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
  66. PROV_CIPHER_CTX *ctx = &xctx->base;
  67. if (!ossl_prov_is_running())
  68. return 0;
  69. ctx->enc = enc;
  70. if (iv != NULL) {
  71. if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
  72. return 0;
  73. }
  74. if (key != NULL) {
  75. if (keylen != ctx->keylen) {
  76. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  77. return 0;
  78. }
  79. if (!aes_xts_check_keys_differ(key, keylen / 2, enc))
  80. return 0;
  81. if (!ctx->hw->init(ctx, key, keylen))
  82. return 0;
  83. }
  84. return aes_xts_set_ctx_params(ctx, params);
  85. }
  86. static int aes_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
  87. const unsigned char *iv, size_t ivlen,
  88. const OSSL_PARAM params[])
  89. {
  90. return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 1);
  91. }
  92. static int aes_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
  93. const unsigned char *iv, size_t ivlen,
  94. const OSSL_PARAM params[])
  95. {
  96. return aes_xts_init(vctx, key, keylen, iv, ivlen, params, 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. ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
  104. flags, ossl_prov_cipher_hw_aes_xts(kbits),
  105. NULL);
  106. }
  107. return ctx;
  108. }
  109. static void aes_xts_freectx(void *vctx)
  110. {
  111. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  112. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  113. OPENSSL_clear_free(ctx, sizeof(*ctx));
  114. }
  115. static void *aes_xts_dupctx(void *vctx)
  116. {
  117. PROV_AES_XTS_CTX *in = (PROV_AES_XTS_CTX *)vctx;
  118. PROV_AES_XTS_CTX *ret = NULL;
  119. if (!ossl_prov_is_running())
  120. return NULL;
  121. if (in->xts.key1 != NULL) {
  122. if (in->xts.key1 != &in->ks1)
  123. return NULL;
  124. }
  125. if (in->xts.key2 != NULL) {
  126. if (in->xts.key2 != &in->ks2)
  127. return NULL;
  128. }
  129. ret = OPENSSL_malloc(sizeof(*ret));
  130. if (ret == NULL)
  131. return NULL;
  132. in->base.hw->copyctx(&ret->base, &in->base);
  133. return ret;
  134. }
  135. static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
  136. size_t outsize, const unsigned char *in, size_t inl)
  137. {
  138. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  139. if (!ossl_prov_is_running()
  140. || ctx->xts.key1 == NULL
  141. || ctx->xts.key2 == NULL
  142. || !ctx->base.iv_set
  143. || out == NULL
  144. || in == NULL
  145. || inl < AES_BLOCK_SIZE)
  146. return 0;
  147. /*
  148. * Impose a limit of 2^20 blocks per data unit as specified by
  149. * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
  150. * indicated that this was a SHOULD NOT rather than a MUST NOT.
  151. * NIST SP 800-38E mandates the same limit.
  152. */
  153. if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
  154. ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
  155. return 0;
  156. }
  157. if (ctx->stream != NULL)
  158. (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
  159. else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
  160. ctx->base.enc))
  161. return 0;
  162. *outl = inl;
  163. return 1;
  164. }
  165. static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
  166. size_t outsize, const unsigned char *in,
  167. size_t inl)
  168. {
  169. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  170. if (outsize < inl) {
  171. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  172. return 0;
  173. }
  174. if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
  175. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  176. return 0;
  177. }
  178. return 1;
  179. }
  180. static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
  181. size_t outsize)
  182. {
  183. if (!ossl_prov_is_running())
  184. return 0;
  185. *outl = 0;
  186. return 1;
  187. }
  188. static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
  189. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  190. OSSL_PARAM_END
  191. };
  192. static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *cctx,
  193. ossl_unused void *provctx)
  194. {
  195. return aes_xts_known_settable_ctx_params;
  196. }
  197. static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  198. {
  199. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  200. const OSSL_PARAM *p;
  201. if (params == NULL)
  202. return 1;
  203. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  204. if (p != NULL) {
  205. size_t keylen;
  206. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  207. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  208. return 0;
  209. }
  210. /* The key length can not be modified for xts mode */
  211. if (keylen != ctx->keylen)
  212. return 0;
  213. }
  214. return 1;
  215. }
  216. #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
  217. static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
  218. static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  219. { \
  220. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  221. flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
  222. AES_XTS_IV_BITS); \
  223. } \
  224. static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx; \
  225. static void *aes_##kbits##_xts_newctx(void *provctx) \
  226. { \
  227. return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
  228. AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
  229. } \
  230. const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = { \
  231. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
  232. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
  233. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
  234. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
  235. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
  236. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
  237. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
  238. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
  239. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  240. (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
  241. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  242. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  243. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  244. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  245. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  246. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  247. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  248. (void (*)(void))aes_xts_set_ctx_params }, \
  249. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  250. (void (*)(void))aes_xts_settable_ctx_params }, \
  251. OSSL_DISPATCH_END \
  252. }
  253. IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
  254. IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);