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

crypto/rsa: GenerateKey: uncaught EOF exception #375

Closed
gopherbot opened this issue Dec 3, 2009 · 2 comments
Closed

crypto/rsa: GenerateKey: uncaught EOF exception #375

gopherbot opened this issue Dec 3, 2009 · 2 comments

Comments

@gopherbot
Copy link

by fabio.pianese:

Probable bug in the crypto/rsa package. GenerateKey is declaring new errs
which override the return value ones. Caller function gets a nil error in
the top scope because the lower scoped error is not bound to the
return. 

What steps will reproduce the problem?
1. generate a rsa private key (let's call it priv)
2. priv.Validate() or any attempt to print fields from priv results in a crash

What is your $GOOS?  $GOARCH?
$ echo $GOOS $GOARCH
linux 386

Which revision are you using?  (hg identify)

$ hg log -l 1
changeset:   4108:d1b75410b793
tag:         tip
user:        Adam Langley <agl@golang.org>
date:        Tue Nov 17 18:21:47 2009 -0800

Please provide any additional information below.

Modifying the GenerateKey function as follows properly returns a EOF error
to the caller. (Thanks to Noah Evans for figuring out the issue!)

----
// GenerateKeyPair generates an RSA keypair of the given bit size.
func GenerateKey(rand io.Reader, bits int) (priv *PrivateKey, err os.Error) {
    priv = new(PrivateKey);
    // Smaller public exponents lead to faster public key
    // operations. Since the exponent must be coprime to
    // (p-1)(q-1), the smallest possible value is 3. Some have
    // suggested that a larger exponent (often 2**16+1) be used
    // since previous implementation bugs[1] were avoided when this
    // was the case. However, there are no current reasons not to use
    // small exponents.
    // [1] http://marc.info/?l=cryptography&;m=115694833312008&w=2
    priv.E = 3;

    pminus1 := new(big.Int);
    qminus1 := new(big.Int);
    totient := new(big.Int);

    for {
        var p, q *big.Int;   // line changed from original
        p, err = randomSafePrime(rand, bits/2);
        if err != nil {
            return
        }

        q, err = randomSafePrime(rand, bits/2);
        if err != nil {
            return
        }

        if p.Cmp(q) == 0 {
            continue
        }

        n := new(big.Int).Mul(p, q);
        pminus1.Sub(p, bigOne);
        qminus1.Sub(q, bigOne);
        totient.Mul(pminus1, qminus1);

        g := new(big.Int);
        priv.D = new(big.Int);
        y := new(big.Int);
        e := big.NewInt(int64(priv.E));
        big.GcdInt(g, priv.D, y, e, totient);

        if g.Cmp(bigOne) == 0 {
            priv.D.Add(priv.D, totient);
            priv.P = p;
            priv.Q = q;
            priv.N = n;

            break;
        }
    }

    return;
}

----
@agl
Copy link
Contributor

agl commented Dec 3, 2009

Comment 1:

Yep, I'm a prat. Fixing now.

Owner changed to a...@golang.org.

Status changed to Accepted.

@agl
Copy link
Contributor

agl commented Dec 4, 2009

Comment 2:

This issue was closed by revision e93132c.

Status changed to Fixed.

Merged into issue #-.

@gopherbot gopherbot added the fixed label Dec 4, 2009
@mikioh mikioh changed the title crypto/rsa GenerateKey: uncaught EOF exception crypto/rsa: GenerateKey: uncaught EOF exception Jan 14, 2015
@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants