Skip to content

Commit

Permalink
sm4: expandkey align with single block encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
emmansun authored Sep 18, 2024
1 parent acdc78c commit 6eb5efd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ Go语言商用密码软件,简称**GMSM**,一个安全、高性能、易于
- [PKCS12应用指南](./docs/pkcs12.md)

## 包结构
- **SM2** - SM2椭圆曲线公钥密码算法,曲线的具体实现位于[internal/sm2ec](https://github.com/emmansun/gmsm/tree/main/internal/sm2ec) package中。SM2曲线实现性能和Golang SDK中的NIST P256椭圆曲线原生实现(非BoringCrypto)类似,也对**amd64****arm64****s390x****ppc64le**架构做了专门汇编优化实现,您也可以参考[SM2实现细节](https://github.com/emmansun/gmsm/wiki/SM2%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96)及相关Wiki和代码,以获得更多实现细节。SM2包实现了SM2椭圆曲线公钥密码算法的数字签名算法、公钥加密算法、密钥交换算法,以及《GB/T 35276-2017信息安全技术 SM2密码算法使用规范》中的密钥对保护数据格式。
- **SM2** - SM2椭圆曲线公钥密码算法,曲线的具体实现位于[internal/sm2ec](https://github.com/emmansun/gmsm/tree/main/internal/sm2ec) package中。SM2曲线实现性能和Golang标准库中的NIST P256椭圆曲线原生实现(非BoringCrypto)类似,也对**amd64****arm64****s390x****ppc64le**架构做了专门汇编优化实现,您也可以参考[SM2实现细节](https://github.com/emmansun/gmsm/wiki/SM2%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96)及相关Wiki和代码,以获得更多实现细节。SM2包实现了SM2椭圆曲线公钥密码算法的数字签名算法、公钥加密算法、密钥交换算法,以及《GB/T 35276-2017信息安全技术 SM2密码算法使用规范》中的密钥对保护数据格式。

- **SM3** - SM3密码杂凑算法实现。**amd64**下分别针对**AVX2+BMI2、AVX、SSE2+SSSE3**做了消息扩展部分的SIMD实现; **arm64**下使用NEON指令做了消息扩展部分的SIMD实现,同时也提供了基于**A64扩展密码指令**的汇编实现;**s390x****ppc64x**通过向量指令做了消息扩展部分的优化实现。您也可以参考[SM3性能优化](https://github.com/emmansun/gmsm/wiki/SM3%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96)及相关Wiki和代码,以获得更多实现细节。

- **SM4** - SM4分组密码算法实现。**amd64**下使用**AES**指令加上**AVX2、AVX、SSE2+SSSE3**实现了比较好的性能。**arm64**下使用**AES**指令加上NEON指令实现了比较好的性能,同时也提供了基于**A64扩展密码指令**的汇编实现。**ppc64x**下使用**AES**指令加上向量指令实现了比较好的性能。针对**ECB/CBC/GCM/XTS**加密模式,做了和SM4分组密码算法的融合汇编优化实现。您也可以参考[SM4性能优化](https://github.com/emmansun/gmsm/wiki/SM4%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96)及相关Wiki和代码,以获得更多实现细节。
- **SM4** - SM4分组密码算法实现。**amd64**下使用**AES**指令加上**AVX2、AVX、SSE2+SSSE3**实现了比较好的性能。**arm64**下使用**AES**指令加上NEON指令实现了比较好的性能,同时也提供了基于**A64扩展密码指令**的汇编实现。**ppc64x**下使用**VCIPHERLAST**指令加上向量指令进行了并行优化。针对**ECB/CBC/GCM/XTS**加密模式,做了和SM4分组密码算法的融合汇编优化实现。您也可以参考[SM4性能优化](https://github.com/emmansun/gmsm/wiki/SM4%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96)及相关Wiki和代码,以获得更多实现细节。

- **SM9** - SM9标识密码算法实现。基础的素域、扩域、椭圆曲线运算以及双线性对运算位于[bn256](https://github.com/emmansun/gmsm/tree/main/sm9/bn256)包中,分别对**amd64****arm64**架构做了优化实现。您也可以参考[SM9实现及优化](https://github.com/emmansun/gmsm/wiki/SM9%E5%AE%9E%E7%8E%B0%E5%8F%8A%E4%BC%98%E5%8C%96)及相关讨论和代码,以获得更多实现细节。SM9包实现了SM9标识密码算法的密钥生成、数字签名算法、密钥封装机制和公钥加密算法、密钥交换协议。

Expand Down
2 changes: 1 addition & 1 deletion sm4/cipher_asm.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (c *sm4CipherAsm) DecryptBlocks(dst, src []byte) {
func expandKey(key []byte, enc, dec []uint32) {
if supportSM4 {
expandKeyAsm(&key[0], &ck[0], &enc[0], &dec[0], INST_SM4)
} else if supportsAES {
} else if supportsAES && useAESNI4SingleBlock {
expandKeyAsm(&key[0], &ck[0], &enc[0], &dec[0], INST_AES)
} else {
expandKeyGo(key, (*[rounds]uint32)(enc), (*[rounds]uint32)(dec))
Expand Down
8 changes: 8 additions & 0 deletions sm4/cipher_asm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,11 @@ func TestEncryptBlocksDoubleWithAESNI(t *testing.T) {
t.Errorf("expected=%x, result=%x\n", src, dst)
}
}

func BenchmarkExpand(b *testing.B) {
c := &sm4Cipher{}
b.ResetTimer()
for i := 0; i < b.N; i++ {
expandKey(encryptTests[0].key, c.enc[:], c.dec[:])
}
}

0 comments on commit 6eb5efd

Please sign in to comment.