Skip to content

Commit ee5e408

Browse files
authored
crypto-common: add KeyExport trait (#2213)
Adds a companion trait to `KeyInit*` for exporting a key to a byte array sized according to `KeySizeUser`. This is needed when generating e.g. asymmetric secret keys types using the `Generate` trait and it returns an actual key type (e.g. `SigningKey`, `DecryptionKey`, `DecapsulationKey`) as opposed to its serialized byte representation. This PR also shows another use case for it: there's currently a gap in the `kem` crate around serialization where you can generically go from `Decapsulate::encapsulator` to the associated encapsulation key for a KEM, but there is currently no generic way to serialize it. It adds `TryKeyInit` and `KeyExport` bounds to `kem::Encapsulate`, because as the public key component we should always be able to serialize/deserialize it. (NOTE: happy to split the `kem` changes out, that would probably be for the best, but I thought it would help illustrate the use case)
1 parent f57ec3b commit ee5e408

2 files changed

Lines changed: 8 additions & 0 deletions

File tree

crypto-common/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Sealed `BlockSizes` trait implemented for types from `U1` to `U255`
1111
- `Generate` trait ([#2096])
12+
- `KeyExport` trait ([#2213])
1213

1314
### Changed
1415
- `BlockUser::BlockSize` is now bounded by the `BlockSizes` trait
@@ -20,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2021

2122
[#1759]: https://github.com/RustCrypto/traits/pull/1759
2223
[#2096]: https://github.com/RustCrypto/traits/pull/2096
24+
[#2213]: https://github.com/RustCrypto/traits/pull/2213
2325

2426
## 0.1.7 (2025-11-12)
2527
### Changed

crypto-common/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,12 @@ pub trait AlgorithmName {
156156
fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result;
157157
}
158158

159+
/// Serialize a key to a byte array.
160+
pub trait KeyExport: KeySizeUser {
161+
/// Serialize this key as a byte array.
162+
fn to_bytes(&self) -> Key<Self>;
163+
}
164+
159165
/// Types which can be initialized from a key.
160166
pub trait KeyInit: KeySizeUser + Sized {
161167
/// Create new value from fixed size key.

0 commit comments

Comments
 (0)