1#if !defined(_RADIOLIB_CRYPTOGRAPHY_H)
2#define _RADIOLIB_CRYPTOGRAPHY_H
7#define RADIOLIB_AES128_BLOCK_SIZE (16)
8#define RADIOLIB_AES128_KEY_SIZE (RADIOLIB_AES128_BLOCK_SIZE)
9#define RADIOLIB_AES128_N_K ((RADIOLIB_AES128_BLOCK_SIZE) / sizeof(uint32_t))
10#define RADIOLIB_AES128_N_B (4)
11#define RADIOLIB_AES128_N_R (10)
12#define RADIOLIB_AES128_KEY_EXP_SIZE (176)
15 uint8_t X[RADIOLIB_AES128_BLOCK_SIZE];
16 uint8_t buffer[RADIOLIB_AES128_BLOCK_SIZE];
18 uint8_t k1[RADIOLIB_AES128_BLOCK_SIZE];
19 uint8_t k2[RADIOLIB_AES128_BLOCK_SIZE];
20 bool subkeys_generated;
24typedef uint8_t state_t[4][4];
45 virtual void init(uint8_t* key) = 0;
57 virtual size_t encryptECB(
const uint8_t* in,
size_t len, uint8_t* out) = 0;
69 virtual size_t decryptECB(
const uint8_t* in,
size_t len, uint8_t* out) = 0;
77 void generateCMAC(
const uint8_t* in,
size_t len, uint8_t* cmac);
107 bool verifyCMAC(
const uint8_t* in,
size_t len,
const uint8_t* cmac);
110 void blockXor(uint8_t* dst,
const uint8_t* a,
const uint8_t* b);
111 void blockLeftshift(uint8_t* dst,
const uint8_t* src);
112 void generateSubkeys(uint8_t* key1, uint8_t* key2);
116#if !RADIOLIB_CUSTOM_AES128
135 void init(uint8_t* key)
override;
145 size_t encryptECB(
const uint8_t* in,
size_t len, uint8_t* out)
override;
155 size_t decryptECB(
const uint8_t* in,
size_t len, uint8_t* out)
override;
158 uint8_t* keyPtr =
nullptr;
159 uint8_t roundKey[RADIOLIB_AES128_KEY_EXP_SIZE] = { 0 };
161 void keyExpansion(uint8_t* roundKey,
const uint8_t* key);
162 void cipher(state_t* state, uint8_t* roundKey);
163 void decipher(state_t* state, uint8_t* roundKey);
165 void subWord(uint8_t* word);
166 void rotWord(uint8_t* word);
168 void subBytes(state_t* state,
const uint8_t* box);
169 void shiftRows(state_t* state,
bool inv);
170 void mixColumns(state_t* state,
bool inv);
173 uint8_t mul(uint8_t a, uint8_t b);
174 void addRoundKey(uint8_t round, state_t* state,
const uint8_t* roundKey);
Class to perform AES encryption, decryption and CMAC.
Definition Cryptography.h:32
RadioLibAES128()
Default constructor.
Definition Cryptography.cpp:5
void generateCMAC(const uint8_t *in, size_t len, uint8_t *cmac)
Calculate message authentication code according to RFC4493.
Definition Cryptography.cpp:98
void initCMAC(RadioLibCmacState *st)
Initialize the CMAC state. This must be called before any updateCMAC calls.
Definition Cryptography.cpp:21
virtual size_t decryptECB(const uint8_t *in, size_t len, uint8_t *out)=0
Perform ECB-type AES decryption. If the user has a hardware with AES acceleration,...
bool verifyCMAC(const uint8_t *in, size_t len, const uint8_t *cmac)
Verify the received CMAC. This just calculates the CMAC again and compares the results.
Definition Cryptography.cpp:105
void finishCMAC(RadioLibCmacState *st, uint8_t *out)
Finalize the CMAC calculation and save the result. This must be called after all updateCMAC calls are...
Definition Cryptography.cpp:69
virtual size_t encryptECB(const uint8_t *in, size_t len, uint8_t *out)=0
Perform ECB-type AES encryption. If the user has a hardware with AES acceleration,...
virtual void init(uint8_t *key)=0
Initialize the AES. If the user has a hardware with AES acceleration, this method is the interface to...
void updateCMAC(RadioLibCmacState *st, const uint8_t *data, size_t len)
Update the CMAC state with a chunk of data. This can be called multiple times to process the data in ...
Definition Cryptography.cpp:31
Class to perform AES encryption and decryption in software only. Contains implementation of pure virt...
Definition Cryptography.h:124
void init(uint8_t *key) override
Initialize the AES.
Definition Cryptography.cpp:238
RadioLibSoftwareAES128()
Default constructor.
Definition Cryptography.cpp:234
size_t encryptECB(const uint8_t *in, size_t len, uint8_t *out) override
Perform ECB-type AES encryption.
Definition Cryptography.cpp:243
size_t decryptECB(const uint8_t *in, size_t len, uint8_t *out) override
Perform ECB-type AES decryption.
Definition Cryptography.cpp:259
Definition Cryptography.h:14