Skip to content

Commit 0c4272c

Browse files
committed
simulator: make simulator the server side in socket connection
In the first implementation of socket connection in simulator, it was acting as a client in the communication, trying to connect to a specific network host and port. It was done like this because `send_message.py` running in host machine was having problems connecting to a simulator But actually, now that we do not need to connect BitBox02 device physically while running the simulator, we also do not need to run `send_message.py` in the host machine. Also ideally simulator should be a server hosting the firmware to the clients. Signed-off-by: asi345 <inanata15@gmail.com>
1 parent 1c5491d commit 0c4272c

File tree

14 files changed

+33
-235
lines changed

14 files changed

+33
-235
lines changed

py/send_message.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,28 +1573,24 @@ class Simulator(PhysicalLayer):
15731573
def __init__(self) -> None:
15741574
self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
15751575
port = 15423
1576-
self.client_socket.bind(("", port))
1577-
self.client_socket.listen(50)
1578-
print(f"Waiting for connection on port {port}")
1579-
self.connection, addr = self.client_socket.accept()
1580-
print(f"Connected to {addr}")
1576+
self.client_socket.connect(("127.0.0.1", port))
1577+
if debug:
1578+
print("Connected to the simulator")
15811579

15821580
def write(self, data: bytes) -> None:
1583-
self.connection.send(data[1:])
1581+
self.client_socket.send(data[1:])
15841582
if debug:
15851583
print(f"Written to the simulator:\n{data.hex()[2:]}")
15861584

15871585
def read(self, size: int, timeout_ms: int) -> bytes:
1588-
res = self.connection.recv(64)
1586+
res = self.client_socket.recv(64)
15891587
if debug:
15901588
print(f"Read from the simulator:\n{res.hex()}")
15911589
return res
15921590

15931591
def __del__(self) -> None:
15941592
print("Simulator quit")
1595-
if self.connection:
1596-
self.connection.shutdown(socket.SHUT_RDWR)
1597-
self.connection.close()
1593+
self.client_socket.close()
15981594

15991595
simulator = Simulator()
16001596

src/rust/bitbox02-rust-c/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ target-c-unit-tests = [
5959
# enable these features
6060
"app-bitcoin",
6161
"app-ethereum",
62+
"app-cardano",
6263
"firmware",
6364
"c-unit-testing",
6465
]

test/simulator/CMakeLists.txt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Shift Cryptosecurity AG
1+
# Copyright 2024 Shift Cryptosecurity AG
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -31,7 +31,6 @@ set(IGNORE_SOURCES
3131
"src/memory/smarteeprom.c"
3232
"src/memory.mpu.c"
3333
)
34-
#f9b3ceaa91e
3534

3635
# Exclude some files which depends on the hardware.
3736
foreach(SOURCEFILE ${IGNORE_SOURCES})
@@ -106,7 +105,7 @@ target_include_directories(
106105
bitbox_objects-simulator
107106
PUBLIC
108107
${INCLUDES}
109-
${CMAKE_CURRENT_SOURCE_DIR}/framework/includes
108+
${CMAKE_CURRENT_SOURCE_DIR}/../unit-test/framework/includes
110109
${CMAKE_CURRENT_SOURCE_DIR}
111110
${CMAKE_BINARY_DIR}/src
112111
)
@@ -127,7 +126,7 @@ target_include_directories(
127126
bitbox-simulator
128127
PUBLIC
129128
${INCLUDES}
130-
${CMAKE_CURRENT_SOURCE_DIR}/framework/includes
129+
${CMAKE_CURRENT_SOURCE_DIR}/../unit-test/framework/includes
131130
${CMAKE_CURRENT_SOURCE_DIR}
132131
${CMAKE_BINARY_DIR}/src
133132
)
@@ -169,11 +168,6 @@ if(SANTIZE_UNDEFINED)
169168
target_compile_options(bitbox_objects-simulator PUBLIC "-fsanitize=undefined")
170169
target_compile_options(bitbox-simulator PUBLIC "-fsanitize=undefined")
171170
endif()
172-
if(COVERAGE)
173-
target_link_libraries(bitbox-simulator PUBLIC "--coverage")
174-
target_compile_options(bitbox_objects-simulator PUBLIC "--coverage")
175-
target_compile_options(bitbox-simulator PUBLIC "--coverage")
176-
endif()
177171

