Skip to content

Commit 7695764

Browse files
committed
Add primitives to compute provisioningID
1 parent c4dee69 commit 7695764

File tree

8 files changed

+327
-0
lines changed

8 files changed

+327
-0
lines changed

src/bpid/bpid.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include "bpid.h"
12+
13+
namespace arduino { namespace bpid {
14+
15+
static String hexEncode(uint8_t* in, uint32_t size) {
16+
String out;
17+
out.reserve((size * 2) + 1);
18+
19+
char *ptr = out.begin();
20+
for (uint32_t i = 0; i < size; i++) {
21+
ptr += sprintf(ptr, "%02X", in[i]);
22+
}
23+
return String(out.c_str());
24+
}
25+
26+
bool get(uint8_t* in, uint32_t size) {
27+
if (size < BOARD_PROVISIONING_ID_SIZE) {
28+
return false;
29+
}
30+
uint8_t offset = 0;
31+
if (!arduino::ucid::get(&in[offset], size)) {
32+
return false;
33+
}
34+
offset += arduino::ucid::UC_UID_SIZE;
35+
if (!arduino::mac::get(&in[offset], size - offset)) {
36+
return false;
37+
}
38+
offset += arduino::mac::IFACE_MAC_SIZE;
39+
if (!arduino::csn::get(&in[offset], size - offset)) {
40+
return false;
41+
}
42+
return true;
43+
}
44+
45+
String get() {
46+
uint8_t data[BOARD_PROVISIONING_ID_SIZE];
47+
if (!get(data, sizeof(data))) {
48+
return String("");
49+
}
50+
return hexEncode(data, sizeof(data));
51+
}
52+
53+
}} // arduino::bpid

src/bpid/bpid.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#pragma once
12+
13+
#include <Arduino.h>
14+
#include "ucid.h"
15+
#include "mac.h"
16+
#include "csn.h"
17+
18+
namespace arduino { namespace bpid {
19+
/*
20+
* This library contains the methods to get board provisioning id
21+
*/
22+
23+
constexpr int BOARD_PROVISIONING_ID_SIZE = arduino::ucid::UC_UID_SIZE +
24+
arduino::mac::IFACE_MAC_SIZE +
25+
arduino::csn::CRYPTO_SN_SIZE;
26+
27+
bool get(uint8_t* in, uint32_t size);
28+
String get();
29+
30+
}} // arduino::bpid

src/bpid/csn.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include "csn.h"
12+
13+
namespace arduino { namespace csn {
14+
15+
bool get(uint8_t *in, uint32_t size) {
16+
if (size < CRYPTO_SN_SIZE) {
17+
return false;
18+
}
19+
SecureElement se;
20+
if (!se.begin() || !se.serialNumber(in)) {
21+
return false;
22+
}
23+
return true;
24+
}
25+
26+
}} // arduino::csn

src/bpid/csn.h

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#pragma once
12+
13+
#include <Arduino.h>
14+
#include <Arduino_SecureElement.h>
15+
16+
namespace arduino { namespace csn {
17+
/*
18+
* This library contains the methods to get board microcontroller id
19+
*/
20+
21+
#if defined(ARDUINO_NANO_RP2040_CONNECT) || \
22+
defined(ARDUINO_SAMD_MKRWIFI1010) || \
23+
defined(ARDUINO_SAMD_NANO_33_IOT) || \
24+
defined(ARDUINO_PORTENTA_H7_M7) || \
25+
defined(ARDUINO_OPTA) || \
26+
defined(ARDUINO_GIGA)
27+
constexpr int CRYPTO_SN_SIZE = 12;
28+
#elif defined(ARDUINO_PORTENTA_C33) || \
29+
defined(ARDUINO_NICLA_VISION)
30+
constexpr int CRYPTO_SN_SIZE = SE05X_SN_LENGTH;
31+
#elif defined(ARDUINO_UNOR4_WIFI)
32+
constexpr int CRYPTO_SN_SIZE = 6;
33+
#else
34+
#error "Unknown board"
35+
#endif
36+
37+
bool get(uint8_t *in, uint32_t size);
38+
39+
}} // arduino::csn

