ubiq

package module
v0.0.0-...-5b2380c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2024 License: MIT Imports: 8 Imported by: 2

README

Format Preserving Encryption in Go

An implementation of the NIST-approved FF1 and FF3-1 algorithms in Go.

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; however, no official examples/samples exist (or are known) for FF3-1. FF3 is not implemented as NIST has officially deprecated its use in light of recent cryptanalysis performed on it.

Testing

To run the tests:

$ go test

As described above, the unit tests for FF1 come from the NIST guidelines. As no such guidelines are available for FF3-1, the unit tests verify only that the encryption and decryption implementations are compatible with each other.

Documentation

$ go doc -all
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.

By default, a radix of up to 36 is supported, and the alphabet for a radix of 36 is "0123456789abcdefghijklmnopqrstuvwxyz". However, he interfaces allow the caller to specify a custom alphabet that differs from the default. Using a custom alphabet, radixes up to the number of characters in the alphabet can be supported. Note that custom alphabets must not contain duplicate characters.

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. For FF3-1, the size of the tweak is fixed at 7 bytes.

Plain/ciphertext input lengths

For both FF1 and FF3-1, 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

For FF3-1, the maximum input length is

  • 2 * logradix 296

or:

  • 192 / log2 radix

Examples

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

FF1
	// K is a slice containing the key
	// T is a slice containing the tweak
	// tweak length is unbounded
	// r is the radix
	ff1, err := NewFF1(K, T, 0, 0, r)
	if err != nil {
		...
	}

	// PT is a slice containing the plaintext
	CT, err := ff1.Encrypt(PT, nil)
	if err != nil {
		...
	}

	PT, err = ff1.Decrypt(CT, nil)
	if err != nil {
		...
	}
FF3-1
	// K is a slice containing the key
	// T is a slice containing the tweak
	// r is the radix
	ff3_1, err := NewFF3_1(K, T, r)
	if err != nil {
		...
	}

	// PT is a slice containing the plaintext
	CT, err := ff3_1.Encrypt(PT, nil)
	if err != nil {
		...
	}

	PT, err = ff3_1.Decrypt(CT, nil)
	if err != nil {
		...
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BigIntToRunes

func BigIntToRunes(alpha *Alphabet, _n *big.Int, l int) []rune

convert a big integer to an array of runes in the specified radix, padding the output to the left with 0's

func RunesToBigInt

func RunesToBigInt(n *big.Int, alpha *Alphabet, s []rune) *big.Int

Types

type Alphabet

type Alphabet struct {
	// contains filtered or unexported fields
}

func NewAlphabet

func NewAlphabet(s string) (Alphabet, error)

func (*Alphabet) IsDef

func (self *Alphabet) IsDef() bool

func (*Alphabet) Len

func (self *Alphabet) Len() int

func (*Alphabet) PosOf

func (self *Alphabet) PosOf(c rune) int

func (*Alphabet) ValAt

func (self *Alphabet) ValAt(i int) rune

type FF1

type FF1 struct {
	// contains filtered or unexported fields
}

Context structure for FF1 FPE algorithm

func NewFF1

func NewFF1(key, twk []byte, mintwk, maxtwk, radix int, args ...interface{}) (
	*FF1, error)

Allocate a new FF1 context structure

@key specifies the key for the algorthim, the length of which will determine the underlying aes encryption to use.

@twk specifies the default tweak to be used if one is not specified to the Encrypt or Decrypt functions. nil is allowed

@mintwk and @maxtwk specify the minimum and maximum tweak sizes allowed by the algorithm. both may be set to 0 to indicate that there is no limit on the tweak size

@radix specifies the radix of the input/output data

the function also accepts an optional argument: @alpha is a string containing the alphabet for numerical conversions

func (*FF1) Decrypt

func (this *FF1) Decrypt(X string, T []byte) (Y string, err error)

Decrypt a string @X with the tweak @T

@T may be nil, in which case the default tweak will be used

func (*FF1) DecryptRunes

func (this *FF1) DecryptRunes(X []rune, T []byte) ([]rune, error)

func (*FF1) Encrypt

func (this *FF1) Encrypt(X string, T []byte) (Y string, err error)

Encrypt a string @X with the tweak @T

@T may be nil, in which case the default tweak will be used

func (*FF1) EncryptRunes

func (this *FF1) EncryptRunes(X []rune, T []byte) ([]rune, error)

type FF3_1

type FF3_1 struct {
	// contains filtered or unexported fields
}

Context structure for the FF3-1 FPE algorithm

func NewFF3_1

func NewFF3_1(key, twk []byte, radix int, args ...interface{}) (*FF3_1, error)

Allocate a new FF3-1 context structure

@key specifies the key for the algorthim, the length of which will determine the underlying aes encryption to use.

@twk specifies the default tweak to be used. the tweak must be exactly 7 bytes long

@radix specifies the radix of the input/output data

the function also accepts an optional argument: @alpha is a string containing the alphabet for numerical conversions

func (*FF3_1) Decrypt

func (this *FF3_1) Decrypt(X string, T []byte) (Y string, err error)

Decrypt a string @X with the tweak @T

@T may be nil, in which case the default tweak will be used

func (*FF3_1) DecryptRunes

func (this *FF3_1) DecryptRunes(X []rune, T []byte) ([]rune, error)

func (*FF3_1) Encrypt

func (this *FF3_1) Encrypt(X string, T []byte) (Y string, err error)

Encrypt a string @X with the tweak @T

@T may be nil, in which case the default tweak will be used

func (*FF3_1) EncryptRunes

func (this *FF3_1) EncryptRunes(X []rune, T []byte) ([]rune, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL