RadioLib
Universal wireless communication library for Arduino
Loading...
Searching...
No Matches
Cryptography.h
1#if !defined(_RADIOLIB_CRYPTOGRAPHY_H)
2#define _RADIOLIB_CRYPTOGRAPHY_H
3
4#include "../TypeDef.h"
5
6// AES-128 constants
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)
13
14typedef struct {
15 uint8_t X[RADIOLIB_AES128_BLOCK_SIZE];
16 uint8_t buffer[RADIOLIB_AES128_BLOCK_SIZE];
17 size_t buffer_len;
18 uint8_t k1[RADIOLIB_AES128_BLOCK_SIZE];
19 uint8_t k2[RADIOLIB_AES128_BLOCK_SIZE];
20 bool subkeys_generated;
22
23// helper type
24typedef uint8_t state_t[4][4];
25
33 public:
38
45 virtual void init(uint8_t* key) = 0;
46
57 virtual size_t encryptECB(const uint8_t* in, size_t len, uint8_t* out) = 0;
58
69 virtual size_t decryptECB(const uint8_t* in, size_t len, uint8_t* out) = 0;
70
77 void generateCMAC(const uint8_t* in, size_t len, uint8_t* cmac);
78
84
91 void updateCMAC(RadioLibCmacState* st, const uint8_t* data, size_t len);
92
98 void finishCMAC(RadioLibCmacState* st, uint8_t* out);
99
107 bool verifyCMAC(const uint8_t* in, size_t len, const uint8_t* cmac);
108
109 private:
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);
113};
114
115// in cases the user does not provide their own hardware-based AES-128, use the default software implementation
116#if !RADIOLIB_CUSTOM_AES128
117
125 public:
130
135 void init(uint8_t* key) override;
136
145 size_t encryptECB(const uint8_t* in, size_t len, uint8_t* out) override;
146
155 size_t decryptECB(const uint8_t* in, size_t len, uint8_t* out) override;
156
157 private:
158 uint8_t* keyPtr = nullptr;
159 uint8_t roundKey[RADIOLIB_AES128_KEY_EXP_SIZE] = { 0 };
160
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);
164
165 void subWord(uint8_t* word);
166 void rotWord(uint8_t* word);
167
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);
171
172 // cppcheck seems convinced these are nut used, which is not true
173 uint8_t mul(uint8_t a, uint8_t b); // cppcheck-suppress unusedPrivateFunction
174 void addRoundKey(uint8_t round, state_t* state, const uint8_t* roundKey); // cppcheck-suppress unusedPrivateFunction
175};
176
177#endif
178
179#endif
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