cipher_sm4_hw.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. #include "cipher_sm4.h"
  10. static int cipher_hw_sm4_initkey(PROV_CIPHER_CTX *ctx,
  11. const unsigned char *key, size_t keylen)
  12. {
  13. PROV_SM4_CTX *sctx = (PROV_SM4_CTX *)ctx;
  14. SM4_KEY *ks = &sctx->ks.ks;
  15. ossl_sm4_set_key(key, ks);
  16. ctx->ks = ks;
  17. if (ctx->enc
  18. || (ctx->mode != EVP_CIPH_ECB_MODE
  19. && ctx->mode != EVP_CIPH_CBC_MODE))
  20. ctx->block = (block128_f)ossl_sm4_encrypt;
  21. else
  22. ctx->block = (block128_f)ossl_sm4_decrypt;
  23. return 1;
  24. }
  25. IMPLEMENT_CIPHER_HW_COPYCTX(cipher_hw_sm4_copyctx, PROV_SM4_CTX)
  26. # define PROV_CIPHER_HW_sm4_mode(mode) \
  27. static const PROV_CIPHER_HW sm4_##mode = { \
  28. cipher_hw_sm4_initkey, \
  29. ossl_cipher_hw_chunked_##mode, \
  30. cipher_hw_sm4_copyctx \
  31. }; \
  32. const PROV_CIPHER_HW *ossl_prov_cipher_hw_sm4_##mode(size_t keybits) \
  33. { \
  34. return &sm4_##mode; \
  35. }
  36. PROV_CIPHER_HW_sm4_mode(cbc)
  37. PROV_CIPHER_HW_sm4_mode(ecb)
  38. PROV_CIPHER_HW_sm4_mode(ofb128)
  39. PROV_CIPHER_HW_sm4_mode(cfb128)
  40. PROV_CIPHER_HW_sm4_mode(ctr)