diff --git a/README.md b/README.md index af1870a..f0103c3 100644 --- a/README.md +++ b/README.md @@ -19,14 +19,14 @@ This projects aims at possibility to easily launch Matter internet-of-things pro 5. Run example sketch ## Example usage -Please look at [examples](https://github.com/jakubdybczak/esp32-arduino-matter/tree/master/examples). You can test integration with Android and Matter controller by downloading compiled [CHIPTool](https://drive.google.com/drive/folders/1NXqfbRzBQRWCH4VWJQwQSO6KKYeIH7VK) for Android. +Please look at [examples](https://github.com/jakubdybczak/esp32-arduino-matter/tree/master/examples). ## Limitations * Library only works on base ESP32 (with experimental support for ESP32-S3, ESP32-C3). * There is no possibility to change vendor/product ID as this value is pre-compiled. * There is no known possibility to change setup PIN. * This library comes with precompiled NimBLE, because default Bluedroid shipped with arduino-esp32 takes too much RAM memory. -* As of 06 Nov 2022, this library does not work with Google Home as this app is compatible with older version of Matter. You can test this library with CHIPTool. +* Matter Controllers such as Apple Home, Google Home, Smarthings and other might not have full support of all device types. ## Versions This project is currently build based on these projects: @@ -34,8 +34,8 @@ This project is currently build based on these projects: | Project | Tag/Commit Hash | | ------------- | ------------- | | [Espressif's esp-idf](https://github.com/espressif/esp-idf) | 4.4.2
Arduino IDE ESP32 board @ 2.0.5
PlatformIO espressif platform @ 5.2.0 | -| [Espressif's SDK for Matter](https://github.com/espressif/esp-matter) | e7c70721 | -| [Matter](https://github.com/project-chip/connectedhomeip) | 87bee4de | +| [Espressif's SDK for Matter](https://github.com/espressif/esp-matter) | a0f13786 | +| [Matter](https://github.com/project-chip/connectedhomeip) | 7c2353bb | ## Enabling C++17 on Arduino IDE 1. Find `platform.txt` for ESP32 board. Location of this file is platform depended. @@ -52,4 +52,4 @@ This project is currently build based on these projects: Please look [here](https://github.com/jakubdybczak/esp32-arduino-matter-builder). ## Future and possibilities -* Creating more user-friendly wrapper API. +* Creating more user-friendly wrapper API. \ No newline at end of file diff --git a/examples/Debug/Debug.ino b/examples/Debug/Debug.ino new file mode 100644 index 0000000..6335d0b --- /dev/null +++ b/examples/Debug/Debug.ino @@ -0,0 +1,140 @@ +#include "Matter.h" +#include +using namespace chip; +using namespace chip::app::Clusters; +using namespace esp_matter; +using namespace esp_matter::endpoint; + +/** + This program presents example many Matter devices and should be used only + for debug purposes (for example checking which devices types are supported) + + Keep in mind that it IS NOT POSSIBLE to run all those endpoints due to + out of memory. There is need to manually comment out endpoints! + Running about 4-5 endpoints at the same time should work. +*/ + +static void on_device_event(const ChipDeviceEvent *event, intptr_t arg) {} +static esp_err_t on_identification(identification::callback_type_t type, uint16_t endpoint_id, + uint8_t effect_id, void *priv_data) { + return ESP_OK; +} + +static esp_err_t on_attribute_update(attribute::callback_type_t type, uint16_t endpoint_id, uint32_t cluster_id, + uint32_t attribute_id, esp_matter_attr_val_t *val, void *priv_data) { + if (type == attribute::PRE_UPDATE) { + Serial.print("Update on endpoint: "); + Serial.print(endpoint_id); + Serial.print(" cluster: "); + Serial.print(cluster_id); + Serial.print(" attribute: "); + Serial.println(attribute_id); + } + return ESP_OK; +} + +void print_endpoint_info(String clusterName, endpoint_t *endpoint) { + uint16_t endpoint_id = endpoint::get_id(endpoint); + Serial.print(clusterName); + Serial.print(" has endpoint: "); + Serial.println(endpoint_id); +} + +void setup() { + Serial.begin(115200); + + esp_log_level_set("*", ESP_LOG_DEBUG); + + node::config_t node_config; + node_t *node = node::create(&node_config, on_attribute_update, on_identification); + + endpoint_t *endpoint; + cluster_t *cluster; + + // !!! + // USE ONLY ABOUT 4 ENDPOINTS TO AVOID OUT OF MEMORY ERRORS + // !!! + + on_off_light::config_t light_config; + endpoint = on_off_light::create(node, &light_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("on_off_light", endpoint); + + dimmable_light::config_t dimmable_light_config; + endpoint = dimmable_light::create(node, &dimmable_light_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("dimmable_light", endpoint); + + color_temperature_light::config_t color_temperature_light_config; + endpoint = color_temperature_light::create(node, &color_temperature_light_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("color_temperature_light", endpoint); + + extended_color_light::config_t extended_color_light_config; + endpoint = extended_color_light::create(node, &extended_color_light_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("extended_color_light", endpoint); + + generic_switch::config_t generic_switch_config; + generic_switch_config.switch_cluster.current_position = 1; + generic_switch_config.switch_cluster.number_of_positions = 3; + endpoint = generic_switch::create(node, &generic_switch_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("generic_switch", endpoint); + + on_off_plugin_unit::config_t on_off_plugin_unit_config; + on_off_plugin_unit_config.on_off.on_off = true; + endpoint = on_off_plugin_unit::create(node, &on_off_plugin_unit_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("on_off_plugin_unit", endpoint); + + dimmable_plugin_unit::config_t dimmable_plugin_unit_config; + endpoint = dimmable_plugin_unit::create(node, &dimmable_plugin_unit_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("dimmable_plugin_unit", endpoint); + + fan::config_t fan_config; + endpoint = fan::create(node, &fan_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("fan", endpoint); + + thermostat::config_t thermostat_config; + thermostat_config.thermostat.local_temperature = 19; + endpoint = thermostat::create(node, &thermostat_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("thermostat", endpoint); + + door_lock::config_t door_lock_config; + door_lock_config.door_lock.lock_state = 1; + endpoint = door_lock::create(node, &door_lock_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("door_lock", endpoint); + + window_covering_device::config_t window_covering_device_config; + endpoint = window_covering_device::create(node, &window_covering_device_config, ENDPOINT_FLAG_NONE, NULL); + cluster = cluster::get(endpoint, WindowCovering::Id); + cluster::window_covering::feature::lift::config_t lift; + cluster::window_covering::feature::tilt::config_t tilt; + cluster::window_covering::feature::position_aware_lift::config_t position_aware_lift; + cluster::window_covering::feature::position_aware_tilt::config_t position_aware_tilt; + cluster::window_covering::feature::absolute_position::config_t absolute_position; + cluster::window_covering::feature::lift::add(cluster, &lift); + cluster::window_covering::feature::tilt::add(cluster, &tilt); + cluster::window_covering::feature::position_aware_lift::add(cluster, &position_aware_lift); + cluster::window_covering::feature::position_aware_tilt::add(cluster, &position_aware_tilt); + cluster::window_covering::feature::absolute_position::add(cluster, &absolute_position); + print_endpoint_info("window_covering_device", endpoint); + + temperature_sensor::config_t temperature_sensor_config; + temperature_sensor_config.temperature_measurement.measured_value = 22; + endpoint = temperature_sensor::create(node, &temperature_sensor_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("temperature_sensor", endpoint); + + occupancy_sensor::config_t occupancy_sensor_config; + occupancy_sensor_config.occupancy_sensing.occupancy = 1; + endpoint = occupancy_sensor::create(node, &occupancy_sensor_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("occupancy_sensor", endpoint); + + contact_sensor::config_t contact_sensor_config; + contact_sensor_config.boolean_state.state_value = true; + endpoint = contact_sensor::create(node, &contact_sensor_config, ENDPOINT_FLAG_NONE, NULL); + print_endpoint_info("contact_sensor", endpoint); + + esp_matter::start(on_device_event); + + PrintOnboardingCodes(chip::RendezvousInformationFlags(chip::RendezvousInformationFlag::kBLE)); +} + +void loop() { + +} \ No newline at end of file diff --git a/examples/Two_endpoints_plugin_unit/Two_endpoints_plugin_unit.ino b/examples/TwoEndpointsPluginUnit/TwoEndpointsPluginUnit.ino similarity index 98% rename from examples/Two_endpoints_plugin_unit/Two_endpoints_plugin_unit.ino rename to examples/TwoEndpointsPluginUnit/TwoEndpointsPluginUnit.ino index b25b759..02f5735 100644 --- a/examples/Two_endpoints_plugin_unit/Two_endpoints_plugin_unit.ino +++ b/examples/TwoEndpointsPluginUnit/TwoEndpointsPluginUnit.ino @@ -35,7 +35,7 @@ attribute_t *attribute_ref_1; attribute_t *attribute_ref_2; // There is possibility to listen for various device events, related for example -// to setup process Leaved as empty for simplicity +// to setup process. Leaved as empty for simplicity. static void on_device_event(const ChipDeviceEvent *event, intptr_t arg) {} static esp_err_t on_identification(identification::callback_type_t type, uint16_t endpoint_id, uint8_t effect_id, @@ -88,8 +88,7 @@ void setup() { endpoint_t *endpoint_2 = on_off_plugin_unit::create(node, &plugin_unit_config, ENDPOINT_FLAG_NONE, NULL); - // Save on/off attribute reference. It will be used to read attribute value - // later. + // Save on/off attribute reference. It will be used to read attribute value later. attribute_ref_1 = attribute::get(cluster::get(endpoint_1, CLUSTER_ID), ATTRIBUTE_ID); attribute_ref_2 = @@ -139,4 +138,4 @@ void loop() { set_onoff_attribute_value(&onoff_value, plugin_unit_endpoint_id_2); } } -} +} \ No newline at end of file diff --git a/src/ConfigurationManagerImpl.h b/src/ConfigurationManagerImpl.h index 32f4951..6443861 100644 --- a/src/ConfigurationManagerImpl.h +++ b/src/ConfigurationManagerImpl.h @@ -57,6 +57,7 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp CHIP_ERROR GetProductLabel(char * buf, size_t bufSize) override; CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize); CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) override; + CHIP_ERROR GetLocationCapability(uint8_t & location) override; static ConfigurationManagerImpl & GetDefaultInstance(); private: diff --git a/src/ESP32Config.h b/src/ESP32Config.h index f884af8..6a008db 100644 --- a/src/ESP32Config.h +++ b/src/ESP32Config.h @@ -79,6 +79,7 @@ class ESP32Config static const Key kConfigKey_SupportedCalTypes; static const Key kConfigKey_SupportedLocaleSize; static const Key kConfigKey_RotatingDevIdUniqueId; + static const Key kConfigKey_LocationCapability; // CHIP Config keys static const Key kConfigKey_ServiceConfig; diff --git a/src/app/ReadHandler.h b/src/app/ReadHandler.h index 7e32fa9..cf0ee7a 100644 --- a/src/app/ReadHandler.h +++ b/src/app/ReadHandler.h @@ -263,6 +263,9 @@ class ReadHandler : public Messaging::ExchangeDelegate bool IsIdle() const { return mState == HandlerState::Idle; } bool IsReportable() const { + // Important: Anything that changes the state IsReportable depends on in + // a way that causes IsReportable to become true must call ScheduleRun + // on the reporting engine. return mState == HandlerState::GeneratingReports && !mFlags.Has(ReadHandlerFlags::HoldReport) && (IsDirty() || !mFlags.Has(ReadHandlerFlags::HoldSync)); } @@ -299,7 +302,7 @@ class ReadHandler : public Messaging::ExchangeDelegate { return (mDirtyGeneration > mPreviousReportsBeginGeneration) || mFlags.Has(ReadHandlerFlags::ForceDirty); } - void ClearForceDirtyFlag() { mFlags.Clear(ReadHandlerFlags::ForceDirty); } + void ClearForceDirtyFlag() { ClearStateFlag(ReadHandlerFlags::ForceDirty); } NodeId GetInitiatorNodeId() const { auto session = GetSession(); @@ -317,7 +320,7 @@ class ReadHandler : public Messaging::ExchangeDelegate auto GetTransactionStartGeneration() const { return mTransactionStartGeneration; } - void UnblockUrgentEventDelivery() { mFlags.Set(ReadHandlerFlags::ForceDirty); } + void UnblockUrgentEventDelivery(); const AttributeValueEncoder::AttributeEncodeState & GetAttributeEncodeState() const { return mAttributeEncoderState; } void SetAttributeEncodeState(const AttributeValueEncoder::AttributeEncodeState & aState) { mAttributeEncoderState = aState; } @@ -374,6 +377,10 @@ class ReadHandler : public Messaging::ExchangeDelegate const char * GetStateStr() const; + // Helpers for managing our state flags properly. + void SetStateFlag(ReadHandlerFlags aFlag, bool aValue = true); + void ClearStateFlag(ReadHandlerFlags aFlag); + AttributePathExpandIterator mAttributePathExpandIterator = AttributePathExpandIterator(nullptr); // The current generation of the reporting engine dirty set the last time we were notified that a path we're interested in was diff --git a/src/app/reporting/Engine.h b/src/app/reporting/Engine.h index 45754b4..e0f81f4 100644 --- a/src/app/reporting/Engine.h +++ b/src/app/reporting/Engine.h @@ -39,6 +39,9 @@ namespace chip { namespace app { + +class TestReadInteraction; + namespace reporting { /* * @class Engine @@ -138,6 +141,7 @@ class Engine void Run(); friend class TestReportingEngine; + friend class ::chip::app::TestReadInteraction; struct AttributePathParamsWithGeneration : public AttributePathParams { diff --git a/src/button_gpio.h b/src/button_gpio.h index 78bb985..50180f5 100644 --- a/src/button_gpio.h +++ b/src/button_gpio.h @@ -25,8 +25,8 @@ extern "C" { * */ typedef struct { - int32_t gpio_num; - uint8_t active_level; + int32_t gpio_num; /**< num of gpio */ + uint8_t active_level; /**< gpio level when press down */ } button_gpio_config_t; /** diff --git a/src/esp32/libCHIP.a b/src/esp32/libCHIP.a index 07f1fbe..4828e8b 100644 Binary files a/src/esp32/libCHIP.a and b/src/esp32/libCHIP.a differ diff --git a/src/esp32/libesp_matter.a b/src/esp32/libesp_matter.a index 86f0ddc..1ca5837 100644 Binary files a/src/esp32/libesp_matter.a and b/src/esp32/libesp_matter.a differ diff --git a/src/esp32/libesp_matter_bridge.a b/src/esp32/libesp_matter_bridge.a index 5315735..5322fa8 100644 Binary files a/src/esp32/libesp_matter_bridge.a and b/src/esp32/libesp_matter_bridge.a differ diff --git a/src/esp32c3/libCHIP.a b/src/esp32c3/libCHIP.a index 9bcaa76..1f8ef2f 100644 Binary files a/src/esp32c3/libCHIP.a and b/src/esp32c3/libCHIP.a differ diff --git a/src/esp32c3/libesp_matter.a b/src/esp32c3/libesp_matter.a index c554a75..5af57fb 100644 Binary files a/src/esp32c3/libesp_matter.a and b/src/esp32c3/libesp_matter.a differ diff --git a/src/esp32c3/libesp_matter_bridge.a b/src/esp32c3/libesp_matter_bridge.a index 9aa1c99..d4141bb 100644 Binary files a/src/esp32c3/libesp_matter_bridge.a and b/src/esp32c3/libesp_matter_bridge.a differ diff --git a/src/esp32s3/libCHIP.a b/src/esp32s3/libCHIP.a index 7e2191a..73cd68a 100644 Binary files a/src/esp32s3/libCHIP.a and b/src/esp32s3/libCHIP.a differ diff --git a/src/esp32s3/libesp_matter.a b/src/esp32s3/libesp_matter.a index d26621d..ef1d74b 100644 Binary files a/src/esp32s3/libesp_matter.a and b/src/esp32s3/libesp_matter.a differ diff --git a/src/esp32s3/libesp_matter_bridge.a b/src/esp32s3/libesp_matter_bridge.a index 36bb033..54c335b 100644 Binary files a/src/esp32s3/libesp_matter_bridge.a and b/src/esp32s3/libesp_matter_bridge.a differ diff --git a/src/esp_matter_attribute_utils.h b/src/esp_matter_attribute_utils.h index 49d15df..188e242 100644 --- a/src/esp_matter_attribute_utils.h +++ b/src/esp_matter_attribute_utils.h @@ -153,12 +153,38 @@ typedef struct esp_matter_attr_bounds { template class nullable { + +/** NOTE: GetNullValue is taken from src/app/util/attribute-storage-null-handling.h */ +private: + template ::value, int> = 0> + static constexpr T GetNullValue() + { + return std::numeric_limits::quiet_NaN(); + } + + template ::value, int> = 0> + static constexpr T GetNullValue() + { + return std::is_signed::value ? std::numeric_limits::min() : std::numeric_limits::max(); + } + + template ::value, int> = 0> + static constexpr T GetNullValue() + { + static_assert(!std::is_signed>::value, "Enums must be unsigned"); + return static_cast(std::numeric_limits>::max()); + } + public: nullable(T value) { - assert(!chip::app::NumericAttributeTraits::IsNullValue(value)); - val = value; + if (chip::app::NumericAttributeTraits::IsNullValue(value)) { + chip::app::NumericAttributeTraits::SetNull(val); + } else { + val = value; + } } + nullable() { chip::app::NumericAttributeTraits::SetNull(val); @@ -166,8 +192,12 @@ class nullable { T value() { - assert(!is_null()); - return val; + if (is_null()) { + return GetNullValue(); + } else { + return val; + } + } T value_or(T ret) @@ -182,8 +212,11 @@ class nullable { void operator=(T value) { - assert(!chip::app::NumericAttributeTraits::IsNullValue(value)); - this->val = value; + if (chip::app::NumericAttributeTraits::IsNullValue(value)) { + chip::app::NumericAttributeTraits::SetNull(this->val); + } else { + this->val = value; + } } void operator=(std::nullptr_t) diff --git a/src/esp_matter_client.h b/src/esp_matter_client.h index ef82f39..40dfa3f 100644 --- a/src/esp_matter_client.h +++ b/src/esp_matter_client.h @@ -54,6 +54,21 @@ esp_err_t send_step_with_on_off(peer_device_t *remote_device, uint16_t remote_en esp_err_t send_stop(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t option_mask, uint8_t option_override); esp_err_t send_stop_with_on_off(peer_device_t *remote_device, uint16_t remote_endpoint_id); +esp_err_t group_send_move(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, uint8_t rate, + uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_move_to_level(uint8_t fabric_index, uint16_t group_id, uint8_t level, + uint16_t transition_time, uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_move_to_level_with_on_off(uint8_t fabric_index, uint16_t group_id, uint8_t level, + uint16_t transition_time); +esp_err_t group_send_move_with_on_off(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, + uint8_t rate); +esp_err_t group_send_step(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, uint8_t step_size, + uint16_t transition_time, uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_step_with_on_off(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, + uint8_t step_size, uint16_t transition_time); +esp_err_t group_send_stop(uint8_t fabric_index, uint16_t group_id, uint8_t option_mask, + uint8_t option_override); +esp_err_t group_send_stop_with_on_off(uint8_t fabric_index, uint16_t group_id); } /* command */ } /* level_control */ @@ -76,6 +91,23 @@ esp_err_t send_step_hue(peer_device_t *remote_device, uint16_t remote_endpoint_i esp_err_t send_step_saturation(peer_device_t *remote_device, uint16_t remote_endpoint_id, uint8_t step_mode, uint8_t step_size, uint16_t transition_time, uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_move_hue(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, uint8_t rate, uint8_t option_mask, + uint8_t option_override); +esp_err_t group_send_move_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t move_mode, + uint8_t rate, uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_move_to_hue(uint8_t fabric_index, uint16_t group_id, uint8_t hue, uint8_t direction, uint16_t transition_time, + uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_move_to_hue_and_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t hue, + uint8_t saturation, uint16_t transition_time, uint8_t option_mask, + uint8_t option_override); +esp_err_t group_send_move_to_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t saturation, + uint16_t transition_time, uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_step_hue(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, uint8_t step_size, + uint16_t transition_time, uint8_t option_mask, uint8_t option_override); +esp_err_t group_send_step_saturation(uint8_t fabric_index, uint16_t group_id, uint8_t step_mode, + uint8_t step_size, uint16_t transition_time, uint8_t option_mask, + uint8_t option_override); + } /* command */ } /* color_control */ diff --git a/src/esp_matter_core.h b/src/esp_matter_core.h index 0ba49cd..a55b29a 100644 --- a/src/esp_matter_core.h +++ b/src/esp_matter_core.h @@ -138,6 +138,21 @@ namespace endpoint { */ endpoint_t *create(node_t *node, uint8_t flags, void *priv_data); +/** Resume endpoint + * + * This will resume an endpoint after reboot and add it to the node. + * + * @param[in] node Node handle. + * @param[in] flags Bitmap of `endpoint_flags_t`. + * @param[in] endpoint_id Endpoint ID of the endpoint resumed. + * @param[in] priv_data (Optional) Private data associated with the endpoint. This will be passed to the + * attribute_update and identify callbacks. It should stay allocated throughout the lifetime of the device. + * + * @return Endpoint handle on success. + * @return NULL in case of failure. + */ +endpoint_t *resume(node_t *node, uint8_t flags, uint16_t endpoint_id, void *priv_data); + /** Destroy endpoint * * This will destroy the endpoint which has been created and added to the node. It also destroys the associated @@ -233,6 +248,19 @@ uint32_t *get_device_type_ids(endpoint_t *endpoint, uint8_t *device_type_count_p */ uint8_t *get_device_type_versions(endpoint_t *endpoint, uint8_t *device_type_count_ptr); +/** Set parent endpoint + * + * Set the parent endpoint. This is useful in correctly setting the parts_list attribute for the parent, when the + * parent is a composite endpoint. + * + * @param[in] endpoint Endpoint handle. + * @param[out] parent_endpoint Parent endpoint handle. + * + * @return ESP_OK on success. + * @return error in case of failure. + */ +esp_err_t set_parent_endpoint(endpoint_t *endpoint, endpoint_t *parent_endpoint); + /** Get private data * * Get the private data passed while creating the endpoint. @@ -252,12 +280,11 @@ void *get_priv_data(uint16_t endpoint_id); * called after all the clusters, attributes and commands have been added to the created endpoint. * * @param[in] endpoint Endpoint handle. - * @param[in] parent_endpoint_id Parent Endpoint Id for the endpoint to be enabled, applicable only for bridges. * * @return ESP_OK on success. * @return error in case of failure. */ -esp_err_t enable(endpoint_t *endpoint, uint16_t parent_endpoint_id); +esp_err_t enable(endpoint_t *endpoint); } /* endpoint */ @@ -710,15 +737,25 @@ namespace client { */ typedef struct command_handle { - uint16_t endpoint_id; + union { + uint16_t endpoint_id; + uint16_t group_id; + }; uint32_t cluster_id; uint32_t command_id; void *command_data { NULL }; bool is_group; command_handle() : endpoint_id(chip::kInvalidEndpointId), cluster_id(chip::kInvalidClusterId), command_id(chip::kInvalidCommandId), command_data(NULL), is_group(false) {} - command_handle(struct command_handle* cmd) : endpoint_id(cmd->endpoint_id), cluster_id(cmd->cluster_id), - command_id(cmd->command_id), command_data(cmd->command_data), is_group(cmd->is_group) {} + command_handle(struct command_handle* cmd) : cluster_id(cmd->cluster_id), command_id(cmd->command_id), + command_data(cmd->command_data), is_group(cmd->is_group) + { + if (cmd->is_group) { + this->group_id = cmd->group_id; + } else { + this->endpoint_id = cmd->endpoint_id; + } + } } command_handle_t; /** Peer device handle */ @@ -730,25 +767,22 @@ typedef chip::DeviceProxy peer_device_t; * send_command APIs can then be called from the callback. * * @param[in] peer_device Peer device handle. This can be passed to the send_command APIs. - * @param[in] remote_endpoint_id Endpoint ID of the other device. This can be passed to the send_command APIs. * @param[in] cmd_handle Command handle used by `connect()` or `cluster_update()`. * @param[in] priv_data (Optional) Private data associated with the callback. This will be passed to callback. It * should stay allocated throughout the lifetime of the device. */ -typedef void (*command_callback_t)(peer_device_t *peer_device, uint16_t remote_endpoint_id, command_handle_t *cmd_handle, - void *priv_data); +typedef void (*command_callback_t)(peer_device_t *peer_device, command_handle_t *cmd_handle, void *priv_data); /** Group command send callback * * This callback will be called when `cluster_update()` is called and the group command is triggered for binding cluster. * * @param[in] fabric_index The index of the fabric that the group command is sent to. - * @param[in] group_id The group_id that the group command is sent to. * @param[in] cmd_handle Command handle used by `cluster_update()`. * @param[in] priv_data (Optional) Private data associated with the callback. This will be passed to callback. It * should stay allocated throughout the lifetime of the device. */ -typedef void (*group_command_callback_t)(uint8_t fabric_index, uint16_t group_id, command_handle_t *cmd_handle, void *priv_data); +typedef void (*group_command_callback_t)(uint8_t fabric_index, command_handle_t *cmd_handle, void *priv_data); /** Initialize binding * @@ -777,6 +811,18 @@ void binding_manager_init(); */ esp_err_t connect(uint8_t fabric_index, uint64_t node_id, command_handle_t *cmd_handle); +/** group_command_send + * + * on the same fabric to send a group command. + * + * @param[in] fabric_index Fabric index. + * @param[in] cmd_handle Command to be sent to the group. + * + * @return ESP_OK on success. + * @return error in case of failure. + */ +esp_err_t group_command_send(uint8_t fabric_index, command_handle_t *cmd_handle); + /** Set command send callback * * Set the common command send callback and the group command send callback. The common callback will be called diff --git a/src/esp_matter_dac_provider.h b/src/esp_matter_dac_provider.h new file mode 100644 index 0000000..defe43a --- /dev/null +++ b/src/esp_matter_dac_provider.h @@ -0,0 +1,35 @@ +// Copyright 2022 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#pragma once + +#include + +namespace esp_matter { + +/** + * @brief Get the DAC Provider implementation based on the configuration option + * + * If CONFIG_EXAMPLE_DAC_PROVIDER option is enabled then implementation which uses test data is used. + * + * If CONFIG_FACTORY_PARTITION_DAC_PROVIDER option is enabled then implementation which reads attestation + * information from factory partition is used. + * + * If CONFIG_SEC_CERT_DAC_PROVIDER option is enabled then implementation which reads attestation information + * from the esp_secure_cert partition is used. + * + * @return Pointer to the object of type DeviceAttestationCredentialsProvider + */ +chip::Credentials::DeviceAttestationCredentialsProvider * get_dac_provider(void); + +} // namespace esp_matter diff --git a/src/esp_matter_endpoint.h b/src/esp_matter_endpoint.h index 3237068..cca043a 100644 --- a/src/esp_matter_endpoint.h +++ b/src/esp_matter_endpoint.h @@ -18,6 +18,53 @@ #include #include +/* Replace these with IDs from submodule whenever they are implemented */ +#define ESP_MATTER_ROOT_NODE_DEVICE_TYPE_ID 0x0016 +#define ESP_MATTER_ROOT_NODE_DEVICE_TYPE_VERSION 1 +#define ESP_MATTER_AGGREGATOR_DEVICE_TYPE_ID 0x000E +#define ESP_MATTER_AGGREGATOR_DEVICE_TYPE_VERSION 1 +#define ESP_MATTER_BRIDGED_NODE_DEVICE_TYPE_ID 0x0013 +#define ESP_MATTER_BRIDGED_NODE_DEVICE_TYPE_VERSION 1 + +#define ESP_MATTER_ON_OFF_LIGHT_DEVICE_TYPE_ID 0x0100 +#define ESP_MATTER_ON_OFF_LIGHT_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_DIMMABLE_LIGHT_DEVICE_TYPE_ID 0x0101 +#define ESP_MATTER_DIMMABLE_LIGHT_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_COLOR_TEMPERATURE_LIGHT_DEVICE_TYPE_ID 0x010C +#define ESP_MATTER_COLOR_TEMPERATURE_LIGHT_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_EXTENDED_COLOR_LIGHT_DEVICE_TYPE_ID 0x010D +#define ESP_MATTER_EXTENDED_COLOR_LIGHT_DEVICE_TYPE_VERSION 2 + +#define ESP_MATTER_ON_OFF_SWITCH_DEVICE_TYPE_ID 0x0103 +#define ESP_MATTER_ON_OFF_SWITCH_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_DIMMER_SWITCH_DEVICE_TYPE_ID 0x0104 +#define ESP_MATTER_DIMMER_SWITCH_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_COLOR_DIMMER_SWITCH_DEVICE_TYPE_ID 0x0105 +#define ESP_MATTER_COLOR_DIMMER_SWITCH_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_GENERIC_SWITCH_DEVICE_TYPE_ID 0x000F +#define ESP_MATTER_GENERIC_SWITCH_DEVICE_TYPE_VERSION 1 + +#define ESP_MATTER_ON_OFF_PLUGIN_UNIT_DEVICE_TYPE_ID 0x010A +#define ESP_MATTER_ON_OFF_PLUGIN_UNIT_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_DIMMABLE_PLUGIN_UNIT_DEVICE_TYPE_ID 0x010B +#define ESP_MATTER_DIMMABLE_PLUGIN_UNIT_DEVICE_TYPE_VERSION 2 + +#define ESP_MATTER_TEMPERATURE_SENSOR_DEVICE_TYPE_ID 0x0302 +#define ESP_MATTER_TEMPERATURE_SENSOR_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_OCCUPANCY_SENSOR_DEVICE_TYPE_ID 0x0107 +#define ESP_MATTER_OCCUPANCY_SENSOR_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_CONTACT_SENSOR_DEVICE_TYPE_ID 0x0015 +#define ESP_MATTER_CONTACT_SENSOR_DEVICE_TYPE_VERSION 1 + +#define ESP_MATTER_FAN_DEVICE_TYPE_ID 0x002B +#define ESP_MATTER_FAN_DEVICE_TYPE_VERSION 1 +#define ESP_MATTER_THERMOSTAT_DEVICE_TYPE_ID 0x0301 +#define ESP_MATTER_THERMOSTAT_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_DOOR_LOCK_DEVICE_TYPE_ID 0x000A +#define ESP_MATTER_DOOR_LOCK_DEVICE_TYPE_VERSION 2 +#define ESP_MATTER_WINDOW_COVERING_DEVICE_TYPE_ID 0x0202 +#define ESP_MATTER_WINDOW_COVERING_DEVICE_TYPE_VERSION 2 + namespace esp_matter { /** Specific endpoint (device type) create APIs @@ -45,6 +92,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* root_node */ namespace on_off_light { @@ -58,6 +106,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* on_off_light */ namespace dimmable_light { @@ -72,6 +121,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* dimmable_light */ namespace color_temperature_light { @@ -87,6 +137,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* color_temperature_light */ namespace extended_color_light { @@ -102,6 +153,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* extended_color_light */ namespace on_off_switch { @@ -113,6 +165,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* on_off_switch */ namespace dimmer_switch { @@ -124,6 +177,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* dimmer_switch */ namespace color_dimmer_switch { @@ -135,6 +189,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* color_dimmer_switch */ namespace generic_switch { @@ -146,6 +201,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* generic_switch */ namespace on_off_plugin_unit { @@ -159,6 +215,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* on_off_plugin_unit */ namespace dimmable_plugin_unit { @@ -173,6 +230,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* dimmable_plugin_unit */ namespace fan { @@ -185,6 +243,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* fan */ namespace thermostat { @@ -197,13 +256,15 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* thermostat */ namespace aggregator { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, uint8_t flags, void *priv_data); -} /* bridge */ +endpoint_t *add(endpoint_t *endpoint); +} /* aggregator */ namespace bridged_node { typedef struct config { @@ -213,6 +274,8 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); +endpoint_t *resume(node_t *node, config_t *config, uint8_t flags, uint16_t endpoint_id, void *priv_data); } /* bridged_node */ namespace door_lock { @@ -224,6 +287,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* door_lock */ namespace window_covering_device { @@ -237,6 +301,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* window_covering */ namespace temperature_sensor { @@ -248,6 +313,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* temperature_sensor */ namespace occupancy_sensor { @@ -259,6 +325,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* occupancy_sensor */ namespace contact_sensor { @@ -270,6 +337,7 @@ typedef struct config { uint32_t get_device_type_id(); uint8_t get_device_type_version(); endpoint_t *create(node_t *node, config_t *config, uint8_t flags, void *priv_data); +endpoint_t *add(endpoint_t *endpoint, config_t *config); } /* contact_sensor */ } /* endpoint */ diff --git a/src/esp_matter_feature.h b/src/esp_matter_feature.h index 6f58a09..7bf4274 100644 --- a/src/esp_matter_feature.h +++ b/src/esp_matter_feature.h @@ -170,36 +170,72 @@ namespace window_covering { namespace feature { namespace lift { +typedef struct config { + uint16_t number_of_actuations_lift; + config() : number_of_actuations_lift(0) {} +} config_t; + uint32_t get_id(); -esp_err_t add(cluster_t *cluster); +esp_err_t add(cluster_t *cluster, config_t *config); } /* lift */ namespace tilt { +typedef struct config { + uint16_t number_of_actuations_tilt; + config() : number_of_actuations_tilt(0) {} +} config_t; + uint32_t get_id(); -esp_err_t add(cluster_t *cluster); +esp_err_t add(cluster_t *cluster, config_t *config); } /* tilt */ namespace position_aware_lift { +typedef struct config { + uint16_t current_position_lift_percentage; + uint16_t target_position_lift_percent_100ths; + uint16_t current_position_lift_percent_100ths; + config() : current_position_lift_percentage(), target_position_lift_percent_100ths(), current_position_lift_percent_100ths() {} +} config_t; + uint32_t get_id(); -esp_err_t add(cluster_t *cluster); +esp_err_t add(cluster_t *cluster, config_t *config); } /* position_aware_lift */ namespace absolute_position { +typedef struct config { + uint16_t physical_closed_limit_lift; + uint16_t current_position_lift; + uint16_t installed_open_limit_lift; + uint16_t installed_closed_limit_lift; + uint16_t physical_closed_limit_tilt; + uint16_t current_position_tilt; + uint16_t installed_open_limit_tilt; + uint16_t installed_closed_limit_tilt; + config() : physical_closed_limit_lift(0), current_position_lift(0), installed_open_limit_lift(0), installed_closed_limit_lift(65534), physical_closed_limit_tilt(0), current_position_tilt(0), installed_open_limit_tilt(0), installed_closed_limit_tilt(65534) {} +} config_t; + uint32_t get_id(); -esp_err_t add(cluster_t *cluster); +esp_err_t add(cluster_t *cluster, config_t *config); } /* absolute_position */ namespace position_aware_tilt { +typedef struct config { + uint16_t current_position_tilt_percentage; + uint16_t target_position_tilt_percent_100ths; + uint16_t current_position_tilt_percent_100ths; + config() : current_position_tilt_percentage(), target_position_tilt_percent_100ths(), current_position_tilt_percent_100ths() {} +} config_t; + uint32_t get_id(); -esp_err_t add(cluster_t *cluster); +esp_err_t add(cluster_t *cluster, config_t *config); } /* position_aware_tilt */ } /* feature */ diff --git a/src/esp_secure_cert_read.h b/src/esp_secure_cert_read.h new file mode 100644 index 0000000..e606ceb --- /dev/null +++ b/src/esp_secure_cert_read.h @@ -0,0 +1,166 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once +#include "esp_err.h" + +#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +#include "rsa_sign_alt.h" +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* @info + * Init the esp_secure_cert nvs partition + * + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_init_nvs_partition(void); + +/* @info + * Get the device cert from the esp_secure_cert partition + * + * @note + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the device cert and return the pointer. + * The pointer can be freed in this case (NVS) using respective free API + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. This pointer should not be freed + * + * @params + * - buffer(out) This value shall be filled with the device cert address + * on successfull completion + * - len(out) This value shall be filled with the length of the device cert + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the device cert + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_get_device_cert(char **buffer, uint32_t *len); + +/* + * Free any internally allocated resources for the device cert. + * @note + * This API is only needed in case the partition is of type NVS. + * In other cases no internal memory is allocated by the respective API. + * + * @params + * - buffer(in) The data pointer + * This pointer should be the same one which has been obtained + * through "esp_secure_cert_get_device_cert" API. + */ +esp_err_t esp_secure_cert_free_device_cert(char *buffer); + +/* @info + * Get the ca cert from the esp_secure_cert partition + * + * @note + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the ca cert and return the pointer. + * The pointer can be freed in this case (NVS) using respective free API + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. This pointer should not be freed + * + * @params + * - buffer(out) This value shall be filled with the ca cert address + * on successfull completion + * - len(out) This value shall be filled with the length of the ca cert + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the ca cert + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_get_ca_cert(char **buffer, uint32_t *len); + +/* + * Free any internally allocated resources for the ca cert. + * @note + * This API is only needed in case the partition is of type NVS. + * In other cases no internal memory is allocated by the respective API. + * + * @params + * - buffer(in) The data pointer + * This pointer should be the same one which + * has been obtained through "esp_secure_cert_get_ca_cert" API. + */ +esp_err_t esp_secure_cert_free_ca_cert(char *buffer); + +#ifndef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +/* @info + * Get the private key from the esp_secure_cert partition + * + * @note + * + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the private key and return the pointer. + * The pointer can be freed in this case (NVS) using respective free API + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. This pointer should not be freed + * + * @params + * - buffer(out) This value shall be filled with the private key address + * on successfull completion + * - len(out) This value shall be filled with the length of the private key + * + * + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_get_priv_key(char **buffer, uint32_t *len); + +/* + * Free any internally allocated resources for the priv key. + * @note + * This API is only needed in case the partition is of type NVS. + * In other cases no internal memory is allocated by the respective API. + * + * @params + * - buffer(in) The data pointer + * This pointer should be the same one which + * has been obtained through "esp_secure_cert_get_priv_key" API. + */ +esp_err_t esp_secure_cert_free_priv_key(char *buffer); + +#else /* !CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL */ +/* @info + * This function returns the flash esp_ds_context which can then be + * directly provided to an esp-tls connection through its config structure. + * The memory for the context is dynamically allocated. + * + * @params + * - ds_ctx The pointer to the DS context + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_ds_data_ctx_t *esp_secure_cert_get_ds_ctx(void); + +/* + *@info + * Free the ds context + */ +void esp_secure_cert_free_ds_ctx(esp_ds_data_ctx_t *ds_ctx); +#endif /* CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL */ + +#ifdef __cplusplus +} +#endif diff --git a/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h b/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h index c4dd017..c3f1de4 100644 --- a/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h +++ b/src/include/platform/internal/GenericPlatformManagerImpl_Zephyr.h @@ -27,7 +27,7 @@ #include #include -#include +#include namespace chip { namespace DeviceLayer { diff --git a/src/inet/InetInterface.h b/src/inet/InetInterface.h index f2e89fe..8a18229 100644 --- a/src/inet/InetInterface.h +++ b/src/inet/InetInterface.h @@ -42,7 +42,7 @@ struct ifaddrs; #endif // CHIP_SYSTEM_CONFIG_USE_BSD_IFADDRS #if CHIP_SYSTEM_CONFIG_USE_ZEPHYR_NET_IF -#include +#include struct net_if; struct net_if_ipv4; diff --git a/src/iot_button.h b/src/iot_button.h index 7c68e4a..a50976f 100644 --- a/src/iot_button.h +++ b/src/iot_button.h @@ -21,7 +21,7 @@ extern "C" { #endif -typedef void (* button_cb_t)(void *); +typedef void (* button_cb_t)(void *button_handle, void *usr_data); typedef void *button_handle_t; /** @@ -32,6 +32,7 @@ typedef enum { BUTTON_PRESS_DOWN = 0, BUTTON_PRESS_UP, BUTTON_PRESS_REPEAT, + BUTTON_PRESS_REPEAT_DONE, BUTTON_SINGLE_CLICK, BUTTON_DOUBLE_CLICK, BUTTON_LONG_PRESS_START, @@ -55,9 +56,11 @@ typedef enum { */ typedef struct { button_type_t type; /**< button type, The corresponding button configuration must be filled */ + uint16_t long_press_time; /**< Trigger time(ms) for long press, if 0 default to BUTTON_LONG_PRESS_TIME_MS */ + uint16_t short_press_time; /**< Trigger time(ms) for short press, if 0 default to BUTTON_SHORT_PRESS_TIME_MS */ union { - button_gpio_config_t gpio_button_config; /**< gpio button configuration */ - button_adc_config_t adc_button_config; /**< adc button configuration */ + button_gpio_config_t gpio_button_config; /**< gpio button configuration */ + button_adc_config_t adc_button_config; /**< adc button configuration */ }; /**< button configuration */ } button_config_t; @@ -87,12 +90,13 @@ esp_err_t iot_button_delete(button_handle_t btn_handle); * @param btn_handle A button handle to register * @param event Button event * @param cb Callback function. + * @param usr_data user data * * @return * - ESP_OK on success * - ESP_ERR_INVALID_ARG Arguments is invalid. */ -esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb); +esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb, void *usr_data); /** * @brief Unregister the button event callback function. @@ -106,6 +110,15 @@ esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t even */ esp_err_t iot_button_unregister_cb(button_handle_t btn_handle, button_event_t event); +/** + * @brief how many Callbacks are still registered. + * + * @param btn_handle A button handle to unregister + * + * @return 0 if no callbacks registered, or 1 .. (BUTTON_EVENT_MAX-1) for the number of Registered Buttons. + */ +size_t iot_button_count_cb(button_handle_t btn_handle); + /** * @brief Get button event * @@ -124,6 +137,24 @@ button_event_t iot_button_get_event(button_handle_t btn_handle); */ uint8_t iot_button_get_repeat(button_handle_t btn_handle); +/** + * @brief Get button ticks time + * + * @param btn_handle Button handle + * + * @return Actual time from press down to up (ms). + */ +uint16_t iot_button_get_ticks_time(button_handle_t btn_handle); + +/** + * @brief Get button long press hold count + * + * @param btn_handle Button handle + * + * @return Count of trigger cb(BUTTON_LONG_PRESS_HOLD) + */ +uint16_t iot_button_get_long_press_hold_cnt(button_handle_t btn_handle); + #ifdef __cplusplus } #endif diff --git a/src/managed_components/espressif__button/include/button_gpio.h b/src/managed_components/espressif__button/include/button_gpio.h index 78bb985..50180f5 100644 --- a/src/managed_components/espressif__button/include/button_gpio.h +++ b/src/managed_components/espressif__button/include/button_gpio.h @@ -25,8 +25,8 @@ extern "C" { * */ typedef struct { - int32_t gpio_num; - uint8_t active_level; + int32_t gpio_num; /**< num of gpio */ + uint8_t active_level; /**< gpio level when press down */ } button_gpio_config_t; /** diff --git a/src/managed_components/espressif__button/include/iot_button.h b/src/managed_components/espressif__button/include/iot_button.h index 7c68e4a..a50976f 100644 --- a/src/managed_components/espressif__button/include/iot_button.h +++ b/src/managed_components/espressif__button/include/iot_button.h @@ -21,7 +21,7 @@ extern "C" { #endif -typedef void (* button_cb_t)(void *); +typedef void (* button_cb_t)(void *button_handle, void *usr_data); typedef void *button_handle_t; /** @@ -32,6 +32,7 @@ typedef enum { BUTTON_PRESS_DOWN = 0, BUTTON_PRESS_UP, BUTTON_PRESS_REPEAT, + BUTTON_PRESS_REPEAT_DONE, BUTTON_SINGLE_CLICK, BUTTON_DOUBLE_CLICK, BUTTON_LONG_PRESS_START, @@ -55,9 +56,11 @@ typedef enum { */ typedef struct { button_type_t type; /**< button type, The corresponding button configuration must be filled */ + uint16_t long_press_time; /**< Trigger time(ms) for long press, if 0 default to BUTTON_LONG_PRESS_TIME_MS */ + uint16_t short_press_time; /**< Trigger time(ms) for short press, if 0 default to BUTTON_SHORT_PRESS_TIME_MS */ union { - button_gpio_config_t gpio_button_config; /**< gpio button configuration */ - button_adc_config_t adc_button_config; /**< adc button configuration */ + button_gpio_config_t gpio_button_config; /**< gpio button configuration */ + button_adc_config_t adc_button_config; /**< adc button configuration */ }; /**< button configuration */ } button_config_t; @@ -87,12 +90,13 @@ esp_err_t iot_button_delete(button_handle_t btn_handle); * @param btn_handle A button handle to register * @param event Button event * @param cb Callback function. + * @param usr_data user data * * @return * - ESP_OK on success * - ESP_ERR_INVALID_ARG Arguments is invalid. */ -esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb); +esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t event, button_cb_t cb, void *usr_data); /** * @brief Unregister the button event callback function. @@ -106,6 +110,15 @@ esp_err_t iot_button_register_cb(button_handle_t btn_handle, button_event_t even */ esp_err_t iot_button_unregister_cb(button_handle_t btn_handle, button_event_t event); +/** + * @brief how many Callbacks are still registered. + * + * @param btn_handle A button handle to unregister + * + * @return 0 if no callbacks registered, or 1 .. (BUTTON_EVENT_MAX-1) for the number of Registered Buttons. + */ +size_t iot_button_count_cb(button_handle_t btn_handle); + /** * @brief Get button event * @@ -124,6 +137,24 @@ button_event_t iot_button_get_event(button_handle_t btn_handle); */ uint8_t iot_button_get_repeat(button_handle_t btn_handle); +/** + * @brief Get button ticks time + * + * @param btn_handle Button handle + * + * @return Actual time from press down to up (ms). + */ +uint16_t iot_button_get_ticks_time(button_handle_t btn_handle); + +/** + * @brief Get button long press hold count + * + * @param btn_handle Button handle + * + * @return Count of trigger cb(BUTTON_LONG_PRESS_HOLD) + */ +uint16_t iot_button_get_long_press_hold_cnt(button_handle_t btn_handle); + #ifdef __cplusplus } #endif diff --git a/src/managed_components/espressif__esp_secure_cert_mgr/include/esp_secure_cert_read.h b/src/managed_components/espressif__esp_secure_cert_mgr/include/esp_secure_cert_read.h new file mode 100644 index 0000000..e606ceb --- /dev/null +++ b/src/managed_components/espressif__esp_secure_cert_mgr/include/esp_secure_cert_read.h @@ -0,0 +1,166 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once +#include "esp_err.h" + +#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +#include "rsa_sign_alt.h" +#endif + +#ifdef __cplusplus +extern "C" +{ +#endif + +/* @info + * Init the esp_secure_cert nvs partition + * + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_init_nvs_partition(void); + +/* @info + * Get the device cert from the esp_secure_cert partition + * + * @note + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the device cert and return the pointer. + * The pointer can be freed in this case (NVS) using respective free API + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. This pointer should not be freed + * + * @params + * - buffer(out) This value shall be filled with the device cert address + * on successfull completion + * - len(out) This value shall be filled with the length of the device cert + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the device cert + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_get_device_cert(char **buffer, uint32_t *len); + +/* + * Free any internally allocated resources for the device cert. + * @note + * This API is only needed in case the partition is of type NVS. + * In other cases no internal memory is allocated by the respective API. + * + * @params + * - buffer(in) The data pointer + * This pointer should be the same one which has been obtained + * through "esp_secure_cert_get_device_cert" API. + */ +esp_err_t esp_secure_cert_free_device_cert(char *buffer); + +/* @info + * Get the ca cert from the esp_secure_cert partition + * + * @note + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the ca cert and return the pointer. + * The pointer can be freed in this case (NVS) using respective free API + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. This pointer should not be freed + * + * @params + * - buffer(out) This value shall be filled with the ca cert address + * on successfull completion + * - len(out) This value shall be filled with the length of the ca cert + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the ca cert + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_get_ca_cert(char **buffer, uint32_t *len); + +/* + * Free any internally allocated resources for the ca cert. + * @note + * This API is only needed in case the partition is of type NVS. + * In other cases no internal memory is allocated by the respective API. + * + * @params + * - buffer(in) The data pointer + * This pointer should be the same one which + * has been obtained through "esp_secure_cert_get_ca_cert" API. + */ +esp_err_t esp_secure_cert_free_ca_cert(char *buffer); + +#ifndef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +/* @info + * Get the private key from the esp_secure_cert partition + * + * @note + * + * If your esp_secure_cert partition is of type NVS, the API will dynamically allocate + * the required memory to store the private key and return the pointer. + * The pointer can be freed in this case (NVS) using respective free API + * + * In case of cust_flash partition, a read only flash pointer shall be returned here. This pointer should not be freed + * + * @params + * - buffer(out) This value shall be filled with the private key address + * on successfull completion + * - len(out) This value shall be filled with the length of the private key + * + * + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_get_priv_key(char **buffer, uint32_t *len); + +/* + * Free any internally allocated resources for the priv key. + * @note + * This API is only needed in case the partition is of type NVS. + * In other cases no internal memory is allocated by the respective API. + * + * @params + * - buffer(in) The data pointer + * This pointer should be the same one which + * has been obtained through "esp_secure_cert_get_priv_key" API. + */ +esp_err_t esp_secure_cert_free_priv_key(char *buffer); + +#else /* !CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL */ +/* @info + * This function returns the flash esp_ds_context which can then be + * directly provided to an esp-tls connection through its config structure. + * The memory for the context is dynamically allocated. + * + * @params + * - ds_ctx The pointer to the DS context + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_ds_data_ctx_t *esp_secure_cert_get_ds_ctx(void); + +/* + *@info + * Free the ds context + */ +void esp_secure_cert_free_ds_ctx(esp_ds_data_ctx_t *ds_ctx); +#endif /* CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL */ + +#ifdef __cplusplus +} +#endif diff --git a/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_config.h b/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_config.h new file mode 100644 index 0000000..5b315c3 --- /dev/null +++ b/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_config.h @@ -0,0 +1,87 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once +#include + +#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +#include "esp_ds.h" +#endif + +#define ESP_SECURE_CERT_PKEY_MAGIC_BYTE 0xC1 /* Magic byte of the generated private key */ +#define ESP_SECURE_CERT_DEV_CERT_MAGIC_BYTE 0xC2 /* Magic byte of the generated device certificate */ +#define ESP_SECURE_CERT_CA_CERT_MAGIC_BYTE 0xC3 /* Magic byte of the CA certificate */ + +/* NVS Config */ + +#define ESP_SECURE_CERT_NVS_PARTITION_NAME "esp_secure_cert" /* Name of the nvs pre prov partition */ +#define ESP_SECURE_CERT_NVS_KEYS_PARTITION "esp_secure_cert_keys" + +#define ESP_SECURE_CERT_NVS_LEGACY_PARTITION_NAME "pre_prov" +#define ESP_SECURE_CERT_NVS_LEGACY_KEYS_PARTITION "pre_prov_keys" + +#define ESP_SECURE_CERT_PRIV_KEY "priv_key" +#define ESP_SECURE_CERT_DEV_CERT "dev_cert" +#define ESP_SECURE_CERT_CA_CERT "ca_cert" +#define ESP_SECURE_CERT_NAMESPACE "esp_secure_cert" +#define ESP_SECURE_CERT_LEGACY_NAMESPACE "pre_prov" + + +#define ESP_SECURE_CERT_CIPHERTEXT "cipher_c" +#define ESP_SECURE_CERT_RSA_LEN "rsa_len" +#define ESP_SECURE_CERT_EFUSE_KEY_ID "ds_key_id" +#define ESP_SECURE_CERT_IV "iv" + +#define ESP_SECURE_CERT_METADATA_SIZE 64 /* 32 bytes are reserved for the metadata (Must be a multiple of 32)*/ + +#define ESP_SECURE_CERT_DEV_CERT_SIZE 2048 +#define ESP_SECURE_CERT_CA_CERT_SIZE 4096 +#ifndef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +#define ESP_SECURE_CERT_PRIV_KEY_SIZE 4096 +#else +#define ESP_SECURE_CERT_CIPHERTEXT_SIZE (ESP_DS_C_LEN + 16) +#define ESP_SECURE_CERT_IV_SIZE (ESP_DS_IV_LEN + 16) +#endif + +#define ESP_SECURE_CERT_METADATA_OFFSET 0 /* 32 bytes are reserved for the metadata (Must be a multiple of 32)*/ +#define ESP_SECURE_CERT_DEV_CERT_OFFSET (ESP_SECURE_CERT_METADATA_OFFSET + ESP_SECURE_CERT_METADATA_SIZE) +#define ESP_SECURE_CERT_CA_CERT_OFFSET (ESP_SECURE_CERT_DEV_CERT_OFFSET + ESP_SECURE_CERT_DEV_CERT_SIZE) +#ifndef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +#define ESP_SECURE_CERT_PRIV_KEY_OFFSET (ESP_SECURE_CERT_CA_CERT_OFFSET + ESP_SECURE_CERT_CA_CERT_SIZE) +#define ESP_SECURE_CERT_MAX_SIZE (ESP_SECURE_CERT_PRIV_KEY_OFFSET + ESP_SECURE_CERT_PRIV_KEY_SIZE) +#else +#define ESP_SECURE_CERT_CIPHERTEXT_OFFSET (ESP_SECURE_CERT_CA_CERT_OFFSET + ESP_SECURE_CERT_CA_CERT_SIZE) +#define ESP_SECURE_CERT_IV_OFFSET (ESP_SECURE_CERT_CIPHERTEXT_OFFSET + ESP_SECURE_CERT_CIPHERTEXT_SIZE) +#define ESP_SECURE_CERT_MAX_SIZE (ESP_SECURE_CERT_IV_OFFSET + ESP_SECURE_CERT_IV_SIZE) +#endif + +#define ESP_SECURE_CERT_CUST_FLASH_PARTITION_TYPE 0x3F /* Custom partition type */ +#define ESP_SECURE_CERT_NVS_PARTITION_TYPE 0x01 /* Data type partition */ +#define ESP_SECURE_CERT_PARTITION_NAME "esp_secure_cert" /* Name of the custom pre prov partition */ +#define ESP_SECURE_CERT_LEGACY_PARTITION_NAME "pre_prov" + +#define ESP_SECURE_CERT_METADATA_MAGIC_WORD 0x12345678 + + + +typedef struct { + uint32_t dev_cert_crc; /* CRC of the dev cert data */ + uint16_t dev_cert_len; /* The actual length of the device cert */ + uint32_t ca_cert_crc; /* CRC of the ca cert data */ + uint16_t ca_cert_len; /* The actual length of the ca cert [The length before the 32 byte alignment] */ +#ifndef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL + uint32_t priv_key_crc; /* CRC of the priv key data */ + uint16_t priv_key_len; /* The actual length of the private key */ +#else + uint32_t ciphertext_crc; /* CRC of the ciphertext data */ + uint16_t ciphertext_len; /* The actual length of the ciphertext */ + uint32_t iv_crc; /* CRC of the iv data */ + uint16_t iv_len; /* The actual length of iv*/ + uint16_t rsa_length; /* Length of the RSA private key that is encrypted as ciphertext */ + uint8_t efuse_key_id; /* The efuse key block id which holds the HMAC key used to encrypt the ciphertext */ +#endif + uint32_t magic_word; /* The magic word which shall identify the valid metadata when read from flash */ +} esp_secure_cert_metadata; diff --git a/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_tlv_config.h b/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_tlv_config.h new file mode 100644 index 0000000..e1404e9 --- /dev/null +++ b/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_tlv_config.h @@ -0,0 +1,73 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#pragma once +#include +#include "sdkconfig.h" +#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +#include "esp_ds.h" +#endif + +#define ESP_SECURE_CERT_TLV_PARTITION_TYPE 0x3F /* Custom partition type */ +#define ESP_SECURE_CERT_TLV_PARTITION_NAME "esp_secure_cert" /* Name of the custom esp_secure_cert partition */ +#define ESP_SECURE_CERT_TLV_MAGIC 0xBA5EBA11 + +/* secure cert partition of cust_flash type in this case is of 8 KB size, + * out of which 3-3.1 KB size is utilized. + */ + +/* + * Plase note that no two TLV structures of the same type + * can be stored in the esp_secure_cert partition at one time. + */ +typedef enum esp_secure_cert_tlv_type { + ESP_SECURE_CERT_CA_CERT_TLV = 0, + ESP_SECURE_CERT_DEV_CERT_TLV, + ESP_SECURE_CERT_PRIV_KEY_TLV, + ESP_SECURE_CERT_DS_DATA_TLV, + ESP_SECURE_CERT_DS_CONTEXT_TLV, + // Any new tlv types should be added above this + ESP_SECURE_CERT_TLV_END = 50, + //Custom data types + //that can be defined by the user + ESP_SECURE_CERT_USER_DATA_1 = 51, + ESP_SECURE_CERT_USER_DATA_2 = 52, + ESP_SECURE_CERT_USER_DATA_3 = 53, + ESP_SECURE_CERT_USER_DATA_4 = 54, + ESP_SECURE_CERT_USER_DATA_5 = 54, +} esp_secure_cert_tlv_type_t; + +/* + * Header for each tlv + */ +typedef struct esp_secure_cert_tlv_header { + uint32_t magic; + uint8_t reserved[4]; /* Reserved bytes for future use, the value currently should be 0xFF */ + uint16_t type; /* Type of tlv structure, this shall be typecasted + to esp_secure_cert_tlv_type_t for further use */ + uint16_t length; /* Length of the data */ + uint8_t value[0]; /* Actual data in form of byte array */ +} __attribute__((packed)) esp_secure_cert_tlv_header_t; + +/* + * Footer for each tlv + */ +typedef struct esp_secure_cert_tlv_footer { + uint32_t crc; /* crc of the data */ +} esp_secure_cert_tlv_footer_t; + +_Static_assert(sizeof(esp_secure_cert_tlv_header_t) == 12, "TLV header size should be 12 bytes"); + +_Static_assert(sizeof(esp_secure_cert_tlv_footer_t) == 4, "TLV footer size should be 4 bytes"); + +/* + * Note: + * + * The data stored in a cust flash partition should be as follows: + * + * tlv_header1 -> data_1 -> tlv_footer1 -> tlv_header2... + * + */ diff --git a/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_tlv_private.h b/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_tlv_private.h new file mode 100644 index 0000000..5cdec8c --- /dev/null +++ b/src/managed_components/espressif__esp_secure_cert_mgr/private_include/esp_secure_cert_tlv_private.h @@ -0,0 +1,60 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#pragma once +#include "esp_secure_cert_config.h" +#include "esp_secure_cert_tlv_config.h" + +#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +#include "rsa_sign_alt.h" +#endif +/* + * Get the flash address of a structure + * + * Note: This API also validates the crc of the respective tlv before returning the offset + * @input + * for calculating current crc for esp_secure_cert + * + * tlv_address Void pointer to store tlv address + * + * Note: If tlv type = ESP_SECURE_CERT_TLV_END then the address returned shall be the end address of current tlv formatted data. + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_err_t esp_secure_cert_tlv_get_addr(esp_secure_cert_tlv_type_t type, char **buffer, uint32_t *len); + +/* + * Identify if esp_secure_cert partition of type TLV is present. + * @return + * - 1 on if the partition is identified as TLV + * - 0 on failure + */ +bool esp_secure_cert_is_tlv_partition(void); + +#ifdef CONFIG_ESP_SECURE_CERT_DS_PERIPHERAL +/* @info + * This function returns the flash esp_ds_context which can then be + * directly provided to an esp-tls connection through its config structure. + * The memory for the context is dynamically allocated. + * The internal structures are however directly accessed from flash. + * e.g. esp_ds_data + * + * @params + * - ds_ctx The pointer to the DS context + * @return + * - ESP_OK On success + * - ESP_FAIL/other relevant esp error code + * On failure + */ +esp_ds_data_ctx_t *esp_secure_cert_tlv_get_ds_ctx(void); + +/* + *@info + * Free the ds context + */ +void esp_secure_cert_tlv_free_ds_ctx(esp_ds_data_ctx_t *ds_ctx); +#endif diff --git a/src/messaging/ReliableMessageContext.h b/src/messaging/ReliableMessageContext.h index 886ed5c..57ec456 100644 --- a/src/messaging/ReliableMessageContext.h +++ b/src/messaging/ReliableMessageContext.h @@ -36,6 +36,11 @@ #include namespace chip { +namespace app { +class TestCommandInteraction; +class TestReadInteraction; +class TestWriteInteraction; +} // namespace app namespace Messaging { class ChipMessageInfo; @@ -193,6 +198,9 @@ class ReliableMessageContext friend class ReliableMessageMgr; friend class ExchangeContext; friend class ExchangeMessageDispatch; + friend class ::chip::app::TestCommandInteraction; + friend class ::chip::app::TestReadInteraction; + friend class ::chip::app::TestWriteInteraction; System::Clock::Timestamp mNextAckTime; // Next time for triggering Solo Ack uint32_t mPendingPeerAckMessageCounter; diff --git a/src/messaging/ReliableMessageMgr.h b/src/messaging/ReliableMessageMgr.h index c50ca12..e2fc1e2 100644 --- a/src/messaging/ReliableMessageMgr.h +++ b/src/messaging/ReliableMessageMgr.h @@ -192,6 +192,15 @@ class ReliableMessageMgr #if CHIP_CONFIG_TEST // Functions for testing int TestGetCountRetransTable(); + + // Enumerate the retransmission table. Clearing an entry while enumerating + // that entry is allowed. F must take a RetransTableEntry as an argument + // and return Loop::Continue or Loop::Break. + template + void EnumerateRetransTable(F && functor) + { + mRetransTable.ForEachActiveObject(std::forward(functor)); + } #endif // CHIP_CONFIG_TEST private: diff --git a/src/platform/ESP32/ConfigurationManagerImpl.h b/src/platform/ESP32/ConfigurationManagerImpl.h index 32f4951..6443861 100644 --- a/src/platform/ESP32/ConfigurationManagerImpl.h +++ b/src/platform/ESP32/ConfigurationManagerImpl.h @@ -57,6 +57,7 @@ class ConfigurationManagerImpl : public Internal::GenericConfigurationManagerImp CHIP_ERROR GetProductLabel(char * buf, size_t bufSize) override; CHIP_ERROR GetSoftwareVersionString(char * buf, size_t bufSize); CHIP_ERROR GetSoftwareVersion(uint32_t & softwareVer) override; + CHIP_ERROR GetLocationCapability(uint8_t & location) override; static ConfigurationManagerImpl & GetDefaultInstance(); private: diff --git a/src/platform/ESP32/ESP32Config.h b/src/platform/ESP32/ESP32Config.h index f884af8..6a008db 100644 --- a/src/platform/ESP32/ESP32Config.h +++ b/src/platform/ESP32/ESP32Config.h @@ -79,6 +79,7 @@ class ESP32Config static const Key kConfigKey_SupportedCalTypes; static const Key kConfigKey_SupportedLocaleSize; static const Key kConfigKey_RotatingDevIdUniqueId; + static const Key kConfigKey_LocationCapability; // CHIP Config keys static const Key kConfigKey_ServiceConfig; diff --git a/src/platform/Zephyr/KeyValueStoreManagerImpl.h b/src/platform/Zephyr/KeyValueStoreManagerImpl.h index daaba11..1d4ee7d 100644 --- a/src/platform/Zephyr/KeyValueStoreManagerImpl.h +++ b/src/platform/Zephyr/KeyValueStoreManagerImpl.h @@ -24,7 +24,7 @@ #pragma once -#include +#include namespace chip { namespace DeviceLayer { diff --git a/src/platform/Zephyr/ThreadStackManagerImpl.h b/src/platform/Zephyr/ThreadStackManagerImpl.h index 19e5d7e..6600554 100644 --- a/src/platform/Zephyr/ThreadStackManagerImpl.h +++ b/src/platform/Zephyr/ThreadStackManagerImpl.h @@ -25,8 +25,8 @@ #include -#include -#include +#include +#include #include #if !CONFIG_SOC_SERIES_RISCV_TELINK_B91 diff --git a/src/platform/bouffalolab/BL602/lwip_default_hooks.h b/src/platform/bouffalolab/BL602/lwip_default_hooks.h new file mode 100644 index 0000000..3c1d1d1 --- /dev/null +++ b/src/platform/bouffalolab/BL602/lwip_default_hooks.h @@ -0,0 +1,17 @@ +#ifndef _LWIP_DEFAULT_HOOKS_H_ +#define _LWIP_DEFAULT_HOOKS_H_ +#include "lwip/arch.h" +#include "lwip/err.h" +#include "lwip/ip_addr.h" + +#ifdef CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT +extern struct netif * lwip_hook_ip6_route(const ip6_addr_t * src, const ip6_addr_t * dest); +#define LWIP_HOOK_IP6_ROUTE lwip_hook_ip6_route +#endif + +#ifdef CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT +extern const ip6_addr_t * lwip_hook_nd6_get_gw(struct netif * netif, const ip6_addr_t * dest); +#define LWIP_HOOK_ND6_GET_GW lwip_hook_nd6_get_gw +#endif + +#endif /* _LWIP_DEFAULT_HOOKS_H_ */ diff --git a/src/platform/internal/GenericPlatformManagerImpl_Zephyr.h b/src/platform/internal/GenericPlatformManagerImpl_Zephyr.h index c4dd017..c3f1de4 100644 --- a/src/platform/internal/GenericPlatformManagerImpl_Zephyr.h +++ b/src/platform/internal/GenericPlatformManagerImpl_Zephyr.h @@ -27,7 +27,7 @@ #include #include -#include +#include namespace chip { namespace DeviceLayer { diff --git a/src/platform/telink/BLEManagerImpl.h b/src/platform/telink/BLEManagerImpl.h index 86aae89..d1b7971 100644 --- a/src/platform/telink/BLEManagerImpl.h +++ b/src/platform/telink/BLEManagerImpl.h @@ -107,7 +107,7 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla CHIPoBLEServiceMode mServiceMode; BitFlags mFlags; char mDeviceName[kMaxDeviceNameLength + 1]; - uint16_t mNumConnections; + uint16_t mGAPConns; bool mSubscribedConns[kMaxConnections]; uint8_t mAdvDataBuf[kMaxAdvertisementDataSetSize]; uint8_t mScanRespDataBuf[kMaxAdvertisementDataSetSize]; @@ -144,6 +144,8 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla /* Callbacks from BLE stack*/ static void DriveBLEState(intptr_t arg); + static void HandleBLEAdvertisementIntervalChange(System::Layer * layer, void * param); + /* Handlers for stack events */ static void CancelBleAdvTimeoutTimer(void); static void StartBleAdvTimeoutTimer(uint32_t aTimeoutInMs); diff --git a/src/platform/telink/CHIPDevicePlatformConfig.h b/src/platform/telink/CHIPDevicePlatformConfig.h index dadbf34..6a40ab8 100644 --- a/src/platform/telink/CHIPDevicePlatformConfig.h +++ b/src/platform/telink/CHIPDevicePlatformConfig.h @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2021 Project CHIP Authors + * Copyright (c) 2022 Project CHIP Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,42 @@ // ==================== Platform Adaptations ==================== +#ifndef CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER +#define CHIP_DEVICE_CONFIG_TEST_SERIAL_NUMBER CONFIG_CHIP_DEVICE_SERIAL_NUMBER +#endif + +#ifndef CHIP_DEVICE_CONFIG_TEST_MANUFACTURING_DATE +#define CHIP_DEVICE_CONFIG_TEST_MANUFACTURING_DATE CONFIG_CHIP_DEVICE_MANUFACTURING_DATE +#endif + +#ifndef CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION +#define CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION CONFIG_CHIP_DEVICE_HARDWARE_VERSION +#endif + +#ifndef CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE +#define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE CONFIG_CHIP_DEVICE_PAIRING_PASSCODE +#endif + +#ifndef CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR +#define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR CONFIG_CHIP_DEVICE_DISCRIMINATOR +#endif + +#ifndef CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION_STRING +#define CHIP_DEVICE_CONFIG_DEFAULT_DEVICE_HARDWARE_VERSION_STRING CONFIG_CHIP_DEVICE_HARDWARE_VERSION_STRING +#endif + +#ifndef CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT +#define CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_ITERATION_COUNT CONFIG_CHIP_DEVICE_SPAKE2_IT +#endif + +#ifndef CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT +#define CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_SALT CONFIG_CHIP_DEVICE_SPAKE2_SALT +#endif + +#ifndef CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER +#define CHIP_DEVICE_CONFIG_USE_TEST_SPAKE2P_VERIFIER CONFIG_CHIP_DEVICE_SPAKE2_TEST_VERIFIER +#endif + #ifndef CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID #define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_ID CONFIG_CHIP_DEVICE_VENDOR_ID #endif @@ -35,8 +71,12 @@ #define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_ID CONFIG_CHIP_DEVICE_PRODUCT_ID #endif -#ifdef CONFIG_CHIP_DEVICE_TYPE -#define CHIP_DEVICE_CONFIG_DEVICE_TYPE CONFIG_CHIP_DEVICE_TYPE +#ifndef CHIP_DEVICE_CONFIG_DEVICE_VENDOR_NAME +#define CHIP_DEVICE_CONFIG_DEVICE_VENDOR_NAME CONFIG_CHIP_DEVICE_VENDOR_NAME +#endif + +#ifndef CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_NAME +#define CHIP_DEVICE_CONFIG_DEVICE_PRODUCT_NAME CONFIG_CHIP_DEVICE_PRODUCT_NAME #endif #ifndef CHIP_DEVICE_CONFIG_DEVICE_SOFTWARE_VERSION @@ -82,6 +122,58 @@ #endif // !defined(CONFIG_CHIP_MALLOC_SYS_HEAP) && defined(CONFIG_NEWLIB_LIBC) #endif // CHIP_DEVICE_CONFIG_HEAP_STATISTICS_MALLINFO +#ifndef CHIP_DEVICE_CONFIG_CERTIFICATION_DECLARATION +//-> format_version = 1 +//-> vendor_id = 0xFFF1 +//-> product_id_array = [ 0x8000, 0x8001, 0x8002, 0x8003, 0x8004, 0x8005, 0x8006, 0x8007, 0x8008, 0x8009, 0x800A, 0x800B, +// 0x800C, 0x800D, 0x800E, 0x800F, 0x8010, 0x8011, 0x8012, 0x8013, 0x8014, 0x8015, 0x8016, 0x8017, 0x8018, 0x8019, 0x801A, +// 0x801B, 0x801C, 0x801D, 0x801E, 0x801F, 0x8020, 0x8021, 0x8022, 0x8023, 0x8024, 0x8025, 0x8026, 0x8027, 0x8028, 0x8029, +// 0x802A, 0x802B, 0x802C, 0x802D, 0x802E, 0x802F, 0x8030, 0x8031, 0x8032, 0x8033, 0x8034, 0x8035, 0x8036, 0x8037, 0x8038, +// 0x8039, 0x803A, 0x803B, 0x803C, 0x803D, 0x803E, 0x803F, 0x8040, 0x8041, 0x8042, 0x8043, 0x8044, 0x8045, 0x8046, 0x8047, +// 0x8048, 0x8049, 0x804A, 0x804B, 0x804C, 0x804D, 0x804E, 0x804F, 0x8050, 0x8051, 0x8052, 0x8053, 0x8054, 0x8055, 0x8056, +// 0x8057, 0x8058, 0x8059, 0x805A, 0x805B, 0x805C, 0x805D, 0x805E, 0x805F, 0x8060, 0x8061, 0x8062, 0x8063 ] +//-> device_type_id = 0x0016 +//-> certificate_id = "ZIG20142ZB330003-24" +//-> security_level = 0 +//-> security_information = 0 +//-> version_number = 0x2694 +//-> certification_type = 0 +//-> dac_origin_vendor_id is not present +//-> dac_origin_product_id is not present +#define CHIP_DEVICE_CONFIG_CERTIFICATION_DECLARATION \ + { \ + 0x30, 0x82, 0x02, 0x19, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x02, 0xa0, 0x82, 0x02, 0x0a, 0x30, \ + 0x82, 0x02, 0x06, 0x02, 0x01, 0x03, 0x31, 0x0d, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, \ + 0x02, 0x01, 0x30, 0x82, 0x01, 0x71, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x82, \ + 0x01, 0x62, 0x04, 0x82, 0x01, 0x5e, 0x15, 0x24, 0x00, 0x01, 0x25, 0x01, 0xf1, 0xff, 0x36, 0x02, 0x05, 0x00, 0x80, \ + 0x05, 0x01, 0x80, 0x05, 0x02, 0x80, 0x05, 0x03, 0x80, 0x05, 0x04, 0x80, 0x05, 0x05, 0x80, 0x05, 0x06, 0x80, 0x05, \ + 0x07, 0x80, 0x05, 0x08, 0x80, 0x05, 0x09, 0x80, 0x05, 0x0a, 0x80, 0x05, 0x0b, 0x80, 0x05, 0x0c, 0x80, 0x05, 0x0d, \ + 0x80, 0x05, 0x0e, 0x80, 0x05, 0x0f, 0x80, 0x05, 0x10, 0x80, 0x05, 0x11, 0x80, 0x05, 0x12, 0x80, 0x05, 0x13, 0x80, \ + 0x05, 0x14, 0x80, 0x05, 0x15, 0x80, 0x05, 0x16, 0x80, 0x05, 0x17, 0x80, 0x05, 0x18, 0x80, 0x05, 0x19, 0x80, 0x05, \ + 0x1a, 0x80, 0x05, 0x1b, 0x80, 0x05, 0x1c, 0x80, 0x05, 0x1d, 0x80, 0x05, 0x1e, 0x80, 0x05, 0x1f, 0x80, 0x05, 0x20, \ + 0x80, 0x05, 0x21, 0x80, 0x05, 0x22, 0x80, 0x05, 0x23, 0x80, 0x05, 0x24, 0x80, 0x05, 0x25, 0x80, 0x05, 0x26, 0x80, \ + 0x05, 0x27, 0x80, 0x05, 0x28, 0x80, 0x05, 0x29, 0x80, 0x05, 0x2a, 0x80, 0x05, 0x2b, 0x80, 0x05, 0x2c, 0x80, 0x05, \ + 0x2d, 0x80, 0x05, 0x2e, 0x80, 0x05, 0x2f, 0x80, 0x05, 0x30, 0x80, 0x05, 0x31, 0x80, 0x05, 0x32, 0x80, 0x05, 0x33, \ + 0x80, 0x05, 0x34, 0x80, 0x05, 0x35, 0x80, 0x05, 0x36, 0x80, 0x05, 0x37, 0x80, 0x05, 0x38, 0x80, 0x05, 0x39, 0x80, \ + 0x05, 0x3a, 0x80, 0x05, 0x3b, 0x80, 0x05, 0x3c, 0x80, 0x05, 0x3d, 0x80, 0x05, 0x3e, 0x80, 0x05, 0x3f, 0x80, 0x05, \ + 0x40, 0x80, 0x05, 0x41, 0x80, 0x05, 0x42, 0x80, 0x05, 0x43, 0x80, 0x05, 0x44, 0x80, 0x05, 0x45, 0x80, 0x05, 0x46, \ + 0x80, 0x05, 0x47, 0x80, 0x05, 0x48, 0x80, 0x05, 0x49, 0x80, 0x05, 0x4a, 0x80, 0x05, 0x4b, 0x80, 0x05, 0x4c, 0x80, \ + 0x05, 0x4d, 0x80, 0x05, 0x4e, 0x80, 0x05, 0x4f, 0x80, 0x05, 0x50, 0x80, 0x05, 0x51, 0x80, 0x05, 0x52, 0x80, 0x05, \ + 0x53, 0x80, 0x05, 0x54, 0x80, 0x05, 0x55, 0x80, 0x05, 0x56, 0x80, 0x05, 0x57, 0x80, 0x05, 0x58, 0x80, 0x05, 0x59, \ + 0x80, 0x05, 0x5a, 0x80, 0x05, 0x5b, 0x80, 0x05, 0x5c, 0x80, 0x05, 0x5d, 0x80, 0x05, 0x5e, 0x80, 0x05, 0x5f, 0x80, \ + 0x05, 0x60, 0x80, 0x05, 0x61, 0x80, 0x05, 0x62, 0x80, 0x05, 0x63, 0x80, 0x18, 0x24, 0x03, 0x16, 0x2c, 0x04, 0x13, \ + 0x5a, 0x49, 0x47, 0x32, 0x30, 0x31, 0x34, 0x32, 0x5a, 0x42, 0x33, 0x33, 0x30, 0x30, 0x30, 0x33, 0x2d, 0x32, 0x34, \ + 0x24, 0x05, 0x00, 0x24, 0x06, 0x00, 0x25, 0x07, 0x94, 0x26, 0x24, 0x08, 0x00, 0x18, 0x31, 0x7d, 0x30, 0x7b, 0x02, \ + 0x01, 0x03, 0x80, 0x14, 0x62, 0xfa, 0x82, 0x33, 0x59, 0xac, 0xfa, 0xa9, 0x96, 0x3e, 0x1c, 0xfa, 0x14, 0x0a, 0xdd, \ + 0xf5, 0x04, 0xf3, 0x71, 0x60, 0x30, 0x0b, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x30, \ + 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x04, 0x47, 0x30, 0x45, 0x02, 0x20, 0x24, 0xe5, \ + 0xd1, 0xf4, 0x7a, 0x7d, 0x7b, 0x0d, 0x20, 0x6a, 0x26, 0xef, 0x69, 0x9b, 0x7c, 0x97, 0x57, 0xb7, 0x2d, 0x46, 0x90, \ + 0x89, 0xde, 0x31, 0x92, 0xe6, 0x78, 0xc7, 0x45, 0xe7, 0xf6, 0x0c, 0x02, 0x21, 0x00, 0xf8, 0xaa, 0x2f, 0xa7, 0x11, \ + 0xfc, 0xb7, 0x9b, 0x97, 0xe3, 0x97, 0xce, 0xda, 0x66, 0x7b, 0xae, 0x46, 0x4e, 0x2b, 0xd3, 0xff, 0xdf, 0xc3, 0xcc, \ + 0xed, 0x7a, 0xa8, 0xca, 0x5f, 0x4c, 0x1a, 0x7c \ + } +#endif + // ========== Platform-specific Configuration Overrides ========= #ifndef CHIP_DEVICE_CONFIG_CHIP_TASK_PRIORITY @@ -114,3 +206,11 @@ #define CHIP_DEVICE_CONFIG_ENABLE_THREAD_COMMISSIONABLE_DISCOVERY 1 #endif // CONFIG_CHIP_ENABLE_DNS_CLIENT #endif // CONFIG_CHIP_ENABLE_DNSSD_SRP + +#ifdef CONFIG_CHIP_DEVICE_TYPE +#define CHIP_DEVICE_CONFIG_DEVICE_TYPE CONFIG_CHIP_DEVICE_TYPE +#endif // CONFIG_CHIP_DEVICE_TYPE + +#ifdef CONFIG_CHIP_EXTENDED_DISCOVERY +#define CHIP_DEVICE_CONFIG_ENABLE_EXTENDED_DISCOVERY 1 +#endif // CONFIG_CHIP_EXTENDED_DISCOVERY diff --git a/src/platform/telink/CHIPDevicePlatformEvent.h b/src/platform/telink/CHIPDevicePlatformEvent.h index f777257..b040595 100644 --- a/src/platform/telink/CHIPDevicePlatformEvent.h +++ b/src/platform/telink/CHIPDevicePlatformEvent.h @@ -26,7 +26,7 @@ #include -#include +#include namespace chip { namespace DeviceLayer { diff --git a/src/platform/telink/FactoryDataParser.h b/src/platform/telink/FactoryDataParser.h new file mode 100644 index 0000000..156010a --- /dev/null +++ b/src/platform/telink/FactoryDataParser.h @@ -0,0 +1,78 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct FactoryDataString +{ + void * data; + size_t len; +}; + +struct FactoryData +{ + uint16_t version; + struct FactoryDataString sn; + uint16_t date_year; + uint8_t date_month; + uint8_t date_day; + uint16_t vendor_id; + uint16_t product_id; + struct FactoryDataString vendor_name; + struct FactoryDataString product_name; + uint16_t hw_ver; + struct FactoryDataString hw_ver_str; + struct FactoryDataString rd_uid; + struct FactoryDataString dac_cert; + struct FactoryDataString dac_priv_key; + struct FactoryDataString pai_cert; + uint32_t spake2_it; + struct FactoryDataString spake2_salt; + struct FactoryDataString spake2_verifier; + uint16_t discriminator; + uint32_t passcode; + struct FactoryDataString enable_key; + struct FactoryDataString user; + + bool vendorIdPresent; + bool productIdPresent; + bool hwVerPresent; + bool discriminatorPresent; +}; + +/** + * @brief Parses raw factory data into the factory data structure. + * + * @param[in] buffer Buffer containing raw factory data. + * @param[in] bufferSize Size of factory data. + * @param[out] factoryData address of object to be filled with parsed factory data. + * + * @returns true on success, false otherwise. + */ +bool ParseFactoryData(uint8_t * buffer, uint16_t bufferSize, struct FactoryData * factoryData); + +#ifdef __cplusplus +} +#endif diff --git a/src/platform/telink/FactoryDataProvider.h b/src/platform/telink/FactoryDataProvider.h new file mode 100644 index 0000000..79d34a6 --- /dev/null +++ b/src/platform/telink/FactoryDataProvider.h @@ -0,0 +1,117 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include +#include + +#include +#include +#include + +#include "FactoryDataParser.h" + +namespace chip { +namespace DeviceLayer { + +struct InternalFlashFactoryData +{ + CHIP_ERROR GetFactoryDataPartition(uint8_t *& data, size_t & dataSize) + { + data = reinterpret_cast(FLASH_AREA_OFFSET(factory_data)); + dataSize = FLASH_AREA_SIZE(factory_data); + return CHIP_NO_ERROR; + } + + CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite() { return CHIP_ERROR_NOT_IMPLEMENTED; } +}; + +struct ExternalFlashFactoryData +{ + CHIP_ERROR GetFactoryDataPartition(uint8_t *& data, size_t & dataSize) + { + int ret = flash_read(mFlashDevice, FLASH_AREA_OFFSET(factory_data), mFactoryDataBuffer, FLASH_AREA_SIZE(factory_data)); + + if (ret != 0) + { + return CHIP_ERROR_READ_FAILED; + } + + data = mFactoryDataBuffer; + dataSize = FLASH_AREA_SIZE(factory_data); + + return CHIP_NO_ERROR; + } + + CHIP_ERROR ProtectFactoryDataPartitionAgainstWrite() { return CHIP_ERROR_NOT_IMPLEMENTED; } + + const struct device * mFlashDevice = DEVICE_DT_GET(DT_CHOSEN(zephyr_flash_controller)); + uint8_t mFactoryDataBuffer[FLASH_AREA_SIZE(factory_data)]; +}; + +template +class FactoryDataProvider : public chip::Credentials::DeviceAttestationCredentialsProvider, + public CommissionableDataProvider, + public DeviceInstanceInfoProvider +{ +public: + CHIP_ERROR Init(); + + // ===== Members functions that implement the DeviceAttestationCredentialsProvider + CHIP_ERROR GetCertificationDeclaration(MutableByteSpan & outBuffer) override; + CHIP_ERROR GetFirmwareInformation(MutableByteSpan & out_firmware_info_buffer) override; + CHIP_ERROR GetDeviceAttestationCert(MutableByteSpan & outBuffer) override; + CHIP_ERROR GetProductAttestationIntermediateCert(MutableByteSpan & outBuffer) override; + CHIP_ERROR SignWithDeviceAttestationKey(const ByteSpan & messageToSign, MutableByteSpan & outSignBuffer) override; + + // ===== Members functions that implement the CommissionableDataProvider + CHIP_ERROR GetSetupDiscriminator(uint16_t & setupDiscriminator) override; + CHIP_ERROR SetSetupDiscriminator(uint16_t setupDiscriminator) override; + CHIP_ERROR GetSpake2pIterationCount(uint32_t & iterationCount) override; + CHIP_ERROR GetSpake2pSalt(MutableByteSpan & saltBuf) override; + CHIP_ERROR GetSpake2pVerifier(MutableByteSpan & verifierBuf, size_t & verifierLen) override; + CHIP_ERROR GetSetupPasscode(uint32_t & setupPasscode) override; + CHIP_ERROR SetSetupPasscode(uint32_t setupPasscode) override; + + // ===== Members functions that implement the DeviceInstanceInfoProvider + CHIP_ERROR GetVendorName(char * buf, size_t bufSize) override; + CHIP_ERROR GetVendorId(uint16_t & vendorId) override; + CHIP_ERROR GetProductName(char * buf, size_t bufSize) override; + CHIP_ERROR GetProductId(uint16_t & productId) override; + CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize) override; + CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & day) override; + CHIP_ERROR GetHardwareVersion(uint16_t & hardwareVersion) override; + CHIP_ERROR GetHardwareVersionString(char * buf, size_t bufSize) override; + CHIP_ERROR GetRotatingDeviceIdUniqueId(MutableByteSpan & uniqueIdSpan) override; + + // ===== Members functions that are platform-specific + CHIP_ERROR GetEnableKey(MutableByteSpan & enableKey); + +private: + static constexpr uint16_t kFactoryDataPartitionSize = FLASH_AREA_SIZE(factory_data); + static constexpr uint32_t kFactoryDataPartitionAddress = FLASH_AREA_OFFSET(factory_data); + static constexpr uint8_t kDACPrivateKeyLength = 32; + static constexpr uint8_t kDACPublicKeyLength = 65; + + struct FactoryData mFactoryData; + FlashFactoryData mFlashFactoryData; +}; + +} // namespace DeviceLayer +} // namespace chip diff --git a/src/platform/telink/crypto/aes_alt.h b/src/platform/telink/crypto/aes_alt.h deleted file mode 100644 index c8560e8..0000000 --- a/src/platform/telink/crypto/aes_alt.h +++ /dev/null @@ -1,56 +0,0 @@ -/* aes_alt.h */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef AES_ALT_H -#define AES_ALT_H - -#if defined(MBEDTLS_AES_ALT) -// Regular implementation -// - -/** - * \brief The AES context-type definition. - */ -typedef struct mbedtls_aes_context -{ - int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */ - uint32_t * MBEDTLS_PRIVATE(rk); /*!< AES round keys. */ - uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can - hold 32 extra Bytes, which can be used for - one of the following purposes: -
  • Alignment if VIA padlock is used.
  • -
  • Simplifying key expansion in the 256-bit - case by generating an extra round key. -
*/ -} mbedtls_aes_context; - -#if defined(MBEDTLS_CIPHER_MODE_XTS) -/** - * \brief The AES XTS context-type definition. - */ -typedef struct mbedtls_aes_xts_context -{ - mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block encryption or decryption. */ - mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak computation. */ -} mbedtls_aes_xts_context; - -#endif /* MBEDTLS_CIPHER_MODE_XTS */ - -#endif /* MBEDTLS_AES_ALT */ - -#endif /* aes_alt.h */ diff --git a/src/platform/telink/crypto/ecp_alt.h b/src/platform/telink/crypto/ecp_alt.h deleted file mode 100644 index 920774c..0000000 --- a/src/platform/telink/crypto/ecp_alt.h +++ /dev/null @@ -1,105 +0,0 @@ -/** - * \file ecp_alt.h - * - * \brief Utility macros for internal use in the library - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef ECP_ALT_H -#define ECP_ALT_H - -#if defined(MBEDTLS_ECP_ALT) - -typedef struct mbedtls_ecp_group -{ - mbedtls_ecp_group_id id; /*!< An internal group identifier. */ - mbedtls_mpi P; /*!< The prime modulus of the base field. */ - mbedtls_mpi A; /*!< For Short Weierstrass: \p A in the equation. For - Montgomery curves: (A + 2) / 4. */ - mbedtls_mpi B; /*!< For Short Weierstrass: \p B in the equation. - For Montgomery curves: unused. */ - mbedtls_ecp_point G; /*!< The generator of the subgroup used. */ - mbedtls_mpi N; /*!< The order of \p G. */ - size_t pbits; /*!< The number of bits in \p P.*/ - size_t nbits; /*!< For Short Weierstrass: The number of bits in \p P. - For Montgomery curves: the number of bits in the - private keys. */ - /* End of public fields */ - - unsigned int MBEDTLS_PRIVATE(h); /*!< \internal 1 if the constants are static. */ - int (*MBEDTLS_PRIVATE(modp))(mbedtls_mpi *); /*!< The function for fast pseudo-reduction mod \p P (see above).*/ - int (*MBEDTLS_PRIVATE(t_pre))(mbedtls_ecp_point *, void *); /*!< Unused. */ - int (*MBEDTLS_PRIVATE(t_post))(mbedtls_ecp_point *, void *); /*!< Unused. */ - void * MBEDTLS_PRIVATE(t_data); /*!< Unused. */ - mbedtls_ecp_point * MBEDTLS_PRIVATE(T); /*!< Pre-computed points for ecp_mul_comb(). */ - size_t MBEDTLS_PRIVATE(T_size); /*!< The number of dynamic allocated pre-computed points. */ -} mbedtls_ecp_group; - -/** - * \name SECTION: Module settings - * - * The configuration options you can set for this module are in this section. - * Either change them in mbedtls_config.h, or define them using the compiler command line. - * \{ - */ - -#if !defined(MBEDTLS_ECP_WINDOW_SIZE) -/* - * Maximum "window" size used for point multiplication. - * Default: a point where higher memory usage yields disminishing performance - * returns. - * Minimum value: 2. Maximum value: 7. - * - * Result is an array of at most ( 1 << ( MBEDTLS_ECP_WINDOW_SIZE - 1 ) ) - * points used for point multiplication. This value is directly tied to EC - * peak memory usage, so decreasing it by one should roughly cut memory usage - * by two (if large curves are in use). - * - * Reduction in size may reduce speed, but larger curves are impacted first. - * Sample performances (in ECDHE handshakes/s, with FIXED_POINT_OPTIM = 1): - * w-size: 6 5 4 3 2 - * 521 145 141 135 120 97 - * 384 214 209 198 177 146 - * 256 320 320 303 262 226 - * 224 475 475 453 398 342 - * 192 640 640 633 587 476 - */ -#define MBEDTLS_ECP_WINDOW_SIZE 4 /**< The maximum window size used. */ -#endif /* MBEDTLS_ECP_WINDOW_SIZE */ - -#if !defined(MBEDTLS_ECP_FIXED_POINT_OPTIM) -/* - * Trade code size for speed on fixed-point multiplication. - * - * This speeds up repeated multiplication of the generator (that is, the - * multiplication in ECDSA signatures, and half of the multiplications in - * ECDSA verification and ECDHE) by a factor roughly 3 to 4. - * - * For each n-bit Short Weierstrass curve that is enabled, this adds 4n bytes - * of code size if n < 384 and 8n otherwise. - * - * Change this value to 0 to reduce code size. - */ -#define MBEDTLS_ECP_FIXED_POINT_OPTIM 1 /**< Enable fixed-point speed-up. */ -#endif /* MBEDTLS_ECP_FIXED_POINT_OPTIM */ - -/* \} name SECTION: Module settings */ - -#endif /* MBEDTLS_ECP_ALT */ - -#endif /* ecp_alt.h */ diff --git a/src/platform/telink/crypto/internal/common.h b/src/platform/telink/crypto/internal/common.h deleted file mode 100644 index a2c8a1e..0000000 --- a/src/platform/telink/crypto/internal/common.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * \file common.h - * - * \brief Utility macros for internal use in the library - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MBEDTLS_LIBRARY_COMMON_H -#define MBEDTLS_LIBRARY_COMMON_H - -#include "mbedtls/build_info.h" - -/** Helper to define a function as static except when building invasive tests. - * - * If a function is only used inside its own source file and should be - * declared `static` to allow the compiler to optimize for code size, - * but that function has unit tests, define it with - * ``` - * MBEDTLS_STATIC_TESTABLE int mbedtls_foo(...) { ... } - * ``` - * and declare it in a header in the `library/` directory with - * ``` - * #if defined(MBEDTLS_TEST_HOOKS) - * int mbedtls_foo(...); - * #endif - * ``` - */ -#if defined(MBEDTLS_TEST_HOOKS) -#define MBEDTLS_STATIC_TESTABLE -#else -#define MBEDTLS_STATIC_TESTABLE static -#endif - -/** Allow library to access its structs' private members. - * - * Although structs defined in header files are publicly available, - * their members are private and should not be accessed by the user. - */ -#define MBEDTLS_ALLOW_PRIVATE_ACCESS - -#endif /* MBEDTLS_LIBRARY_COMMON_H */ diff --git a/src/platform/telink/crypto/internal/compatibility/aesni.h b/src/platform/telink/crypto/internal/compatibility/aesni.h deleted file mode 100644 index c53ff3f..0000000 --- a/src/platform/telink/crypto/internal/compatibility/aesni.h +++ /dev/null @@ -1,121 +0,0 @@ -/** - * \file aesni.h - * - * \brief AES-NI for hardware AES acceleration on some Intel processors - * - * \warning These functions are only for internal use by other library - * functions; you must not call them directly. - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MBEDTLS_AESNI_H -#define MBEDTLS_AESNI_H - -#include "mbedtls/build_info.h" - -#include "mbedtls/aes.h" - -#define MBEDTLS_AESNI_AES 0x02000000u -#define MBEDTLS_AESNI_CLMUL 0x00000002u - -#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && (defined(__amd64__) || defined(__x86_64__)) && !defined(MBEDTLS_HAVE_X86_64) -#define MBEDTLS_HAVE_X86_64 -#endif - -#if defined(MBEDTLS_HAVE_X86_64) - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * \brief Internal function to detect the AES-NI feature in CPUs. - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param what The feature to detect - * (MBEDTLS_AESNI_AES or MBEDTLS_AESNI_CLMUL) - * - * \return 1 if CPU has support for the feature, 0 otherwise - */ -int mbedtls_aesni_has_support(unsigned int what); - -/** - * \brief Internal AES-NI AES-ECB block encryption and decryption - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param ctx AES context - * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT - * \param input 16-byte input block - * \param output 16-byte output block - * - * \return 0 on success (cannot fail) - */ -int mbedtls_aesni_crypt_ecb(mbedtls_aes_context * ctx, int mode, const unsigned char input[16], unsigned char output[16]); - -/** - * \brief Internal GCM multiplication: c = a * b in GF(2^128) - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param c Result - * \param a First operand - * \param b Second operand - * - * \note Both operands and result are bit strings interpreted as - * elements of GF(2^128) as per the GCM spec. - */ -void mbedtls_aesni_gcm_mult(unsigned char c[16], const unsigned char a[16], const unsigned char b[16]); - -/** - * \brief Internal round key inversion. This function computes - * decryption round keys from the encryption round keys. - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param invkey Round keys for the equivalent inverse cipher - * \param fwdkey Original round keys (for encryption) - * \param nr Number of rounds (that is, number of round keys minus one) - */ -void mbedtls_aesni_inverse_key(unsigned char * invkey, const unsigned char * fwdkey, int nr); - -/** - * \brief Internal key expansion for encryption - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param rk Destination buffer where the round keys are written - * \param key Encryption key - * \param bits Key size in bits (must be 128, 192 or 256) - * - * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH - */ -int mbedtls_aesni_setkey_enc(unsigned char * rk, const unsigned char * key, size_t bits); - -#ifdef __cplusplus -} -#endif - -#endif /* MBEDTLS_HAVE_X86_64 */ - -#endif /* MBEDTLS_AESNI_H */ diff --git a/src/platform/telink/crypto/internal/compatibility/bn_mul.h b/src/platform/telink/crypto/internal/compatibility/bn_mul.h deleted file mode 100644 index 3de9f26..0000000 --- a/src/platform/telink/crypto/internal/compatibility/bn_mul.h +++ /dev/null @@ -1,891 +0,0 @@ -/** - * \file bn_mul.h - * - * \brief Multi-precision integer library - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* - * Multiply source vector [s] with b, add result - * to destination vector [d] and set carry c. - * - * Currently supports: - * - * . IA-32 (386+) . AMD64 / EM64T - * . IA-32 (SSE2) . Motorola 68000 - * . PowerPC, 32-bit . MicroBlaze - * . PowerPC, 64-bit . TriCore - * . SPARC v8 . ARM v3+ - * . Alpha . MIPS32 - * . C, longlong . C, generic - */ -#ifndef MBEDTLS_BN_MUL_H -#define MBEDTLS_BN_MUL_H - -#include "mbedtls/build_info.h" - -#include "mbedtls/bignum.h" - -/* - * Conversion macros for embedded constants: - * build lists of mbedtls_mpi_uint's from lists of unsigned char's grouped by 8, 4 or 2 - */ -#if defined(MBEDTLS_HAVE_INT32) - -#define MBEDTLS_BYTES_TO_T_UINT_4(a, b, c, d) \ - ((mbedtls_mpi_uint)(a) << 0) | ((mbedtls_mpi_uint)(b) << 8) | ((mbedtls_mpi_uint)(c) << 16) | ((mbedtls_mpi_uint)(d) << 24) - -#define MBEDTLS_BYTES_TO_T_UINT_2(a, b) MBEDTLS_BYTES_TO_T_UINT_4(a, b, 0, 0) - -#define MBEDTLS_BYTES_TO_T_UINT_8(a, b, c, d, e, f, g, h) \ - MBEDTLS_BYTES_TO_T_UINT_4(a, b, c, d), MBEDTLS_BYTES_TO_T_UINT_4(e, f, g, h) - -#else /* 64-bits */ - -#define MBEDTLS_BYTES_TO_T_UINT_8(a, b, c, d, e, f, g, h) \ - ((mbedtls_mpi_uint)(a) << 0) | ((mbedtls_mpi_uint)(b) << 8) | ((mbedtls_mpi_uint)(c) << 16) | ((mbedtls_mpi_uint)(d) << 24) | \ - ((mbedtls_mpi_uint)(e) << 32) | ((mbedtls_mpi_uint)(f) << 40) | ((mbedtls_mpi_uint)(g) << 48) | \ - ((mbedtls_mpi_uint)(h) << 56) - -#define MBEDTLS_BYTES_TO_T_UINT_4(a, b, c, d) MBEDTLS_BYTES_TO_T_UINT_8(a, b, c, d, 0, 0, 0, 0) - -#define MBEDTLS_BYTES_TO_T_UINT_2(a, b) MBEDTLS_BYTES_TO_T_UINT_8(a, b, 0, 0, 0, 0, 0, 0) - -#endif /* bits in mbedtls_mpi_uint */ - -#if defined(MBEDTLS_HAVE_ASM) - -#ifndef asm -#define asm __asm -#endif - -/* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */ -#if defined(__GNUC__) && (!defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000) - -/* - * Disable use of the i386 assembly code below if option -O0, to disable all - * compiler optimisations, is passed, detected with __OPTIMIZE__ - * This is done as the number of registers used in the assembly code doesn't - * work with the -O0 option. - */ -#if defined(__i386__) && defined(__OPTIMIZE__) - -#define MULADDC_INIT \ - asm( \ - "movl %%ebx, %0 \n\t" \ - "movl %5, %%esi \n\t" \ - "movl %6, %%edi \n\t" \ - "movl %7, %%ecx \n\t" \ - "movl %8, %%ebx \n\t" - -#define MULADDC_CORE \ - "lodsl \n\t" \ - "mull %%ebx \n\t" \ - "addl %%ecx, %%eax \n\t" \ - "adcl $0, %%edx \n\t" \ - "addl (%%edi), %%eax \n\t" \ - "adcl $0, %%edx \n\t" \ - "movl %%edx, %%ecx \n\t" \ - "stosl \n\t" - -#if defined(MBEDTLS_HAVE_SSE2) - -#define MULADDC_HUIT \ - "movd %%ecx, %%mm1 \n\t" \ - "movd %%ebx, %%mm0 \n\t" \ - "movd (%%edi), %%mm3 \n\t" \ - "paddq %%mm3, %%mm1 \n\t" \ - "movd (%%esi), %%mm2 \n\t" \ - "pmuludq %%mm0, %%mm2 \n\t" \ - "movd 4(%%esi), %%mm4 \n\t" \ - "pmuludq %%mm0, %%mm4 \n\t" \ - "movd 8(%%esi), %%mm6 \n\t" \ - "pmuludq %%mm0, %%mm6 \n\t" \ - "movd 12(%%esi), %%mm7 \n\t" \ - "pmuludq %%mm0, %%mm7 \n\t" \ - "paddq %%mm2, %%mm1 \n\t" \ - "movd 4(%%edi), %%mm3 \n\t" \ - "paddq %%mm4, %%mm3 \n\t" \ - "movd 8(%%edi), %%mm5 \n\t" \ - "paddq %%mm6, %%mm5 \n\t" \ - "movd 12(%%edi), %%mm4 \n\t" \ - "paddq %%mm4, %%mm7 \n\t" \ - "movd %%mm1, (%%edi) \n\t" \ - "movd 16(%%esi), %%mm2 \n\t" \ - "pmuludq %%mm0, %%mm2 \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "movd 20(%%esi), %%mm4 \n\t" \ - "pmuludq %%mm0, %%mm4 \n\t" \ - "paddq %%mm3, %%mm1 \n\t" \ - "movd 24(%%esi), %%mm6 \n\t" \ - "pmuludq %%mm0, %%mm6 \n\t" \ - "movd %%mm1, 4(%%edi) \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "movd 28(%%esi), %%mm3 \n\t" \ - "pmuludq %%mm0, %%mm3 \n\t" \ - "paddq %%mm5, %%mm1 \n\t" \ - "movd 16(%%edi), %%mm5 \n\t" \ - "paddq %%mm5, %%mm2 \n\t" \ - "movd %%mm1, 8(%%edi) \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "paddq %%mm7, %%mm1 \n\t" \ - "movd 20(%%edi), %%mm5 \n\t" \ - "paddq %%mm5, %%mm4 \n\t" \ - "movd %%mm1, 12(%%edi) \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "paddq %%mm2, %%mm1 \n\t" \ - "movd 24(%%edi), %%mm5 \n\t" \ - "paddq %%mm5, %%mm6 \n\t" \ - "movd %%mm1, 16(%%edi) \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "paddq %%mm4, %%mm1 \n\t" \ - "movd 28(%%edi), %%mm5 \n\t" \ - "paddq %%mm5, %%mm3 \n\t" \ - "movd %%mm1, 20(%%edi) \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "paddq %%mm6, %%mm1 \n\t" \ - "movd %%mm1, 24(%%edi) \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "paddq %%mm3, %%mm1 \n\t" \ - "movd %%mm1, 28(%%edi) \n\t" \ - "addl $32, %%edi \n\t" \ - "addl $32, %%esi \n\t" \ - "psrlq $32, %%mm1 \n\t" \ - "movd %%mm1, %%ecx \n\t" - -#define MULADDC_STOP \ - "emms \n\t" \ - "movl %4, %%ebx \n\t" \ - "movl %%ecx, %1 \n\t" \ - "movl %%edi, %2 \n\t" \ - "movl %%esi, %3 \n\t" \ - : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \ - : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \ - : "eax", "ebx", "ecx", "edx", "esi", "edi" \ - ); - -#else - -#define MULADDC_STOP \ - "movl %4, %%ebx \n\t" \ - "movl %%ecx, %1 \n\t" \ - "movl %%edi, %2 \n\t" \ - "movl %%esi, %3 \n\t" \ - : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \ - : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \ - : "eax", "ebx", "ecx", "edx", "esi", "edi" \ - ); -#endif /* SSE2 */ -#endif /* i386 */ - -#if defined(__amd64__) || defined(__x86_64__) - -#define MULADDC_INIT \ - asm( \ - "xorq %%r8, %%r8\n" - -#define MULADDC_CORE \ - "movq (%%rsi), %%rax\n" \ - "mulq %%rbx\n" \ - "addq $8, %%rsi\n" \ - "addq %%rcx, %%rax\n" \ - "movq %%r8, %%rcx\n" \ - "adcq $0, %%rdx\n" \ - "nop \n" \ - "addq %%rax, (%%rdi)\n" \ - "adcq %%rdx, %%rcx\n" \ - "addq $8, %%rdi\n" - -#define MULADDC_STOP \ - : "+c" (c), "+D" (d), "+S" (s) \ - : "b" (b) \ - : "rax", "rdx", "r8" \ - ); - -#endif /* AMD64 */ - -#if defined(__aarch64__) - -#define MULADDC_INIT \ - asm( - -#define MULADDC_CORE \ - "ldr x4, [%2], #8 \n\t" \ - "ldr x5, [%1] \n\t" \ - "mul x6, x4, %3 \n\t" \ - "umulh x7, x4, %3 \n\t" \ - "adds x5, x5, x6 \n\t" \ - "adc x7, x7, xzr \n\t" \ - "adds x5, x5, %0 \n\t" \ - "adc %0, x7, xzr \n\t" \ - "str x5, [%1], #8 \n\t" - -#define MULADDC_STOP \ - : "+r" (c), "+r" (d), "+r" (s) \ - : "r" (b) \ - : "x4", "x5", "x6", "x7", "cc" \ - ); - -#endif /* Aarch64 */ - -#if defined(__mc68020__) || defined(__mcpu32__) - -#define MULADDC_INIT \ - asm( \ - "movl %3, %%a2 \n\t" \ - "movl %4, %%a3 \n\t" \ - "movl %5, %%d3 \n\t" \ - "movl %6, %%d2 \n\t" \ - "moveq #0, %%d0 \n\t" - -#define MULADDC_CORE \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d4:%%d1 \n\t" \ - "addl %%d3, %%d1 \n\t" \ - "addxl %%d0, %%d4 \n\t" \ - "moveq #0, %%d3 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "addxl %%d4, %%d3 \n\t" - -#define MULADDC_STOP \ - "movl %%d3, %0 \n\t" \ - "movl %%a3, %1 \n\t" \ - "movl %%a2, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "d0", "d1", "d2", "d3", "d4", "a2", "a3" \ - ); - -#define MULADDC_HUIT \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d4:%%d1 \n\t" \ - "addxl %%d3, %%d1 \n\t" \ - "addxl %%d0, %%d4 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d3:%%d1 \n\t" \ - "addxl %%d4, %%d1 \n\t" \ - "addxl %%d0, %%d3 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d4:%%d1 \n\t" \ - "addxl %%d3, %%d1 \n\t" \ - "addxl %%d0, %%d4 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d3:%%d1 \n\t" \ - "addxl %%d4, %%d1 \n\t" \ - "addxl %%d0, %%d3 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d4:%%d1 \n\t" \ - "addxl %%d3, %%d1 \n\t" \ - "addxl %%d0, %%d4 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d3:%%d1 \n\t" \ - "addxl %%d4, %%d1 \n\t" \ - "addxl %%d0, %%d3 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d4:%%d1 \n\t" \ - "addxl %%d3, %%d1 \n\t" \ - "addxl %%d0, %%d4 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "movel %%a2@+, %%d1 \n\t" \ - "mulul %%d2, %%d3:%%d1 \n\t" \ - "addxl %%d4, %%d1 \n\t" \ - "addxl %%d0, %%d3 \n\t" \ - "addl %%d1, %%a3@+ \n\t" \ - "addxl %%d0, %%d3 \n\t" - -#endif /* MC68000 */ - -#if defined(__powerpc64__) || defined(__ppc64__) - -#if defined(__MACH__) && defined(__APPLE__) - -#define MULADDC_INIT \ - asm( \ - "ld r3, %3 \n\t" \ - "ld r4, %4 \n\t" \ - "ld r5, %5 \n\t" \ - "ld r6, %6 \n\t" \ - "addi r3, r3, -8 \n\t" \ - "addi r4, r4, -8 \n\t" \ - "addic r5, r5, 0 \n\t" - -#define MULADDC_CORE \ - "ldu r7, 8(r3) \n\t" \ - "mulld r8, r7, r6 \n\t" \ - "mulhdu r9, r7, r6 \n\t" \ - "adde r8, r8, r5 \n\t" \ - "ld r7, 8(r4) \n\t" \ - "addze r5, r9 \n\t" \ - "addc r8, r8, r7 \n\t" \ - "stdu r8, 8(r4) \n\t" - -#define MULADDC_STOP \ - "addze r5, r5 \n\t" \ - "addi r4, r4, 8 \n\t" \ - "addi r3, r3, 8 \n\t" \ - "std r5, %0 \n\t" \ - "std r4, %1 \n\t" \ - "std r3, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \ - ); - -#else /* __MACH__ && __APPLE__ */ - -#define MULADDC_INIT \ - asm( \ - "ld %%r3, %3 \n\t" \ - "ld %%r4, %4 \n\t" \ - "ld %%r5, %5 \n\t" \ - "ld %%r6, %6 \n\t" \ - "addi %%r3, %%r3, -8 \n\t" \ - "addi %%r4, %%r4, -8 \n\t" \ - "addic %%r5, %%r5, 0 \n\t" - -#define MULADDC_CORE \ - "ldu %%r7, 8(%%r3) \n\t" \ - "mulld %%r8, %%r7, %%r6 \n\t" \ - "mulhdu %%r9, %%r7, %%r6 \n\t" \ - "adde %%r8, %%r8, %%r5 \n\t" \ - "ld %%r7, 8(%%r4) \n\t" \ - "addze %%r5, %%r9 \n\t" \ - "addc %%r8, %%r8, %%r7 \n\t" \ - "stdu %%r8, 8(%%r4) \n\t" - -#define MULADDC_STOP \ - "addze %%r5, %%r5 \n\t" \ - "addi %%r4, %%r4, 8 \n\t" \ - "addi %%r3, %%r3, 8 \n\t" \ - "std %%r5, %0 \n\t" \ - "std %%r4, %1 \n\t" \ - "std %%r3, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \ - ); - -#endif /* __MACH__ && __APPLE__ */ - -#elif defined(__powerpc__) || defined(__ppc__) /* end PPC64/begin PPC32 */ - -#if defined(__MACH__) && defined(__APPLE__) - -#define MULADDC_INIT \ - asm( \ - "lwz r3, %3 \n\t" \ - "lwz r4, %4 \n\t" \ - "lwz r5, %5 \n\t" \ - "lwz r6, %6 \n\t" \ - "addi r3, r3, -4 \n\t" \ - "addi r4, r4, -4 \n\t" \ - "addic r5, r5, 0 \n\t" - -#define MULADDC_CORE \ - "lwzu r7, 4(r3) \n\t" \ - "mullw r8, r7, r6 \n\t" \ - "mulhwu r9, r7, r6 \n\t" \ - "adde r8, r8, r5 \n\t" \ - "lwz r7, 4(r4) \n\t" \ - "addze r5, r9 \n\t" \ - "addc r8, r8, r7 \n\t" \ - "stwu r8, 4(r4) \n\t" - -#define MULADDC_STOP \ - "addze r5, r5 \n\t" \ - "addi r4, r4, 4 \n\t" \ - "addi r3, r3, 4 \n\t" \ - "stw r5, %0 \n\t" \ - "stw r4, %1 \n\t" \ - "stw r3, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \ - ); - -#else /* __MACH__ && __APPLE__ */ - -#define MULADDC_INIT \ - asm( \ - "lwz %%r3, %3 \n\t" \ - "lwz %%r4, %4 \n\t" \ - "lwz %%r5, %5 \n\t" \ - "lwz %%r6, %6 \n\t" \ - "addi %%r3, %%r3, -4 \n\t" \ - "addi %%r4, %%r4, -4 \n\t" \ - "addic %%r5, %%r5, 0 \n\t" - -#define MULADDC_CORE \ - "lwzu %%r7, 4(%%r3) \n\t" \ - "mullw %%r8, %%r7, %%r6 \n\t" \ - "mulhwu %%r9, %%r7, %%r6 \n\t" \ - "adde %%r8, %%r8, %%r5 \n\t" \ - "lwz %%r7, 4(%%r4) \n\t" \ - "addze %%r5, %%r9 \n\t" \ - "addc %%r8, %%r8, %%r7 \n\t" \ - "stwu %%r8, 4(%%r4) \n\t" - -#define MULADDC_STOP \ - "addze %%r5, %%r5 \n\t" \ - "addi %%r4, %%r4, 4 \n\t" \ - "addi %%r3, %%r3, 4 \n\t" \ - "stw %%r5, %0 \n\t" \ - "stw %%r4, %1 \n\t" \ - "stw %%r3, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "r3", "r4", "r5", "r6", "r7", "r8", "r9" \ - ); - -#endif /* __MACH__ && __APPLE__ */ - -#endif /* PPC32 */ - -/* - * The Sparc(64) assembly is reported to be broken. - * Disable it for now, until we're able to fix it. - */ -#if 0 && defined(__sparc__) -#if defined(__sparc64__) - -#define MULADDC_INIT \ - asm( \ - "ldx %3, %%o0 \n\t" \ - "ldx %4, %%o1 \n\t" \ - "ld %5, %%o2 \n\t" \ - "ld %6, %%o3 \n\t" - -#define MULADDC_CORE \ - "ld [%%o0], %%o4 \n\t" \ - "inc 4, %%o0 \n\t" \ - "ld [%%o1], %%o5 \n\t" \ - "umul %%o3, %%o4, %%o4 \n\t" \ - "addcc %%o4, %%o2, %%o4 \n\t" \ - "rd %%y, %%g1 \n\t" \ - "addx %%g1, 0, %%g1 \n\t" \ - "addcc %%o4, %%o5, %%o4 \n\t" \ - "st %%o4, [%%o1] \n\t" \ - "addx %%g1, 0, %%o2 \n\t" \ - "inc 4, %%o1 \n\t" - -#define MULADDC_STOP \ - "st %%o2, %0 \n\t" \ - "stx %%o1, %1 \n\t" \ - "stx %%o0, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "g1", "o0", "o1", "o2", "o3", "o4", \ - "o5" \ - ); - -#else /* __sparc64__ */ - -#define MULADDC_INIT \ - asm( \ - "ld %3, %%o0 \n\t" \ - "ld %4, %%o1 \n\t" \ - "ld %5, %%o2 \n\t" \ - "ld %6, %%o3 \n\t" - -#define MULADDC_CORE \ - "ld [%%o0], %%o4 \n\t" \ - "inc 4, %%o0 \n\t" \ - "ld [%%o1], %%o5 \n\t" \ - "umul %%o3, %%o4, %%o4 \n\t" \ - "addcc %%o4, %%o2, %%o4 \n\t" \ - "rd %%y, %%g1 \n\t" \ - "addx %%g1, 0, %%g1 \n\t" \ - "addcc %%o4, %%o5, %%o4 \n\t" \ - "st %%o4, [%%o1] \n\t" \ - "addx %%g1, 0, %%o2 \n\t" \ - "inc 4, %%o1 \n\t" - -#define MULADDC_STOP \ - "st %%o2, %0 \n\t" \ - "st %%o1, %1 \n\t" \ - "st %%o0, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "g1", "o0", "o1", "o2", "o3", "o4", \ - "o5" \ - ); - -#endif /* __sparc64__ */ -#endif /* __sparc__ */ - -#if defined(__microblaze__) || defined(microblaze) - -#define MULADDC_INIT \ - asm( \ - "lwi r3, %3 \n\t" \ - "lwi r4, %4 \n\t" \ - "lwi r5, %5 \n\t" \ - "lwi r6, %6 \n\t" \ - "andi r7, r6, 0xffff \n\t" \ - "bsrli r6, r6, 16 \n\t" - -#define MULADDC_CORE \ - "lhui r8, r3, 0 \n\t" \ - "addi r3, r3, 2 \n\t" \ - "lhui r9, r3, 0 \n\t" \ - "addi r3, r3, 2 \n\t" \ - "mul r10, r9, r6 \n\t" \ - "mul r11, r8, r7 \n\t" \ - "mul r12, r9, r7 \n\t" \ - "mul r13, r8, r6 \n\t" \ - "bsrli r8, r10, 16 \n\t" \ - "bsrli r9, r11, 16 \n\t" \ - "add r13, r13, r8 \n\t" \ - "add r13, r13, r9 \n\t" \ - "bslli r10, r10, 16 \n\t" \ - "bslli r11, r11, 16 \n\t" \ - "add r12, r12, r10 \n\t" \ - "addc r13, r13, r0 \n\t" \ - "add r12, r12, r11 \n\t" \ - "addc r13, r13, r0 \n\t" \ - "lwi r10, r4, 0 \n\t" \ - "add r12, r12, r10 \n\t" \ - "addc r13, r13, r0 \n\t" \ - "add r12, r12, r5 \n\t" \ - "addc r5, r13, r0 \n\t" \ - "swi r12, r4, 0 \n\t" \ - "addi r4, r4, 4 \n\t" - -#define MULADDC_STOP \ - "swi r5, %0 \n\t" \ - "swi r4, %1 \n\t" \ - "swi r3, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "r3", "r4", "r5", "r6", "r7", "r8", \ - "r9", "r10", "r11", "r12", "r13" \ - ); - -#endif /* MicroBlaze */ - -#if defined(__tricore__) - -#define MULADDC_INIT \ - asm( \ - "ld.a %%a2, %3 \n\t" \ - "ld.a %%a3, %4 \n\t" \ - "ld.w %%d4, %5 \n\t" \ - "ld.w %%d1, %6 \n\t" \ - "xor %%d5, %%d5 \n\t" - -#define MULADDC_CORE \ - "ld.w %%d0, [%%a2+] \n\t" \ - "madd.u %%e2, %%e4, %%d0, %%d1 \n\t" \ - "ld.w %%d0, [%%a3] \n\t" \ - "addx %%d2, %%d2, %%d0 \n\t" \ - "addc %%d3, %%d3, 0 \n\t" \ - "mov %%d4, %%d3 \n\t" \ - "st.w [%%a3+], %%d2 \n\t" - -#define MULADDC_STOP \ - "st.w %0, %%d4 \n\t" \ - "st.a %1, %%a3 \n\t" \ - "st.a %2, %%a2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "d0", "d1", "e2", "d4", "a2", "a3" \ - ); - -#endif /* TriCore */ - -/* - * Note, gcc -O0 by default uses r7 for the frame pointer, so it complains about - * our use of r7 below, unless -fomit-frame-pointer is passed. - * - * On the other hand, -fomit-frame-pointer is implied by any -Ox options with - * x !=0, which we can detect using __OPTIMIZE__ (which is also defined by - * clang and armcc5 under the same conditions). - * - * So, only use the optimized assembly below for optimized build, which avoids - * the build error and is pretty reasonable anyway. - */ -#if defined(__GNUC__) && !defined(__OPTIMIZE__) -#define MULADDC_CANNOT_USE_R7 -#endif - -#if defined(__arm__) && !defined(MULADDC_CANNOT_USE_R7) - -#if defined(__thumb__) && !defined(__thumb2__) - -#define MULADDC_INIT \ - asm( \ - "ldr r0, %3 \n\t" \ - "ldr r1, %4 \n\t" \ - "ldr r2, %5 \n\t" \ - "ldr r3, %6 \n\t" \ - "lsr r7, r3, #16 \n\t" \ - "mov r9, r7 \n\t" \ - "lsl r7, r3, #16 \n\t" \ - "lsr r7, r7, #16 \n\t" \ - "mov r8, r7 \n\t" - -#define MULADDC_CORE \ - "ldmia r0!, {r6} \n\t" \ - "lsr r7, r6, #16 \n\t" \ - "lsl r6, r6, #16 \n\t" \ - "lsr r6, r6, #16 \n\t" \ - "mov r4, r8 \n\t" \ - "mul r4, r6 \n\t" \ - "mov r3, r9 \n\t" \ - "mul r6, r3 \n\t" \ - "mov r5, r9 \n\t" \ - "mul r5, r7 \n\t" \ - "mov r3, r8 \n\t" \ - "mul r7, r3 \n\t" \ - "lsr r3, r6, #16 \n\t" \ - "add r5, r5, r3 \n\t" \ - "lsr r3, r7, #16 \n\t" \ - "add r5, r5, r3 \n\t" \ - "add r4, r4, r2 \n\t" \ - "mov r2, #0 \n\t" \ - "adc r5, r2 \n\t" \ - "lsl r3, r6, #16 \n\t" \ - "add r4, r4, r3 \n\t" \ - "adc r5, r2 \n\t" \ - "lsl r3, r7, #16 \n\t" \ - "add r4, r4, r3 \n\t" \ - "adc r5, r2 \n\t" \ - "ldr r3, [r1] \n\t" \ - "add r4, r4, r3 \n\t" \ - "adc r2, r5 \n\t" \ - "stmia r1!, {r4} \n\t" - -#define MULADDC_STOP \ - "str r2, %0 \n\t" \ - "str r1, %1 \n\t" \ - "str r0, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "r0", "r1", "r2", "r3", "r4", "r5", \ - "r6", "r7", "r8", "r9", "cc" \ - ); - -#elif (__ARM_ARCH >= 6) && defined(__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1) - -#define MULADDC_INIT \ - asm( - -#define MULADDC_CORE \ - "ldr r0, [%0], #4 \n\t" \ - "ldr r1, [%1] \n\t" \ - "umaal r1, %2, %3, r0 \n\t" \ - "str r1, [%1], #4 \n\t" - -#define MULADDC_STOP \ - : "=r" (s), "=r" (d), "=r" (c) \ - : "r" (b), "0" (s), "1" (d), "2" (c) \ - : "r0", "r1", "memory" \ - ); - -#else - -#define MULADDC_INIT \ - asm( \ - "ldr r0, %3 \n\t" \ - "ldr r1, %4 \n\t" \ - "ldr r2, %5 \n\t" \ - "ldr r3, %6 \n\t" - -#define MULADDC_CORE \ - "ldr r4, [r0], #4 \n\t" \ - "mov r5, #0 \n\t" \ - "ldr r6, [r1] \n\t" \ - "umlal r2, r5, r3, r4 \n\t" \ - "adds r7, r6, r2 \n\t" \ - "adc r2, r5, #0 \n\t" \ - "str r7, [r1], #4 \n\t" - -#define MULADDC_STOP \ - "str r2, %0 \n\t" \ - "str r1, %1 \n\t" \ - "str r0, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "r0", "r1", "r2", "r3", "r4", "r5", \ - "r6", "r7", "cc" \ - ); - -#endif /* Thumb */ - -#endif /* ARMv3 */ - -#if defined(__alpha__) - -#define MULADDC_INIT \ - asm( \ - "ldq $1, %3 \n\t" \ - "ldq $2, %4 \n\t" \ - "ldq $3, %5 \n\t" \ - "ldq $4, %6 \n\t" - -#define MULADDC_CORE \ - "ldq $6, 0($1) \n\t" \ - "addq $1, 8, $1 \n\t" \ - "mulq $6, $4, $7 \n\t" \ - "umulh $6, $4, $6 \n\t" \ - "addq $7, $3, $7 \n\t" \ - "cmpult $7, $3, $3 \n\t" \ - "ldq $5, 0($2) \n\t" \ - "addq $7, $5, $7 \n\t" \ - "cmpult $7, $5, $5 \n\t" \ - "stq $7, 0($2) \n\t" \ - "addq $2, 8, $2 \n\t" \ - "addq $6, $3, $3 \n\t" \ - "addq $5, $3, $3 \n\t" - -#define MULADDC_STOP \ - "stq $3, %0 \n\t" \ - "stq $2, %1 \n\t" \ - "stq $1, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "$1", "$2", "$3", "$4", "$5", "$6", "$7" \ - ); -#endif /* Alpha */ - -#if defined(__mips__) && !defined(__mips64) - -#define MULADDC_INIT \ - asm( \ - "lw $10, %3 \n\t" \ - "lw $11, %4 \n\t" \ - "lw $12, %5 \n\t" \ - "lw $13, %6 \n\t" - -#define MULADDC_CORE \ - "lw $14, 0($10) \n\t" \ - "multu $13, $14 \n\t" \ - "addi $10, $10, 4 \n\t" \ - "mflo $14 \n\t" \ - "mfhi $9 \n\t" \ - "addu $14, $12, $14 \n\t" \ - "lw $15, 0($11) \n\t" \ - "sltu $12, $14, $12 \n\t" \ - "addu $15, $14, $15 \n\t" \ - "sltu $14, $15, $14 \n\t" \ - "addu $12, $12, $9 \n\t" \ - "sw $15, 0($11) \n\t" \ - "addu $12, $12, $14 \n\t" \ - "addi $11, $11, 4 \n\t" - -#define MULADDC_STOP \ - "sw $12, %0 \n\t" \ - "sw $11, %1 \n\t" \ - "sw $10, %2 \n\t" \ - : "=m" (c), "=m" (d), "=m" (s) \ - : "m" (s), "m" (d), "m" (c), "m" (b) \ - : "$9", "$10", "$11", "$12", "$13", "$14", "$15", "lo", "hi" \ - ); - -#endif /* MIPS */ -#endif /* GNUC */ - -#if (defined(_MSC_VER) && defined(_M_IX86)) || defined(__WATCOMC__) - -#define MULADDC_INIT __asm mov esi, s __asm mov edi, d __asm mov ecx, c __asm mov ebx, b - -#define MULADDC_CORE \ - __asm lodsd __asm mul ebx __asm add eax, ecx __asm adc edx, 0 __asm add eax, [ edi ] __asm adc edx, 0 __asm mov ecx, \ - edx __asm stosd - -#if defined(MBEDTLS_HAVE_SSE2) - -#define EMIT __asm _emit - -#define MULADDC_HUIT \ - EMIT 0x0F EMIT 0x6E EMIT 0xC9 EMIT 0x0F EMIT 0x6E EMIT 0xC3 EMIT 0x0F EMIT 0x6E EMIT 0x1F EMIT 0x0F EMIT 0xD4 EMIT 0xCB EMIT 0x0F EMIT 0x6E EMIT 0x16 EMIT 0x0F EMIT 0xF4 EMIT 0xD0 EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x04 EMIT 0x0F EMIT 0xF4 EMIT 0xE0 EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x08 EMIT 0x0F EMIT 0xF4 EMIT 0xF0 EMIT 0x0F EMIT 0x6E EMIT 0x7E EMIT 0x0C EMIT 0x0F EMIT 0xF4 EMIT 0xF8 EMIT 0x0F EMIT 0xD4 EMIT 0xCA EMIT 0x0F EMIT 0x6E EMIT 0x5F EMIT 0x04 EMIT 0x0F EMIT 0xD4 EMIT 0xDC EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x08 EMIT 0x0F EMIT 0xD4 EMIT 0xEE EMIT 0x0F EMIT 0x6E EMIT 0x67 EMIT 0x0C EMIT 0x0F EMIT 0xD4 EMIT 0xFC EMIT 0x0F EMIT 0x7E EMIT 0x0F EMIT 0x0F EMIT 0x6E EMIT 0x56 EMIT 0x10 EMIT 0x0F EMIT 0xF4 EMIT 0xD0 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0x6E EMIT 0x66 EMIT 0x14 EMIT 0x0F EMIT 0xF4 EMIT 0xE0 EMIT 0x0F EMIT 0xD4 EMIT 0xCB EMIT 0x0F EMIT 0x6E EMIT 0x76 EMIT 0x18 EMIT 0x0F EMIT 0xF4 EMIT 0xF0 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x04 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0x6E EMIT 0x5E EMIT 0x1C EMIT 0x0F EMIT 0xF4 EMIT 0xD8 EMIT 0x0F EMIT 0xD4 EMIT 0xCD EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x10 EMIT 0x0F EMIT 0xD4 EMIT 0xD5 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x08 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0xD4 EMIT 0xCF EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x14 EMIT 0x0F EMIT 0xD4 EMIT 0xE5 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x0C EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0xD4 EMIT 0xCA EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x18 EMIT 0x0F EMIT 0xD4 EMIT 0xF5 EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x10 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0xD4 EMIT 0xCC EMIT 0x0F EMIT 0x6E EMIT 0x6F EMIT 0x1C EMIT 0x0F EMIT 0xD4 EMIT 0xDD EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x14 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0xD4 EMIT 0xCE EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x18 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0xD4 EMIT 0xCB EMIT 0x0F EMIT 0x7E EMIT 0x4F EMIT 0x1C EMIT 0x83 EMIT 0xC7 EMIT 0x20 EMIT 0x83 EMIT 0xC6 EMIT 0x20 EMIT 0x0F EMIT 0x73 EMIT 0xD1 EMIT 0x20 EMIT 0x0F EMIT 0x7E EMIT 0xC9 - -#define MULADDC_STOP EMIT 0x0F EMIT 0x77 __asm mov c, ecx __asm mov d, edi __asm mov s, esi - -#else - -#define MULADDC_STOP __asm mov c, ecx __asm mov d, edi __asm mov s, esi - -#endif /* SSE2 */ -#endif /* MSVC */ - -#endif /* MBEDTLS_HAVE_ASM */ - -#if !defined(MULADDC_CORE) -#if defined(MBEDTLS_HAVE_UDBL) - -#define MULADDC_INIT \ - { \ - mbedtls_t_udbl r; \ - mbedtls_mpi_uint r0, r1; - -#define MULADDC_CORE \ - r = *(s++) * (mbedtls_t_udbl) b; \ - r0 = (mbedtls_mpi_uint) r; \ - r1 = (mbedtls_mpi_uint)(r >> biL); \ - r0 += c; \ - r1 += (r0 < c); \ - r0 += *d; \ - r1 += (r0 < *d); \ - c = r1; \ - *(d++) = r0; - -#define MULADDC_STOP } - -#else -#define MULADDC_INIT \ - { \ - mbedtls_mpi_uint s0, s1, b0, b1; \ - mbedtls_mpi_uint r0, r1, rx, ry; \ - b0 = (b << biH) >> biH; \ - b1 = (b >> biH); - -#define MULADDC_CORE \ - s0 = (*s << biH) >> biH; \ - s1 = (*s >> biH); \ - s++; \ - rx = s0 * b1; \ - r0 = s0 * b0; \ - ry = s1 * b0; \ - r1 = s1 * b1; \ - r1 += (rx >> biH); \ - r1 += (ry >> biH); \ - rx <<= biH; \ - ry <<= biH; \ - r0 += rx; \ - r1 += (r0 < rx); \ - r0 += ry; \ - r1 += (r0 < ry); \ - r0 += c; \ - r1 += (r0 < c); \ - r0 += *d; \ - r1 += (r0 < *d); \ - c = r1; \ - *(d++) = r0; - -#define MULADDC_STOP } - -#endif /* C (generic) */ -#endif /* C (longlong) */ - -#endif /* bn_mul.h */ diff --git a/src/platform/telink/crypto/internal/compatibility/ecp_internal_alt.h b/src/platform/telink/crypto/internal/compatibility/ecp_internal_alt.h deleted file mode 100644 index b9afb4a..0000000 --- a/src/platform/telink/crypto/internal/compatibility/ecp_internal_alt.h +++ /dev/null @@ -1,284 +0,0 @@ -/** - * \file ecp_internal_alt.h - * - * \brief Function declarations for alternative implementation of elliptic curve - * point arithmetic. - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/* - * References: - * - * [1] BERNSTEIN, Daniel J. Curve25519: new Diffie-Hellman speed records. - * - * - * [2] CORON, Jean-S'ebastien. Resistance against differential power analysis - * for elliptic curve cryptosystems. In : Cryptographic Hardware and - * Embedded Systems. Springer Berlin Heidelberg, 1999. p. 292-302. - * - * - * [3] HEDABOU, Mustapha, PINEL, Pierre, et B'EN'ETEAU, Lucien. A comb method to - * render ECC resistant against Side Channel Attacks. IACR Cryptology - * ePrint Archive, 2004, vol. 2004, p. 342. - * - * - * [4] Certicom Research. SEC 2: Recommended Elliptic Curve Domain Parameters. - * - * - * [5] HANKERSON, Darrel, MENEZES, Alfred J., VANSTONE, Scott. Guide to Elliptic - * Curve Cryptography. - * - * [6] Digital Signature Standard (DSS), FIPS 186-4. - * - * - * [7] Elliptic Curve Cryptography (ECC) Cipher Suites for Transport Layer - * Security (TLS), RFC 4492. - * - * - * [8] - * - * [9] COHEN, Henri. A Course in Computational Algebraic Number Theory. - * Springer Science & Business Media, 1 Aug 2000 - */ - -#ifndef MBEDTLS_ECP_INTERNAL_H -#define MBEDTLS_ECP_INTERNAL_H - -#include "mbedtls/build_info.h" - -#if defined(MBEDTLS_ECP_INTERNAL_ALT) - -/** - * \brief Indicate if the Elliptic Curve Point module extension can - * handle the group. - * - * \param grp The pointer to the elliptic curve group that will be the - * basis of the cryptographic computations. - * - * \return Non-zero if successful. - */ -unsigned char mbedtls_internal_ecp_grp_capable(const mbedtls_ecp_group * grp); - -/** - * \brief Initialise the Elliptic Curve Point module extension. - * - * If mbedtls_internal_ecp_grp_capable returns true for a - * group, this function has to be able to initialise the - * module for it. - * - * This module can be a driver to a crypto hardware - * accelerator, for which this could be an initialise function. - * - * \param grp The pointer to the group the module needs to be - * initialised for. - * - * \return 0 if successful. - */ -int mbedtls_internal_ecp_init(const mbedtls_ecp_group * grp); - -/** - * \brief Frees and deallocates the Elliptic Curve Point module - * extension. - * - * \param grp The pointer to the group the module was initialised for. - */ -void mbedtls_internal_ecp_free(const mbedtls_ecp_group * grp); - -#if defined(MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED) - -#if defined(MBEDTLS_ECP_RANDOMIZE_JAC_ALT) -/** - * \brief Randomize jacobian coordinates: - * (X, Y, Z) -> (l^2 X, l^3 Y, l Z) for random l. - * - * \param grp Pointer to the group representing the curve. - * - * \param pt The point on the curve to be randomised, given with Jacobian - * coordinates. - * - * \param f_rng A function pointer to the random number generator. - * - * \param p_rng A pointer to the random number generator state. - * - * \return 0 if successful. - */ -int mbedtls_internal_ecp_randomize_jac(const mbedtls_ecp_group * grp, mbedtls_ecp_point * pt, - int (*f_rng)(void *, unsigned char *, size_t), void * p_rng); -#endif - -#if defined(MBEDTLS_ECP_ADD_MIXED_ALT) -/** - * \brief Addition: R = P + Q, mixed affine-Jacobian coordinates. - * - * The coordinates of Q must be normalized (= affine), - * but those of P don't need to. R is not normalized. - * - * This function is used only as a subrutine of - * ecp_mul_comb(). - * - * Special cases: (1) P or Q is zero, (2) R is zero, - * (3) P == Q. - * None of these cases can happen as intermediate step in - * ecp_mul_comb(): - * - at each step, P, Q and R are multiples of the base - * point, the factor being less than its order, so none of - * them is zero; - * - Q is an odd multiple of the base point, P an even - * multiple, due to the choice of precomputed points in the - * modified comb method. - * So branches for these cases do not leak secret information. - * - * We accept Q->Z being unset (saving memory in tables) as - * meaning 1. - * - * Cost in field operations if done by [5] 3.22: - * 1A := 8M + 3S - * - * \param grp Pointer to the group representing the curve. - * - * \param R Pointer to a point structure to hold the result. - * - * \param P Pointer to the first summand, given with Jacobian - * coordinates - * - * \param Q Pointer to the second summand, given with affine - * coordinates. - * - * \return 0 if successful. - */ -int mbedtls_internal_ecp_add_mixed(const mbedtls_ecp_group * grp, mbedtls_ecp_point * R, const mbedtls_ecp_point * P, - const mbedtls_ecp_point * Q); -#endif - -/** - * \brief Point doubling R = 2 P, Jacobian coordinates. - * - * Cost: 1D := 3M + 4S (A == 0) - * 4M + 4S (A == -3) - * 3M + 6S + 1a otherwise - * when the implementation is based on the "dbl-1998-cmo-2" - * doubling formulas in [8] and standard optimizations are - * applied when curve parameter A is one of { 0, -3 }. - * - * \param grp Pointer to the group representing the curve. - * - * \param R Pointer to a point structure to hold the result. - * - * \param P Pointer to the point that has to be doubled, given with - * Jacobian coordinates. - * - * \return 0 if successful. - */ -#if defined(MBEDTLS_ECP_DOUBLE_JAC_ALT) -int mbedtls_internal_ecp_double_jac(const mbedtls_ecp_group * grp, mbedtls_ecp_point * R, const mbedtls_ecp_point * P); -#endif - -/** - * \brief Normalize jacobian coordinates of an array of (pointers to) - * points. - * - * Using Montgomery's trick to perform only one inversion mod P - * the cost is: - * 1N(t) := 1I + (6t - 3)M + 1S - * (See for example Algorithm 10.3.4. in [9]) - * - * This function is used only as a subrutine of - * ecp_mul_comb(). - * - * Warning: fails (returning an error) if one of the points is - * zero! - * This should never happen, see choice of w in ecp_mul_comb(). - * - * \param grp Pointer to the group representing the curve. - * - * \param T Array of pointers to the points to normalise. - * - * \param t_len Number of elements in the array. - * - * \return 0 if successful, - * an error if one of the points is zero. - */ -#if defined(MBEDTLS_ECP_NORMALIZE_JAC_MANY_ALT) -int mbedtls_internal_ecp_normalize_jac_many(const mbedtls_ecp_group * grp, mbedtls_ecp_point * T[], size_t t_len); -#endif - -/** - * \brief Normalize jacobian coordinates so that Z == 0 || Z == 1. - * - * Cost in field operations if done by [5] 3.2.1: - * 1N := 1I + 3M + 1S - * - * \param grp Pointer to the group representing the curve. - * - * \param pt pointer to the point to be normalised. This is an - * input/output parameter. - * - * \return 0 if successful. - */ -#if defined(MBEDTLS_ECP_NORMALIZE_JAC_ALT) -int mbedtls_internal_ecp_normalize_jac(const mbedtls_ecp_group * grp, mbedtls_ecp_point * pt); -#endif - -#endif /* MBEDTLS_ECP_SHORT_WEIERSTRASS_ENABLED */ - -#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) - -#if defined(MBEDTLS_ECP_DOUBLE_ADD_MXZ_ALT) -int mbedtls_internal_ecp_double_add_mxz(const mbedtls_ecp_group * grp, mbedtls_ecp_point * R, mbedtls_ecp_point * S, - const mbedtls_ecp_point * P, const mbedtls_ecp_point * Q, const mbedtls_mpi * d); -#endif - -/** - * \brief Randomize projective x/z coordinates: - * (X, Z) -> (l X, l Z) for random l - * - * \param grp pointer to the group representing the curve - * - * \param P the point on the curve to be randomised given with - * projective coordinates. This is an input/output parameter. - * - * \param f_rng a function pointer to the random number generator - * - * \param p_rng a pointer to the random number generator state - * - * \return 0 if successful - */ -#if defined(MBEDTLS_ECP_RANDOMIZE_MXZ_ALT) -int mbedtls_internal_ecp_randomize_mxz(const mbedtls_ecp_group * grp, mbedtls_ecp_point * P, - int (*f_rng)(void *, unsigned char *, size_t), void * p_rng); -#endif - -/** - * \brief Normalize Montgomery x/z coordinates: X = X/Z, Z = 1. - * - * \param grp pointer to the group representing the curve - * - * \param P pointer to the point to be normalised. This is an - * input/output parameter. - * - * \return 0 if successful - */ -#if defined(MBEDTLS_ECP_NORMALIZE_MXZ_ALT) -int mbedtls_internal_ecp_normalize_mxz(const mbedtls_ecp_group * grp, mbedtls_ecp_point * P); -#endif - -#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ - -#endif /* MBEDTLS_ECP_INTERNAL_ALT */ - -#endif /* ecp_internal_alt.h */ diff --git a/src/platform/telink/crypto/internal/compatibility/ecp_invasive.h b/src/platform/telink/crypto/internal/compatibility/ecp_invasive.h deleted file mode 100644 index ce1ff6f..0000000 --- a/src/platform/telink/crypto/internal/compatibility/ecp_invasive.h +++ /dev/null @@ -1,77 +0,0 @@ -/** - * \file ecp_invasive.h - * - * \brief ECP module: interfaces for invasive testing only. - * - * The interfaces in this file are intended for testing purposes only. - * They SHOULD NOT be made available in library integrations except when - * building the library for testing. - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MBEDTLS_ECP_INVASIVE_H -#define MBEDTLS_ECP_INVASIVE_H - -#include "../common.h" -#include "mbedtls/bignum.h" -#include "mbedtls/ecp.h" - -#if defined(MBEDTLS_TEST_HOOKS) && defined(MBEDTLS_ECP_C) - -#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || \ - defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) -/* Preconditions: - * - bits is a multiple of 64 or is 224 - * - c is -1 or -2 - * - 0 <= N < 2^bits - * - N has room for bits plus one limb - * - * Behavior: - * Set N to c * 2^bits + old_value_of_N. - */ -void mbedtls_ecp_fix_negative(mbedtls_mpi * N, signed char c, size_t bits); -#endif - -#if defined(MBEDTLS_ECP_MONTGOMERY_ENABLED) -/** Generate a private key on a Montgomery curve (Curve25519 or Curve448). - * - * This function implements key generation for the set of secret keys - * specified in [Curve25519] p. 5 and in [Curve448]. The resulting value - * has the lower bits masked but is not necessarily canonical. - * - * \note - [Curve25519] http://cr.yp.to/ecdh/curve25519-20060209.pdf - * - [RFC7748] https://tools.ietf.org/html/rfc7748 - * - * \p high_bit The position of the high-order bit of the key to generate. - * This is the bit-size of the key minus 1: - * 254 for Curve25519 or 447 for Curve448. - * \param d The randomly generated key. This is a number of size - * exactly \p n_bits + 1 bits, with the least significant bits - * masked as specified in [Curve25519] and in [RFC7748] ยง5. - * \param f_rng The RNG function. - * \param p_rng The RNG context to be passed to \p f_rng. - * - * \return \c 0 on success. - * \return \c MBEDTLS_ERR_ECP_xxx or MBEDTLS_ERR_MPI_xxx on failure. - */ -int mbedtls_ecp_gen_privkey_mx(size_t n_bits, mbedtls_mpi * d, int (*f_rng)(void *, unsigned char *, size_t), void * p_rng); - -#endif /* MBEDTLS_ECP_MONTGOMERY_ENABLED */ - -#endif /* MBEDTLS_TEST_HOOKS && MBEDTLS_ECP_C */ - -#endif /* MBEDTLS_ECP_INVASIVE_H */ diff --git a/src/platform/telink/crypto/internal/compatibility/padlock.h b/src/platform/telink/crypto/internal/compatibility/padlock.h deleted file mode 100644 index 6435a20..0000000 --- a/src/platform/telink/crypto/internal/compatibility/padlock.h +++ /dev/null @@ -1,112 +0,0 @@ -/** - * \file padlock.h - * - * \brief VIA PadLock ACE for HW encryption/decryption supported by some - * processors - * - * \warning These functions are only for internal use by other library - * functions; you must not call them directly. - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MBEDTLS_PADLOCK_H -#define MBEDTLS_PADLOCK_H - -#include "mbedtls/build_info.h" - -#include "mbedtls/aes.h" - -#define MBEDTLS_ERR_PADLOCK_DATA_MISALIGNED -0x0030 /**< Input data should be aligned. */ - -#if defined(__has_feature) -#if __has_feature(address_sanitizer) -#define MBEDTLS_HAVE_ASAN -#endif -#endif - -/* Some versions of ASan result in errors about not enough registers */ -#if defined(MBEDTLS_HAVE_ASM) && defined(__GNUC__) && defined(__i386__) && !defined(MBEDTLS_HAVE_ASAN) - -#ifndef MBEDTLS_HAVE_X86 -#define MBEDTLS_HAVE_X86 -#endif - -#include - -#define MBEDTLS_PADLOCK_RNG 0x000C -#define MBEDTLS_PADLOCK_ACE 0x00C0 -#define MBEDTLS_PADLOCK_PHE 0x0C00 -#define MBEDTLS_PADLOCK_PMM 0x3000 - -#define MBEDTLS_PADLOCK_ALIGN16(x) (uint32_t *) (16 + ((int32_t)(x) & ~15)) - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * \brief Internal PadLock detection routine - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param feature The feature to detect - * - * \return non-zero if CPU has support for the feature, 0 otherwise - */ -int mbedtls_padlock_has_support(int feature); - -/** - * \brief Internal PadLock AES-ECB block en(de)cryption - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param ctx AES context - * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT - * \param input 16-byte input block - * \param output 16-byte output block - * - * \return 0 if success, 1 if operation failed - */ -int mbedtls_padlock_xcryptecb(mbedtls_aes_context * ctx, int mode, const unsigned char input[16], unsigned char output[16]); - -/** - * \brief Internal PadLock AES-CBC buffer en(de)cryption - * - * \note This function is only for internal use by other library - * functions; you must not call it directly. - * - * \param ctx AES context - * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT - * \param length length of the input data - * \param iv initialization vector (updated after use) - * \param input buffer holding the input data - * \param output buffer holding the output data - * - * \return 0 if success, 1 if operation failed - */ -int mbedtls_padlock_xcryptcbc(mbedtls_aes_context * ctx, int mode, size_t length, unsigned char iv[16], const unsigned char * input, - unsigned char * output); - -#ifdef __cplusplus -} -#endif - -#endif /* HAVE_X86 */ - -#endif /* padlock.h */ diff --git a/src/platform/telink/crypto/internal/multithread.h b/src/platform/telink/crypto/internal/multithread.h deleted file mode 100644 index 5588b7a..0000000 --- a/src/platform/telink/crypto/internal/multithread.h +++ /dev/null @@ -1,41 +0,0 @@ -/** - * \file multithread.h - * - * \brief Utility macros for internal use in the library - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MBEDTLS_TEST_UTILS_H -#define MBEDTLS_TEST_UTILS_H - -#ifdef __cplusplus -extern "C" { -#endif - -void mbedtls_entropy_lock(void); -void mbedtls_entropy_unlock(void); -void mbedtls_ecp_lock(void); -void mbedtls_ecp_unlock(void); -void mbedtls_aes_lock(void); -void mbedtls_aes_unlock(void); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/platform/telink/crypto/internal/test_utils.h b/src/platform/telink/crypto/internal/test_utils.h deleted file mode 100644 index 422986e..0000000 --- a/src/platform/telink/crypto/internal/test_utils.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * \file test_utils.h - * - * \brief Utility macros for internal use in the library - */ -/* - * Copyright The Mbed TLS Contributors - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MBEDTLS_TEST_UTILS_H -#define MBEDTLS_TEST_UTILS_H - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -void mbedtls_printbuf(const char * comment, const void * buf, size_t len); -void mbedtls_printbuf_c(const char * comment, const void * buf, size_t len); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/platform/telink/crypto/telink-mbedtls-config.h b/src/platform/telink/crypto/telink-mbedtls-config.h deleted file mode 100644 index 7ed6f7a..0000000 --- a/src/platform/telink/crypto/telink-mbedtls-config.h +++ /dev/null @@ -1,117 +0,0 @@ -/* - * - * Copyright (c) 2021 Project CHIP Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -/** - * @file - * Platform-specific configuration for mbedtls * - */ - -#include - -#pragma once - -#define MBEDTLS_PLATFORM_SNPRINTF_MACRO snprintf - -#define MBEDTLS_CIPHER_MODE_CTR -#define MBEDTLS_SHA512_C -#define MBEDTLS_ECDH_C -#define MBEDTLS_SHA1_C -#define MBEDTLS_HKDF_C -#define MBEDTLS_CHACHAPOLY_C -#define MBEDTLS_POLY1305_C -#define MBEDTLS_CHACHA20_C -#define MBEDTLS_SSL_KEEP_PEER_CERTIFICATE -#define MBEDTLS_ECP_DP_CURVE25519_ENABLED -#define MBEDTLS_AES_C -#define MBEDTLS_AES_ROM_TABLES -#define MBEDTLS_ASN1_PARSE_C -#define MBEDTLS_ASN1_WRITE_C -#define MBEDTLS_BIGNUM_C -#define MBEDTLS_CCM_C -#define MBEDTLS_CIPHER_C -#define MBEDTLS_CMAC_C -#define MBEDTLS_CTR_DRBG_C -#define MBEDTLS_ECJPAKE_C -#define MBEDTLS_ECP_C -#define MBEDTLS_ECP_DP_SECP256R1_ENABLED -#define MBEDTLS_ECP_NIST_OPTIM -#define MBEDTLS_ENTROPY_C -#define MBEDTLS_HAVE_ASM -#define MBEDTLS_HMAC_DRBG_C -#define MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED -#define MBEDTLS_MD_C -#define MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES -#define MBEDTLS_NO_PLATFORM_ENTROPY -#define MBEDTLS_PK_C -#define MBEDTLS_PK_PARSE_C -#define MBEDTLS_PK_WRITE_C -#define MBEDTLS_PKCS5_C -#define MBEDTLS_PLATFORM_C -#define MBEDTLS_PLATFORM_MEMORY -#define MBEDTLS_PLATFORM_NO_STD_FUNCTIONS -#define MBEDTLS_SHA256_C -#define MBEDTLS_SHA256_SMALLER -#define MBEDTLS_SSL_CLI_C -#define MBEDTLS_SSL_DTLS_ANTI_REPLAY -#define MBEDTLS_SSL_DTLS_HELLO_VERIFY -#define MBEDTLS_SSL_EXPORT_KEYS -#define MBEDTLS_SSL_MAX_FRAGMENT_LENGTH -#define MBEDTLS_SSL_PROTO_TLS1_2 -#define MBEDTLS_SSL_PROTO_DTLS -#define MBEDTLS_SSL_TLS_C -#define MBEDTLS_SSL_COOKIE_C -#define MBEDTLS_SSL_SRV_C -#define MBEDTLS_KEY_EXCHANGE_PSK_ENABLED -#define MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED -#define MBEDTLS_BASE64_C -#define MBEDTLS_ECDH_C -#define MBEDTLS_ECDSA_C -#define MBEDTLS_OID_C -#define MBEDTLS_PEM_PARSE_C -#define MBEDTLS_X509_USE_C -#define MBEDTLS_X509_CRT_PARSE_C -#define MBEDTLS_X509_CREATE_C -#define MBEDTLS_X509_CSR_WRITE_C -#define MBEDTLS_ECDSA_DETERMINISTIC - -#if OPENTHREAD_CONFIG_ECDSA_ENABLE -#define MBEDTLS_BASE64_C -#define MBEDTLS_ECDH_C -#define MBEDTLS_ECDSA_C -#define MBEDTLS_OID_C -#define MBEDTLS_PEM_PARSE_C -#endif - -#define MBEDTLS_MPI_WINDOW_SIZE 1 /**< Maximum windows size used. */ -#define MBEDTLS_MPI_MAX_SIZE 32 /**< Maximum number of bytes for usable MPIs. */ -#define MBEDTLS_ECP_MAX_BITS 256 /**< Maximum bit size of groups */ -#define MBEDTLS_ECP_WINDOW_SIZE 2 /**< Maximum window size used */ -#define MBEDTLS_ECP_FIXED_POINT_OPTIM 0 /**< Enable fixed-point speed-up */ -#define MBEDTLS_ENTROPY_MAX_SOURCES 1 /**< Maximum number of sources supported */ - -#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE -#define MBEDTLS_PLATFORM_STD_CALLOC otPlatCAlloc /**< Default allocator to use, can be undefined */ -#define MBEDTLS_PLATFORM_STD_FREE otPlatFree /**< Default free to use, can be undefined */ -#else -#define MBEDTLS_MEMORY_BUFFER_ALLOC_C -#endif - -#define MBEDTLS_SSL_CIPHERSUITES MBEDTLS_TLS_ECJPAKE_WITH_AES_128_CCM_8 -#define MBEDTLS_DEBUG_C - -#define MBEDTLS_ECP_ALT -#define MBEDTLS_AES_ALT diff --git a/src/platform/telink/telink-mbedtls-config.h b/src/platform/telink/telink-mbedtls-config.h new file mode 100644 index 0000000..0a37b4a --- /dev/null +++ b/src/platform/telink/telink-mbedtls-config.h @@ -0,0 +1,34 @@ +/* + * + * Copyright (c) 2022 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * @file + * Telink mbedtls configuration file. + * + */ + +#ifndef MBEDTLS_TSLR9_CONF_H +#define MBEDTLS_TSLR9_CONF_H + +#define MBEDTLS_HKDF_C +#define MBEDTLS_X509_CREATE_C +#define MBEDTLS_X509_CSR_WRITE_C + +#define MBEDTLS_AES_ALT +#define MBEDTLS_ECP_ALT + +#endif /* MBEDTLS_TSLR9_CONF_H */ diff --git a/src/sdkconfig_matter.h b/src/sdkconfig_matter.h index 11fc61a..afdace8 100644 --- a/src/sdkconfig_matter.h +++ b/src/sdkconfig_matter.h @@ -624,13 +624,16 @@ #define CONFIG_CHIP_KVS_NAMESPACE_PARTITION_LABEL "nvs" #define CONFIG_ESP_MATTER_MAX_DEVICE_TYPE_COUNT 16 #define CONFIG_ESP_MATTER_NVS_PART_NAME "nvs" +#define CONFIG_EXAMPLE_DAC_PROVIDER 1 #define CONFIG_ESP_MATTER_AGGREGATOR_ENDPOINT_COUNT 1 +#define CONFIG_ESP_MATTER_BRIDGE_INFO_PART_NAME "nvs" #define CONFIG_ESP_MATTER_CONSOLE_TASK_STACK 2048 #define CONFIG_ESP_MATTER_CONSOLE_MAX_COMMANDS 10 #define CONFIG_BUTTON_PERIOD_TIME_MS 5 #define CONFIG_BUTTON_DEBOUNCE_TICKS 2 #define CONFIG_BUTTON_SHORT_PRESS_TIME_MS 180 #define CONFIG_BUTTON_LONG_PRESS_TIME_MS 1500 +#define CONFIG_BUTTON_SERIAL_TIME_MS 20 #define CONFIG_ADC_BUTTON_MAX_CHANNEL 3 #define CONFIG_ADC_BUTTON_MAX_BUTTON_PER_CHANNEL 8 #define CONFIG_ADC_BUTTON_SAMPLE_TIMES 1