aes.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #ifndef _AES_H_
  2. #define _AES_H_
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. // #define the macros below to 1/0 to enable/disable the mode of operation.
  6. //
  7. // CBC enables AES encryption in CBC-mode of operation.
  8. // CTR enables encryption in counter-mode.
  9. // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
  10. // The #ifndef-guard allows it to be configured before #include'ing or at compile time.
  11. #ifndef CBC
  12. #define CBC 1
  13. #endif
  14. #ifndef ECB
  15. #define ECB 1
  16. #endif
  17. #ifndef CTR
  18. #define CTR 1
  19. #endif
  20. // #define AES128 1
  21. #define AES192 1
  22. //#define AES256 1
  23. #define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
  24. #if defined(AES256) && (AES256 == 1)
  25. #define AES_KEYLEN 32
  26. #define AES_keyExpSize 240
  27. #elif defined(AES192) && (AES192 == 1)
  28. #define AES_KEYLEN 24
  29. #define AES_keyExpSize 208
  30. #else
  31. #define AES_KEYLEN 16 // Key length in bytes
  32. #define AES_keyExpSize 176
  33. #endif
  34. struct AES_ctx {
  35. uint8_t RoundKey[AES_keyExpSize];
  36. #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
  37. uint8_t Iv[AES_BLOCKLEN];
  38. #endif
  39. };
  40. void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
  41. #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
  42. void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key,
  43. const uint8_t* iv);
  44. void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
  45. #endif
  46. #if defined(ECB) && (ECB == 1)
  47. // buffer size is exactly AES_BLOCKLEN bytes;
  48. // you need only AES_init_ctx as IV is not used in ECB
  49. // NB: ECB is considered insecure for most uses
  50. void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
  51. void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
  52. #endif // #if defined(ECB) && (ECB == !)
  53. #if defined(CBC) && (CBC == 1)
  54. // buffer size MUST be mutile of AES_BLOCKLEN;
  55. // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
  56. // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
  57. // no IV should ever be reused with the same key
  58. void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
  59. void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
  60. #endif // #if defined(CBC) && (CBC == 1)
  61. #if defined(CTR) && (CTR == 1)
  62. // Same function for encrypting as for decrypting.
  63. // IV is incremented for every block, and used after encryption as XOR-compliment for output
  64. // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
  65. // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
  66. // no IV should ever be reused with the same key
  67. void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
  68. #endif // #if defined(CTR) && (CTR == 1)
  69. #endif // _AES_H_