cipher_sm4_xts.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright 2022-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. /* Dispatch functions for SM4 XTS mode */
  10. #include <openssl/proverr.h>
  11. #include "cipher_sm4_xts.h"
  12. #include "prov/implementations.h"
  13. #include "prov/providercommon.h"
  14. #define SM4_XTS_FLAGS PROV_CIPHER_FLAG_CUSTOM_IV
  15. #define SM4_XTS_IV_BITS 128
  16. #define SM4_XTS_BLOCK_BITS 8
  17. /* forward declarations */
  18. static OSSL_FUNC_cipher_encrypt_init_fn sm4_xts_einit;
  19. static OSSL_FUNC_cipher_decrypt_init_fn sm4_xts_dinit;
  20. static OSSL_FUNC_cipher_update_fn sm4_xts_stream_update;
  21. static OSSL_FUNC_cipher_final_fn sm4_xts_stream_final;
  22. static OSSL_FUNC_cipher_cipher_fn sm4_xts_cipher;
  23. static OSSL_FUNC_cipher_freectx_fn sm4_xts_freectx;
  24. static OSSL_FUNC_cipher_dupctx_fn sm4_xts_dupctx;
  25. static OSSL_FUNC_cipher_set_ctx_params_fn sm4_xts_set_ctx_params;
  26. static OSSL_FUNC_cipher_settable_ctx_params_fn sm4_xts_settable_ctx_params;
  27. /*-
  28. * Provider dispatch functions
  29. */
  30. static int sm4_xts_init(void *vctx, const unsigned char *key, size_t keylen,
  31. const unsigned char *iv, size_t ivlen,
  32. const OSSL_PARAM params[], int enc)
  33. {
  34. PROV_SM4_XTS_CTX *xctx = (PROV_SM4_XTS_CTX *)vctx;
  35. PROV_CIPHER_CTX *ctx = &xctx->base;
  36. if (!ossl_prov_is_running())
  37. return 0;
  38. ctx->enc = enc;
  39. if (iv != NULL) {
  40. if (!ossl_cipher_generic_initiv(vctx, iv, ivlen))
  41. return 0;
  42. }
  43. if (key != NULL) {
  44. if (keylen != ctx->keylen) {
  45. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
  46. return 0;
  47. }
  48. if (!ctx->hw->init(ctx, key, keylen))
  49. return 0;
  50. }
  51. return sm4_xts_set_ctx_params(xctx, params);
  52. }
  53. static int sm4_xts_einit(void *vctx, const unsigned char *key, size_t keylen,
  54. const unsigned char *iv, size_t ivlen,
  55. const OSSL_PARAM params[])
  56. {
  57. return sm4_xts_init(vctx, key, keylen, iv, ivlen, params, 1);
  58. }
  59. static int sm4_xts_dinit(void *vctx, const unsigned char *key, size_t keylen,
  60. const unsigned char *iv, size_t ivlen,
  61. const OSSL_PARAM params[])
  62. {
  63. return sm4_xts_init(vctx, key, keylen, iv, ivlen, params, 0);
  64. }
  65. static void *sm4_xts_newctx(void *provctx, unsigned int mode, uint64_t flags,
  66. size_t kbits, size_t blkbits, size_t ivbits)
  67. {
  68. PROV_SM4_XTS_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  69. if (ctx != NULL) {
  70. ossl_cipher_generic_initkey(&ctx->base, kbits, blkbits, ivbits, mode,
  71. flags, ossl_prov_cipher_hw_sm4_xts(kbits),
  72. NULL);
  73. }
  74. return ctx;
  75. }
  76. static void sm4_xts_freectx(void *vctx)
  77. {
  78. PROV_SM4_XTS_CTX *ctx = (PROV_SM4_XTS_CTX *)vctx;
  79. ossl_cipher_generic_reset_ctx((PROV_CIPHER_CTX *)vctx);
  80. OPENSSL_clear_free(ctx, sizeof(*ctx));
  81. }
  82. static void *sm4_xts_dupctx(void *vctx)
  83. {
  84. PROV_SM4_XTS_CTX *in = (PROV_SM4_XTS_CTX *)vctx;
  85. PROV_SM4_XTS_CTX *ret = NULL;
  86. if (!ossl_prov_is_running())
  87. return NULL;
  88. if (in->xts.key1 != NULL) {
  89. if (in->xts.key1 != &in->ks1)
  90. return NULL;
  91. }
  92. if (in->xts.key2 != NULL) {
  93. if (in->xts.key2 != &in->ks2)
  94. return NULL;
  95. }
  96. ret = OPENSSL_malloc(sizeof(*ret));
  97. if (ret == NULL)
  98. return NULL;
  99. in->base.hw->copyctx(&ret->base, &in->base);
  100. return ret;
  101. }
  102. static int sm4_xts_cipher(void *vctx, unsigned char *out, size_t *outl,
  103. size_t outsize, const unsigned char *in, size_t inl)
  104. {
  105. PROV_SM4_XTS_CTX *ctx = (PROV_SM4_XTS_CTX *)vctx;
  106. if (!ossl_prov_is_running()
  107. || ctx->xts.key1 == NULL
  108. || ctx->xts.key2 == NULL
  109. || !ctx->base.iv_set
  110. || out == NULL
  111. || in == NULL
  112. || inl < SM4_BLOCK_SIZE)
  113. return 0;
  114. /*
  115. * Impose a limit of 2^20 blocks per data unit as specified by
  116. * IEEE Std 1619-2018. The earlier and obsolete IEEE Std 1619-2007
  117. * indicated that this was a SHOULD NOT rather than a MUST NOT.
  118. * NIST SP 800-38E mandates the same limit.
  119. */
  120. if (inl > XTS_MAX_BLOCKS_PER_DATA_UNIT * SM4_BLOCK_SIZE) {
  121. ERR_raise(ERR_LIB_PROV, PROV_R_XTS_DATA_UNIT_IS_TOO_LARGE);
  122. return 0;
  123. }
  124. if (ctx->xts_standard) {
  125. if (ctx->stream != NULL)
  126. (*ctx->stream)(in, out, inl, ctx->xts.key1, ctx->xts.key2,
  127. ctx->base.iv, ctx->base.enc);
  128. else if (CRYPTO_xts128_encrypt(&ctx->xts, ctx->base.iv, in, out, inl,
  129. ctx->base.enc))
  130. return 0;
  131. } else {
  132. if (ctx->stream_gb != NULL)
  133. (*ctx->stream_gb)(in, out, inl, ctx->xts.key1, ctx->xts.key2,
  134. ctx->base.iv, ctx->base.enc);
  135. else if (ossl_crypto_xts128gb_encrypt(&ctx->xts, ctx->base.iv, in, out,
  136. inl, ctx->base.enc))
  137. return 0;
  138. }
  139. *outl = inl;
  140. return 1;
  141. }
  142. static int sm4_xts_stream_update(void *vctx, unsigned char *out, size_t *outl,
  143. size_t outsize, const unsigned char *in,
  144. size_t inl)
  145. {
  146. PROV_SM4_XTS_CTX *ctx = (PROV_SM4_XTS_CTX *)vctx;
  147. if (outsize < inl) {
  148. ERR_raise(ERR_LIB_PROV, PROV_R_OUTPUT_BUFFER_TOO_SMALL);
  149. return 0;
  150. }
  151. if (!sm4_xts_cipher(ctx, out, outl, outsize, in, inl)) {
  152. ERR_raise(ERR_LIB_PROV, PROV_R_CIPHER_OPERATION_FAILED);
  153. return 0;
  154. }
  155. return 1;
  156. }
  157. static int sm4_xts_stream_final(void *vctx, unsigned char *out, size_t *outl,
  158. size_t outsize)
  159. {
  160. if (!ossl_prov_is_running())
  161. return 0;
  162. *outl = 0;
  163. return 1;
  164. }
  165. static const OSSL_PARAM sm4_xts_known_settable_ctx_params[] = {
  166. OSSL_PARAM_utf8_string(OSSL_CIPHER_PARAM_XTS_STANDARD, NULL, 0),
  167. OSSL_PARAM_END
  168. };
  169. static const OSSL_PARAM *sm4_xts_settable_ctx_params(ossl_unused void *cctx,
  170. ossl_unused void *provctx)
  171. {
  172. return sm4_xts_known_settable_ctx_params;
  173. }
  174. static int sm4_xts_set_ctx_params(void *vxctx, const OSSL_PARAM params[])
  175. {
  176. PROV_SM4_XTS_CTX *xctx = (PROV_SM4_XTS_CTX *)vxctx;
  177. const OSSL_PARAM *p;
  178. if (params == NULL)
  179. return 1;
  180. /*-
  181. * Sets the XTS standard to use with SM4-XTS algorithm.
  182. *
  183. * Must be utf8 string "GB" or "IEEE",
  184. * "GB" means the GB/T 17964-2021 standard
  185. * "IEEE" means the IEEE Std 1619-2007 standard
  186. */
  187. p = OSSL_PARAM_locate_const(params, OSSL_CIPHER_PARAM_XTS_STANDARD);
  188. if (p != NULL) {
  189. const char *xts_standard = NULL;
  190. if (p->data_type != OSSL_PARAM_UTF8_STRING)
  191. return 0;
  192. if (!OSSL_PARAM_get_utf8_string_ptr(p, &xts_standard)) {
  193. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_GET_PARAMETER);
  194. return 0;
  195. }
  196. if (OPENSSL_strcasecmp(xts_standard, "GB") == 0) {
  197. xctx->xts_standard = 0;
  198. } else if (OPENSSL_strcasecmp(xts_standard, "IEEE") == 0) {
  199. xctx->xts_standard = 1;
  200. } else {
  201. ERR_raise(ERR_LIB_PROV, PROV_R_FAILED_TO_SET_PARAMETER);
  202. return 0;
  203. }
  204. }
  205. return 1;
  206. }
  207. #define IMPLEMENT_cipher(lcmode, UCMODE, kbits, flags) \
  208. static OSSL_FUNC_cipher_get_params_fn sm4_##kbits##_##lcmode##_get_params; \
  209. static int sm4_##kbits##_##lcmode##_get_params(OSSL_PARAM params[]) \
  210. { \
  211. return ossl_cipher_generic_get_params(params, EVP_CIPH_##UCMODE##_MODE, \
  212. flags, 2 * kbits, SM4_XTS_BLOCK_BITS,\
  213. SM4_XTS_IV_BITS); \
  214. } \
  215. static OSSL_FUNC_cipher_newctx_fn sm4_##kbits##_xts_newctx; \
  216. static void *sm4_##kbits##_xts_newctx(void *provctx) \
  217. { \
  218. return sm4_xts_newctx(provctx, EVP_CIPH_##UCMODE##_MODE, flags, 2 * kbits, \
  219. SM4_XTS_BLOCK_BITS, SM4_XTS_IV_BITS); \
  220. } \
  221. const OSSL_DISPATCH ossl_sm4##kbits##xts_functions[] = { \
  222. { OSSL_FUNC_CIPHER_NEWCTX, (void (*)(void))sm4_##kbits##_xts_newctx }, \
  223. { OSSL_FUNC_CIPHER_ENCRYPT_INIT, (void (*)(void))sm4_xts_einit }, \
  224. { OSSL_FUNC_CIPHER_DECRYPT_INIT, (void (*)(void))sm4_xts_dinit }, \
  225. { OSSL_FUNC_CIPHER_UPDATE, (void (*)(void))sm4_xts_stream_update }, \
  226. { OSSL_FUNC_CIPHER_FINAL, (void (*)(void))sm4_xts_stream_final }, \
  227. { OSSL_FUNC_CIPHER_CIPHER, (void (*)(void))sm4_xts_cipher }, \
  228. { OSSL_FUNC_CIPHER_FREECTX, (void (*)(void))sm4_xts_freectx }, \
  229. { OSSL_FUNC_CIPHER_DUPCTX, (void (*)(void))sm4_xts_dupctx }, \
  230. { OSSL_FUNC_CIPHER_GET_PARAMS, \
  231. (void (*)(void))sm4_##kbits##_##lcmode##_get_params }, \
  232. { OSSL_FUNC_CIPHER_GETTABLE_PARAMS, \
  233. (void (*)(void))ossl_cipher_generic_gettable_params }, \
  234. { OSSL_FUNC_CIPHER_GET_CTX_PARAMS, \
  235. (void (*)(void))ossl_cipher_generic_get_ctx_params }, \
  236. { OSSL_FUNC_CIPHER_GETTABLE_CTX_PARAMS, \
  237. (void (*)(void))ossl_cipher_generic_gettable_ctx_params }, \
  238. { OSSL_FUNC_CIPHER_SET_CTX_PARAMS, \
  239. (void (*)(void))sm4_xts_set_ctx_params }, \
  240. { OSSL_FUNC_CIPHER_SETTABLE_CTX_PARAMS, \
  241. (void (*)(void))sm4_xts_settable_ctx_params }, \
  242. OSSL_DISPATCH_END \
  243. }
  244. /* ossl_sm4128xts_functions */
  245. IMPLEMENT_cipher(xts, XTS, 128, SM4_XTS_FLAGS);