Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fake radio and other test #11

Merged
merged 38 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e168719
Add a fake radio for testing
ngraziano Nov 14, 2020
555bc6f
Correct some warning on avr.
ngraziano Nov 15, 2020
e291952
Make print more compatible with stm32
ngraziano Nov 15, 2020
020b4f6
Correct a warning.
ngraziano Nov 15, 2020
01a3925
Use std:array
ngraziano Nov 15, 2020
b0f3067
More std::array
ngraziano Nov 16, 2020
da720a6
More std::array
ngraziano Nov 16, 2020
cc0cc97
More std:array
ngraziano Nov 20, 2020
5941a3d
more std::array
ngraziano Nov 20, 2020
513b6a2
Replace stl lib.
ngraziano Nov 22, 2020
6b227f3
Correct esp32 aes
ngraziano Nov 22, 2020
b113d8f
Add some logs
ngraziano Feb 14, 2021
2ff36ed
CPPCheck warning
ngraziano Jul 26, 2021
180d8b8
Improve job api
ngraziano Jul 26, 2021
c03d400
Use array
ngraziano Jul 26, 2021
33941fc
Simplification
ngraziano Jul 26, 2021
232c326
Use std::array
ngraziano Jul 26, 2021
5c73626
Remove incompatible with esp
ngraziano Jul 26, 2021
a28a6e2
Remove some include
ngraziano Jul 28, 2021
151b9bb
Add reading of packet status
ngraziano Jul 29, 2021
70aacd0
Remove unused value.
ngraziano Aug 7, 2021
8bd2eef
Cleanup syntax
ngraziano Aug 7, 2021
b2efef3
Some code formating
ngraziano Aug 7, 2021
9907053
Try to handle non arduino platform
ngraziano Aug 8, 2021
a5461c4
Make work without arduino
ngraziano Aug 14, 2021
1a6fd16
Add some unit tests
ngraziano Aug 14, 2021
0675cc5
Add test for jey
ngraziano Aug 15, 2021
eb39c7e
Correct ifdef
ngraziano Aug 15, 2021
5eff572
Add unit test to build
ngraziano Aug 15, 2021
ab7a22c
try to correct test
ngraziano Aug 15, 2021
8ee0a68
Try to correct test
ngraziano Aug 15, 2021
471477e
Correct some problems
ngraziano Aug 15, 2021
3ffe066
Add test for mic
ngraziano Aug 15, 2021
8be8671
Add some mic test
ngraziano Aug 15, 2021
611e11d
Simplify end of tx
ngraziano Oct 10, 2021
ee48f52
Simplification
ngraziano Oct 10, 2021
1e2af12
Change vesrion
ngraziano Oct 10, 2021
f49e3a9
Merge branch 'library' into fakeRadio
ngraziano Oct 10, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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