aes.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_AES_H
  10. #define GMSSL_AES_H
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13. #ifdef __cplusplus
  14. extern "C" {
  15. #endif
  16. #define AES128_KEY_BITS 128
  17. #define AES192_KEY_BITS 192
  18. #define AES256_KEY_BITS 256
  19. #define AES128_KEY_SIZE (AES128_KEY_BITS/8)
  20. #define AES192_KEY_SIZE (AES192_KEY_BITS/8)
  21. #define AES256_KEY_SIZE (AES256_KEY_BITS/8)
  22. #define AES_BLOCK_SIZE 16
  23. #define AES128_ROUNDS 10
  24. #define AES192_ROUNDS 12
  25. #define AES256_ROUNDS 14
  26. #define AES_MAX_ROUNDS AES256_ROUNDS
  27. typedef struct {
  28. uint32_t rk[4 * (AES_MAX_ROUNDS + 1)];
  29. size_t rounds;
  30. } AES_KEY;
  31. int aes_set_encrypt_key(AES_KEY *key, const uint8_t *raw_key, size_t raw_key_len);
  32. int aes_set_decrypt_key(AES_KEY *key, const uint8_t *raw_key, size_t raw_key_len);
  33. void aes_encrypt(const AES_KEY *key, const uint8_t in[AES_BLOCK_SIZE], uint8_t out[AES_BLOCK_SIZE]);
  34. void aes_decrypt(const AES_KEY *key, const uint8_t in[AES_BLOCK_SIZE], uint8_t out[AES_BLOCK_SIZE]);
  35. void aes_cbc_encrypt(const AES_KEY *key, const uint8_t iv[AES_BLOCK_SIZE],
  36. const uint8_t *in, size_t nblocks, uint8_t *out);
  37. void aes_cbc_decrypt(const AES_KEY *key, const uint8_t iv[AES_BLOCK_SIZE],
  38. const uint8_t *in, size_t nblocks, uint8_t *out);
  39. int aes_cbc_padding_encrypt(const AES_KEY *key, const uint8_t iv[AES_BLOCK_SIZE],
  40. const uint8_t *in, size_t inlen,
  41. uint8_t *out, size_t *outlen);
  42. int aes_cbc_padding_decrypt(const AES_KEY *key, const uint8_t iv[AES_BLOCK_SIZE],
  43. const uint8_t *in, size_t inlen,
  44. uint8_t *out, size_t *outlen);
  45. void aes_ctr_encrypt(const AES_KEY *key, uint8_t ctr[AES_BLOCK_SIZE],
  46. const uint8_t *in, size_t inlen, uint8_t *out);
  47. #define aes_ctr_decrypt(key,ctr,in,inlen,out) aes_ctr_encrypt(key,ctr,in,inlen,out)
  48. #define AES_GCM_IV_MIN_SIZE 1
  49. #define AES_GCM_IV_MAX_SIZE ((uint64_t)(1 << (64-3)))
  50. #define AES_GCM_IV_DEFAULT_BITS 96
  51. #define AES_GCM_IV_DEFAULT_SIZE 12
  52. #define AES_GCM_MIN_AAD_SIZE 0
  53. #define AES_GCM_MAX_AAD_SIZE ((uint64_t)(1 << (64-3)))
  54. #define AES_GCM_MIN_PLAINTEXT_SIZE 0
  55. #define AES_GCM_MAX_PLAINTEXT_SIZE ((((uint64_t)1 << 39) - 256) >> 3)
  56. #define AES_GCM_MAX_TAG_SIZE 16
  57. int aes_gcm_encrypt(const AES_KEY *key, const uint8_t *iv, size_t ivlen,
  58. const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
  59. uint8_t *out, size_t taglen, uint8_t *tag);
  60. int aes_gcm_decrypt(const AES_KEY *key, const uint8_t *iv, size_t ivlen,
  61. const uint8_t *aad, size_t aadlen, const uint8_t *in, size_t inlen,
  62. const uint8_t *tag, size_t taglen, uint8_t *out);
  63. #ifdef __cplusplus
  64. }
  65. #endif
  66. #endif