cipher_aes_xts.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Copyright 2019-2021 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. 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 *cctx,
  195. ossl_unused void *provctx)
  196. {
  197. return aes_xts_known_settable_ctx_params;
  198. }
  199. static int aes_xts_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  200. {
  201. PROV_CIPHER_CTX *ctx = (PROV_CIPHER_CTX *)vctx;
  202. const OSSL_PARAM *p;
  203. if (params == NULL)
  204. return 1;
  205. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_KEYLEN);
  206. if (p != NULL) {
  207. size_t keylen;
  208. if (!OSSL_PARAM_get_size_t(p, &keylen)) {
  209. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  210. return 0;
  211. }
  212. /* The key length can not be modified for xts mode */
  213. if (keylen != ctx->keylen)
  214. return 0;
  215. }
  216. return 1;
  217. }
  218. #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
  219. static OSSL_FUNC_cipher_get_params_fn aes_##kbits##_##lcmode##_get_params; \
  220. static int aes_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  221. { \
  222. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  223. flags, 2 * kbits, AES_XTS_BLOCK_BITS, \
  224. AES_XTS_IV_BITS); \
  225. } \
  226. static OSSL_FUNC_cipher_newctx_fn aes_##kbits##_xts_newctx; \
  227. static void *aes_##kbits##_xts_newctx(void *provctx) \
  228. { \
  229. return aes_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
  230. AES_XTS_BLOCK_BITS, AES_XTS_IV_BITS); \
  231. } \
  232. const OSSL_DISPATCH ossl_aes##kbits##xts_functions[] = { \
  233. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))aes_##kbits##_xts_newctx }, \
  234. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))aes_xts_einit }, \
  235. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))aes_xts_dinit }, \
  236. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))aes_xts_stream_update }, \
  237. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))aes_xts_stream_final }, \
  238. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))aes_xts_cipher }, \
  239. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))aes_xts_freectx }, \
  240. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))aes_xts_dupctx }, \
  241. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  242. (void (*)(void))aes_##kbits##_##lcmode##_get_params }, \
  243. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  244. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  245. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  246. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  247. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  248. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  249. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  250. (void (*)(void))aes_xts_set_ctx_params }, \
  251. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  252. (void (*)(void))aes_xts_settable_ctx_params }, \
  253. { 0, NULL } \
  254. }
  255. IMPLEMENT_cipher(xts, XTS, 256, AES_XTS_FLAGS);
  256. IMPLEMENT_cipher(xts, XTS, 128, AES_XTS_FLAGS);