rage
rage copied to clipboard
feat(age): add BIP39 mnemonic support for Identity
Summary
This PR introduces support for BIP39 mnemonics to the age crate, enabling users to backup and restore x25519::Identity using a 24-word phrase. This facilitates deterministic key generation and easier key management.
Changes
age Crate
- Added
Identity::from_secret_bytes: A public method to construct anIdentityfrom a raw 32-byte array.- Note: Added a strong
# Securitysection to the documentation warning users to only provide high-entropy inputs (CSPRNG or KDF derived).
- Note: Added a strong
- Added
bip39Feature:- Introduced an optional dependency on
bip39(v2.0). - Added a
bip39feature flag toCargo.toml.
- Introduced an optional dependency on
- BIP39 Implementation:
- Implemented
Identity::to_mnemonic(&self) -> Mnemonic: Serializes the secret key into a 24-word English mnemonic. - Implemented
Identity::from_mnemonic(&Mnemonic) -> Result<Self, &'static str>: Restores an identity from a mnemonic. - Security: Intermediate entropy vectors are explicitly zeroized using the
Zeroizetrait before being dropped.
- Implemented
Testing
- Added unit tests for the new functionality:
identity_from_secret_bytes: Verifies raw byte deserialization.mnemonic_round_trip: Verifies that an identity can be converted to a mnemonic and restored back correctly (guarded bybip39feature).invalid_mnemonic_length: Ensures that mnemonics with insufficient entropy (e.g., 12 words/128 bits) are rejected safely.
Verification
Run the tests including the new feature:
cargo test --package age --lib --features bip39