tasign 0.2.3

TA ELF signing utilities with CMS/PKCS#7 support
Documentation
//! Map `tee_crypto` errors and RNG into tasign types.

use crate::crypto::CryptoError;

pub(crate) fn map_tee_err(err: tee_crypto::CryptoError) -> CryptoError {
    match err {
        tee_crypto::CryptoError::InvalidKey => CryptoError::InvalidKey,
        tee_crypto::CryptoError::InvalidInput => CryptoError::InvalidInput,
        tee_crypto::CryptoError::InvalidLength => CryptoError::InvalidLength,
        tee_crypto::CryptoError::AlgorithmMismatch => CryptoError::InvalidInput,
        tee_crypto::CryptoError::EncodingMismatch => CryptoError::InvalidInput,
        tee_crypto::CryptoError::InvalidDigestAlgorithm => CryptoError::UnsupportedAlgorithm,
        tee_crypto::CryptoError::InvalidSignatureEncoding => CryptoError::InvalidInput,
        tee_crypto::CryptoError::InvalidCiphertextAlgorithm => CryptoError::UnsupportedAlgorithm,
        tee_crypto::CryptoError::BufferTooSmall => CryptoError::InvalidLength,
        tee_crypto::CryptoError::DivideByZero => CryptoError::InternalError,
        tee_crypto::CryptoError::InvalidModulus => CryptoError::InvalidKey,
        tee_crypto::CryptoError::InvalidExponent => CryptoError::InvalidKey,
        tee_crypto::CryptoError::ArithmeticOverflow => CryptoError::InternalError,
        tee_crypto::CryptoError::UnsupportedAlgorithm => CryptoError::UnsupportedAlgorithm,
        tee_crypto::CryptoError::VerificationFailed => CryptoError::VerificationFailed,
        tee_crypto::CryptoError::InternalError => CryptoError::InternalError,
        tee_crypto::CryptoError::Backend(_) => CryptoError::InternalError,
    }
}

#[cfg(feature = "std")]
mod rng_bridge {
    /// Bridge tasign [`super::super::rng::CryptoRng`] to [`tee_crypto::rng::CryptoRng`].
    pub(crate) struct TasignRngAdapter<'a>(pub &'a mut dyn super::super::rng::CryptoRng);

    impl rand_core_0_10::TryRng for TasignRngAdapter<'_> {
        type Error = core::convert::Infallible;

        fn try_next_u32(&mut self) -> core::result::Result<u32, Self::Error> {
            let mut buf = [0u8; 4];
            self.try_fill_bytes(&mut buf)?;
            Ok(u32::from_le_bytes(buf))
        }

        fn try_next_u64(&mut self) -> core::result::Result<u64, Self::Error> {
            let mut buf = [0u8; 8];
            self.try_fill_bytes(&mut buf)?;
            Ok(u64::from_le_bytes(buf))
        }

        fn try_fill_bytes(&mut self, dest: &mut [u8]) -> core::result::Result<(), Self::Error> {
            self.0.fill_bytes(dest);
            Ok(())
        }
    }

    impl rand_core_0_10::TryCryptoRng for TasignRngAdapter<'_> {}
}

#[cfg(feature = "std")]
pub(crate) use rng_bridge::TasignRngAdapter;