Skip to main content

Format Preserving Encryption in Python

The Ubiq Structured (Format Preserving Encryption) Library has been merged into the core Ubiq Python Library to improve supportability and maintainability. This package has been deprecated and is no longer supported.

An implementation of the NIST-approved FF1 algorithm in Python.

This implementation conforms (as best as possible) to Draft SP 800-38G Rev. 1. The implementation passes all tests specified by NIST in their Cryptographic Standards and Guidelines examples for FF1

Documentation

See the Python API docs.

Installation

Using the package manager:

You may want to make sure you are running the latest version of pip3 by first executing

$ pip3 install --upgrade pip

You don't need this source code unless you want to modify the package. If you just want to use the package, install from PyPi using pip3, a package manager for Python3.

$ pip3 install --upgrade ubiq_security_fpe

Installing from source:

From within the cloned git repository directory, Install from source with:

$ cd ubiq-fpe-python
$ pip3 install -r requirements.txt
$ python3 setup.py install

You may need to run the python3 commands above using sudo.

Testing

To run the tests:

$ python3 -m unittest  ubiq_security_fpe/*test.py -v

Requirements

  • Python 3.5+

About alphabets and the radix parameter

The interfaces operate on strings, and the radix parameter determines which characters are valid within those strings, i.e. the alphabet. For example, if your radix is 10, then the alphabet for your plain text consists of the characters in the string "0123456789". If your radix is 16, then the alphabet is the characters in the string "0123456789abcdef".

More concretely, if you want to encrypt, say, a 16 digit number grouped into 4 groups of 4 using a - as a delimiter as in 0123-4567-8901-2345, then you would need a radix of at least 11, and you would need to translate the - character to an a (as that is the value that follows 9) prior to the encryption. Conversely, you would need to translate an a to a - after decryption.

This mapping of user inputs to alphabets defined by the radix is not performed by the library and must be done prior to calling the encrypt and after calling the decrypt functions.

A radix of up to 36 is supported, and the alphabet for a radix of 36 is "0123456789abcdefghijklmnopqrstuvwxyz".

Tweaks

Tweaks are very much like Initialization Vectors (IVs) in "traditional" encryption algorithms. For FF1, the minimun and maximum allowed lengths of the tweak may be specified by the user, and any tweak length between those values may be used.

Plain/ciphertext input lengths

For FF1, the minimum length is determined by the inequality:

  • radixminlen >= 1000000

or:

  • minlen >= 6 / log10 radix

Thus, the minimum length is determined by the radix and is automatically calculated from it.

For FF1, the maximum input length is

  • 232

Examples

The unit test code provides the best and simplest example of how to use the interfaces.

FF1

    /*
     * @key is a byte array whose length must be 16, 24, or 32
     * @twk is an optional byte array that can be used for the FF1 context
     * @twk_min_len is a constraint that can be applied for the tweak during
     *      the encrypt or decrypt calls
     * @twk_max_len is a constraint that can be applied for the tweak during
     *      the encrypt or decrypt calls
     * @radix is the radix for the text string
     * @alpha is the character set to use when perfoming the FPE encryption
     */

    ctx = ff1.Context(bytes(key), bytes(twk), twk_min_len, twk_max_len, radix, alpha)

    /*
     * @pt is the plain text string where the characters correspond to the 
     *     alpha character set and the radix value
     * @tweak is an optional byte array that will override the twk value
     *     supplied in the Context function.  If supplied it must be constrained to
     *     the twk_min_len and twk_max_len values
     */


    ct = ctx.Encrypt(pt, tweak)
    out = ctx.Decrypt(ct, tweak)

Metadata

Release files for ubiq-security-fpe 2.0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ubiq-security-fpe 2.0.1.1
File Size Uploaded
ubiq_security_fpe-2.0.1.1.tar.gz 9.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ubiq-security-fpe 2.0.1.1
File Interpreter ABI Platform
ubiq_security_fpe-2.0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 17.2 kB

Release files / ubiq_security_fpe-2.0.1.1.tar.gz

Download URL ubiq_security_fpe-2.0.1.1.tar.gz
Size 9.0 kB
Tags Source
SHA-256 checksum
How to use checksums
4a936f9a3404909f135d485ab715f712801dececc88c003903f0f29b04ccaf2c
BLAKE2b-256 checksum
How to use checksums
5ee902db63ce9ff3e9f0235a63c356524d2354e9ffbf2adee54c378fa76209a3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release files / ubiq_security_fpe-2.0.1.1-py3-none-any.whl

Download URL ubiq_security_fpe-2.0.1.1-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ec0d746bfc7d6e65a69f54f9a7922c3c0a18f9577fc264882c5054775d3638ed
BLAKE2b-256 checksum
How to use checksums
7d0998489d42d44d97e191c62522fdc0f641b58e01a5f564d6c9c06cb683a6b4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.5

Release history Release notifications | RSS feed

This release

2.0.1.1 This release

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page