39 #include <user_interface.h>
42 #include <esp_system.h>
70 mCreatorId = UNIOT_CREATOR_ID;
71 mDeviceId = _calcDeviceId();
74 if (mPrivateKey.size() == 0) {
75 _generatePrivateKey();
90 object().
put(
"account", mOwnerId.c_str());
91 object().
put(
"private_key", mPrivateKey.raw(), mPrivateKey.size());
119 if (mOwnerId !=
id) {
120 mOnwerChanged =
true;
135 return mOnwerChanged;
139 mOnwerChanged =
false;
178 return ESP.getChipId();
180 uint64_t mac = ESP.getEfuseMac();
181 return (uint32_t)(mac >> 32);
191 return mPublicKeyRaw;
203 uint8_t signature[64];
204 uint8_t publicKey[32];
205 Ed25519::derivePublicKey(publicKey, mPrivateKey.raw());
206 Ed25519::sign(signature, mPrivateKey.raw(), publicKey, data.
raw(), data.
size());
207 return Bytes(signature,
sizeof(signature));
216 return COSEAlgorithm::EdDSA;
227 String _calcDeviceId() {
229 char macStr[13] = {0};
231 wifi_get_macaddr(STATION_IF, mac);
233 esp_read_mac(mac, ESP_MAC_WIFI_STA);
235 for (uint8_t i = 0; i < 6; i++)
236 sprintf(macStr + i * 2,
"%02x", mac[i]);
238 return String(macStr);
246 void _generatePrivateKey() {
248 RNG.begin(String(
"uniot::entropy::" + mCreatorId +
"::" + mDeviceId).c_str());
250 uint8_t privateKey[32];
251 Ed25519::generatePrivateKey(privateKey);
252 mPrivateKey = Bytes(privateKey,
sizeof(privateKey));
260 void _derivePublicKey() {
261 uint8_t publicKey[32];
262 Ed25519::derivePublicKey(publicKey, mPrivateKey.raw());
263 mPublicKeyRaw = Bytes(publicKey,
sizeof(publicKey));
264 mPublicKey = mPublicKeyRaw.toHexString();
const uint8_t * raw() const
Gets a const pointer to the raw byte array.
Definition Bytes.h:235
size_t size() const
Gets the size of the byte array.
Definition Bytes.h:303
Bytes getBytes(int key) const
Get binary data at a specific integer key.
Definition CBORObject.h:461
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:170
String getString(int key) const
Get a string value at a specific integer key.
Definition CBORObject.h:419
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:92
virtual bool store() override
Store the CBOR object to the filesystem.
Definition CBORStorage.h:74
CBORStorage(const String &path)
Constructs a new CBORStorage object.
Definition CBORStorage.h:46
CBORObject & object()
Get access to the underlying CBORObject.
Definition CBORStorage.h:60
const String & getCreatorId() const
Gets the creator ID.
Definition Credentials.h:147
const String & getPublicKey() const
Gets the device's public key as a hexadecimal string.
Definition Credentials.h:165
const String & getDeviceId() const
Gets the unique device ID.
Definition Credentials.h:156
Credentials()
Constructor that initializes device credentials.
Definition Credentials.h:69
const String & getOwnerId() const
Gets the current owner ID.
Definition Credentials.h:130
uint32_t getShortDeviceId() const
Gets a shorter unique identifier for the device.
Definition Credentials.h:176
virtual Bytes sign(const Bytes &data) const override
Implements ICOSESigner interface to sign data.
Definition Credentials.h:202
void resetOwnerChanged()
Definition Credentials.h:138
bool isOwnerChanged() const
Definition Credentials.h:134
virtual bool store() override
Stores credentials to persistent storage.
Definition Credentials.h:89
virtual bool restore() override
Restores credentials from persistent storage.
Definition Credentials.h:103
virtual Bytes keyId() const override
Implements ICOSESigner interface to provide key ID.
Definition Credentials.h:190
virtual COSEAlgorithm signerAlgorithm() const override
Implements ICOSESigner interface to specify the signing algorithm.
Definition Credentials.h:215
void setOwnerId(const String &id)
Sets the owner ID of the device.
Definition Credentials.h:118
Interface for CBOR Object Signing and Encryption (COSE) signing operations.
Definition ICOSESigner.h:35
#define UNIOT_LOG_ERROR(...)
Log an ERROR level message Used for critical errors that may prevent normal operation....
Definition Logger.h:226
COSEAlgorithm
Cryptographic algorithm identifiers for COSE.
Definition COSE.h:84
Contains all classes and functions related to the Uniot Core.