Sign in

    wbgxiaosu/smcrypto/sm4 does not have a README file

    Sm4

    pub struct Sm4 {
    key : Bytes
    }

    SM4 密码器:保存已校验的 16 字节密钥,可反复加解密数据块。

    Sm4::decrypt_block

    fn Sm4::decrypt_block(self : Sm4, block : Bytes) -> Bytes

    解密一个 16 字节块。调用契约同 [Sm4::encrypt_block]。

    Sm4::encrypt_block

    fn Sm4::encrypt_block(self : Sm4, block : Bytes) -> Bytes

    加密一个 16 字节块。调用契约:block 为 16 字节、密钥已校验, 此时上游单块加密不会失败;违反契约视为程序错误。

    Sm4::new

    fn Sm4::new(key : Bytes) -> Result[Sm4, Sm4Error]

    从 16 字节密钥创建 SM4 密码器。密钥长度错误时返回 [InvalidKeyLength]。

    Sm4Error

    pub(all) enum Sm4Error {
    InvalidKeyLength(Int)
    InvalidIvLength(Int)
    InvalidCiphertextLength(Int)
    InvalidPadding
    AuthenticationFailed
    } derive(Eq,
    Debug
    )

    SM4 相关错误类型。

    Sm4Error::equal

    fn Sm4Error::equal(Sm4Error, Sm4Error) -> Bool

    Sm4Error::not_equal

    fn Sm4Error::not_equal(x : Sm4Error, y : Sm4Error) -> Bool

    Sm4Error::to_repr

    Sm4Error::to_string

    fn Sm4Error::to_string(self : Sm4Error) -> String

    将错误转换为可读字符串。

    decrypt_cbc

    fn decrypt_cbc(key : Bytes, iv : Bytes, ciphertext : Bytes) -> Result[Bytes, Sm4Error]

    CBC 模式解密(去 PKCS7 填充,委托上游实现)。iv 必须为 16 字节。

    decrypt_ctr

    fn decrypt_ctr(key : Bytes, counter : Bytes, ciphertext : Bytes) -> Result[Bytes, Sm4Error]

    CTR 模式解密。CTR 模式下解密与加密为同一变换。

    decrypt_ecb

    fn decrypt_ecb(key : Bytes, ciphertext : Bytes) -> Result[Bytes, Sm4Error]

    ECB 模式解密(去 PKCS7 填充,委托上游实现)。

    decrypt_gcm

    fn decrypt_gcm(key : Bytes, iv : Bytes, ciphertext : Bytes, tag : Bytes, aad : Bytes) -> Result[Bytes, Sm4Error]

    GCM 认证解密:先校验 16 字节标签,不匹配返回 [AuthenticationFailed], 校验通过后输出与密文等长的明文。

    encrypt_cbc

    fn encrypt_cbc(key : Bytes, iv : Bytes, plaintext : Bytes) -> Result[Bytes, Sm4Error]

    CBC 模式加密(含 PKCS7 填充,委托上游实现)。iv 必须为 16 字节。

    encrypt_ctr

    fn encrypt_ctr(key : Bytes, counter : Bytes, plaintext : Bytes) -> Result[Bytes, Sm4Error]

    CTR 模式加密。counter 为 16 字节初始计数器,输出长度与明文一致。 CTR 加解密是同一变换(XOR 密钥流),decrypt_ctr 复用本函数。

    encrypt_ecb

    fn encrypt_ecb(key : Bytes, plaintext : Bytes) -> Result[Bytes, Sm4Error]

    ECB 模式加密(含 PKCS7 填充,委托上游实现)。

    encrypt_gcm

    fn encrypt_gcm(key : Bytes, iv : Bytes, plaintext : Bytes, aad : Bytes) -> Result[(Bytes, Bytes), Sm4Error]

    GCM 认证加密。返回 (密文, 16 字节认证标签),密文与明文等长。 aad 为附加认证数据(不加密但参与认证),可为空。