39 #include <user_interface.h>
42 #include <esp_system.h>
74 mCreatorId = UNIOT_CREATOR_ID;
75 mDeviceId = _calcDeviceId();
79 _generatePrivateKey();
94 object().
put(
"account", mOwnerId.c_str());
95 object().
put(
"private_key", mPrivateKey.raw(), mPrivateKey.size());
116 UNIOT_LOG_ERROR(
"stored private key is %u bytes, expected %u, discarding it",
132 if (mOwnerId !=
id) {
133 mOnwerChanged =
true;
148 return mOnwerChanged;
152 mOnwerChanged =
false;
191 return ESP.getChipId();
193 uint64_t mac = ESP.getEfuseMac();
194 return (uint32_t)(mac >> 32);
204 return mPublicKeyRaw;
224 Ed25519::sign(signature, mPrivateKey.raw(), mPublicKeyRaw.raw(), data.
raw(), data.
size());
225 return Bytes(signature,
sizeof(signature));
234 return COSEAlgorithm::EdDSA;
245 String _calcDeviceId() {
247 char macStr[13] = {0};
249 wifi_get_macaddr(STATION_IF, mac);
251 esp_read_mac(mac, ESP_MAC_WIFI_STA);
253 for (uint8_t i = 0; i < 6; i++)
254 sprintf(macStr + i * 2,
"%02x", mac[i]);
256 return String(macStr);
264 void _generatePrivateKey() {
266 RNG.begin(String(
"uniot::entropy::" + mCreatorId +
"::" + mDeviceId).c_str());
269 Ed25519::generatePrivateKey(privateKey);
270 mPrivateKey = Bytes(privateKey,
sizeof(privateKey));
278 void _derivePublicKey() {
285 Ed25519::derivePublicKey(publicKey, mPrivateKey.raw());
286 mPublicKeyRaw = Bytes(publicKey,
sizeof(publicKey));
287 mPublicKey = mPublicKeyRaw.toHexString();
const uint8_t * raw() const
Gets a const pointer to the raw byte array.
Definition Bytes.h:270
size_t size() const
Gets the size of the byte array.
Definition Bytes.h:343
Bytes getBytes(int key) const
Get binary data at a specific integer key.
Definition CBORObject.h:471
CBORObject & put(int key, int value)
Put an integer value at a specific integer key.
Definition CBORObject.h:180
String getString(int key) const
Get a string value at a specific integer key.
Definition CBORObject.h:429
virtual bool restore() override
Restore the CBOR object from the filesystem.
Definition CBORStorage.h:102
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:160
const String & getPublicKey() const
Gets the device's public key as a hexadecimal string.
Definition Credentials.h:178
const String & getDeviceId() const
Gets the unique device ID.
Definition Credentials.h:169
static constexpr size_t SIGNATURE_SIZE
Size of an Ed25519 signature.
Definition Credentials.h:65
Credentials()
Constructor that initializes device credentials.
Definition Credentials.h:73
const String & getOwnerId() const
Gets the current owner ID.
Definition Credentials.h:143
static constexpr size_t PRIVATE_KEY_SIZE
Size of an Ed25519 private key.
Definition Credentials.h:63
uint32_t getShortDeviceId() const
Gets a shorter unique identifier for the device.
Definition Credentials.h:189
virtual Bytes sign(const Bytes &data) const override
Implements ICOSESigner interface to sign data.
Definition Credentials.h:215
void resetOwnerChanged()
Definition Credentials.h:151
static constexpr size_t PUBLIC_KEY_SIZE
Size of an Ed25519 public key.
Definition Credentials.h:64
bool isOwnerChanged() const
Definition Credentials.h:147
virtual bool store() override
Stores credentials to persistent storage.
Definition Credentials.h:93
virtual bool restore() override
Restores credentials from persistent storage.
Definition Credentials.h:107
virtual Bytes keyId() const override
Implements ICOSESigner interface to provide key ID.
Definition Credentials.h:203
virtual COSEAlgorithm signerAlgorithm() const override
Implements ICOSESigner interface to specify the signing algorithm.
Definition Credentials.h:233
void setOwnerId(const String &id)
Sets the owner ID of the device.
Definition Credentials.h:131
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:273
COSEAlgorithm
Cryptographic algorithm identifiers for COSE.
Definition COSE.h:84
Contains all classes and functions related to the Uniot Core.