Skip to content

Commit 66eb925

Browse files
authored
Merge pull request #2 from DougAnderson444/switch-chacha20
make `wasm32` compatible
2 parents ea25369 + 0335c53 commit 66eb925

File tree

12 files changed

+439
-307
lines changed

12 files changed

+439
-307
lines changed

.github/workflows/rust.yml

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
name: Rust
1+
name: Rust
22

33
on:
4+
workflow_dispatch:
45
push:
56
branches: [ "main" ]
67
pull_request:
@@ -11,12 +12,64 @@ env:
1112

1213
jobs:
1314
build:
15+
strategy:
16+
matrix:
17+
include:
18+
# Linux (32-bit)
19+
- target: i686-unknown-linux-gnu
20+
platform: ubuntu-latest
21+
rust: stable
22+
deps: sudo apt update && sudo apt install gcc-multilib
1423

15-
runs-on: ubuntu-latest
24+
# macOS (64-bit)
25+
- target: x86_64-apple-darwin
26+
platform: macos-latest
27+
rust: stable
1628

29+
# Windows (64-bit)
30+
- target: x86_64-pc-windows-msvc
31+
platform: windows-latest
32+
rust: stable
33+
runs-on: ${{ matrix.platform }}
1734
steps:
18-
- uses: actions/checkout@v4
19-
- name: Build
20-
run: cargo build --verbose
21-
- name: Run tests
22-
run: cargo test --verbose
35+
- uses: actions/checkout@v4
36+
- uses: dtolnay/rust-toolchain@stable
37+
with:
38+
toolchain: ${{ matrix.rust }}
39+
target: ${{ matrix.target }}
40+
- run: ${{ matrix.deps }}
41+
- run: cargo build --target ${{ matrix.target }} --release --all-features
42+
43+
wasm_builds:
44+
runs-on: ubuntu-latest
45+
strategy:
46+
matrix:
47+
rust:
48+
- 1.73.0 # MSRV
49+
- stable
50+
target:
51+
- wasm32-wasi
52+
- wasm32-unknown-unknown
53+
steps:
54+
- uses: actions/checkout@v4
55+
- uses: dtolnay/rust-toolchain@stable
56+
with:
57+
toolchain: ${{ matrix.rust }}
58+
target: ${{ matrix.target }}
59+
- run: cargo build --target ${{ matrix.target }} --features wasm
60+
61+
test:
62+
runs-on: ubuntu-latest
63+
strategy:
64+
matrix:
65+
rust:
66+
- 1.73.0 # MSRV
67+
- stable
68+
steps:
69+
- uses: actions/checkout@v4
70+
- uses: dtolnay/rust-toolchain@stable
71+
with:
72+
toolchain: ${{ matrix.rust }}
73+
- run: cargo test --verbose --release
74+
- run: cargo test --all-features # debug build
75+
- run: cargo test --release --all-features --all-targets

Cargo.toml

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "Apache-2.0"
1010

1111
[features]
1212
default = ["serde"]
13+
wasm = ["getrandom/js"] # needed for CI testing on wasm32-unknown-unknown
1314

1415
[dependencies]
1516
bcrypt-pbkdf = "0.10"
@@ -21,20 +22,34 @@ k256 = "0.13"
2122
multibase = { version = "1.0", git = "https://github.com/cryptidtech/rust-multibase.git" }
2223
multicodec = { version = "1.0", git = "https://github.com/cryptidtech/rust-multicodec.git" }
2324
multihash = { version = "1.0", git = "https://github.com/cryptidtech/multihash.git" }
24-
multisig = { version = "1.0", git = "https://github.com/cryptidtech/multisig.git" }
25+
multisig = { version = "^1.0", git = "https://github.com/cryptidtech/multisig.git" }
2526
multitrait = { version = "1.0", git = "https://github.com/cryptidtech/multitrait.git" }
2627
multiutil = { version = "1.0", git = "https://github.com/cryptidtech/multiutil.git" }
2728
rand = "0.8"
2829
sec1 = "0.7"
29-
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"], optional = true }
30-
sodiumoxide = "0.2"
31-
ssh-key = { version = "0.6", features = ["alloc", "crypto", "ed25519"]}
32-
ssh-encoding = "0.2"
30+
serde = { version = "1.0", default-features = false, features = [
31+
"alloc",
32+
"derive",
33+
], optional = true }
34+
chacha20 = "0.9"
35+
poly1305 = "0.8"
3336
thiserror = "1.0"
3437
typenum = "1.17"
35-
unsigned-varint = { version = "0.8", features = ["std"]}
38+
unsigned-varint = { version = "0.8", features = ["std"] }
3639
vsss-rs = "3.4"
3740
zeroize = "1.7"
41+
ssh-encoding = { version = "0.2" }
42+
43+
[target.'cfg(target_arch = "wasm32")'.dependencies]
44+
ssh-key = { version = "0.6", default-features = false, features = [
45+
"alloc",
46+
"ecdsa",
47+
"ed25519",
48+
] }
49+
getrandom = { version = "0.2", features = ["js"], optional = true }
50+
51+
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
52+
ssh-key = { version = "0.6", features = ["alloc", "crypto", "ed25519"] }
3853

