#[non_exhaustive]pub struct KeyPairGenerator { /* private fields */ }Expand description
Builder for X25519 key-pair generation.
Mirrors the Encryptor builder pattern: pick the passphrase at
construction, optionally override the Argon2id parameters used to
seal the resulting private.key, then call KeyPairGenerator::write
with the destination directory.
The free function generate_key_pair is a thin convenience wrapper
around this builder for callers that do not need to override KDF
parameters.
Implementations§
Source§impl KeyPairGenerator
impl KeyPairGenerator
Sourcepub fn with_passphrase(passphrase: Passphrase) -> Self
pub fn with_passphrase(passphrase: Passphrase) -> Self
Constructs a key-pair generator with the passphrase that will be
used to seal the resulting private.key. The passphrase is
checked against the fixed bound of 1 to 4,096 bytes when
KeyPairGenerator::write runs; constructing this builder is
infallible.
Sourcepub fn kdf_params(self, params: KdfParams) -> Self
pub fn kdf_params(self, params: KdfParams) -> Self
Overrides the Argon2id parameters used to seal private.key.
If unset, the generator uses KdfParams::default (1 GiB memory,
time_cost 4, parallelism 4).
Same production memory floor as Encryptor::kdf_params: a
params.mem_cost below the 19 MiB production memory floor rejects
at KeyPairGenerator::write time with
CryptoError::KdfBelowWriteFloor, so a caller cannot seal a
private.key with weak Argon2id memory; the floor is hard and has
no override.
§Default-decrypt round-trip
kdf_params is also checked at KeyPairGenerator::write time
against the writer’s KdfLimit policy. By default, memory is capped
at 1 GiB and combined work at the writer’s own budget, while time cost
and lanes are capped at the format maximum, so KdfParams::default
is accepted. A value above the effective policy rejects with the
matching typed cap error. To write a private.key with memory above
1 GiB or more total work than KdfParams::default, or to use a
deliberately tightened policy, configure
KeyPairGenerator::kdf_limit and configure the unlocking
PrivateKeyDecryptor with a compatible
PrivateKeyDecryptor::kdf_limit.
Sourcepub fn kdf_limit(self, limit: KdfLimit) -> Self
pub fn kdf_limit(self, limit: KdfLimit) -> Self
Sets the writer-side KDF resource policy for sealing private.key.
The policy caps Argon2id memory cost, time cost, lane count, and
combined work before key generation begins. The default policy accepts
KdfParams::default, which sits exactly at the default work budget,
and rejects memory above 1 GiB unless the caller opts into a higher
memory cap. Time cost and lanes default to the format maximum, so
they only reject when the caller tightens them.
Use this builder together with KeyPairGenerator::kdf_params to raise
the memory or work ceiling, or to tighten any dimension. Raising memory
does not raise the work budget, so parameters above
KdfParams::default’s work need KdfLimit::max_work as well. The
receiving PrivateKeyDecryptor must be configured via
PrivateKeyDecryptor::kdf_limit with a policy that accepts the same
parameters.
Sourcepub fn write(
self,
output_dir: impl AsRef<Path>,
on_event: impl Fn(&ProgressEvent),
) -> Result<KeyGenOutcome, CryptoError>
pub fn write( self, output_dir: impl AsRef<Path>, on_event: impl Fn(&ProgressEvent), ) -> Result<KeyGenOutcome, CryptoError>
Generates an X25519 key pair and writes private.key and public.key
into output_dir, creating the directory and missing parents if needed.
Choose a private directory whose path ancestry untrusted processes
cannot modify. Existing key files are rejected.
Both files are written and synced before either receives its final
name. private.key is committed first, and the output directory is
flushed after each commit. This order prevents process interruption
from leaving a usable public.key without its matching private.key.
A successful return means the supported durability barriers completed. Power-loss durability depends on the filesystem and storage device honoring those barriers. Filesystems without directory flushing provide no such ordering guarantee across power loss.
§Errors
Returns CryptoError::InvalidInput if the passphrase is empty or too
long or either key file already exists. It is also returned when a
committed key file’s path no longer names the file just written.
Complete key files may remain; the error describes the resulting state.
Invalid KDF parameters return CryptoError::InvalidKdfParams,
memory below the writer floor returns CryptoError::KdfBelowWriteFloor,
and costs above KdfLimit return the matching Kdf*CapExceeded variant.
Returns CryptoError::Io for filesystem failures, including a
directory flush failure or a committed key file that carries more than
one filesystem name. If the flush after committing
public.key fails, the method makes a best-effort attempt to remove
public.key but keeps private.key. Removing both without a successful
directory flush could leave only the public key after power loss. A
remaining private.key is safe to delete. An error returned after a
commit can leave one or both complete key files for the caller to
inspect; its message names each key file that is complete. If a
removal cannot be confirmed — the key file was replaced during the
operation, or it still had another name — the error message says so,
and for a key file that had already been committed it first says that
a complete key file was committed.