Class: Cyphera::FF31

Inherits:
Object
  • Object
show all
Defined in:
lib/cyphera/ff3.rb

Overview

FF3-1 Format-Preserving Encryption (NIST SP 800-38G Revision 1).

FF3-1 is FF3 with a 56-bit (7-byte) tweak. The tweak is expanded into the 64-bit form the FF3 round function consumes; the algorithm is identical FF3.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') ⇒ FF31

Returns a new instance of FF31.



172
173
174
175
176
177
# File 'lib/cyphera/ff3.rb', line 172

def initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  unless tweak.bytesize == 7
    raise ArgumentError, "FF3-1 tweak must be exactly 7 bytes (56 bits)"
  end
  @inner = FF3.new(key, FF31.expand_tweak(tweak), alphabet)
end

Class Method Details

.expand_tweak(t) ⇒ Object

Expand the 56-bit FF3-1 tweak into the 64-bit tweak the FF3 round function consumes (NIST SP 800-38G Rev 1): bytes = T_L, [4,8] = T_R.



189
190
191
192
# File 'lib/cyphera/ff3.rb', line 189

def self.expand_tweak(t)
  b = t.bytes
  [b[0], b[1], b[2], b[3] & 0xF0, b[4], b[5], b[6], (b[3] & 0x0F) << 4].pack('C*')
end

Instance Method Details

#decrypt(ciphertext) ⇒ Object



183
184
185
# File 'lib/cyphera/ff3.rb', line 183

def decrypt(ciphertext)
  @inner.decrypt(ciphertext)
end

#encrypt(plaintext) ⇒ Object



179
180
181
# File 'lib/cyphera/ff3.rb', line 179

def encrypt(plaintext)
  @inner.encrypt(plaintext)
end