1
0
Fork 0
mirror of https://gitlab.redox-os.org/CoffeeCode/redox-ssh.git synced 2025-12-28 13:22:19 +01:00

Fix (clippy): Replace unit result error types with error types (result_unit_err)

e.g., `Result<Vec<u8>, ()>`

All remaining warnings are in regards to unused variables, methods, enum variants, fields, or unreachable patterns.
This commit is contained in:
Laurenz 2024-09-26 16:10:41 +02:00
parent cedf6ce410
commit c68ba22265
Signed by: C0ffeeCode
SSH key fingerprint: SHA256:jnEltBNftC3wUZESLSMvM9zVPOkkevGRzqqoW2k2ORI
2 changed files with 34 additions and 12 deletions

View file

@ -4,7 +4,9 @@ use std::io::ErrorKind::InvalidData;
use crypto::ed25519;
use rand::RngCore;
use crate::public_key::{CryptoSystem, KeyPair};
use crate::public_key::{
CryptoSystem, KeyPair, KeyPairIdValidationError, SigningError
};
pub static ED25519: CryptoSystem = CryptoSystem {
id: "ed25519",
@ -84,32 +86,34 @@ impl KeyPair for Ed25519KeyPair {
self.private.is_some()
}
fn verify(&self, data: &[u8], signature: &[u8]) -> Result<bool, ()> {
use crate::packet::ReadPacketExt;
fn verify(&self, data: &[u8], signature: &[u8]) -> Result<bool, KeyPairIdValidationError> {
use std::io::Cursor;
use crate::packet::ReadPacketExt;
const EXPECTED_ID: &[u8] = b"ssh-ed25519";
let mut reader = Cursor::new(signature);
let id = reader.read_string().unwrap_or_default();
let received_id = reader.read_string().unwrap_or_default();
if id == b"ssh-ed25519" {
if received_id == EXPECTED_ID {
if let Ok(sig) = reader.read_string() {
return Ok(ed25519::verify(data, &self.public, sig.as_slice()));
}
}
Err(())
Err(KeyPairIdValidationError {received_id, expected_id: EXPECTED_ID })
}
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, ()> {
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, SigningError> {
use crate::packet::WritePacketExt;
if let Some(private_key) = self.private {
let mut result = Vec::new();
let sig = ed25519::signature(data, &private_key);
result.write_string("ssh-ed25519").or(Err(()))?;
result.write_bytes(&sig).or(Err(()))?;
result.write_string("ssh-ed25519")?;
result.write_bytes(&sig)?;
Ok(result)
}
else {
Err(())
Err(SigningError::NoPrivateKey)
}
}

View file

@ -11,8 +11,8 @@ pub trait KeyPair: Sync + Send {
fn has_private(&self) -> bool;
fn verify(&self, data: &[u8], signature: &[u8]) -> Result<bool, ()>;
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, ()>;
fn verify(&self, data: &[u8], signature: &[u8]) -> Result<bool, KeyPairIdValidationError>;
fn sign(&self, data: &[u8]) -> Result<Vec<u8>, SigningError>;
fn write_public(&self, w: &mut dyn Write) -> io::Result<()>;
fn export(&self, w: &mut dyn Write) -> io::Result<()>;
@ -24,3 +24,21 @@ pub struct CryptoSystem {
pub import: fn(r: &mut dyn Read) -> io::Result<Box<dyn KeyPair>>,
pub read_public: fn(r: &mut dyn Read) -> io::Result<Box<dyn KeyPair>>,
}
#[derive(Debug)]
pub enum SigningError {
NoPrivateKey,
Io(io::Error),
}
impl From<io::Error> for SigningError {
fn from(value: io::Error) -> Self {
SigningError::Io(value)
}
}
#[derive(Debug)]
pub struct KeyPairIdValidationError<'a> {
pub expected_id: &'a [u8],
pub received_id: Vec<u8>,
}