Skip to content

Commit

Permalink
ssh-encoding migration (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugeny authored Nov 10, 2024
1 parent a02871a commit d07f9e2
Show file tree
Hide file tree
Showing 43 changed files with 1,636 additions and 1,741 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/semver.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Rust
name: Semver check

on:
push:
Expand All @@ -21,5 +21,6 @@ jobs:

- name: Check semver compatibility (russh)
uses: obi1kenobi/cargo-semver-checks-action@v2
continue-on-error: true
with:
package: russh
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ russh-config = { path = "russh-config" }
aes = "0.8"
async-trait = "0.1"
byteorder = "1.4"
bytes = "1.7"
digest = "0.10"
delegate = "0.13"
futures = "0.3"
hmac = "0.12"
log = "0.4"
rand = "0.8"
sha1 = { version = "0.10", features = ["oid"] }
sha2 = { version = "0.10", features = ["oid"] }
signature = "2.2"
ssh-encoding = "0.2"
ssh-encoding = { version = "0.2", features = ["bytes"] }
ssh-key = { version = "0.6", features = [
"ed25519",
"rsa",
Expand Down
8 changes: 6 additions & 2 deletions cryptovec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ include = ["Cargo.toml", "src/lib.rs"]
license = "Apache-2.0"
name = "russh-cryptovec"
repository = "https://github.com/warp-tech/russh"
version = "0.7.3"
version = "0.8.0-beta.1"
rust-version = "1.60"

[dependencies]
libc = "0.2"
ssh-encoding = { workspace = true, optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = {version = "0.3", features = ["basetsd", "minwindef", "memoryapi"]}

[dev-dependencies]
wasm-bindgen-test = "0.3"
wasm-bindgen-test = "0.3"

[features]
ssh-encoding = ["dep:ssh-encoding"]
34 changes: 1 addition & 33 deletions cryptovec/src/cryptovec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::{Deref, DerefMut, Index, IndexMut, Range, RangeFrom, RangeFull, RangeTo};

use crate::platform::{self, memcpy, memset, mlock, munlock};
use crate::platform::{self, memset, mlock, munlock};

/// A buffer which zeroes its memory on `.clear()`, `.resize()`, and
/// reallocations, to avoid copying secrets around.
Expand Down Expand Up @@ -246,38 +246,6 @@ impl CryptoVec {
unsafe { *self.p.add(size) = s }
}

/// Append a new u32, big endian-encoded, at the end of this CryptoVec.
///
/// ```
/// let mut v = russh_cryptovec::CryptoVec::new();
/// let n = 43554;
/// v.push_u32_be(n);
/// assert_eq!(n, v.read_u32_be(0))
/// ```
pub fn push_u32_be(&mut self, s: u32) {
let s = s.to_be();
let x: [u8; 4] = s.to_ne_bytes();
self.extend(&x)
}

/// Read a big endian-encoded u32 from this CryptoVec, with the
/// first byte at position `i`.
///
/// ```
/// let mut v = russh_cryptovec::CryptoVec::new();
/// let n = 99485710;
/// v.push_u32_be(n);
/// assert_eq!(n, v.read_u32_be(0))
/// ```
pub fn read_u32_be(&self, i: usize) -> u32 {
assert!(i + 4 <= self.size);
let mut x: u32 = 0;
unsafe {
memcpy((&mut x) as *mut u32, self.p.add(i), 4);
}
u32::from_be(x)
}

/// Read `n_bytes` from `r`, and append them at the end of this
/// `CryptoVec`. Returns the number of bytes read (and appended).
pub fn read<R: std::io::Read>(
Expand Down
3 changes: 3 additions & 0 deletions cryptovec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ pub use cryptovec::CryptoVec;

// Platform-specific modules
mod platform;

#[cfg(feature = "ssh-encoding")]
mod ssh;
6 changes: 3 additions & 3 deletions cryptovec/src/platform/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ mod wasm;
// Re-export functions based on the platform
#[cfg(not(windows))]
#[cfg(not(target_arch = "wasm32"))]
pub use unix::{memcpy, memset, mlock, munlock};
pub use unix::{memset, mlock, munlock};
#[cfg(target_arch = "wasm32")]
pub use wasm::{memcpy, memset, mlock, munlock};
pub use wasm::{memset, mlock, munlock};
#[cfg(windows)]
pub use windows::{memcpy, memset, mlock, munlock};
pub use windows::{memset, mlock, munlock};

#[cfg(test)]
mod tests {
Expand Down
6 changes: 0 additions & 6 deletions cryptovec/src/platform/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,3 @@ pub fn memset(ptr: *mut u8, value: i32, size: usize) {
libc::memset(ptr as *mut c_void, value, size);
}
}

pub fn memcpy(dest: *mut u32, src: *const u8, size: usize) {
unsafe {
libc::memcpy(dest as *mut c_void, src as *const c_void, size);
}
}
20 changes: 20 additions & 0 deletions cryptovec/src/ssh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use ssh_encoding::{Reader, Result, Writer};

use crate::CryptoVec;

impl Reader for CryptoVec {
fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]> {
(&self[..]).read(out)
}

fn remaining_len(&self) -> usize {
self.len()
}
}

impl Writer for CryptoVec {
fn write(&mut self, bytes: &[u8]) -> Result<()> {
self.extend(bytes);
Ok(())
}
}
4 changes: 2 additions & 2 deletions pageant/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ futures = { workspace = true }
thiserror = { workspace = true }
rand = { workspace = true }
tokio = { workspace = true, features = ["io-util", "rt"] }
bytes = "1.7"
delegate = "0.13"
bytes = { workspace = true }
delegate.workspace = true

[target.'cfg(windows)'.dependencies]
windows = { version = "0.58", features = [
Expand Down
15 changes: 9 additions & 6 deletions russh-keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ rust-version = "1.65"
aes = { workspace = true }
async-trait = { workspace = true }
bcrypt-pbkdf = "0.10"
bytes = { workspace = true }
cbc = "0.1"
ctr = "0.9"
block-padding = { version = "0.3", features = ["std"] }
Expand All @@ -41,7 +42,9 @@ pkcs8 = { version = "0.10", features = ["pkcs5", "encryption"] }
rand = { workspace = true }
rand_core = { version = "0.6.4", features = ["std"] }
rsa = "0.9"
russh-cryptovec = { version = "0.7.0", path = "../cryptovec" }
russh-cryptovec = { version = "0.8.0-beta.1", path = "../cryptovec", features = [
"ssh-encoding",
] }
russh-util = { version = "0.46.0", path = "../russh-util" }
sec1 = { version = "0.7", features = ["pkcs8"] }
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -53,13 +56,13 @@ ssh-encoding = { workspace = true }
ssh-key = { workspace = true }
thiserror = { workspace = true }
typenum = "1.17"
yasna = { version = "0.5.0", features = ["bit-vec", "num-bigint"], optional = true }
yasna = { version = "0.5.0", features = [
"bit-vec",
"num-bigint",
], optional = true }
zeroize = "1.7"
getrandom = { version = "0.2.15", features = ["js"] }
tokio = { workspace = true, features = [
"io-util",
"time",
] }
tokio = { workspace = true, features = ["io-util", "time"] }

[features]
legacy-ed25519-pkcs8-parser = ["yasna"]
Expand Down
Loading

0 comments on commit d07f9e2

Please sign in to comment.