Skip to content

Commit 3327700

Browse files
manchozpennam
authored andcommitted
Use upstream ArduinoBearSSL
1 parent 22f3a2d commit 3327700

File tree

8 files changed

+33
-29
lines changed

8 files changed

+33
-29
lines changed

examples/utility/SelfProvisioning/ECCX08Cert.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <ArduinoIoTCloud.h>
2323
#include <ArduinoECCX08.h>
2424
#include "ECCX08Cert.h"
25-
#include "tls/utility/SHA256.h"
25+
#include <SHA256.h>
2626

2727
/******************************************************************************
2828
* DEFINE
@@ -188,13 +188,14 @@ String ECCX08CertClass::endCSR() {
188188
*out++ = 0xa0;
189189
*out++ = 0x00;
190190

191-
SHA256 sha256;
192-
byte csrInfoSha256[64];
191+
SHA256Class sha256;
192+
byte csrInfoSha256[SHA256_DIGEST_SIZE];
193193
byte signature[64];
194194

195-
sha256.begin();
196-
sha256.update(csrInfo, csrInfoHeaderLen + csrInfoLen);
197-
sha256.finalize(csrInfoSha256);
195+
sha256.beginHash();
196+
sha256.write(csrInfo, csrInfoHeaderLen + csrInfoLen);
197+
sha256.endHash();
198+
sha256.readBytes(csrInfoSha256, SHA256_DIGEST_SIZE);
198199

199200
if (!ECCX08.ecSign(_keySlot, csrInfoSha256, signature)) {
200201
return "";

src/ArduinoIoTCloudTCP.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ ArduinoIoTCloudTCP::ArduinoIoTCloudTCP()
8888
, _mqtt_data_len{0}
8989
, _mqtt_data_request_retransmit{false}
9090
#ifdef BOARD_HAS_ECCX08
91-
, _sslClient(nullptr, ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM, getTime)
91+
, _sslClient(nullptr, ArduinoIoTCloudTrustAnchor, ArduinoIoTCloudTrustAnchor_NUM)
9292
#endif
9393
#ifdef BOARD_HAS_SECRET_KEY
9494
, _password("")
@@ -160,6 +160,7 @@ int ArduinoIoTCloudTCP::begin(bool const enable_watchdog, String brokerAddress,
160160
#endif
161161

162162
#if defined(BOARD_HAS_ECCX08)
163+
ArduinoBearSSL.onGetTime(getTime);
163164
_sslClient.setClient(_connection->getClient());
164165
#elif defined(ARDUINO_PORTENTA_C33)
165166
_sslClient.setClient(_connection->getClient());

src/ArduinoIoTCloudTCP.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include <ArduinoIoTCloud.h>
2828

2929
#ifdef BOARD_HAS_ECCX08
30-
#include "tls/BearSSLClient.h"
30+
#include <ArduinoBearSSL.h>
3131
#include "tls/utility/CryptoUtil.h"
3232
#elif defined(BOARD_ESP)
3333
#include <WiFiClientSecure.h>

src/tls/utility/CryptoUtil.cpp

+5-4
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ int CryptoUtil::buildCSR(ArduinoIoTCloudCertClass & cert, const CryptoSlot keySl
7575
}
7676

7777
/* compute CSR SHA256 */
78-
SHA256 sha256;
78+
SHA256Class sha256;
7979
byte sha256buf[CRYPTO_SHA256_BUFFER_LENGTH];
80-
sha256.begin();
81-
sha256.update(cert.bytes(), cert.length());
82-
sha256.finalize(sha256buf);
80+
sha256.beginHash();
81+
sha256.write(cert.bytes(), cert.length());
82+
sha256.endHash();
83+
sha256.readBytes(sha256buf, CRYPTO_SHA256_BUFFER_LENGTH);
8384

8485
if (!_crypto.ecSign(static_cast<int>(keySlot), sha256buf, signature)) {
8586
return 0;

src/utility/ota/FlashSHA256.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
#include "FlashSHA256.h"
2626

27-
#include "../../tls/utility/SHA256.h"
27+
#include <SHA256.h>
2828

2929
#include <Arduino_DebugUtils.h>
3030

@@ -38,11 +38,11 @@
3838

3939
String FlashSHA256::calc(uint32_t const start_addr, uint32_t const max_flash_size)
4040
{
41-
SHA256 sha256;
41+
SHA256Class sha256;
4242
uint8_t chunk [FLASH_READ_CHUNK_SIZE],
4343
next_chunk[FLASH_READ_CHUNK_SIZE];
4444

45-
sha256.begin();
45+
sha256.beginHash();
4646

4747
/* Read the first two chunks of flash. */
4848
uint32_t flash_addr = start_addr;
@@ -75,27 +75,28 @@ String FlashSHA256::calc(uint32_t const start_addr, uint32_t const max_flash_siz
7575
break;
7676
}
7777
/* Update with the remaining bytes. */
78-
sha256.update(chunk, valid_bytes_in_chunk);
78+
sha256.write(chunk, valid_bytes_in_chunk);
7979
bytes_read += valid_bytes_in_chunk;
8080
break;
8181
}
8282

8383
/* We've read a normal segment with the next segment not containing
8484
* any erased elements, just update the SHA256 hash calculation.
8585
*/
86-
sha256.update(chunk, FLASH_READ_CHUNK_SIZE);
86+
sha256.write(chunk, FLASH_READ_CHUNK_SIZE);
8787
bytes_read += FLASH_READ_CHUNK_SIZE;
8888

8989
/* Copy next_chunk to chunk. */
9090
memcpy(chunk, next_chunk, FLASH_READ_CHUNK_SIZE);
9191
}
9292

9393
/* Retrieve the final hash string. */
94-
uint8_t sha256_hash[SHA256::HASH_SIZE] = {0};
95-
sha256.finalize(sha256_hash);
94+
uint8_t sha256_hash[SHA256_DIGEST_SIZE] = {0};
95+
sha256.endHash();
96+
sha256.readBytes(sha256_hash, SHA256_DIGEST_SIZE);
9697
String sha256_str;
9798
std::for_each(sha256_hash,
98-
sha256_hash + SHA256::HASH_SIZE,
99+
sha256_hash + SHA256_DIGEST_SIZE,
99100
[&sha256_str](uint8_t const elem)
100101
{
101102
char buf[4];

src/utility/ota/OTA-esp32.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
#include "OTA.h"
2727
#include <Arduino_DebugUtils.h>
2828
#include <Arduino_ESP32_OTA.h>
29-
#include "tls/utility/SHA256.h"
29+
#include <SHA256.h>
3030

3131
#include <esp_ota_ops.h>
3232

@@ -105,11 +105,11 @@ String esp32_getOTAImageSHA256()
105105
free(b);
106106

107107
/* Retrieve the final hash string. */
108-
uint8_t sha256_hash[SHA256::HASH_SIZE] = {0};
108+
uint8_t sha256_hash[SHA256_DIGEST_SIZE] = {0};
109109
sha256.finalize(sha256_hash);
110110
String sha256_str;
111111
std::for_each(sha256_hash,
112-
sha256_hash + SHA256::HASH_SIZE,
112+
sha256_hash + SHA256_DIGEST_SIZE,
113113
[&sha256_str](uint8_t const elem)
114114
{
115115
char buf[4];

src/utility/ota/OTA-portenta-h7.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#include <stm32h7xx_hal_rtc_ex.h>
3333

34-
#include "tls/utility/SHA256.h"
34+
#include <SHA256.h>
3535

3636
#include "../watchdog/Watchdog.h"
3737

@@ -128,11 +128,11 @@ String portenta_h7_getOTAImageSHA256()
128128
sha256.update(reinterpret_cast<uint8_t *>(&b), sizeof(b));
129129
}
130130
/* Retrieve the final hash string. */
131-
uint8_t sha256_hash[SHA256::HASH_SIZE] = {0};
131+
uint8_t sha256_hash[SHA256_DIGEST_SIZE] = {0};
132132
sha256.finalize(sha256_hash);
133133
String sha256_str;
134134
std::for_each(sha256_hash,
135-
sha256_hash + SHA256::HASH_SIZE,
135+
sha256_hash + SHA256_DIGEST_SIZE,
136136
[&sha256_str](uint8_t const elem)
137137
{
138138
char buf[4];

src/utility/ota/OTA-unor4.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#include "OTAUpdate.h"
2727
#include <Arduino_DebugUtils.h>
28-
#include "tls/utility/SHA256.h"
28+
#include <SHA256.h>
2929
#include "fsp_common_api.h"
3030
#include "r_flash_lp.h"
3131
#include "WiFiS3.h"
@@ -159,11 +159,11 @@ String unor4_getOTAImageSHA256()
159159
unor4_codeFlashClose(&ctrl);
160160

161161
/* Retrieve the final hash string. */
162-
uint8_t sha256_hash[SHA256::HASH_SIZE] = {0};
162+
uint8_t sha256_hash[SHA256_DIGEST_SIZE] = {0};
163163
sha256.finalize(sha256_hash);
164164
String sha256_str;
165165
std::for_each(sha256_hash,
166-
sha256_hash + SHA256::HASH_SIZE,
166+
sha256_hash + SHA256_DIGEST_SIZE,
167167
[&sha256_str](uint8_t const elem)
168168
{
169169
char buf[4];

0 commit comments

Comments
 (0)