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

sm4: cbc解密出错 #143

Closed
zhangyongding opened this issue Jul 12, 2023 · 7 comments
Closed

sm4: cbc解密出错 #143

zhangyongding opened this issue Jul 12, 2023 · 7 comments
Assignees
Labels
bug Something isn't working

Comments

@zhangyongding
Copy link

使用最新版本的单元测试,报如下错误

go test -timeout 30s -run ^TestCBCDecrypterSM4$ github.com/emmansun/gmsm/cipher -count=1 -v

=== RUN TestCBCDecrypterSM4
SIGILL: illegal instruction
PC=0x510135 m=0 sigcode=2
instruction bytes: 0xc4 0x62 0x79 0x58 0x4 0x8 0xc5 0x39 0xef 0xc5 0xc5 0x39 0xef 0xc6 0xc5 0x39

goroutine 6 [running]:
github.com/emmansun/gmsm/sm4.decryptBlocksChain(0xc00011a280, {0xc00001c140, 0x40, 0x40}, {0xc00001e0b0, 0x40, 0x40}, 0xc00001e0a0)
/home/godev/go/src/gmsm/sm4/cbc_cipher_asm_amd64.s:220 +0x535 fp=0xc000054580 sp=0xc000054578 pc=0x510135
github.com/emmansun/gmsm/sm4.(*cbc).CryptBlocks(0xc00010a8c0, {0xc000018320, 0x10, 0x10}, {0xc000018320, 0x10, 0x10})
/home/godev/go/src/gmsm/sm4/cbc_cipher_asm.go:91 +0x46c fp=0xc000054610 sp=0xc000054580 pc=0x508eac
github.com/emmansun/gmsm/cipher_test.TestCBCDecrypterSM4(0xc000007a00)
/home/godev/go/src/gmsm/cipher/cbc_sm4_test.go:178 +0x259 fp=0xc000054770 sp=0xc000054610 pc=0x543c39
testing.tRunner(0xc000007a00, 0x591338)
/usr/local/go/src/testing/testing.go:1576 +0x10b fp=0xc0000547c0 sp=0xc000054770 pc=0x4bc3ab
testing.(*T).Run.func1()
/usr/local/go/src/testing/testing.go:1629 +0x2a fp=0xc0000547e0 sp=0xc0000547c0 pc=0x4bd3ea
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000547e8 sp=0xc0000547e0 pc=0x4678e1
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1629 +0x3ea