178172
#-----------------------------------------------------------------------------
179173
# Simulator

test/simulator/framework/eh_personality.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
// Needed to link the C unit test executables in /test/unit-test, which link to bitbox_merged.
16-
// `rust_eh_personality` is provided by Rust when building the firmware or running Rust unit tests
17-
//
18-
// See
19-
// https://doc.rust-lang.org/unstable-book/language-features/lang-items.html#writing-an-executable-without-stdlib.
20-
//
21-
// One could get rid of this and also considerably shrink the binary size by compiling core instead
22-
// of using pre-built binaries. See a proof of concept implementation here:
23-
// https://github.com/digitalbitbox/bitbox02-firmware/tree/build-std-PoC. We decided against doing
24-
// this for now as the feature seems immature and because of the warnings against using it in
25-
// production:
26-
// https://github.com/rust-lang/wg-cargo-std-aware/tree/81765f0eb744b9c47840c16f43a32c9f61fd7f0c#mvp-implementation
15+
// Needed to link the simulator executable in /test/simulator, which link to
16+
// bitbox_merged-simulator. `rust_eh_personality` is provided by Rust when building the firmware or
17+
// running Rust unit tests.
2718
void rust_eh_personality(void);
2819
void rust_eh_personality(void) {}

test/simulator/framework/includes/mock_cipher.h

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/simulator/framework/includes/mock_memory.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/simulator/framework/includes/mock_qtouch.h

Lines changed: 0 additions & 34 deletions
This file was deleted.

test/simulator/framework/includes/mock_screen_stack.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

test/simulator/framework/includes/test_random.h

Lines changed: 0 additions & 25 deletions
This file was deleted.

test/simulator/framework/mock_memory.c

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,37 +71,4 @@ void memory_read_chunk_mock(uint32_t chunk_num, uint8_t* chunk_out)
7171
void memory_read_shared_bootdata_mock(uint8_t* chunk_out)
7272
{
7373
memcpy(chunk_out, _memory_shared_data, FLASH_SHARED_DATA_LEN);
74-
}
75-
76-
static uint8_t _encrypted_seed_and_hmac[96];
77-
static uint8_t _encrypted_seed_and_hmac_len;
78-
79-
bool __wrap_memory_set_encrypted_seed_and_hmac(uint8_t* encrypted_seed_and_hmac, uint8_t len)
80-
{
81-
memcpy(_encrypted_seed_and_hmac, encrypted_seed_and_hmac, len);
82-
_encrypted_seed_and_hmac_len = len;
83-
return true;
84-
}
85-
86-
bool __wrap_memory_get_encrypted_seed_and_hmac(
87-
uint8_t* encrypted_seed_and_hmac_out,
88-
uint8_t* len_out)
89-
{
90-
*len_out = _encrypted_seed_and_hmac_len;
91-
memcpy(encrypted_seed_and_hmac_out, _encrypted_seed_and_hmac, *len_out);
92-
return true;
93-
}
94-
95-
static uint8_t _salt_root[32] = {
96-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
97-
0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
98-
};
99-
void mock_memory_set_salt_root(const uint8_t* salt_root)
100-
{
101-
memcpy(_salt_root, salt_root, 32);
102-
}
103-
bool __wrap_memory_get_salt_root(uint8_t* salt_root_out)
104-
{
105-
memcpy(salt_root_out, _salt_root, 32);
106-
return true;
107-
}
74+
}

0 commit comments

Comments
 (0)