Skip to content

Commit 7f72d75

Browse files
committed
CryptoUtil: include in the build and add support for devices using SE050 crypto
1 parent 4d34ba1 commit 7f72d75

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/tls/utility/CryptoUtil.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
#include <AIoTC_Config.h>
2323

24-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
24+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2525

2626
#include "CryptoUtil.h"
2727
#include "SHA256.h"
@@ -36,7 +36,11 @@
3636
* CTOR/DTOR
3737
**************************************************************************************/
3838
CryptoUtil::CryptoUtil()
39+
#if defined(BOARD_HAS_SE050)
40+
: _crypto {SE05X}
41+
#else
3942
: _crypto {ECCX08}
43+
#endif
4044
{
4145

4246
}
@@ -133,18 +137,35 @@ int CryptoUtil::writeDeviceId(String & device_id, const CryptoSlot device_id_slo
133137

134138
int CryptoUtil::writeCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot)
135139
{
140+
#if defined(BOARD_HAS_SE050)
141+
if (!_crypto.writeSlot(static_cast<int>(certSlot), cert.bytes(), cert.length())) {
142+
return 0;
143+
}
144+
#else
136145
if (!_crypto.writeSlot(static_cast<int>(certSlot), cert.compressedCertSignatureAndDatesBytes(), cert.compressedCertSignatureAndDatesLength())) {
137146
return 0;
138147
}
139148

140149
if (!_crypto.writeSlot(static_cast<int>(certSlot) + 1, cert.compressedCertSerialAndAuthorityKeyIdBytes(), cert.compressedCertSerialAndAuthorityKeyIdLenght())) {
141150
return 0;
142151
}
152+
#endif
143153
return 1;
144154
}
145155

146156
int CryptoUtil::readCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot)
147157
{
158+
#if defined(BOARD_HAS_SE050)
159+
byte derBuffer[CRYPTO_CERT_BUFFER_LENGTH];
160+
size_t derLen;
161+
if (!_crypto.readBinaryObject(static_cast<int>(certSlot), derBuffer, sizeof(derBuffer), &derLen)) {
162+
return 0;
163+
}
164+
165+
if (!cert.importCert(derBuffer, derLen)) {
166+
return 0;
167+
}
168+
#else
148169
String deviceId;
149170
byte publicKey[CERT_PUBLIC_KEY_LENGTH];
150171

@@ -183,7 +204,8 @@ int CryptoUtil::readCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certS
183204
if (!cert.signCert()) {
184205
return 0;
185206
}
207+
#endif
186208
return 1;
187209
}
188210

189-
#endif /* (BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) */
211+
#endif /* (BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050) */

src/tls/utility/CryptoUtil.h

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,34 @@
2424

2525
#include <AIoTC_Config.h>
2626

27-
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08)
27+
#if defined(BOARD_HAS_ECCX08) || defined(BOARD_HAS_OFFLOADED_ECCX08) || defined(BOARD_HAS_SE050)
2828
#include <Arduino.h>
2929
#include "Cert.h"
30+
31+
#if defined(BOARD_HAS_SE050)
32+
#include <SE05X.h>
33+
#else
3034
#include <ArduinoECCX08.h>
35+
#endif
36+
37+
/******************************************************************************
38+
* DEFINE
39+
******************************************************************************/
40+
#if defined(BOARD_HAS_SE050)
41+
#define CRYPTO_SLOT_OFFSET 100
42+
#else
43+
#define CRYPTO_SLOT_OFFSET 0
44+
#endif
3145

3246
/******************************************************************************
3347
TYPEDEF
3448
******************************************************************************/
3549
enum class CryptoSlot : int
3650
{
37-
Key = 0,
38-
CompressedCertificate = 10,
39-
SerialNumberAndAuthorityKeyIdentifier = 11,
40-
DeviceId = 12
51+
Key = (0 + CRYPTO_SLOT_OFFSET),
52+
CompressedCertificate = (10 + CRYPTO_SLOT_OFFSET),
53+
SerialNumberAndAuthorityKeyIdentifier = (11 + CRYPTO_SLOT_OFFSET),
54+
DeviceId = (12 + CRYPTO_SLOT_OFFSET)
4155
};
4256

4357
/******************************************************************************
@@ -64,10 +78,14 @@ class CryptoUtil
6478
int readCert(ArduinoIoTCloudCertClass & cert, const CryptoSlot certSlot);
6579

6680
private:
81+
#if defined(BOARD_HAS_SE050)
82+
SE05XClass & _crypto;
83+
#else
6784
ECCX08Class & _crypto;
85+
#endif
6886

6987
};
7088

71-
#endif /* BOARD_HAS_ECCX08 || BOARD_HAS_OFFLOADED_ECCX08 */
89+
#endif /* BOARD_HAS_ECCX08 || BOARD_HAS_OFFLOADED_ECCX08 || BOARD_HAS_SE050 */
7290

7391
#endif /* ARDUINO_IOT_CLOUD_UTILITY_CRYPTO_CRYPTO_UTIL_H_ */

0 commit comments

Comments
 (0)