ciphercommon_ccm_hw.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 "prov/ciphercommon.h"
  10. #include "prov/ciphercommon_ccm.h"
  11. int ossl_ccm_generic_setiv(PROV_CCM_CTX *ctx, const unsigned char *nonce,
  12. size_t nlen, size_t mlen)
  13. {
  14. return CRYPTO_ccm128_setiv(&ctx->ccm_ctx, nonce, nlen, mlen) == 0;
  15. }
  16. int ossl_ccm_generic_setaad(PROV_CCM_CTX *ctx, const unsigned char *aad,
  17. size_t alen)
  18. {
  19. CRYPTO_ccm128_aad(&ctx->ccm_ctx, aad, alen);
  20. return 1;
  21. }
  22. int ossl_ccm_generic_gettag(PROV_CCM_CTX *ctx, unsigned char *tag, size_t tlen)
  23. {
  24. return CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, tlen) > 0;
  25. }
  26. int ossl_ccm_generic_auth_encrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
  27. unsigned char *out, size_t len,
  28. unsigned char *tag, size_t taglen)
  29. {
  30. int rv;
  31. if (ctx->str != NULL)
  32. rv = CRYPTO_ccm128_encrypt_ccm64(&ctx->ccm_ctx, in,
  33. out, len, ctx->str) == 0;
  34. else
  35. rv = CRYPTO_ccm128_encrypt(&ctx->ccm_ctx, in, out, len) == 0;
  36. if (rv == 1 && tag != NULL)
  37. rv = (CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, taglen) > 0);
  38. return rv;
  39. }
  40. int ossl_ccm_generic_auth_decrypt(PROV_CCM_CTX *ctx, const unsigned char *in,
  41. unsigned char *out, size_t len,
  42. unsigned char *expected_tag, size_t taglen)
  43. {
  44. int rv = 0;
  45. if (ctx->str != NULL)
  46. rv = CRYPTO_ccm128_decrypt_ccm64(&ctx->ccm_ctx, in, out, len,
  47. ctx->str) == 0;
  48. else
  49. rv = CRYPTO_ccm128_decrypt(&ctx->ccm_ctx, in, out, len) == 0;
  50. if (rv) {
  51. unsigned char tag[16];
  52. if (!CRYPTO_ccm128_tag(&ctx->ccm_ctx, tag, taglen)
  53. || CRYPTO_memcmp(tag, expected_tag, taglen) != 0)
  54. rv = 0;
  55. }
  56. if (rv == 0)
  57. OPENSSL_cleanse(out, len);
  58. return rv;
  59. }