-
Notifications
You must be signed in to change notification settings - Fork 5
/
hybrid_key_alg.go
473 lines (362 loc) · 12.9 KB
/
hybrid_key_alg.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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
package foundation
// #include <virgil/crypto/foundation/vscf_foundation_public.h>
import "C"
import unsafe "unsafe"
import "runtime"
/*
* Implements public key cryptography over hybrid keys.
* Hybrid encryption - TODO
* Hybrid signatures - TODO
*/
type HybridKeyAlg struct {
cCtx *C.vscf_hybrid_key_alg_t /*ct10*/
}
func (obj *HybridKeyAlg) SetRandom(random Random) {
C.vscf_hybrid_key_alg_release_random(obj.cCtx)
C.vscf_hybrid_key_alg_use_random(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(random.Ctx())))
runtime.KeepAlive(random)
runtime.KeepAlive(obj)
}
func (obj *HybridKeyAlg) SetCipher(cipher CipherAuth) {
C.vscf_hybrid_key_alg_release_cipher(obj.cCtx)
C.vscf_hybrid_key_alg_use_cipher(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(cipher.Ctx())))
runtime.KeepAlive(cipher)
runtime.KeepAlive(obj)
}
func (obj *HybridKeyAlg) SetHash(hash Hash) {
C.vscf_hybrid_key_alg_release_hash(obj.cCtx)
C.vscf_hybrid_key_alg_use_hash(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(hash.Ctx())))
runtime.KeepAlive(hash)
runtime.KeepAlive(obj)
}
/*
* Setup predefined values to the uninitialized class dependencies.
*/
func (obj *HybridKeyAlg) SetupDefaults() error {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_setup_defaults(obj.cCtx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return err
}
runtime.KeepAlive(obj)
return nil
}
/*
* Make hybrid private key from given keys.
*/
func (obj *HybridKeyAlg) MakeKey(firstKey PrivateKey, secondKey PrivateKey) (PrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_make_key(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(firstKey.Ctx())), (*C.vscf_impl_t)(unsafe.Pointer(secondKey.Ctx())), &error)
err := FoundationErrorHandleStatus(error.status)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(firstKey)
runtime.KeepAlive(secondKey)
return FoundationImplementationWrapPrivateKey(proxyResult) /* r4 */
}
/* Handle underlying C context. */
func (obj *HybridKeyAlg) Ctx() uintptr {
return uintptr(unsafe.Pointer(obj.cCtx))
}
func NewHybridKeyAlg() *HybridKeyAlg {
ctx := C.vscf_hybrid_key_alg_new()
obj := &HybridKeyAlg {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*HybridKeyAlg).Delete)
return obj
}
/* Acquire C context.
* Note. This method is used in generated code only, and SHOULD NOT be used in another way.
*/
func newHybridKeyAlgWithCtx(ctx *C.vscf_hybrid_key_alg_t /*ct10*/) *HybridKeyAlg {
obj := &HybridKeyAlg {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*HybridKeyAlg).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 newHybridKeyAlgCopy(ctx *C.vscf_hybrid_key_alg_t /*ct10*/) *HybridKeyAlg {
obj := &HybridKeyAlg {
cCtx: C.vscf_hybrid_key_alg_shallow_copy(ctx),
}
runtime.SetFinalizer(obj, (*HybridKeyAlg).Delete)
return obj
}
/*
* Release underlying C context.
*/
func (obj *HybridKeyAlg) Delete() {
if obj == nil {
return
}
runtime.SetFinalizer(obj, nil)
obj.delete()
}
/*
* Release underlying C context.
*/
func (obj *HybridKeyAlg) delete() {
C.vscf_hybrid_key_alg_delete(obj.cCtx)
}
/*
* Defines whether a public key can be imported or not.
*/
func (obj *HybridKeyAlg) GetCanImportPublicKey() bool {
return true
}
/*
* Define whether a public key can be exported or not.
*/
func (obj *HybridKeyAlg) GetCanExportPublicKey() bool {
return true
}
/*
* Define whether a private key can be imported or not.
*/
func (obj *HybridKeyAlg) GetCanImportPrivateKey() bool {
return true
}
/*
* Define whether a private key can be exported or not.
*/
func (obj *HybridKeyAlg) GetCanExportPrivateKey() bool {
return true
}
/*
* Generate ephemeral private key of the same type.
* Note, this operation might be slow.
*/
func (obj *HybridKeyAlg) GenerateEphemeralKey(key Key) (PrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_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 *HybridKeyAlg) ImportPublicKey(rawKey *RawPublicKey) (PublicKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_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 *HybridKeyAlg) ExportPublicKey(publicKey PublicKey) (*RawPublicKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_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 *HybridKeyAlg) ImportPrivateKey(rawKey *RawPrivateKey) (PrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_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 *HybridKeyAlg) ExportPrivateKey(privateKey PrivateKey) (*RawPrivateKey, error) {
var error C.vscf_error_t
C.vscf_error_reset(&error)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_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
}
/*
* Check if algorithm can encrypt data with a given key.
*/
func (obj *HybridKeyAlg) CanEncrypt(publicKey PublicKey, dataLen uint) bool {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_can_encrypt(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())), (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return bool(proxyResult) /* r9 */
}
/*
* Calculate required buffer length to hold the encrypted data.
*/
func (obj *HybridKeyAlg) EncryptedLen(publicKey PublicKey, dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_encrypted_len(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())), (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return uint(proxyResult) /* r9 */
}
/*
* Encrypt data with a given public key.
*/
func (obj *HybridKeyAlg) Encrypt(publicKey PublicKey, data []byte) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.EncryptedLen(publicKey.(PublicKey), uint(len(data))) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
dataData := helperWrapData (data)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_encrypt(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())), dataData, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return outBuf.getData() /* r7 */, nil
}
/*
* Check if algorithm can decrypt data with a given key.
* However, success result of decryption is not guaranteed.
*/
func (obj *HybridKeyAlg) CanDecrypt(privateKey PrivateKey, dataLen uint) bool {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_can_decrypt(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())), (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return bool(proxyResult) /* r9 */
}
/*
* Calculate required buffer length to hold the decrypted data.
*/
func (obj *HybridKeyAlg) DecryptedLen(privateKey PrivateKey, dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_decrypted_len(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())), (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return uint(proxyResult) /* r9 */
}
/*
* Decrypt given data.
*/
func (obj *HybridKeyAlg) Decrypt(privateKey PrivateKey, data []byte) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.DecryptedLen(privateKey.(PrivateKey), uint(len(data))) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
dataData := helperWrapData (data)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_decrypt(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())), dataData, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return outBuf.getData() /* r7 */, nil
}
/*
* Check if algorithm can sign data digest with a given key.
*/
func (obj *HybridKeyAlg) CanSign(privateKey PrivateKey) bool {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_can_sign(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())))
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return bool(proxyResult) /* r9 */
}
/*
* Return length in bytes required to hold signature.
* Return zero if a given private key can not produce signatures.
*/
func (obj *HybridKeyAlg) SignatureLen(privateKey PrivateKey) uint {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_signature_len(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())))
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return uint(proxyResult) /* r9 */
}
/*
* Sign data digest with a given private key.
*/
func (obj *HybridKeyAlg) SignHash(privateKey PrivateKey, hashId AlgId, digest []byte) ([]byte, error) {
signatureBuf, signatureBufErr := newBuffer(int(obj.SignatureLen(privateKey.(PrivateKey)) /* lg2 */))
if signatureBufErr != nil {
return nil, signatureBufErr
}
defer signatureBuf.delete()
digestData := helperWrapData (digest)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_sign_hash(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(privateKey.Ctx())), C.vscf_alg_id_t(hashId) /*pa7*/, digestData, signatureBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(privateKey)
return signatureBuf.getData() /* r7 */, nil
}
/*
* Check if algorithm can verify data digest with a given key.
*/
func (obj *HybridKeyAlg) CanVerify(publicKey PublicKey) bool {
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_can_verify(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())))
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return bool(proxyResult) /* r9 */
}
/*
* Verify data digest with a given public key and signature.
*/
func (obj *HybridKeyAlg) VerifyHash(publicKey PublicKey, hashId AlgId, digest []byte, signature []byte) bool {
digestData := helperWrapData (digest)
signatureData := helperWrapData (signature)
proxyResult := /*pr4*/C.vscf_hybrid_key_alg_verify_hash(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(publicKey.Ctx())), C.vscf_alg_id_t(hashId) /*pa7*/, digestData, signatureData)
runtime.KeepAlive(obj)
runtime.KeepAlive(publicKey)
return bool(proxyResult) /* r9 */
}