silk.crypto.sm3.

Compute SM3 digests (GB/T 32905-2016), the hash of the Chinese commercial cryptography suite.

When to use#

Use Sm3 where a protocol or format names SM3, such as SM2 signatures or the TLS 1.3 ShangMi suites of RFC 8998. Use SHA-256 elsewhere.

Details#

The streaming state does not allocate. Call make, update, and finish, or use hash. The digest is 32 bytes.

Examples#

Hash a message in parts#

silk
import silk.crypto.sm3 { Sm3 }

pub fn main() -> i32 {
  let mut state = Sm3.make()
  state.update(b"a")
  state.update(b"bc")
  let digest = state.finish()
  // SM3("abc") is 66c7f0f4...a8e0.
  if digest[0] != 0x66 || digest[31] != 0xe0 {
    return 1
  }
  return 0
}

See also#

RFC 8998 and the SM3 draft describe the algorithm.

Import as Sm3 with import silk.crypto.sm3 { Sm3 }.

Public declarations: 1.

Sm3#

silk
pub struct Sm3

A streaming SM3 state that returns a 32-byte digest. It does not allocate.

Implementation Sm3#

silk
impl Sm3

Associated function make#

silk
pub fn make() -> Self

Returns a state for an empty message.

Method update#

silk
pub fn update(self: &mut Self, bytes: &[u8]) -> ()

Adds bytes to the message. The length in bits wraps modulo 2^64.

Method finish#

silk
pub fn finish(self: Self) -> [u8; 32]

Consumes the state and returns its 32-byte digest.

Associated function hash#

silk
pub fn hash(bytes: &[u8]) -> [u8; 32]

Returns the 32-byte SM3 digest of bytes. It does not allocate.

Implementation Digest for Sm3#

silk
impl Digest for Sm3

Operation make#

silk
make: Sm3.make

Returns an SM3 state for an empty message.

Operation update#

silk
update: Sm3.update

Adds bytes to the end of the message.

Operation blockLength#

silk
fn blockLength(self: &Self) -> usize

Returns 64, the SM3 block length in bytes.

Operation digestLength#

silk
fn digestLength(self: &Self) -> usize

Returns 32, the SM3 digest length in bytes.

Operation objectIdentifier#

silk
fn objectIdentifier(self: &Self) -> &[u8]

Returns the content bytes of the SM3 object identifier, 1.2.156.10197.1.401.

Operation copy#

silk
fn copy(self: &Self) -> Self

Returns an independent copy of the state.

Operation finishInto#

silk
fn finishInto(self: Self, output: &mut [u8]) -> ()

Writes the 32-byte SM3 digest to the start of output and consumes the state.

Category: Cryptography.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page