Skip to content

Commit a83198b

Browse files
committed
Get a higher sleep after TCXO on for a better radio drift stability + cleaning
Fix the AES CBC encryption for HMAC calculation
1 parent b92419a commit a83198b

File tree

6 files changed

+97
-206
lines changed

6 files changed

+97
-206
lines changed

Inc/it_sdk/configSigfox.h.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
// | SDK SETTING | USER SELECTED VALUE | SETTING DESCRIPTION |
4343
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4444
// +------------SIGFOX-------------|--------------------------------------|---------------------------------------|
45-
#if ITSDK_WITH_SIGFOX_LIB == 1
45+
#if ITSDK_WITH_SIGFOX_LIB == __ENABLE
4646
#define ITSDK_SIGFOX_ENCRYPTION ( __PAYLOAD_ENCRYPT_NONE \
4747
| __PAYLOAD_ENCRYPT_AESCTR \
4848
| __PAYLOAD_ENCRYPT_SPECK \

Inc/it_sdk/encrypt/encrypt.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,13 @@ void itsdk_aes_ecb_decrypt_128B(
9797
uint8_t dataLen, // Size of data to be encrypted
9898
uint8_t * masterKey // 128B key used for encryption (hidden with ITSDK_PROTECT_KEY)
9999
);
100+
101+
102+
void itsdk_aes_cbc_encrypt_128B(
103+
uint8_t * clearData, // Data to be encrypted
104+
uint8_t * encryptedData, // Can be the same as clearData
105+
uint8_t dataLen, // Size of data to be encrypted can be higher than 16B
106+
uint8_t * masterKey // 128B key used for encryption (hidden with ITSDK_PROTECT_KEY)
107+
);
108+
100109
#endif /* IT_SDK_ENCRYPT_H_ */

Src/drivers/sx1276/murata_cmwx1zzabz.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Maintainer: Miguel Luis and Gregory Cristian
4343

4444
#include <it_sdk/wrappers.h>
4545

46-
#define BOARD_WAKEUP_TIME 5
46+
#define BOARD_WAKEUP_TIME 50 // Was 5ms wait after TCXO is set ON, try 50 to see if we have a better stability
4747
#define IRQ_HIGH_PRIORITY 0
4848

4949
#define TCXO_ON() gpio_set(ITSDK_SX1276_TCXO_VCC_BANK,ITSDK_SX1276_TCXO_VCC_PIN);
Lines changed: 29 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
1-
/******************************************************************************
2-
* @file sgfx_credentials_template.c
3-
* @author MCD Application Team
4-
* @version V1.1.0
5-
* @date 30-April-2018
6-
* @brief manages keys and encryption algorithm
7-
******************************************************************************
8-
* @attention
1+
/* ==========================================================
2+
* sigfox_credentials.c - Driver for Sigfox SX1276
3+
* Project : Disk91 SDK
4+
* ----------------------------------------------------------
5+
* Created on: 18 may 2019
6+
* Author: Paul Pinault aka Disk91
7+
* ----------------------------------------------------------
8+
* Copyright (C) 2019 Disk91
99
*
10-
* <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
11-
* All rights reserved.</center></h2>
10+
* This program is free software: you can redistribute it and/or modify
11+
* it under the terms of the GNU LESSER General Public License as published by
12+
* the Free Software Foundation, either version 3 of the License, or
13+
* any later version.
1214
*
13-
* Redistribution and use in source and binary forms, with or without
14-
* modification, are permitted, provided that the following conditions are met:
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU General Lesser Public License for more details.
1519
*
16-
* 1. Redistribution of source code must retain the above copyright notice,
17-
* this list of conditions and the following disclaimer.
18-
* 2. Redistributions in binary form must reproduce the above copyright notice,
19-
* this list of conditions and the following disclaimer in the documentation
20-
* and/or other materials provided with the distribution.
21-
* 3. Neither the name of STMicroelectronics nor the names of other
22-
* contributors to this software may be used to endorse or promote products
23-
* derived from this software without specific written permission.
24-
* 4. This software, including modifications and/or derivative works of this
25-
* software, must execute solely and exclusively on microcontroller or
26-
* microprocessor devices manufactured by or for STMicroelectronics.
27-
* 5. Redistribution and use of this software other than as permitted under
28-
* this license is void and will automatically terminate your rights under
29-
* this license.
20+
* You should have received a copy of the GNU Lesser General Public License
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
22+
* ----------------------------------------------------------
3023
*
31-
* THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
32-
* AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
33-
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
34-
* PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
35-
* RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
36-
* SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37-
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38-
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39-
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40-
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42-
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+
* Some peaces of that code directly comes from ST Libraries
25+
* and identified with << COPYRIGHT(c) 2019 STMicroelectronics >>
4326
*
44-
******************************************************************************
27+
* ==========================================================
4528
*/
4629
#include <stdint.h>
4730
#include <string.h>
@@ -79,55 +62,18 @@ typedef struct manuf_device_info_s
7962
} manuf_device_info_t;
8063
*/
8164

