cipher_sm4_xts_hw.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include "cipher_sm4_xts.h"
  10. #define XTS_SET_KEY_FN(fn_set_enc_key, fn_set_dec_key, \
  11. fn_block_enc, fn_block_dec, \
  12. fn_stream, fn_stream_gb) { \
  13. size_t bytes = keylen / 2; \
  14. \
  15. if (ctx->enc) { \
  16. fn_set_enc_key(key, &xctx->ks1.ks); \
  17. xctx->xts.block1 = (block128_f)fn_block_enc; \
  18. } else { \
  19. fn_set_dec_key(key, &xctx->ks1.ks); \
  20. xctx->xts.block1 = (block128_f)fn_block_dec; \
  21. } \
  22. fn_set_enc_key(key + bytes, &xctx->ks2.ks); \
  23. xctx->xts.block2 = (block128_f)fn_block_enc; \
  24. xctx->xts.key1 = &xctx->ks1; \
  25. xctx->xts.key2 = &xctx->ks2; \
  26. xctx->stream = fn_stream; \
  27. xctx->stream_gb = fn_stream_gb; \
  28. }
  29. static int cipher_hw_sm4_xts_generic_initkey(PROV_CIPHER_CTX *ctx,
  30. const unsigned char *key,
  31. size_t keylen)
  32. {
  33. PROV_SM4_XTS_CTX *xctx = (PROV_SM4_XTS_CTX *)ctx;
  34. OSSL_xts_stream_fn stream = NULL;
  35. OSSL_xts_stream_fn stream_gb = NULL;
  36. #ifdef HWSM4_CAPABLE
  37. if (HWSM4_CAPABLE) {
  38. XTS_SET_KEY_FN(HWSM4_set_encrypt_key, HWSM4_set_decrypt_key,
  39. HWSM4_encrypt, HWSM4_decrypt, stream, stream_gb);
  40. return 1;
  41. } else
  42. #endif /* HWSM4_CAPABLE */
  43. #ifdef VPSM4_EX_CAPABLE
  44. if (VPSM4_EX_CAPABLE) {
  45. stream = vpsm4_ex_xts_encrypt;
  46. stream_gb = vpsm4_ex_xts_encrypt_gb;
  47. XTS_SET_KEY_FN(vpsm4_ex_set_encrypt_key, vpsm4_ex_set_decrypt_key,
  48. vpsm4_ex_encrypt, vpsm4_ex_decrypt, stream, stream_gb);
  49. return 1;
  50. } else
  51. #endif /* VPSM4_EX_CAPABLE */
  52. #ifdef VPSM4_CAPABLE
  53. if (VPSM4_CAPABLE) {
  54. stream = vpsm4_xts_encrypt;
  55. stream_gb = vpsm4_xts_encrypt_gb;
  56. XTS_SET_KEY_FN(vpsm4_set_encrypt_key, vpsm4_set_decrypt_key,
  57. vpsm4_encrypt, vpsm4_decrypt, stream, stream_gb);
  58. return 1;
  59. } else
  60. #endif /* VPSM4_CAPABLE */
  61. {
  62. (void)0;
  63. }
  64. {
  65. XTS_SET_KEY_FN(ossl_sm4_set_key, ossl_sm4_set_key, ossl_sm4_encrypt,
  66. ossl_sm4_decrypt, stream, stream_gb);
  67. }
  68. return 1;
  69. }
  70. static void cipher_hw_sm4_xts_copyctx(PROV_CIPHER_CTX *dst,
  71. const PROV_CIPHER_CTX *src)
  72. {
  73. PROV_SM4_XTS_CTX *sctx = (PROV_SM4_XTS_CTX *)src;
  74. PROV_SM4_XTS_CTX *dctx = (PROV_SM4_XTS_CTX *)dst;
  75. *dctx = *sctx;
  76. dctx->xts.key1 = &dctx->ks1.ks;
  77. dctx->xts.key2 = &dctx->ks2.ks;
  78. }
  79. static const PROV_CIPHER_HW sm4_generic_xts = {
  80. cipher_hw_sm4_xts_generic_initkey,
  81. NULL,
  82. cipher_hw_sm4_xts_copyctx
  83. };
  84. #if defined(__riscv) && __riscv_xlen == 64
  85. # include "cipher_sm4_xts_hw_rv64i.inc"
  86. #else
  87. const PROV_CIPHER_HW *ossl_prov_cipher_hw_sm4_xts(size_t keybits)
  88. {
  89. return &sm4_generic_xts;
  90. }
  91. #endif