gmsm

Go ShangMi (Commercial Cryptography) Library

Github CI arm64 sm3-sm4-ni riscv64 loong64 ppc64le s390x codecov Go Report Card Documentation GitHub go.mod Go version (branch) Release

English 简体中文

Go ShangMi (Commercial Cryptography) Library, abbreviated as GMSM, is a secure, high-performance, and easy-to-use Go cryptographic library that covers the Chinese commercial cryptographic public algorithms SM2/SM3/SM4/SM9/ZUC.

User Documentation

If you want to raise an issue, we recommend reading How To Ask Questions The Smart Way.

Core Modules

Public Key Cryptography Algorithms

SM2 - Elliptic Curve Public Key Cryptography Algorithm

The core implementation of SM2 elliptic curve public key cryptography algorithm is located in the internal/sm2ec package. This implementation achieves performance comparable to the native NIST P-256 curve implementation in the Go standard library (non-BoringCrypto) and includes assembly optimizations for amd64, arm64, s390x, ppc64le, riscv64, and loong64 architectures.

Features:

For detailed performance optimization analysis and implementation details, please refer to the SM2 Performance Optimization Wiki.

SM9 - Identity-Based Cryptography Algorithm

The underlying mathematical operations (prime fields, extension fields, elliptic curves, and bilinear pairings) of SM9 identity-based cryptography algorithm are implemented in the bn256 package, with optimizations for amd64, arm64, ppc64x, riscv64, and loong64 architectures.

Features:

For implementation details and optimization strategies, please refer to the SM9 Implementation and Optimization Wiki.


Symmetric Cryptography Algorithms

SM3 - Cryptographic Hash Algorithm

SM3 cryptographic hash algorithm (GM/T 0004-2012) implements efficient SIMD optimizations:

Architecture Optimizations:

For detailed implementation analysis, please refer to the SM3 Performance Optimization Wiki.

SM4 - Block Cipher Algorithm

SM4 block cipher algorithm (GM/T 0002-2012) implements multi-architecture assembly optimizations with fused optimizations for common operation modes:

Architecture Optimizations:

Operation Mode Optimizations:

For detailed implementation analysis, please refer to the SM4 Performance Optimization Wiki.

ZUC - Zu Chongzhi Stream Cipher Algorithm

The Zu Chongzhi stream cipher algorithm (GM/T 0001-2012) implements optimizations based on SIMD, AES instructions, and carry-less multiplication instructions, supporting amd64, arm64, and ppc64x architectures.

Features:

For detailed implementation analysis, please refer to the Efficient Software Implementations of ZUC Wiki.


Message Authentication Codes

CBCMAC - Block Cipher Based Message Authentication Code

Compliant with the standard “GB/T 15852.1-2020 Information Security Technology - Message Authentication Code Algorithm Part 1: Mechanism using a Block Cipher”, implementing various MAC algorithms:

Supported MAC Modes:


Operation Modes and Padding

CIPHER - Block Cipher Operation Modes

Implements various operation modes defined in the standard “GB/T 17964-2021 Information Security Technology - Modes of Operation for Block Ciphers”:

Supported Operation Modes:

Notes:

PADDING - Padding Schemes

Implements various standards-compliant padding schemes with constant-time unpadding to defend against Padding Oracle attacks:

Padding Scheme Corresponding Standard
PKCS#7 GB/T 17964-2021 Appendix C.2 Padding Method 1 / RFC 5652
ISO/IEC 9797-1 Method 2 GB/T 17964-2021 Appendix C.3 Padding Method 2
ANSI X.923 ANSI X9.23 Standard
ISO/IEC 9797-1 Method 3 GB/T 17964-2021 Appendix C.4 Padding Method 3
Zero Padding Non-standard (legacy compatibility)

Secure Implementation:


PKI and Certificates

SMX509 - Chinese Commercial Cryptography Certificate Extension

Extended from the Go standard library crypto/x509 package with added support for Chinese commercial cryptography algorithms, compliant with “GM/T 0015-2012 Digital Certificate Format Specification Based on SM2 Cryptographic Algorithm”.

PKCS#7 - Cryptographic Message Syntax

Extended from the mozilla-services/pkcs7 project (archived in February 2024) with added support for Chinese commercial cryptography algorithms, compliant with RFC 2315 standard.

PKCS#8 - Private-Key Information Syntax

Extended from the youmark/pkcs8 project with added support for Chinese commercial cryptography algorithms, compliant with RFC 5208 / RFC 5958 standards.


Key Agreement and Random Numbers

ECDH - Elliptic Curve Diffie-Hellman

Provides an interface design similar to the Go standard library crypto/ecdh, supporting SM2 curve key agreement protocols:

Supported Protocols:

Features:

DRBG - Deterministic Random Bit Generator

Compliant with “GM/T 0105-2021 Software Random Number Generator Design Guide” and compatible with NIST SP 800-90A partial requirements.

Features:


CFCA Interoperability

CFCA - China Financial Certification Authority Extension

Provides interoperability support with CFCA SADK:

Features:


Post-Quantum Cryptography

MLKEM - Module-Lattice-Based Key-Encapsulation Mechanism

Compliant with NIST FIPS 203 standard, based on the Go standard library implementation.

Supported Parameter Sets:

MLDSA - Module-Lattice-Based Digital Signature

Compliant with NIST FIPS 204 standard.

SLHDSA - Stateless Hash-Based Digital Signature

Compliant with NIST FIPS 205 standard.

License

This software is licensed under the MIT License. For details, please refer to the LICENSE file. If you are unfamiliar with the terms of the MIT License, please refer to MIT License. Please be aware of and comply with the licensee obligations!

Acknowledgements

The basic architecture, design, and some code of this project are derived from golang crypto.

The SM4 block cipher algorithm amd64 SIMD AES-NI implementation (SSE part) algorithm is derived from mjosaarinen/sm4ni.

The initial version of SM9/BN256 code was copied from the cloudflare/bn256 project, and later the basic prime field, extension field, elliptic curve operations, etc. were rewritten.

The Zu Chongzhi stream cipher algorithm implementation amd64 SIMD AES-NI, CLMUL implementation algorithm is derived from the Intel(R) Multi-Buffer Crypto for IPsec Library project.

The PKCS7 package code is a fork of the mozilla-services/pkcs7 project (which was archived on February 10, 2024) with commercial cryptography extensions.

The PKCS8 package code is a fork of the youmark/pkcs8 project with commercial cryptography extensions.

Disclaimer

Before using this project, please be sure to carefully read the GMSM Software Disclaimer!

Stargazers Over Time

If this project is useful to you, please consider giving it a Star. Stargazers over time