src/bpid/mac.cpp

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include "mac.h"
12+
13+
namespace arduino { namespace mac {
14+
15+
bool get(uint8_t *in, uint32_t size) {
16+
if (size < IFACE_MAC_SIZE) {
17+
return false;
18+
}
19+
#if defined(ARDUINO_SAMD_MKRWIFI1010) || \
20+
defined(ARDUINO_SAMD_NANO_33_IOT)
21+
WiFi.macAddress(in);
22+
#elif defined(ARDUINO_PORTENTA_H7_M7) || \
23+
defined(ARDUINO_NICLA_VISION) || \
24+
defined(ARDUINO_GIGA)
25+
WiFi.macAddress(in);
26+
#elif defined(ARDUINO_PORTENTA_C33) || \
27+
defined(ARDUINO_UNOR4_WIFI)
28+
WiFi.macAddress(in);
29+
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
30+
WiFi.macAddress(in);
31+
#elif defined(ARDUINO_OPTA)
32+
Ethernet.MACAddress(in);
33+
#endif
34+
return true;
35+
}
36+
37+
}} // arduino::mac

src/bpid/mac.h

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#pragma once
12+
13+
#if defined(ARDUINO_NANO_RP2040_CONNECT) || \
14+
defined(ARDUINO_SAMD_MKRWIFI1010) || \
15+
defined(ARDUINO_SAMD_NANO_33_IOT) || \
16+
defined(ARDUINO_PORTENTA_H7_M7) || \
17+
defined(ARDUINO_NICLA_VISION) || \
18+
defined(ARDUINO_GIGA)
19+
#include <WiFi.h>
20+
#define IFACE_MAC_ADDR_LENGTH WL_MAC_ADDR_LENGTH
21+
#elif defined(ARDUINO_PORTENTA_C33)
22+
#include <WiFiC3.h>
23+
#define IFACE_MAC_ADDR_LENGTH WL_MAC_ADDR_LENGTH
24+
#elif defined(ARDUINO_UNOR4_WIFI)
25+
#include <WiFi.h>
26+
#define IFACE_MAC_ADDR_LENGTH WL_MAC_ADDR_LENGTH
27+
#elif defined(ARDUINO_OPTA)
28+
#include <Ethernet.h>
29+
#define IFACE_MAC_ADDR_LENGTH 6
30+
#else
31+
#error "Unknown board"
32+
#endif
33+
34+
namespace arduino { namespace mac {
35+
/*
36+
* This library contains the methods to get board mac address
37+
* ARDUINO_NANO_RP2040_CONNECT: reversed
38+
* ARDUINO_SAMD_MKRWIFI1010: reversed
39+
* ARDUINO_SAMD_NANO_33_IOT: reversed
40+
* ARDUINO_PORTENTA_H7_M7: WiFi.setTimeout(0);WiFi.begin("In33dm4c4ddr35", "In33dm4c4ddr35", ENC_TYPE_TKIP) reversed
41+
* ARDUINO_NICLA_VISION:
42+
* ARDUINO_GIGA:
43+
* ARDUINO_PORTENTA_C33: Interface.begin() not reversed
44+
* ARDUINO_UNOR4_WIFI: not reversed
45+
* ARDUINO_OPTA: Ethernet.begin(NULL,0,0); reversed
46+
*/
47+
48+
constexpr int IFACE_MAC_SIZE = IFACE_MAC_ADDR_LENGTH;
49+
50+
bool get(uint8_t *in, uint32_t size);
51+
52+
}} // arduino::mac

