Skip to content

Commit

Permalink
Merge pull request #11 from ngraziano/fakeRadio
Browse files Browse the repository at this point in the history
Fake radio and other test
  • Loading branch information
ngraziano committed Oct 10, 2021
2 parents 213a219 + f49e3a9 commit 980357f
Show file tree
Hide file tree
Showing 61 changed files with 1,100 additions and 521 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ jobs:
run: |
python -m pip install --upgrade pip
pip install platformio
platformio platform install native
# Workaround.
pip install "click!=8.0.2"
- name: Run unit test
run: platformio test -e native
- name: Build simple exemple
run: platformio ci --lib="." --board=pro8MHzatmega328 --project-option="lib_deps=ArduinoSTL" examples/simple
run: platformio ci --lib="." --board=pro8MHzatmega328 --project-option="lib_deps=ciband/avr_stl" examples/simple
- name: Build simple sx1262 exemple
run: platformio ci --lib="." --board=pro8MHzatmega328 --project-option="lib_deps=ArduinoSTL" examples/simple_sx1262
run: platformio ci --lib="." --board=pro8MHzatmega328 --project-option="lib_deps=ciband/avr_stl" examples/simple_sx1262
- name: Build esp32 exemple
run: platformio ci --lib="." --board=heltec_wifi_lora_32 examples/esp32
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"string_iostream": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"utility": "cpp"
"utility": "cpp",
"array": "cpp"
},
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This library try to comply with lorawan 1.0.x but do not implement all feature.

For a more complete Lorawan library based on lmic, please have a look to <https://github.com/mcci-catena/arduino-lmic>

:warning: This library do not compile in Arduino IDE due to dependency to STL (ArduinoSTL in case of AVR platform).
:warning: This library do not compile in Arduino IDE due to dependency to STL (ciband/avr_stl in case of AVR platform).
It need PlatfomIO for the dependencies to be handle correctly.

For the SX1262 it only support board with TCXO.
Expand Down
2 changes: 1 addition & 1 deletion examples/BME280_2.4v/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ board_build.f_cpu = 2000000L


lib_deps =
ArduinoSTL
ciband/avr_stl
https://github.com/ngraziano/LMICPP-Arduino.git


2 changes: 1 addition & 1 deletion examples/balise/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ build_flags = -Wall -Wextra -O3
upload_speed = 38400

lib_deps =
ArduinoSTL
ciband/avr_stl
https://github.com/ngraziano/LMICPP-Arduino.git

2 changes: 1 addition & 1 deletion examples/balise_2.4v/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ board_build.f_cpu = 2000000L


lib_deps =
ArduinoSTL
ciband/avr_stl
https://github.com/ngraziano/LMICPP-Arduino.git


2 changes: 1 addition & 1 deletion examples/simple/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ build_flags = -Wall -Wextra -O3
# upload_speed = 38400

lib_deps =
ArduinoSTL
ciband/avr_stl
ngraziano/LMICPP-Arduino

2 changes: 1 addition & 1 deletion examples/simple_sx1262/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ board_build.f_cpu = 8000000L
upload_speed = 38400

lib_deps =
ArduinoSTL
ciband/avr_stl
https://github.com/ngraziano/LMICPP-Arduino.git

2 changes: 1 addition & 1 deletion examples/tempsensor/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ build_flags = -Wall -Wextra -O3
upload_speed = 38400

lib_deps =
ArduinoSTL
ciband/avr_stl
DallasTemperature
https://github.com/ngraziano/LMICPP-Arduino.git

2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "LMICPP-Arduino",
"version": "2.1.2",
"version": "2.2.0",
"keywords": "Lora",
"description": "Modified Arduino port of the LMIC (LoraWAN-in-C, formerly LoraMAC-in-C) framework provided by IBM. Changed to C++ format.",
"frameworks": ["arduino"],
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=LMICPP-Arduino
version=2.1.2
version=2.2.0
author=IBM
maintainer=Nicolas Graziano <[email protected]>
sentence=Modified Arduino port of the LMIC (LoraWAN-in-C, formerly LoraMAC-in-C) framework provided by IBM. Changed to C++ format.
Expand Down
20 changes: 19 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ build_flags = -Wall -Wextra -O3 -DENABLE_SAVE_RESTORE
test_build_project_src = true

