ciphercommon_gcm_hw.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright 2001-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 "prov/ciphercommon.h"
  10. #include "prov/ciphercommon_gcm.h"
  11. int ossl_gcm_setiv(PROV_GCM_CTX *ctx, const unsigned char *iv, size_t ivlen)
  12. {
  13. CRYPTO_gcm128_setiv(&ctx->gcm, iv, ivlen);
  14. return 1;
  15. }
  16. int ossl_gcm_aad_update(PROV_GCM_CTX *ctx, const unsigned char *aad,
  17. size_t aad_len)
  18. {
  19. return CRYPTO_gcm128_aad(&ctx->gcm, aad, aad_len) == 0;
  20. }
  21. int ossl_gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
  22. size_t len, unsigned char *out)
  23. {
  24. if (ctx->enc) {
  25. if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
  26. return 0;
  27. } else {
  28. if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
  29. return 0;
  30. }
  31. return 1;
  32. }
  33. int ossl_gcm_cipher_final(PROV_GCM_CTX *ctx, unsigned char *tag)
  34. {
  35. if (ctx->enc) {
  36. CRYPTO_gcm128_tag(&ctx->gcm, tag, GCM_TAG_MAX_SIZE);
  37. ctx->taglen = GCM_TAG_MAX_SIZE;
  38. } else {
  39. if (CRYPTO_gcm128_finish(&ctx->gcm, tag, ctx->taglen) != 0)
  40. return 0;
  41. }
  42. return 1;
  43. }
  44. int ossl_gcm_one_shot(PROV_GCM_CTX *ctx, unsigned char *aad, size_t aad_len,
  45. const unsigned char *in, size_t in_len,
  46. unsigned char *out, unsigned char *tag, size_t tag_len)
  47. {
  48. int ret = 0;
  49. /* Use saved AAD */
  50. if (!ctx->hw->aadupdate(ctx, aad, aad_len))
  51. goto err;
  52. if (!ctx->hw->cipherupdate(ctx, in, in_len, out))
  53. goto err;
  54. ctx->taglen = GCM_TAG_MAX_SIZE;
  55. if (!ctx->hw->cipherfinal(ctx, tag))
  56. goto err;
  57. ret = 1;
  58. err:
  59. return ret;
  60. }