MySensors Library & Examples  2.3.2-62-ge298769
aes.h
1 /*
2 * The MySensors Arduino library handles the wireless radio link and protocol
3 * between your home built sensors/actuators and HA controller of choice.
4 * The sensors forms a self healing radio network with optional repeaters. Each
5 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
6 * network topology allowing messages to be routed to nodes.
7 *
8 * Created by Henrik Ekblad <[email protected]>
9 * Copyright (C) 2013-2022 Sensnology AB
10 * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
11 *
12 * Documentation: http://www.mysensors.org
13 * Support Forum: http://forum.mysensors.org
14 *
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * version 2 as published by the Free Software Foundation.
18 *
19 * AES implementation: https://github.com/kokke/tiny-AES-c
20 */
21 
22 #ifndef _AES_H_
23 #define _AES_H_
24 
25 // #define the macros below to 1/0 to enable/disable the mode of operation.
26 //
27 // CBC enables AES encryption in CBC-mode of operation.
28 // CTR enables encryption in counter-mode.
29 // ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
30 
31 // The #ifndef-guard allows it to be configured before #include'ing or at compile time.
32 #ifndef CBC
33 #define CBC 1
34 #endif
35 
36 #ifndef ECB
37 #define ECB 0
38 #endif
39 
40 #ifndef CTR
41 #define CTR 0
42 #endif
43 
44 
45 #define AES128 1
46 //#define AES192 1
47 //#define AES256 1
48 
49 #define AES_BLOCKLEN 16 //Block length in bytes AES is 128b block only
50 
51 #if defined(AES256) && (AES256 == 1)
52 #define AES_KEYLEN 32
53 #define AES_keyExpSize 240
54 #elif defined(AES192) && (AES192 == 1)
55 #define AES_KEYLEN 24
56 #define AES_keyExpSize 208
57 #else
58 #define AES_KEYLEN 16 // Key length in bytes
59 #define AES_keyExpSize 176
60 #endif
61 
65 struct AES_ctx {
66  uint8_t RoundKey[AES_keyExpSize];
67 #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
68  uint8_t Iv[AES_BLOCKLEN];
69 #endif
70 };
71 
72 void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key);
73 #if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
74 void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, const uint8_t* iv);
75 void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
76 #endif
77 
78 #if defined(ECB) && (ECB == 1)
79 // buffer size is exactly AES_BLOCKLEN bytes;
80 // you need only AES_init_ctx as IV is not used in ECB
81 // NB: ECB is considered insecure for most uses
82 void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
83 void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
84 
85 #endif // #if defined(ECB) && (ECB == !)
86 
87 
88 #if defined(CBC) && (CBC == 1)
89 // buffer size MUST be mutile of AES_BLOCKLEN;
90 // Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
91 // NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
92 // no IV should ever be reused with the same key
93 void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
94 void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
95 
96 #endif // #if defined(CBC) && (CBC == 1)
97 
98 
99 #if defined(CTR) && (CTR == 1)
100 
101 // Same function for encrypting as for decrypting.
102 // IV is incremented for every block, and used after encryption as XOR-compliment for output
103 // Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
104 // NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
105 // no IV should ever be reused with the same key
106 void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
107 
108 #endif // #if defined(CTR) && (CTR == 1)
109 
110 
111 #endif //_AES_H_
AES_ctx::Iv
uint8_t Iv[AES_BLOCKLEN]
Iv.
Definition: aes.h:68
AES_ctx::RoundKey
uint8_t RoundKey[AES_keyExpSize]
RoundKey.
Definition: aes.h:66
AES_ctx
AES state structure.
Definition: aes.h:65