lib_deps =
ArduinoSTL
ciband/avr_stl

[env:esp32]
platform = espressif32
Expand All @@ -40,4 +40,22 @@ monitor_speed = 19200
test_build_project_src=true
build_flags = -Wall -Wextra -O3 -DENABLE_SAVE_RESTORE

lib_deps =


[env:windows]
platform = windows_x86
# lib_compat_mode = off
build_flags = -std=c++17 -Wall -Wextra -O3
test_build_project_src=true
test_transport = native
lib_deps =


[env:native]
platform = native
# lib_compat_mode = off
build_flags = -std=c++17 -Wall -Wextra -O3
test_build_project_src=true
test_transport = native
lib_deps =
21 changes: 5 additions & 16 deletions src/aes/aes_encrypt.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,19 @@
#ifndef __aes_tiny_h__
#define __aes_tiny_h__

#include <array>
#include <stdint.h>
struct AesKey {
static const uint8_t key_size = 16;
uint8_t data[key_size];

uint8_t *begin() { return data; };
uint8_t *end() { return data + key_size; };


uint8_t const *begin() const { return data; };
uint8_t const *end() const { return data + key_size; };


AesKey() = default;
};

constexpr uint8_t key_size = 16;
using AesKey = std::array<uint8_t, key_size>;

void aes_tiny_128_encrypt(uint8_t *buffer, AesKey const &key);

#ifdef ARDUINO_ARCH_ESP32
void aes_esp_128_encrypt(uint8_t *buffer, AesKey const &key);
constexpr auto aes_128_encrypt=aes_esp_128_encrypt;
constexpr auto aes_128_encrypt = aes_esp_128_encrypt;
#else
constexpr auto aes_128_encrypt=aes_tiny_128_encrypt;
constexpr auto aes_128_encrypt = aes_tiny_128_encrypt;
#endif

#endif
67 changes: 44 additions & 23 deletions src/aes/aes_tiny.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,41 @@ Change to use this project type and adapt to some C++ type.

#include "aes_encrypt.h"
#include <algorithm>
#include <array>
#include <stdint.h>


#ifdef ARDUINO
#ifdef __AVR__
#include <avr/pgmspace.h>
#else
#else
#include <pgmspace.h>
#endif

