pg_core/
test.rs

1//! Test helpers.
2
3use crate::artifacts::{PublicKey, SigningKey, SigningKeyExt, UserSecretKey, VerifyingKey};
4use crate::identity::{Attribute, EncryptionPolicy, Policy};
5use ibe::kem::cgw_kv::CGWKV;
6use ibe::kem::IBKEM;
7use rand::{CryptoRng, Rng};
8
9use alloc::string::String;
10use alloc::vec::Vec;
11
12/// A test setup.
13#[derive(Debug)]
14pub struct TestSetup {
15    /// The encryption public key.
16    pub ibe_pk: PublicKey<CGWKV>,
17
18    /// The IBS public key.
19    pub ibs_pk: VerifyingKey,
20
21    /// All policies.
22    pub policies: Vec<Policy>,
23
24    /// Associated USKs for all policies.
25    pub usks: Vec<UserSecretKey<CGWKV>>,
26
27    /// Associated signing keys for all policies.
28    pub signing_keys: Vec<SigningKeyExt>,
29
30    /// An example encryption policy.
31    pub policy: EncryptionPolicy,
32}
33
34impl TestSetup {
35    /// Create a new test setup.
36    pub fn new<R: Rng + CryptoRng>(rng: &mut R) -> Self {
37        let (ibe_pk, ibe_msk) = ibe::kem::cgw_kv::CGWKV::setup(rng);
38        let (ibs_pk, ibs_sk) = ibs::gg::setup(rng);
39
40        let ibe_pk = PublicKey::<CGWKV>(ibe_pk);
41        let ibs_pk = VerifyingKey(ibs_pk);
42
43        // Some recipient identifiers.
44        let id2 = String::from("Bob");
45        let id3 = String::from("Charlie");
46
47        // Some example policies.
48        let policies = vec![
49            // Alice just email
50            Policy {
51                timestamp: 1566722350,
52                con: vec![Attribute::new(
53                    "pbdf.sidn-pbdf.email.email",
54                    Some("alice@example.com"),
55                )],
56            }, // Alice just BSN
57            Policy {
58                timestamp: 1566722350,
59                con: vec![Attribute::new(
60                    "pbdf.gemeente.personalData.bsn",
61                    Some("<Alice's social security number>"),
62                )],
63            }, // Bob name + email
64            Policy {
65                timestamp: 1566722350,
66                con: vec![
67                    Attribute::new("pbdf.gemeente.personalData.name", Some("Bob")),
68                    Attribute::new("pbdf.sidn-pbdf.email.email", Some("bob@example.com")),
69                ],
70            }, // Charlie name + email
71            Policy {
72                timestamp: 1566722350,
73                con: vec![
74                    Attribute::new("pbdf.gemeente.personalData.name", Some("Charlie")),
75                    Attribute::new("pbdf.sidn-pbdf.email.email", Some("charlie@example.com")),
76                ],
77            }, // Charlie just name
78            Policy {
79                timestamp: 1566722350,
80                con: vec![Attribute::new(
81                    "pbdf.gemeente.personalData.name",
82                    Some("Charlie"),
83                )],
84            },
85        ];
86
87        // Encrypts for Bob (email + name) and Charlie (email + name).
88        let policy =
89            EncryptionPolicy::from([(id2, policies[2].clone()), (id3, policies[3].clone())]);
90
91        // Make USKs (decryption) for all policies.
92        let usks = policies
93            .iter()
94            .map(|pol| {
95                let derived = pol.derive_kem::<CGWKV>().unwrap();
96                let usk = CGWKV::extract_usk(Some(&ibe_pk.0), &ibe_msk, &derived, rng);
97                UserSecretKey::<CGWKV>(usk)
98            })
99            .collect();
100
101        // Also make signing keys for all policies.
102        let signing_keys = policies
103            .iter()
104            .map(|pol| {
105                let derived = pol.derive_ibs().unwrap();
106                let signing_key = ibs::gg::keygen(&ibs_sk, &derived, rng);
107
108                SigningKeyExt {
109                    key: SigningKey(signing_key),
110                    policy: pol.clone(),
111                }
112            })
113            .collect();
114
115        TestSetup {
116            ibe_pk,
117            ibs_pk,
118            policies,
119            usks,
120            signing_keys,
121            policy,
122        }
123    }
124}