]> Cypherpunks.ru repositories - gogost.git/blobdiff - gost3410/public.go
gost3410.Public/Private keys BE/LE marshalling
[gogost.git] / gost3410 / public.go
index 85d3557bfe03c7506e4417ed258c6e62665aa46f..ef5926bc336a03705ef44eaceb00f4656cc574bb 100644 (file)
@@ -1,5 +1,5 @@
 // GoGOST -- Pure Go GOST cryptographic functions library
-// Copyright (C) 2015-2021 Sergey Matveev <stargrave@stargrave.org>
+// Copyright (C) 2015-2023 Sergey Matveev <stargrave@stargrave.org>
 //
 // This program is free software: you can redistribute it and/or modify
 // it under the terms of the GNU General Public License as published by
@@ -23,11 +23,11 @@ import (
 
 type PublicKey struct {
        C *Curve
-       X *big.Int
-       Y *big.Int
+       X, Y *big.Int
 }
 
-func NewPublicKey(c *Curve, raw []byte) (*PublicKey, error) {
+// Unmarshal LE(X)||LE(Y) public key. "raw" must be 2*c.PointSize() length.
+func NewPublicKeyLE(c *Curve, raw []byte) (*PublicKey, error) {
        pointSize := c.PointSize()
        key := make([]byte, 2*pointSize)
        if len(raw) != len(key) {
@@ -43,7 +43,26 @@ func NewPublicKey(c *Curve, raw []byte) (*PublicKey, error) {
        }, nil
 }
 
-func (pub *PublicKey) Raw() []byte {
+// 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) RawLE() []byte {
        pointSize := pub.C.PointSize()
        raw := append(
                pad(pub.Y.Bytes(), pointSize),
@@ -53,10 +72,24 @@ func (pub *PublicKey) 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 {
-               return false, fmt.Errorf("gogost/gost3410: len(signature) != %d", 2*pointSize)
+               return false, fmt.Errorf("gogost/gost3410: len(signature)=%d != %d", len(signature), 2*pointSize)
        }
        s := bytes2big(signature[:pointSize])
        r := bytes2big(signature[pointSize:])