Skip to content

Tutorial KP ABE

Claudio Lucisano edited this page Sep 22, 2026 · 1 revision

Tutorial: KP-ABE

In key-policy ABE the roles are flipped compared to CP-ABE: ciphertexts carry attributes (usually metadata describing the data) and keys carry a policy. A key decrypts every ciphertext whose attributes satisfy its policy.

Use it when the data is labelled at encryption time and access is decided later: auditing an email archive, answering a subpoena, giving an analyst a slice of a data lake.

1. The smallest complete program

Adapted from examples/test_kp.cpp:

#include <cassert>
#include <iostream>
#include <string>
#include <openabe/openabe.h>
#include <openabe/zsymcrypto.h>

using namespace std;
using namespace oabe;
using namespace oabe::crypto;

int main() {
  InitializeOpenABE();

  OpenABECryptoContext kpabe("KP-ABE");
  kpabe.generateParams();

  kpabe.keygen("attr1 and attr2", "key0");     // policy goes on the key

  string ct, pt1 = "hello world!", pt2;
  kpabe.encrypt("|attr1|attr2", pt1, ct);      // attributes go on the ciphertext

  bool ok = kpabe.decrypt("key0", ct, pt2);
  assert(ok && pt1 == pt2);
  cout << "Recovered message: " << pt2 << endl;

  ShutdownOpenABE();
  return 0;
}

Build it exactly like the CP-ABE tutorial: see Tutorial: CP-ABE, step 2.

2. Auditing an email archive

Every email is encrypted with its metadata as attributes. Later, an auditor receives a key that opens only the emails from Alice in a given period:

// At archiving time: one ciphertext per email
kpabe.encrypt("From:Alice|To:Bob|Date = March 7, 2017",      mail1, ct1);
kpabe.encrypt("From:Alice|To:Charlie|Date = March 15, 2017", mail2, ct2);

// During the investigation: a key for exactly the slice that is needed
kpabe.keygen("From:Alice and (Date = March 1-14, 2017 or Date = February 22-28, 2017)",
             "auditor");

kpabe.decrypt("auditor", ct1, out);   // true:  March 7 is in range
kpabe.decrypt("auditor", ct2, out);   // false: March 15 is not

Two features are at work here:

  • From:Alice uses the optional type:value form to namespace attributes.
  • Date = March 7, 2017 is a date attribute, and Date = March 1-14, 2017 a date range in the policy.

Both are described in Policy and Attribute Syntax.

3. From the command line

./oabe_setup  -s KP -p org2
./oabe_keygen -s KP -p org2 \
  -i "From:Alice and (Date = March 1-14, 2017 or Date = February 22-28, 2017)" -o analystKP
./oabe_enc -s KP -p org2 -e "From:Alice|To:Bob|Date = March 7, 2017"      -i input.txt -o input1.kpabe
./oabe_enc -s KP -p org2 -e "From:Alice|To:Charlie|Date = March 15, 2017" -i input.txt -o input2.kpabe
./oabe_dec -s KP -p org2 -k analystKP.key -i input1.kpabe -o ok.txt     # exit 0
./oabe_dec -s KP -p org2 -k analystKP.key -i input2.kpabe -o fail.txt   # exit 32

CP-ABE or KP-ABE?

CP-ABE KP-ABE
Policy lives on the ciphertext the key
Attributes live on the key the ciphertext
Who decides access whoever encrypts whoever issues keys
Typical question "Who may read this?" "What may this person read?"
Good for documents with known audiences archives, audits, analytics

Next

Clone this wiki locally