Skip to content

Commit 01a5e57

Browse files
committed
Merge branch 'sha512'
2 parents e20d4f2 + daa745f commit 01a5e57

File tree

25 files changed

+371
-164
lines changed

25 files changed

+371
-164
lines changed

.ci/ci

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,14 @@ if arm-none-eabi-nm build/bin/firmware.elf | grep -q "strftime"; then
6363
echo "strftime adds significant binary bloat. Use custom formatting like in `format_dateimte()`."
6464
exit 1
6565
fi
66+
if arm-none-eabi-nm build/bin/firmware.elf | grep -q "sha26sha512"; then
67+
# sha26sha512 is a mangled Rust symbol standing for `sha2::sha512`.
68+
# One can use rustfilt to see the demangled symbols:
69+
# cargo install rustfilt; arm-none-eabi-nm build/bin/firmware.elf | rustfilt
70+
echo "sha2::Sha512 adds significant binary bloat."
71+
echo "Only use it if there is no other sha512 impl available that is smaller."
72+
exit 1
73+
fi
6674

6775
(cd tools/atecc608; go test ./...)
6876

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ add_custom_target(rust-bindgen
416416
--allowlist-function secp256k1_ecdsa_anti_exfil_host_commit
417417
--allowlist-function wally_get_secp_context
418418
--allowlist-function wally_hash160
419+
--allowlist-function wally_sha512
419420
--allowlist-function printf
420421
${CMAKE_CURRENT_SOURCE_DIR}/rust/bitbox02-sys/wrapper.h --
421422
-DPB_NO_PACKED_STRUCTS=1 -DPB_FIELD_16BIT=1 -fshort-enums ${RUST_BINDGEN_FLAGS} ${RUST_INCLUDES}

src/rust/.cargo/config.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ replace-with = "vendored-sources"
33

44
[source."git+https://github.com/digitalbitbox/rust-bip32-ed25519?tag=v0.1.2"]
55
git = "https://github.com/digitalbitbox/rust-bip32-ed25519"
6-
tag = "v0.1.2"
6+
tag = "v0.2.0"
77
replace-with = "vendored-sources"
88

99
[source.vendored-sources]

src/rust/Cargo.lock

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/rust/bitbox02-rust/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ zeroize = { workspace = true }
4040
num-bigint = { workspace = true, optional = true }
4141
num-traits = { version = "0.2", default-features = false }
4242
# If you change this, also change src/rust/.cargo/config.toml.
43-
bip32-ed25519 = { git = "https://github.com/digitalbitbox/rust-bip32-ed25519", tag = "v0.1.2", optional = true }
43+
bip32-ed25519 = { git = "https://github.com/digitalbitbox/rust-bip32-ed25519", tag = "v0.2.0", optional = true }
4444
bech32 = { version = "0.11.0", default-features = false, features = ["alloc"], optional = true }
4545
blake2 = { version = "0.10.6", default-features = false, features = ["size_opt"], optional = true }
4646
minicbor = { version = "0.24.0", default-features = false, features = ["alloc"], optional = true }
4747
crc = { version = "3.0.1", optional = true }
48-
ed25519-dalek = { version = "2.0.0", default-features = false, features = ["hazmat"], optional = true }
48+
ed25519-dalek = { version = "2.1.1", default-features = false, features = ["hazmat", "digest"], optional = true }
4949
lazy_static = { workspace = true, optional = true }
5050
hmac = { version = "0.12.1", default-features = false, features = ["reset"] }
5151

src/rust/bitbox02-rust/src/keystore/ed25519.rs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Shift Crypto AG
1+
// Copyright 2021, 2024 Shift Crypto 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.
@@ -15,22 +15,61 @@
1515
use alloc::vec::Vec;
1616

1717
use bip32_ed25519::{Xprv, Xpub, ED25519_EXPANDED_SECRET_KEY_SIZE};
18-
use sha2::Sha512;
18+
19+
/// Implements the digest traits for Sha512 backing it with the wally_sha512 C function. This is
20+
/// done to avoid using a second sha512 implementation like `sha2::Sha512`, which bloats the binary
21+
/// by an additional ~12.7kB (at the time of writing).
22+
///
23+
/// This implementation accumulates the data to be hashed in heap, it does **not** hash in a
24+
/// streaming fashion, even when using `update()`. This is okay for the use within this module, as
25+
/// bip32_ed25519 and sign_raw() do not hash a lot of data.
26+
#[derive(Default, Clone)]
27+
pub struct Sha512(Vec<u8>);
28+
29+
impl digest::HashMarker for Sha512 {}
30+
31+
impl digest::OutputSizeUser for Sha512 {
32+
type OutputSize = digest::typenum::U64;
33+
}
34+
35+
impl digest::FixedOutput for Sha512 {
36+
fn finalize_into(self, out: &mut digest::Output<Self>) {
37+
// use digest::Digest;
38+
// out.copy_from_slice(&sha2::Sha512::digest(&self.0));
39+
out.copy_from_slice(&bitbox02::sha512(&self.0));
40+
}
41+
}
42+
43+
impl digest::Update for Sha512 {
44+
fn update(&mut self, data: &[u8]) {
45+
self.0.extend(data);
46+
}
47+
}
48+
49+
impl digest::Reset for Sha512 {
50+
fn reset(&mut self) {
51+
self.0 = vec![];
52+
}
53+
}
54+
55+
impl digest::core_api::BlockSizeUser for Sha512 {
56+
type BlockSize = digest::typenum::U128;
57+
}
1958

2059
fn get_seed() -> Result<zeroize::Zeroizing<Vec<u8>>, ()> {
2160
bitbox02::keystore::get_ed25519_seed()
2261
}
2362

24-
fn get_xprv(keypath: &[u32]) -> Result<Xprv, ()> {
63+
fn get_xprv(keypath: &[u32]) -> Result<Xprv<Sha512>, ()> {
2564
let root = get_seed()?;
26-
Ok(Xprv::from_normalize(
65+
Ok(Xprv::<Sha512>::from_normalize(
2766
&root[..ED25519_EXPANDED_SECRET_KEY_SIZE],
2867
&root[ED25519_EXPANDED_SECRET_KEY_SIZE..],
2968
)
3069
.derive_path(keypath))
3170
}
3271

33-
pub fn get_xpub(keypath: &[u32]) -> Result<Xpub, ()> {
72+
pub fn get_xpub(keypath: &[u32]) -> Result<Xpub<Sha512>, ()> {
3473
Ok(get_xprv(keypath)?.public())
3574
}
3675

@@ -57,6 +96,24 @@ mod tests {
5796

5897
use bip32_ed25519::HARDENED_OFFSET;
5998
use bitbox02::testing::{mock_unlocked, mock_unlocked_using_mnemonic};
99+
use digest::Digest;
100+
101+
#[test]
102+
fn test_sha512() {
103+
assert_eq!(Sha512::digest(b"foobar"), sha2::Sha512::digest(b"foobar"));
104+
105+
let mut hasher: Sha512 = Default::default();
106+
hasher.update(b"foo");
107+
hasher.update(b"bar");
108+
assert_eq!(hasher.finalize(), sha2::Sha512::digest(b"foobar"));
109+
110+
hasher = Default::default();
111+
hasher.update(b"foo");
112+
hasher.update(b"bar");
113+
hasher.reset();
114+
hasher.update(b"baz");
115+
assert_eq!(hasher.finalize(), sha2::Sha512::digest(b"baz"));
116+
}
60117

61118
#[test]
62119
fn test_get_seed() {

src/rust/bitbox02/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,19 @@ pub fn println_stdout(msg: &str) {
255255
}
256256
}
257257

258+
pub fn sha512(msg: &[u8]) -> [u8; 64] {
259+
let mut result = [0u8; 64];
260+
unsafe {
261+
bitbox02_sys::wally_sha512(
262+
msg.as_ptr(),
263+
msg.len() as _,
264+
result.as_mut_ptr(),
265+
result.len() as _,
266+
);
267+
}
268+
result
269+
}
270+
258271
#[cfg(test)]
259272
mod tests {
260273
use super::*;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"files":{".github/workflows/ci.yml":"c8a9063d44963604b66d58eada923660f915ba0491051d1ffc052de32635e3b3","Cargo.toml":"58d701ce118c3e26d7cf28327acf2511cbf2b45e5977f0453347f6cdf016bccc","LICENSE-APACHE":"c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4","LICENSE-MIT":"5530596cde343de2238ec3276d94599ff6e78b7b7a20f6143162b3eceb727f29","README.md":"99a18dfc2a588d8b8cb3c287dad52d2e76c2b2b3be0e7a95cce19f509e9d59d7","src/arbitrary.rs":"ecb52788eec1142459ab252c320a5f0e421eb63cf959260b16e689ccfc8f2590","src/bigint.rs":"756b33c8d971b282b43268671c08fb4d8af095a6ebb6e9dc0c750631650e4d14","src/lib.rs":"8a663d5860d5ce9e5f200b786b7b4aec938627b141cc73260322f1cd9a20fec0","tests/table_test.rs":"a2e40ed32e495dbed8fc93f402476624e4609b9e5ef47a34e8be8dd8ed90dc92","tests/testdata/gen_table.py":"2fd91055920d9ac4cfb6c004b96aca42aaf2e3cf074d0d49d48ada993abdaedf","tests/testdata/table.json":"9e37a43d759f793b091f87488ea4b6d733154a10d29554c5103e12d617ae70cd"},"package":null}
1+
{"files":{".github/workflows/ci.yml":"f465a052857e00c4a513784ae8527fca34a9a0b1075c7511bbf302b96d6e9e36","Cargo.toml":"4f133fb93e1cfba99cee3a46432e4927593e4a88d24594b4b62d15c5922c5a34","LICENSE-APACHE":"c71d239df91726fc519c6eb72d318ec65820627232b2f796219e87dcf35d0ab4","LICENSE-MIT":"5530596cde343de2238ec3276d94599ff6e78b7b7a20f6143162b3eceb727f29","README.md":"99a18dfc2a588d8b8cb3c287dad52d2e76c2b2b3be0e7a95cce19f509e9d59d7","src/arbitrary.rs":"ecb52788eec1142459ab252c320a5f0e421eb63cf959260b16e689ccfc8f2590","src/bigint.rs":"756b33c8d971b282b43268671c08fb4d8af095a6ebb6e9dc0c750631650e4d14","src/lib.rs":"93a19d865e85bde9646e42ef6c6b5106d6876c71fcffedad4b2c523deeb6f951","tests/table_test.rs":"7cba52f2578cfaf44f7d01a14ae1ce3b2b21857079722b7f04a507ca2884d878","tests/testdata/gen_table.py":"2fd91055920d9ac4cfb6c004b96aca42aaf2e3cf074d0d49d48ada993abdaedf","tests/testdata/table.json":"9e37a43d759f793b091f87488ea4b6d733154a10d29554c5103e12d617ae70cd"},"package":null}

src/rust/vendor/bip32-ed25519/.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ on: [push, pull_request]
88
jobs:
99
lint:
1010
name: Lint
11-
runs-on: ubuntu-18.04
11+
runs-on: ubuntu-22.04
1212
steps:
1313
- name: Checkout sources
14-
uses: actions/checkout@v2
14+
uses: actions/checkout@v4
1515

1616
- name: Install stable toolchain
1717
uses: actions-rs/toolchain@v1
@@ -35,10 +35,10 @@ jobs:
3535

3636
test:
3737
name: Test Suite
38-
runs-on: ubuntu-18.04
38+
runs-on: ubuntu-22.04
3939
steps:
4040
- name: Checkout sources
41-
uses: actions/checkout@v2
41+
uses: actions/checkout@v4
4242

4343
- name: Install stable toolchain
4444
uses: actions-rs/toolchain@v1

src/rust/vendor/bip32-ed25519/Cargo.toml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
[package]
1313
edition = "2021"
1414
name = "bip32-ed25519"
15-
version = "0.1.2"
15+
version = "0.2.0"
1616
authors = ["Shift Crypto AG <support@shiftcrypto.ch>"]
1717
description = "BIP32-Ed25519"
1818
readme = "README.md"
@@ -27,15 +27,15 @@ license = "MIT OR Apache-2.0"
2727
version = "4"
2828
default-features = false
2929

30+
[dependencies.digest]
31+
version = "0.10"
32+
default-features = false
33+
3034
[dependencies.hmac]
3135
version = "0.12"
3236
features = ["reset"]
3337
default-features = false
3438

35-
[dependencies.sha2]
36-
version = "0.10"
37-
default-features = false
38-
3939
[dependencies.zeroize]
4040
version = "1"
4141
features = ["zeroize_derive"]
@@ -59,3 +59,7 @@ features = ["serde"]
5959
[dev-dependencies.serde]
6060
version = "1.0.104"
6161
features = ["derive"]
62+
63+
[dev-dependencies.sha2]
64+
version = "0.10"
65+
default-features = false

0 commit comments

Comments
 (0)