-
Notifications
You must be signed in to change notification settings - Fork 5
/
aes256_cbc.go
345 lines (269 loc) · 7.76 KB
/
aes256_cbc.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
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 CBC mode.
* Note, this implementation contains dynamic memory allocations,
* this should be improved in the future releases.
*/
type Aes256Cbc struct {
cCtx *C.vscf_aes256_cbc_t /*ct10*/
}
/* Handle underlying C context. */
func (obj *Aes256Cbc) Ctx() uintptr {
return uintptr(unsafe.Pointer(obj.cCtx))
}
func NewAes256Cbc() *Aes256Cbc {
ctx := C.vscf_aes256_cbc_new()
obj := &Aes256Cbc {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Aes256Cbc).Delete)
return obj
}
/* Acquire C context.
* Note. This method is used in generated code only, and SHOULD NOT be used in another way.
*/
func newAes256CbcWithCtx(ctx *C.vscf_aes256_cbc_t /*ct10*/) *Aes256Cbc {
obj := &Aes256Cbc {
cCtx: ctx,
}
runtime.SetFinalizer(obj, (*Aes256Cbc).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 newAes256CbcCopy(ctx *C.vscf_aes256_cbc_t /*ct10*/) *Aes256Cbc {
obj := &Aes256Cbc {
cCtx: C.vscf_aes256_cbc_shallow_copy(ctx),
}
runtime.SetFinalizer(obj, (*Aes256Cbc).Delete)
return obj
}
/*
* Release underlying C context.
*/
func (obj *Aes256Cbc) Delete() {
if obj == nil {
return
}
runtime.SetFinalizer(obj, nil)
obj.delete()
}
/*
* Release underlying C context.
*/
func (obj *Aes256Cbc) delete() {
C.vscf_aes256_cbc_delete(obj.cCtx)
}
/*
* Provide algorithm identificator.
*/
func (obj *Aes256Cbc) AlgId() AlgId {
proxyResult := /*pr4*/C.vscf_aes256_cbc_alg_id(obj.cCtx)
runtime.KeepAlive(obj)
return AlgId(proxyResult) /* r8 */
}
/*
* Produce object with algorithm information and configuration parameters.
*/
func (obj *Aes256Cbc) ProduceAlgInfo() (AlgInfo, error) {
proxyResult := /*pr4*/C.vscf_aes256_cbc_produce_alg_info(obj.cCtx)
runtime.KeepAlive(obj)
return FoundationImplementationWrapAlgInfo(proxyResult) /* r4 */
}
/*
* Restore algorithm configuration from the given object.
*/
func (obj *Aes256Cbc) RestoreAlgInfo(algInfo AlgInfo) error {
proxyResult := /*pr4*/C.vscf_aes256_cbc_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 *Aes256Cbc) 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_cbc_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 *Aes256Cbc) EncryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_cbc_encrypted_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Precise length calculation of encrypted data.
*/
func (obj *Aes256Cbc) PreciseEncryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_cbc_precise_encrypted_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Decrypt given data.
*/
func (obj *Aes256Cbc) 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_cbc_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 *Aes256Cbc) DecryptedLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_cbc_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 *Aes256Cbc) GetNonceLen() uint {
return 16
}
/*
* Cipher key length in bytes.
*/
func (obj *Aes256Cbc) GetKeyLen() uint {
return 32
}
/*
* Cipher key length in bits.
*/
func (obj *Aes256Cbc) GetKeyBitlen() uint {
return 256
}
/*
* Cipher block length in bytes.
*/
func (obj *Aes256Cbc) GetBlockLen() uint {
return 16
}
/*
* Setup IV or nonce.
*/
func (obj *Aes256Cbc) SetNonce(nonce []byte) {
nonceData := helperWrapData (nonce)
C.vscf_aes256_cbc_set_nonce(obj.cCtx, nonceData)
runtime.KeepAlive(obj)
return
}
/*
* Set cipher encryption / decryption key.
*/
func (obj *Aes256Cbc) SetKey(key []byte) {
keyData := helperWrapData (key)
C.vscf_aes256_cbc_set_key(obj.cCtx, keyData)
runtime.KeepAlive(obj)
return
}
/*
* Start sequential encryption.
*/
func (obj *Aes256Cbc) StartEncryption() {
C.vscf_aes256_cbc_start_encryption(obj.cCtx)
runtime.KeepAlive(obj)
return
}
/*
* Start sequential decryption.
*/
func (obj *Aes256Cbc) StartDecryption() {
C.vscf_aes256_cbc_start_decryption(obj.cCtx)
runtime.KeepAlive(obj)
return
}
/*
* Process encryption or decryption of the given data chunk.
*/
func (obj *Aes256Cbc) 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_cbc_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 *Aes256Cbc) OutLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_cbc_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 *Aes256Cbc) EncryptedOutLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_cbc_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 *Aes256Cbc) DecryptedOutLen(dataLen uint) uint {
proxyResult := /*pr4*/C.vscf_aes256_cbc_decrypted_out_len(obj.cCtx, (C.size_t)(dataLen)/*pa10*/)
runtime.KeepAlive(obj)
return uint(proxyResult) /* r9 */
}
/*
* Accomplish encryption or decryption process.
*/
func (obj *Aes256Cbc) 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_cbc_finish(obj.cCtx, outBuf.ctx)
err := FoundationErrorHandleStatus(proxyResult)
if err != nil {
return nil, err
}
runtime.KeepAlive(obj)
return outBuf.getData() /* r7 */, nil
}