github.com/true-sqn/fabric@v2.1.1+incompatible/bccsp/idemix/bccsp_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 package idemix_test 7 8 import ( 9 "crypto/rand" 10 11 "github.com/hyperledger/fabric/bccsp" 12 "github.com/hyperledger/fabric/bccsp/idemix" 13 "github.com/hyperledger/fabric/bccsp/sw" 14 . "github.com/onsi/ginkgo" 15 . "github.com/onsi/gomega" 16 ) 17 18 var _ = Describe("Idemix Bridge", func() { 19 20 Describe("setting up the environment with one issuer and one user", func() { 21 var ( 22 CSP bccsp.BCCSP 23 IssuerKey bccsp.Key 24 IssuerPublicKey bccsp.Key 25 AttributeNames []string 26 27 UserKey bccsp.Key 28 NymKey bccsp.Key 29 NymPublicKey bccsp.Key 30 31 IssuerNonce []byte 32 credRequest []byte 33 34 credential []byte 35 36 RevocationKey bccsp.Key 37 RevocationPublicKey bccsp.Key 38 cri []byte 39 ) 40 41 BeforeEach(func() { 42 var err error 43 CSP, err = idemix.New(sw.NewDummyKeyStore()) 44 Expect(err).NotTo(HaveOccurred()) 45 46 // Issuer 47 AttributeNames = []string{"Attr1", "Attr2", "Attr3", "Attr4", "Attr5"} 48 IssuerKey, err = CSP.KeyGen(&bccsp.IdemixIssuerKeyGenOpts{Temporary: true, AttributeNames: AttributeNames}) 49 Expect(err).NotTo(HaveOccurred()) 50 IssuerPublicKey, err = IssuerKey.PublicKey() 51 Expect(err).NotTo(HaveOccurred()) 52 53 // User 54 UserKey, err = CSP.KeyGen(&bccsp.IdemixUserSecretKeyGenOpts{Temporary: true}) 55 Expect(err).NotTo(HaveOccurred()) 56 57 // User Nym Key 58 NymKey, err = CSP.KeyDeriv(UserKey, &bccsp.IdemixNymKeyDerivationOpts{Temporary: true, IssuerPK: IssuerPublicKey}) 59 Expect(err).NotTo(HaveOccurred()) 60 NymPublicKey, err = NymKey.PublicKey() 61 Expect(err).NotTo(HaveOccurred()) 62 63 IssuerNonce = make([]byte, 32) 64 n, err := rand.Read(IssuerNonce) 65 Expect(n).To(BeEquivalentTo(32)) 66 Expect(err).NotTo(HaveOccurred()) 67 68 // Credential Request for User 69 credRequest, err = CSP.Sign( 70 UserKey, 71 nil, 72 &bccsp.IdemixCredentialRequestSignerOpts{IssuerPK: IssuerPublicKey, IssuerNonce: IssuerNonce}, 73 ) 74 Expect(err).NotTo(HaveOccurred()) 75 76 // Credential 77 credential, err = CSP.Sign( 78 IssuerKey, 79 credRequest, 80 &bccsp.IdemixCredentialSignerOpts{ 81 Attributes: []bccsp.IdemixAttribute{ 82 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0}}, 83 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0, 1}}, 84 {Type: bccsp.IdemixIntAttribute, Value: 1}, 85 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0, 1, 2}}, 86 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0, 1, 2, 3}}, 87 }, 88 }, 89 ) 90 Expect(err).NotTo(HaveOccurred()) 91 92 // Revocation 93 RevocationKey, err = CSP.KeyGen(&bccsp.IdemixRevocationKeyGenOpts{Temporary: true}) 94 Expect(err).NotTo(HaveOccurred()) 95 RevocationPublicKey, err = RevocationKey.PublicKey() 96 Expect(err).NotTo(HaveOccurred()) 97 98 // CRI 99 cri, err = CSP.Sign( 100 RevocationKey, 101 nil, 102 &bccsp.IdemixCRISignerOpts{}, 103 ) 104 Expect(err).NotTo(HaveOccurred()) 105 106 }) 107 108 It("the environment is properly set", func() { 109 // Verify CredRequest 110 valid, err := CSP.Verify( 111 IssuerPublicKey, 112 credRequest, 113 nil, 114 &bccsp.IdemixCredentialRequestSignerOpts{IssuerNonce: IssuerNonce}, 115 ) 116 Expect(err).NotTo(HaveOccurred()) 117 Expect(valid).To(BeTrue()) 118 119 // Verify Credential 120 valid, err = CSP.Verify( 121 UserKey, 122 credential, 123 nil, 124 &bccsp.IdemixCredentialSignerOpts{ 125 IssuerPK: IssuerPublicKey, 126 Attributes: []bccsp.IdemixAttribute{ 127 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0}}, 128 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0, 1}}, 129 {Type: bccsp.IdemixIntAttribute, Value: 1}, 130 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0, 1, 2}}, 131 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0, 1, 2, 3}}, 132 }, 133 }, 134 ) 135 Expect(err).NotTo(HaveOccurred()) 136 Expect(valid).To(BeTrue()) 137 138 // Verify CRI 139 valid, err = CSP.Verify( 140 RevocationPublicKey, 141 cri, 142 nil, 143 &bccsp.IdemixCRISignerOpts{}, 144 ) 145 Expect(err).NotTo(HaveOccurred()) 146 Expect(valid).To(BeTrue()) 147 }) 148 149 Describe("producing an idemix signature with no disclosed attribute", func() { 150 var ( 151 digest []byte 152 signature []byte 153 ) 154 155 BeforeEach(func() { 156 var err error 157 158 digest = []byte("a digest") 159 160 signature, err = CSP.Sign( 161 UserKey, 162 digest, 163 &bccsp.IdemixSignerOpts{ 164 Credential: credential, 165 Nym: NymKey, 166 IssuerPK: IssuerPublicKey, 167 Attributes: []bccsp.IdemixAttribute{ 168 {Type: bccsp.IdemixHiddenAttribute}, 169 {Type: bccsp.IdemixHiddenAttribute}, 170 {Type: bccsp.IdemixHiddenAttribute}, 171 {Type: bccsp.IdemixHiddenAttribute}, 172 {Type: bccsp.IdemixHiddenAttribute}, 173 }, 174 RhIndex: 4, 175 Epoch: 0, 176 CRI: cri, 177 }, 178 ) 179 Expect(err).NotTo(HaveOccurred()) 180 }) 181 182 It("the signature is valid", func() { 183 valid, err := CSP.Verify( 184 IssuerPublicKey, 185 signature, 186 digest, 187 &bccsp.IdemixSignerOpts{ 188 RevocationPublicKey: RevocationPublicKey, 189 Attributes: []bccsp.IdemixAttribute{ 190 {Type: bccsp.IdemixHiddenAttribute}, 191 {Type: bccsp.IdemixHiddenAttribute}, 192 {Type: bccsp.IdemixHiddenAttribute}, 193 {Type: bccsp.IdemixHiddenAttribute}, 194 {Type: bccsp.IdemixHiddenAttribute}, 195 }, 196 RhIndex: 4, 197 Epoch: 0, 198 }, 199 ) 200 Expect(err).NotTo(HaveOccurred()) 201 Expect(valid).To(BeTrue()) 202 }) 203 204 }) 205 206 Describe("producing an idemix signature with disclosed attributes", func() { 207 var ( 208 digest []byte 209 signature []byte 210 ) 211 212 BeforeEach(func() { 213 var err error 214 215 digest = []byte("a digest") 216 217 signature, err = CSP.Sign( 218 UserKey, 219 digest, 220 &bccsp.IdemixSignerOpts{ 221 Credential: credential, 222 Nym: NymKey, 223 IssuerPK: IssuerPublicKey, 224 Attributes: []bccsp.IdemixAttribute{ 225 {Type: bccsp.IdemixBytesAttribute}, 226 {Type: bccsp.IdemixHiddenAttribute}, 227 {Type: bccsp.IdemixIntAttribute}, 228 {Type: bccsp.IdemixHiddenAttribute}, 229 {Type: bccsp.IdemixHiddenAttribute}, 230 }, 231 RhIndex: 4, 232 Epoch: 0, 233 CRI: cri, 234 }, 235 ) 236 Expect(err).NotTo(HaveOccurred()) 237 }) 238 239 It("the signature is valid", func() { 240 valid, err := CSP.Verify( 241 IssuerPublicKey, 242 signature, 243 digest, 244 &bccsp.IdemixSignerOpts{ 245 RevocationPublicKey: RevocationPublicKey, 246 Attributes: []bccsp.IdemixAttribute{ 247 {Type: bccsp.IdemixBytesAttribute, Value: []byte{0}}, 248 {Type: bccsp.IdemixHiddenAttribute}, 249 {Type: bccsp.IdemixIntAttribute, Value: 1}, 250 {Type: bccsp.IdemixHiddenAttribute}, 251 {Type: bccsp.IdemixHiddenAttribute}, 252 }, 253 RhIndex: 4, 254 Epoch: 0, 255 }, 256 ) 257 Expect(err).NotTo(HaveOccurred()) 258 Expect(valid).To(BeTrue()) 259 }) 260 261 }) 262 263 Describe("producing an idemix nym signature", func() { 264 var ( 265 digest []byte 266 signature []byte 267 ) 268 269 BeforeEach(func() { 270 var err error 271 272 digest = []byte("a digest") 273 274 signature, err = CSP.Sign( 275 UserKey, 276 digest, 277 &bccsp.IdemixNymSignerOpts{ 278 Nym: NymKey, 279 IssuerPK: IssuerPublicKey, 280 }, 281 ) 282 Expect(err).NotTo(HaveOccurred()) 283 }) 284 285 It("the signature is valid", func() { 286 valid, err := CSP.Verify( 287 NymPublicKey, 288 signature, 289 digest, 290 &bccsp.IdemixNymSignerOpts{ 291 IssuerPK: IssuerPublicKey, 292 }, 293 ) 294 Expect(err).NotTo(HaveOccurred()) 295 Expect(valid).To(BeTrue()) 296 }) 297 298 }) 299 }) 300 })