-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use ledger protocol to split NFC APDUs
- Loading branch information
1 parent
b2a5789
commit 17a28b6
Showing
3 changed files
with
144 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
|
||
/******************************************************************************* | ||
* Ledger Nano S - Secure firmware | ||
* (c) 2022 Ledger | ||
* | ||
* 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 OS_IO_NFC_H | ||
#define OS_IO_NFC_H | ||
|
||
void io_nfc_init(void); | ||
void io_nfc_recv_event(void); | ||
void io_nfc_send_response(const uint8_t *packet, uint16_t packet_length); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
|
||
/******************************************************************************* | ||
* Ledger Nano S - Secure firmware | ||
* (c) 2022 Ledger | ||
* | ||
* 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. | ||
********************************************************************************/ | ||
#include "bolos_target.h" | ||
#include "errors.h" | ||
#include "exceptions.h" | ||
#ifdef HAVE_NFC | ||
|
||
#if defined(DEBUG_OS_STACK_CONSUMPTION) | ||
#include "os_debug.h" | ||
#endif // DEBUG_OS_STACK_CONSUMPTION | ||
|
||
#include "os_io.h" | ||
#include "os_utils.h" | ||
#include "os_io_seproxyhal.h" | ||
#include <string.h> | ||
|
||
|
||
#ifdef DEBUG | ||
#define LOG printf | ||
#else | ||
#define LOG(...) | ||
#endif | ||
|
||
#include "os.h" | ||
#include "ledger_protocol.h" | ||
|
||
static uint8_t rx_apdu_buffer[IO_APDU_BUFFER_SIZE]; | ||
static ledger_protocol_t ledger_protocol_data; | ||
|
||
void io_nfc_init(void){ | ||
memset(&rx_apdu_buffer, 0, sizeof(rx_apdu_buffer)); | ||
memset(&ledger_protocol_data, 0, sizeof(ledger_protocol_data)); | ||
ledger_protocol_data.rx_apdu_buffer = rx_apdu_buffer; | ||
ledger_protocol_data.rx_apdu_buffer_max_length = sizeof(rx_apdu_buffer); | ||
ledger_protocol_data.mtu = MIN(sizeof(ledger_protocol_data.tx_chunk), sizeof(G_io_seproxyhal_spi_buffer) - 3); | ||
#ifdef HAVE_LOCAL_APDU_BUFFER | ||
ledger_protocol_data.rx_dst_buffer = NULL; | ||
#else | ||
ledger_protocol_data.rx_dst_buffer = G_io_apdu_buffer; | ||
#endif | ||
LEDGER_PROTOCOL_init(&ledger_protocol_data); | ||
} | ||
|
||
void io_nfc_recv_event(void) | ||
{ | ||
size_t size = U2BE(G_io_seproxyhal_spi_buffer, 1); | ||
|
||
//TODO check max size | ||
LEDGER_PROTOCOL_rx(&ledger_protocol_data, G_io_seproxyhal_spi_buffer+3, size); | ||
|
||
if (ledger_protocol_data.rx_apdu_status == APDU_STATUS_COMPLETE) { | ||
memcpy(ledger_protocol_data.rx_dst_buffer, | ||
ledger_protocol_data.rx_apdu_buffer, | ||
ledger_protocol_data.rx_apdu_length); | ||
G_io_app.apdu_length = ledger_protocol_data.rx_apdu_length; | ||
G_io_app.apdu_state = APDU_NFC; | ||
G_io_app.apdu_media = IO_APDU_MEDIA_NFC; | ||
ledger_protocol_data.rx_apdu_length = 0; | ||
ledger_protocol_data.rx_apdu_status = APDU_STATUS_WAITING; | ||
} | ||
} | ||
|
||
|
||
|
||
void io_nfc_send_response(const uint8_t *packet, uint16_t packet_length){ | ||
|
||
LEDGER_PROTOCOL_tx(&ledger_protocol_data, packet, packet_length); | ||
if (ledger_protocol_data.tx_chunk_length >= 2) { | ||
// reply the NFC APDU over SEPROXYHAL protocol | ||
G_io_seproxyhal_spi_buffer[0] = SEPROXYHAL_TAG_NFC_RAPDU; | ||
G_io_seproxyhal_spi_buffer[1] = (ledger_protocol_data.tx_chunk_length) >> 8; | ||
G_io_seproxyhal_spi_buffer[2] = (ledger_protocol_data.tx_chunk_length); | ||
memcpy(G_io_seproxyhal_spi_buffer+3, ledger_protocol_data.tx_chunk, ledger_protocol_data.tx_chunk_length); | ||
io_seproxyhal_spi_send(G_io_seproxyhal_spi_buffer, 3+ledger_protocol_data.tx_chunk_length); | ||
} | ||
|
||
while (ledger_protocol_data.tx_apdu_buffer) { | ||
LEDGER_PROTOCOL_tx(&ledger_protocol_data, NULL, 0); | ||
if (ledger_protocol_data.tx_chunk_length >= 2) { | ||
// reply the NFC APDU over SEPROXYHAL protocol | ||
G_io_seproxyhal_spi_buffer[0] = SEPROXYHAL_TAG_NFC_RAPDU; | ||
G_io_seproxyhal_spi_buffer[1] = (ledger_protocol_data.tx_chunk_length) >> 8; | ||
G_io_seproxyhal_spi_buffer[2] = (ledger_protocol_data.tx_chunk_length); | ||
memcpy(G_io_seproxyhal_spi_buffer+3, ledger_protocol_data.tx_chunk, ledger_protocol_data.tx_chunk_length); | ||
io_seproxyhal_spi_send(G_io_seproxyhal_spi_buffer, 3+ledger_protocol_data.tx_chunk_length); | ||
} | ||
} | ||
} | ||
|
||
#endif // HAVE_NFC | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters