...
1
2
3
4
5
6
7
8
9
10
11 package tkn20
12
13 import (
14 cryptoRand "crypto/rand"
15 "io"
16
17 "github.com/cloudflare/circl/abe/cpabe/tkn20/internal/dsl"
18 "github.com/cloudflare/circl/abe/cpabe/tkn20/internal/tkn"
19 )
20
21 type PublicKey struct {
22 pp tkn.PublicParams
23 }
24
25 func (p *PublicKey) MarshalBinary() ([]byte, error) {
26 return p.pp.MarshalBinary()
27 }
28
29 func (p *PublicKey) UnmarshalBinary(data []byte) error {
30 return p.pp.UnmarshalBinary(data)
31 }
32
33 func (p *PublicKey) Equal(p2 *PublicKey) bool {
34 return p.pp.Equal(&p2.pp)
35 }
36
37 func (p *PublicKey) Encrypt(rand io.Reader, policy Policy, msg []byte) ([]byte, error) {
38 if rand == nil {
39 rand = cryptoRand.Reader
40 }
41 return tkn.EncryptCCA(rand, &p.pp, &policy.policy, msg)
42 }
43
44 type SystemSecretKey struct {
45 sp tkn.SecretParams
46 }
47
48 func (msk *SystemSecretKey) MarshalBinary() ([]byte, error) {
49 return msk.sp.MarshalBinary()
50 }
51
52 func (msk *SystemSecretKey) UnmarshalBinary(data []byte) error {
53 return msk.sp.UnmarshalBinary(data)
54 }
55
56 func (msk *SystemSecretKey) Equal(msk2 *SystemSecretKey) bool {
57 return msk.sp.Equal(&msk2.sp)
58 }
59
60 func (msk *SystemSecretKey) KeyGen(rand io.Reader, attrs Attributes) (AttributeKey, error) {
61 if rand == nil {
62 rand = cryptoRand.Reader
63 }
64 sk, err := tkn.DeriveAttributeKeysCCA(rand, &msk.sp, &attrs.attrs)
65 if err != nil {
66 return AttributeKey{}, err
67 }
68 return AttributeKey{*sk}, nil
69 }
70
71 type AttributeKey struct {
72 ak tkn.AttributesKey
73 }
74
75 func (s *AttributeKey) MarshalBinary() ([]byte, error) {
76 return s.ak.MarshalBinary()
77 }
78
79 func (s *AttributeKey) UnmarshalBinary(data []byte) error {
80 return s.ak.UnmarshalBinary(data)
81 }
82
83 func (s *AttributeKey) Equal(s2 *AttributeKey) bool {
84 return s.ak.Equal(&s2.ak)
85 }
86
87 func (s *AttributeKey) Decrypt(ct []byte) ([]byte, error) {
88 return tkn.DecryptCCA(ct, &s.ak)
89 }
90
91 type Policy struct {
92 policy tkn.Policy
93 }
94
95 func (p *Policy) FromString(str string) error {
96 policy, err := dsl.Run(str)
97 if err != nil {
98 return err
99 }
100 p.policy = *policy
101 return nil
102 }
103
104 func (p *Policy) String() string {
105 return p.policy.String()
106 }
107
108 func (p *Policy) ExtractFromCiphertext(ct []byte) error {
109 return p.policy.ExtractFromCiphertext(ct)
110 }
111
112 func (p *Policy) ExtractAttributeValuePairs() map[string][]string {
113 pairs := make(map[string][]string, len(p.policy.Inputs))
114 for _, w := range p.policy.Inputs {
115 pairs[w.Label] = append(pairs[w.Label], w.RawValue)
116 }
117 return pairs
118 }
119
120 func (p *Policy) Equal(p2 *Policy) bool {
121 return p.policy.Equal(&p2.policy)
122 }
123
124 func (p *Policy) Satisfaction(a Attributes) bool {
125 _, err := p.policy.Satisfaction(&a.attrs)
126 return err == nil
127 }
128
129 type Attributes struct {
130 attrs tkn.Attributes
131 }
132
133 func (a *Attributes) Equal(a2 *Attributes) bool {
134 return a.attrs.Equal(&a2.attrs)
135 }
136
137 func (a *Attributes) CouldDecrypt(ciphertext []byte) bool {
138 return tkn.CouldDecrypt(ciphertext, &a.attrs)
139 }
140
141 func (a *Attributes) FromMap(in map[string]string) {
142 attrs := make(map[string]tkn.Attribute, len(in))
143 for k, v := range in {
144 attrs[k] = tkn.Attribute{
145 Value: tkn.HashStringToScalar(dsl.AttrHashKey, v),
146 }
147 }
148 a.attrs = attrs
149 }
150
151 func Setup(rand io.Reader) (PublicKey, SystemSecretKey, error) {
152 if rand == nil {
153 rand = cryptoRand.Reader
154 }
155 pp, sp, err := tkn.GenerateParams(rand)
156 if err != nil {
157 return PublicKey{}, SystemSecretKey{}, err
158 }
159 return PublicKey{*pp}, SystemSecretKey{*sp}, nil
160 }
161
View as plain text