82-
/*PREPROCESSOR CONVERSION*/
83-
/*
84-
#define DECIMAL2STRING_DEF(s) #s
85-
#define DECIMAL2STRING(s) DECIMAL2STRING_DEF(s)
86-
87-
#ifndef UNUSED
88-
#define UNUSED(x) ((void) x)
89-
#endif
90-
#ifndef ALIGN
91-
#define ALIGN(n) __attribute__((aligned(n)))
92-
#endif
93-
*/
94-
/*SIGfox defines*/
95-
9665

9766
#define SIGNATURE_LEN 16
9867
static uint8_t session_key[SIGNATURE_LEN]={0};
9968

10069

101-
102-
103-
//#define SIGFOX_DATA_LEN 48 /*bytes*/
104-
105-
//#define CREDENTIALS_VERSION 11
106-
107-
//#define PUBLIC_KEY {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
108-
/*CREDENTIAL_KEY may be used to encrypt sigfox_data
109-
CREDENTIAL_KEY must be aligned with the sigfox tool generating and encrypting the sigfox_data*/
110-
/*
111-
#define CREDENTIAL_KEY {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01}
112-
*/
113-
/* Private macro -------------------------------------------------------------*/
114-
/* Private variables ---------------------------------------------------------*/
115-
//static sfx_bool encrypt_flag = SFX_FALSE;
116-
117-
//static aes_context AesContext;
118-
119-
//extern sfx_u8 encrypted_sigfox_data[SIGFOX_DATA_LEN];
120-
121-
//static uint8_t device_public_key[]=ITSDK_SIGFOX_KEY_PUBLIC;
122-
12370
static const char sgfxSeeLibVersion[]="CRED v1.1";
12471

12572
//
12673

12774
/* Private function prototypes -----------------------------------------------*/
12875
static void CREDENTIALS_get_key (uint8_t* key, sfx_key_type_t KeyType );
12976

130-
//static sfx_error_t CREDENTIALS_get_cra(sfx_u8 *decrypted_data, sfx_u8 *data_to_decrypt, sfx_u8 data_len);
13177

13278
/* Public function definition -----------------------------------------------*/
13379

