Skip to content

Tutorial CP ABE

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

Tutorial: CP-ABE

In ciphertext-policy ABE, keys carry attributes and ciphertexts carry a policy. A key decrypts a ciphertext only if its attributes satisfy the policy. Use it when the person encrypting knows who should read the data: medical records, HR documents, classified reports.

This tutorial uses the high-level crypto box API (OpenABECryptoContext). It is CCA-secure by default and handles serialization and base64 encoding for you.

1. The smallest complete program

Adapted from examples/test_cp.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();                       // always first

  OpenABECryptoContext cpabe("CP-ABE");
  cpabe.generateParams();                    // master public + secret params

  // A key with two attributes, stored in the context under the ID "key0"
  cpabe.keygen("|attr1|attr2", "key0");

  string ct, pt1 = "hello world!", pt2;
  cpabe.encrypt("attr1 and attr2", pt1, ct); // policy goes on the ciphertext

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

  ShutdownOpenABE();                         // always last
  return 0;
}

Things to notice:

  • InitializeOpenABE() / ShutdownOpenABE() must bracket all OpenABE calls.
  • keygen takes an attribute list (|a|b|c) in CP-ABE. encrypt takes a policy (a and b).
  • Keys live inside the context under the ID you choose ("key0"). To move them elsewhere, see Key Management.

2. Build it

After make (and optionally make install), compile with the same flags the examples use:

. ./env
g++ -std=c++17 -include cstdint -pthread -DSSL_LIB_INIT \
  -I"$ZROOT/deps/root/include" -I"$ZROOT/root/include" \
  my_cpabe.cpp -o my_cpabe \
  -L"$ZROOT/deps/root/lib" -L"$ZROOT/deps/root/lib64" -L"$ZROOT/root/lib" \
  -lopenabe -lrelic -lrelic_ec -lssl -lcrypto
./my_cpabe

If you installed to /usr/local, the -I/-L flags pointing into $ZROOT are not needed.

3. A realistic policy

Alice and Charlie get keys; a medical file is encrypted so that only floor 2-5 staff who are doctors or nurses can read it:

cpabe.keygen("Female|Nurse|Floor=3|RespSpecialist", "alice");
cpabe.keygen("Male|Doctor|Floor=6|Cardiologist",    "charlie");

cpabe.encrypt("(Doctor or Nurse) and Floor in {2-5}", record, ct);

cpabe.decrypt("alice",   ct, out);   // true:  Nurse, floor 3
cpabe.decrypt("charlie", ct, out);   // false: floor 6 is out of range

Floor=3 in the key is a numeric attribute, and Floor in {2-5} is an inclusive range. Floor in (2-5) would be exclusive. The full language, including dates and bit widths, is in Policy and Attribute Syntax.

4. Separate the authority from the users

In a real system the authority that holds the master secret is not the party that encrypts. Export the public parameters and hand them to encryptors:

// Authority
OpenABECryptoContext authority("CP-ABE");
authority.generateParams();
string mpk, msk;
authority.exportPublicParams(mpk);   // share freely
authority.exportSecretParams(msk);   // keep secret

// Encryptor: only needs the public parameters
OpenABECryptoContext sender("CP-ABE");
sender.importPublicParams(mpk);
sender.encrypt("Doctor and Floor in {2-5}", "patient data", ct);

See Key Management for exporting user keys and using the keystore.

5. The same thing from the command line

./oabe_setup  -s CP -p org1
./oabe_keygen -s CP -p org1 -i "Female|Nurse|Floor=3|RespSpecialist" -o aliceCP
./oabe_keygen -s CP -p org1 -i "Male|Doctor|Floor=6|Cardiologist"    -o charlieCP
./oabe_enc    -s CP -p org1 -e "(Doctor or Nurse) and Floor in {2-5}" -i input.txt -o input.cpabe
./oabe_dec    -s CP -p org1 -k aliceCP.key   -i input.cpabe -o ok.txt     # exit 0
./oabe_dec    -s CP -p org1 -k charlieCP.key -i input.cpabe -o fail.txt   # exit 32

Exit code 32 means OpenABE_ERROR_DECRYPTION_FAILED: see Error Codes and the CLI Reference.

Next

Clone this wiki locally