dtuaes.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef _AES_H_
  2. #define _AES_H_
  3. #include <stdint.h>
  4. #include <stddef.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 CBC 1
  22. //#define AES192 1
  23. //#define AES256 1
  24. #define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
  25. #if defined(AES256) && (AES256 == 1)
  26. #define AES_KEYLEN 32
  27. #define AES_keyExpSize 240
  28. #elif defined(AES192) && (AES192 == 1)
  29. #define AES_KEYLEN 24
  30. #define AES_keyExpSize 208
  31. #else
  32. #define AES_KEYLEN 16 // Key length in bytes
  33. #define AES_keyExpSize 176
  34. #endif
  35. struct AES_ctx
  36. {
  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, const uint8_t* iv);
  45. void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
  46. #endif
  47. #if defined(ECB) && (ECB == 1)
  48. // buffer size is exactly AES_BLOCKLEN bytes;
  49. // you need only AES_init_ctx as IV is not used in ECB
  50. // NB: ECB is considered insecure for most uses
  51. void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
  52. void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
  53. #endif // #if defined(ECB) && (ECB == !)
  54. #if defined(CBC) && (CBC == 1)
  55. // buffer size MUST be mutile of AES_BLOCKLEN;
  56. // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
  57. // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
  58. // no IV should ever be reused with the same key
  59. void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
  60. void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
  61. #endif // #if defined(CBC) && (CBC == 1)
  62. #if defined(CTR) && (CTR == 1)
  63. // Same function for encrypting as for decrypting.
  64. // IV is incremented for every block, and used after encryption as XOR-compliment for output
  65. // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
  66. // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
  67. // no IV should ever be reused with the same key
  68. void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, size_t length);
  69. #endif // #if defined(CTR) && (CTR == 1)
  70. #endif /*__AES_H*/