Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

`digest::Update`, `FixedOutput`, and `Mac` are infallible; hardware backends are forced to panic when operations fail

Aperta
#2,366 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Tranquilla
Stack tecnologico
c, rust
Ambito
api, cryptography

Direzione di ricerca

Inizia con le definizioni di digest::Update, digest::FixedOutput, digest::FixedOutputReset, universal_hash::UniversalHash e cipher::StreamCipher, quindi confronta il pattern TryCryptoRng di rand_core. Esamina i percorsi di implementazione di wolfcrypt elencati in digest.rs, hmac.rs, cmac.rs e poly1305.rs per comprendere i fallimenti che vengono nascosti. Il lavoro è completato quando la forma dell’API fallible e la strategia di compatibilità sono concordate per tutti i trait interessati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Repo: RustCrypto/traits
Labels: api-design, digest, hardware

Background

Issue 1 covers infallible constructors. This issue covers infallible runtime
operations
.

The core streaming traits define methods that return ():

  • digest::Update::update(&mut self, data: &[u8])
  • digest::FixedOutput::finalize_into(self, out: &mut Output<Self>)
  • digest::FixedOutputReset::finalize_into_reset(&mut self, out: &mut Output<Self>)
  • universal_hash::UniversalHash::update(&mut self, blocks: &[Block<Self>])

For a pure-Rust software implementation, these are genuinely infallible.
A SHA-256 update call is a few arithmetic operations; it cannot fail.
Making the return type Result<(), E> for software implementations would be
noise — the Err branch is unreachable.

The problem

For a hardware backend, every one of these operations dispatches to a driver,
HSM, or hardware accelerator that can fail at runtime:

Trait Method Return type Hardware failure mode
digest::Update update(&mut self, data: &[u8]) () HSM busy, DMA fault, CryptoCb error
digest::FixedOutput finalize_into(self, out: &mut Output<Self>) () Hardware finalization error
digest::FixedOutputReset finalize_into_reset(&mut self, out: &mut Output<Self>) () Finalization + re-init failure
universal_hash::UniversalHash update(&mut self, blocks) () Hardware MAC block fault
digest::FixedOutput finalize_into (used by Mac via blanket) () Hardware MAC finalization error
cipher::StreamCipher apply_keystream(&mut self, buf: &mut [u8]) () Hardware cipher fault

wolfCrypt's WOLF_CRYPTO_CB mechanism routes each algorithm operation through a
registered C callback. That callback can return WC_NO_ERR_TRACE(CRYPTOCB_UNAVAILABLE)
when the hardware device is busy, or a device-specific error code if the
hardware operation fails. The trait gives us nowhere to put that error.

In our implementation

Discovered while implementing wolfcrypt, a RustCrypto backend wrapping wolfCrypt — a FIPS 140-3 validated C cryptographic library with hardware dispatch via WOLF_CRYPTO_CB.

In every case, we are forced to assert! on the C return code, converting
hardware failures into panics:

Proposed change

Add Try* variants of the affected traits with fallible signatures and blanket
impls for existing software implementations:

pub trait TryUpdate {
    type Error;
    fn try_update(&mut self, data: &[u8]) -> Result<(), Self::Error>;
}

// Blanket impl: all existing Update implementors get TryUpdate for free
impl<T: Update> TryUpdate for T {
    type Error = core::convert::Infallible;
    fn try_update(&mut self, data: &[u8]) -> Result<(), Infallible> {
        self.update(data);
        Ok(())
    }
}

pub trait TryFixedOutput: TryUpdate {
    fn try_finalize_into(self, out: &mut Output<Self>) -> Result<(), Self::Error>;
}

Hardware backends implement TryUpdate and TryFixedOutput directly.
Software implementations get them via the blanket impls at zero cost. The
existing Update and FixedOutput traits are unchanged; no existing code
breaks.

Prior art

rand_core 0.9 already solved this exact problem for the RNG case by adding
TryCryptoRng with a fallible try_fill_bytes method (see companion
Issue 10). We are asking for the same pattern to be applied to digest::Update
and digest::FixedOutput.

The embedded-hal crate uses type Error associated types on all peripheral
traits for the same reason: hardware peripherals can fail at any method call,
not just at construction time.

Lingua principale
Rust
Stelle
756
Fork
256
Merge medio
1h 27m
PR unite (30g)
2

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di RustCrypto/traits

Tutte le issue di RustCrypto/traits

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.