Skip to content

Commit e5a5af0

Browse files
committed
Add secure communication
1 parent 7e349ee commit e5a5af0

18 files changed

Lines changed: 667 additions & 40 deletions
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2022 IMProject Development Team. All rights reserved.
4+
* Authors: Igor Misic <igy1000mb@gmail.com>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* 3. Neither the name IMProject nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
****************************************************************************/
34+
35+
#ifndef BOOTLOADER_ADAPTERS_INC_SECURITY_ADAPTER_H_
36+
#define BOOTLOADER_ADAPTERS_INC_SECURITY_ADAPTER_H_
37+
38+
#include <stdint.h>
39+
#include <stdbool.h>
40+
41+
void SecurityAdapter_init(void);
42+
bool SecurityAdapter_getRandomData(uint8_t* data, uint32_t size);
43+
44+
#endif /* BOOTLOADER_ADAPTERS_INC_SECURITY_ADAPTER_H_ */

Bootloader/Adapters/Src/flash_adapter.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,8 @@ FlashAdapter_program(uint32_t address, uint8_t* buffer, uint32_t length) {
375375

376376
if ((length_program != 0U) && (length_program < FLASH_WORD_SIZE)) {
377377

378-
uint8_t data[32];
378+
uint8_t data[FLASH_WORD_SIZE];
379+
(void*)memset((void*)data, 0xFF, FLASH_WORD_SIZE);
379380
(void*)memcpy((void*)data, (void*)&buffer[memory_index], length_program);
380381
// cppcheck-suppress misra-c2012-11.4; function expects address of data as uint32_t
381382
HAL_StatusTypeDef status = HAL_FLASH_Program(type_program, address + memory_index, (uint32_t)data);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2022 IMProject Development Team. All rights reserved.
4+
* Authors: Igor Misic <igy1000mb@gmail.com>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* 3. Neither the name IMProject nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
****************************************************************************/
34+
35+
#include "security_adapter.h"
36+
#include "main.h"
37+
38+
#if defined(STM32H7xx)
39+
static RNG_HandleTypeDef hrng;
40+
#endif
41+
42+
void
43+
SecurityAdapter_init() {
44+
45+
#if defined(STM32H7xx)
46+
hrng.Instance = RNG;
47+
HAL_RNG_Init(&hrng);
48+
#endif
49+
}
50+
51+
bool
52+
SecurityAdapter_getRandomData(uint8_t* data, uint32_t size) {
53+
54+
#if defined(STM32H7xx)
55+
bool success = true;
56+
uint32_t random_data;
57+
58+
HAL_StatusTypeDef status = HAL_OK;
59+
60+
for (uint32_t i = 0u; i < size; ++i) {
61+
62+
if ((i % 4u) == 0u) {
63+
status = HAL_RNG_GenerateRandomNumber(&hrng, &random_data);
64+
}
65+
66+
if (status != HAL_OK) {
67+
success = false;
68+
break;
69+
}
70+
71+
data[i] = (uint8_t)(random_data >> (8u * (i % 4u)));
72+
}
73+
74+
#else
75+
if (data && size) {} //make MISRA happy
76+
bool success = false;
77+
#endif
78+
79+
return success;
80+
81+
}

Bootloader/Inc/binary_update.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void BinaryUpdate_resetJumpAddress(void);
5454
bool BinaryUpdate_checkSkipLoopFlag(void);
5555
void BinaryUpdate_disableLoopFlag(void);
5656
bool BinaryUpdate_erase(uint32_t firmware_size);
57-
bool BinaryUpdate_write(uint8_t* write_buffer, const uint32_t data_length, uint32_t* crc);
57+
bool BinaryUpdate_write(uint8_t* write_buffer, const uint32_t packet_length, uint32_t* crc);
5858
bool BinaryUpdate_finish(void);
5959

6060
#endif /* BOOTLOADER_INC_BINARYUPDATE_H_ */

Bootloader/Inc/board_info.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,11 @@
4040
#include <assert.h>
4141
#include <string.h>
4242
#include <main.h>
43+
#include "security.h"
4344

4445
/* 32 bytes fake board id. If enabled, board and manufacturer id communication will be skipped. */
4546
//#define FAKE_BOARD_ID "NOT_SECURED_MAGIC_STRING_1234567"
4647

47-
typedef enum secureHash_ENUM {
48-
BLAKE2B = 0,
49-
SHA256,
50-
MD5
51-
} secureHash_E;
52-
5348
#define HASH_BOARD_ID_ALGORITHM BLAKE2B //!< Selected algorithm for calculating hashed board id from UUID
5449

5550
#define HASHED_BOARD_ID_SIZE 32U //!< Size for hashed board id binary

Bootloader/Inc/security.h

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/****************************************************************************
2+
*
3+
* Copyright (c) 2022 IMProject Development Team. All rights reserved.
4+
* Authors: Igor Misic <igy1000mb@gmail.com>
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions
8+
* are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright
11+
* notice, this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in
14+
* the documentation and/or other materials provided with the
15+
* distribution.
16+
* 3. Neither the name IMProject nor the names of its contributors may be
17+
* used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30+
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31+
* POSSIBILITY OF SUCH DAMAGE.
32+
*
33+
****************************************************************************/
34+
35+
#ifndef BOOTLOADER_INC_SECURITY_H_
36+
#define BOOTLOADER_INC_SECURITY_H_
37+
38+
#include <stdint.h>
39+
#include <stdbool.h>
40+
#include <assert.h>
41+
#include <string.h>
42+
43+
typedef enum securityAlgorithm_ENUM {
44+
BLAKE2B = 0,
45+
SHA256
46+
} securityAlgorithm_E;
47+
48+
#define MAC_SIZE 16U
49+
#define NONCE_SIZE 24U
50+
#define DATA_SIZE 256U
51+
#define SECURE_PACKET_SIZE 296U // PACKET_SIZE = MAC_SIZE + NONCE_SIZE + DATA_SIZE
52+
53+
#define SERVER_SECURITY_DATA_SIZE 175U
54+
55+
#define SIGNATURE_ALGORITHM BLAKE2B //!< Selected algorithm for calculating public key signature
56+
57+
#define PRESHARED_KEY_SIZE 32U //!< Size for preshared key binary
58+
#define PRESHARED_KEY_SIZE_BASE64_STR 45U //!< Size for preshared key string in base64 format including null-terminator
59+
#define PRESHARED_KEY_BASE64_STR (const char*)("cHJlc2hhcmVkX2tleV9pbl8zMl9ieXRlc19mb3JtYXQ=")
60+
61+
static_assert(strlen((const char*)PRESHARED_KEY_BASE64_STR) == (PRESHARED_KEY_SIZE_BASE64_STR - 1U), "PRESHARED_KEY_BASE64_STR is wrong size");
62+
63+
bool Security_setServerSecurityDataJson(char* buffer, uint16_t buffer_size);
64+
bool Security_getClientSecurityDataJson(char* buffer, uint16_t buffer_size);
65+
bool Security_isSecured(void);
66+
bool Security_decrypt(
67+
const uint8_t mac[MAC_SIZE],
68+
const uint8_t nonce[NONCE_SIZE],
69+
const uint8_t* cipher_data,
70+
uint8_t* plain_data,
71+
const uint16_t data_length);
72+
void Security_wipeKeys(void);
73+
74+
#endif /* BOOTLOADER_INC_SECURITY_H_ */

Bootloader/STM32/Inc/stm32f7xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ extern "C" {
5656
/* #define HAL_LPTIM_MODULE_ENABLED */
5757
/* #define HAL_LTDC_MODULE_ENABLED */
5858
/* #define HAL_QSPI_MODULE_ENABLED */
59-
/* #define HAL_RNG_MODULE_ENABLED */
59+
/* #define HAL_RNG_MODULE_ENABLED */
6060
/* #define HAL_RTC_MODULE_ENABLED */
6161
/* #define HAL_SAI_MODULE_ENABLED */
6262
/* #define HAL_SD_MODULE_ENABLED */

Bootloader/STM32/Inc/stm32h7xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extern "C" {
5858
/* #define HAL_LPTIM_MODULE_ENABLED */
5959
/* #define HAL_LTDC_MODULE_ENABLED */
6060
#define HAL_QSPI_MODULE_ENABLED
61-
/* #define HAL_RNG_MODULE_ENABLED */
61+
#define HAL_RNG_MODULE_ENABLED
6262
/* #define HAL_RTC_MODULE_ENABLED */
6363
/* #define HAL_SAI_MODULE_ENABLED */
6464
/* #define HAL_SD_MODULE_ENABLED */

Bootloader/STM32/Inc/stm32l4xx_hal_conf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ extern "C" {
7979
#define HAL_PCD_MODULE_ENABLED
8080
/*#define HAL_QSPI_MODULE_ENABLED */
8181
/*#define HAL_QSPI_MODULE_ENABLED */
82-
/*#define HAL_RNG_MODULE_ENABLED */
82+
#define HAL_RNG_MODULE_ENABLED
8383
#define HAL_RTC_MODULE_ENABLED
8484
/*#define HAL_SAI_MODULE_ENABLED */
8585
/*#define HAL_SD_MODULE_ENABLED */

Bootloader/STM32/Src/stm32h7xx_hal_msp.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,48 @@ HAL_QSPI_MspDeInit(QSPI_HandleTypeDef* hqspi) {
170170

171171
}
172172

173+
/**
174+
* @brief RNG MSP Initialization
175+
* This function configures the hardware resources used in this example
176+
* @param hrng: RNG handle pointer
177+
* @retval None
178+
*/
179+
void
180+
HAL_RNG_MspInit(RNG_HandleTypeDef* hrng) {
181+
if (hrng->Instance == RNG) {
182+
/* USER CODE BEGIN RNG_MspInit 0 */
183+
184+
/* USER CODE END RNG_MspInit 0 */
185+
/* Peripheral clock enable */
186+
__HAL_RCC_RNG_CLK_ENABLE();
187+
/* USER CODE BEGIN RNG_MspInit 1 */
188+
189+
/* USER CODE END RNG_MspInit 1 */
190+
}
191+
192+
}
193+
194+
/**
195+
* @brief RNG MSP De-Initialization
196+
* This function freeze the hardware resources used in this example
197+
* @param hrng: RNG handle pointer
198+
* @retval None
199+
*/
200+
void
201+
HAL_RNG_MspDeInit(RNG_HandleTypeDef* hrng) {
202+
if (hrng->Instance == RNG) {
203+
/* USER CODE BEGIN RNG_MspDeInit 0 */
204+
205+
/* USER CODE END RNG_MspDeInit 0 */
206+
/* Peripheral clock disable */
207+
__HAL_RCC_RNG_CLK_DISABLE();
208+
/* USER CODE BEGIN RNG_MspDeInit 1 */
209+
210+
/* USER CODE END RNG_MspDeInit 1 */
211+
}
212+
213+
}
214+
173215
/* USER CODE BEGIN 1 */
174216

175217
/* USER CODE END 1 */

0 commit comments

Comments
 (0)