skf-rs 0.8.1

Rust wrapper for GM/T 0016-2012(Smart token cryptography application interface specification).
Documentation
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
use libloading::{Library, Symbol};
use std::sync::Arc;

/// Helper macro to define extern function type for each platform
#[macro_export]
macro_rules! define_extern_fn_type {
    ($vis:vis $name:ident = fn($($arg:ty),*) -> $ret:ty) => {
        #[cfg(all(target_os = "windows", target_arch = "x86"))]
        $vis type $name = $crate::engine::symbol::SymbolBundle<unsafe extern "stdcall" fn($($arg),*) -> $ret>;

        #[cfg(not(all(target_os = "windows", target_arch = "x86")))]
        $vis type $name = $crate::engine::symbol::SymbolBundle<unsafe extern "C" fn($($arg),*) -> $ret>;
    };
}

/// Symbol bundle with library pointer
pub struct SymbolBundle<T: 'static> {
    _lib: Arc<Library>,
    symbol: Symbol<'static, T>,
}
impl<T> SymbolBundle<T> {
    /// Get a pointer to a function or static variable by symbol name.
    pub unsafe fn new(
        lib: &Arc<Library>,
        sym: &[u8],
    ) -> Result<SymbolBundle<T>, libloading::Error> {
        let lc = lib.clone();
        unsafe {
            let symbol: Symbol<T> = lib.get(sym)?;
            let bundle = SymbolBundle {
                _lib: lc,
                symbol: std::mem::transmute(symbol),
            };
            Ok(bundle)
        }
    }
}
impl<T> std::ops::Deref for SymbolBundle<T> {
    type Target = Symbol<'static, T>;
    fn deref(&self) -> &Self::Target {
        &self.symbol
    }
}

#[allow(non_camel_case_types)]
pub(crate) mod device_fn {
    use crate::define_extern_fn_type;
    use skf_api::native::types::{DeviceInfo, BOOL, BYTE, CHAR, DWORD, HANDLE, LPSTR, ULONG};

