MyNixOS website logo
Description

ShangMi(SM) Cryptographic Algorithms(SM2/SM3/SM4).

Bindings to 'smcrypto' <https://github.com/zhuobie/smcrypto>: a 'Rust' implementation of China's Standards of Encryption Algorithms, which is usually called 'ShangMi(SM)' algorithms. It contains 'SM3' message digest algorithm, 'SM2' asymmetric encryption algorithm and 'SM4' symmetric encryption algorithm. Users can do message hash, encrypt/decrypt, sign/verify, key exchange and more.

smcrypto

A rust implementation of China's standards of encryption algorithms(SM2/SM3/SM4).

Quick Start

SM3

let hash = sm3::sm3_hash(b"abc");
assert_eq!(hash, "66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0");

SM2 Key Generate

Note that the public key is in hexadecimal format and does not contain the "04" prefix.

let (sk, pk) = sm2::gen_keypair();

SM2 Sign/Verify

let sign_ctx = sm2::Sign::new(&sk);
let sign = sign_ctx.sign(b"abc");
let verify_ctx = sm2::Verify::new(&pk);
let verify = verify_ctx.verify(b"abc", &sign);
assert_eq!(verify, true);

SM2 Encrypt/Decrypt

let enc_ctx = sm2::Encrypt::new(&pk);
let enc = enc_ctx.encrypt(b"abc");
let dec_ctx = sm2::Decrypt::new(&sk);
let dec = dec_ctx.decrypt(&enc);
assert_eq!(String::from_utf8(dec).unwrap(), "abc");

SM2 Key Exchange

// Step 1
// a side
let ida = b"[email protected]";
let (ska, _) = sm2::gen_keypair();
let keyexchange_a = sm2::KeyExchange::new(ida, &ska);
let (a2b, rska) = keyexchange_a.keyexchange_1ab(16);
// b side
let idb = b"[email protected]";
let (skb, _) = sm2::gen_keypair();
let keyexchange_b = sm2::KeyExchange::new(idb, &skb);
let (b2a, rskb) = keyexchange_b.keyexchange_1ab(16);
// Step 2
// a side
let ka = keyexchange_a.keyexchange_2a(&rska, &b2a);
// b side
let kb = keyexchange_b.keyexchange_2b(&rskb, &a2b);
// Step 3
assert_eq!(ka.k, kb.k);
assert_eq!(ka.s12, kb.s12);

SM4 ECB Encrypt/Decrypt

let key = b"1234567812345678";
let sm4_ecb = sm4::CryptSM4ECB::new(key);
let enc_ecb = sm4_ecb.encrypt_ecb(b"abc");
let dec_ecb = sm4_ecb.decrypt_ecb(&enc_ecb);
assert_eq!(String::from_utf8(dec_ecb).unwrap(), "abc");

SM4 CBC Encrypt/Decrypt

let key = b"1234567812345678";
let iv = b"0000000000000000";
let sm4_cbc = sm4::CryptSM4CBC::new(key, iv);
let enc_cbc = sm4_cbc.encrypt_cbc(b"abc");
let dec_cbc = sm4_cbc.decrypt_cbc(&enc_cbc);
assert_eq!(String::from_utf8(dec_cbc).unwrap(), "abc");
Metadata

Version

0.1.2

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows