AES / GCM Experimentation Error

1,042 views
Skip to first unread message

david...@gmail.com

unread,
Mar 25, 2014, 10:29:14 PM3/25/14
to golan...@googlegroups.com
I have just started experimenting with AES in GCM mode encryption / decryption in Go (and in general) and was trying to throw together a simple program tonight.  I think I have the basics but I'm getting the following error:

panic: cipher: message authentication failed

goroutine 1 [running]:
runtime.panic(0x53cc0, 0x10300048)
	/tmp/sandbox/go/src/pkg/runtime/panic.c:266 +0xe0
main.main()
	/tmpfs/gosandbox-0f37b052_f5beecb6_b2e494c3_28fce22e_e6600cb3/prog.go:42 +0x840


My play code is here: http://play.golang.org/p/hYMU6aX2lj

Any ideas as to what I'm missing?

Sanjay

unread,
Mar 25, 2014, 11:31:42 PM3/25/14
to golan...@googlegroups.com
tl;dr; you're passing a slightly invalid slice. I've fixed it here: http://play.golang.org/p/v1tVc2UERI

Longer version: you're getting N bytes of 0s, and then appending the encrypted text to that, and calling that the encrypted text. When you try to decrypt it, the authentication fails, for obvious reasons.

Cheers,
Sanjay

PS. For one-of encryption like this, you can just pass nil as the dst, and it'll return a newly allocated slice with your ciphertext. The "pass a slice to be appended to" pattern allows this use-case, and if you're just going to allocate right before the method, then you might as well let it happen inside the method.

david...@gmail.com

unread,
Mar 26, 2014, 8:52:32 PM3/26/14
to golan...@googlegroups.com
Doh!  Thanks for the quick spot!


Reply all
Reply to author
Forward
0 new messages