forked from TBD54566975/ssi-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto.go
executable file
·215 lines (182 loc) · 6.55 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package modena
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
sdkcrypto "github.com/extrimian/ssi-sdk/crypto/jwx"
"github.com/goccy/go-json"
"github.com/gowebpki/jcs"
"github.com/multiformats/go-multihash"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// HashEncode hashes given data according to the protocol's hashing process
// https://identity.foundation/sidetree/spec/#hashing-process
func HashEncode(data []byte) (string, error) {
hashed, err := Multihash(data)
if err != nil {
return "", err
}
return Encode(hashed), nil
}
// Hash hashes given data according to the protocol's hashing process; not multihashed
func Hash(data []byte) []byte {
hashed := sha256.Sum256(data)
return hashed[:]
}
// Multihash https://multiformats.io/multihash/
func Multihash(data []byte) ([]byte, error) {
// first hash using the given hashing algorithm
hashed := sha256.Sum256(data)
// next encode as a mulithash
multiHashed, err := multihash.Encode(hashed[:], multihash.SHA2_256)
if err != nil {
logrus.WithError(err).Error("could not multi-hash the given data")
return nil, err
}
return multiHashed, nil
}
// Encode encodes according to the encoding scheme of the sidetree spec
func Encode(data []byte) string {
return base64.RawURLEncoding.EncodeToString(data)
}
// EncodeString encodes a string according to the encoding scheme of the sidetree spec
func EncodeString(data string) string {
return Encode([]byte(data))
}
// EncodeAny encodes any according to the encoding scheme of the sidetree spec
func EncodeAny(data any) (string, error) {
anyBytes, err := json.Marshal(data)
if err != nil {
return "", err
}
return Encode(anyBytes), nil
}
// Decode decodes according to the encoding scheme of the sidetree spec
func Decode(data string) ([]byte, error) {
return base64.RawURLEncoding.DecodeString(data)
}
// Canonicalize transforms JSON according to the protocol's JSON Canonicalization Scheme
// https://identity.foundation/sidetree/spec/#json-canonicalization-scheme
func Canonicalize(data []byte) ([]byte, error) {
return jcs.Transform(data)
}
// CanonicalizeAny transforms JSON according to the protocol's JSON Canonicalization Scheme
// https://identity.foundation/sidetree/spec/#json-canonicalization-scheme
func CanonicalizeAny(data any) ([]byte, error) {
anyBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
return Canonicalize(anyBytes)
}
// Commit creates a public key commitment according to the steps defined in the protocol
// https://identity.foundation/sidetree/spec/#public-key-commitment-scheme
func Commit(key sdkcrypto.PublicKeyJWK) (reveal, commitment string, err error) {
// 1. Encode the public key into the form of a valid JWK.
// 2. Canonicalize the JWK encoded public key using the implementation’s JSON_CANONICALIZATION_SCHEME.
canonicalKey, err := CanonicalizeAny(key)
if err != nil {
logrus.WithError(err).Error("could not canonicalize JWK")
return "", "", err
}
// 3. Use the implementation’s HASH_PROTOCOL to Multihash the canonicalized public key to generate the REVEAL_VALUE,
// then Multihash the resulting Multihash value again using the implementation’s HASH_PROTOCOL to produce
// the public key commitment.
intermediateHash := Hash(canonicalKey)
reveal, err = HashEncode(canonicalKey)
if err != nil {
return "", "", errors.Wrap(err, "generating reveal value")
}
commitment, err = HashEncode(intermediateHash)
if err != nil {
return "", "", errors.Wrap(err, "generating commitment value")
}
return reveal, commitment, nil
}
type BTCSignerVerifier struct {
publicKey *btcec.PublicKey
privateKey *btcec.PrivateKey
}
// NewBTCSignerVerifier creates a new signer/verifier for signatures suited for the BTC blockchain
func NewBTCSignerVerifier(privateKey sdkcrypto.PrivateKeyJWK) (*BTCSignerVerifier, error) {
privateKeyBytes, err := Decode(privateKey.D)
if err != nil {
return nil, errors.Wrap(err, "constructing BTC signer/verifier")
}
privKey, pubKey := btcec.PrivKeyFromBytes(privateKeyBytes)
return &BTCSignerVerifier{
publicKey: pubKey,
privateKey: privKey,
}, nil
}
// GetJWSHeader returns the default JWS header for the BTC signer
func (*BTCSignerVerifier) GetJWSHeader() map[string]any {
return map[string]any{
"alg": "ES256K",
}
}
// Sign signs the given data according to Bitcoin's signing process
func (sv *BTCSignerVerifier) Sign(dataHash []byte) ([]byte, error) {
signature, err := ecdsa.SignCompact(sv.privateKey, dataHash, false)
if err != nil {
return nil, err
}
rBytes := signature[1:33]
sBytes := signature[33:65]
// Convert the signature from DER format to 64-byte hexadecimal format
r := fmt.Sprintf("%064s", hex.EncodeToString(rBytes))
s := fmt.Sprintf("%064s", hex.EncodeToString(sBytes))
// convert to bytes and return
return hex.DecodeString(r + s)
}
// Verify verifies the given data according to Bitcoin's verification process
func (sv *BTCSignerVerifier) Verify(data, signature []byte) (bool, error) {
r := new(secp256k1.ModNScalar)
r.SetBytes((*[32]byte)(signature[:32]))
s := new(secp256k1.ModNScalar)
s.SetBytes((*[32]byte)(signature[32:]))
// Reconstruct the signature once we have the R and S values
reconstructedSignature := ecdsa.NewSignature(r, s)
return reconstructedSignature.Verify(data, sv.publicKey), nil
}
// SignJWT signs the given data according to the protocol's JWT signing process,
// creating a compact JWS in a JWT
func (sv *BTCSignerVerifier) SignJWT(data any) (string, error) {
encodedHeader, err := EncodeAny(sv.GetJWSHeader())
if err != nil {
return "", err
}
encodedPayload, err := EncodeAny(data)
if err != nil {
return "", err
}
signingContent := encodedHeader + "." + encodedPayload
contentHash := Hash([]byte(signingContent))
signature, err := sv.Sign(contentHash)
if err != nil {
return "", nil
}
encodedSignature := Encode(signature)
compactJWS := encodedHeader + "." + encodedPayload + "." + encodedSignature
return compactJWS, nil
}
// VerifyJWS verifies the given data according to the protocol's JWS verification process
func (sv *BTCSignerVerifier) VerifyJWS(jws string) (bool, error) {
jwsParts := strings.Split(jws, ".")
if len(jwsParts) != 3 {
return false, errors.Errorf("invalid JWS: %s", jws)
}
signingContent := jwsParts[0] + "." + jwsParts[1]
contentHash := Hash([]byte(signingContent))
decodedSignature, err := Decode(jwsParts[2])
if err != nil {
return false, errors.Wrap(err, "decoding signature")
}
return sv.Verify(contentHash, decodedSignature)
}