Skip to content

Commit

Permalink
Merge pull request #3 from disk91/step3-in-progress
Browse files Browse the repository at this point in the history
Step3 done
  • Loading branch information
disk91 committed Dec 18, 2018
2 parents 1c7dfc3 + 7c9f2a9 commit 56a29fa
Show file tree
Hide file tree
Showing 6 changed files with 2,284 additions and 2,031 deletions.
315 changes: 158 additions & 157 deletions .cproject

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Core/Inc/it_sdk/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@
#define ITSDK_SIGFOX_LIB __SIGFOX_S2LP // Type of Sigfox module


#define ITSDK_PROTECT_KEY 0xA7459BC3 /* CHANGE ME */ // A random value used to protect the SIGFOX (and others) KEY in memory (better than nothing)



#if ITSDK_PLATFORM == __PLATFORM_STM32L0x1 || ITSDK_PLATFORM == __PLATFORM_STM32L0x3
#include <stm32l_sdk/config.h>
#include "stm32l0xx_hal.h"
Expand Down
11 changes: 10 additions & 1 deletion Core/Inc/it_sdk/configSigfox.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
#define ITSDK_SIGFOX_ENCRYPTION ( __SIGFOX_ENCRYPT_AESCTR \
| __SIGFOX_ENCRYPT_SPECK \
) // Encryption code activated
#define ITSDK_SIGFOX_AES_SHAREDKEY ( 0xAE632397 ^ ITSDK_PROTECT_KEY ) // CHANGE ME
// Shared Key for CTR generation
#define ITSDK_SIGFOX_AES_INITALNONCE ( 0x25 ) // CHANGE ME
// Initial value for Nonce used for AES128-CRT
#define ITSDK_SIGFOX_SPECKKEY ( (uint64_t)0xEF583AB7A57834BC \
^ ( (uint64_t)ITSDK_PROTECT_KEY \
| ((uint64_t)ITSDK_PROTECT_KEY << 32)) \
) // CHANGE ME
// Shared Key for SPECK32/64 Encryption

// +---------------S2LP------------|--------------------------------------|---------------------------------------|
#if ITSDK_SIGFOX_LIB == __SIGFOX_S2LP
Expand Down Expand Up @@ -92,12 +101,12 @@
0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00 } // The device KEY when NVM_SOURCE is HEADERS
// The Key is in clear here
#define ITSDK_SIGFOX_AUX { 0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00, \
0x00, 0x00, 0x00, 0x00 } // The device AUX when NVM_SOURCE is HEADERS

#define ITSDK_SIGFOX_PROTECT_KEY 0xA7459BC3 // A random value used to protect the SIGFOX KEY (better than nothing)
#endif
// +-------------OTHERS------------|--------------------------------------|---------------------------------------|

Expand Down
102 changes: 75 additions & 27 deletions Core/Src/project_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,36 +35,71 @@
#include <it_sdk/sigfox/sigfox.h>
#include <it_sdk/eeprom/eeprom.h>

#define VERSION 0x03
struct conf {
uint8_t version;
uint32_t aesSharedkey; // sigfox aes-ctr shared key (protected)
uint64_t speckSharedkey; // sigfox speck shared key (protected)
uint8_t nonce; // sigfox aes-ctr current nonce
} s_conf;

