cipher_gcm_hw.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2001-2019 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_local.h"
  10. #include "internal/ciphers/cipher_gcm.h"
  11. int 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 gcm_aad_update(PROV_GCM_CTX *ctx, const unsigned char *aad, size_t aad_len)
  17. {
  18. return CRYPTO_gcm128_aad(&ctx->gcm, aad, aad_len) == 0;
  19. }
  20. int gcm_cipher_update(PROV_GCM_CTX *ctx, const unsigned char *in,
  21. size_t len, unsigned char *out)
  22. {
  23. if (ctx->enc) {
  24. if (ctx->ctr != NULL) {
  25. #if defined(AES_GCM_ASM)
  26. size_t bulk = 0;
  27. if (len >= 32 && AES_GCM_ASM(ctx)) {
  28. size_t res = (16 - ctx->gcm.mres) % 16;
  29. if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, res))
  30. return 0;
  31. bulk = aesni_gcm_encrypt(in + res, out + res, len - res,
  32. ctx->gcm.key,
  33. ctx->gcm.Yi.c, ctx->gcm.Xi.u);
  34. ctx->gcm.len.u[1] += bulk;
  35. bulk += res;
  36. }
  37. if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in + bulk, out + bulk,
  38. len - bulk, ctx->ctr))
  39. return 0;
  40. #else
  41. if (CRYPTO_gcm128_encrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
  42. return 0;
  43. #endif /* AES_GCM_ASM */
  44. } else {
  45. if (CRYPTO_gcm128_encrypt(&ctx->gcm, in, out, len))
  46. return 0;
  47. }
  48. } else {
  49. if (ctx->ctr != NULL) {
  50. #if defined(AES_GCM_ASM)
  51. size_t bulk = 0;
  52. if (len >= 16 && AES_GCM_ASM(ctx)) {
  53. size_t res = (16 - ctx->gcm.mres) % 16;
  54. if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, res))
  55. return -1;
  56. bulk = aesni_gcm_decrypt(in + res, out + res, len - res,
  57. ctx->gcm.key,
  58. ctx->gcm.Yi.c, ctx->gcm.Xi.u);
  59. ctx->gcm.len.u[1] += bulk;
  60. bulk += res;
  61. }
  62. if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in + bulk, out + bulk,
  63. len - bulk, ctx->ctr))
  64. return 0;
  65. #else
  66. if (CRYPTO_gcm128_decrypt_ctr32(&ctx->gcm, in, out, len, ctx->ctr))
  67. return 0;
  68. #endif /* AES_GCM_ASM */
  69. } else {
  70. if (CRYPTO_gcm128_decrypt(&ctx->gcm, in, out, len))
  71. return 0;
  72. }
  73. }
  74. return 1;
  75. }
  76. int gcm_cipher_final(PROV_GCM_CTX *ctx, unsigned char *tag)
  77. {
  78. if (ctx->enc) {
  79. CRYPTO_gcm128_tag(&ctx->gcm, tag, GCM_TAG_MAX_SIZE);
  80. ctx->taglen = GCM_TAG_MAX_SIZE;
  81. } else {
  82. if (CRYPTO_gcm128_finish(&ctx->gcm, tag, ctx->taglen) != 0)
  83. return 0;
  84. }
  85. return 1;
  86. }
  87. int gcm_one_shot(PROV_GCM_CTX *ctx, unsigned char *aad, size_t aad_len,
  88. const unsigned char *in, size_t in_len,
  89. unsigned char *out, unsigned char *tag, size_t tag_len)
  90. {
  91. int ret = 0;
  92. /* Use saved AAD */
  93. if (!ctx->hw->aadupdate(ctx, aad, aad_len))
  94. goto err;
  95. if (!ctx->hw->cipherupdate(ctx, in, in_len, out))
  96. goto err;
  97. ctx->taglen = GCM_TAG_MAX_SIZE;
  98. if (!ctx->hw->cipherfinal(ctx, tag))
  99. goto err;
  100. ret = 1;
  101. err:
  102. return ret;
  103. }