-
Notifications
You must be signed in to change notification settings - Fork 5
/
round5.go
351 lines (275 loc) · 9.06 KB
/
round5.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package foundation
// #include <virgil/crypto/foundation/vscf_foundation_public.h>
import "C"
import unsafe "unsafe"
import "runtime"
/*
* Provide post-quantum encryption based on the round5 implementation.
* For algorithm details check https://github.com/round5/code
*/
type Round5 struct {
cCtx *C.vscf_round5_t /*ct10*/
}
func (obj *Round5) SetRandom(random Random) {
C.vscf_round5_release_random(obj.cCtx)
C.vscf_round5_use_random(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(random.Ctx())))
runtime.KeepAlive(random)
runtime.KeepAlive(obj)
}
/*
* Setup predefined values to the uninitialized class dependencies.
*/
func (obj *Round5) SetupDefaults() error {
proxyResult := /*pr4*/C.vscf_round5_setup_defaults(obj.cCtx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return err
}
runtime.KeepAlive(obj)
return nil
}
/*
* Generate new private key.
* Note, this operation might be slow.
*/
func (obj *Round5) GenerateKey(algId AlgId) (PrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_round5_generate_key(obj.cCtx, C.vscf_alg_id_t(algId) /*pa7*/, &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
return FoundationImplementationWrapPrivateKey(proxyResult) /* r4 */
}
/* Handle underlying C context. */
func (obj *Round5) Ctx() uintptr {
return uintptr(unsafe.Pointer(obj.cCtx))
}
func NewRound5() *Round5 {
ctx := C.vscf_round5_new()
obj := &Round5 {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Round5).Delete)
return obj
}
/* Acquire C context.
* Note. This method is used in generated code only, and SHOULD NOT be used in another way.
*/
func newRound5WithCtx(ctx *C.vscf_round5_t /*ct10*/) *Round5 {
obj := &Round5 {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Round5).Delete)
return obj
}
/* Acquire retained C context.
* Note. This method is used in generated code only, and SHOULD NOT be used in another way.
*/
func newRound5Copy(ctx *C.vscf_round5_t /*ct10*/) *Round5 {
obj := &Round5 {
cCtx: C.vscf_round5_shallow_copy(ctx),
}
runtime.SetFinalizer(obj, (*Round5).Delete)
return obj
}
/*
* Release underlying C context.
*/
func (obj *Round5) Delete() {
if obj == nil {
return
}
runtime.SetFinalizer(obj, nil)
obj.delete()
}
/*
* Release underlying C context.
*/
func (obj *Round5) delete() {
C.vscf_round5_delete(obj.cCtx)
}
/*
* Defines whether a public key can be imported or not.
*/
func (obj *Round5) GetCanImportPublicKey() bool {
return true
}
/*
* Define whether a public key can be exported or not.
*/
func (obj *Round5) GetCanExportPublicKey() bool {
return true
}
/*
* Define whether a private key can be imported or not.
*/
func (obj *Round5) GetCanImportPrivateKey() bool {
return true
}
/*
* Define whether a private key can be exported or not.
*/
func (obj *Round5) GetCanExportPrivateKey() bool {
return true
}
/*
* Generate ephemeral private key of the same type.
* Note, this operation might be slow.
*/
func (obj *Round5) GenerateEphemeralKey(key Key) (PrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_round5_generate_ephemeral_key(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(key.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(key)
return FoundationImplementationWrapPrivateKey(proxyResult) /* r4 */
}
/*
* Import public key from the raw binary format.
*
* Return public key that is adopted and optimized to be used
* with this particular algorithm.
*
* Binary format must be defined in the key specification.
* For instance, RSA public key must be imported from the format defined in
* RFC 3447 Appendix A.1.1.
*/
func (obj *Round5) ImportPublicKey(rawKey *RawPublicKey) (PublicKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_round5_import_public_key(obj.cCtx, (*C.vscf_raw_public_key_t)(unsafe.Pointer(rawKey.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(rawKey)
return FoundationImplementationWrapPublicKey(proxyResult) /* r4 */
}
/*
* Export public key to the raw binary format.
*
* Binary format must be defined in the key specification.
* For instance, RSA public key must be exported in format defined in
* RFC 3447 Appendix A.1.1.
*/
func (obj *Round5) ExportPublicKey(publicKey PublicKey) (*RawPublicKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_round5_export_public_key(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return newRawPublicKeyWithCtx(proxyResult) /* r6 */, nil
}
/*
* Import private key from the raw binary format.
*
* Return private key that is adopted and optimized to be used
* with this particular algorithm.
*
* Binary format must be defined in the key specification.
* For instance, RSA private key must be imported from the format defined in
* RFC 3447 Appendix A.1.2.
*/
func (obj *Round5) ImportPrivateKey(rawKey *RawPrivateKey) (PrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_round5_import_private_key(obj.cCtx, (*C.vscf_raw_private_key_t)(unsafe.Pointer(rawKey.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(rawKey)
return FoundationImplementationWrapPrivateKey(proxyResult) /* r4 */
}
/*
* Export private key in the raw binary format.
*
* Binary format must be defined in the key specification.
* For instance, RSA private key must be exported in format defined in
* RFC 3447 Appendix A.1.2.
*/
func (obj *Round5) ExportPrivateKey(privateKey PrivateKey) (*RawPrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_round5_export_private_key(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return newRawPrivateKeyWithCtx(proxyResult) /* r6 */, nil
}
/*
* Return length in bytes required to hold encapsulated shared key.
*/
func (obj *Round5) KemSharedKeyLen(key Key) uint {
proxyResult := /*pr4*/C.vscf_round5_kem_shared_key_len(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(key.Ctx())))
runtime.KeepAlive(obj)
runtime.KeepAlive(key)
return uint(proxyResult) /* r9 */
}
/*
* Return length in bytes required to hold encapsulated key.
*/
func (obj *Round5) KemEncapsulatedKeyLen(publicKey PublicKey) uint {
proxyResult := /*pr4*/C.vscf_round5_kem_encapsulated_key_len(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())))
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return uint(proxyResult) /* r9 */
}
/*
* Generate a shared key and a key encapsulated message.
*/
func (obj *Round5) KemEncapsulate(publicKey PublicKey) ([]byte, []byte, error) {
sharedKeyBuf, sharedKeyBufErr := newBuffer(int(obj.KemSharedKeyLen(publicKey.(Key)) /* lg2 */))
if sharedKeyBufErr != nil {
return nil, nil, sharedKeyBufErr
}
defer sharedKeyBuf.delete()
encapsulatedKeyBuf, encapsulatedKeyBufErr := newBuffer(int(obj.KemEncapsulatedKeyLen(publicKey.(PublicKey)) /* lg2 */))
if encapsulatedKeyBufErr != nil {
return nil, nil, encapsulatedKeyBufErr
}
defer encapsulatedKeyBuf.delete()
proxyResult := /*pr4*/C.vscf_round5_kem_encapsulate(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())), sharedKeyBuf.ctx, encapsulatedKeyBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return sharedKeyBuf.getData() /* r7 */, encapsulatedKeyBuf.getData() /* r7 */, nil
}
/*
* Decapsulate the shared key.
*/
func (obj *Round5) KemDecapsulate(encapsulatedKey []byte, privateKey PrivateKey) ([]byte, error) {
sharedKeyBuf, sharedKeyBufErr := newBuffer(int(obj.KemSharedKeyLen(privateKey.(Key)) /* lg2 */))
if sharedKeyBufErr != nil {
return nil, sharedKeyBufErr
}
defer sharedKeyBuf.delete()
encapsulatedKeyData := helperWrapData (encapsulatedKey)
proxyResult := /*pr4*/C.vscf_round5_kem_decapsulate(obj.cCtx, encapsulatedKeyData, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())), sharedKeyBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return sharedKeyBuf.getData() /* r7 */, nil
}