-
Notifications
You must be signed in to change notification settings - Fork 0
/
pseudonymsys_ec_ca.go
192 lines (166 loc) · 5.34 KB
/
pseudonymsys_ec_ca.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
/*
* Copyright 2017 XLAB d.o.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package compat
import (
"math/big"
"fmt"
"github.com/emmyzkp/crypto/ec"
"github.com/emmyzkp/emmy/anauth/ecpsys"
)
// Representations of specific elliptic curves to be used in elliptic cryptography based schemes.
const (
P256 = int(ec.P256)
P224 = int(ec.P224)
P384 = int(ec.P384)
P521 = int(ec.P521)
)
// ECGroupElement represents an equivalent of ec.GroupElement, but has string
// field types to overcome type restrictions of Go language binding tools.
type ECGroupElement struct {
X string
Y string
}
func NewECGroupElement(x, y string) *ECGroupElement {
return &ECGroupElement{
X: x,
Y: y,
}
}
// getNativeType translates compatibility ECGroupElement to emmy's native ec.GroupElement.
func (e *ECGroupElement) getNativeType() (*ec.GroupElement, error) {
x, xOk := new(big.Int).SetString(e.X, 10)
y, yOk := new(big.Int).SetString(e.Y, 10)
if !xOk || !yOk {
return nil, ArgsConversionError
}
ecGroupEl := ec.NewGroupElement(x, y)
return ecGroupEl, nil
}
// PseudonymEC represents an equivalent of pseudonymsys.PseudonymEC,
// but has field types compatible with Go language binding tools.
type PseudonymEC struct {
A *ECGroupElement
B *ECGroupElement
}
func NewPseudonymEC(a, b *ECGroupElement) *PseudonymEC {
return &PseudonymEC{
A: a,
B: b,
}
}
// getNativeType translates compatibility PseudonymEC to emmy's native pseudonymsys.PseudonymEC.
func (p *PseudonymEC) getNativeType() (*ecpsys.Nym, error) {
a, err := p.A.getNativeType()
if err != nil {
return nil, fmt.Errorf("nym.A: %s", ArgsConversionError)
}
b, err := p.B.getNativeType()
if err != nil {
return nil, fmt.Errorf("nym.B: %s", ArgsConversionError)
}
pseudonym := ecpsys.NewNym(a, b)
return pseudonym, nil
}
// CACertificateEC represents an equivalent of pseudonymsys.CACertificateEC,
// but has field types compatible with Go language binding tools.
type CACertificateEC struct {
BlindedA *ECGroupElement
BlindedB *ECGroupElement
R string
S string
}
func NewCACertificateEC(bA, bB *ECGroupElement, r, s string) *CACertificateEC {
return &CACertificateEC{
BlindedA: bA,
BlindedB: bB,
R: r,
S: s,
}
}
// getNativeType translates compatibility CACertificateEC to emmy's native pseudonymsys.CACertificateEC.
func (c *CACertificateEC) getNativeType() (*ecpsys.CACert, error) {
blindedA, err := c.BlindedA.getNativeType()
if err != nil {
return nil, fmt.Errorf("cert.BlindedA: %s", ArgsConversionError)
}
blindedB, err := c.BlindedB.getNativeType()
if err != nil {
return nil, fmt.Errorf("cert.BlindedB: %s", ArgsConversionError)
}
r, rOk := new(big.Int).SetString(c.R, 10)
if !rOk {
return nil, fmt.Errorf("cert.R (%s): %s", c.R, ArgsConversionError)
}
s, sOk := new(big.Int).SetString(c.S, 10)
if !sOk {
return nil, fmt.Errorf("cert.S (%s): %s", c.S, ArgsConversionError)
}
certificate := ecpsys.NewCACert(blindedA, blindedB, r, s)
return certificate, nil
}
// PseudonymsysCAECClient wraps around client.CAClient to conform to
// type restrictions of Go language binding tools. It exposes the same set of methods as
// client.CAClient.
type PseudonymsysCAClientEC struct {
*ecpsys.CAClient
}
func NewPseudonymsysCAClientEC(curve int) *PseudonymsysCAClientEC {
return &PseudonymsysCAClientEC{
CAClient: ecpsys.NewCAClient(ec.Curve(curve)),
}
}
func (c *PseudonymsysCAClientEC) Connect(conn *Connection) {
c.CAClient.Connect(conn.ClientConn)
}
func (c *PseudonymsysCAClientEC) GenerateMasterNym(secret string, curve int) (*PseudonymEC,
error) {
// Translate secret
s, sOk := new(big.Int).SetString(secret, 10)
if !sOk {
return nil, fmt.Errorf("secret (%s): %s", secret, ArgsConversionError)
}
// Call CAClient client with translated parameters
masterNym := c.CAClient.GenerateMasterNym(s)
// Translate from native emmy types to compatibility types
a := NewECGroupElement(masterNym.A.X.String(), masterNym.A.Y.String())
b := NewECGroupElement(masterNym.B.X.String(), masterNym.B.Y.String())
return NewPseudonymEC(a, b), nil
}
func (c *PseudonymsysCAClientEC) GenerateCertificate(userSecret string,
nym *PseudonymEC) (*CACertificateEC, error) {
// Translate secret
secret, secretOk := new(big.Int).SetString(userSecret, 10)
if !secretOk {
return nil, fmt.Errorf("secret (%s): %s", secret, ArgsConversionError)
}
// Translate PseudonymEC
pseudonym, err := nym.getNativeType()
if err != nil {
return nil, err
}
// Call CAClient client with translated parameters
cert, err := c.CAClient.GenerateCertificate(secret, pseudonym)
if err != nil {
return nil, err
}
// Translate from native emmy types to compatibility types
return NewCACertificateEC(
NewECGroupElement(cert.BlindedA.X.String(), cert.BlindedA.Y.String()),
NewECGroupElement(cert.BlindedB.X.String(), cert.BlindedB.Y.String()),
cert.R.String(),
cert.S.String()), nil
}