// 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
package gost3410
import (
+ "crypto"
"fmt"
"math/big"
)
Y *big.Int
}
-func NewPublicKey(curve *Curve, raw []byte) (*PublicKey, error) {
- pointSize := curve.PointSize()
+// Unmarshal LE(X)||LE(Y) public key. "raw" must be 2*c.PointSize() length.
+func NewPublicKey(c *Curve, raw []byte) (*PublicKey, error) {
+ pointSize := c.PointSize()
key := make([]byte, 2*pointSize)
if len(raw) != len(key) {
return nil, fmt.Errorf("gogost/gost3410: len(key) != %d", len(key))
key[i] = raw[len(raw)-i-1]
}
return &PublicKey{
- curve,
+ c,
bytes2big(key[pointSize : 2*pointSize]),
bytes2big(key[:pointSize]),
}, nil
}
-func (pub *PublicKey) Raw() []byte {
+// Marshal LE(X)||LE(Y) public key. raw will be 2*pub.C.PointSize() length.
+func (pub *PublicKey) Raw() (raw []byte) {
pointSize := pub.C.PointSize()
- raw := append(
+ raw = append(
pad(pub.Y.Bytes(), pointSize),
pad(pub.X.Bytes(), pointSize)...,
)
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:])
lm.Mod(lm, pub.C.Q)
return lm.Cmp(r) == 0, nil
}
+
+func (our *PublicKey) Equal(theirKey crypto.PublicKey) bool {
+ their, ok := theirKey.(*PublicKey)
+ if !ok {
+ return false
+ }
+ return our.X.Cmp(their.X) == 0 && our.Y.Cmp(their.Y) == 0 && our.C.Equal(their.C)
+}