gcm.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright 2014-2022 The GmSSL Project. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the License); you may
  5. * not use this file except in compliance with the License.
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. */
  9. #ifndef GMSSL_GCM_H
  10. #define GMSSL_GCM_H
  11. #include <stdlib.h>
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include <gmssl/gf128.h>
  15. #include <gmssl/block_cipher.h>
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. #define GCM_IV_MIN_SIZE 1
  20. #define GCM_IV_MAX_SIZE ((uint64_t)(1 << (64-3)))
  21. #define GCM_IV_DEFAULT_BITS 96
  22. #define GCM_IV_DEFAULT_SIZE 12
  23. #define GCM_MIN_AAD_SIZE 0
  24. #define GCM_MAX_AAD_SIZE ((uint64_t)(1 << (64-3)))
  25. #define GCM_MIN_PLAINTEXT_SIZE 0
  26. #define GCM_MAX_PLAINTEXT_SIZE ((((uint64_t)1 << 39) - 256) >> 3)
  27. #define GHASH_SIZE (16)
  28. #define GCM_IS_LITTLE_ENDIAN 1
  29. void ghash(const uint8_t h[16], const uint8_t *aad, size_t aadlen,
  30. const uint8_t *c, size_t clen, uint8_t out[16]);
  31. typedef struct {
  32. gf128_t H;
  33. gf128_t X;
  34. size_t aadlen;
  35. size_t clen;
  36. uint8_t block[16];
  37. size_t num;
  38. } GHASH_CTX;
  39. void ghash_init(GHASH_CTX *ctx, const uint8_t h[16], const uint8_t *aad, size_t aadlen);
  40. void ghash_update(GHASH_CTX *ctx, const uint8_t *c, size_t clen);
  41. void ghash_finish(GHASH_CTX *ctx, uint8_t out[16]);
  42. int gcm_encrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *iv, size_t ivlen,
  43. const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
  44. uint8_t *out, size_t taglen, uint8_t *tag);
  45. int gcm_decrypt(const BLOCK_CIPHER_KEY *key, const uint8_t *iv, size_t ivlen,
  46. const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
  47. const uint8_t *tag, size_t taglen, uint8_t *out);
  48. #ifdef __cplusplus
  49. }
  50. #endif
  51. #endif