// GenerateKey returns a public/private key pair. The private key is
// generated using the given reader, which must return random data.
// TODO(runcom): add support for Go stdlib `elliptic.Curve`, for now unsupported
GenerateKey returns a public/private key pair. The private key is
generated using the given reader, which must return random data.
TODO(runcom): add support for Go stdlib `elliptic.Curve`, for now unsupported
// GenerateKey returns a public / private key pair . The private key is // generated using the given reader which must return random data . // TODO ( runcom ) : add support for Go stdlib ` elliptic . Curve ` for now unsupported
func GenerateKey(curve elliptic.Curve, rand io.Reader) (priv []byte, x, y *big.Int, err error) {
	c, ok := curve.(CurveParams)
	if !ok {
		return nil, nil, nil, errors.New("provided curve is unsupported")
	}
	k := C.EC_KEY_new()
	if k == nil {
		return nil, nil, nil, errors.New("can't create key")
	}
	defer C.EC_KEY_free(k)
	if C.EC_KEY_set_group(k, c.curve) != 1 {
		return nil, nil, nil, errors.New("can't set ec_group on key")
	}
	if C.EC_KEY_generate_key(k) != 1 {
		return nil, nil, nil, errors.New("can't generate key")
	}
	blen := C.i2d_ECPrivateKey(k, nil)
	if blen == 0 {
		return nil, nil, nil, errors.New("can't get private key")
	}
	buf := C.malloc(C.size_t(blen))
	defer C.free(buf)
	pkey := (*C.uchar)(unsafe.Pointer(buf))
	if C.i2d_ECPrivateKey(k, &pkey) == 0 {
		return nil, nil, nil, errors.New("can't get private key")
	}
	point := C.EC_KEY_get0_public_key(k)
	if point == nil {
		return nil, nil, nil, errors.New("can't get public key")
	}
	defer C.EC_POINT_free(point)
	rx := C.BN_new()
	if rx == nil {
		return nil, nil, nil, errors.New("error creating big num")
	}
	defer C.BN_free(rx)
	ry := C.BN_new()
	if ry == nil {
		return nil, nil, nil, errors.New("errors creating big num")
	}
	defer C.BN_free(ry)
	if C.EC_POINT_get_affine_coordinates_GFp(c.curve, point, rx, ry, nil) != 1 {
		return nil, nil, nil, errors.New("can't get public key")
	}
	x, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(rx)), 16)
	y, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(ry)), 16)
	priv = C.GoBytes(unsafe.Pointer(pkey), C.int(blen))
	return priv, x, y, nil
}
func GenerateKey ( curve elliptic . Curve , rand io . Reader ) ( priv [ ] byte , x , y * big . Int , err error ) { c , ok := curve . ( CurveParams ) if ! ok { return nil , nil , nil , errors . New ( " " ) } k := C . EC_KEY_new ( ) if k == nil { return nil , nil , nil , errors . New ( " " ) } defer C . EC_KEY_free ( k ) if C . EC_KEY_set_group ( k , c . curve ) != 1 { return nil , nil , nil , errors . New ( " " ) } if C . EC_KEY_generate_key ( k ) != 1 { return nil , nil , nil , errors . New ( " " ) } blen := C . i2d_ECPrivateKey ( k , nil ) if blen == 0 { return nil , nil , nil , errors . New ( " " ) } buf := C . malloc ( C . size_t ( blen ) ) defer C . free ( buf ) pkey := ( * C . uchar ) ( unsafe . Pointer ( buf ) ) if C . i2d_ECPrivateKey ( k , & pkey ) == 0 { return nil , nil , nil , errors . New ( " " ) } point := C . EC_KEY_get0_public_key ( k ) if point == nil { return nil , nil , nil , errors . New ( " " ) } defer C . EC_POINT_free ( point ) rx := C . BN_new ( ) if rx == nil { return nil , nil , nil , errors . New ( " " ) } defer C . BN_free ( rx ) ry := C . BN_new ( ) if ry == nil { return nil , nil , nil , errors . New ( " " ) } defer C . BN_free ( ry ) if C . EC_POINT_get_affine_coordinates_GFp ( c . curve , point , rx , ry , nil ) != 1 { return nil , nil , nil , errors . New ( " " ) } x , _ = new ( big . Int ) . SetString ( C . GoString ( C . BN_bn2hex ( rx ) ) , 16 ) y , _ = new ( big . Int ) . SetString ( C . GoString ( C . BN_bn2hex ( ry ) ) , 16 ) priv = C . GoBytes ( unsafe . Pointer ( pkey ) , C . int ( blen ) ) return priv , x , y , nil }
GenerateKey returns a public/private key pair.
GenerateKey returns a public / private key pair .
// rand isn't used with openssl... // for go stdlib compatibility //c, _ := curve.(*elliptic.CurveParams) // to use this pkg