Skip to content

Conversation

@rusty1968
Copy link
Collaborator

Add Symmetric Cipher Support to OpenPRoT HAL

Summary

Introduces symmetric cipher capabilities with HAL traits and RustCrypto reference implementation.

Changes

HAL Cipher Traits

  • Core cipher traits: SymmetricCipher, CipherInit, CipherOp, CipherStatus
  • AEAD support: AeadCipherOp for authenticated encryption
  • Utilities: BlockAligned container, cipher mode markers
  • Zero-copy serialization via zerocopy, comprehensive error handling

RustCrypto reference Implementation

  • AES-256-CTR using RustCrypto (aes v0.8.4, ctr v0.9.2)
  • Single-use contexts, NIST test vectors, no_std compatible
  • Reference implementation for future hardware integrations

Add cipher traits for the OpenPRoT blocking HAL:
- SymmetricCipher: Type definitions for keys, nonces, plaintext, ciphertext
- CipherInit: Initialize cipher contexts with keys and modes
- CipherOp: Encrypt/decrypt operations with error handling
- CipherStatus: Hardware status monitoring (ready, idle, output available)
- CipherMode markers: BlockCipherMode, AeadCipherMode, StreamCipherMode
- SecureCipherOp: State cleanup and zeroization
- BlockAligned: Container for block-aligned data
- AeadCipherOp: Authenticated encryption with associated data

Features:
- Zero-copy serialization via zerocopy crate
- Error mapping with ErrorKind enum
- Support for software and hardware implementations

Provides foundation for cipher implementations across OpenPRoT.
Add AES-256-CTR cipher implementation using RustCrypto as reference
implementation of the HAL cipher traits.

Implementation:
- AES-256-CTR using aes v0.8.4 and ctr v0.9.2 crates
- Single-use contexts for security
- Test suite with NIST test vectors
- no_std compatibility for embedded systems
- Zero-copy operations

Dependencies:
- aes = "0.8.4" (AES block cipher)
- ctr = "0.9.2" (CTR mode)
- cipher = "0.4.4" (Cipher traits)
- generic-array = "0.14.7" (Fixed arrays)

Serves as both functional cipher and reference for implementing
HAL cipher traits with other libraries or hardware accelerators.
Replace Vec<u8> suggestions with no_std alternatives like [u8; N]
and custom containers. Removes heap allocation references from
embedded systems documentation.
Replace problematic intra-doc links with code formatting
to resolve rustdoc warnings in digest and i2c_device modules.
The buffer module provided FixedPlainText and FixedCipherText wrapper types
but was not used by the current AES-CTR cipher implementation, which uses
simple [u8; 256] arrays directly. Removing it simplifies the crate and
follows YAGNI principles.

- Remove buffer module declaration from lib.rs
- Remove buffer type re-exports (BufferError, FixedCipherText, etc.)
- Delete unused buffer.rs file

All cipher tests continue to pass with this simplification.
Add #[allow(clippy::unwrap_used)] attribute to the cipher test module to
permit unwrap() calls in test code while maintaining strict safety in
production code.

This follows Rust best practices where:
- Production code remains panic-free (no unwrap/expect/panic)
- Test code can use unwrap() for fail-fast behavior and cleaner assertions
- Tests are meant to panic immediately when assumptions are violated

All clippy warnings about unwrap usage are now resolved while preserving
test readability and maintaining security guidelines for production code.
Replace all direct array indexing with safe .get() and .get_mut() methods
in the BlockAligned container implementation to prevent potential panics
and meet strict security requirements.

**Production code fixes:**
- from_slice_padded(): Use get_mut(i) instead of blocks[i]
- push_block(): Use get_mut(block_count) instead of blocks[block_count]
- get_block(): Use get(index) instead of &blocks[index]

**Test code fixes:**
- Replace blocks[0] and blocks[1] with safe .get() calls
- Replace third_block[0] with safe .get() access

**Security improvements:**
- Zero panic risk: All array access now bounds-checked
- Proper error handling: Failed access returns errors instead of panicking
- Compliance: Follows security guidelines forbidding direct indexing

All tests pass and clippy indexing warnings are eliminated while
maintaining full functionality and performance.
Add backticks around type references to prevent rustdoc from interpreting
them as HTML tags:
- `Option<u8>` instead of Option<u8>
- `Option<enum>` instead of Option<enum>

This eliminates the rustdoc warnings:
- warning: unclosed HTML tag `u8`
- warning: unclosed HTML tag `enum`

The type references are now properly formatted as code in the generated
documentation.
Address clippy warnings for safer and more idiomatic code:

**get_first warnings:**
- Replace `.get(0)` with `.first()` for accessing first array elements
- More expressive and idiomatic Rust code

**arithmetic_side_effects warnings:**
- Replace `+=` with `.saturating_add()` for safe increment
- Replace `*` with `.saturating_mul()` for safe multiplication
- Prevents potential overflow in arithmetic operations

**Security improvements:**
- Arithmetic operations now cannot overflow/panic
- Follows security guidelines for overflow-safe operations
- All tests continue to pass with improved safety

These changes align with the project's strict safety requirements while
maintaining full functionality and performance.
Add #[allow(clippy::unwrap_used)] attribute to the RustCrypto cipher test
module to permit unwrap() calls in test code while maintaining strict
safety in production code.

This matches the pattern established in the HAL cipher tests where:
- Production code remains panic-free (no unwrap/expect/panic)
- Test code can use unwrap() for fail-fast behavior and cleaner assertions
- Tests are meant to panic immediately when assumptions are violated

Resolves clippy warnings about unwrap usage in the comprehensive AES-CTR
test suite while preserving test readability and maintaining security
guidelines for production code.
Apply cargo fmt formatting to correct comment spacing from double space
to single space after // in the clippy allow attribute:

- `#[allow(clippy::unwrap_used)]  // Allow unwrap...`
+ `#[allow(clippy::unwrap_used)] // Allow unwrap...`

This ensures consistent formatting according to Rust style guidelines
and passes `cargo fmt --check` validation.
@FerralCoder FerralCoder merged commit c53eddb into OpenPRoT:main Oct 15, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants