|
| 1 | +package crypto |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "crypto/sha256" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + bbsg2 "github.com/hyperledger/aries-framework-go/pkg/crypto/primitive/bbs12381g2pub" |
| 10 | +) |
| 11 | + |
| 12 | +// GenerateBBSKeyPair https://w3c-ccg.github.io/ldp-bbs2020 |
| 13 | +func GenerateBBSKeyPair() (*bbsg2.PublicKey, *bbsg2.PrivateKey, error) { |
| 14 | + seed := make([]byte, 32) |
| 15 | + if _, err := rand.Read(seed); err != nil { |
| 16 | + return nil, nil, err |
| 17 | + } |
| 18 | + return bbsg2.GenerateKeyPair(sha256.New, seed) |
| 19 | +} |
| 20 | + |
| 21 | +type BBSPlusSigner struct { |
| 22 | + kid string |
| 23 | + *bbsg2.PrivateKey |
| 24 | + *bbsg2.PublicKey |
| 25 | + *BBSPlusVerifier |
| 26 | +} |
| 27 | + |
| 28 | +func NewBBSPlusSigner(kid string, privKey *bbsg2.PrivateKey) *BBSPlusSigner { |
| 29 | + pubKey := privKey.PublicKey() |
| 30 | + return &BBSPlusSigner{ |
| 31 | + kid: kid, |
| 32 | + PrivateKey: privKey, |
| 33 | + PublicKey: pubKey, |
| 34 | + BBSPlusVerifier: &BBSPlusVerifier{ |
| 35 | + KID: kid, |
| 36 | + PublicKey: pubKey, |
| 37 | + }, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (s *BBSPlusSigner) GetKeyID() string { |
| 42 | + return s.kid |
| 43 | +} |
| 44 | + |
| 45 | +func (s *BBSPlusSigner) Sign(message []byte) ([]byte, error) { |
| 46 | + bls := bbsg2.New() |
| 47 | + return bls.SignWithKey(prepareBBSMessage(message), s.PrivateKey) |
| 48 | +} |
| 49 | + |
| 50 | +func (s *BBSPlusSigner) SignMultiple(messages ...[]byte) ([]byte, error) { |
| 51 | + bls := bbsg2.New() |
| 52 | + return bls.SignWithKey(messages, s.PrivateKey) |
| 53 | +} |
| 54 | + |
| 55 | +func (s *BBSPlusSigner) GetVerifier() *BBSPlusVerifier { |
| 56 | + return s.BBSPlusVerifier |
| 57 | +} |
| 58 | + |
| 59 | +type BBSPlusVerifier struct { |
| 60 | + KID string |
| 61 | + *bbsg2.PublicKey |
| 62 | +} |
| 63 | + |
| 64 | +func NewBBSPlusVerifier(kid string, pubKey *bbsg2.PublicKey) *BBSPlusVerifier { |
| 65 | + return &BBSPlusVerifier{ |
| 66 | + KID: kid, |
| 67 | + PublicKey: pubKey, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func (v *BBSPlusVerifier) GetKeyID() string { |
| 72 | + return v.KID |
| 73 | +} |
| 74 | + |
| 75 | +func (v *BBSPlusVerifier) Verify(message, signature []byte) error { |
| 76 | + bls := bbsg2.New() |
| 77 | + pubKeyBytes, err := v.PublicKey.Marshal() |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + return bls.Verify(prepareBBSMessage(message), signature, pubKeyBytes) |
| 82 | +} |
| 83 | + |
| 84 | +// VerifyDerived verifies a derived proof, or a selective disclosure proof that has been derived from a |
| 85 | +// BBSPlusSignature signed object. |
| 86 | +func (v *BBSPlusVerifier) VerifyDerived(message, signature, nonce []byte) error { |
| 87 | + bls := bbsg2.New() |
| 88 | + pubKeyBytes, err := v.PublicKey.Marshal() |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + return bls.VerifyProof(prepareBBSDerivedMessage(message), signature, nonce, pubKeyBytes) |
| 93 | +} |
| 94 | + |
| 95 | +func (v *BBSPlusVerifier) VerifyMultiple(signature []byte, messages ...[]byte) error { |
| 96 | + bls := bbsg2.New() |
| 97 | + pubKeyBytes, err := v.PublicKey.Marshal() |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + return bls.Verify(messages, signature, pubKeyBytes) |
| 102 | +} |
| 103 | + |
| 104 | +func (v *BBSPlusVerifier) DeriveProof(messages [][]byte, sigBytes, nonce []byte, revealedIndexes []int) ([]byte, error) { |
| 105 | + bls := bbsg2.New() |
| 106 | + pubKeyBytes, err := v.PublicKey.Marshal() |
| 107 | + if err != nil { |
| 108 | + return nil, err |
| 109 | + } |
| 110 | + return bls.DeriveProof(messages, sigBytes, nonce, pubKeyBytes, revealedIndexes) |
| 111 | +} |
| 112 | + |
| 113 | +// Utility methods to be used without a signer |
| 114 | + |
| 115 | +func SignBBSMessage(privKey *bbsg2.PrivateKey, messages ...[]byte) ([]byte, error) { |
| 116 | + signer := BBSPlusSigner{ |
| 117 | + PrivateKey: privKey, |
| 118 | + } |
| 119 | + return signer.SignMultiple(messages...) |
| 120 | +} |
| 121 | + |
| 122 | +func VerifyBBSMessage(pubKey *bbsg2.PublicKey, signature, message []byte) error { |
| 123 | + verifier := BBSPlusVerifier{ |
| 124 | + PublicKey: pubKey, |
| 125 | + } |
| 126 | + return verifier.Verify(message, signature) |
| 127 | +} |
| 128 | + |
| 129 | +func VerifyDerivedBBSMessage(pubKey *bbsg2.PublicKey, signature, message, nonce []byte) error { |
| 130 | + verifier := BBSPlusVerifier{ |
| 131 | + PublicKey: pubKey, |
| 132 | + } |
| 133 | + return verifier.VerifyDerived(message, signature, nonce) |
| 134 | +} |
| 135 | + |
| 136 | +// helpers |
| 137 | + |
| 138 | +// prepareBBSMessage transforms a message into a message that can be used to verify a BBS+ signature |
| 139 | +// as per https://w3c-ccg.github.io/ldp-bbs2020/#create-verify-data-algorithm |
| 140 | +func prepareBBSMessage(msg []byte) [][]byte { |
| 141 | + rows := strings.Split(string(msg), "\n") |
| 142 | + msgs := make([][]byte, 0, len(rows)) |
| 143 | + for _, row := range rows { |
| 144 | + if strings.TrimSpace(row) == "" { |
| 145 | + continue |
| 146 | + } |
| 147 | + msgs = append(msgs, []byte(row)) |
| 148 | + } |
| 149 | + return msgs |
| 150 | +} |
| 151 | + |
| 152 | +// prepareBBSDerivedMessage transforms a message from a derived proof into a message that can be used |
| 153 | +// to verify the derived proof as per https://w3c-ccg.github.io/ldp-bbs2020/#create-verify-data-algorithm |
| 154 | +func prepareBBSDerivedMessage(msg []byte) [][]byte { |
| 155 | + rows := strings.Split(string(msg), "\n") |
| 156 | + msgs := make([][]byte, 0, len(rows)) |
| 157 | + for _, row := range rows { |
| 158 | + if strings.TrimSpace(row) == "" { |
| 159 | + continue |
| 160 | + } |
| 161 | + transformedRow := transformFromBlankNode(row) |
| 162 | + msgs = append(msgs, []byte(transformedRow)) |
| 163 | + } |
| 164 | + return msgs |
| 165 | +} |
| 166 | + |
| 167 | +// necessary to patch this version of the go JSON-LD library; taken from: |
| 168 | +// https://github.com/hyperledger/aries-framework-go/blob/02f80847168a99c8eb3baeaafcba8d0367bd9551/pkg/doc/signature/jsonld/processor.go#L453 |
| 169 | +func transformFromBlankNode(row string) string { |
| 170 | + // transform from "urn:bnid:_:c14n0" to "_:c14n0" |
| 171 | + const ( |
| 172 | + emptyNodePlaceholder = "<urn:bnid:_:c14n" |
| 173 | + emptyNodePrefixLen = 10 |
| 174 | + ) |
| 175 | + |
| 176 | + prefixIndex := strings.Index(row, emptyNodePlaceholder) |
| 177 | + if prefixIndex < 0 { |
| 178 | + return row |
| 179 | + } |
| 180 | + |
| 181 | + sepIndex := strings.Index(row[prefixIndex:], ">") |
| 182 | + if sepIndex < 0 { |
| 183 | + return row |
| 184 | + } |
| 185 | + |
| 186 | + sepIndex += prefixIndex |
| 187 | + |
| 188 | + prefix := row[:prefixIndex] |
| 189 | + blankNode := row[prefixIndex+emptyNodePrefixLen : sepIndex] |
| 190 | + suffix := row[sepIndex+1:] |
| 191 | + |
| 192 | + return fmt.Sprintf("%s%s%s", prefix, blankNode, suffix) |
| 193 | +} |
0 commit comments