-
Notifications
You must be signed in to change notification settings - Fork 18
/
signature.go
98 lines (81 loc) · 2.43 KB
/
signature.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package crypto
import (
"crypto"
"encoding/json"
"errors"
)
const (
Ed25519 string = "vega/ed25519"
)
var ErrUnsupportedSignatureAlgorithm = errors.New("unsupported signature algorithm")
type SignatureAlgorithm struct {
impl signatureAlgorithmImpl
}
type signatureAlgorithmImpl interface {
Sign(priv crypto.PrivateKey, buf []byte) ([]byte, error)
Verify(pub crypto.PublicKey, message, sig []byte) (bool, error)
Name() string
Version() uint32
}
func NewEd25519() SignatureAlgorithm {
return SignatureAlgorithm{
impl: newEd25519(),
}
}
func NewSignatureAlgorithm(name string, version uint32) (SignatureAlgorithm, error) {
if name == Ed25519 && version == 1 {
return NewEd25519(), nil
}
return SignatureAlgorithm{}, ErrUnsupportedSignatureAlgorithm
}
func (a *SignatureAlgorithm) Sign(priv crypto.PrivateKey, buf []byte) ([]byte, error) {
return a.impl.Sign(priv, buf)
}
func (a *SignatureAlgorithm) Verify(pub crypto.PublicKey, message, sig []byte) (bool, error) {
return a.impl.Verify(pub, message, sig)
}
func (a *SignatureAlgorithm) Name() string {
return a.impl.Name()
}
func (a *SignatureAlgorithm) Version() uint32 {
return a.impl.Version()
}
func (a *SignatureAlgorithm) MarshalJSON() ([]byte, error) {
if a == nil {
return nil, ErrSignatureIsNil
}
return json.Marshal(&jsonAlgorithm{
Name: a.Name(),
Version: a.Version(),
})
}
func (a *SignatureAlgorithm) UnmarshalJSON(data []byte) error {
jsonAlgo := &jsonAlgorithm{}
if err := json.Unmarshal(data, &jsonAlgo); err != nil {
return err
}
algo, err := NewSignatureAlgorithm(jsonAlgo.Name, jsonAlgo.Version)
if err != nil {
return err
}
*a = algo
return nil
}
type jsonAlgorithm struct {
Name string `json:"name"`
Version uint32 `json:"version"`
}