Sqlcipher backend

  • Although this extention’s code is short, it has not been properly peer-reviewed yet and may have introduced vulnerabilities.

Also note that this code relies on pysqlcipher and sqlcipher, and the code there might have vulnerabilities as well, but since these are widely used crypto modules, we can expect “short zero days” there.

sqlcipher_ext API notes

class SqlCipherDatabase(database, passphrase, \*kwargs*)

Subclass of SqliteDatabase that stores the database encrypted. Instead of the standard sqlite3 backend, it uses pysqlcipher: a python wrapper for sqlcipher, which – in turn – is an encrypted wrapper around sqlite3, so the API is identical to SqliteDatabase’s, except for object construction parameters:

Parameters:
  • database – Path to encrypted database filename to open [or create].
  • passphrase – Database encryption passphrase: should be at least 8 character long, but it is strongly advised to enforce better passphrase strength criteria in your implementation.
  • If the database file doesn’t exist, it will be created with encryption by a key derived from passhprase.
  • When trying to open an existing database, passhprase should be identical to the ones used when it was created. If the passphrase is incorrect, an error will be raised when first attempting to access the database.

  • rekey(passphrase)

    Parameters:passphrase (str) – New passphrase for database.

    Change the passphrase for database.

Note

SQLCipher can be configured using a number of extension PRAGMAs. The list of PRAGMAs and their descriptions can be found in the SQLCipher documentation.

For example to specify the number of PBKDF2 iterations for the key derivation (64K in SQLCipher 3.x, 256K in SQLCipher 4.x by default):

  1. # Use 1,000,000 iterations.
  2. db = SqlCipherDatabase('my_app.db', pragmas={'kdf_iter': 1000000})

To use a cipher page-size of 16KB and a cache-size of 10,000 pages:

  1. db = SqlCipherDatabase('my_app.db', passphrase='secret!!!', pragmas={
  2. 'cipher_page_size': 1024 * 16,
  3. 'cache_size': 10000}) # 10,000 16KB pages, or 160MB.

Example of prompting the user for a passphrase:

  1. db = SqlCipherDatabase(None)
  2. class BaseModel(Model):
  3. """Parent for all app's models"""
  4. class Meta:
  5. # We won't have a valid db until user enters passhrase.
  6. database = db
  7. # Derive our model subclasses
  8. class Person(BaseModel):
  9. name = TextField(primary_key=True)
  10. right_passphrase = False
  11. while not right_passphrase:
  12. db.init(
  13. 'testsqlcipher.db',
  14. passphrase=get_passphrase_from_user())
  15. try: # Actually execute a query against the db to test passphrase.
  16. db.get_tables()
  17. except DatabaseError as exc:
  18. # This error indicates the password was wrong.
  19. if exc.args[0] == 'file is encrypted or is not a database':
  20. tell_user_the_passphrase_was_wrong()
  21. db.init(None) # Reset the db.
  22. else:
  23. raise exc
  24. else:
  25. # The password was correct.
  26. right_passphrase = True

See also: a slightly more elaborate example.