-
Notifications
You must be signed in to change notification settings - Fork 0
/
cred.go
159 lines (116 loc) · 3.29 KB
/
cred.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
package ecdaa
import (
"crypto/rsa"
"fmt"
"github.com/akakou-fork/amcl-go/miracl/core"
"github.com/akakou-fork/amcl-go/miracl/core/FP256BN"
"github.com/akakou/ecdaa/tpm_utils"
amcl_utils "github.com/akakou/fp256bn-amcl-utils"
legacy "github.com/google/go-tpm/legacy/tpm2"
)
type Credential struct {
A, B, C, D *FP256BN.ECP
}
type CredentialCipher struct {
A, C []byte
WrappedCredential []byte
IdObject []byte
EncA []byte
EncC []byte
IV []byte
}
/**
* Step3. make credential for join (by Issuer)
*/
func (issuer *Issuer) MakeCred(req *JoinRequest, B *FP256BN.ECP, rng *core.RAND) (*Credential, error) {
var cred Credential
invY := FP256BN.NewBIGcopy(issuer.Isk.Y)
invY.Invmodp(amcl_utils.P())
cred.A = B.Mul(invY)
cred.C = FP256BN.NewECP()
cred.C.Copy(cred.A)
cred.C.Add(req.Q)
cred.C = cred.C.Mul(issuer.Isk.X)
cred.B = B
cred.D = req.Q
return &cred, nil
}
func (issuer *Issuer) MakeCredEncrypted(req *JoinRequestTPM, B *FP256BN.ECP, rng *core.RAND) (*CredentialCipher, *Credential, error) {
var credCipher CredentialCipher
secret := amcl_utils.RandomBytes(rng, 16)
iv := amcl_utils.RandomBytes(rng, 16)
cred, err := issuer.MakeCred(req.JoinReq, B, rng)
if err != nil {
return nil, nil, fmt.Errorf("enc cred: %v", err)
}
ABuf := amcl_utils.EcpToBytes(cred.A)
CBuf := amcl_utils.EcpToBytes(cred.C)
credCipher.A, credCipher.C, err = tpm_utils.EncCredAES(ABuf, CBuf, secret, iv)
if err != nil {
return nil, nil, fmt.Errorf("enc cred: %v", err)
}
aikName := legacy.HashValue{
Alg: legacy.AlgSHA256,
Value: req.SrkName,
}
pub := req.EKCert.PublicKey.(*rsa.PublicKey)
credCipher.IdObject, credCipher.WrappedCredential, err = tpm_utils.MakeCred(&aikName, pub, 16, secret)
if err != nil {
return nil, nil, fmt.Errorf("enc cred: %v", err)
}
credCipher.IV = iv
return &credCipher, cred, nil
}
func VerifyCred(cred *Credential, ipk *IPK) error {
tmp := FP256BN.NewECP()
tmp.Copy(cred.A)
tmp.Add(cred.D)
a := FP256BN.Ate(ipk.Y, cred.A)
b := FP256BN.Ate(amcl_utils.G2(), cred.B)
a = FP256BN.Fexp(a)
b = FP256BN.Fexp(b)
if !a.Equals(b) {
return fmt.Errorf("Ate(ipk.Y, cred.A) != Ate(g2(), cred.B)")
}
c := FP256BN.Ate(amcl_utils.G2(), cred.C)
d := FP256BN.Ate(ipk.X, tmp)
c = FP256BN.Fexp(c)
d = FP256BN.Fexp(d)
if !c.Equals(d) {
return fmt.Errorf("Ate(g2(), cred.C) != Ate(ipk.X, tmp)")
}
return nil
}
/**
* Step4. activate credential for join with TPM2_activate_credential (by Member)
*/
func ActivateCredential(
encCred *CredentialCipher,
B, D *FP256BN.ECP,
ipk *IPK,
handle *KeyHandles,
tpm *tpm_utils.TPM) (*Credential, error) {
secret, err := (*tpm).ActivateCredential(handle.EkHandle, handle.SrkHandle, encCred.IdObject, encCred.WrappedCredential)
if err != nil {
return nil, err
}
decA, decC, err := tpm_utils.DecCredAES(encCred.A, encCred.C, secret, encCred.IV)
if err != nil {
return nil, err
}
A := FP256BN.ECP_fromBytes(decA)
C := FP256BN.ECP_fromBytes(decC)
cred := Credential{
A, B, C, D,
}
return &cred, nil
}
func RandomizeCred(cred *Credential, rng *core.RAND) *Credential {
var randomized Credential
l := FP256BN.Random(rng)
randomized.A = cred.A.Mul(l)
randomized.B = cred.B.Mul(l)
randomized.C = cred.C.Mul(l)
randomized.D = cred.D.Mul(l)
return &randomized
}