Skip to content
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

Fetch seed / RNG / attestation / user keys / appname / appversion from env at startup then stick with it #440

Merged
merged 12 commits into from
Jan 11, 2024

Conversation

lpascal-ledger
Copy link
Contributor

@lpascal-ledger lpascal-ledger commented Dec 7, 2023

Feature 1

Previously, seed, RNG and app name & version were fetched from the environment each time they were needed.
When several Speculos instances runs simultaneously in the same shell, the last declared env variable would overwrite the environment variable for every Speculos instance, and thus every Speculos instances would use the same value (same seed, same RNG, same app name / version).

Now these information are fetched once when Speculos starts, and stored internally. Successive Speculos instances no longer overwrite these information for former Speculos instances which are still running.

Feature 2

Attestation key and user private keys were not configurable.
Now they can be.

Implementation

These features are implemented in a environment.c module which embeds static variables to stores these values.
This module exposes an init_environment function, which is called at the start of Speculos (early in launcher.c:main, before the emulator is properly started). This function fetches, checks and sets the variables from the environment, or keeps them with their default values if they are not defined.

Module which need these values (bolos/endorsement.c, bolos/cx.c, bolos/os.c, bolos/os_eip2333.c and bolos/os_bip32.c) can use env_get_[seed|rng|user_private_key|user_certificate|app_tag] to access them.

@lpascal-ledger lpascal-ledger marked this pull request as draft December 7, 2023 14:46
@lpascal-ledger lpascal-ledger force-pushed the persistent_seed branch 4 times, most recently from 1ab4095 to 74f578a Compare December 7, 2023 15:07
@lpascal-ledger lpascal-ledger marked this pull request as ready for review December 7, 2023 15:08
src/seed.c Outdated Show resolved Hide resolved
@lpascal-ledger lpascal-ledger marked this pull request as draft December 7, 2023 16:59
@lpascal-ledger
Copy link
Contributor Author

I hold this PR for some time as I'm going to have a very similar issue with the RNG.
Still I'm already interested with thoughts from @srasoamiaramanana-ledger as I'm going for a similar approach (only I'm going for an environment.[c|h] instead of seed.[c|h], and I'll rename [get|init]_seed with [get|init]_env_seed)

@xchapron-ledger
Copy link
Contributor

@lpascal-ledger In that case, there is also app name and version which could be handled similarly.

@lpascal-ledger lpascal-ledger force-pushed the persistent_seed branch 11 times, most recently from 319c98e to 1ab369b Compare December 20, 2023 13:16
@lpascal-ledger lpascal-ledger added the enhancement New feature or request label Dec 20, 2023
@lpascal-ledger lpascal-ledger marked this pull request as ready for review December 20, 2023 13:23
@lpascal-ledger
Copy link
Contributor Author

@xchapron-ledger This can be reviewed, I'm going to add unit tests but that's functionally ready.

src/environment.c Outdated Show resolved Hide resolved
@lpascal-ledger lpascal-ledger changed the title Fetch seed from env at startup then stick with it Fetch seed / RNG / attestation / user keys / appname / appversion from env at startup then stick with it Dec 21, 2023
src/environment.c Outdated Show resolved Hide resolved
Copy link
Contributor

@xchapron-ledger xchapron-ledger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xchapron-ledger
Copy link
Contributor

The launch output of Speculos get mixed info. I'm wondering if we can do something simple to fix it to ease readability?

@lpascal-ledger
Copy link
Contributor Author

lpascal-ledger commented Jan 10, 2024

$ speculos ~/repos/apps/boilerplate/app/bin/app.elf 
Device model detected from metadata: nanos
17:53:08.094:speculos: Disabling OCR.
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
[*] speculos launcher revision: 8e9897d
[*] using SDK version 6 on nanos
[*] loading CXLIB from "/home/lpascal/.virtualenvs/speculos/lib/python3.10/site-packages/speculos/cxlib/nanos-cx-2.1.elf"
[*] patching svc instruction at 0x126358
[*] Seed initialized from environment
[*] RNG initialized by default (time(NULL)): '1704905588'
[*] Private key ('ATTESTATION_PRIVATE_KEY') initialized with default value: '0x138fb9b91da745f12977a2b46f0bce2f0418b50fcb76631baf0f08ceefdb5d57'
[*] Private key ('USER_PRIVATE_KEY') initialized with default value: '0xe15e01d47082f0ea4771c99fe312f9d70093c89af47787fdf82e031f6728b710'
[*] User certificate 1 initialized (size: 71)
[*] Private key ('USER_PRIVATE_KEY') initialized with default value: '0xe15e01d47082f0ea4771c99fe312f9d70093c89af47787fdf82e031f6728b710'
 * Serving Flask app 'speculos.api.api'
 * Debug mode: off
