-
Notifications
You must be signed in to change notification settings - Fork 224
Allow serde
without alloc
#1972
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
datdenkikniet
wants to merge
1
commit into
RustCrypto:master
Choose a base branch
from
datdenkikniet:no-alloc-serde
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.
+31
−2
Open
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -425,6 +425,28 @@ where | |
} | ||
} | ||
|
||
#[cfg(feature = "serde")] | ||
impl<C> PublicKey<C> | ||
where | ||
C: AssociatedOid + CurveArithmetic, | ||
AffinePoint<C>: FromEncodedPoint<C> + ToEncodedPoint<C>, | ||
FieldBytesSize<C>: ModulusSize, | ||
{ | ||
/// Encode this [`PublicKey`] as der bytes, placing the result in `output`. This function | ||
/// returns a slice containing the encoded DER bytes. | ||
fn encode_as_der<'buf>(&self, output: &'buf mut [u8]) -> der::Result<&'buf [u8]> { | ||
let public_key_bytes = self.to_encoded_point(false); | ||
let subject_public_key = der::asn1::BitStringRef::new(0, public_key_bytes.as_bytes())?; | ||
|
||
let spki = pkcs8::SubjectPublicKeyInfo { | ||
algorithm: Self::ALGORITHM_IDENTIFIER, | ||
subject_public_key, | ||
}; | ||
|
||
der::Encode::encode_to_slice(&spki, output) | ||
} | ||
} | ||
|
||
#[cfg(all(feature = "alloc", feature = "pkcs8"))] | ||
impl<C> EncodePublicKey for PublicKey<C> | ||
where | ||
|
@@ -436,6 +458,7 @@ where | |
let public_key_bytes = self.to_encoded_point(false); | ||
let subject_public_key = der::asn1::BitStringRef::new(0, public_key_bytes.as_bytes())?; | ||
|
||
// TODO: use `encode_as_der` here? | ||
pkcs8::SubjectPublicKeyInfo { | ||
algorithm: Self::ALGORITHM_IDENTIFIER, | ||
subject_public_key, | ||
|
@@ -483,7 +506,11 @@ where | |
where | ||
S: ser::Serializer, | ||
{ | ||
let der = self.to_public_key_der().map_err(ser::Error::custom)?; | ||
// TODO: can we determine DER encoding length up-front? Using `MockCurve` gives | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to collect this information from some associated const? Was hard to figure out |
||
// 91 bytes of output, but it feels like that depends on the curve that is being | ||
// used here. | ||
let mut buf = [0u8; 91]; | ||
let der = self.encode_as_der(&mut buf).map_err(ser::Error::custom)?; | ||
serdect::slice::serialize_hex_upper_or_bin(&der, serializer) | ||
} | ||
} | ||
|
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Using
encode_as_der
seemed like a hassle here. Is there a better approach thanencode_as_der
followed directly byDocument::decode().unwrap()
?Also suffers from the encoding length question, and this specific conversion will require
alloc
anyways (becauseder::Document
contains aVec<u8>
)