ecies icon indicating copy to clipboard operation
ecies copied to clipboard

Panic run time error when encrypting with P521

Open adam-hanna opened this issue 6 years ago • 1 comments

I'm getting an error when encrypting with the P521 elliptic curve. The error is pasted below:

panic: runtime error: slice bounds out of range [recovered]
        panic: runtime error: slice bounds out of range

goroutine 5 [running]:
testing.tRunner.func1(0xc4200ec0f0)
        /home/adam/.gvm/gos/go1.10.3/src/testing/testing.go:742 +0x29d
panic(0x559fa0, 0x654e40)
        /home/adam/.gvm/gos/go1.10.3/src/runtime/panic.go:502 +0x229
github.com/obscuren/ecies.(*PrivateKey).GenerateShared(0xc420010780, 0xc42006fc90, 0x20, 0x20, 0x655840, 0xc420010780, 0x0, 0x0, 0xc42006fab8)
        /home/adam/go/src/github.com/obscuren/ecies/ecies.go:106 +0x292
github.com/obscuren/ecies.Encrypt(0x5a3080, 0xc420010300, 0xc42006fc90, 0xc420016418, 0x3, 0x8, 0x0, 0x0, 0x0, 0x0, ...)
        /home/adam/go/src/github.com/obscuren/ecies/ecies.go:233 +0x101
github.com/c3systems/c3/core/c3crypto.Encrypt(0xc420010840, 0xc420016418, 0x3, 0x8, 0x1, 0x1, 0x610090, 0x6100d0, 0xf)
        /home/adam/go/src/github.com/c3systems/c3/core/c3crypto/c3crypto.go:114 +0x3dd
github.com/c3systems/c3/core/c3crypto.TestEncryptAndDecrypt(0xc4200ec0f0)
        /home/adam/go/src/github.com/c3systems/c3/core/c3crypto/c3crypto_unit_test.go:127 +0x38b
testing.tRunner(0xc4200ec0f0, 0x5906a8)
        /home/adam/.gvm/gos/go1.10.3/src/testing/testing.go:777 +0xd0
created by testing.(*T).Run
        /home/adam/.gvm/gos/go1.10.3/src/testing/testing.go:824 +0x2e0
exit status 2

My code is below:

func Encrypt(pub *ecdsa.PublicKey, data []byte) ([]byte, error) {
	if pub == nil {
		return nil, ErrNilPublicKey
	}
	if data == nil {
		return nil, ErrNilData
	}

	eciesPubKey := ecies.ImportECDSAPublic(pub)
	if eciesPubKey == nil {
		return nil, ErrGeneratingECIESPublicKey
	}

	return ecies.Encrypt(rand.Reader, eciesPubKey, data, nil, nil)
}

The error is happening on L106 of ecies.go. The problem is that len(skBytes) is 66, whereas pub.params.KeyLen is 32 and therefore len(sk) is only 64. The code is trying to copy into the sk array starting at the index position -2 (len(sk)-len(skBytes) = -2).

I don't know enough about cryptography to suggest a solution, otherwise I would be happy to submit a PR. Any thoughts?

EDIT - I'm also getting the following error when using a P384 key: shared key params are too big

adam-hanna avatar Jul 07 '18 20:07 adam-hanna

hi, do you have any solutions for this issue? it looks like this problem still exists

utopizza avatar Oct 08 '22 03:10 utopizza