17:53:08.302:werkzeug: WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.1.15:5000
17:53:08.302:werkzeug: Press CTRL+C to quit
QPixmap::scaled: Pixmap is a null pixmap
[*] User certificate 2 initialized (size: 71)
[*] Env app name: 'Boilerplate'
[*] Env app version: '2.1.0'
[*] patching svc instruction at 0x4000204a
[*] patching svc instruction at 0x40002058

What's bothering you? Given the tool and the amount of features, it still seems reasonable to me.

We could drop "default" lines such as [*] RNG initialized by default (time(NULL)): '1704905588', implying it would only print something if not the default behavior?

As some logs comes from the Python server(s?) which are asynchronous vs. the emulator, it's hard to manage some of the logs order.

@xchapron-ledger
Copy link
Contributor

What's bothering you? Given the tool and the amount of features, it still seems reasonable to me.

The fact that there are lines starting with *, [*] , timestamp, .. that are mixed, And would rather have them grouped.
But I'm probably being too picky?

@lpascal-ledger
Copy link
Contributor Author

Right I see. [*] comes from the emulator, all the rest is from the Flask server and other Python threads. As different programs, I don't know if we could arrange their logs, except by artificially postponing the server startup by one or two seconds.

@lpascal-ledger
Copy link
Contributor Author

lpascal-ledger commented Jan 10, 2024

Adding all this crypto stuff may have slowed the emulator startup enough for the Flask API to catchup and log on top of it, so this may be more mixed than it was.

@xchapron-ledger
Copy link
Contributor

Yeap, not sure that postponing is a good thing. This will slow test everywhere...
But I preferred the previous logs...

@xchapron-ledger
Copy link
Contributor

I'd say, let's remove logs that are mostly non necessary, so that output stay relevant, and let's merge.
Seed / RNG / Private Key / Certificate won't be necessary most of the time. Can we make them available only in a specific debug mode?

@lpascal-ledger
Copy link
Contributor Author

$ speculos ~/repos/apps/boilerplate/app/bin/app.elf 
Device model detected from metadata: nanos
18:32:27.220:speculos: Disabling OCR.
Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.
[*] speculos launcher revision: b253aa4
[*] using SDK version 6 on nanos
[*] loading CXLIB from "/home/lpascal/.virtualenvs/speculos/lib/python3.10/site-packages/speculos/cxlib/nanos-cx-2.1.elf"
[*] patching svc instruction at 0x126358
[*] Seed initialized from environment
 * Serving Flask app 'speculos.api.api'
 * Debug mode: off
18:32:27.368:werkzeug: WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on all addresses (0.0.0.0)
 * Running on http://127.0.0.1:5000
 * Running on http://192.168.1.15:5000
18:32:27.368:werkzeug: Press CTRL+C to quit
QPixmap::scaled: Pixmap is a null pixmap
[*] Env app name: 'Boilerplate'
[*] Env app version: '2.1.0'
[*] patching svc instruction at 0x4000204a
[*] patching svc instruction at 0x40002058

I've dropped quite some logs. Smaller, yet mixed up.

There's no logging mechanism in the emulator, only fprintf(stderr, ...), so hard to have DEBUG prints except by adding a whole log level mechanism - which is quite heavy to do.

And TBH, most of the logs here are not that interesting: either the values are the default one, and you can easily get them from the code if needed, or the user set them through the environment, and in this case it probably already has them in a variable or something.

@lpascal-ledger lpascal-ledger merged commit 3559f56 into master Jan 11, 2024
22 checks passed
@lpascal-ledger lpascal-ledger deleted the persistent_seed branch January 11, 2024 13:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants