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.



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

def initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  unless tweak.bytesize == 7
    raise ArgumentError, "invalid tweak length: #{tweak.bytesize} (expected 7)"
  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.



195
196
197
198
# File 'lib/cyphera/ff3.rb', line 195

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



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

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

#encrypt(plaintext) ⇒ Object



185
186
187
# File 'lib/cyphera/ff3.rb', line 185

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