X-Git-Url: http://www.git.cypherpunks.ru/?p=gogost.git;a=blobdiff_plain;f=gost3410%2Fpublic.go;fp=gost3410%2Fpublic.go;h=ef5926bc336a03705ef44eaceb00f4656cc574bb;hp=d2c01bc67acbd2990378f3bd3d1e578617a05e2b;hb=e077d5f903e233fbefed0815be21be5f8f482017;hpb=ff37b11bcc4673a97d61be0ba6026e213de2223b diff --git a/gost3410/public.go b/gost3410/public.go index d2c01bc..ef5926b 100644 --- a/gost3410/public.go +++ b/gost3410/public.go @@ -23,12 +23,11 @@ import ( type PublicKey struct { C *Curve - X *big.Int - Y *big.Int + X, Y *big.Int } // Unmarshal LE(X)||LE(Y) public key. "raw" must be 2*c.PointSize() length. -func NewPublicKey(c *Curve, raw []byte) (*PublicKey, error) { +func NewPublicKeyLE(c *Curve, raw []byte) (*PublicKey, error) { pointSize := c.PointSize() key := make([]byte, 2*pointSize) if len(raw) != len(key) { @@ -44,10 +43,28 @@ func NewPublicKey(c *Curve, raw []byte) (*PublicKey, error) { }, nil } +// Unmarshal BE(X)||BE(Y) public key. "raw" must be 2*c.PointSize() length. +func NewPublicKeyBE(c *Curve, raw []byte) (*PublicKey, error) { + pointSize := c.PointSize() + if len(raw) != 2*pointSize { + return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", 2*pointSize) + } + return &PublicKey{ + c, + bytes2big(raw[:pointSize]), + bytes2big(raw[pointSize:]), + }, nil +} + +// This is an alias for NewPublicKeyLE(). +func NewPublicKey(c *Curve, raw []byte) (*PublicKey, error) { + return NewPublicKeyLE(c, raw) +} + // Marshal LE(X)||LE(Y) public key. raw will be 2*pub.C.PointSize() length. -func (pub *PublicKey) Raw() (raw []byte) { +func (pub *PublicKey) RawLE() []byte { pointSize := pub.C.PointSize() - raw = append( + raw := append( pad(pub.Y.Bytes(), pointSize), pad(pub.X.Bytes(), pointSize)..., ) @@ -55,6 +72,20 @@ func (pub *PublicKey) Raw() (raw []byte) { return raw } +// Marshal BE(X)||BE(Y) public key. raw will be 2*pub.C.PointSize() length. +func (pub *PublicKey) RawBE() []byte { + pointSize := pub.C.PointSize() + return append( + pad(pub.X.Bytes(), pointSize), + pad(pub.Y.Bytes(), pointSize)..., + ) +} + +// This is an alias for RawLE(). +func (pub *PublicKey) Raw() []byte { + return pub.RawLE() +} + func (pub *PublicKey) VerifyDigest(digest, signature []byte) (bool, error) { pointSize := pub.C.PointSize() if len(signature) != 2*pointSize {