    define_extern_fn_type!(pub(super) SKF_WaitForDevEvent = fn(*mut CHAR, *mut ULONG, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_CancelWaitForDevEvent = fn() -> ULONG);
    define_extern_fn_type!(pub(super) SKF_EnumDev = fn(BOOL, *mut CHAR, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GetDevState = fn(*const CHAR, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ConnectDev = fn(*const CHAR, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DisConnectDev = fn(HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_SetLabel = fn(HANDLE, *const CHAR) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GetDevInfo = fn(HANDLE, *mut DeviceInfo) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_LockDev = fn(HANDLE, ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_UnlockDev = fn(HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_Transmit = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ChangeDevAuthKey = fn(HANDLE, *const BYTE, ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DevAuth = fn(HANDLE, *const BYTE, ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_CreateApplication = fn(HANDLE, LPSTR, LPSTR, DWORD, LPSTR, DWORD, DWORD, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_OpenApplication = fn(HANDLE, LPSTR, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DeleteApplication = fn(HANDLE, LPSTR) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_EnumApplication = fn(HANDLE, *mut CHAR, *mut ULONG) -> ULONG);
}

#[allow(non_camel_case_types)]
pub(crate) mod app_fn {
    use crate::define_extern_fn_type;
    use skf_api::native::types::{FileAttribute, BOOL, BYTE, CHAR, HANDLE, LPSTR, ULONG};

    define_extern_fn_type!(pub(super) SKF_CloseApplication = fn(HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ClearSecureState = fn(HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_EnumFiles = fn(HANDLE, *mut CHAR, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_CreateFile = fn(HANDLE, LPSTR, ULONG, ULONG, ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DeleteFile = fn(HANDLE, LPSTR) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GetFileInfo = fn(HANDLE, LPSTR, *mut FileAttribute) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ReadFile = fn(HANDLE, LPSTR, ULONG, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_WriteFile = fn(HANDLE, LPSTR, ULONG, *const BYTE, ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ChangePIN = fn(HANDLE, ULONG, LPSTR, LPSTR, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GetPINInfo = fn(HANDLE, ULONG, *mut ULONG, *mut ULONG, *mut BOOL) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_VerifyPIN = fn(HANDLE, ULONG, LPSTR, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_UnblockPIN = fn(HANDLE, LPSTR, LPSTR, *mut ULONG) -> ULONG);
}

#[allow(non_camel_case_types)]
pub(crate) mod container_fn {
    use crate::define_extern_fn_type;
    use skf_api::native::types::{BOOL, BYTE, CHAR, HANDLE, LPSTR, ULONG};

    define_extern_fn_type!(pub(super) SKF_CreateContainer = fn(HANDLE, LPSTR, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DeleteContainer = fn(HANDLE, LPSTR) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_OpenContainer = fn(HANDLE, LPSTR, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_EnumContainer = fn(HANDLE, *mut CHAR, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_CloseContainer = fn(HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GetContainerType = fn(HANDLE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ImportCertificate = fn(HANDLE, BOOL, *const BYTE, ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ExportCertificate = fn(HANDLE, BOOL, *mut BYTE, *mut ULONG) -> ULONG);
}

#[allow(non_camel_case_types)]
pub(crate) mod crypto_fn {
    use crate::define_extern_fn_type;
    use skf_api::native::types::{
        BlockCipherParam, ECCCipherBlob, ECCPrivateKeyBlob, ECCPublicKeyBlob, ECCSignatureBlob,
        EnvelopedKeyBlob, BOOL, BYTE, HANDLE, ULONG,
    };

    define_extern_fn_type!(pub(crate) SKF_GenRandom = fn(HANDLE, *mut BYTE, ULONG) -> ULONG);
    define_extern_fn_type!(pub(crate) SKF_CloseHandle = fn(HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_SetSymmKey = fn(HANDLE, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_EncryptInit = fn(HANDLE, BlockCipherParam) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_Encrypt = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_EncryptUpdate = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_EncryptFinal = fn(HANDLE, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DecryptInit = fn(HANDLE, BlockCipherParam) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_Decrypt = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DecryptUpdate = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_DecryptFinal = fn(HANDLE, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ExtECCEncrypt = fn(HANDLE, *const ECCPublicKeyBlob, *const BYTE, ULONG, *mut ECCCipherBlob) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ExtECCDecrypt = fn(HANDLE, *const ECCPrivateKeyBlob, *const ECCCipherBlob, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ExtECCSign = fn(HANDLE, *const ECCPrivateKeyBlob, *const BYTE, ULONG, *mut ECCSignatureBlob) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ExtECCVerify = fn(HANDLE, *const ECCPublicKeyBlob, *const BYTE, ULONG, *const ECCSignatureBlob) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GenECCKeyPair = fn(HANDLE, ULONG, *mut ECCPublicKeyBlob) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ImportECCKeyPair = fn(HANDLE, *const EnvelopedKeyBlob) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ECCSignData = fn(HANDLE, *const BYTE, ULONG, *mut ECCSignatureBlob) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ECCVerify = fn(HANDLE, *const ECCPublicKeyBlob, *const BYTE, ULONG, *const ECCSignatureBlob) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ECCExportSessionKey = fn(HANDLE, ULONG, *const ECCPublicKeyBlob, *mut ECCCipherBlob, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GenerateAgreementDataWithECC = fn(HANDLE, ULONG, *mut ECCPublicKeyBlob, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GenerateAgreementDataAndKeyWithECC = fn(HANDLE, ULONG, *const ECCPublicKeyBlob, *const ECCPublicKeyBlob, *mut ECCPublicKeyBlob, *const BYTE, ULONG, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ExportPublicKey = fn(HANDLE, BOOL, *mut BYTE, *mut ULONG) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_ImportSessionKey = fn(HANDLE, ULONG, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
    define_extern_fn_type!(pub(super) SKF_GenerateKeyWithECC = fn(HANDLE, *const ECCPublicKeyBlob, *const ECCPublicKeyBlob, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
}

#[derive(Default)]
pub(crate) struct ModMag {
    pub enum_dev: Option<device_fn::SKF_EnumDev>,
    pub wait_plug_event: Option<device_fn::SKF_WaitForDevEvent>,
    pub cancel_wait_plug_event: Option<device_fn::SKF_CancelWaitForDevEvent>,
    pub get_dev_state: Option<device_fn::SKF_GetDevState>,
    pub connect_dev: Option<device_fn::SKF_ConnectDev>,
}

impl ModMag {
    pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
        let enum_dev = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumDev\0")? });
        let wait_plug_event = Some(unsafe { SymbolBundle::new(lib, b"SKF_WaitForDevEvent\0")? });
        let cancel_wait_plug_event =
            Some(unsafe { SymbolBundle::new(lib, b"SKF_CancelWaitForDevEvent\0")? });
        let get_dev_state = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetDevState\0")? });
        let connect_dev = Some(unsafe { SymbolBundle::new(lib, b"SKF_ConnectDev\0")? });
        let holder = Self {
            enum_dev,
            wait_plug_event,
            cancel_wait_plug_event,
            get_dev_state,
            connect_dev,
        };
        Ok(holder)
    }
}

#[derive(Default)]
pub(crate) struct ModDev {
    pub dev_set_label: Option<device_fn::SKF_SetLabel>,
    pub dev_dis_connect: Option<device_fn::SKF_DisConnectDev>,
    pub dev_get_info: Option<device_fn::SKF_GetDevInfo>,
    pub dev_lock: Option<device_fn::SKF_LockDev>,
    pub dev_unlock: Option<device_fn::SKF_UnlockDev>,
    pub dev_transmit: Option<device_fn::SKF_Transmit>,
    pub dev_auth: Option<device_fn::SKF_DevAuth>,
    pub dev_change_auth_key: Option<device_fn::SKF_ChangeDevAuthKey>,
    pub app_create: Option<device_fn::SKF_CreateApplication>,
    pub app_open: Option<device_fn::SKF_OpenApplication>,
    pub app_delete: Option<device_fn::SKF_DeleteApplication>,
    pub app_enum: Option<device_fn::SKF_EnumApplication>,
    pub gen_random: Option<crypto_fn::SKF_GenRandom>,
    pub sym_key_import: Option<crypto_fn::SKF_SetSymmKey>,
    pub ecc_ext_encrypt: Option<crypto_fn::SKF_ExtECCEncrypt>,
    pub ecc_ext_decrypt: Option<crypto_fn::SKF_ExtECCDecrypt>,
    pub ecc_ext_sign: Option<crypto_fn::SKF_ExtECCSign>,
    pub ecc_ext_verify: Option<crypto_fn::SKF_ExtECCVerify>,
    pub ecc_verify: Option<crypto_fn::SKF_ECCVerify>,
    pub ecc_gen_sk: Option<crypto_fn::SKF_GenerateKeyWithECC>,
}

impl ModDev {
    pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
        let dev_set_label = Some(unsafe { SymbolBundle::new(lib, b"SKF_SetLabel\0")? });
        let dev_dis_connect = Some(unsafe { SymbolBundle::new(lib, b"SKF_DisConnectDev\0")? });
        let dev_get_info = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetDevInfo\0")? });
        let dev_lock = Some(unsafe { SymbolBundle::new(lib, b"SKF_LockDev\0")? });
        let dev_unlock = Some(unsafe { SymbolBundle::new(lib, b"SKF_UnlockDev\0")? });
        let dev_transmit = Some(unsafe { SymbolBundle::new(lib, b"SKF_Transmit\0")? });
        let dev_auth = Some(unsafe { SymbolBundle::new(lib, b"SKF_DevAuth\0")? });
        let dev_change_auth_key =
            Some(unsafe { SymbolBundle::new(lib, b"SKF_ChangeDevAuthKey\0")? });
        let app_create = Some(unsafe { SymbolBundle::new(lib, b"SKF_CreateApplication\0")? });
        let app_open = Some(unsafe { SymbolBundle::new(lib, b"SKF_OpenApplication\0")? });
        let app_delete = Some(unsafe { SymbolBundle::new(lib, b"SKF_DeleteApplication\0")? });
        let app_enum = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumApplication\0")? });
        let gen_random = Some(unsafe { SymbolBundle::new(lib, b"SKF_GenRandom\0")? });
        let sym_key_import = Some(unsafe { SymbolBundle::new(lib, b"SKF_SetSymmKey\0")? });
        let ecc_ext_encrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCEncrypt\0")? });
        let ecc_ext_decrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCDecrypt\0")? });
        let ecc_ext_sign = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCSign\0")? });
        let ecc_ext_verify = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCVerify\0")? });
        let ecc_verify = Some(unsafe { SymbolBundle::new(lib, b"SKF_ECCVerify\0")? });
        let ecc_gen_sk = Some(unsafe { SymbolBundle::new(lib, b"SKF_GenerateKeyWithECC\0")? });

        let holder = Self {
            dev_set_label,
            dev_dis_connect,
            dev_get_info,
            dev_lock,
            dev_unlock,
            dev_transmit,
            dev_auth,
            dev_change_auth_key,
            app_create,
            app_open,
            app_delete,
            app_enum,
            gen_random,
            sym_key_import,
            ecc_ext_encrypt,
            ecc_ext_decrypt,
            ecc_ext_sign,
            ecc_ext_verify,
            ecc_verify,
            ecc_gen_sk,
        };
        Ok(holder)
    }
}

#[derive(Default)]
pub(crate) struct ModApp {
    pub app_close: Option<app_fn::SKF_CloseApplication>,
    pub app_clear_secure_state: Option<app_fn::SKF_ClearSecureState>,
    pub file_get_list: Option<app_fn::SKF_EnumFiles>,
    pub file_create: Option<app_fn::SKF_CreateFile>,
    pub file_delete: Option<app_fn::SKF_DeleteFile>,
    pub file_get_info: Option<app_fn::SKF_GetFileInfo>,
    pub file_read: Option<app_fn::SKF_ReadFile>,
    pub file_write: Option<app_fn::SKF_WriteFile>,
    pub container_get_list: Option<container_fn::SKF_EnumContainer>,
    pub container_create: Option<container_fn::SKF_CreateContainer>,
    pub container_delete: Option<container_fn::SKF_DeleteContainer>,
    pub container_open: Option<container_fn::SKF_OpenContainer>,
    pub pin_change: Option<app_fn::SKF_ChangePIN>,
    pub pin_get_info: Option<app_fn::SKF_GetPINInfo>,
    pub pin_verify: Option<app_fn::SKF_VerifyPIN>,
    pub pin_unblock: Option<app_fn::SKF_UnblockPIN>,
}

impl ModApp {
    pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
        let app_close = Some(unsafe { SymbolBundle::new(lib, b"SKF_CloseApplication\0")? });
        let app_clear_secure_state =
            Some(unsafe { SymbolBundle::new(lib, b"SKF_ClearSecureState\0")? });
        let file_get_list = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumFiles\0")? });
        let file_create = Some(unsafe { SymbolBundle::new(lib, b"SKF_CreateFile\0")? });
        let file_delete = Some(unsafe { SymbolBundle::new(lib, b"SKF_DeleteFile\0")? });
        let file_get_info = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetFileInfo\0")? });
        let file_read = Some(unsafe { SymbolBundle::new(lib, b"SKF_ReadFile\0")? });
        let file_write = Some(unsafe { SymbolBundle::new(lib, b"SKF_WriteFile\0")? });
        let container_get_list = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumContainer\0")? });
        let container_create = Some(unsafe { SymbolBundle::new(lib, b"SKF_CreateContainer\0")? });
        let container_delete = Some(unsafe { SymbolBundle::new(lib, b"SKF_DeleteContainer\0")? });
        let container_open = Some(unsafe { SymbolBundle::new(lib, b"SKF_OpenContainer\0")? });

        let pin_change = Some(unsafe { SymbolBundle::new(lib, b"SKF_ChangePIN\0")? });
        let pin_get_info = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetPINInfo\0")? });
        let pin_verify = Some(unsafe { SymbolBundle::new(lib, b"SKF_VerifyPIN\0")? });
        let pin_unblock = Some(unsafe { SymbolBundle::new(lib, b"SKF_UnblockPIN\0")? });

        let holder = Self {
            app_close,
            file_get_list,
            file_create,
            file_delete,
            file_get_info,
            file_read,
            file_write,
            container_get_list,
            container_create,
            container_delete,
            container_open,
            app_clear_secure_state,
            pin_change,
            pin_get_info,
            pin_verify,
            pin_unblock,
        };
        Ok(holder)
    }
}

#[derive(Default)]
pub(crate) struct ModContainer {
    pub ct_close: Option<container_fn::SKF_CloseContainer>,
    pub ct_get_type: Option<container_fn::SKF_GetContainerType>,
    pub ct_imp_cert: Option<container_fn::SKF_ImportCertificate>,
    pub ct_exp_cert: Option<container_fn::SKF_ExportCertificate>,
    pub ct_ecc_gen_pair: Option<crypto_fn::SKF_GenECCKeyPair>,
    pub ct_ecc_imp_pair: Option<crypto_fn::SKF_ImportECCKeyPair>,
    pub ct_ecc_sign: Option<crypto_fn::SKF_ECCSignData>,
    pub ct_sk_gen_agreement: Option<crypto_fn::SKF_GenerateAgreementDataWithECC>,
    pub ct_sk_gen_agreement_and_key: Option<crypto_fn::SKF_GenerateAgreementDataAndKeyWithECC>,
    pub ct_ecc_exp_pub_key: Option<crypto_fn::SKF_ExportPublicKey>,
    pub ct_sk_imp: Option<crypto_fn::SKF_ImportSessionKey>,
    pub ct_sk_exp: Option<crypto_fn::SKF_ECCExportSessionKey>,
}

impl ModContainer {
    pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
        let ct_close = Some(unsafe { SymbolBundle::new(lib, b"SKF_CloseContainer\0")? });
        let ct_get_type = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetContainerType\0")? });
        let ct_imp_cert = Some(unsafe { SymbolBundle::new(lib, b"SKF_ImportCertificate\0")? });
        let ct_exp_cert = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExportCertificate\0")? });
        let ct_ecc_gen_pair = Some(unsafe { SymbolBundle::new(lib, b"SKF_GenECCKeyPair\0")? });
        let ct_ecc_imp_pair = Some(unsafe { SymbolBundle::new(lib, b"SKF_ImportECCKeyPair\0")? });
        let ct_ecc_sign = Some(unsafe { SymbolBundle::new(lib, b"SKF_ECCSignData\0")? });
        let ct_sk_gen_agreement =
            Some(unsafe { SymbolBundle::new(lib, b"SKF_GenerateAgreementDataWithECC\0")? });
        let ct_sk_gen_agreement_and_key =
            Some(unsafe { SymbolBundle::new(lib, b"SKF_GenerateAgreementDataAndKeyWithECC\0")? });
        let ct_ecc_exp_pub_key = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExportPublicKey\0")? });
        let ct_sk_imp = Some(unsafe { SymbolBundle::new(lib, b"SKF_ImportSessionKey\0")? });
        let ct_sk_exp = Some(unsafe { SymbolBundle::new(lib, b"SKF_ECCExportSessionKey\0")? });
        let holder = Self {
            ct_close,
            ct_get_type,
            ct_imp_cert,
            ct_exp_cert,
            ct_ecc_gen_pair,
            ct_ecc_imp_pair,
            ct_ecc_sign,
            ct_sk_gen_agreement,
            ct_sk_gen_agreement_and_key,
            ct_ecc_exp_pub_key,
            ct_sk_imp,
            ct_sk_exp,
        };
        Ok(holder)
    }
}

#[derive(Default)]
pub(crate) struct ModBlockCipher {
    pub encrypt_init: Option<crypto_fn::SKF_EncryptInit>,
    pub encrypt: Option<crypto_fn::SKF_Encrypt>,
    pub encrypt_update: Option<crypto_fn::SKF_EncryptUpdate>,
    pub encrypt_final: Option<crypto_fn::SKF_EncryptFinal>,
    pub decrypt_init: Option<crypto_fn::SKF_DecryptInit>,
    pub decrypt: Option<crypto_fn::SKF_Decrypt>,
    pub decrypt_update: Option<crypto_fn::SKF_DecryptUpdate>,
    pub decrypt_final: Option<crypto_fn::SKF_DecryptFinal>,
}

impl ModBlockCipher {
    pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
        let encrypt_init = Some(unsafe { SymbolBundle::new(lib, b"SKF_EncryptInit\0")? });
        let encrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_Encrypt\0")? });
        let encrypt_update = Some(unsafe { SymbolBundle::new(lib, b"SKF_EncryptUpdate\0")? });
        let encrypt_final = Some(unsafe { SymbolBundle::new(lib, b"SKF_EncryptFinal\0")? });

        let decrypt_init = Some(unsafe { SymbolBundle::new(lib, b"SKF_DecryptInit\0")? });
        let decrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_Decrypt\0")? });
        let decrypt_update = Some(unsafe { SymbolBundle::new(lib, b"SKF_DecryptUpdate\0")? });
        let decrypt_final = Some(unsafe { SymbolBundle::new(lib, b"SKF_DecryptFinal\0")? });
        let holder = Self {
            encrypt_init,
            encrypt,
            encrypt_update,
            encrypt_final,
            decrypt_init,
            decrypt,
            decrypt_update,
            decrypt_final,
        };
        Ok(holder)
    }
}
#[cfg(test)]
mod test {
    use super::*;
    use crate::LibLoader;

    #[test]
    #[ignore]
    fn sym_mod_ctl_test() {
        let lib = Arc::new(LibLoader::env_lookup().unwrap());
        assert!(ModMag::load_symbols(&lib).is_ok());
    }

    #[test]
    #[ignore]
    fn sym_mod_dev_test() {
        let lib = Arc::new(LibLoader::env_lookup().unwrap());
        assert!(ModDev::load_symbols(&lib).is_ok());
    }
}