Crates.io | rustkey |
lib.rs | rustkey |
version | 0.4.2 |
source | src |
created_at | 2024-03-12 01:03:02.436943 |
updated_at | 2024-10-31 20:24:19.468442 |
description | rusTkey — rust library for tillitis TKey application development |
homepage | |
repository | https://codeberg.org/rustkey/rustkey/ |
max_upload_size | |
id | 1170024 |
size | 70,397 |
A library for developing bare-bones applications for the tillitis TKey in rust.
warning this library is still in development. There may be API-breaking changes in the future.
The crate provides:
unsafe
operations),This library and the proposed set-up is chosen to keep things simple. The set-up itself requires no external dependencies and the linker-script and initial assembly-code are almost exactly as provided by tillitis.
note a few points of attention:
random(out, seed)
is not yet sufficiently mature. It is based on a persistent buffer of random bytes and 4-byte input from TRNG. Until new entropy is available, currently sampled entropy is used to derive new unpredictable bits. There is undoubtedly room for improvement.rustTkey
is provided to enable development in Rust, as opposed to a statement against C. Although Rust is praised for safer, more secure applications, I do not think the difference is always as big as proclaimed. Applications written in C, e.g. with a stack-only development approach, already avoid some issues. It may prove beneficial to consider the larger ecosystem before choosing either one.rusTkey
will panic in case of improper use: there is no point in obscuring or working around incorrect uses. There is more risk in making things seem okay. Instead, assertions and panics will signal improper use (or a bug in rusTkey).0.4.2
hash_firmware_rom(key)
for calculating a hash of the TKey device's firmware-ROM. It computes a hash over the complete available ROM-space. (Experimental)0.4.1
cpumonitor::monitor_application_memory
: fix address of "last" in monitoring range.0.4.0
io
:
read_available
, read_into_checked
, read_into_timed
, read_into_limited
, read_checked
, read_timed
.available()
to return usize
, which is the more appropriate data-type for what it represents.configuration()
, configure(bitrate, data_bits, stop_bits)
for configuring UART I/O operation. See documentation for parameter specifics. bitrate
is expressed in the representation used by the device.rustkey::Error
: added variants Underrun
and Timeout
used by controlled-read functions.timer::PRESCALE_MILLISECONDS
, also used for timed-read functions.rustkey::TK1_CPU_FREQUENCY
as the CPU frequency of the TKey1.0.3.1
rustkey::random
: proper unsafe access to buffer without using &mut
(static_mut_refs
)0.3.0
rustkey::random
: remix existing TRNG entropy if TRNG is not yet ready. Given TRNG's rate of approx. 66.6 times per second, this improves performance.bench
from bench_trng
, as it tests a few more aspects of TKey.README.md
from CHANGELOG.md
, to make it available wherever the README is readable.0.2.0
cpumonitor
: monitor_range
allow first
and last
to be same address.timer
:
PRESCALE_SECONDS
prescaler constant.sleep
for timer-based blocking sleep in seconds.touch
: tweak, improve request
.trng
:
read_next
, rename read_next_bytes
to read_bytes_next
.frame
provides LENGTH_MAX
for maximum length of frame according to the protocol.Error
derives Debug
trait.README.md
: comments documenting some configuration options, ref for minimizing rust binaries, clean-up.bench_trng
: application used to perform repeated sampling of TRNG entropy for purpose of testing performance. Seems to be stable at approximately 66.6 samples per second.0.1.0
Initial release.
These are known open issues.
cargo build
warns of an unstable feature. Maybe we should file an issue for this.
cargo build --release --bins
warning: unknown and unstable feature specified for `-Ctarget-feature`: `zmmul`
|
= note: it is still passed through to the codegen backend, but use of this feature might be unsound and the behavior of this feature can change in the future
= help: consider filing a feature request
Apart from items listed here, I leave TODO
comments for things that need to be considered, even if they turn out to be irrelevant.
std
or customized threading).main
is obvious.random
can be improved within reasonable constraints.The following describes the application set-up needed to build app binaries for loading onto the TKey.
The following steps are required:
riscv32i-unknown-none-elf
, with additional CPU features 'c
' (compressed instructions) and 'Zmmul
' (multiplication instructions only, i.e. not division). Other build-options contribute to a smaller binary such that it fits in 128 KiB of memory.llvm-objcopy
to create the raw bare-bones application binary for the tillitis TKey.src/main.rs
is an example that uses this set-up and demonstrates a working TKey-app.The build target riscv32i-unknown-none-elf
must be available in the rust ecosystem.
rustup target add riscv32i-unknown-none-elf
The following build configuration enables the necessary features and options.
.cargo/config.toml
:
[build]
target = "riscv32i-unknown-none-elf"
[profile.dev]
panic = "abort"
[profile.release]
codegen-units = 1 # No parallelism, may increase ability to optimize.
debug = false # Do not produce a debug-build.
debug-assertions = false # Drop debug-assertions.
opt-level = 3 # Perform a high level of optimization, also 'z' to specifically target size.
overflow-checks = false
strip = true # Currently translates to 'strip = "symbols"', i.e. strip all excess information.
lto = true # Perform link-time optimization, to further optimize and reduce binary size.
panic = "abort"
[target.riscv32i-unknown-none-elf]
rustflags = [
"-Ctarget-feature=+c,+zmmul",
"-Ccode-model=medium",
"-Cpanic=abort",
"-Crelocation-model=pic",
"-Clink-arg=-Tapp.lds",
]
This configuration applies to --release
builds.
Note: the panic
settings should obviate the need for .eh_frame
section in the linker-script, but this is not currently the case.
The following linker-script defines the memory region for the TKey.
Changes to the original linker-script:
_stack_start
symbol such that this address can be modified outside of assembly-code._stack_start
is assigned a different value: 'ORIGIN(RAM)+LENGTH(RAM)
' from the original value '0x4001fff0
'. The original value is slightly lower, which I suspect is unnecessary given that the stack-pointer is manipulated before use. (issue)*(.eh_frame)
to the .text
region.panic=abort
should obviate the need for .eh_frame
section according to many references and from my own understanding, as it disables stack-unwinding. There should be no need to keep extra data. This changed, however, quite suddenly with no clear explanation. Adding the .eh_frame
section produces a memory-layout that accomodates for this, although it would be better if we could drop it completely./*
* SPDX-FileCopyrightText: 2022 Tillitis AB <tillitis.se>
* SPDX-License-Identifier: BSD-2-Clause
*
* Modified for rusTkey. (See README.md for details.)
*/
OUTPUT_ARCH( "riscv" )
ENTRY(_start)
MEMORY
{
RAM (rwx) : ORIGIN = 0x40000000, LENGTH = 0x20000 /* 128 KB */
}
SECTIONS
{
.text.init :
{
*(.text.init)
} >RAM
.text :
{
. = ALIGN(4);
*(.text) /* .text sections (code) */
*(.text*) /* .text* sections (code) */
*(.rodata) /* .rodata sections (constants, strings, etc.) */
*(.rodata*) /* .rodata* sections (constants, strings, etc.) */
*(.srodata) /* .rodata sections (constants, strings, etc.) */
*(.srodata*) /* .rodata* sections (constants, strings, etc.) */
*(.eh_frame)
. = ALIGN(4);
_etext = .;
_sidata = _etext;
} >RAM
.data : AT (_etext)
{
. = ALIGN(4);
_sdata = .;
. = ALIGN(4);
*(.data) /* .data sections */
*(.data*) /* .data* sections */
*(.sdata) /* .sdata sections */
*(.sdata*) /* .sdata* sections */
. = ALIGN(4);
_edata = .;
} >RAM
/* Uninitialized data section */
.bss :
{
. = ALIGN(4);
_sbss = .;
*(.bss)
*(.bss*)
*(.sbss)
*(.sbss*)
*(COMMON)
. = ALIGN(4);
_ebss = .;
} >RAM
}
_stack_start = ORIGIN(RAM) + LENGTH(RAM);
The following (global) assembly-code create the entry-point _start
at which the device is initialized for the loaded application, after which function main
is called.
The response of the device on returning from 'main
' seems to deviate based on the optimization-level of the application binary. It is best to define main as '#[no_mangle] extern "C" fn main() -> !
', i.e. non-returning as to avoid running into this situation.
Changes to the original assembly-code:
_stack_start
to be defined in the linker-script for initialization of the stack-pointer.// Note: this assembly provides the initialization procedure that clears memory and finally calls
// `main` to start execution of the app-binary.
global_asm!(
".section \".text.init\"",
".global _start",
"_start:",
"li x1, 0",
"li x2, 0",
"li x3, 0",
"li x4, 0",
"li x5, 0",
"li x6, 0",
"li x7, 0",
"li x8, 0",
"li x9, 0",
"li x10,0",
"li x11,0",
"li x12,0",
"li x13,0",
"li x14,0",
"li x15,0",
"li x16,0",
"li x17,0",
"li x18,0",
"li x19,0",
"li x20,0",
"li x21,0",
"li x22,0",
"li x23,0",
"li x24,0",
"li x25,0",
"li x26,0",
"li x27,0",
"li x28,0",
"li x29,0",
"li x30,0",
"li x31,0",
/* init stack below 0x40020000 (TK1_RAM_BASE+TK1_RAM_SIZE) */
"la sp, _stack_start",
/* zero-init bss section */
"la a0, _sbss",
"la a1, _ebss",
"bge a0, a1, end_init_bss",
"loop_init_bss:",
"sw zero, 0(a0)",
"addi a0, a0, 4",
"blt a0, a1, loop_init_bss",
"end_init_bss:",
"call main",
options(raw)
);
Use 'llvm-objcopy --input-target=elf32-littleriscv --output-target=binary target/riscv32i-unknown-none-elf/release/example example.bin
' to produce the raw binary data for loading onto the TKey.
At this point, the set-up is done up to entering the application at function main
.
There are a few recommendations:
#![no_std]
' to prevent standard library from being included. This also means that std::*
is unavailable, although most primitive functions can also be found at core::*
.#![no_main]
' to indicate that we provide our own entry-point into the application#[no_mangle] extern "C" fn main() -> !
' as our definition for main, such that:
main
can be found after initialization,#[no_mangle] extern "C"
to ensure availability under the expected, unchanged function-name,#[panic_handler]
, given that the run-time is minimized.An basic example src/main.rs
can be found in this code-base.
An alternative approach, is to use a #![feature(start)]
macro with #[start]
annotated fn main()
. This is currently only possible if Rust nightly features are activated. This, together with appropriate build configuration to ensure that main
as a function is available in unmangled form, is sufficient get the application to run. Unfortunately, this does not resolve the issue with .eh_frame
described above.
For now, this crate will keep using the global_asm!
macro in an application. This creates a straight-forward path from linker-script (app.lds
) to global assembly (_start
) to calling main()
. This also leaves open possibilities for a call to a set-up of stack-protector and other security-features that need early initialization, with everything concentrated within the application.
Copyright 2024 D. van Heumen
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
See LICENSE
for the full license.
TODO support for heap memory (dynamic allocation)?
Embedded RiscV (riscv, riscv-rt, etc.) code-base