-
Notifications
You must be signed in to change notification settings - Fork 178
/
crypto.go
132 lines (112 loc) · 2.9 KB
/
crypto.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package fvm
import (
"fmt"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/model/flow"
)
type SignatureVerifier interface {
Verify(
signature []byte,
tag []byte,
message []byte,
publicKey crypto.PublicKey,
hashAlgo hash.HashingAlgorithm,
) (bool, error)
}
type DefaultSignatureVerifier struct{}
func NewDefaultSignatureVerifier() DefaultSignatureVerifier {
return DefaultSignatureVerifier{}
}
func (DefaultSignatureVerifier) Verify(
signature []byte,
tag []byte,
message []byte,
publicKey crypto.PublicKey,
hashAlgo hash.HashingAlgorithm,
) (bool, error) {
hasher := newHasher(hashAlgo)
if hasher == nil {
return false, ErrInvalidHashAlgorithm
}
message = append(tag, message...)
valid, err := publicKey.Verify(signature, message, hasher)
if err != nil {
return false, fmt.Errorf("failed to verify signature: %w", err)
}
return valid, nil
}
func newHasher(hashAlgo hash.HashingAlgorithm) hash.Hasher {
switch hashAlgo {
case hash.SHA2_256:
return hash.NewSHA2_256()
case hash.SHA3_256:
return hash.NewSHA3_256()
}
return nil
}
// StringToSigningAlgorithm converts a string to a SigningAlgorithm.
func StringToSigningAlgorithm(s string) crypto.SigningAlgorithm {
switch s {
case crypto.ECDSAP256.String():
return crypto.ECDSAP256
case crypto.ECDSASecp256k1.String():
return crypto.ECDSASecp256k1
default:
return crypto.UnknownSigningAlgorithm
}
}
// StringToHashingAlgorithm converts a string to a HashingAlgorithm.
func StringToHashingAlgorithm(s string) hash.HashingAlgorithm {
switch s {
case hash.SHA2_256.String():
return hash.SHA2_256
case hash.SHA3_256.String():
return hash.SHA3_256
default:
return hash.UnknownHashingAlgorithm
}
}
// verifySignatureFromRuntime is an adapter that performs signature verification using
// raw values provided by the Cadence runtime.
func verifySignatureFromRuntime(
verifier SignatureVerifier,
signature []byte,
rawTag string,
message []byte,
rawPublicKey []byte,
rawSigAlgo string,
rawHashAlgo string,
) (bool, error) {
sigAlgo := StringToSigningAlgorithm(rawSigAlgo)
hashAlgo := StringToHashingAlgorithm(rawHashAlgo)
publicKey, err := crypto.DecodePublicKey(sigAlgo, rawPublicKey)
if err != nil {
// TODO: improve error passing https://github.com/onflow/cadence/issues/202
return false, err
}
tag := parseRuntimeDomainTag(rawTag)
if tag == nil {
// TODO: improve error passing https://github.com/onflow/cadence/issues/202
return false, fmt.Errorf("invalid domain tag")
}
valid, err := verifier.Verify(
signature,
tag,
message,
publicKey,
hashAlgo,
)
if err != nil {
// TODO: improve error passing https://github.com/onflow/cadence/issues/202
return false, err
}
return valid, nil
}
const runtimeUserDomainTag = "user"
func parseRuntimeDomainTag(tag string) []byte {
if tag == runtimeUserDomainTag {
return flow.UserDomainTag[:]
}
return nil
}