src/bpid/ucid.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#include "ucid.h"
12+
13+
namespace arduino { namespace ucid {
14+
15+
bool get(uint8_t *in, uint32_t size) {
16+
if (size < UC_UID_SIZE) {
17+
return false;
18+
}
19+
#if defined(ARDUINO_SAMD_MKRWIFI1010) || \
20+
defined(ARDUINO_SAMD_NANO_33_IOT)
21+
(*(uint32_t*)(&in[0x0])) = __builtin_bswap32(*(volatile uint32_t*)(0x0080A00C));
22+
(*(uint32_t*)(&in[0x4])) = __builtin_bswap32(*(volatile uint32_t*)(0x0080A040));
23+
(*(uint32_t*)(&in[0x8])) = __builtin_bswap32(*(volatile uint32_t*)(0x0080A044));
24+
(*(uint32_t*)(&in[0xC])) = __builtin_bswap32(*(volatile uint32_t*)(0x0080A048));
25+
#elif defined(ARDUINO_PORTENTA_H7_M7) || \
26+
defined(ARDUINO_NICLA_VISION) || \
27+
defined(ARDUINO_OPTA) || \
28+
defined(ARDUINO_GIGA)
29+
(*(uint32_t*)(&in[0x0])) = __builtin_bswap32(HAL_GetUIDw0());
30+
(*(uint32_t*)(&in[0x4])) = __builtin_bswap32(HAL_GetUIDw1());
31+
(*(uint32_t*)(&in[0x8])) = __builtin_bswap32(HAL_GetUIDw2());
32+
#elif defined(ARDUINO_PORTENTA_C33) || \
33+
defined(ARDUINO_UNOR4_WIFI)
34+
const bsp_unique_id_t* t = R_BSP_UniqueIdGet();
35+
(*(uint32_t*)(&in[0x0])) = __builtin_bswap32(t->unique_id_words[0x0]);
36+
(*(uint32_t*)(&in[0x4])) = __builtin_bswap32(t->unique_id_words[0x1]);
37+
(*(uint32_t*)(&in[0x8])) = __builtin_bswap32(t->unique_id_words[0x2]);
38+
(*(uint32_t*)(&in[0xC])) = __builtin_bswap32(t->unique_id_words[0x3]);
39+
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
40+
uint8_t id[UC_UID_SIZE];
41+
flash_get_unique_id(id);
42+
(*(uint32_t*)(&in[0x0])) = __builtin_bswap32(*(uint32_t*)&id[0x0]);
43+
(*(uint32_t*)(&in[0x4])) = __builtin_bswap32(*(uint32_t*)&id[0x4]);
44+
#endif
45+
return true;
46+
}
47+
48+
}} // arduino::ucid

src/bpid/ucid.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
This file is part of the Arduino_CloudUtils library.
3+
4+
Copyright (c) 2024 Arduino SA
5+
6+
This Source Code Form is subject to the terms of the Mozilla Public
7+
License, v. 2.0. If a copy of the MPL was not distributed with this
8+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
11+
#pragma once
12+
13+
#include <Arduino.h>
14+
15+
namespace arduino { namespace ucid {
16+
/*
17+
* This library contains the methods to get board microcontroller id
18+
*/
19+
20+
#if defined(ARDUINO_SAMD_MKRWIFI1010) || \
21+
defined(ARDUINO_SAMD_NANO_33_IOT)
22+
constexpr int UC_UID_SIZE = 16;
23+
#elif defined(ARDUINO_PORTENTA_H7_M7) || \
24+
defined(ARDUINO_NICLA_VISION) || \
25+
defined(ARDUINO_OPTA) || \
26+
defined(ARDUINO_GIGA)
27+
constexpr int UC_UID_SIZE = 12;
28+
#elif defined(ARDUINO_PORTENTA_C33) || \
29+
defined(ARDUINO_UNOR4_WIFI)
30+
constexpr int UC_UID_SIZE = 16;
31+
#elif defined(ARDUINO_NANO_RP2040_CONNECT)
32+
extern "C" {
33+
#include "hardware/flash.h"
34+
}
35+
constexpr int UC_UID_SIZE = FLASH_UNIQUE_ID_SIZE_BYTES;
36+
#else
37+
#error "Unknown board"
38+
#endif
39+
40+
bool get(uint8_t *in, uint32_t size);
41+
42+
}} // arduino::ucid

0 commit comments

Comments
 (0)