mirror of
https://github.com/esphome/esphome.git
synced 2025-09-29 08:32:26 +01:00
optimize
This commit is contained in:
@@ -536,19 +536,16 @@ bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string
|
|||||||
const size_t hex_size = hasher->get_size() * 2; // Hex is twice the byte size
|
const size_t hex_size = hasher->get_size() * 2; // Hex is twice the byte size
|
||||||
const size_t nonce_len = hasher->get_size() / 4; // Nonce is 1/4 of hash size in bytes
|
const size_t nonce_len = hasher->get_size() / 4; // Nonce is 1/4 of hash size in bytes
|
||||||
|
|
||||||
// Use the provided buffer for all hex operations
|
// Use the provided buffer for all operations
|
||||||
|
|
||||||
// Small stack buffer for nonce seed bytes
|
|
||||||
uint8_t nonce_bytes[8]; // Max 8 bytes (2 x uint32_t for SHA256)
|
|
||||||
|
|
||||||
hasher->init();
|
|
||||||
|
|
||||||
// Generate nonce seed bytes using random_bytes
|
// Generate nonce seed bytes using random_bytes
|
||||||
if (!random_bytes(nonce_bytes, nonce_len)) {
|
if (!random_bytes(reinterpret_cast<uint8_t *>(buf), nonce_len)) {
|
||||||
this->log_auth_warning_(LOG_STR("Random bytes generation failed"), name);
|
this->log_auth_warning_(LOG_STR("Random bytes generation failed"), name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
hasher->add(nonce_bytes, nonce_len);
|
|
||||||
|
hasher->init();
|
||||||
|
hasher->add(buf, nonce_len);
|
||||||
hasher->calculate();
|
hasher->calculate();
|
||||||
|
|
||||||
// Prepare buffer: auth_type (1 byte) + nonce (hex_size bytes)
|
// Prepare buffer: auth_type (1 byte) + nonce (hex_size bytes)
|
||||||
@@ -571,31 +568,37 @@ bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string
|
|||||||
hasher->add(buf + 1, hex_size);
|
hasher->add(buf + 1, hex_size);
|
||||||
|
|
||||||
// Read cnonce and add to hash
|
// Read cnonce and add to hash
|
||||||
if (!this->readall_(reinterpret_cast<uint8_t *>(buf), hex_size)) {
|
if (!this->readall_(reinterpret_cast<uint8_t *>(buf), hex_size * 2)) {
|
||||||
this->log_auth_warning_(LOG_STR("Reading cnonce"), name);
|
this->log_auth_warning_(LOG_STR("Reading cnonce response"), name);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
buf[hex_size] = '\0';
|
|
||||||
ESP_LOGV(TAG, "Auth: %s CNonce is %s", LOG_STR_ARG(name), buf);
|
|
||||||
|
|
||||||
hasher->add(buf, hex_size);
|
// Response is located after CNonce in the buffer
|
||||||
|
const char *response = buf + hex_size;
|
||||||
|
|
||||||
|
hasher->add(buf, hex_size); // add CNonce in binary
|
||||||
hasher->calculate();
|
hasher->calculate();
|
||||||
|
|
||||||
// Log expected result (digest is already in hasher)
|
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||||
hasher->get_hex(buf);
|
char log_buf[hex_size + 1];
|
||||||
buf[hex_size] = '\0';
|
// Log CNonce for debugging
|
||||||
ESP_LOGV(TAG, "Auth: %s Result is %s", LOG_STR_ARG(name), buf);
|
memcpy(log_buf, buf, hex_size); // Save CNonce for logging
|
||||||
|
log_buf[hex_size] = '\0';
|
||||||
|
ESP_LOGV(TAG, "Auth: %s CNonce is %s", LOG_STR_ARG(name), log_buf);
|
||||||
|
|
||||||
// Read response into the buffer
|
// Log computed hash for debugging
|
||||||
if (!this->readall_(reinterpret_cast<uint8_t *>(buf), hex_size)) {
|
hasher->get_hex(log_buf);
|
||||||
this->log_auth_warning_(LOG_STR("Reading response"), name);
|
log_buf[hex_size] = '\0';
|
||||||
return false;
|
ESP_LOGV(TAG, "Auth: %s Result is %s", LOG_STR_ARG(name), log_buf);
|
||||||
}
|
|
||||||
buf[hex_size] = '\0';
|
// Log received response
|
||||||
ESP_LOGV(TAG, "Auth: %s Response is %s", LOG_STR_ARG(name), buf);
|
memcpy(log_buf, response, hex_size); // Save response for logging
|
||||||
|
log_buf[hex_size] = '\0';
|
||||||
|
ESP_LOGV(TAG, "Auth: %s Response is %s", LOG_STR_ARG(name), log_buf);
|
||||||
|
#endif // ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||||
|
|
||||||
// Compare response directly with digest in hasher
|
// Compare response directly with digest in hasher
|
||||||
bool matches = hasher->equals_hex(buf);
|
bool matches = hasher->equals_hex(response);
|
||||||
|
|
||||||
if (!matches) {
|
if (!matches) {
|
||||||
this->log_auth_warning_(LOG_STR("Password mismatch"), name);
|
this->log_auth_warning_(LOG_STR("Password mismatch"), name);
|
||||||
|
Reference in New Issue
Block a user