Repository navigation
Expand file tree
/
Copy patheddsa.go
More file actions
100 lines (81 loc) · 2.24 KB
/
Copy patheddsa.go
File metadata and controls
100 lines (81 loc) · 2.24 KB
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
99
100
package varsig
import (
"encoding/binary"
"fmt"
"io"
)
// AlgorithmEdDSA is the value specifying an EdDSA signature.
const AlgorithmEdDSA = Algorithm(0xed)
// EdDSACurve are values that specify which Edwards curve is used when
// generating the signature.
type EdDSACurve uint64
// Constants describing the values for each specific Edwards curve that can
// be encoded into a Varsig.
const (
CurveEd25519 = EdDSACurve(0xed)
CurveEd448 = EdDSACurve(0x1203)
)
func decodeEdDSACurve(r io.ByteReader) (EdDSACurve, error) {
u, err := binary.ReadUvarint(r)
if err != nil {
return 0, err
}
switch curve := EdDSACurve(u); curve {
case CurveEd25519, CurveEd448:
return curve, nil
default:
return 0, fmt.Errorf("%w: %x", ErrUnknownEdDSACurve, u)
}
}
var _ Varsig = EdDSAVarsig{}
// EdDSAVarsig is a varsig that encodes the parameters required to describe
// an EdDSA signature.
type EdDSAVarsig struct {
varsig
curve EdDSACurve
hashAlg Hash
}
// NewEdDSAVarsig creates and validates an EdDSA varsig with the provided
// curve, hash algorithm and payload encoding.
func NewEdDSAVarsig(curve EdDSACurve, hashAlgorithm Hash, payloadEncoding PayloadEncoding) EdDSAVarsig {
return EdDSAVarsig{
varsig: varsig{
algo: AlgorithmEdDSA,
payEnc: payloadEncoding,
},
curve: curve,
hashAlg: hashAlgorithm,
}
}
// Curve returns the Edwards curve used to generate the EdDSA signature.
func (v EdDSAVarsig) Curve() EdDSACurve {
return v.curve
}
// Hash returns the value describing the hash algorithm used to hash
// the payload content before the signature is generated.
func (v EdDSAVarsig) Hash() Hash {
return v.hashAlg
}
// Encode returns the encoded byte format of the EdDSAVarsig.
func (v EdDSAVarsig) Encode() []byte {
buf := v.encode()
buf = binary.AppendUvarint(buf, uint64(v.curve))
buf = binary.AppendUvarint(buf, uint64(v.hashAlg))
buf = append(buf, EncodePayloadEncoding(v.payEnc)...)
return buf
}
func decodeEdDSA(r io.ByteReader) (Varsig, error) {
curve, err := decodeEdDSACurve(r)
if err != nil {
return nil, err
}
hashAlg, err := DecodeHashAlgorithm(r)
if err != nil {
return nil, err
}
payEnc, err := DecodePayloadEncoding(r)
if err != nil {
return nil, err
}
return NewEdDSAVarsig(curve, hashAlg, payEnc), nil
}