Sign in

    smcrypto

    基于 2d5rrr333/toolsfx-moonbit 的国密工程化扩展:RFC 8998 符合的 SM4-GCM 认证加密、SM4 CTR、HMAC-SM3(RFC 2104)、PBKDF2-HMAC-SM3(RFC 8018),核心 SM3/SM4/SM2 原语委托 toolsfx-moonbit / Engineering-grade GM extension for toolsfx-moonbit: RFC 8998-conformant SM4-GCM, CTR, HMAC-SM3 and PBKDF2-HMAC-SM3

    sm3
    sm4
    chinese-national-cryptography
    hash
    cipher
    hmac
    kdf
    gcm
    aead
    pbkdf2
    toolsfx
    Download zip
    Author
    Version
    0.4.0
    License
    Apache-2.0
    Last updated
    7 days ago
    Downloads
    23

    #wbgxiaosu/smcrypto

    基于 2d5rrr333/toolsfx-moonbit v0.3.0 的国密工程化扩展:RFC 8998 符合的 SM4-GCM 认证加密、SM4 CTR、HMAC-SM3、PBKDF2-HMAC-SM3。

    核心国密原语(SM3 杂凑、SM4 单块与 ECB/CBC、SM3-KDF)委托上游 toolsfx-moonbit(ISC License,已通过 GB/T 32905/32907 国标向量);本项目专注上游未覆盖或不符合标准的部分。

    #安装

    moon add wbgxiaosu/smcrypto

    #能力清单

    能力来源验证
    SM3 摘要(一次性/流式)委托上游国标向量 + 51 条 gmssl 交叉对拍
    SM4 ECB/CBC(PKCS7)委托上游国标向量 + 24 组交叉对拍 + 往返测试
    SM3-KDF(GB/T 32918.3 5.4.2)委托上游国标向量
    SM4 CTR本项目实现往返测试 + 交叉对拍
    SM4-GCM(SP 800-38D)本项目实现RFC 8998 附录 A.1 密文+标签逐字节对拍 + 篡改检测
    HMAC-SM3(RFC 2104)本项目新增RFC 2104 结构向量 + 篡改检测
    PBKDF2-HMAC-SM3(RFC 8018)本项目新增独立 Python 基准交叉对拍(迭代 1~80000、跨块、长密钥、截断)

    #为什么 GCM 是本项目实现的

    以上游 sm4_gcm_encrypt 对 RFC 8998 附录 A.1 官方向量实测:按 [key, iv, plaintext, aad] 语义调用时密文输出仅 20 字节(输入 64 字节);按其内部校验唯一接受的 [key, iv, aad, plaintext] 序调用,标签为 09976f85...,与 RFC 8998 规定的 83de3541e4c2b58177e065a9bf7b62ec 不一致。本项目 GCM 在上游 SM4 单块原语之上实现完整 SP 800-38D 流程,密文与标签均与 RFC 8998 逐字节一致。

    #快速上手

    // GCM 认证加密(16 字节标签,支持任意长度 IV 与 AAD)
    let (ct, tag) = @smcrypto.sm4_encrypt_gcm(key, iv, plaintext, aad).unwrap()
    // 解密自动校验标签,篡改返回 AuthenticationFailed
    let pt = @smcrypto.sm4_decrypt_gcm(key, iv, ct, tag, aad).unwrap()

    // HMAC-SM3 / PBKDF2-HMAC-SM3
    let mac = @smcrypto.sm3_hmac(key, message)
    let dk = @smcrypto.sm3_pbkdf2(b"password", b"salt", 4096, 32)

    // SM3 / SM4 ECB、CBC、CTR、KDF 亦可经根包直接使用
    let hex = @smcrypto.sm3_hex_digest(b"abc") // 国标示例 1

    #可复现验证

    moon check # 零警告 moon test # 30 组测试(含 RFC 8998、国标、gmssl 交叉对拍) moon run cmd/main # 演示全流程

    测试向量来源:RFC 8998 附录 A(GCM)、GB/T 32905/32907 附录(核心向量)、gen/vectors.json 与 gen/pbkdf2_vectors.json(Python gmssl 基准生成,脚本见 gen/gen_*.py)。

    #交付边界

    不包含 SM2/SM9(上游已提供完整实现);不做侧信道防护;不封装协议层。

    #License

    Apache-2.0(本项目);上游 toolsfx-moonbit 为 ISC License。

    sm3_digest

    fn sm3_digest(data : Bytes) -> Bytes

    计算 SM3 摘要,返回 32 字节。

    sm3_hex_digest

    fn sm3_hex_digest(data : Bytes) -> String

    计算 SM3 摘要,返回小写十六进制字符串。

    sm3_hmac

    fn sm3_hmac(key : Bytes, message : Bytes) -> Bytes

    计算 HMAC-SM3 消息认证码(RFC 2104 结构)。

    sm3_kdf

    fn sm3_kdf(z : Bytes, klen : Int) -> Bytes

    SM3 密钥派生函数 KDF(GB/T 32918.3-2016 5.4.2)。

    sm3_pbkdf2

    fn sm3_pbkdf2(password : Bytes, salt : Bytes, iterations : Int, dklen : Int) -> Bytes

    PBKDF2-HMAC-SM3 口令基密钥派生(RFC 8018)。

    sm4_decrypt_cbc

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

    SM4 CBC 模式解密(去 PKCS7 填充)。

    sm4_decrypt_ctr

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

    SM4 CTR 模式解密(与加密同一变换)。

    sm4_decrypt_ecb

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

    SM4 ECB 模式解密(去 PKCS7 填充)。

    sm4_decrypt_gcm

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

    SM4 GCM 认证解密:先校验标签,不匹配返回认证失败错误。

    sm4_encrypt_cbc

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

    SM4 CBC 模式加密(含 PKCS7 填充)。

    sm4_encrypt_ctr

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

    SM4 CTR 模式加密(无填充,输出与明文等长)。

    sm4_encrypt_ecb

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

    SM4 ECB 模式加密(含 PKCS7 填充)。

    sm4_encrypt_gcm

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

    SM4 GCM 认证加密(SP 800-38D,16 字节认证标签)。 返回 (密文, 标签),密文与明文等长。

    sm4_new

    从 16 字节密钥创建 SM4 密码器。

    Source Files