Skip to content

Commit

Permalink
update versiong to 0.2.5
Browse files Browse the repository at this point in the history
add encrypt2() and decrypt2(), use the content of a pem file as  parameter
  • Loading branch information
CadenzaYu committed Jul 13, 2024
1 parent 36681b8 commit 1f08611
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "sm9"
version = "0.2.4"
version = "0.2.5"
edition = "2021"
authors = ["John Yu <cb8866@sina.com>"]
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/"
Expand All @@ -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"

Expand Down
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.)
Expand All @@ -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
Expand All @@ -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

Expand Down
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
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
Expand All @@ -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<u8>) -> Option<Vec<u8>> {
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
Expand All @@ -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!");
Expand Down
20 changes: 20 additions & 0 deletions tests/encryption.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sm9::*;
use std::fs;

#[test]
fn test_encrypt_decrypt() {
Expand All @@ -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());
}

0 comments on commit 1f08611

Please sign in to comment.