goroutine 1 [chan receive]:
runtime.gopark(0x68cda0?, 0xc000012058?, 0xf0?, 0x88?, 0xc000063a28?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc0000639a8 sp=0xc000063988 pc=0x438576
runtime.chanrecv(0xc000028150, 0xc000063aa7, 0x1)
/usr/local/go/src/runtime/chan.go:583 +0x49d fp=0xc000063a38 sp=0xc0000639a8 pc=0x40695d
runtime.chanrecv1(0x68c4a0?, 0x5592a0?)
/usr/local/go/src/runtime/chan.go:442 +0x18 fp=0xc000063a60 sp=0xc000063a38 pc=0x406498
testing.(*T).Run(0xc000007860, {0x582326?, 0x4bc0c5?}, 0x591338)
/usr/local/go/src/testing/testing.go:1630 +0x405 fp=0xc000063b20 sp=0xc000063a60 pc=0x4bd265
testing.runTests.func1(0x68cda0?)
/usr/local/go/src/testing/testing.go:2036 +0x45 fp=0xc000063b70 sp=0xc000063b20 pc=0x4bf3c5
testing.tRunner(0xc000007860, 0xc000063c88)
/usr/local/go/src/testing/testing.go:1576 +0x10b fp=0xc000063bc0 sp=0xc000063b70 pc=0x4bc3ab
testing.runTests(0xc0001180a0?, {0x686800, 0x15, 0x15}, {0x20?, 0x100c0001149d8?, 0x68c5c0?})
/usr/local/go/src/testing/testing.go:2034 +0x489 fp=0xc000063cb8 sp=0xc000063bc0 pc=0x4bf2a9
testing.(*M).Run(0xc0001180a0)
/usr/local/go/src/testing/testing.go:1906 +0x63a fp=0xc000063f00 sp=0xc000063cb8 pc=0x4bdc1a
main.main()
_testmain.go:191 +0x1aa fp=0xc000063f80 sp=0xc000063f00 pc=0x54a2ca
runtime.main()
/usr/local/go/src/runtime/proc.go:250 +0x207 fp=0xc000063fe0 sp=0xc000063f80 pc=0x438147
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000063fe8 sp=0xc000063fe0 pc=0x4678e1

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000052fb0 sp=0xc000052f90 pc=0x438576
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:387
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:305 +0xb0 fp=0xc000052fe0 sp=0xc000052fb0 pc=0x4383b0
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000052fe8 sp=0xc000052fe0 pc=0x4678e1
created by runtime.init.6
/usr/local/go/src/runtime/proc.go:293 +0x25

goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000053780 sp=0xc000053760 pc=0x438576
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:387
runtime.bgsweep(0x0?)
/usr/local/go/src/runtime/mgcsweep.go:278 +0x8e fp=0xc0000537c8 sp=0xc000053780 pc=0x4230ee
runtime.gcenable.func1()
/usr/local/go/src/runtime/mgc.go:178 +0x26 fp=0xc0000537e0 sp=0xc0000537c8 pc=0x4183a6
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000537e8 sp=0xc0000537e0 pc=0x4678e1
created by runtime.gcenable
/usr/local/go/src/runtime/mgc.go:178 +0x6b

goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc000028070?, 0x5b2f98?, 0x1?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000053f70 sp=0xc000053f50 pc=0x438576
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:387
runtime.(*scavengerState).park(0x68c640)
/usr/local/go/src/runtime/mgcscavenge.go:400 +0x53 fp=0xc000053fa0 sp=0xc000053f70 pc=0x421013
runtime.bgscavenge(0x0?)
/usr/local/go/src/runtime/mgcscavenge.go:628 +0x45 fp=0xc000053fc8 sp=0xc000053fa0 pc=0x4215e5
runtime.gcenable.func2()
/usr/local/go/src/runtime/mgc.go:179 +0x26 fp=0xc000053fe0 sp=0xc000053fc8 pc=0x418346
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000053fe8 sp=0xc000053fe0 pc=0x4678e1
created by runtime.gcenable
/usr/local/go/src/runtime/mgc.go:179 +0xaa

goroutine 5 [finalizer wait]:
runtime.gopark(0x4388f2?, 0x4382e5?, 0x0?, 0x0?, 0xc000052770?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000052628 sp=0xc000052608 pc=0x438576
runtime.runfinq()
/usr/local/go/src/runtime/mfinal.go:193 +0x107 fp=0xc0000527e0 sp=0xc000052628 pc=0x4173e7
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000527e8 sp=0xc0000527e0 pc=0x4678e1
created by runtime.createfing
/usr/local/go/src/runtime/mfinal.go:163 +0x45

rax 0xc00011a280
rbx 0xc00001c140
rcx 0x0
rdx 0xc00001e0b0
rdi 0xc00011a280
rsi 0xc00001e0a0
rbp 0xc000054600
rsp 0xc000054578
r8 0x10
r9 0xc000018320
r10 0x10
r11 0xc00001e0b0
r12 0x1
r13 0x3
r14 0xc000007ba0
r15 0x68cda0
rip 0x510135
rflags 0x10246
cs 0x33
fs 0x0
gs 0x0
FAIL github.com/emmansun/gmsm/cipher 0.007s

@zhangyongding
Copy link
Author

go test -timeout 30s -run ^TestWithoutGFMUL$ github.com/emmansun/gmsm/sm4 -count=1 -v

=== RUN TestWithoutGFMUL
SIGILL: illegal instruction
PC=0x5105c6 m=0 sigcode=2
instruction bytes: 0xc4 0x62 0x79 0x58 0x4 0x8 0xc5 0x39 0xef 0xc5 0xc5 0x39 0xef 0xc6 0xc5 0x39

goroutine 6 [running]:
github.com/emmansun/gmsm/sm4.encryptBlocksAsm(0xc00011a180, {0xc00001c140, 0x40, 0x40}, {0xc00001c180, 0x40, 0x40}, 0x0)
/home/godev/go/src/gmsm/sm4/asm_amd64.s:228 +0x526 fp=0xc000054468 sp=0xc000054460 pc=0x5105c6
github.com/emmansun/gmsm/sm4.(*sm4CipherAsm).EncryptBlocks(0x40d0aa?, {0xc00001c140?, 0x6a4d40?, 0xc000054538?}, {0xc00001c180?, 0x100c000138000?, 0x7f8c07e1d428?})
/home/godev/go/src/gmsm/sm4/cipher_asm.go:86 +0xba fp=0xc0000544b8 sp=0xc000054468 pc=0x508cda
github.com/emmansun/gmsm/sm4.(*gcm).counterCrypt(0xc000138000, {0xc00001a160, 0x20, 0x20}, {0xc0000182e0, 0x10, 0x10}, 0xc000054610)
/home/godev/go/src/gmsm/sm4/gcm_cipher_asm.go:288 +0x36d fp=0xc000054570 sp=0xc0000544b8 pc=0x50b52d
github.com/emmansun/gmsm/sm4.(*gcm).Seal(0xc000138000, {0x0?, 0x0?, 0x5bc66e?}, {0xc0000182f0?, 0xc?, 0xc?}, {0xc0000182e0, 0x10, 0x10}, ...)
/home/godev/go/src/gmsm/sm4/gcm_cipher_asm.go:100 +0x259 fp=0xc000054640 sp=0xc000054570 pc=0x50a859
github.com/emmansun/gmsm/sm4.TestWithoutGFMUL(0xc000007a00)
/home/godev/go/src/gmsm/sm4/cipher_asm_test.go:42 +0x465 fp=0xc000054770 sp=0xc000054640 pc=0x50e105
testing.tRunner(0xc000007a00, 0x583308)
/usr/local/go/src/testing/testing.go:1576 +0x10b fp=0xc0000547c0 sp=0xc000054770 pc=0x4c18cb
testing.(*T).Run.func1()
/usr/local/go/src/testing/testing.go:1629 +0x2a fp=0xc0000547e0 sp=0xc0000547c0 pc=0x4c290a
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000547e8 sp=0xc0000547e0 pc=0x467d01
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1629 +0x3ea

goroutine 1 [chan receive]:
runtime.gopark(0x676c80?, 0xc000012058?, 0xc0?, 0x89?, 0xc000063a28?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc0000639a8 sp=0xc000063988 pc=0x4387b6
runtime.chanrecv(0xc000028150, 0xc000063aa7, 0x1)
/usr/local/go/src/runtime/chan.go:583 +0x49d fp=0xc000063a38 sp=0xc0000639a8 pc=0x40695d
runtime.chanrecv1(0x676380?, 0x551340?)
/usr/local/go/src/runtime/chan.go:442 +0x18 fp=0xc000063a60 sp=0xc000063a38 pc=0x406498
testing.(*T).Run(0xc000007860, {0x5793ba?, 0x4c15e5?}, 0x583308)
/usr/local/go/src/testing/testing.go:1630 +0x405 fp=0xc000063b20 sp=0xc000063a60 pc=0x4c2785
testing.runTests.func1(0x676c80?)
/usr/local/go/src/testing/testing.go:2036 +0x45 fp=0xc000063b70 sp=0xc000063b20 pc=0x4c48e5
testing.tRunner(0xc000007860, 0xc000063c88)
/usr/local/go/src/testing/testing.go:1576 +0x10b fp=0xc000063bc0 sp=0xc000063b70 pc=0x4c18cb
testing.runTests(0xc0001180a0?, {0x671c60, 0x8, 0x8}, {0x0?, 0x100c000114918?, 0x6764a0?})
/usr/local/go/src/testing/testing.go:2034 +0x489 fp=0xc000063cb8 sp=0xc000063bc0 pc=0x4c47c9
testing.(*M).Run(0xc0001180a0)
/usr/local/go/src/testing/testing.go:1906 +0x63a fp=0xc000063f00 sp=0xc000063cb8 pc=0x4c313a
main.main()
_testmain.go:85 +0x1aa fp=0xc000063f80 sp=0xc000063f00 pc=0x54278a
runtime.main()
/usr/local/go/src/runtime/proc.go:250 +0x207 fp=0xc000063fe0 sp=0xc000063f80 pc=0x438387
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000063fe8 sp=0xc000063fe0 pc=0x467d01

goroutine 2 [force gc (idle)]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000052fb0 sp=0xc000052f90 pc=0x4387b6
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:387
runtime.forcegchelper()
/usr/local/go/src/runtime/proc.go:305 +0xb0 fp=0xc000052fe0 sp=0xc000052fb0 pc=0x4385f0
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000052fe8 sp=0xc000052fe0 pc=0x467d01
created by runtime.init.6
/usr/local/go/src/runtime/proc.go:293 +0x25

goroutine 3 [GC sweep wait]:
runtime.gopark(0x0?, 0x0?, 0x0?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000053780 sp=0xc000053760 pc=0x4387b6
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:387
runtime.bgsweep(0x0?)
/usr/local/go/src/runtime/mgcsweep.go:278 +0x8e fp=0xc0000537c8 sp=0xc000053780 pc=0x42332e
runtime.gcenable.func1()
/usr/local/go/src/runtime/mgc.go:178 +0x26 fp=0xc0000537e0 sp=0xc0000537c8 pc=0x4185e6
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000537e8 sp=0xc0000537e0 pc=0x467d01
created by runtime.gcenable
/usr/local/go/src/runtime/mgc.go:178 +0x6b

goroutine 4 [GC scavenge wait]:
runtime.gopark(0xc000028070?, 0x5a4e78?, 0x1?, 0x0?, 0x0?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000053f70 sp=0xc000053f50 pc=0x4387b6
runtime.goparkunlock(...)
/usr/local/go/src/runtime/proc.go:387
runtime.(*scavengerState).park(0x676520)
/usr/local/go/src/runtime/mgcscavenge.go:400 +0x53 fp=0xc000053fa0 sp=0xc000053f70 pc=0x421253
runtime.bgscavenge(0x0?)
/usr/local/go/src/runtime/mgcscavenge.go:628 +0x45 fp=0xc000053fc8 sp=0xc000053fa0 pc=0x421825
runtime.gcenable.func2()
/usr/local/go/src/runtime/mgc.go:179 +0x26 fp=0xc000053fe0 sp=0xc000053fc8 pc=0x418586
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc000053fe8 sp=0xc000053fe0 pc=0x467d01
created by runtime.gcenable
/usr/local/go/src/runtime/mgc.go:179 +0xaa

goroutine 5 [finalizer wait]:
runtime.gopark(0x438b32?, 0x438525?, 0x0?, 0x0?, 0xc000052770?)
/usr/local/go/src/runtime/proc.go:381 +0xd6 fp=0xc000052628 sp=0xc000052608 pc=0x4387b6
runtime.runfinq()
/usr/local/go/src/runtime/mfinal.go:193 +0x107 fp=0xc0000527e0 sp=0xc000052628 pc=0x417627
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1598 +0x1 fp=0xc0000527e8 sp=0xc0000527e0 pc=0x467d01
created by runtime.createfing
/usr/local/go/src/runtime/mfinal.go:163 +0x45

rax 0xc00011a180
rbx 0xc00001c140
rcx 0x0
rdx 0xc00001c180
rdi 0x40
rsi 0xc00001c180
rbp 0xc0000544a8
rsp 0xc000054460
r8 0x40
r9 0x40
r10 0xc00001c17f
r11 0xc00001c180
r12 0x10
r13 0x10
r14 0xc000007ba0
r15 0x1
rip 0x5105c6
rflags 0x10246
cs 0x33
fs 0x0
gs 0x0
FAIL github.com/emmansun/gmsm/sm4 0.007s

@emmansun
Copy link
Owner

1.第一步:把cipher_asm.go中的supportAVX改为false测试一下。
2.第二步:supportAVX改回原值,然后改aesni_macro_and64.s中第217行:VAESENCLAST X_NIBBLE_MASK, x, x;改为AESENCLAST X_NIBBLE_MASK, x;试一下。

@zhangyongding
Copy link
Author

1.第一步:设置useAVX = false,测试成功
2.第二步:一样的错误

@emmansun
Copy link
Owner

1.第一步:设置useAVX = false,测试成功 2.第二步:一样的错误

明天我确认一下哪个指令AVX不支持

@emmansun
Copy link
Owner

@zhangyongding ,请帮忙clone最新代码测试一下!

@zhangyongding
Copy link
Author

都测试通过了,谢谢

@emmansun emmansun self-assigned this Jul 13, 2023
@emmansun emmansun added the bug Something isn't working label Jul 13, 2023
@emmansun
Copy link
Owner

emmansun commented Jul 13, 2023

Fixed in v0.19.1,由于没有测试机器,自己手写支持AVX,但不支持AVX2代码还是容易误用指令的,还是有必要使用avo辅助。

@emmansun emmansun changed the title cbc解密出错 sm4: cbc解密出错 Nov 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants