-
Notifications
You must be signed in to change notification settings - Fork 5
/
aes256_gcm.go
502 lines (392 loc) · 12 KB
/
aes256_gcm.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
package foundation
// #include <virgil/crypto/foundation/vscf_foundation_public.h>
import "C"
import unsafe "unsafe"
import "runtime"
/*
* Implementation of the symmetric cipher AES-256 bit in a GCM mode.
* Note, this implementation contains dynamic memory allocations,
* this should be improved in the future releases.
*/
type Aes256Gcm struct {
cCtx *C.vscf_aes256_gcm_t /*ct10*/
}
/* Handle underlying C context. */
func (obj *Aes256Gcm) Ctx() uintptr {
return uintptr(unsafe.Pointer(obj.cCtx))
}
func NewAes256Gcm() *Aes256Gcm {
ctx := C.vscf_aes256_gcm_new()
obj := &Aes256Gcm {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Aes256Gcm).Delete)
return obj
}
/* Acquire C context.
* Note. This method is used in generated code only, and SHOULD NOT be used in another way.
*/
func newAes256GcmWithCtx(ctx *C.vscf_aes256_gcm_t /*ct10*/) *Aes256Gcm {
obj := &Aes256Gcm {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Aes256Gcm).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 newAes256GcmCopy(ctx *C.vscf_aes256_gcm_t /*ct10*/) *Aes256Gcm {
obj := &Aes256Gcm {
cCtx: C.vscf_aes256_gcm_shallow_copy(ctx),
}
runtime.SetFinalizer(obj, (*Aes256Gcm).Delete)
return obj
}
/*
* Release underlying C context.
*/
func (obj *Aes256Gcm) Delete() {
if obj == nil {
return
}
runtime.SetFinalizer(obj, nil)
obj.delete()
}
/*
* Release underlying C context.
*/
func (obj *Aes256Gcm) delete() {
C.vscf_aes256_gcm_delete(obj.cCtx)
}
/*
* Provide algorithm identificator.
*/
func (obj *Aes256Gcm) AlgId() AlgId {
proxyResult := /*pr4*/C.vscf_aes256_gcm_alg_id(obj.cCtx)
runtime.KeepAlive(obj)
return AlgId(proxyResult) /* r8 */
}
/*
* Produce object with algorithm information and configuration parameters.
*/
func (obj *Aes256Gcm) ProduceAlgInfo() (AlgInfo, error) {
proxyResult := /*pr4*/C.vscf_aes256_gcm_produce_alg_info(obj.cCtx)
runtime.KeepAlive(obj)
return FoundationImplementationWrapAlgInfo(proxyResult) /* r4 */
}
/*
* Restore algorithm configuration from the given object.
*/
func (obj *Aes256Gcm) RestoreAlgInfo(algInfo AlgInfo) error {
proxyResult := /*pr4*/C.vscf_aes256_gcm_restore_alg_info(obj.cCtx, (*C.vscf_impl_t)(unsafe.Pointer(algInfo.Ctx())))
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return err
}
runtime.KeepAlive(obj)
runtime.KeepAlive(algInfo)
return nil
}
/*
* Encrypt given data.
*/
func (obj *Aes256Gcm) Encrypt(data []byte) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.EncryptedLen(uint(len(data))) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
dataData := helperWrapData (data)
proxyResult := /*pr4*/C.vscf_aes256_gcm_encrypt(obj.cCtx, dataData, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, nil
}
/*
* Calculate required buffer length to hold the encrypted data.
*/
func (obj *Aes256Gcm) EncryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_encrypted_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Precise length calculation of encrypted data.
*/
func (obj *Aes256Gcm) PreciseEncryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_precise_encrypted_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Decrypt given data.
*/
func (obj *Aes256Gcm) Decrypt(data []byte) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.DecryptedLen(uint(len(data))) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
dataData := helperWrapData (data)
proxyResult := /*pr4*/C.vscf_aes256_gcm_decrypt(obj.cCtx, dataData, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, nil
}
/*
* Calculate required buffer length to hold the decrypted data.
*/
func (obj *Aes256Gcm) DecryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_decrypted_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Cipher nfonce length or IV length in bytes, or 0 if nonce is not required.
*/
func (obj *Aes256Gcm) GetNonceLen() uint {
return 12
}
/*
* Cipher key length in bytes.
*/
func (obj *Aes256Gcm) GetKeyLen() uint {
return 32
}
/*
* Cipher key length in bits.
*/
func (obj *Aes256Gcm) GetKeyBitlen() uint {
return 256
}
/*
* Cipher block length in bytes.
*/
func (obj *Aes256Gcm) GetBlockLen() uint {
return 16
}
/*
* Setup IV or nonce.
*/
func (obj *Aes256Gcm) SetNonce(nonce []byte) {
nonceData := helperWrapData (nonce)
C.vscf_aes256_gcm_set_nonce(obj.cCtx, nonceData)
runtime.KeepAlive(obj)
return
}
/*
* Set cipher encryption / decryption key.
*/
func (obj *Aes256Gcm) SetKey(key []byte) {
keyData := helperWrapData (key)
C.vscf_aes256_gcm_set_key(obj.cCtx, keyData)
runtime.KeepAlive(obj)
return
}
/*
* Start sequential encryption.
*/
func (obj *Aes256Gcm) StartEncryption() {
C.vscf_aes256_gcm_start_encryption(obj.cCtx)
runtime.KeepAlive(obj)
return
}
/*
* Start sequential decryption.
*/
func (obj *Aes256Gcm) StartDecryption() {
C.vscf_aes256_gcm_start_decryption(obj.cCtx)
runtime.KeepAlive(obj)
return
}
/*
* Process encryption or decryption of the given data chunk.
*/
func (obj *Aes256Gcm) Update(data []byte) []byte {
outBuf, outBufErr := newBuffer(int(obj.OutLen(uint(len(data))) /* lg2 */))
if outBufErr != nil {
return nil
}
defer outBuf.delete()
dataData := helperWrapData (data)
C.vscf_aes256_gcm_update(obj.cCtx, dataData, outBuf.ctx)
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */
}
/*
* Return buffer length required to hold an output of the methods
* "update" or "finish" in an current mode.
* Pass zero length to define buffer length of the method "finish".
*/
func (obj *Aes256Gcm) OutLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_out_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Return buffer length required to hold an output of the methods
* "update" or "finish" in an encryption mode.
* Pass zero length to define buffer length of the method "finish".
*/
func (obj *Aes256Gcm) EncryptedOutLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_encrypted_out_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Return buffer length required to hold an output of the methods
* "update" or "finish" in an decryption mode.
* Pass zero length to define buffer length of the method "finish".
*/
func (obj *Aes256Gcm) DecryptedOutLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_decrypted_out_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Accomplish encryption or decryption process.
*/
func (obj *Aes256Gcm) Finish() ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.OutLen(0) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
proxyResult := /*pr4*/C.vscf_aes256_gcm_finish(obj.cCtx, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, nil
}
/*
* Defines authentication tag length in bytes.
*/
func (obj *Aes256Gcm) GetAuthTagLen() uint {
return 16
}
/*
* Encrypt given data.
* If 'tag' is not given, then it will written to the 'enc'.
*/
func (obj *Aes256Gcm) AuthEncrypt(data []byte, authData []byte) ([]byte, []byte, error) {
outBuf, outBufErr := newBuffer(int(obj.AuthEncryptedLen(uint(len(data))) /* lg2 */))
if outBufErr != nil {
return nil, nil, outBufErr
}
defer outBuf.delete()
tagBuf, tagBufErr := newBuffer(int(obj.GetAuthTagLen() /* lg3 */))
if tagBufErr != nil {
return nil, nil, tagBufErr
}
defer tagBuf.delete()
dataData := helperWrapData (data)
authDataData := helperWrapData (authData)
proxyResult := /*pr4*/C.vscf_aes256_gcm_auth_encrypt(obj.cCtx, dataData, authDataData, outBuf.ctx, tagBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, tagBuf.getData() /* r7 */, nil
}
/*
* Calculate required buffer length to hold the authenticated encrypted data.
*/
func (obj *Aes256Gcm) AuthEncryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_auth_encrypted_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Decrypt given data.
* If 'tag' is not given, then it will be taken from the 'enc'.
*/
func (obj *Aes256Gcm) AuthDecrypt(data []byte, authData []byte, tag []byte) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.AuthDecryptedLen(uint(len(data))) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
dataData := helperWrapData (data)
authDataData := helperWrapData (authData)
tagData := helperWrapData (tag)
proxyResult := /*pr4*/C.vscf_aes256_gcm_auth_decrypt(obj.cCtx, dataData, authDataData, tagData, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, nil
}
/*
* Calculate required buffer length to hold the authenticated decrypted data.
*/
func (obj *Aes256Gcm) AuthDecryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_gcm_auth_decrypted_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Set additional data for for AEAD ciphers.
*/
func (obj *Aes256Gcm) SetAuthData(authData []byte) {
authDataData := helperWrapData (authData)
C.vscf_aes256_gcm_set_auth_data(obj.cCtx, authDataData)
runtime.KeepAlive(obj)
return
}
/*
* Accomplish an authenticated encryption and place tag separately.
*
* Note, if authentication tag should be added to an encrypted data,
* method "finish" can be used.
*/
func (obj *Aes256Gcm) FinishAuthEncryption() ([]byte, []byte, error) {
outBuf, outBufErr := newBuffer(int(obj.OutLen(0) /* lg2 */))
if outBufErr != nil {
return nil, nil, outBufErr
}
defer outBuf.delete()
tagBuf, tagBufErr := newBuffer(int(obj.GetAuthTagLen() /* lg3 */))
if tagBufErr != nil {
return nil, nil, tagBufErr
}
defer tagBuf.delete()
proxyResult := /*pr4*/C.vscf_aes256_gcm_finish_auth_encryption(obj.cCtx, outBuf.ctx, tagBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, tagBuf.getData() /* r7 */, nil
}
/*
* Accomplish an authenticated decryption with explicitly given tag.
*
* Note, if authentication tag is a part of an encrypted data then,
* method "finish" can be used for simplicity.
*/
func (obj *Aes256Gcm) FinishAuthDecryption(tag []byte) ([]byte, error) {
outBuf, outBufErr := newBuffer(int(obj.OutLen(0) /* lg2 */))
if outBufErr != nil {
return nil, outBufErr
}
defer outBuf.delete()
tagData := helperWrapData (tag)
proxyResult := /*pr4*/C.vscf_aes256_gcm_finish_auth_decryption(obj.cCtx, tagData, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, nil
}