How it works: technical overview

Two plugins, one key and a simple rule: the key comes with each connection and is never stored on the server.

  • DbCrypt encrypts pages
  • KeyHolder manages keys
  • AES-256 · key of 32 bytes
  • 4-8% typical decrease of performance
A page is encrypted on the disk and decrypted in the Firebird page cache Diskthe page is encryptedfile, copy, backup DbCryptdecrypts on readencrypts on write Page cachethe page is decryptedserver memory Applicationusual SQL,usual data

Pages

What is encrypted

A Firebird database consists of pages. The plugin encrypts the pages with your data. Metadata is on data pages, so table names and procedure texts are encrypted too.

encrypted pagepage in workdashed: service pages and the header, not encrypted
  • EncryptedData pages, index pages, BLOB pages and generator pages.
  • Not encryptedService pages and the database header. The header has the plugin name and the key name, but not the key.
  • In the backgroundOne command starts encryption. A server thread encrypts the pages while users work.
  • Checked at connectionIf the key value is wrong, the server sees it at once and refuses the connection.
SQL> ALTER DATABASE ENCRYPT WITH "DbCrypt" KEY Red;
C:\> gstat -h employee.fdb
        Attributes              encrypted, plugin DbCrypt
        Encryption key name:    RED

Key exchange

The server keeps no key

A key file on the server is a bad idea: a person who steals the server gets the database and the key together. So each connection brings the key, and the server keeps it only in memory.

The key goes from the client plugin to the server plugin in encrypted form. The exchange is protected by RSA key pairs, which are built into the plugins.

  1. 1The receiver asksthe server plugin needs the key
  2. 2A random challengethe sender makes it for this exchange only
  3. 3A one-time key pairthe receiver signs it with its built-in key
  4. 4The sender checksa false plugin cannot make this signature
  5. 5✓ The key is sentencrypted with the one-time key
  • Never in open formNeither in the network nor between the plugins inside the server.
  • No replayA recorded exchange is useless: the next one has a new challenge and a new key pair.
  • A false plugin gets nothingA changed DbCrypt or a false server cannot sign the answer.
  • One packageThe client library, KeyHolder and DbCrypt of one package trust each other.

Each type of program has its way

  • Applications with source codeThe library fbcrypt: three calls before the connection. Examples for Lazarus, Python and PHP.
  • Applications without source codeKeySource.dll, the built-in dialog or a pass phrase. Details.
  • Java and .NETThe drivers send the key in a connection property. The server needs UnsafeClient = true and WireCrypt = Required.
  • Utilities and scriptsThe plugin KeyHolderStdin reads the key from the input stream. The key is not in a file or in the command line.

Control

Know who uses the key

Firebird 5.0

Individual client keys

Make a separate key for each workstation, branch or customer from one database key. The keys work only on a server with your license. The server writes to firebird.log which key connected.

All versions

Key check hook

Put your library KeyChecker near the plugin. It gets the key name and the full path of the program each time a key is used. Use it to write your own audit log.

Backups

The backup is encrypted too

The most frequent mistake: the database is encrypted, but the backup is near it in open form. The gbak utility gets decrypted data from the server. The plugin package contains a gbak that encrypts the backup file with the same key.

  1. 1gbak sends the keyfrom KeySource.dll or from the input stream
  2. 2The server reads pagesand decrypts them for the utility
  3. 3gbak encrypts the fileswitch -KEYHOLDER in Firebird 4.0 and 5.0
  4. 4Checkthe file has no readable table names
  5. 5✓ Test restoreinto an encrypted database
  • gbakEncrypted backup and restore into an encrypted database.
  • nbackupCopies pages as they are: the copy stays encrypted with the same key.
  • Ready scriptThe example script 50-backup.ps1 makes the backup, checks it and does a test restore.
  • From your programThe Services API has the same options. The examples show a backup from Python.

Old backups can be read only with the key that encrypted them. Keep old keys while you need these backups.

Operations

Monitoring and key change

How far is encryption

SQL> select mon$crypt_page * 100.0 / mon$pages
       from mon$database;
C:\> gstat -e employee.fdb
Data pages: total 116, encrypted 116, non-crypted 0
Index pages: total 99, encrypted 99, non-crypted 0

Encryption is complete when the number of non-encrypted pages is zero. In Firebird 4.0 and 5.0, MON$CRYPT_STATE shows the state, and show database in isql prints it in words.

How to change the key

Decrypt the database and encrypt it with a new key, or make a backup and restore it with a new key. Give the new key a new name.

A lost key cannot be restored. Keep a copy of the key apart from the server and from the backups.

Compatibility

Versions and platforms

FunctionFirebird 3.0Firebird 4.0Firebird 5.0
Database encryption, AES-256yes (3.0.3+)yesyes
KeySource.dll: no changes in the applicationyesyesyes
Pass phrase instead of a keyyesyesyes
Encrypted gbak backupyes, automaticswitch -KEYHOLDERswitch -KEYHOLDER
Built-in key dialogyesyesyes
Individual client keys, log of connectionsnonoyes
Client library fbclientfrom the plugin package or HQbirdpackage, HQbird or officialpackage, HQbird or official
State in MON$CRYPT_STATEnoyesyes
OpenSSL1.11.13, or 1.1
Windows x64 server, 32- and 64-bit clientsyesyesyes
Linux x64yesyesyes
  • HQbirdThe plugin files are a part of HQbird: server plugins, client files, utilities. You add the license file.
  • Standard FirebirdAn installation script for Windows or Linux downloads the package, copies the files and runs the tests.
  • License fileNecessary where the DbCrypt plugin works: the server, an embedded application folder, the folder of gbak.
  • PerformanceEncryption decreases performance by about 4 to 8 percent. The exact number depends on the hardware and the settings.

For developers

Ready examples for your language

All examples on GitHub
LanguageExamples
Delphi, Lazarus, Free Pascalconsole and multi-thread programs with SQLdb, an IBX application, FireDAC notes, KeySource templates
Pythonfirebird-driver with fbcrypt, with KeySource and with a callback; encryption with monitoring; backup through the Services API
JavaJDBC (Jaybird), HikariCP data source, Spring Boot, native client with fbcrypt or KeySource
.NETADO.NET, connection pool, Entity Framework Core
PHPPDO with fbcrypt through FFI, PDO with KeySource
C++KeySource and KeyChecker libraries
All of thembackup with gbak and the key from the input stream