-
Notifications
You must be signed in to change notification settings - Fork 950
Add support for MLDSA in hsmtool #29202
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
willyzha
wants to merge
10
commits into
lowRISC:master
Choose a base branch
from
willyzha:mldsa-hsmtool-rebased
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,744
−168
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cbedf7a
build(rust): Bump cryptoki to 0.12.0
7ceb7c3
[hsmtool] Add MLDSA support
b97669f
[hsmtool] Configure domain for MLDSA operations
b2d30eb
[hsmtool] Fix rust format
3ccc8a9
[hsmtool] Use ml_dsa crate for key MLDSA encoding and decoding
6e782dd
[hsmtool] Update PKCS#11 constants and attributes for MLDSA
2ce5400
[hsmtool] Use enum type for mldsa generate alg input
5b5bd98
[hsmtool] Refactor MLDSA key logic to util/key/mldsa
989e452
[hsmtool] rename mldsa generate to generate-keypair and add docs
1d35cc6
[hsmtool] Run rustfmt on mldsa command and util
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright lowRISC contributors (OpenTitan project). | ||
| // Licensed under the Apache License, Version 2.0, see LICENSE for details. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use anyhow::{Result, anyhow}; | ||
| use cryptoki::object::{Attribute, ObjectHandle}; | ||
| use cryptoki::session::Session; | ||
| use serde::{Deserialize, Serialize}; | ||
| use std::any::Any; | ||
| use std::fs; | ||
| use std::path::PathBuf; | ||
|
|
||
| use crate::commands::{BasicResult, Dispatch}; | ||
| use crate::error::HsmError; | ||
| use crate::module::Module; | ||
| use crate::util::attribute::{AttributeMap, KeyType, ObjectClass}; | ||
| use crate::util::helper; | ||
| use crate::util::key::KeyEncoding; | ||
| use crate::util::key::mldsa; | ||
| use crate::util::wrap::{Wrap, WrapPrivateKey}; | ||
|
|
||
| #[derive(clap::Args, Debug, Serialize, Deserialize)] | ||
| pub struct Export { | ||
| /// Unique identifier of the key. | ||
| #[arg(long)] | ||
| id: Option<String>, | ||
| /// Label of the key. | ||
| #[arg(short, long)] | ||
| label: Option<String>, | ||
| /// Export the private key. | ||
| #[arg(long)] | ||
| private: bool, | ||
| /// Wrap the exported key with a wrapping key. | ||
| #[arg(long)] | ||
| wrap: Option<String>, | ||
| /// Wrapping key mechanism. Required when wrap is specified. | ||
| #[arg(long, default_value = "aes-key-wrap-pad")] | ||
| wrap_mechanism: Option<WrapPrivateKey>, | ||
| /// Encoding format of the exported key. | ||
| #[arg(short, long, value_enum, default_value = "der")] | ||
| format: KeyEncoding, | ||
| /// Path to the file where the key will be saved. | ||
| filename: PathBuf, | ||
| } | ||
|
|
||
| impl Export { | ||
| fn export(&self, session: &Session, object: ObjectHandle) -> Result<()> { | ||
| let map = AttributeMap::from_object(session, object)?; | ||
| if self.private { | ||
| let key = mldsa::MldsaSigningKey::try_from(&map)?; | ||
| mldsa::save_private_key(&self.filename, &key, self.format)?; | ||
| } else { | ||
| let key = mldsa::MldsaVerifyingKey::try_from(&map)?; | ||
| mldsa::save_public_key(&self.filename, &key, self.format)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn wrap_key(&self, session: &Session, object: ObjectHandle) -> Result<()> { | ||
| let wrapper: Wrap = self | ||
| .wrap_mechanism | ||
| .ok_or(anyhow!("wrap_mechanism is required when wrap is specified"))? | ||
| .into(); | ||
| let wrapped = wrapper.wrap(session, object, self.wrap.as_deref())?; | ||
| fs::write(&self.filename, &wrapped)?; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[typetag::serde(name = "mldsa-export")] | ||
| impl Dispatch for Export { | ||
| fn run( | ||
| &self, | ||
| _context: &dyn Any, | ||
| _hsm: &Module, | ||
| session: Option<&Session>, | ||
| ) -> Result<Box<dyn erased_serde::Serialize>> { | ||
| let session = session.ok_or(HsmError::SessionRequired)?; | ||
| let mut attrs = helper::search_spec(self.id.as_deref(), self.label.as_deref())?; | ||
| attrs.push(Attribute::KeyType(KeyType::MlDsa.try_into()?)); | ||
| if self.private { | ||
| attrs.push(Attribute::Class(ObjectClass::PrivateKey.try_into()?)); | ||
| } else { | ||
| attrs.push(Attribute::Class(ObjectClass::PublicKey.try_into()?)); | ||
| } | ||
| let object = helper::find_one_object(session, &attrs)?; | ||
|
|
||
| if self.wrap.is_some() { | ||
| self.wrap_key(session, object)?; | ||
| } else { | ||
| self.export(session, object)?; | ||
| } | ||
| Ok(Box::<BasicResult>::default()) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you provide doc comments for each parameter?