-
Notifications
You must be signed in to change notification settings - Fork 175
/
crypto.go
181 lines (157 loc) · 4.85 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package fvm
import (
"fmt"
"github.com/onflow/cadence/runtime"
"github.com/onflow/flow-go/crypto"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/model/flow"
)
const runtimeUserDomainTag = "user"
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, errors.NewValueErrorf(hashAlgo.String(), "hashing algorithm type not found")
}
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()
case hash.SHA2_384:
return hash.NewSHA2_384()
case hash.SHA3_384:
return hash.NewSHA3_384()
}
return nil
}
// RuntimeToCryptoSigningAlgorithm converts a runtime signature algorithm to a crypto signature algorithm.
func RuntimeToCryptoSigningAlgorithm(s runtime.SignatureAlgorithm) crypto.SigningAlgorithm {
switch s {
case runtime.SignatureAlgorithmECDSA_P256:
return crypto.ECDSAP256
case runtime.SignatureAlgorithmECDSA_secp256k1:
return crypto.ECDSASecp256k1
default:
return crypto.UnknownSigningAlgorithm
}
}
// CryptoToRuntimeSigningAlgorithm converts a crypto signature algorithm to a runtime signature algorithm.
func CryptoToRuntimeSigningAlgorithm(s crypto.SigningAlgorithm) runtime.SignatureAlgorithm {
switch s {
case crypto.ECDSAP256:
return runtime.SignatureAlgorithmECDSA_P256
case crypto.ECDSASecp256k1:
return runtime.SignatureAlgorithmECDSA_secp256k1
default:
return runtime.SignatureAlgorithmUnknown
}
}
// RuntimeToCryptoHashingAlgorithm converts a runtime hash algorithm to a crypto hashing algorithm.
func RuntimeToCryptoHashingAlgorithm(s runtime.HashAlgorithm) hash.HashingAlgorithm {
switch s {
case runtime.HashAlgorithmSHA2_256:
return hash.SHA2_256
case runtime.HashAlgorithmSHA3_256:
return hash.SHA3_256
case runtime.HashAlgorithmSHA2_384:
return hash.SHA2_384
case runtime.HashAlgorithmSHA3_384:
return hash.SHA3_384
default:
return hash.UnknownHashingAlgorithm
}
}
// CryptoToRuntimeHashingAlgorithm converts a crypto hashing algorithm to a runtime hash algorithm.
func CryptoToRuntimeHashingAlgorithm(h hash.HashingAlgorithm) runtime.HashAlgorithm {
switch h {
case hash.SHA2_256:
return runtime.HashAlgorithmSHA2_256
case hash.SHA3_256:
return runtime.HashAlgorithmSHA3_256
case hash.SHA2_384:
return runtime.HashAlgorithmSHA2_384
case hash.SHA3_384:
return runtime.HashAlgorithmSHA3_384
default:
return runtime.HashAlgorithmUnknown
}
}
// 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,
signatureAlgorithm runtime.SignatureAlgorithm,
hashAlgorithm runtime.HashAlgorithm,
) (bool, error) {
sigAlgo := RuntimeToCryptoSigningAlgorithm(signatureAlgorithm)
if sigAlgo == crypto.UnknownSigningAlgorithm {
return false, errors.NewValueErrorf(signatureAlgorithm.Name(), "signature algorithm type not found")
}
if sigAlgo == crypto.BLSBLS12381 {
return false, errors.NewValueErrorf(signatureAlgorithm.Name(), "signature algorithm type %s not supported", crypto.BLSBLS12381.String())
}
hashAlgo := RuntimeToCryptoHashingAlgorithm(hashAlgorithm)
if hashAlgo == hash.UnknownHashingAlgorithm {
return false, errors.NewValueErrorf(hashAlgorithm.Name(), "hashing algorithm type not found")
}
if hashAlgo == hash.KMAC128 {
return false, errors.NewValueErrorf(signatureAlgorithm.Name(), "hashing algorithm %s not supported", hash.KMAC128.String())
}
publicKey, err := crypto.DecodePublicKey(sigAlgo, rawPublicKey)
if err != nil {
return false, errors.NewValueErrorf(string(rawPublicKey), "cannot decode public key: %w", err)
}
tag := parseRuntimeDomainTag(rawTag)
if tag == nil {
return false, errors.NewValueErrorf(string(rawTag), "invalid domain tag")
}
valid, err := verifier.Verify(
signature,
tag,
message,
publicKey,
hashAlgo,
)
if err != nil {
return false, err
}
return valid, nil
}
func parseRuntimeDomainTag(tag string) []byte {
if tag == runtimeUserDomainTag {
return flow.UserDomainTag[:]
}
return nil
}