-
Notifications
You must be signed in to change notification settings - Fork 29
Add support for iAP2 over seph protocol #1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /***************************************************************************** | ||
| * (c) 2025 Ledger SAS. | ||
| * | ||
| * 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 USB_LEDGER_IAP_H | ||
| #define USB_LEDGER_IAP_H | ||
|
|
||
| /* Includes ------------------------------------------------------------------*/ | ||
| #include <stdint.h> | ||
| #include "usbd_def.h" | ||
| #include "usbd_ledger_types.h" | ||
|
|
||
| /* Exported enumerations -----------------------------------------------------*/ | ||
|
|
||
| /* Exported defines --------------------------------------------------------*/ | ||
|
|
||
| /* Exported types, structures, unions ----------------------------------------*/ | ||
|
|
||
| /* Exported macros------------------------------------------------------------*/ | ||
|
|
||
| /* Exported variables --------------------------------------------------------*/ | ||
|
|
||
| /* Exported functions --------------------------------------------------------*/ | ||
|
|
||
| void USB_LEDGER_IAP_init(void); | ||
|
|
||
| int USB_LEDGER_iap_rx_seph_evt(uint8_t *seph_buffer, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor: harmonize function naming
|
||
| uint16_t seph_buffer_length, | ||
| uint8_t *apdu_buffer, | ||
| uint16_t apdu_buffer_max_length); | ||
|
|
||
| int USB_LEDGER_IAP_send_apdu(const uint8_t *apdu_buf, uint16_t apdu_buf_length); | ||
|
|
||
| #endif // USBD_LEDGER_WEBUSB_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,145 @@ | ||
| /***************************************************************************** | ||
| * (c) 2025 Ledger SAS. | ||
| * | ||
| * 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. | ||
| *****************************************************************************/ | ||
|
|
||
| /* Includes ------------------------------------------------------------------*/ | ||
| #include "ledger_protocol.h" | ||
| #include "usbd_ledger.h" | ||
| #include "usb_iap.h" | ||
| #include "seproxyhal_protocol.h" | ||
|
|
||
| /* Private enumerations ------------------------------------------------------*/ | ||
|
|
||
| /* Private defines------------------------------------------------------------*/ | ||
|
|
||
| /* Private types, structures, unions -----------------------------------------*/ | ||
|
|
||
| /* Private macros-------------------------------------------------------------*/ | ||
|
|
||
| /* Private functions prototypes ----------------------------------------------*/ | ||
|
|
||
| /* Private variables ---------------------------------------------------------*/ | ||
| static ledger_protocol_t protocol_data = {}; | ||
|
|
||
| /* Exported variables --------------------------------------------------------*/ | ||
|
|
||
| /* Private functions ---------------------------------------------------------*/ | ||
| static void USB_LEDGER_IAP_send_packet(uint8_t *buffer, uint16_t length) | ||
| { | ||
| if (length) { | ||
| unsigned char hdr[3]; | ||
| hdr[0] = SEPROXYHAL_TAG_USB_IAP_SEND; | ||
| hdr[1] = length >> 8; | ||
| hdr[2] = length; | ||
| os_io_tx_cmd(OS_IO_PACKET_TYPE_SEPH, hdr, 3, NULL); | ||
| os_io_tx_cmd(OS_IO_PACKET_TYPE_SEPH, buffer, length, NULL); | ||
| } | ||
| } | ||
|
|
||
| /* Exported functions --------------------------------------------------------*/ | ||
|
|
||
| void USB_LEDGER_IAP_init(void) | ||
| { | ||
| memset(&protocol_data, 0, sizeof(protocol_data)); | ||
| protocol_data.mtu = sizeof(USBD_LEDGER_protocol_chunk_buffer); | ||
|
|
||
| LEDGER_PROTOCOL_init(&protocol_data, OS_IO_PACKET_TYPE_USB_IAP_APDU); | ||
| } | ||
|
|
||
| int USB_LEDGER_IAP_send_apdu(const uint8_t *apdu_buf, uint16_t apdu_buf_length) | ||
| { | ||
| uint32_t status = -1; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be an |
||
|
|
||
| ledger_protocol_result_t result = LEDGER_PROTOCOL_tx(&protocol_data, | ||
| apdu_buf, | ||
| apdu_buf_length, | ||
| USBD_LEDGER_protocol_chunk_buffer, | ||
| sizeof(USBD_LEDGER_protocol_chunk_buffer), | ||
| 0); | ||
|
|
||
| if (result != LP_SUCCESS) { | ||
| goto error; | ||
| } | ||
| if (protocol_data.tx_chunk_length >= 2) { | ||
| USB_LEDGER_IAP_send_packet(USBD_LEDGER_protocol_chunk_buffer, | ||
| protocol_data.tx_chunk_length); | ||
| } | ||
|
|
||
| while (protocol_data.tx_apdu_buffer) { | ||
| result = LEDGER_PROTOCOL_tx(&protocol_data, | ||
| NULL, | ||
| 0, | ||
| USBD_LEDGER_protocol_chunk_buffer, | ||
| sizeof(USBD_LEDGER_protocol_chunk_buffer), | ||
| 0); | ||
| if (result != LP_SUCCESS) { | ||
| goto error; | ||
| } | ||
| if (protocol_data.tx_chunk_length >= 2) { | ||
| USB_LEDGER_IAP_send_packet(USBD_LEDGER_protocol_chunk_buffer, | ||
| protocol_data.tx_chunk_length); | ||
| } | ||
| } | ||
| status = 0; | ||
|
|
||
| error: | ||
| return status; | ||
| } | ||
|
|
||
| int USB_LEDGER_iap_rx_seph_evt(uint8_t *seph_buffer, | ||
| uint16_t seph_buffer_length, | ||
| uint8_t *apdu_buffer, | ||
| uint16_t apdu_buffer_max_length) | ||
| { | ||
| int status = -1; | ||
|
|
||
| if (seph_buffer[1] != SEPROXYHAL_TAG_USB_IAP_EVENT || seph_buffer_length < 4) { | ||
| goto error; | ||
| } | ||
|
|
||
| ledger_protocol_result_t result = LEDGER_PROTOCOL_rx(&protocol_data, | ||
| &seph_buffer[4], | ||
| seph_buffer_length - 4, | ||
| USBD_LEDGER_protocol_chunk_buffer, | ||
| sizeof(USBD_LEDGER_protocol_chunk_buffer), | ||
| apdu_buffer, | ||
| apdu_buffer_max_length, | ||
| 0); | ||
| if (result != LP_SUCCESS) { | ||
| goto error; | ||
| } | ||
|
|
||
| if (protocol_data.tx_chunk_length > 0) { | ||
| // Tx chunck was generated while processing rx | ||
| // For example MTU request -> MTU response | ||
| USB_LEDGER_IAP_send_packet(USBD_LEDGER_protocol_chunk_buffer, | ||
| protocol_data.tx_chunk_length); | ||
| protocol_data.tx_chunk_length = 0; | ||
| } | ||
|
|
||
| if (protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) { | ||
| // Should not happen as it is verified by LEDGER_PROTOCOL_rx() already | ||
| if (apdu_buffer_max_length < protocol_data.rx_apdu_length) { | ||
| status = -1; | ||
| } | ||
| else { | ||
| status = protocol_data.rx_apdu_length; | ||
| } | ||
| protocol_data.rx_apdu_status = APDU_STATUS_WAITING; | ||
| } | ||
|
|
||
| error: | ||
| return status; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't it be under ifdef ?