6 // #define the macros below to 1/0 to enable/disable the mode of operation.
8 // CBC enables AES encryption in CBC-mode of operation.
9 // CTR enables encryption in counter-mode.
10 // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
12 // The #ifndef-guard allows it to be configured before #include'ing or at compile time.
30 #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only
32 #if defined(AES256) && (AES256 == 1)
34 #define AES_keyExpSize 240
35 #elif defined(AES192) && (AES192 == 1)
37 #define AES_keyExpSize 208
39 #define AES_KEYLEN 16 // Key length in bytes
40 #define AES_keyExpSize 176
45 uint8_t RoundKey[AES_keyExpSize];
46 #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
47 uint8_t Iv[AES_BLOCKLEN];
51 void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
52 #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
53 void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
54 void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
57 #if defined(ECB) && (ECB == 1)
58 // buffer size is exactly AES_BLOCKLEN bytes;
59 // you need only AES_init_ctx as IV is not used in ECB
60 // NB: ECB is considered insecure for most uses
61 void AES_ECB_encrypt(struct AES_ctx* ctx, const uint8_t* buf);
62 void AES_ECB_decrypt(struct AES_ctx* ctx, const uint8_t* buf);
64 #endif // #if defined(ECB) && (ECB == !)
67 #if defined(CBC) && (CBC == 1)
68 // buffer size MUST be mutile of AES_BLOCKLEN;
69 // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
70 // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
71 // no IV should ever be reused with the same key
72 void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
73 void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
75 #endif // #if defined(CBC) && (CBC == 1)
78 #if defined(CTR) && (CTR == 1)
80 // Same function for encrypting as for decrypting.
81 // IV is incremented for every block, and used after encryption as XOR-compliment for output
82 // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
83 // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
84 // no IV should ever be reused with the same key
85 void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
86 void My_AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
88 #endif // #if defined(CTR) && (CTR == 1)
90 void My_KeyExpansion(uint8_t* RoundKey, const uint8_t* Key);
91 void rc4key(unsigned char *key, int size_DK);