@@ -143,22 +89,13 @@ sfx_error_t CREDENTIALS_aes_128_cbc_encrypt(uint8_t* encrypted_data, uint8_t* da
14389
uint8_t key[AES_KEY_LEN];
14490
sfx_key_type_t KeyType = SE_NVM_get_key_type();
14591
CREDENTIALS_get_key ( key, KeyType );
146-
itsdk_aes_ecb_encrypt_128B(
92+
itsdk_aes_cbc_encrypt_128B(
14793
data_to_encrypt,
14894
encrypted_data,
149-
blocks,
95+
16*blocks,
15096
key
15197
);
15298
bzero(key,AES_KEY_LEN);
153-
/*
154-
//aes_set_key( key, AES_KEY_LEN, &AesContext);
155-
//memset(key, 0, AES_KEY_LEN);
156-
aes_cbc_encrypt( data_to_encrypt,
157-
encrypted_data,
158-
blocks,
159-
iv,
160-
&AesContext );
161-
*/
16299
return SFX_ERR_NONE;
163100
}
164101

@@ -168,21 +105,13 @@ sfx_error_t CREDENTIALS_aes_128_cbc_encrypt(uint8_t* encrypted_data, uint8_t* da
168105
*/
169106
sfx_error_t CREDENTIALS_aes_128_cbc_encrypt_with_session_key(uint8_t* encrypted_data, uint8_t* data_to_encrypt, uint8_t blocks) {
170107
LOG_DEBUG_SFXSX1276((">> CREDENTIALS_aes_128_cbc_encrypt_with_session_key\r\n"));
171-
itsdk_aes_ecb_encrypt_128B(
108+
itsdk_aes_cbc_encrypt_128B(
172109
data_to_encrypt,
173110
encrypted_data,
174-
blocks,
111+
16*blocks,
175112
session_key
176113
);
177114

178-
//uint8_t iv[N_BLOCK] = {0x00};
179-
//aes_set_key( session_key, AES_KEY_LEN, &AesContext);
180-
//aes_cbc_encrypt( data_to_encrypt,
181-
// encrypted_data,
182-
// blocks,
183-
// iv,
184-
// &AesContext );
185-
186115
return SFX_ERR_NONE;
187116
}
188117

@@ -195,25 +124,13 @@ sfx_error_t CREDENTIALS_wrap_session_key( uint8_t* data, uint8_t blocks) {
195124

196125
uint8_t key[AES_KEY_LEN];
197126
CREDENTIALS_get_key ( key, CREDENTIALS_KEY_PRIVATE);
198-
itsdk_aes_ecb_encrypt_128B(
127+
itsdk_aes_cbc_encrypt_128B(
199128
data,
200129
session_key,
201-
blocks,
130+
16*blocks,
202131
key
203132
);
204133
itsdk_encrypt_cifferKey(session_key,16); // we keep the session_key more secure in ram
205-
206-
207-
// uint8_t iv[N_BLOCK] = {0x00};
208-
// uint8_t key[AES_KEY_LEN];
209-
// CREDENTIALS_get_key ( key, CREDENTIALS_KEY_PRIVATE);
210-
// aes_set_key( key, AES_KEY_LEN, &AesContext);
211-
// memset(key, 0, AES_KEY_LEN);
212-
// aes_cbc_encrypt( data,
213-
// session_key,
214-
// blocks,
215-
// iv,
216-
// &AesContext );
217134

218135
return SFX_ERR_NONE;
219136
}
@@ -230,20 +147,14 @@ const char* CREDENTIALS_get_version( void )
230147
/**
231148
* Returns the device Id
232149
*/
233-
#warning "unclear if devId is little or bigendian"
234150
void CREDENTIALS_get_dev_id( uint8_t* dev_id)
235151
{
236152
LOG_DEBUG_SFXSX1276((">> CREDENTIALS_get_dev_id\r\n"));
237153
uint32_t devId;
238154
itsdk_sigfox_getDeviceId(&devId);
239155
for (int i = 0 ; i < 4 ; i++) {
240-
dev_id[i]=(devId >> ((32-8)-8*i)) & 0xFF;
156+
dev_id[3-i]=(devId >> ((32-8)-8*i)) & 0xFF;
241157
}
242-
243-
//manuf_device_info_t DeviceInfo;
244-
//CREDENTIALS_get_cra( (uint8_t*) &DeviceInfo, encrypted_sigfox_data, sizeof(manuf_device_info_t) );
245-
//memcpy(dev_id, DeviceInfo.dev_id, MANUF_DEVICE_ID_LENGTH);
246-
//memset( DeviceInfo.dev_key, 0, AES_KEY_LEN);
247158
}
248159

249160
/**
@@ -253,11 +164,6 @@ void CREDENTIALS_get_initial_pac( uint8_t* pac)
253164
{
254165
LOG_DEBUG_SFXSX1276((">> CREDENTIALS_get_initial_pac\r\n"));
255166
itsdk_sigfox_getInitialPac(pac);
256-
257-
//manuf_device_info_t DeviceInfo;
258-
//CREDENTIALS_get_cra( (uint8_t*) &DeviceInfo, encrypted_sigfox_data, sizeof(manuf_device_info_t) );
259-
//memcpy(pac, DeviceInfo.pac, MANUF_PAC_LENGTH);
260-
//memset( DeviceInfo.dev_key, 0, AES_KEY_LEN);
261167
}
262168

263169
/**
@@ -289,11 +195,6 @@ static void CREDENTIALS_get_key(uint8_t* key, sfx_key_type_t KeyType){
289195
break;
290196
case CREDENTIALS_KEY_PRIVATE: {
291197
itsdk_sigfox_getKEY(key);
292-
293-
//manuf_device_info_t DeviceInfo;
294-
//CREDENTIALS_get_cra( (uint8_t*) &DeviceInfo, encrypted_sigfox_data, sizeof(manuf_device_info_t) );
295-
//memcpy(key, DeviceInfo.dev_key, AES_KEY_LEN);
296-
//memset( DeviceInfo.dev_key, 0, AES_KEY_LEN);
297198
}
298199
break;
299200
default:
@@ -303,42 +204,5 @@ static void CREDENTIALS_get_key(uint8_t* key, sfx_key_type_t KeyType){
303204
}
304205

305206

306-
/**
307-
* This procedure extracts the data from an eeprom where i is stored encrypted
308-
* then it decrypt it and returns it.
309-
*/
310-
/*
311-
static sfx_error_t CREDENTIALS_get_cra(sfx_u8 *decrypted_data, sfx_u8 *data_to_decrypt, sfx_u8 data_len) {
312-
313-
314-
315-
#ifdef CREDENTIAL_KEY
316-
uint8_t iv[N_BLOCK] = {0x00};
317-
318-
uint8_t CredentialKey[AES_KEY_LEN]=CREDENTIAL_KEY;
319-
320-
//device is provisioned with sigfox_data.h
321-
//encrypted with CREDENTIAL_KEY in Sigfox Tool
322-
aes_set_key( CredentialKey, AES_KEY_LEN, &AesContext);
323-
324-
memset( CredentialKey, 0, AES_KEY_LEN);
325-
326-
aes_cbc_decrypt( data_to_decrypt,
327-
decrypted_data,
328-
sizeof(manuf_device_info_t) / AES_KEY_LEN,
329-
iv,
330-
&AesContext );
331-
#else
332-
// default sigfox_data.h provided, sigfox_data.h is not encrypted
333-
memcpy( (uint8_t*) decrypted_data, (uint8_t*) data_to_decrypt, sizeof(manuf_device_info_t) );
334-
335-
#endif
336-
337-
return SFX_ERR_NONE;
338-
}
339-
*/
340-
341207
#endif
342208

343-
344-
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

0 commit comments

Comments
 (0)