sentc_crypto_utils/cryptomat/
macros.rs1#[macro_export]
2macro_rules! wrapper_impl {
3 ($trait_impl:ident, $name:ident, $inner:ident) => {
4 impl $trait_impl for $name
5 {
6 type Inner = $inner;
7
8 fn get_id(&self) -> &str
9 {
10 &self.key_id
11 }
12
13 fn get_key(&self) -> &Self::Inner
14 {
15 &self.key
16 }
17 }
18 };
19}
20
21#[macro_export]
22macro_rules! to_string_impl {
23 ($st:ty,$t:ty) => {
24 impl $crate::cryptomat::KeyToString for $st
25 {
26 fn to_string(self) -> Result<String, $crate::error::SdkUtilError>
27 {
28 serde_json::to_string(&Into::<$t>::into(self)).map_err(|_e| $crate::error::SdkUtilError::JsonToStringFailed)
29 }
30
31 fn to_string_ref(&self) -> Result<String, $crate::error::SdkUtilError>
32 {
33 serde_json::to_string(&Into::<$t>::into(self)).map_err(|_e| $crate::error::SdkUtilError::JsonToStringFailed)
34 }
35 }
36 };
37}
38
39#[macro_export]
40macro_rules! to_string_try_impl {
41 ($st:ty,$t:ty) => {
42 impl $crate::cryptomat::KeyToString for $st
43 {
44 fn to_string(self) -> Result<String, $crate::error::SdkUtilError>
45 {
46 serde_json::to_string(&TryInto::<$t>::try_into(self)?).map_err(|_e| $crate::error::SdkUtilError::JsonToStringFailed)
47 }
48
49 fn to_string_ref(&self) -> Result<String, $crate::error::SdkUtilError>
50 {
51 serde_json::to_string(&TryInto::<$t>::try_into(self)?).map_err(|_e| $crate::error::SdkUtilError::JsonToStringFailed)
52 }
53 }
54 };
55}
56
57#[macro_export]
58macro_rules! from_string_impl {
59 ($st:ty,$t:ty) => {
60 impl FromStr for $st
61 {
62 type Err = $crate::error::SdkUtilError;
63
64 fn from_str(s: &str) -> Result<Self, Self::Err>
65 {
66 let key: $t = serde_json::from_str(s).map_err(|_| $crate::error::SdkUtilError::ImportKeyFailed)?;
67
68 key.try_into()
69 }
70 }
71 };
72}
73
74#[macro_export]
77macro_rules! sym_key_gen_self {
78 ($st:ty,$core:ty) => {
79 impl $crate::cryptomat::SymKeyGenWrapper for $st
80 {
81 type SymmetricKeyWrapper = Self;
82 type KeyGen = $core;
83
84 fn from_inner(
85 inner: <<Self as $crate::cryptomat::SymKeyGenWrapper>::KeyGen as sentc_crypto_core::cryptomat::SymKeyGen>::SymmetricKey,
86 id: String,
87 ) -> Self::SymmetricKeyWrapper
88 {
89 Self {
90 key: inner,
91 key_id: id,
92 }
93 }
94 }
95 };
96}
97
98#[macro_export]
99macro_rules! sym_key_com_self {
100 ($st:ty,$core:ty) => {
101 impl $crate::cryptomat::SymKeyComposerWrapper for $st
102 {
103 type SymmetricKeyWrapper = Self;
104 type Composer = $core;
105
106 fn from_inner(
107 inner: <<Self as $crate::cryptomat::SymKeyComposerWrapper>::Composer as sentc_crypto_core::cryptomat::SymKeyComposer>::SymmetricKey,
108 id: String,
109 ) -> Self::SymmetricKeyWrapper
110 {
111 Self {
112 key_id: id,
113 key: inner,
114 }
115 }
116 }
117 };
118}
119
120#[macro_export]
123macro_rules! static_key_pair_self {
124 ($sk:ty,$core_sk:ty, $pk:ident, $pk_to_str:ident) => {
125 impl $crate::cryptomat::StaticKeyPairWrapper for $sk
126 {
127 type PkWrapper = $pk;
128 type KeyGen = $core_sk;
129
130 fn pk_from_inner(
131 inner: <<Self as $crate::cryptomat::StaticKeyPairWrapper>::KeyGen as sentc_crypto_core::cryptomat::StaticKeyPair>::PublicKey,
132 id: String,
133 ) -> Self::PkWrapper
134 {
135 $pk {
136 key: inner,
137 key_id: id,
138 }
139 }
140
141 fn pk_inner_to_pem(
142 inner: &<<Self as $crate::cryptomat::StaticKeyPairWrapper>::KeyGen as sentc_crypto_core::cryptomat::StaticKeyPair>::PublicKey,
143 ) -> Result<String, $crate::error::SdkUtilError>
144 {
145 $pk_to_str(inner)
146 }
147 }
148 };
149}
150
151#[macro_export]
152macro_rules! static_key_composer_self {
153 ($sk:ty, $core_sk:ty,$pk:ident,$core_pk:ty,$core_pk_from_str:ident) => {
154 impl $crate::cryptomat::StaticKeyComposerWrapper for $sk
155 {
156 type SkWrapper = Self;
157 type PkWrapper = $pk;
158 type InnerPk = $core_pk;
159 type Composer = $core_sk;
160
161 fn sk_from_inner(
162 inner: <<Self as $crate::cryptomat::StaticKeyComposerWrapper>::Composer as sentc_crypto_core::cryptomat::SkComposer>::SecretKey,
163 id: String,
164 ) -> Self::SkWrapper
165 {
166 Self {
167 key_id: id,
168 key: inner,
169 }
170 }
171
172 fn pk_from_pem(public_key: &str, alg: &str, id: String) -> Result<Self::PkWrapper, $crate::error::SdkUtilError>
173 {
174 let key = $core_pk_from_str(public_key, alg)?;
175
176 Ok($pk {
177 key,
178 key_id: id,
179 })
180 }
181
182 fn pk_inner_from_pem(public_key: &str, alg: &str) -> Result<Self::InnerPk, $crate::error::SdkUtilError>
183 {
184 $core_pk_from_str(public_key, alg)
185 }
186 }
187 };
188}
189
190#[macro_export]
191macro_rules! pk_user_pk {
192 ($st:ty,$import_core:ident) => {
193 impl TryFrom<sentc_crypto_common::user::UserPublicKeyData> for $st
194 {
195 type Error = $crate::error::SdkUtilError;
196
197 fn try_from(value: sentc_crypto_common::user::UserPublicKeyData) -> Result<Self, Self::Error>
198 {
199 Ok(Self {
200 key_id: value.public_key_id,
201 key: $import_core(&value.public_key_pem, &value.public_key_alg)?,
202 })
203 }
204 }
205
206 impl<'a> TryFrom<&'a sentc_crypto_common::user::UserPublicKeyData> for $st
207 {
208 type Error = $crate::error::SdkUtilError;
209
210 fn try_from(value: &'a sentc_crypto_common::user::UserPublicKeyData) -> Result<Self, Self::Error>
211 {
212 Ok(Self {
213 key_id: value.public_key_id.clone(),
214 key: $import_core(&value.public_key_pem, &value.public_key_alg)?,
215 })
216 }
217 }
218 };
219}
220
221#[macro_export]
224macro_rules! sign_key_pair_self {
225 ($st:ty,$core_sk:ty,$export_core_vk:ident,$export_sig:ident) => {
226 impl $crate::cryptomat::SignKeyPairWrapper for $st
227 {
228 type KeyGen = $core_sk;
229
230 fn vk_inner_to_pem(
231 inner: &<<Self as $crate::cryptomat::SignKeyPairWrapper>::KeyGen as sentc_crypto_core::cryptomat::SignKeyPair>::VerifyKey,
232 ) -> Result<String, $crate::error::SdkUtilError>
233 {
234 $export_core_vk(inner)
235 }
236
237 fn sig_to_string(
238 sig: <<<Self as $crate::cryptomat::SignKeyPairWrapper>::KeyGen as sentc_crypto_core::cryptomat::SignKeyPair>::SignKey as SignK>::Signature,
239 ) -> String
240 {
241 $export_sig(&sig)
242 }
243 }
244 };
245}
246
247#[macro_export]
248macro_rules! sign_key_composer_self {
249 ($st:ty,$core_sk:ty,$vk:ident,$core_vk:ty,$core_vk_from_str:ident,$sig_from_str:ident,$export_sig:ident) => {
250 impl $crate::cryptomat::SignComposerWrapper for $st
251 {
252 type SignKWrapper = Self;
253 type VerifyKWrapper = $vk;
254 type InnerVk = $core_vk;
255 type Composer = $core_sk;
256
257 fn sk_from_inner(
258 inner: <<Self as $crate::cryptomat::SignComposerWrapper>::Composer as sentc_crypto_core::cryptomat::SignKeyComposer>::Key,
259 id: String,
260 ) -> Self::SignKWrapper
261 {
262 Self {
263 key_id: id,
264 key: inner,
265 }
266 }
267
268 fn vk_from_pem(public_key: &str, alg: &str, id: String) -> Result<Self::VerifyKWrapper, $crate::error::SdkUtilError>
269 {
270 let key = $core_vk_from_str(public_key, alg)?;
271
272 Ok($vk {
273 key,
274 key_id: id,
275 })
276 }
277
278 fn vk_inner_from_pem(public_key: &str, alg: &str) -> Result<Self::InnerVk, $crate::error::SdkUtilError>
279 {
280 $core_vk_from_str(public_key, alg)
281 }
282
283 fn sig_to_string(
284 sig: <<<Self as $crate::cryptomat::SignComposerWrapper>::SignKWrapper as $crate::cryptomat::SignKWrapper>::Inner as sentc_crypto_core::cryptomat::SignK>::Signature,
285 ) -> String
286 {
287 $export_sig(&sig)
288 }
289
290 fn sig_from_string(
291 sig: &str,
292 alg: &str,
293 ) -> Result<
294 <<Self as $crate::cryptomat::SignComposerWrapper>::InnerVk as sentc_crypto_core::cryptomat::VerifyK>::Signature,
295 $crate::error::SdkUtilError,
296 >
297 {
298 $sig_from_str(sig, alg)
299 }
300 }
301 };
302}
303
304#[macro_export]
305macro_rules! vk_user_vk {
306 ($st:ty,$import_core:ident) => {
307 impl TryFrom<sentc_crypto_common::user::UserVerifyKeyData> for $st
308 {
309 type Error = $crate::error::SdkUtilError;
310
311 fn try_from(value: sentc_crypto_common::user::UserVerifyKeyData) -> Result<Self, Self::Error>
312 {
313 Ok(Self {
314 key_id: value.verify_key_id,
315 key: $import_core(&value.verify_key_pem, &value.verify_key_alg)?,
316 })
317 }
318 }
319
320 impl<'a> TryFrom<&'a sentc_crypto_common::user::UserVerifyKeyData> for $st
321 {
322 type Error = $crate::error::SdkUtilError;
323
324 fn try_from(value: &'a sentc_crypto_common::user::UserVerifyKeyData) -> Result<Self, Self::Error>
325 {
326 Ok(Self {
327 key_id: value.verify_key_id.clone(),
328 key: $import_core(&value.verify_key_pem, &value.verify_key_alg)?,
329 })
330 }
331 }
332 };
333}
334
335#[macro_export]
338macro_rules! search_key_composer {
339 ($st:ty,$core:ty) => {
340 impl $crate::cryptomat::SearchableKeyComposerWrapper for $st
341 {
342 type SearchableKeyWrapper = Self;
343 type Composer = $core;
344
345 fn from_inner(
346 inner: <<Self as $crate::cryptomat::SearchableKeyComposerWrapper>::Composer as sentc_crypto_core::cryptomat::SearchableKeyComposer>::Key,
347 id: String,
348 ) -> Self::SearchableKeyWrapper
349 {
350 Self {
351 key: inner,
352 key_id: id,
353 }
354 }
355 }
356 };
357}
358
359#[macro_export]
360macro_rules! sortable_composer {
361 ($st:ty,$core:ty) => {
362 impl $crate::cryptomat::SortableKeyComposerWrapper for $st
363 {
364 type SortableKeyWrapper = Self;
365 type Composer = $core;
366
367 fn from_inner(
368 inner: <<Self as $crate::cryptomat::SortableKeyComposerWrapper>::Composer as sentc_crypto_core::cryptomat::SortableKeyComposer>::Key,
369 id: String,
370 ) -> Self::SortableKeyWrapper
371 {
372 Self {
373 key: inner,
374 key_id: id,
375 }
376 }
377 }
378 };
379}