#else
#define PROGMEM
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
#endif
namespace {

struct Indices {
uint8_t col;
uint8_t row;
const uint8_t col;
const uint8_t row;
};

struct DataBlock {
static constexpr uint8_t data_size = 16;
uint8_t data[data_size];

uint8_t *begin() { return data; };
uint8_t *begin() { return data.begin(); };
uint8_t const *begin() const { return data.begin(); };

uint8_t *end() { return data + data_size; };
uint8_t *end() { return data.end(); };
uint8_t const *end() const { return data.end(); };

uint8_t &operator[](Indices i) { return data[i.col * 4 + i.row]; }
uint8_t *column(uint8_t col) { return data + (4 * col); }
uint8_t &operator[](Indices const i) { return data[i.col * 4 + i.row]; }
uint8_t *column(uint8_t const col) { return data.begin() + (4 * col); }

private:
std::array<uint8_t, data_size> data;
};

constexpr uint8_t sbox[256] PROGMEM = {
Expand Down Expand Up @@ -98,9 +108,8 @@ static inline uint8_t readsbox(uint8_t val) {

// Rcon(i), 2^(i+1) in the Rijndael finite field, for i = 0..9.
// http://en.wikipedia.org/wiki/Rijndael_key_schedule
constexpr uint8_t const rcon[10] PROGMEM = {0x01, 0x02, 0x04,
0x08, 0x10, 0x20, 0x40,
0x80, 0x1B, 0x36};
constexpr uint8_t const rcon[10] PROGMEM = {0x01, 0x02, 0x04, 0x08, 0x10,
0x20, 0x40, 0x80, 0x1B, 0x36};

static inline void keyScheduleCore(uint8_t *output, const uint8_t *input,
uint8_t iteration) {
Expand Down Expand Up @@ -176,34 +185,46 @@ void mixColumn(uint8_t *buffer) {
buffer[3] = a2 ^ a ^ b ^ c ^ d2;
}

void kcore(uint8_t n, uint8_t schedule[16]) {
void kcore(uint8_t n, AesKey &schedule) {
uint8_t temp[4];
keyScheduleCore(temp, schedule + 12, n);
keyScheduleCore(temp, schedule.begin() + 12, n);
schedule[0] ^= temp[0];
schedule[1] ^= temp[1];
schedule[2] ^= temp[2];
schedule[3] ^= temp[3];
}

void kxor(uint8_t a, uint8_t b, uint8_t schedule[16]) {
void kxor(uint8_t const a, uint8_t const b, AesKey &schedule) {
schedule[a * 4] ^= schedule[b * 4];
schedule[a * 4 + 1] ^= schedule[b * 4 + 1];
schedule[a * 4 + 2] ^= schedule[b * 4 + 2];
schedule[a * 4 + 3] ^= schedule[b * 4 + 3];
}

void expand_key(AesKey &schedule, uint8_t round) {
kcore(round, schedule.data);
kxor(1, 0, schedule.data);
kxor(2, 1, schedule.data);
kxor(3, 2, schedule.data);
kcore(round, schedule);
kxor(1, 0, schedule);
kxor(2, 1, schedule);
kxor(3, 2, schedule);
}

void xorbuffer(uint8_t const *source1, AesKey &source2, uint8_t *dest) {
std::transform(source1, source1 + 16, source2.data, dest,
std::transform(source1, source1 + 16, source2.begin(), dest,
[](uint8_t a, uint8_t b) { return a ^ b; });
}

void xorbuffer(DataBlock const &source1, AesKey &source2, uint8_t *dest) {
xorbuffer(source1.begin(), source2, dest);
}

void xorbuffer(uint8_t const *source1, AesKey &source2, DataBlock &dest) {
xorbuffer(source1, source2, dest.begin());
}

void xorbuffer(DataBlock const &source1, AesKey &source2, DataBlock &dest) {
xorbuffer(source1.begin(), source2, dest.begin());
}

} // namespace

void aes_tiny_128_encrypt(uint8_t *buffer, AesKey const &key) {
Expand All @@ -213,7 +234,7 @@ void aes_tiny_128_encrypt(uint8_t *buffer, AesKey const &key) {

DataBlock state1;
// Copy the input into the state and XOR with the key schedule.
xorbuffer(buffer, schedule, state1.data);
xorbuffer(buffer, schedule, state1);

// Perform the first 9 rounds of the cipher.
for (uint8_t round = 0; round < 9; ++round) {
Expand All @@ -226,13 +247,13 @@ void aes_tiny_128_encrypt(uint8_t *buffer, AesKey const &key) {
mixColumn(state1.column(1));
mixColumn(state1.column(2));
mixColumn(state1.column(3));
xorbuffer(state1.data, schedule, state1.data);
xorbuffer(state1, schedule, state1);
}

// Expand the final 16 bytes of the key schedule.
expand_key(schedule, 9);

// Perform the final round.
subBytesAndShiftRows(state1);
xorbuffer(state1.data, schedule, buffer);
xorbuffer(state1, schedule, buffer);
}
2 changes: 1 addition & 1 deletion src/aes/asp_esp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
void aes_esp_128_encrypt(uint8_t *buffer, AesKey const &key) {
mbedtls_aes_context keyCtx;
mbedtls_aes_init(&keyCtx);
mbedtls_aes_setkey_enc(&keyCtx, key.data, 128);
mbedtls_aes_setkey_enc(&keyCtx, key.data(), 128);
mbedtls_aes_crypt_ecb(&keyCtx, ESP_AES_ENCRYPT, buffer, buffer);
mbedtls_aes_free(&keyCtx);
}
Expand Down
Loading

0 comments on commit 980357f

Please sign in to comment.