Skip to content

Commit 014aa97

Browse files
authored
sm2: unify default Mode for encryption and decryption (#1668)
Previously, EncryptingKey and DecryptingKey used inconsistent default modes, C1C2C3 for EncryptingKey vs C1C3C2 for DecryptingKey. Introduced DEFAULT_MODE to standardize the default behavior across both structures.
1 parent f2fbb60 commit 014aa97

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

sm2/src/pke.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ pub enum Mode {
7575
/// new mode
7676
C1C3C2,
7777
}
78+
79+
/// The standard data layout used for SM2 encryption/decryption.
80+
/// C1C3C2 is the default per modern specifications.
81+
const DEFAULT_MODE: Mode = Mode::C1C3C2;
7882
/// Represents a cipher structure containing encryption-related data (asn.1 format).
7983
///
8084
/// The `Cipher` structure includes the coordinates of the elliptic curve point (`x`, `y`),

sm2/src/pke/decrypting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use primeorder::PrimeField;
1818

1919
use sm3::{Digest, Sm3, digest::DynDigest};
2020

21-
use super::{Cipher, Mode, encrypting::EncryptingKey, kdf, vec};
21+
use super::{Cipher, DEFAULT_MODE, Mode, encrypting::EncryptingKey, kdf, vec};
2222
/// Represents a decryption key used for decrypting messages using elliptic curve cryptography.
2323
#[derive(Clone)]
2424
pub struct DecryptingKey {
@@ -30,7 +30,7 @@ pub struct DecryptingKey {
3030
impl DecryptingKey {
3131
/// Creates a new `DecryptingKey` from a `SecretKey` with the default decryption mode (`C1C3C2`).
3232
pub fn new(secret_key: SecretKey) -> Self {
33-
Self::new_with_mode(secret_key.to_nonzero_scalar(), Mode::C1C3C2)
33+
Self::new_with_mode(secret_key.to_nonzero_scalar(), DEFAULT_MODE)
3434
}
3535

3636
/// Creates a new `DecryptingKey` from a non-zero scalar and sets the decryption mode.
@@ -59,7 +59,7 @@ impl DecryptingKey {
5959

6060
/// Create a signing key from a non-zero scalar.
6161
pub fn from_nonzero_scalar(secret_scalar: NonZeroScalar) -> Result<Self> {
62-
Ok(Self::new_with_mode(secret_scalar, Mode::C1C3C2))
62+
Ok(Self::new_with_mode(secret_scalar, DEFAULT_MODE))
6363
}
6464

6565
/// Serialize as bytes.

sm2/src/pke/encrypting.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use sm3::{
2222
digest::{Digest, DynDigest},
2323
};
2424

25-
use super::{Cipher, Mode};
25+
use super::{Cipher, DEFAULT_MODE, Mode};
2626
/// Represents an encryption key used for encrypting messages using elliptic curve cryptography.
2727
#[derive(Clone, Debug)]
2828
pub struct EncryptingKey {
@@ -31,9 +31,9 @@ pub struct EncryptingKey {
3131
}
3232

3333
impl EncryptingKey {
34-
/// Initialize [`EncryptingKey`] from PublicKey
34+
/// Initialize [`EncryptingKey`] from PublicKey with the default encryption mode (`C1C3C2`).
3535
pub fn new(public_key: PublicKey) -> Self {
36-
Self::new_with_mode(public_key, Mode::C1C2C3)
36+
Self::new_with_mode(public_key, DEFAULT_MODE)
3737
}
3838

3939
/// Initialize [`EncryptingKey`] from PublicKey and set Encryption mode

0 commit comments

Comments
 (0)