obscuren / ecies

ECIES implemented in Go
BSD 3-Clause "New" or "Revised" License
31 stars 15 forks source link

ecies: shared key is too big #4

Closed zeroXten closed 9 years ago

zeroXten commented 9 years ago

I am able to reliably get the "ecies: shared key is too big" error after a largish number of tries using the following test code:

package main

import (
        "crypto/ecdsa"
        "crypto/elliptic"
        "crypto/rand"
        "fmt"
        "github.com/pki-io/ecies" // forked from this repo
)

func GenerateECKey() (*ecdsa.PrivateKey, error) {
    key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
    if err != nil {
        return nil, fmt.Errorf("Can't create ECDSA keys: %s", err)
        }
    return key, nil
}

func eciesEncrypt(plaintext []byte, publicKey *ecdsa.PublicKey) ([]byte, error) {
    pub := ecies.ImportECDSAPublic(publicKey)
    return ecies.Encrypt(rand.Reader, pub, plaintext, nil, nil)
}

func main() {
        key, err := GenerateECKey()
    if err != nil {
        panic(err)
    }
    message := []byte("this is a test")
    _, err = eciesEncrypt(message, &key.PublicKey)
    if err != nil {
        panic(err)
    }
}

Running this command:

$ i=0; while :; do echo $i; let i=i+1; date; gom run ecies-test.go || break; done

And getting this result:

347
Fri 27 Mar 2015 22:17:42 GMT
348
Fri 27 Mar 2015 22:17:42 GMT
panic: ecies: shared key is too big

goroutine 1 [running]:
main.main()
    ..../ecies-test.go:32 +0x107
exit status 2
gom:  exit status 1
obscuren commented 9 years ago

I've tried to reproduce the error using the code above using this repo. I've given up after 50.000 tries. Are you sure this problem isn't related to just your own fork?

zeroXten commented 9 years ago

Ah yes, my bad. Looks like this was fixed in commit 04c1a81509576c898f4d30a76f9d84d087e2a4cf after our fork was taken. I've updated our fork to your current master and the problem is resolved.

obscuren commented 9 years ago

If you don't modify the fork and simply do this out of a security reason I'd recommend to use godep over your own fork.