Exemple #1
0
// parseECPrivateKey parses an ASN.1 Elliptic Curve Private Key Structure.
// The OID for the named curve may be provided from another source (such as
// the PKCS8 container) - if it is provided then use this instead of the OID
// that may exist in the EC private key structure.
func parseECPrivateKey(namedCurveOID *asn1.ObjectIdentifier, der []byte) (key *bitecdsa.PrivateKey, err error) {
	var privKey ecPrivateKey
	if _, err := asn1.Unmarshal(der, &privKey); err != nil {
		return nil, errors.New("x509: failed to parse EC private key: " + err.Error())
	}
	if privKey.Version != ecPrivKeyVersion {
		return nil, fmt.Errorf("x509: unknown EC private key version %d", privKey.Version)
	}

	var curve *bitelliptic.BitCurve
	if namedCurveOID != nil {
		curve = namedCurveFromOID(*namedCurveOID)
	} else {
		curve = namedCurveFromOID(privKey.NamedCurveOID)
	}
	if curve == nil {
		return nil, errors.New("x509: unknown elliptic curve")
	}

	k := new(big.Int).SetBytes(privKey.PrivateKey)
	if k.Cmp(curve.N) >= 0 {
		return nil, errors.New("x509: invalid elliptic curve private key value")
	}
	return bitecdsa.NewKeyFromInt(curve, k), nil
}
Exemple #2
0
func (k *Key) Sign(data []byte) (*big.Int, *big.Int) {
	key := bitecdsa.NewKeyFromInt(bitelliptic.S256(), k.prvKey)
	r, s, err := bitecdsa.Sign(rand.Reader, key, data)
	if err != nil {
		panic(err.Error())
	}
	return r, s
}
Exemple #3
0
func privateToPublic(d *big.Int) point {
	key := bitecdsa.NewKeyFromInt(bitelliptic.S256(), d)
	return point{key.X, key.Y}
}