Skip to content

Commit

Permalink
Merge pull request #2 from DougAnderson444/switch-chacha20
Browse files Browse the repository at this point in the history
make `wasm32` compatible
  • Loading branch information
dhuseby authored Sep 20, 2024
2 parents ea25369 + 0335c53 commit 66eb925
Show file tree
Hide file tree
Showing 12 changed files with 439 additions and 307 deletions.
67 changes: 60 additions & 7 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: Rust
name: Rust

on:
workflow_dispatch:
push:
branches: [ "main" ]
pull_request:
Expand All @@ -11,12 +12,64 @@ env:

jobs:
build:
strategy:
matrix:
include:
# Linux (32-bit)
- target: i686-unknown-linux-gnu
platform: ubuntu-latest
rust: stable
deps: sudo apt update && sudo apt install gcc-multilib

runs-on: ubuntu-latest
# macOS (64-bit)
- target: x86_64-apple-darwin
platform: macos-latest
rust: stable

# Windows (64-bit)
- target: x86_64-pc-windows-msvc
platform: windows-latest
rust: stable
runs-on: ${{ matrix.platform }}
steps:
- uses: actions/checkout@v4
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
- run: ${{ matrix.deps }}
- run: cargo build --target ${{ matrix.target }} --release --all-features

wasm_builds:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.73.0 # MSRV
- stable
target:
- wasm32-wasi
- wasm32-unknown-unknown
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
target: ${{ matrix.target }}
- run: cargo build --target ${{ matrix.target }} --features wasm

test:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.73.0 # MSRV
- stable
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ matrix.rust }}
- run: cargo test --verbose --release
- run: cargo test --all-features # debug build
- run: cargo test --release --all-features --all-targets
27 changes: 21 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "Apache-2.0"

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

[dependencies]
bcrypt-pbkdf = "0.10"
Expand All @@ -21,20 +22,34 @@ k256 = "0.13"
multibase = { version = "1.0", git = "https://github.com/cryptidtech/rust-multibase.git" }
multicodec = { version = "1.0", git = "https://github.com/cryptidtech/rust-multicodec.git" }
multihash = { version = "1.0", git = "https://github.com/cryptidtech/multihash.git" }
multisig = { version = "1.0", git = "https://github.com/cryptidtech/multisig.git" }
multisig = { version = "^1.0", git = "https://github.com/cryptidtech/multisig.git" }
multitrait = { version = "1.0", git = "https://github.com/cryptidtech/multitrait.git" }
multiutil = { version = "1.0", git = "https://github.com/cryptidtech/multiutil.git" }
rand = "0.8"
sec1 = "0.7"
serde = { version = "1.0", default-features = false, features = ["alloc", "derive"], optional = true }
sodiumoxide = "0.2"
ssh-key = { version = "0.6", features = ["alloc", "crypto", "ed25519"]}
ssh-encoding = "0.2"
serde = { version = "1.0", default-features = false, features = [
"alloc",
"derive",
], optional = true }
chacha20 = "0.9"
poly1305 = "0.8"
thiserror = "1.0"
typenum = "1.17"
unsigned-varint = { version = "0.8", features = ["std"]}
unsigned-varint = { version = "0.8", features = ["std"] }
vsss-rs = "3.4"
zeroize = "1.7"
ssh-encoding = { version = "0.2" }

[target.'cfg(target_arch = "wasm32")'.dependencies]
ssh-key = { version = "0.6", default-features = false, features = [
"alloc",
"ecdsa",
"ed25519",
] }
getrandom = { version = "0.2", features = ["js"], optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
ssh-key = { version = "0.6", features = ["alloc", "crypto", "ed25519"] }

[dev-dependencies]
serde_test = "1.0"
Expand Down
5 changes: 3 additions & 2 deletions src/cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ mod tests {
.try_build()
.unwrap();

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

// generate a random secret key
let mut rng = rand::rngs::OsRng::default();
let mut rng = rand::rngs::OsRng;
let mk = mk::Builder::new_from_random_bytes(Codec::Ed25519Priv, &mut rng)
.unwrap()
.with_comment("test key")
Expand Down
51 changes: 44 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,7 @@ pub enum AttributesError {
pub enum ConversionsError {
/// Ssh key error
#[error(transparent)]
SshKey(#[from] ssh_key::Error),
/// Ssh key label error
#[error(transparent)]
SshKeyLabel(#[from] ssh_encoding::LabelError),
/// Ssh encoding error
#[error(transparent)]
SshEncoding(#[from] ssh_encoding::Error),
Ssh(#[from] SshErrors),
/// Public key operation failure
#[error("Public key error: {0}")]
PublicKeyFailure(String),
Expand All @@ -124,6 +118,49 @@ pub enum ConversionsError {
UnsupportedCodec(multicodec::Codec),
}

/// SSH Encoding Errors that cannot be handled by thiserror since they may not use the std feature
/// in the case of wasm32 target.
#[derive(Clone, Debug)]
pub enum SshErrors {
/// Error from [ssh_key::Error]
Key(ssh_key::Error),
/// Invalid label from [ssh_encoding::LabelError]
KeyLabel(ssh_encoding::LabelError),
/// Unexpected trailing data at end of message from [ssh_encoding::Error]
Encoding(ssh_encoding::Error),
}

/// Impl Display for EncodingError
impl std::fmt::Display for SshErrors {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
SshErrors::Key(err) => write!(f, "{}", err),
SshErrors::KeyLabel(err) => write!(f, "{}", err),
SshErrors::Encoding(err) => write!(f, "{}", err),
}
}
}

impl std::error::Error for SshErrors {}

impl From<ssh_encoding::Error> for SshErrors {
fn from(err: ssh_encoding::Error) -> Self {
SshErrors::Encoding(err)
}
}

impl From<ssh_key::Error> for SshErrors {
fn from(err: ssh_key::Error) -> Self {
SshErrors::Key(err)
}
}

impl From<ssh_encoding::LabelError> for SshErrors {
fn from(err: ssh_encoding::LabelError) -> Self {
SshErrors::KeyLabel(err)
}
}

/// Cipher errors created by this library
#[derive(Clone, Debug, thiserror::Error)]
#[non_exhaustive]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Idnetifier: Apache-2.0
//! multikey
//! Multikey Crate
#![warn(missing_docs)]
#![deny(
trivial_casts,
Expand Down Expand Up @@ -31,7 +31,7 @@ pub use views::{

/// Multikey type and functions
pub mod mk;
pub use mk::{KEY_CODECS, KEY_SHARE_CODECS, Builder, EncodedMultikey, Multikey};
pub use mk::{Builder, EncodedMultikey, Multikey, KEY_CODECS, KEY_SHARE_CODECS};

/// Nonce type
pub mod nonce;
Expand Down
Loading

0 comments on commit 66eb925

Please sign in to comment.