From 1f08611a854aa6a96097d163f61cce61dc36ea55 Mon Sep 17 00:00:00 2001 From: CadenzaYu Date: Sat, 13 Jul 2024 10:24:01 +0800 Subject: [PATCH] update versiong to 0.2.5 add encrypt2() and decrypt2(), use the content of a pem file as parameter --- Cargo.toml | 6 +++--- README.md | 31 +++++++++++++++++++++++++++++-- src/lib.rs | 16 +++++++++++++++- tests/encryption.rs | 20 ++++++++++++++++++++ 4 files changed, 67 insertions(+), 6 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index bc3b9ac..1abc68f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "sm9" -version = "0.2.4" +version = "0.2.5" edition = "2021" authors = ["John Yu "] -description = "SM9, identity-based cryptography" +description = "SM9 (OSCCA GM/T 0044-2016), identity-based cryptography" keywords = ["SM9", "pairing", "crypto", "cryptography", "shangmi"] license = "MIT OR Apache-2.0" documentation = "https://docs.rs/sm9/" @@ -18,7 +18,7 @@ sec1 = { version = "0.7.3", features = ["alloc", "pem", "std"] } sm3 = "0.4.2" hmac = "0.12.1" signature = "2.2.0" -sm9_core = "0.3.4" +sm9_core = "0.3.6" hex-literal = "0.4" kem = "0.2.0" diff --git a/README.md b/README.md index 25d0317..bc83eb1 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Add the `sm9` crate to your dependencies in `Cargo.toml` ```toml [dependencies] -sm9 = "0.2.3" +sm9 = "0.2.5" ``` ### Examples @@ -27,6 +27,20 @@ sm9 = "0.2.3" println!("{:02X?}", msg); assert_eq!(msg.len(), txt.len()); assert_eq!(txt, msg.as_slice()); + + use std::fs; + let master_public_key = + fs::read_to_string("master_public_key.pem").expect("read master_public_key.pem error"); + let m = Sm9::encrypt2(&master_public_key, usr_id, txt); + println!("{:02X?}", m); + + let bob_private_key = + fs::read_to_string("bob_private_key.pem").expect("read bob_private_key.pem error"); + let msg = Sm9::decrypt2(&bob_private_key, usr_id, m).expect("decrypt error"); + println!("{:02X?}", msg); + assert_eq!(msg.len(), txt.len()); + assert_eq!(txt, msg.as_slice()); + ``` (See `signature.rs` for the full example.) @@ -50,6 +64,19 @@ sm9 = "0.2.3" m, &sig )); + use std::fs; + let master_signature_public_key = fs::read_to_string("master_signature_public_key.pem") + .expect("read master_signature_public_key.pem error"); + let alice_signature_private_key = fs::read_to_string("alice_signature_private_key.pem") + .expect("read alice_signature_private_key.pem error"); + let sig = Sm9::sign2( + &master_signature_public_key, + &alice_signature_private_key, + m, + ); + + assert!(Sm9::verify2(&master_signature_public_key, user_id, m, &sig)); + ``` ## License @@ -61,7 +88,7 @@ Licensed under either of at your option. -Copyright 2023 [John-Yu](https://github.com/John-Yu). +Copyright 2024 [John-Yu](https://github.com/John-Yu). ### Authors diff --git a/src/lib.rs b/src/lib.rs index a62350e..6f1a0fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,6 +193,13 @@ impl Sm9 { assert!(mpk.is_ok()); mpk.encrypt(usr_id, txt) } + // issues#1, use the content of a pem file as parameter + pub fn encrypt2(master_public_key_pem: &str, usr_id: &[u8], txt: &[u8]) -> Vec { + let mpk = MasterPublicKey::from_pem(master_public_key_pem) + .expect("from_pem master_public_key_pem error"); + assert!(mpk.is_ok()); + mpk.encrypt(usr_id, txt) + } /// decrypt, difined in "SM9 identity-based cryptographic algorithms" /// Part 4: Key encapsulation mechanism and public key encryption algorithm /// 7.2.1 Decryption algorithm @@ -206,6 +213,13 @@ impl Sm9 { assert!(upk.is_ok()); upk.decrypt(usr_id, m) } + // issues#1, use the content of a pem file as parameter + pub fn decrypt2(user_privte_key_pem: &str, usr_id: &[u8], m: Vec) -> Option> { + let upk = UserPrivateKey::from_pem(user_privte_key_pem) + .expect("from_pem user_privte_key_pem error"); + assert!(upk.is_ok()); + upk.decrypt(usr_id, m) + } /// sign, difined in "SM9 identity-based cryptographic algorithms" /// Part 2: Digital signature algorithm /// 6.1 Digital signature generation algorithm @@ -230,7 +244,7 @@ impl Sm9 { m: &[u8], ) -> Signature { let uspk = UserSignaturePrivateKey::from_pem(user_signature_privte_key_pem) - .expect("read UserSignaturePrivateKey error"); + .expect("from_pem UserSignaturePrivateKey error"); assert!(uspk.is_ok()); let mspk = MasterSignaturePublicKey::from_pem(master_signature_public_key_pem) .expect("MasterSignaturePublicKey from_pem error!"); diff --git a/tests/encryption.rs b/tests/encryption.rs index 4599d45..6e89c9f 100644 --- a/tests/encryption.rs +++ b/tests/encryption.rs @@ -1,4 +1,5 @@ use sm9::*; +use std::fs; #[test] fn test_encrypt_decrypt() { @@ -15,3 +16,22 @@ fn test_encrypt_decrypt() { assert_eq!(msg.len(), txt.len()); assert_eq!(txt, msg.as_slice()); } +#[test] +fn test_encrypt_decrypt2() { + // SM9 identity-based cryptographic algorithms + // Part 5: Parameter definition + // Annex D: Example of public key encryption + let usr_id = b"Bob"; + let txt = b"Chinese IBE standard"; + let master_public_key = + fs::read_to_string("master_public_key.pem").expect("read master_public_key.pem error"); + let m = Sm9::encrypt2(&master_public_key, usr_id, txt); + println!("{:02X?}", m); + + let bob_private_key = + fs::read_to_string("bob_private_key.pem").expect("read bob_private_key.pem error"); + let msg = Sm9::decrypt2(&bob_private_key, usr_id, m).expect("decrypt error"); + println!("{:02X?}", msg); + assert_eq!(msg.len(), txt.len()); + assert_eq!(txt, msg.as_slice()); +}