3954
[dev-dependencies]
4055
serde_test = "1.0"

src/cipher.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ mod tests {
9494
.try_build()
9595
.unwrap();
9696

97-
let nonce = hex::decode("00b61a43d4d1e8d7").unwrap();
97+
// ChaCha needs 12 bytes of nonce iaw RFC8439
98+
let nonce = hex::decode("00b61a43d4d1e8d700b61a43").unwrap();
9899
// create a cipher multikey
99100
let ciphermk = Builder::new(Codec::Chacha20Poly1305)
100101
.with_nonce(&nonce)
@@ -110,7 +111,7 @@ mod tests {
110111
.unwrap();
111112

112113
// generate a random secret key
113-
let mut rng = rand::rngs::OsRng::default();
114+
let mut rng = rand::rngs::OsRng;
114115
let mk = mk::Builder::new_from_random_bytes(Codec::Ed25519Priv, &mut rng)
115116
.unwrap()
116117
.with_comment("test key")

src/error.rs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,13 +103,7 @@ pub enum AttributesError {
103103
pub enum ConversionsError {
104104
/// Ssh key error
105105
#[error(transparent)]
106-
SshKey(#[from] ssh_key::Error),
107-
/// Ssh key label error
108-
#[error(transparent)]
109-
SshKeyLabel(#[from] ssh_encoding::LabelError),
110-
/// Ssh encoding error
111-
#[error(transparent)]
112-
SshEncoding(#[from] ssh_encoding::Error),
106+
Ssh(#[from] SshErrors),
113107
/// Public key operation failure
114108
#[error("Public key error: {0}")]
115109
PublicKeyFailure(String),
@@ -124,6 +118,49 @@ pub enum ConversionsError {
124118
UnsupportedCodec(multicodec::Codec),
125119
}
126120

121+
/// SSH Encoding Errors that cannot be handled by thiserror since they may not use the std feature
122+
/// in the case of wasm32 target.
123+
#[derive(Clone, Debug)]
124+
pub enum SshErrors {
125+
/// Error from [ssh_key::Error]
126+
Key(ssh_key::Error),
127+
/// Invalid label from [ssh_encoding::LabelError]
128+
KeyLabel(ssh_encoding::LabelError),
129+
/// Unexpected trailing data at end of message from [ssh_encoding::Error]
130+
Encoding(ssh_encoding::Error),
131+
}
132+
133+
/// Impl Display for EncodingError
134+
impl std::fmt::Display for SshErrors {
135+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
136+
match self {
137+
SshErrors::Key(err) => write!(f, "{}", err),
138+
SshErrors::KeyLabel(err) => write!(f, "{}", err),
139+
SshErrors::Encoding(err) => write!(f, "{}", err),
140+
}
141+
}
142+
}
143+
144+
impl std::error::Error for SshErrors {}
145+
146+
impl From<ssh_encoding::Error> for SshErrors {
147+
fn from(err: ssh_encoding::Error) -> Self {
148+
SshErrors::Encoding(err)
149+
}
150+
}
151+
152+
impl From<ssh_key::Error> for SshErrors {
153+
fn from(err: ssh_key::Error) -> Self {
154+
SshErrors::Key(err)
155+
}
156+
}
157+
158+
impl From<ssh_encoding::LabelError> for SshErrors {
159+
fn from(err: ssh_encoding::LabelError) -> Self {
160+
SshErrors::KeyLabel(err)
161+
}
162+
}
163+
127164
/// Cipher errors created by this library
128165
#[derive(Clone, Debug, thiserror::Error)]
129166
#[non_exhaustive]

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// SPDX-License-Idnetifier: Apache-2.0
2-
//! multikey
2+
//! Multikey Crate
33
#![warn(missing_docs)]
44
#![deny(
55
trivial_casts,
@@ -31,7 +31,7 @@ pub use views::{
3131

3232
/// Multikey type and functions
3333
pub mod mk;
34-
pub use mk::{KEY_CODECS, KEY_SHARE_CODECS, Builder, EncodedMultikey, Multikey};
34+
pub use mk::{Builder, EncodedMultikey, Multikey, KEY_CODECS, KEY_SHARE_CODECS};
3535

3636
/// Nonce type
3737
pub mod nonce;

0 commit comments

Comments
 (0)