Skip to content
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

sm2: Fix heap allocation #1099

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions sm2/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,30 @@ edition = "2024"
rust-version = "1.85"

[dependencies]
elliptic-curve = { version = "0.14.0-rc.0", default-features = false, features = ["sec1"] }
elliptic-curve = { version = "0.14.0-rc.0", default-features = false, features = [
"sec1",
] }
rand_core = { version = "0.9", default-features = false }

# optional dependencies
primeorder = { version = "=0.14.0-pre.2", optional = true, path = "../primeorder" }
rfc6979 = { version = "=0.5.0-pre.4", optional = true }
serdect = { version = "0.3", optional = true, default-features = false }
signature = { version = "=2.3.0-pre.6", optional = true, features = ["rand_core"] }
signature = { version = "=2.3.0-pre.6", optional = true, features = [
"rand_core",
"digest",
] }
sm3 = { version = "=0.5.0-pre.5", optional = true, default-features = false }

[dev-dependencies]
hex-literal = "1"
proptest = "1"
rand_core = { version = "0.9", features = ["os_rng"] }
rand = "0.9"

[features]
default = ["arithmetic", "dsa", "pke", "pem", "std"]
alloc = ["elliptic-curve/alloc"]
default = ["arithmetic", "dsa", "pke", "pem"]
alloc = ["elliptic-curve/alloc", "signature?/alloc", "elliptic-curve/alloc"]
std = ["alloc", "elliptic-curve/std", "signature?/std"]

arithmetic = ["dep:primeorder", "elliptic-curve/arithmetic"]
Expand Down
7 changes: 3 additions & 4 deletions sm2/src/dsa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ use signature::{Error, Result, SignatureEncoding};
#[cfg(feature = "alloc")]
use alloc::vec::Vec;

#[cfg(feature = "pkcs8")]
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
use crate::pkcs8::{
AlgorithmIdentifierRef, ObjectIdentifier, der::AnyRef, spki::AssociatedAlgorithmIdentifier,
AlgorithmIdentifierRef, ObjectIdentifier, der, der::AnyRef,
spki::AssociatedAlgorithmIdentifier, spki::SignatureBitStringEncoding,
};
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
use crate::pkcs8::{der, spki::SignatureBitStringEncoding};

/// SM2DSA signature serialized as bytes.
pub type SignatureBytes = [u8; Signature::BYTE_SIZE];
Expand Down
4 changes: 2 additions & 2 deletions sm2/src/dsa/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use signature::{
};
use sm3::Sm3;

#[cfg(feature = "pkcs8")]
#[cfg(all(feature = "pkcs8", feature = "alloc"))]
use crate::pkcs8::{
der::AnyRef,
spki::{AlgorithmIdentifier, AssociatedAlgorithmIdentifier, SignatureAlgorithmIdentifier},
Expand Down Expand Up @@ -230,7 +230,7 @@ fn sign_prehash_rfc6979(secret_scalar: &Scalar, prehash: &[u8], data: &[u8]) ->
Signature::from_scalars(r, s)
}

#[cfg(feature = "pkcs8")]
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
impl SignatureAlgorithmIdentifier for SigningKey {
type Params = AnyRef<'static>;

Expand Down
6 changes: 3 additions & 3 deletions sm2/src/dsa/verifying.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ use sm3::{Sm3, digest::Digest};
use alloc::{boxed::Box, string::String};

#[cfg(all(feature = "alloc", feature = "pkcs8"))]
use crate::pkcs8::{self, EncodePublicKey, spki};
#[cfg(feature = "pkcs8")]
use crate::pkcs8::{
self, EncodePublicKey,
der::AnyRef,
spki,
spki::{AlgorithmIdentifier, AssociatedAlgorithmIdentifier, SignatureAlgorithmIdentifier},
};

Expand Down Expand Up @@ -217,7 +217,7 @@ impl EncodePublicKey for VerifyingKey {
}
}

#[cfg(feature = "pkcs8")]
#[cfg(all(feature = "alloc", feature = "pkcs8"))]
impl SignatureAlgorithmIdentifier for VerifyingKey {
type Params = AnyRef<'static>;

Expand Down
Loading