Skip to content

Key Management

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

Key Management

An OpenABECryptoContext keeps its master parameters and user keys in memory, indexed by string IDs. This page shows how to move them in and out, and how to let the context pick the right key by itself.

What there is to manage

Object Who holds it Export / import
Master public params (mpk) authority, encryptors exportPublicParams / importPublicParams
Master secret params (msk) authority only exportSecretParams / importSecretParams
User key the user it was issued to exportUserKey(id, blob) / importUserKey(id, blob)

All blobs are std::strings. They are base64-encoded unless the context was created with base64encode = false, so they can go straight into files, databases or JSON.

Each key and ciphertext starts with a header that carries the scheme ID, the curve ID, the library version and a unique 128-bit identifier. This lets the library recognize what a blob is. In ciphertexts, the header is also protected against tampering together with the body.

Typical flow

// --- Authority ---------------------------------------------------------
OpenABECryptoContext authority("CP-ABE");
authority.generateParams();

string mpk, msk, aliceKey;
authority.exportPublicParams(mpk);
authority.exportSecretParams(msk);          // store securely, e.g. in an HSM or vault

authority.keygen("Nurse|Floor=3", "alice");
authority.exportUserKey("alice", aliceKey); // deliver to Alice over a secure channel
authority.deleteKey("alice");               // the authority does not need to keep it

// --- Alice -------------------------------------------------------------
OpenABECryptoContext alice("CP-ABE");
alice.importPublicParams(mpk);              // decryption needs the public params
alice.importUserKey("alice", aliceKey);
alice.decrypt("alice", ct, pt);

deleteKey(id) returns true if the key existed and was removed.

The keystore: decrypt without naming a key

When a user holds many keys, finding the one that fits a ciphertext is tedious. Enable the key manager and call the two-argument decrypt: the context searches its keystore for a key whose policy or attributes match. This is adapted from examples/test_km.cpp:

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

kpabe.enableKeyManager("user1");   // must come first: only later imports go to the keystore
kpabe.enableVerbose();             // optional: log which key gets picked

kpabe.importUserKey("key1", blob1); // "(attr1 or attr2) and attr3"
kpabe.importUserKey("key2", blob2); // "attr1 and attr2"
kpabe.importUserKey("key3", blob3); // "attr2 and attr3"

kpabe.encrypt("|attr1|attr2", "hello", ct);
kpabe.decrypt(ct, pt);             // no key ID: the keystore picks key2

If no stored key can decrypt, decrypt throws oabe::ZCryptoBoxException with the message Key Manager could not find an appropriate key to decrypt!. Calling the two-argument decrypt without enabling the key manager throws Key Manager not enabled!.

Good practice

  • Never ship msk to anything but the authority. With it, anyone can mint keys for any attribute set.
  • Key revocation is not built in. ABE keys stay valid for as long as the master parameters do. Common workarounds are attributes with an expiry date, which the date syntax in Policy and Attribute Syntax supports, or periodic rotation of the master parameters.
  • Wrap the crypto box calls in try { ... } catch (oabe::ZCryptoBoxException& e). Invalid input, unknown scheme IDs and failed imports are reported as exceptions, not return codes.

Clone this wiki locally