void loadConfig() {
log_debug("In loadConfig \r\n");

struct conf {
uint8_t v1;
uint16_t v2;
uint32_t v3;
} s_conf;

uint8_t v;
if ( ! eeprom_read(&s_conf, sizeof(s_conf), 1,&v) ) {
log_debug("Flashing\r\n");
s_conf.v1 = 10;
s_conf.v2 = 0xA5A5;
s_conf.v3 = 0xFF5AA5FF;
eeprom_write(&s_conf, sizeof(s_conf), 1);
if ( ! eeprom_read(&s_conf, sizeof(s_conf), VERSION,&v) ) {
log_info("Flash the initial configuration\r\n");
s_conf.version = VERSION;
s_conf.aesSharedkey = ITSDK_SIGFOX_AES_SHAREDKEY;
s_conf.speckSharedkey = ITSDK_SIGFOX_SPECKKEY;
s_conf.nonce = ITSDK_SIGFOX_AES_INITALNONCE;
eeprom_write(&s_conf, sizeof(s_conf), VERSION);
} else {
log_debug("Load version %d\r\n",v);
log_debug("v2 : %0X\r\n",s_conf.v2);
log_info("Loaded version %d\r\n",v);
}
}

uint8_t led = GPIO_PIN_SET;
void task() {
log_debug("In task\r\n");
led = (led==GPIO_PIN_SET)?GPIO_PIN_RESET:GPIO_PIN_SET;
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,led);

log_info("time is %d\r\n",(uint32_t)itsdk_time_get_ms());
/**
* Return current nonce - this function is used in sigfox library with AES encryption
*/
itsdk_sigfox_init_t itsdk_sigfox_eas_getNonce(uint8_t * nonce) {
*nonce = s_conf.nonce;
return SIGFOX_INIT_SUCESS;
}

/**
* Return current sharedKey - this function is used in sigfox library with AES encryption
*/
itsdk_sigfox_init_t itsdk_sigfox_eas_getSharedKey(uint32_t * sharedKey) {
*sharedKey = s_conf.aesSharedkey;
return SIGFOX_INIT_SUCESS;
}

/**
* Return current speck sharedKey - this function is used in sigfox library with Speck encryption
*/
itsdk_sigfox_init_t itsdk_sigfox_speck_getMasterKey(uint64_t * masterKey) {
*masterKey = s_conf.speckSharedkey;
return SIGFOX_INIT_SUCESS;
}

struct state {
uint8_t led;
uint32_t loops;
} s_state;

void initState() {
s_state.led = GPIO_PIN_SET;
s_state.loops = 0;
}

/**
* Cycling task every 2 seconds
*/
void task() {
s_state.led = (s_state.led==GPIO_PIN_SET)?GPIO_PIN_RESET:GPIO_PIN_SET;
HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,s_state.led);
//log_info("time is %d",(uint32_t)itsdk_time_get_ms());
}


Expand All @@ -75,20 +110,33 @@ void project_setup() {
log_info("Reset : %d\r\n",itsdk_getResetCause());
itsdk_cleanResetCause();

HAL_Delay(2000);

// Init at boot time
loadConfig();
initState();

// Misc init
itsdk_sigfox_setup();

uint8_t f[12] = { 0,1,2,3,4,5,6,7,8,9,10,11 };
uint8_t mk[16];
itsdk_sigfox_eas_getMasterKey(mk);
itsdk_encrypt_unCifferKey(mk,16);
log_info("K : [ ");
for ( int i = 0 ; i < 16 ; i++ ) {
log_info("%02X ",mk[i]);
}
log_info("]\r\n");

// Send a Sigfox Frame
uint8_t f[12] = { 'a','b','c','d',4,5,6,7,8,9,10,11 };
uint8_t r[8] = {0};
itdsk_sigfox_txrx_t ret = itsdk_sigfox_sendFrame(f,4,2,SIGFOX_SPEED_DEFAULT,SIGFOX_POWER_DEFAULT,SIGFOX_ENCRYPT_NONE,true,r);
itdsk_sigfox_txrx_t ret = itsdk_sigfox_sendFrame(f,4,2,SIGFOX_SPEED_DEFAULT,SIGFOX_POWER_DEFAULT,SIGFOX_ENCRYPT_AESCTR |SIGFOX_ENCRYPT_SPECK ,false,r);

itdt_sched_registerSched(2000,ITSDK_SCHED_CONF_IMMEDIATE, &task);
}

void project_loop() {


uint8_t t[4] = { '/','|','\\','-'};
log_info("\r%c ",t[s_state.loops & 3]);
s_state.loops++;
}

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ See here : https://www.disk91.com/?p=3388
### Step 2
* Send a Sigfox Frame - see Tag/Release STEP2

### Step 3
* Send an encrypted Sigfox Frame with AES + SPECK32 encryption - see Tag/Release STEP3

The project have different tag for the different step of realization.

# Installation
Expand All @@ -27,7 +30,6 @@ Some important key point for custom confguration
- S2LP/GPIO3 -> PC0 have a pull-up



# License

This code and ItSdk are under GPLv3. You can use it freely, you can modify, redistribute but *you must* to publish your source code. Other licenses can be obtained by contacting me on [disk91.com](https://www.disk91.com)
Expand Down
Loading

0 comments on commit 56a29fa

Please sign in to comment.