/
bbsplussignaturesuite.go
327 lines (275 loc) · 10.4 KB
/
bbsplussignaturesuite.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package cryptosuite
import (
gocrypto "crypto"
"encoding/base64"
"github.com/TBD54566975/ssi-sdk/crypto"
. "github.com/TBD54566975/ssi-sdk/util"
"github.com/goccy/go-json"
"github.com/pkg/errors"
)
const (
BBSSecurityContext string = "https://w3c.github.io/vc-di-bbs/contexts/v1"
BBSPlusSignature2020 SignatureType = "BbsBlsSignature2020"
BBSPlusSignatureSuiteID string = "https://w3c-ccg.github.io/ldp-bbs2020/#the-bbs-signature-suite-2020"
BBSPlusSignatureSuiteType LDKeyType = BLS12381G2Key2020
BBSPlusSignatureSuiteCanonicalizationAlgorithm string = "https://w3id.org/security#URDNA2015"
// BBSPlusSignatureSuiteDigestAlgorithm uses https://www.rfc-editor.org/rfc/rfc4634
BBSPlusSignatureSuiteDigestAlgorithm gocrypto.Hash = gocrypto.BLAKE2b_384
)
type BBSPlusSignatureSuite struct{}
func GetBBSPlusSignatureSuite() CryptoSuite {
return new(BBSPlusSignatureSuite)
}
// CryptoSuiteInfo interface
var _ CryptoSuiteInfo = (*BBSPlusSignatureSuite)(nil)
func (BBSPlusSignatureSuite) ID() string {
return BBSPlusSignatureSuiteID
}
func (BBSPlusSignatureSuite) Type() LDKeyType {
return BBSPlusSignatureSuiteType
}
func (BBSPlusSignatureSuite) CanonicalizationAlgorithm() string {
return BBSPlusSignatureSuiteCanonicalizationAlgorithm
}
func (BBSPlusSignatureSuite) MessageDigestAlgorithm() gocrypto.Hash {
return BBSPlusSignatureSuiteDigestAlgorithm
}
func (BBSPlusSignatureSuite) SignatureAlgorithm() SignatureType {
return BBSPlusSignature2020
}
func (BBSPlusSignatureSuite) RequiredContexts() []string {
return []string{BBSSecurityContext}
}
func (b BBSPlusSignatureSuite) Sign(s Signer, p Provable) error {
// create proof before running the create verify hash algorithm
// TODO(gabe) support required reveal values
proof := b.createProof(s.GetKeyID(), s.GetProofPurpose(), nil)
// prepare proof options
contexts, err := GetContextsFromProvable(p)
if err != nil {
return errors.Wrap(err, "getting contexts from provable")
}
// make sure the suite's context(s) are included
contexts = ensureRequiredContexts(contexts, b.RequiredContexts())
opts := &ProofOptions{Contexts: contexts}
// 3. tbs value as a result of create verify hash
var genericProvable map[string]any
pBytes, err := json.Marshal(p)
if err != nil {
return errors.Wrap(err, "marshaling provable")
}
if err = json.Unmarshal(pBytes, &genericProvable); err != nil {
return errors.Wrap(err, "unmarshaling provable")
}
tbs, err := b.CreateVerifyHash(genericProvable, proof, opts)
if err != nil {
return errors.Wrap(err, "running create verify hash algorithm")
}
// 4 & 5. create the signature over the provable data as a BBS+ signature
proofValue, err := s.Sign(tbs)
if err != nil {
return errors.Wrap(err, "signing provable value")
}
// set the signature on the proof object and return
proof.SetProofValue(base64.RawStdEncoding.EncodeToString(proofValue))
genericProof := crypto.Proof(proof)
p.SetProof(&genericProof)
return nil
}
func (b BBSPlusSignatureSuite) prepareProof(proof crypto.Proof, opts *ProofOptions) (*crypto.Proof, error) {
proofBytes, err := json.Marshal(proof)
if err != nil {
return nil, err
}
var genericProof map[string]any
if err = json.Unmarshal(proofBytes, &genericProof); err != nil {
return nil, err
}
// must make sure there is no proof value before signing/verifying
delete(genericProof, "proofValue")
// make sure the proof has a timestamp
created, ok := genericProof["created"]
if !ok || created == "" {
genericProof["created"] = GetRFC3339Timestamp()
}
var contexts []any
if opts != nil {
contexts = opts.Contexts
} else {
// if none provided, make sure the proof has a context value for this suite
contexts = ArrayStrToInterface(b.RequiredContexts())
}
genericProof["@context"] = contexts
p := crypto.Proof(genericProof)
return &p, nil
}
func (b BBSPlusSignatureSuite) Verify(v Verifier, p Provable) error {
proof := p.GetProof()
gotProof, err := BBSPlusProofFromGenericProof(*proof)
if err != nil {
return errors.Wrap(err, "coercing proof into BBSPlusSignature2020Proof proof")
}
// remove proof before verifying
p.SetProof(nil)
// make sure we set it back after we're done verifying
defer p.SetProof(proof)
// remove the proof value in the proof before verification
signatureValue, err := decodeProofValue(gotProof.ProofValue)
if err != nil {
return errors.Wrap(err, "decoding proof value")
}
gotProof.SetProofValue("")
// prepare proof options
contexts, err := GetContextsFromProvable(p)
if err != nil {
return errors.Wrap(err, "getting contexts from provable")
}
// make sure the suite's context(s) are included
contexts = ensureRequiredContexts(contexts, b.RequiredContexts())
opts := &ProofOptions{Contexts: contexts}
// run the create verify hash algorithm on both provable and the proof
var genericProvable map[string]any
pBytes, err := json.Marshal(p)
if err != nil {
return errors.Wrap(err, "marshaling provable")
}
if err = json.Unmarshal(pBytes, &genericProvable); err != nil {
return errors.Wrap(err, "unmarshaling provable")
}
tbv, err := b.CreateVerifyHash(genericProvable, gotProof, opts)
if err != nil {
return errors.Wrap(err, "running create verify hash algorithm")
}
if err = v.Verify(tbv, signatureValue); err != nil {
return errors.Wrap(err, "verifying BBS+ signature")
}
return nil
}
// decodeProofValue because the proof could have been encoded in a variety of manners we must try them all
// https://github.com/w3c-ccg/ldp-bbs2020/issues/16#issuecomment-1436148820
func decodeProofValue(proofValue string) ([]byte, error) {
signatureBytes, err := base64.RawStdEncoding.DecodeString(proofValue)
if err == nil {
return signatureBytes, nil
}
signatureBytes, err = base64.StdEncoding.DecodeString(proofValue)
if err == nil {
return signatureBytes, nil
}
return nil, errors.New("unknown encoding of proof value")
}
// CryptoSuiteProofType interface
var _ CryptoSuiteProofType = (*BBSPlusSignatureSuite)(nil)
func (BBSPlusSignatureSuite) Marshal(data any) ([]byte, error) {
// JSONify the provable object
jsonBytes, err := json.Marshal(data)
if err != nil {
return nil, err
}
return jsonBytes, nil
}
func (BBSPlusSignatureSuite) Canonicalize(marshaled []byte) (*string, error) {
// the LD library anticipates a generic golang json object to normalize
var generic map[string]any
if err := json.Unmarshal(marshaled, &generic); err != nil {
return nil, err
}
normalized, err := LDNormalize(generic)
if err != nil {
return nil, errors.Wrap(err, "ld normalizing")
}
canonicalString := normalized.(string)
return &canonicalString, nil
}
// CreateVerifyHash https://w3c-ccg.github.io/data-integrity-spec/#create-verify-hash-algorithm
// augmented by https://w3c-ccg.github.io/ldp-bbs2020/#create-verify-data-algorithm
func (b BBSPlusSignatureSuite) CreateVerifyHash(doc map[string]any, proof crypto.Proof, opts *ProofOptions) ([]byte, error) {
// first, make sure "created" exists in the proof and insert an LD context property for the proof vocabulary
preparedProof, err := b.prepareProof(proof, opts)
if err != nil {
return nil, errors.Wrap(err, "preparing proof for the create verify hash algorithm")
}
// marshal doc to prepare for canonicalizaiton
marshaledProvable, err := b.Marshal(doc)
if err != nil {
return nil, errors.Wrap(err, "marshalling doc")
}
// canonicalize doc using the suite's method
canonicalProvable, err := b.Canonicalize(marshaledProvable)
if err != nil {
return nil, errors.Wrap(err, "canonicalizing doc")
}
// marshal proof to prepare for canonicalizaiton
marshaledOptions, err := b.Marshal(preparedProof)
if err != nil {
return nil, errors.Wrap(err, "marshalling proof")
}
// 4.1 canonicalize proof using the suite's method
canonicalizedOptions, err := b.Canonicalize(marshaledOptions)
if err != nil {
return nil, errors.Wrap(err, "canonicalizing proof")
}
// 4.2 set output to the result of the hash of the canonicalized options document
canonicalizedOptionsBytes := []byte(*canonicalizedOptions)
optionsDigest, err := b.Digest(canonicalizedOptionsBytes)
if err != nil {
return nil, errors.Wrap(err, "taking digest of proof")
}
// 4.3 hash the canonicalized doc and append it to the output
canonicalDoc := []byte(*canonicalProvable)
documentDigest, err := b.Digest(canonicalDoc)
if err != nil {
return nil, errors.Wrap(err, "taking digest of doc")
}
// 5. return the output
output := append(optionsDigest, documentDigest...)
return output, nil
}
func (BBSPlusSignatureSuite) Digest(tbd []byte) ([]byte, error) {
// handled by the algorithm itself
return tbd, nil
}
func (b BBSPlusSignatureSuite) createProof(verificationMethod string, purpose ProofPurpose, requiredRevealStatements []int) BBSPlusSignature2020Proof {
return BBSPlusSignature2020Proof{
Type: b.SignatureAlgorithm(),
Created: GetRFC3339Timestamp(),
VerificationMethod: verificationMethod,
ProofPurpose: purpose,
RequiredRevealStatements: requiredRevealStatements,
}
}
type BBSPlusSignature2020Proof struct {
Type SignatureType `json:"type,omitempty"`
Created string `json:"created,omitempty"`
VerificationMethod string `json:"verificationMethod,omitempty"`
ProofPurpose ProofPurpose `json:"proofPurpose,omitempty"`
ProofValue string `json:"proofValue,omitempty"`
Nonce string `json:"nonce,omitempty"`
RequiredRevealStatements []int `json:"requiredRevealStatements,omitempty"`
}
func (b *BBSPlusSignature2020Proof) SetProofValue(proofValue string) {
b.ProofValue = proofValue
}
// BBSPlusProofFromGenericProof accepts either a slice with exactly one element, or a single element and creates a
// BBSPlusProofFromGenericProof by unmarshaling the JSON marshaled representation of the element found in `p`.
func BBSPlusProofFromGenericProof(p crypto.Proof) (*BBSPlusSignature2020Proof, error) {
// check if the proof is an array
if proofArray, ok := p.([]any); ok {
if len(proofArray) == 0 {
return nil, errors.New("expected at least one proof")
}
if len(proofArray) > 1 {
return nil, errors.New("expected only one proof")
}
p = proofArray[0]
}
proofBytes, err := json.Marshal(p)
if err != nil {
return nil, err
}
var result BBSPlusSignature2020Proof
if err = json.Unmarshal(proofBytes, &result); err != nil {
return nil, err
}
return &result, nil
}