2
0

cipher_sm4_gcm.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2021-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 GCM mode */
  10. #include "cipher_sm4_gcm.h"
  11. #include "prov/implementations.h"
  12. #include "prov/providercommon.h"
  13. static OSSL_FUNC_cipher_freectx_fn sm4_gcm_freectx;
  14. static void *sm4_gcm_newctx(void *provctx, size_t keybits)
  15. {
  16. PROV_SM4_GCM_CTX *ctx;
  17. if (!ossl_prov_is_running())
  18. return NULL;
  19. ctx = OPENSSL_zalloc(sizeof(*ctx));
  20. if (ctx != NULL)
  21. ossl_gcm_initctx(provctx, &ctx->base, keybits,
  22. ossl_prov_sm4_hw_gcm(keybits));
  23. return ctx;
  24. }
  25. static void *sm4_gcm_dupctx(void *provctx)
  26. {
  27. PROV_SM4_GCM_CTX *ctx = provctx;
  28. PROV_SM4_GCM_CTX *dctx = NULL;
  29. if (ctx == NULL)
  30. return NULL;
  31. dctx = OPENSSL_memdup(ctx, sizeof(*ctx));
  32. if (dctx != NULL && dctx->base.gcm.key != NULL)
  33. dctx->base.gcm.key = &dctx->ks.ks;
  34. return dctx;
  35. }
  36. static void sm4_gcm_freectx(void *vctx)
  37. {
  38. PROV_SM4_GCM_CTX *ctx = (PROV_SM4_GCM_CTX *)vctx;
  39. OPENSSL_clear_free(ctx, sizeof(*ctx));
  40. }
  41. /* ossl_sm4128gcm_functions */
  42. IMPLEMENT_aead_cipher(sm4, gcm, GCM, AEAD_FLAGS, 128, 8, 96);