Skip to content

Commit

Permalink
sm2pke -- encryption and decryption
Browse files Browse the repository at this point in the history
  • Loading branch information
heliannuuthus committed Aug 12, 2024
1 parent 1935596 commit 640adca
Show file tree
Hide file tree
Showing 9 changed files with 690 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

7 changes: 4 additions & 3 deletions sm2/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sm2"
version = "0.14.0-pre"
version = "0.14.1-pre"
description = """
Pure Rust implementation of the SM2 elliptic curve as defined in the Chinese
national standard GM/T 0003-2012 as well as ISO/IEC 14888. Includes support for
Expand All @@ -13,7 +13,7 @@ homepage = "https://github.com/RustCrypto/elliptic-curves/tree/master/sm2"
repository = "https://github.com/RustCrypto/elliptic-curves"
readme = "README.md"
categories = ["cryptography", "no-std"]
keywords = ["crypto", "ecc", "shangmi", "signature"]
keywords = ["crypto", "ecc", "shangmi", "signature", "encryption"]
edition = "2021"
rust-version = "1.73"

Expand All @@ -33,13 +33,14 @@ proptest = "1"
rand_core = { version = "0.6", features = ["getrandom"] }

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

arithmetic = ["dep:primeorder", "elliptic-curve/arithmetic"]
bits = ["arithmetic", "elliptic-curve/bits"]
dsa = ["arithmetic", "dep:rfc6979", "dep:signature", "dep:sm3"]
pke = ["arithmetic", "dep:rfc6979", "dep:sm3"]
getrandom = ["rand_core/getrandom"]
pem = ["elliptic-curve/pem", "pkcs8"]
pkcs8 = ["elliptic-curve/pkcs8"]
Expand Down
2 changes: 1 addition & 1 deletion sm2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ The SM2 cryptosystem is composed of three distinct algorithms:

- [x] **SM2DSA**: digital signature algorithm defined in [GBT.32918.2-2016], [ISO.IEC.14888-3] (SM2-2)
- [ ] **SM2KEP**: key exchange protocol defined in [GBT.32918.3-2016] (SM2-3)
- [ ] **SM2PKE**: public key encryption algorithm defined in [GBT.32918.4-2016] (SM2-4)
- [x] **SM2PKE**: public key encryption algorithm defined in [GBT.32918.4-2016] (SM2-4)

## Minimum Supported Rust Version

Expand Down
2 changes: 1 addition & 1 deletion sm2/src/arithmetic/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ use core::{
iter::{Product, Sum},
ops::{AddAssign, MulAssign, Neg, SubAssign},
};
use elliptic_curve::ops::Invert;
use elliptic_curve::{
bigint::Limb,
ff::PrimeField,
ops::Invert,
subtle::{Choice, ConstantTimeEq, CtOption},
};

Expand Down
3 changes: 3 additions & 0 deletions sm2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ extern crate alloc;
#[cfg(feature = "dsa")]
pub mod dsa;

#[cfg(feature = "pke")]
pub mod pke;

#[cfg(feature = "arithmetic")]
mod arithmetic;
#[cfg(feature = "dsa")]
Expand Down
175 changes: 175 additions & 0 deletions sm2/src/pke.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
//! SM2 Encryption Algorithm (SM2) as defined in [draft-shen-sm2-ecdsa § 5].
//!
//! ## Usage
//!
//! NOTE: requires the `sm3` crate for digest functions and the `primeorder` crate for prime field operations.
//!
//! The `DecryptingKey` struct is used for decrypting messages that were encrypted using the SM2 encryption algorithm.
//! It is initialized with a `SecretKey` or a non-zero scalar value and can decrypt ciphertexts using the specified decryption mode.
#![cfg_attr(feature = "std", doc = "```")]
#![cfg_attr(not(feature = "std"), doc = "```ignore")]
//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
//! use rand_core::OsRng; // requires 'getrandom` feature
//! use sm2::{
//! pke::{EncryptingKey, Mode},
//! {SecretKey, PublicKey}
//!
//! };
//!
//! // Encrypting
//! let secret_key = SecretKey::random(&mut OsRng); // serialize with `::to_bytes()`
//! let encrypting_key = EncryptingKey::new_with_mode(secret_key, Mode::C1C2C3);
//! let plaintext = b"plaintext";
//! let ciphertext = encrypting_key.encrypt(plaintext)?;
//!
//! use sm2::pke::DecryptingKey;
//! // Decrypting
//! let decrypting_key = DecryptingKey::new_with_mode(secret_key, Mode::C1C2C3);
//! assert_eq!(decrypting_key.decrypt(&ciphertext)?, plaintext);
//!
//! // Encrypting asn.1
//! let ciphertext = encrypting_key.encrypt_asna1(plaintext)?;
//!
//! // Decrypting asn.1
//! assert_eq!(decrypting_key.decrypt_asna1(&ciphertext)?, plaintext);
//! # }
//! ```
//!
//!
//!

use core::cmp::min;

use crate::AffinePoint;

#[cfg(feature = "alloc")]
use alloc::vec;

use elliptic_curve::{
bigint::{Encoding, Uint, U256},
pkcs8::der::{
asn1::UintRef, Decode, DecodeValue, Encode, Length, Reader, Sequence, Tag, Writer,
},
};

use elliptic_curve::{
pkcs8::der::{asn1::OctetStringRef, EncodeValue},
sec1::ToEncodedPoint,
Result,
};
use sm3::digest::DynDigest;

#[cfg(feature = "arithmetic")]
mod decrypting;
#[cfg(feature = "arithmetic")]
mod encrypting;

#[cfg(feature = "arithmetic")]
pub use self::{decrypting::DecryptingKey, encrypting::EncryptingKey};

/// Modes for the cipher encoding/decoding.
#[derive(Clone, Copy, Debug)]
pub enum Mode {
/// old mode
C1C2C3,
/// new mode
C1C3C2,
}
/// Represents a cipher structure containing encryption-related data (asn.1 format).
///
/// The `Cipher` structure includes the coordinates of the elliptic curve point (`x`, `y`),
/// the digest of the message, and the encrypted cipher text.
pub struct Cipher<'a> {
x: U256,
y: U256,
digest: &'a [u8],
cipher: &'a [u8],
}

impl<'a> Sequence<'a> for Cipher<'a> {}

impl<'a> EncodeValue for Cipher<'a> {
fn value_len(&self) -> elliptic_curve::pkcs8::der::Result<Length> {
UintRef::new(&self.x.to_be_bytes())?.encoded_len()?
+ UintRef::new(&self.y.to_be_bytes())?.encoded_len()?
+ OctetStringRef::new(&self.digest)?.encoded_len()?
+ OctetStringRef::new(&self.cipher)?.encoded_len()?
}

fn encode_value(&self, writer: &mut impl Writer) -> elliptic_curve::pkcs8::der::Result<()> {
UintRef::new(&self.x.to_be_bytes())?.encode(writer)?;
UintRef::new(&self.y.to_be_bytes())?.encode(writer)?;
OctetStringRef::new(&self.digest)?.encode(writer)?;
OctetStringRef::new(&self.cipher)?.encode(writer)?;
Ok(())
}
}

impl<'a> DecodeValue<'a> for Cipher<'a> {
type Error = elliptic_curve::pkcs8::der::Error;

fn decode_value<R: Reader<'a>>(
decoder: &mut R,
header: elliptic_curve::pkcs8::der::Header,
) -> core::result::Result<Self, Self::Error> {
decoder.read_nested(header.length, |nr| {
let x = UintRef::decode(nr)?.as_bytes();
let y = UintRef::decode(nr)?.as_bytes();
let digest = OctetStringRef::decode(nr)?.into();
let cipher = OctetStringRef::decode(nr)?.into();
Ok(Cipher {
x: Uint::from_be_bytes(zero_byte_slice(x)?),
y: Uint::from_be_bytes(zero_byte_slice(y)?),
digest,
cipher,
})
})
}
}

/// Performs key derivation using a hash function and elliptic curve point.
fn kdf(hasher: &mut dyn DynDigest, kpb: AffinePoint, c2: &mut [u8]) -> Result<()> {
let klen = c2.len();
let mut ct: i32 = 0x00000001;
let mut offset = 0;
let digest_size = hasher.output_size();
let mut ha = vec![0u8; digest_size];
let encode_point = kpb.to_encoded_point(false);

while offset < klen {
hasher.update(encode_point.x().unwrap());
hasher.update(encode_point.y().unwrap());
hasher.update(&ct.to_be_bytes());

hasher
.finalize_into_reset(&mut ha)
.map_err(|_e| elliptic_curve::Error)?;

let xor_len = min(digest_size, klen - offset);
xor(c2, &ha, offset, xor_len);
offset += xor_len;
ct += 1;
}
Ok(())
}

/// XORs a portion of the buffer `c2` with a hash value.
fn xor(c2: &mut [u8], ha: &[u8], offset: usize, xor_len: usize) {
for i in 0..xor_len {
c2[offset + i] ^= ha[i];
}
}

/// Converts a byte slice to a fixed-size array, padding with leading zeroes if necessary.
pub(crate) fn zero_byte_slice<const N: usize>(
bytes: &[u8],
) -> elliptic_curve::pkcs8::der::Result<[u8; N]> {
let num_zeroes = N
.checked_sub(bytes.len())
.ok_or_else(|| Tag::Integer.length_error())?;

// Copy input into `N`-sized output buffer with leading zeroes
let mut output = [0u8; N];
output[num_zeroes..].copy_from_slice(bytes);
Ok(output)
}
Loading

0 comments on commit 640adca

Please sign in to comment.