aes.h 2.7 KB

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