cipher_aes_xts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * Copyright 2019-2020 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 "cipher_aes_xts.h"
  16. #include "prov/implementations.h"
  17. #include "prov/providercommon.h"
  18. #include "prov/providercommonerr.h"
  19. /* TODO (3.0) Figure out what flags need to be set */
  20. #define AES_XTS_FLAGS (EVP_CIPH_CUSTOM_IV \
  21. | EVP_CIPH_ALWAYS_CALL_INIT \
  22. | EVP_CIPH_CTRL_INIT \
  23. | EVP_CIPH_CUSTOM_COPY)
  24. #define AES_XTS_IV_BITS 128
  25. #define AES_XTS_BLOCK_BITS 8
  26. /* forward declarations */
  27. static OSSL_FUNC_cipher_encrypt_init_fn aes_xts_einit;
  28. static OSSL_FUNC_cipher_decrypt_init_fn aes_xts_dinit;
  29. static OSSL_FUNC_cipher_update_fn aes_xts_stream_update;
  30. static OSSL_FUNC_cipher_final_fn aes_xts_stream_final;
  31. static OSSL_FUNC_cipher_cipher_fn aes_xts_cipher;
  32. static OSSL_FUNC_cipher_freectx_fn aes_xts_freectx;
  33. static OSSL_FUNC_cipher_dupctx_fn aes_xts_dupctx;
  34. static OSSL_FUNC_cipher_set_ctx_params_fn aes_xts_set_ctx_params;
  35. static OSSL_FUNC_cipher_settable_ctx_params_fn aes_xts_settable_ctx_params;
  36. /*
  37. * Verify that the two keys are different.
  38. *
  39. * This addresses the vulnerability described in Rogaway's
  40. * September 2004 paper:
  41. *
  42. * "Efficient Instantiations of Tweakable Blockciphers and
  43. * Refinements to Modes OCB and PMAC".
  44. * (http://web.cs.ucdavis.edu/~rogaway/papers/offsets.pdf)
  45. *
  46. * FIPS 140-2 IG A.9 XTS-AES Key Generation Requirements states
  47. * that:
  48. * "The check for Key_1 != Key_2 shall be done at any place
  49. * BEFORE using the keys in the XTS-AES algorithm to process
  50. * data with them."
  51. */
  52. static int aes_xts_check_keys_differ(const unsigned char *key, size_t bytes,
  53. int enc)
  54. {
  55. if ((!allow_insecure_decrypt || enc)
  56. && CRYPTO_memcmp(key, key + bytes, bytes) == 0) {
  57. ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DUPLICATED_KEYS);
  58. return 0;
  59. }
  60. return 1;
  61. }
  62. /*-
  63. * Provider dispatch functions
  64. */
  65. static int aes_xts_init(void *vctx, const unsigned char *key, size_t keylen,
  66. const unsigned char *iv, size_t ivlen, int enc)
  67. {
  68. PROV_AES_XTS_CTX *xctx = (PROV_AES_XTS_CTX *)vctx;
  69. PROV_CIPHER_CTX *ctx = &xctx->base;
  70. if (!ossl_prov_is_running())
  71. return 0;
  72. ctx->enc = enc;
  73. if (iv != NULL) {
  74. if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
  75. return 0;
  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. 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. ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
  132. return NULL;
  133. }
  134. in->base.hw->copyctx(&ret->base, &in->base);
  135. return ret;
  136. }
  137. static int aes_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
  138. size_t outsize, const unsigned char *in, size_t inl)
  139. {
  140. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  141. if (!ossl_prov_is_running()
  142. || ctx->xts.key1 == NULL
  143. || ctx->xts.key2 == NULL
  144. || !ctx->base.iv_set
  145. || out == NULL
  146. || in == NULL
  147. || inl < AES_BLOCK_SIZE)
  148. return 0;
  149. /*
  150. * Impose a limit of 2^20 blocks per data unit as specified by
  151. * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
  152. * indicated that this was a SHOULD NOT rather than a MUST NOT.
  153. * NIST SP 800-38E mandates the same limit.
  154. */
  155. if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * AES_BLOCK_SIZE) {
  156. ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
  157. return 0;
  158. }
  159. if (ctx->stream != NULL)
  160. (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2, ctx->base.iv);
  161. else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
  162. ctx->base.enc))
  163. return 0;
  164. *outl = inl;
  165. return 1;
  166. }
  167. static int aes_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
  168. size_t outsize, const unsigned char *in,
  169. size_t inl)
  170. {
  171. PROV_AES_XTS_CTX *ctx = (PROV_AES_XTS_CTX *)vctx;
  172. if (outsize < inl) {
  173. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  174. return 0;
  175. }
  176. if (!aes_xts_cipher(ctx, out, outl, outsize, in, inl)) {
  177. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  178. return 0;
  179. }
  180. return 1;
  181. }
  182. static int aes_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
  183. size_t outsize)
  184. {
  185. if (!ossl_prov_is_running())
  186. return 0;
  187. *outl = 0;
  188. return 1;
  189. }
  190. static const OSSL_PARAM aes_xts_known_settable_ctx_params[] = {
  191. OSSL_PARAM_size_t(OSSL_CIPHER_PARAM_KEYLEN, NULL),
  192. OSSL_PARAM_END
  193. };
  194. static const OSSL_PARAM *aes_xts_settable_ctx_params(ossl_unused void *provctx)
  195. {
  196. return aes_xts_known_settable_ctx_params;
  197. }
  198. static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  199. {
  200. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  201. const OSSL_PARAM *p;
  202. /*
  203. * TODO(3.0) We need a general solution for handling missing parameters
  204. * inside set_params and get_params methods.
  205. */
  206. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  207. if (p != NULL) {
  208. size_t keylen;
  209. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  210. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  211. return 0;
  212. }
  213. /* The key length can not be modified for xts mode */
  214. if (keylen != ctx->keylen)
  215. return 0;
  216. }
  217. return 1;
  218. }
  219. #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
  220. static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
  221. static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  222. { \
  223. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  224. flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
  225. AES_XTS_IV_BITS); \
  226. } \
  227. static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx; \
  228. static void *aes_##kbits##_xts_newctx(void *provctx) \
  229. { \
  230. return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
  231. AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
  232. } \
  233. const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = { \
  234. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
  235. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
  236. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
  237. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
  238. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
  239. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
  240. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
  241. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
  242. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  243. (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
  244. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  245. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  246. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  247. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  248. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  249. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  250. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  251. (void (*)(void))aes_xts_set_ctx_params }, \
  252. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  253. (void (*)(void))aes_xts_settable_ctx_params }, \
  254. { 0, NULL } \
  255. }
  256. IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
  257. IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);