1use libloading::{Library, Symbol};
2use std::sync::Arc;
3
4#[macro_export]
6macro_rules! define_extern_fn_type {
7 ($vis:vis $name:ident = fn($($arg:ty),*) -> $ret:ty) => {
8 #[cfg(all(target_os = "windows", target_arch = "x86"))]
9 $vis type $name = $crate::engine::symbol::SymbolBundle<unsafe extern "stdcall" fn($($arg),*) -> $ret>;
10
11 #[cfg(not(all(target_os = "windows", target_arch = "x86")))]
12 $vis type $name = $crate::engine::symbol::SymbolBundle<unsafe extern "C" fn($($arg),*) -> $ret>;
13 };
14}
15
16pub struct SymbolBundle<T: 'static> {
18 _lib: Arc<Library>,
19 symbol: Symbol<'static, T>,
20}
21impl<T> SymbolBundle<T> {
22 pub unsafe fn new(
24 lib: &Arc<Library>,
25 sym: &[u8],
26 ) -> Result<SymbolBundle<T>, libloading::Error> {
27 let lc = lib.clone();
28 unsafe {
29 let symbol: Symbol<T> = lib.get(sym)?;
30 let bundle = SymbolBundle {
31 _lib: lc,
32 symbol: std::mem::transmute(symbol),
33 };
34 Ok(bundle)
35 }
36 }
37}
38impl<T> std::ops::Deref for SymbolBundle<T> {
39 type Target = Symbol<'static, T>;
40 fn deref(&self) -> &Self::Target {
41 &self.symbol
42 }
43}
44
45#[allow(non_camel_case_types)]
46pub(crate) mod device_fn {
47 use crate::define_extern_fn_type;
48 use skf_api::native::types::{DeviceInfo, BOOL, BYTE, CHAR, DWORD, HANDLE, LPSTR, ULONG};
49
50 define_extern_fn_type!(pub(super) SKF_WaitForDevEvent = fn(*mut CHAR, *mut ULONG, *mut ULONG) -> ULONG);
51 define_extern_fn_type!(pub(super) SKF_CancelWaitForDevEvent = fn() -> ULONG);
52 define_extern_fn_type!(pub(super) SKF_EnumDev = fn(BOOL, *mut CHAR, *mut ULONG) -> ULONG);
53 define_extern_fn_type!(pub(super) SKF_GetDevState = fn(*const CHAR, *mut ULONG) -> ULONG);
54 define_extern_fn_type!(pub(super) SKF_ConnectDev = fn(*const CHAR, *mut HANDLE) -> ULONG);
55 define_extern_fn_type!(pub(super) SKF_DisConnectDev = fn(HANDLE) -> ULONG);
56 define_extern_fn_type!(pub(super) SKF_SetLabel = fn(HANDLE, *const CHAR) -> ULONG);
57 define_extern_fn_type!(pub(super) SKF_GetDevInfo = fn(HANDLE, *mut DeviceInfo) -> ULONG);
58 define_extern_fn_type!(pub(super) SKF_LockDev = fn(HANDLE, ULONG) -> ULONG);
59 define_extern_fn_type!(pub(super) SKF_UnlockDev = fn(HANDLE) -> ULONG);
60 define_extern_fn_type!(pub(super) SKF_Transmit = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
61 define_extern_fn_type!(pub(super) SKF_ChangeDevAuthKey = fn(HANDLE, *const BYTE, ULONG) -> ULONG);
62 define_extern_fn_type!(pub(super) SKF_DevAuth = fn(HANDLE, *const BYTE, ULONG) -> ULONG);
63 define_extern_fn_type!(pub(super) SKF_CreateApplication = fn(HANDLE, LPSTR, LPSTR, DWORD, LPSTR, DWORD, DWORD, *mut HANDLE) -> ULONG);
64 define_extern_fn_type!(pub(super) SKF_OpenApplication = fn(HANDLE, LPSTR, *mut HANDLE) -> ULONG);
65 define_extern_fn_type!(pub(super) SKF_DeleteApplication = fn(HANDLE, LPSTR) -> ULONG);
66 define_extern_fn_type!(pub(super) SKF_EnumApplication = fn(HANDLE, *mut CHAR, *mut ULONG) -> ULONG);
67}
68
69#[allow(non_camel_case_types)]
70pub(crate) mod app_fn {
71 use crate::define_extern_fn_type;
72 use skf_api::native::types::{FileAttribute, BOOL, BYTE, CHAR, HANDLE, LPSTR, ULONG};
73
74 define_extern_fn_type!(pub(super) SKF_CloseApplication = fn(HANDLE) -> ULONG);
75 define_extern_fn_type!(pub(super) SKF_ClearSecureState = fn(HANDLE) -> ULONG);
76 define_extern_fn_type!(pub(super) SKF_EnumFiles = fn(HANDLE, *mut CHAR, *mut ULONG) -> ULONG);
77 define_extern_fn_type!(pub(super) SKF_CreateFile = fn(HANDLE, LPSTR, ULONG, ULONG, ULONG) -> ULONG);
78 define_extern_fn_type!(pub(super) SKF_DeleteFile = fn(HANDLE, LPSTR) -> ULONG);
79 define_extern_fn_type!(pub(super) SKF_GetFileInfo = fn(HANDLE, LPSTR, *mut FileAttribute) -> ULONG);
80 define_extern_fn_type!(pub(super) SKF_ReadFile = fn(HANDLE, LPSTR, ULONG, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
81 define_extern_fn_type!(pub(super) SKF_WriteFile = fn(HANDLE, LPSTR, ULONG, *const BYTE, ULONG) -> ULONG);
82 define_extern_fn_type!(pub(super) SKF_ChangePIN = fn(HANDLE, ULONG, LPSTR, LPSTR, *mut ULONG) -> ULONG);
83 define_extern_fn_type!(pub(super) SKF_GetPINInfo = fn(HANDLE, ULONG, *mut ULONG, *mut ULONG, *mut BOOL) -> ULONG);
84 define_extern_fn_type!(pub(super) SKF_VerifyPIN = fn(HANDLE, ULONG, LPSTR, *mut ULONG) -> ULONG);
85 define_extern_fn_type!(pub(super) SKF_UnblockPIN = fn(HANDLE, LPSTR, LPSTR, *mut ULONG) -> ULONG);
86}
87
88#[allow(non_camel_case_types)]
89pub(crate) mod container_fn {
90 use crate::define_extern_fn_type;
91 use skf_api::native::types::{BOOL, BYTE, CHAR, HANDLE, LPSTR, ULONG};
92
93 define_extern_fn_type!(pub(super) SKF_CreateContainer = fn(HANDLE, LPSTR, *mut HANDLE) -> ULONG);
94 define_extern_fn_type!(pub(super) SKF_DeleteContainer = fn(HANDLE, LPSTR) -> ULONG);
95 define_extern_fn_type!(pub(super) SKF_OpenContainer = fn(HANDLE, LPSTR, *mut HANDLE) -> ULONG);
96 define_extern_fn_type!(pub(super) SKF_EnumContainer = fn(HANDLE, *mut CHAR, *mut ULONG) -> ULONG);
97 define_extern_fn_type!(pub(super) SKF_CloseContainer = fn(HANDLE) -> ULONG);
98 define_extern_fn_type!(pub(super) SKF_GetContainerType = fn(HANDLE, *mut ULONG) -> ULONG);
99 define_extern_fn_type!(pub(super) SKF_ImportCertificate = fn(HANDLE, BOOL, *const BYTE, ULONG) -> ULONG);
100 define_extern_fn_type!(pub(super) SKF_ExportCertificate = fn(HANDLE, BOOL, *mut BYTE, *mut ULONG) -> ULONG);
101}
102
103#[allow(non_camel_case_types)]
104pub(crate) mod crypto_fn {
105 use crate::define_extern_fn_type;
106 use skf_api::native::types::{
107 BlockCipherParam, ECCCipherBlob, ECCPrivateKeyBlob, ECCPublicKeyBlob, ECCSignatureBlob,
108 EnvelopedKeyBlob, BOOL, BYTE, HANDLE, ULONG,
109 };
110
111 define_extern_fn_type!(pub(crate) SKF_GenRandom = fn(HANDLE, *mut BYTE, ULONG) -> ULONG);
112 define_extern_fn_type!(pub(crate) SKF_CloseHandle = fn(HANDLE) -> ULONG);
113 define_extern_fn_type!(pub(super) SKF_SetSymmKey = fn(HANDLE, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
114 define_extern_fn_type!(pub(super) SKF_EncryptInit = fn(HANDLE, BlockCipherParam) -> ULONG);
115 define_extern_fn_type!(pub(super) SKF_Encrypt = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
116 define_extern_fn_type!(pub(super) SKF_EncryptUpdate = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
117 define_extern_fn_type!(pub(super) SKF_EncryptFinal = fn(HANDLE, *mut BYTE, *mut ULONG) -> ULONG);
118 define_extern_fn_type!(pub(super) SKF_DecryptInit = fn(HANDLE, BlockCipherParam) -> ULONG);
119 define_extern_fn_type!(pub(super) SKF_Decrypt = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
120 define_extern_fn_type!(pub(super) SKF_DecryptUpdate = fn(HANDLE, *const BYTE, ULONG, *mut BYTE, *mut ULONG) -> ULONG);
121 define_extern_fn_type!(pub(super) SKF_DecryptFinal = fn(HANDLE, *mut BYTE, *mut ULONG) -> ULONG);
122 define_extern_fn_type!(pub(super) SKF_ExtECCEncrypt = fn(HANDLE, *const ECCPublicKeyBlob, *const BYTE, ULONG, *mut ECCCipherBlob) -> ULONG);
123 define_extern_fn_type!(pub(super) SKF_ExtECCDecrypt = fn(HANDLE, *const ECCPrivateKeyBlob, *const ECCCipherBlob, *mut BYTE, *mut ULONG) -> ULONG);
124 define_extern_fn_type!(pub(super) SKF_ExtECCSign = fn(HANDLE, *const ECCPrivateKeyBlob, *const BYTE, ULONG, *mut ECCSignatureBlob) -> ULONG);
125 define_extern_fn_type!(pub(super) SKF_ExtECCVerify = fn(HANDLE, *const ECCPublicKeyBlob, *const BYTE, ULONG, *const ECCSignatureBlob) -> ULONG);
126 define_extern_fn_type!(pub(super) SKF_GenECCKeyPair = fn(HANDLE, ULONG, *mut ECCPublicKeyBlob) -> ULONG);
127 define_extern_fn_type!(pub(super) SKF_ImportECCKeyPair = fn(HANDLE, *const EnvelopedKeyBlob) -> ULONG);
128 define_extern_fn_type!(pub(super) SKF_ECCSignData = fn(HANDLE, *const BYTE, ULONG, *mut ECCSignatureBlob) -> ULONG);
129 define_extern_fn_type!(pub(super) SKF_ECCVerify = fn(HANDLE, *const ECCPublicKeyBlob, *const BYTE, ULONG, *const ECCSignatureBlob) -> ULONG);
130 define_extern_fn_type!(pub(super) SKF_ECCExportSessionKey = fn(HANDLE, ULONG, *const ECCPublicKeyBlob, *mut ECCCipherBlob, *mut HANDLE) -> ULONG);
131 define_extern_fn_type!(pub(super) SKF_GenerateAgreementDataWithECC = fn(HANDLE, ULONG, *mut ECCPublicKeyBlob, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
132 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);
133 define_extern_fn_type!(pub(super) SKF_ExportPublicKey = fn(HANDLE, BOOL, *mut BYTE, *mut ULONG) -> ULONG);
134 define_extern_fn_type!(pub(super) SKF_ImportSessionKey = fn(HANDLE, ULONG, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
135 define_extern_fn_type!(pub(super) SKF_GenerateKeyWithECC = fn(HANDLE, *const ECCPublicKeyBlob, *const ECCPublicKeyBlob, *const BYTE, ULONG, *mut HANDLE) -> ULONG);
136}
137
138#[derive(Default)]
139pub(crate) struct ModMag {
140 pub enum_dev: Option<device_fn::SKF_EnumDev>,
141 pub wait_plug_event: Option<device_fn::SKF_WaitForDevEvent>,
142 pub cancel_wait_plug_event: Option<device_fn::SKF_CancelWaitForDevEvent>,
143 pub get_dev_state: Option<device_fn::SKF_GetDevState>,
144 pub connect_dev: Option<device_fn::SKF_ConnectDev>,
145}
146
147impl ModMag {
148 pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
149 let enum_dev = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumDev\0")? });
150 let wait_plug_event = Some(unsafe { SymbolBundle::new(lib, b"SKF_WaitForDevEvent\0")? });
151 let cancel_wait_plug_event =
152 Some(unsafe { SymbolBundle::new(lib, b"SKF_CancelWaitForDevEvent\0")? });
153 let get_dev_state = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetDevState\0")? });
154 let connect_dev = Some(unsafe { SymbolBundle::new(lib, b"SKF_ConnectDev\0")? });
155 let holder = Self {
156 enum_dev,
157 wait_plug_event,
158 cancel_wait_plug_event,
159 get_dev_state,
160 connect_dev,
161 };
162 Ok(holder)
163 }
164}
165
166#[derive(Default)]
167pub(crate) struct ModDev {
168 pub dev_set_label: Option<device_fn::SKF_SetLabel>,
169 pub dev_dis_connect: Option<device_fn::SKF_DisConnectDev>,
170 pub dev_get_info: Option<device_fn::SKF_GetDevInfo>,
171 pub dev_lock: Option<device_fn::SKF_LockDev>,
172 pub dev_unlock: Option<device_fn::SKF_UnlockDev>,
173 pub dev_transmit: Option<device_fn::SKF_Transmit>,
174 pub dev_auth: Option<device_fn::SKF_DevAuth>,
175 pub dev_change_auth_key: Option<device_fn::SKF_ChangeDevAuthKey>,
176 pub app_create: Option<device_fn::SKF_CreateApplication>,
177 pub app_open: Option<device_fn::SKF_OpenApplication>,
178 pub app_delete: Option<device_fn::SKF_DeleteApplication>,
179 pub app_enum: Option<device_fn::SKF_EnumApplication>,
180 pub gen_random: Option<crypto_fn::SKF_GenRandom>,
181 pub sym_key_import: Option<crypto_fn::SKF_SetSymmKey>,
182 pub ecc_ext_encrypt: Option<crypto_fn::SKF_ExtECCEncrypt>,
183 pub ecc_ext_decrypt: Option<crypto_fn::SKF_ExtECCDecrypt>,
184 pub ecc_ext_sign: Option<crypto_fn::SKF_ExtECCSign>,
185 pub ecc_ext_verify: Option<crypto_fn::SKF_ExtECCVerify>,
186 pub ecc_verify: Option<crypto_fn::SKF_ECCVerify>,
187 pub ecc_gen_sk: Option<crypto_fn::SKF_GenerateKeyWithECC>,
188}
189
190impl ModDev {
191 pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
192 let dev_set_label = Some(unsafe { SymbolBundle::new(lib, b"SKF_SetLabel\0")? });
193 let dev_dis_connect = Some(unsafe { SymbolBundle::new(lib, b"SKF_DisConnectDev\0")? });
194 let dev_get_info = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetDevInfo\0")? });
195 let dev_lock = Some(unsafe { SymbolBundle::new(lib, b"SKF_LockDev\0")? });
196 let dev_unlock = Some(unsafe { SymbolBundle::new(lib, b"SKF_UnlockDev\0")? });
197 let dev_transmit = Some(unsafe { SymbolBundle::new(lib, b"SKF_Transmit\0")? });
198 let dev_auth = Some(unsafe { SymbolBundle::new(lib, b"SKF_DevAuth\0")? });
199 let dev_change_auth_key =
200 Some(unsafe { SymbolBundle::new(lib, b"SKF_ChangeDevAuthKey\0")? });
201 let app_create = Some(unsafe { SymbolBundle::new(lib, b"SKF_CreateApplication\0")? });
202 let app_open = Some(unsafe { SymbolBundle::new(lib, b"SKF_OpenApplication\0")? });
203 let app_delete = Some(unsafe { SymbolBundle::new(lib, b"SKF_DeleteApplication\0")? });
204 let app_enum = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumApplication\0")? });
205 let gen_random = Some(unsafe { SymbolBundle::new(lib, b"SKF_GenRandom\0")? });
206 let sym_key_import = Some(unsafe { SymbolBundle::new(lib, b"SKF_SetSymmKey\0")? });
207 let ecc_ext_encrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCEncrypt\0")? });
208 let ecc_ext_decrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCDecrypt\0")? });
209 let ecc_ext_sign = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCSign\0")? });
210 let ecc_ext_verify = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExtECCVerify\0")? });
211 let ecc_verify = Some(unsafe { SymbolBundle::new(lib, b"SKF_ECCVerify\0")? });
212 let ecc_gen_sk = Some(unsafe { SymbolBundle::new(lib, b"SKF_GenerateKeyWithECC\0")? });
213
214 let holder = Self {
215 dev_set_label,
216 dev_dis_connect,
217 dev_get_info,
218 dev_lock,
219 dev_unlock,
220 dev_transmit,
221 dev_auth,
222 dev_change_auth_key,
223 app_create,
224 app_open,
225 app_delete,
226 app_enum,
227 gen_random,
228 sym_key_import,
229 ecc_ext_encrypt,
230 ecc_ext_decrypt,
231 ecc_ext_sign,
232 ecc_ext_verify,
233 ecc_verify,
234 ecc_gen_sk,
235 };
236 Ok(holder)
237 }
238}
239
240#[derive(Default)]
241pub(crate) struct ModApp {
242 pub app_close: Option<app_fn::SKF_CloseApplication>,
243 pub app_clear_secure_state: Option<app_fn::SKF_ClearSecureState>,
244 pub file_get_list: Option<app_fn::SKF_EnumFiles>,
245 pub file_create: Option<app_fn::SKF_CreateFile>,
246 pub file_delete: Option<app_fn::SKF_DeleteFile>,
247 pub file_get_info: Option<app_fn::SKF_GetFileInfo>,
248 pub file_read: Option<app_fn::SKF_ReadFile>,
249 pub file_write: Option<app_fn::SKF_WriteFile>,
250 pub container_get_list: Option<container_fn::SKF_EnumContainer>,
251 pub container_create: Option<container_fn::SKF_CreateContainer>,
252 pub container_delete: Option<container_fn::SKF_DeleteContainer>,
253 pub container_open: Option<container_fn::SKF_OpenContainer>,
254 pub pin_change: Option<app_fn::SKF_ChangePIN>,
255 pub pin_get_info: Option<app_fn::SKF_GetPINInfo>,
256 pub pin_verify: Option<app_fn::SKF_VerifyPIN>,
257 pub pin_unblock: Option<app_fn::SKF_UnblockPIN>,
258}
259
260impl ModApp {
261 pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
262 let app_close = Some(unsafe { SymbolBundle::new(lib, b"SKF_CloseApplication\0")? });
263 let app_clear_secure_state =
264 Some(unsafe { SymbolBundle::new(lib, b"SKF_ClearSecureState\0")? });
265 let file_get_list = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumFiles\0")? });
266 let file_create = Some(unsafe { SymbolBundle::new(lib, b"SKF_CreateFile\0")? });
267 let file_delete = Some(unsafe { SymbolBundle::new(lib, b"SKF_DeleteFile\0")? });
268 let file_get_info = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetFileInfo\0")? });
269 let file_read = Some(unsafe { SymbolBundle::new(lib, b"SKF_ReadFile\0")? });
270 let file_write = Some(unsafe { SymbolBundle::new(lib, b"SKF_WriteFile\0")? });
271 let container_get_list = Some(unsafe { SymbolBundle::new(lib, b"SKF_EnumContainer\0")? });
272 let container_create = Some(unsafe { SymbolBundle::new(lib, b"SKF_CreateContainer\0")? });
273 let container_delete = Some(unsafe { SymbolBundle::new(lib, b"SKF_DeleteContainer\0")? });
274 let container_open = Some(unsafe { SymbolBundle::new(lib, b"SKF_OpenContainer\0")? });
275
276 let pin_change = Some(unsafe { SymbolBundle::new(lib, b"SKF_ChangePIN\0")? });
277 let pin_get_info = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetPINInfo\0")? });
278 let pin_verify = Some(unsafe { SymbolBundle::new(lib, b"SKF_VerifyPIN\0")? });
279 let pin_unblock = Some(unsafe { SymbolBundle::new(lib, b"SKF_UnblockPIN\0")? });
280
281 let holder = Self {
282 app_close,
283 file_get_list,
284 file_create,
285 file_delete,
286 file_get_info,
287 file_read,
288 file_write,
289 container_get_list,
290 container_create,
291 container_delete,
292 container_open,
293 app_clear_secure_state,
294 pin_change,
295 pin_get_info,
296 pin_verify,
297 pin_unblock,
298 };
299 Ok(holder)
300 }
301}
302
303#[derive(Default)]
304pub(crate) struct ModContainer {
305 pub ct_close: Option<container_fn::SKF_CloseContainer>,
306 pub ct_get_type: Option<container_fn::SKF_GetContainerType>,
307 pub ct_imp_cert: Option<container_fn::SKF_ImportCertificate>,
308 pub ct_exp_cert: Option<container_fn::SKF_ExportCertificate>,
309 pub ct_ecc_gen_pair: Option<crypto_fn::SKF_GenECCKeyPair>,
310 pub ct_ecc_imp_pair: Option<crypto_fn::SKF_ImportECCKeyPair>,
311 pub ct_ecc_sign: Option<crypto_fn::SKF_ECCSignData>,
312 pub ct_sk_gen_agreement: Option<crypto_fn::SKF_GenerateAgreementDataWithECC>,
313 pub ct_sk_gen_agreement_and_key: Option<crypto_fn::SKF_GenerateAgreementDataAndKeyWithECC>,
314 pub ct_ecc_exp_pub_key: Option<crypto_fn::SKF_ExportPublicKey>,
315 pub ct_sk_imp: Option<crypto_fn::SKF_ImportSessionKey>,
316 pub ct_sk_exp: Option<crypto_fn::SKF_ECCExportSessionKey>,
317}
318
319impl ModContainer {
320 pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
321 let ct_close = Some(unsafe { SymbolBundle::new(lib, b"SKF_CloseContainer\0")? });
322 let ct_get_type = Some(unsafe { SymbolBundle::new(lib, b"SKF_GetContainerType\0")? });
323 let ct_imp_cert = Some(unsafe { SymbolBundle::new(lib, b"SKF_ImportCertificate\0")? });
324 let ct_exp_cert = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExportCertificate\0")? });
325 let ct_ecc_gen_pair = Some(unsafe { SymbolBundle::new(lib, b"SKF_GenECCKeyPair\0")? });
326 let ct_ecc_imp_pair = Some(unsafe { SymbolBundle::new(lib, b"SKF_ImportECCKeyPair\0")? });
327 let ct_ecc_sign = Some(unsafe { SymbolBundle::new(lib, b"SKF_ECCSignData\0")? });
328 let ct_sk_gen_agreement =
329 Some(unsafe { SymbolBundle::new(lib, b"SKF_GenerateAgreementDataWithECC\0")? });
330 let ct_sk_gen_agreement_and_key =
331 Some(unsafe { SymbolBundle::new(lib, b"SKF_GenerateAgreementDataAndKeyWithECC\0")? });
332 let ct_ecc_exp_pub_key = Some(unsafe { SymbolBundle::new(lib, b"SKF_ExportPublicKey\0")? });
333 let ct_sk_imp = Some(unsafe { SymbolBundle::new(lib, b"SKF_ImportSessionKey\0")? });
334 let ct_sk_exp = Some(unsafe { SymbolBundle::new(lib, b"SKF_ECCExportSessionKey\0")? });
335 let holder = Self {
336 ct_close,
337 ct_get_type,
338 ct_imp_cert,
339 ct_exp_cert,
340 ct_ecc_gen_pair,
341 ct_ecc_imp_pair,
342 ct_ecc_sign,
343 ct_sk_gen_agreement,
344 ct_sk_gen_agreement_and_key,
345 ct_ecc_exp_pub_key,
346 ct_sk_imp,
347 ct_sk_exp,
348 };
349 Ok(holder)
350 }
351}
352
353#[derive(Default)]
354pub(crate) struct ModBlockCipher {
355 pub encrypt_init: Option<crypto_fn::SKF_EncryptInit>,
356 pub encrypt: Option<crypto_fn::SKF_Encrypt>,
357 pub encrypt_update: Option<crypto_fn::SKF_EncryptUpdate>,
358 pub encrypt_final: Option<crypto_fn::SKF_EncryptFinal>,
359 pub decrypt_init: Option<crypto_fn::SKF_DecryptInit>,
360 pub decrypt: Option<crypto_fn::SKF_Decrypt>,
361 pub decrypt_update: Option<crypto_fn::SKF_DecryptUpdate>,
362 pub decrypt_final: Option<crypto_fn::SKF_DecryptFinal>,
363}
364
365impl ModBlockCipher {
366 pub fn load_symbols(lib: &Arc<Library>) -> crate::Result<Self> {
367 let encrypt_init = Some(unsafe { SymbolBundle::new(lib, b"SKF_EncryptInit\0")? });
368 let encrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_Encrypt\0")? });
369 let encrypt_update = Some(unsafe { SymbolBundle::new(lib, b"SKF_EncryptUpdate\0")? });
370 let encrypt_final = Some(unsafe { SymbolBundle::new(lib, b"SKF_EncryptFinal\0")? });
371
372 let decrypt_init = Some(unsafe { SymbolBundle::new(lib, b"SKF_DecryptInit\0")? });
373 let decrypt = Some(unsafe { SymbolBundle::new(lib, b"SKF_Decrypt\0")? });
374 let decrypt_update = Some(unsafe { SymbolBundle::new(lib, b"SKF_DecryptUpdate\0")? });
375 let decrypt_final = Some(unsafe { SymbolBundle::new(lib, b"SKF_DecryptFinal\0")? });
376 let holder = Self {
377 encrypt_init,
378 encrypt,
379 encrypt_update,
380 encrypt_final,
381 decrypt_init,
382 decrypt,
383 decrypt_update,
384 decrypt_final,
385 };
386 Ok(holder)
387 }
388}
389#[cfg(test)]
390mod test {
391 use super::*;
392 use crate::LibLoader;
393
394 #[test]
395 #[ignore]
396 fn sym_mod_ctl_test() {
397 let lib = Arc::new(LibLoader::env_lookup().unwrap());
398 assert!(ModMag::load_symbols(&lib).is_ok());
399 }
400
401 #[test]
402 #[ignore]
403 fn sym_mod_dev_test() {
404 let lib = Arc::new(LibLoader::env_lookup().unwrap());
405 assert!(ModDev::load_symbols(&lib).is_ok());
406 }
407}