func (c *curve) initNamedCurve(name string, nid C.int) *curve { c.name = name c.ctx = C.BN_CTX_new() if c.ctx == nil { panic("C.BN_CTX_new: " + getErrString()) } c.g = C.EC_GROUP_new_by_curve_name(nid) if c.g == nil { panic("can't find create P256 curve: " + getErrString()) } // Get this curve's prime field c.p = newBigNum() if C.EC_GROUP_get_curve_GFp(c.g, c.p.bn, nil, nil, c.ctx) == 0 { panic("EC_GROUP_get_curve_GFp: " + getErrString()) } c.plen = (c.p.BitLen() + 7) / 8 // Get the curve's group order c.n = newBigNum() if C.EC_GROUP_get_order(c.g, c.n.bn, c.ctx) == 0 { panic("EC_GROUP_get_order: " + getErrString()) } c.nlen = (c.n.BitLen() + 7) / 8 // Get the curve's cofactor c.cofact = newBigNum() if C.EC_GROUP_get_cofactor(c.g, c.cofact.bn, c.ctx) == 0 { panic("EC_GROUP_get_cofactor: " + getErrString()) } // Stash a copy of the point at infinity c.null = newPoint(c) c.null.Null() return c }
func buildCurveParams(curve *C.EC_GROUP) *elliptic.CurveParams { cp := &elliptic.CurveParams{} // handle go < 1.5 // Name wasn't in CurveParams before elem := reflect.ValueOf(cp).Elem() f := elem.FieldByName("Name") if f.IsValid() { f.SetString(getCurveName(curve)) } cp.BitSize = getCurveBitSize(curve) p := C.BN_new() if p == nil { panic(sslerr.SSLErrorMessage().String()) } defer C.BN_free(p) a := C.BN_new() if a == nil { panic(sslerr.SSLErrorMessage().String()) } defer C.BN_free(a) b := C.BN_new() if b == nil { panic(sslerr.SSLErrorMessage().String()) } defer C.BN_free(b) if C.EC_GROUP_get_curve_GFp(curve, p, a, b, nil) != 1 { panic(sslerr.SSLErrorMessage().String()) } if p == nil || a == nil || b == nil { panic("something went wrong getting GFp params") } cp.P, _ = new(big.Int).SetString(C.GoString(C.BN_bn2dec(p)), 10) cp.N, _ = new(big.Int).SetString(C.GoString(C.BN_bn2dec(a)), 10) cp.B, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(b)), 16) generator := C.EC_GROUP_get0_generator(curve) if generator == nil { panic("generator cannot be nil") } x := C.BN_new() if x == nil { panic(sslerr.SSLErrorMessage().String()) } defer C.BN_free(x) y := C.BN_new() if y == nil { panic(sslerr.SSLErrorMessage().String()) } defer C.BN_free(y) if C.EC_POINT_get_affine_coordinates_GFp(curve, generator, x, y, nil) != 1 { panic(sslerr.SSLErrorMessage().String()) } if x == nil || y == nil { panic("something went wrong getting affine coordinates") } cp.Gx, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(x)), 16) cp.Gy, _ = new(big.Int).SetString(C.GoString(C.BN_bn2hex(y)), 16) return cp }