Wirepas SDK
aes.h
Go to the documentation of this file.
1 #ifndef _AES_H_
2 #define _AES_H_
3 
4 #include <stdint.h>
5 
6 // #define the macros below to 1/0 to enable/disable the mode of operation.
7 //
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.
11 
12 // The #ifndef-guard allows it to be configured before #include'ing or at compile time.
13 #ifndef CBC
14  #define CBC 1
15 #endif
16 
17 #ifndef ECB
18  #define ECB 1
19 #endif
20 
21 #ifndef CTR
22  #define CTR 1
23 #endif
24 
25 
26 #define AES128 1
27 //#define AES192 1
28 //#define AES256 1
29 
30 #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only
31 
32 #if defined(AES256) && (AES256 == 1)
33  #define AES_KEYLEN 32
34  #define AES_keyExpSize 240
35 #elif defined(AES192) && (AES192 == 1)
36  #define AES_KEYLEN 24
37  #define AES_keyExpSize 208
38 #else
39  #define AES_KEYLEN 16 // Key length in bytes
40  #define AES_keyExpSize 176
41 #endif
42 
43 struct AES_ctx
44 {
46 #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
47  uint8_t Iv[AES_BLOCKLEN];
48 #endif
49 };
50 
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);
55 #endif
56 
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, uint8_t* buf);
62 void AES_ECB_decrypt(struct AES_ctx* ctx, uint8_t* buf);
63 
64 #endif // #if defined(ECB) && (ECB == !)
65 
66 
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);
74 
75 #endif // #if defined(CBC) && (CBC == 1)
76 
77 
78 #if defined(CTR) && (CTR == 1)
79 
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 
87 #endif // #if defined(CTR) && (CTR == 1)
88 
89 
90 #endif //_AES_H_
AES_keyExpSize
#define AES_keyExpSize
Definition: aes.h:40
AES_ctx::Iv
uint8_t Iv[16]
Definition: aes.h:47
AES_CTR_xcrypt_buffer
void AES_CTR_xcrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, uint32_t length)
AES_ECB_encrypt
void AES_ECB_encrypt(struct AES_ctx *ctx, uint8_t *buf)
AES_ECB_decrypt
void AES_ECB_decrypt(struct AES_ctx *ctx, uint8_t *buf)
AES_CBC_encrypt_buffer
void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, uint32_t length)
AES_ctx_set_iv
void AES_ctx_set_iv(struct AES_ctx *ctx, const uint8_t *iv)
AES_ctx::RoundKey
uint8_t RoundKey[176]
Definition: aes.h:45
AES_init_ctx
void AES_init_ctx(struct AES_ctx *ctx, const uint8_t *key)
AES_init_ctx_iv
void AES_init_ctx_iv(struct AES_ctx *ctx, const uint8_t *key, const uint8_t *iv)
AES_BLOCKLEN
#define AES_BLOCKLEN
Definition: aes.h:30
AES_CBC_decrypt_buffer
void AES_CBC_decrypt_buffer(struct AES_ctx *ctx, uint8_t *buf, uint32_t length)
AES_ctx
Definition: aes.h:43