Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] GHASH逻辑错误造成GCM模式下tag错误 #178

Open
wond4 opened this issue Oct 13, 2022 · 2 comments
Open

[BUG] GHASH逻辑错误造成GCM模式下tag错误 #178

wond4 opened this issue Oct 13, 2022 · 2 comments

Comments

@wond4
Copy link

wond4 commented Oct 13, 2022

近期在做国密密码机对接,发现 sm4 库 gcm 模式下计算出的密文和密码机一致,但是鉴别TAG不同。
系统测试后发现密码机、GmSSL在线验证结果一致,基本确定本库实现有问题。验证截图如下:
image
image

@wond4
Copy link
Author

wond4 commented Oct 13, 2022

测试代码如下:

func Demo() {
	keyHex := "0123456789ABCDEFFEDCBA9876543210"
	plainHex := "AAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBCCCCCCCCCCCCCCCCDDDDDDDDDDDDDDDDEEEEEEEEEEEEEEEEFFFFFFFFFFFFFFFFEEEEEEEEEEEEEEEEAAAAAAAAAAAAAAAA"
	ivHex := "00001234567800000000ABCD"
	aadHex := "FEEDFACEDEADBEEFFEEDFACEDEADBEEFABADDAD2"

	keyBytes, _ := hex.DecodeString(keyHex)
	ivBytes, _ := hex.DecodeString(ivHex)
	aadBytes, _ := hex.DecodeString(aadHex)
	plainBytes, _ := hex.DecodeString(plainHex)
	cipherBytes, tagBytes := sm4.GCMEncrypt(keyBytes, ivBytes, plainBytes, aadBytes)
	log.Println("key in hex:", keyHex)
	log.Println("plain text in hex:", plainHex)
	log.Println("iv in hex:", ivHex)
	log.Println("aad in hex:", aadHex)
	log.Println("cipher text in hex:", hex.EncodeToString(cipherBytes))
	log.Println("tag in hex:", hex.EncodeToString(tagBytes))
}

@emmansun
Copy link

#172 #198 类似

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants