Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions ssh-key/src/mpint.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
//! Multiple precision integer

use crate::{Error, Result};
use alloc::vec::Vec;
use alloc::{boxed::Box, vec::Vec};
use core::fmt;
use core::hash::{Hash, Hasher};
use encoding::{CheckedSum, Decode, Encode, Reader, Writer};
use subtle::{Choice, ConstantTimeEq};
use zeroize::Zeroize;
Expand Down Expand Up @@ -42,7 +41,7 @@ use zeroize::Zeroizing;
#[derive(Clone, PartialOrd, Ord)]
pub struct Mpint {
/// Inner big endian-serialized integer value
inner: Vec<u8>,
inner: Box<[u8]>,
}

impl Mpint {
Expand All @@ -69,7 +68,7 @@ impl Mpint {
}

inner.extend_from_slice(bytes);
inner.try_into()
inner.into_boxed_slice().try_into()
}

/// Get the big integer data encoded as big endian bytes.
Expand Down Expand Up @@ -115,17 +114,11 @@ impl PartialEq for Mpint {
}
}

impl Hash for Mpint {
fn hash<H: Hasher>(&self, state: &mut H) {
self.inner.hash(state)
}
}

impl Decode for Mpint {
type Error = Error;

fn decode(reader: &mut impl Reader) -> Result<Self> {
Vec::decode(reader)?.try_into()
Vec::decode(reader)?.into_boxed_slice().try_into()
}
}

Expand All @@ -144,15 +137,15 @@ impl TryFrom<&[u8]> for Mpint {
type Error = Error;

fn try_from(bytes: &[u8]) -> Result<Self> {
Vec::from(bytes).try_into()
Vec::from(bytes).into_boxed_slice().try_into()
}
}

impl TryFrom<Vec<u8>> for Mpint {
impl TryFrom<Box<[u8]>> for Mpint {
type Error = Error;

fn try_from(bytes: Vec<u8>) -> Result<Self> {
match bytes.as_slice() {
fn try_from(bytes: Box<[u8]>) -> Result<Self> {
match &*bytes {
// Unnecessary leading 0
[0x00] => Err(Error::FormatEncoding),
// Unnecessary leading 0
Expand Down
13 changes: 12 additions & 1 deletion ssh-key/src/public/dsa.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Digital Signature Algorithm (DSA) public keys.

use crate::{Error, Mpint, Result};
use core::hash::{Hash, Hasher};
use encoding::{CheckedSum, Decode, Encode, Reader, Writer};

/// Digital Signature Algorithm (DSA) public key.
///
/// Described in [FIPS 186-4 § 4.1](https://csrc.nist.gov/publications/detail/fips/186/4/final).
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct DsaPublicKey {
/// Prime modulus.
pub p: Mpint,
Expand Down Expand Up @@ -53,6 +54,16 @@ impl Encode for DsaPublicKey {
}
}

impl Hash for DsaPublicKey {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.p.as_bytes().hash(state);
self.q.as_bytes().hash(state);
self.g.as_bytes().hash(state);
self.y.as_bytes().hash(state);
}
}

#[cfg(feature = "dsa")]
impl TryFrom<DsaPublicKey> for dsa::VerifyingKey {
type Error = Error;
Expand Down
2 changes: 1 addition & 1 deletion ssh-key/src/public/key_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{DsaPublicKey, OpaquePublicKey, RsaPublicKey};
use super::{EcdsaPublicKey, SkEcdsaSha2NistP256};

/// Public key data.
#[derive(Clone, Debug, Hash, Ord, Eq, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum KeyData {
/// Digital Signature Algorithm (DSA) public key data.
Expand Down
11 changes: 10 additions & 1 deletion ssh-key/src/public/rsa.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Rivest–Shamir–Adleman (RSA) public keys.

use crate::{Error, Mpint, Result};
use core::hash::{Hash, Hasher};
use encoding::{CheckedSum, Decode, Encode, Reader, Writer};

#[cfg(feature = "rsa")]
Expand All @@ -13,7 +14,7 @@ use {
/// RSA public key.
///
/// Described in [RFC4253 § 6.6](https://datatracker.ietf.org/doc/html/rfc4253#section-6.6).
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct RsaPublicKey {
/// RSA public exponent.
pub e: Mpint,
Expand Down Expand Up @@ -49,6 +50,14 @@ impl Encode for RsaPublicKey {
}
}

impl Hash for RsaPublicKey {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.e.as_bytes().hash(state);
self.n.as_bytes().hash(state);
}
}

#[cfg(feature = "rsa")]
impl TryFrom<RsaPublicKey> for rsa::RsaPublicKey {
type Error = Error;
Expand Down