@@ -54,6 +54,31 @@ class Abe4ApiTest : public ::testing::Test {
5454
5555 ASSERT_FALSE (decrypted_gt.has_value ());
5656 }
57+
58+ void assert_hybrid_round_trip (
59+ const std::vector<accless::abe4::UserAttribute> &user_attrs,
60+ const std::string &policy, const std::string &plaintext,
61+ const std::string &aad) {
62+ auto auths = gather_authorities (user_attrs, policy);
63+ accless::abe4::SetupOutput setup_output = accless::abe4::setup (auths);
64+ std::string gid = " test_gid" ;
65+ std::string usk_b64 =
66+ accless::abe4::keygen (gid, setup_output.msk , user_attrs);
67+
68+ std::vector<uint8_t > plaintext_bytes (plaintext.begin (),
69+ plaintext.end ());
70+ std::vector<uint8_t > aad_bytes (aad.begin (), aad.end ());
71+
72+ auto hybrid_ct = accless::abe4::hybrid::encrypt (
73+ setup_output.mpk , policy, plaintext_bytes, aad_bytes);
74+ auto decrypted = accless::abe4::hybrid::decrypt (
75+ usk_b64, gid, policy, hybrid_ct.abe_ciphertext ,
76+ hybrid_ct.sym_ciphertext , aad_bytes);
77+
78+ ASSERT_TRUE (decrypted.has_value ());
79+ std::string decrypted_str (decrypted->begin (), decrypted->end ());
80+ EXPECT_EQ (plaintext, decrypted_str);
81+ }
5782};
5883
5984TEST_F (Abe4ApiTest, SingleAuthSingleOk) {
@@ -190,3 +215,70 @@ TEST_F(Abe4ApiTest, SimpleNegationOk) {
190215 std::string policy = " !A.c:2" ;
191216 assert_decryption_ok (user_attrs, policy);
192217}
218+
219+ TEST_F (Abe4ApiTest, HybridRoundTripOk) {
220+ std::vector<accless::abe4::UserAttribute> user_attrs = {
221+ {" A" , " a" , " 0" },
222+ {" A" , " c" , " 1" },
223+ };
224+ std::string policy = " A.a:0 & !A.c:0" ;
225+ std::string plaintext = " hybrid plaintext payload" ;
226+ std::string aad = " hybrid aad data" ;
227+ assert_hybrid_round_trip (user_attrs, policy, plaintext, aad);
228+ }
229+
230+ TEST_F (Abe4ApiTest, HybridDecryptFailsForUnauthorizedUser) {
231+ std::vector<accless::abe4::UserAttribute> user_attrs = {};
232+ std::string policy = " A.a:0" ;
233+ std::string plaintext = " hybrid plaintext payload" ;
234+ std::string aad = " hybrid aad data" ;
235+
236+ auto auths = gather_authorities (user_attrs, policy);
237+ accless::abe4::SetupOutput setup_output = accless::abe4::setup (auths);
238+ std::string gid = " test_gid" ;
239+ std::string usk_b64 =
240+ accless::abe4::keygen (gid, setup_output.msk , user_attrs);
241+
242+ std::vector<uint8_t > plaintext_bytes (plaintext.begin (), plaintext.end ());
243+ std::vector<uint8_t > aad_bytes (aad.begin (), aad.end ());
244+ auto hybrid_ct = accless::abe4::hybrid::encrypt (setup_output.mpk , policy,
245+ plaintext_bytes, aad_bytes);
246+
247+ auto decrypted = accless::abe4::hybrid::decrypt (
248+ usk_b64, gid, policy, hybrid_ct.abe_ciphertext ,
249+ hybrid_ct.sym_ciphertext , aad_bytes);
250+ ASSERT_FALSE (decrypted.has_value ());
251+ }
252+
253+ TEST_F (Abe4ApiTest, HybridRejectsModifiedAad) {
254+ std::vector<accless::abe4::UserAttribute> user_attrs = {{" A" , " a" , " 0" }};
255+ std::string policy = " A.a:0" ;
256+ std::string plaintext = " hybrid plaintext payload" ;
257+ std::string aad = " hybrid aad data" ;
258+ std::string wrong_aad = " tampered aad" ;
259+
260+ auto auths = gather_authorities (user_attrs, policy);
261+ accless::abe4::SetupOutput setup_output = accless::abe4::setup (auths);
262+ std::string gid = " test_gid" ;
263+ std::string usk_b64 =
264+ accless::abe4::keygen (gid, setup_output.msk , user_attrs);
265+
266+ std::vector<uint8_t > plaintext_bytes (plaintext.begin (), plaintext.end ());
267+ std::vector<uint8_t > aad_bytes (aad.begin (), aad.end ());
268+ std::vector<uint8_t > wrong_aad_bytes (wrong_aad.begin (), wrong_aad.end ());
269+
270+ auto hybrid_ct = accless::abe4::hybrid::encrypt (setup_output.mpk , policy,
271+ plaintext_bytes, aad_bytes);
272+
273+ auto decrypted = accless::abe4::hybrid::decrypt (
274+ usk_b64, gid, policy, hybrid_ct.abe_ciphertext ,
275+ hybrid_ct.sym_ciphertext , aad_bytes);
276+ ASSERT_TRUE (decrypted.has_value ());
277+ std::string decrypted_str (decrypted->begin (), decrypted->end ());
278+ EXPECT_EQ (plaintext, decrypted_str);
279+
280+ auto tampered = accless::abe4::hybrid::decrypt (
281+ usk_b64, gid, policy, hybrid_ct.abe_ciphertext ,
282+ hybrid_ct.sym_ciphertext , wrong_aad_bytes);
283+ ASSERT_FALSE (tampered.has_value ());
284+ }
0 commit comments