class Utils::EcbOracle

Attributes

mode[R]
prefix[R]
suffix[R]

Public Class Methods

new(static_key: nil,static_mode: nil,block_size: 128,static_prefix: nil,static_suffix: nil,append: false, prepend: false) click to toggle source
# File lib/crypto-toolbox/utils/ecb_oracle.rb, line 5
def initialize(static_key: nil,static_mode: nil,block_size: 128,static_prefix: nil,static_suffix: nil,append: false, prepend: false)
  @key  = CryptBuffer(static_key)
  @mode = static_mode
  @iv   = nil
  @c    = nil
  @block_size = block_size
  
  @append  = append
  @prepend = prepend
  @suffix = static_suffix
  @prefix = static_prefix
end

Public Instance Methods

encipher(plaintext) click to toggle source
# File lib/crypto-toolbox/utils/ecb_oracle.rb, line 18
def encipher(plaintext)
  #support reproducable keys and mode
  @key      ||= CryptBuffer.random(16)
  @mode     ||= [:cbc,:ecb][SecureRandom.random_number(2)]
  message   = pad_message(plaintext) 
  method  = "encipher_#{@mode}".to_sym
  # we dispatch the method to avoid if-else dispatches
  # due to the difference of IV usage
  @c = send(method,message)
end

Private Instance Methods

append?() click to toggle source
# File lib/crypto-toolbox/utils/ecb_oracle.rb, line 57
def append?
  @append == true
end
encipher_cbc(plaintext) click to toggle source
# File lib/crypto-toolbox/utils/ecb_oracle.rb, line 45
def encipher_cbc(plaintext)
  @iv     = CryptBuffer.random(16)
  crypter = Ciphers::Aes.new
  crypter.send(:encipher_cbc,@key,plaintext,iv: @iv.str)
end
encipher_ecb(plaintext) click to toggle source
# File lib/crypto-toolbox/utils/ecb_oracle.rb, line 51
def encipher_ecb(plaintext)
  crypter = Ciphers::Aes.new
  crypter.send(:encipher_ecb,@key,plaintext)
end
pad_message(msg) click to toggle source
# File lib/crypto-toolbox/utils/ecb_oracle.rb, line 31
def pad_message(msg)
  pad_range = (5..10).to_a
  lpad_size = pad_range.sample
  rpad_size = pad_range.sample
  @prefix ||= SecureRandom.random_bytes(lpad_size)
  @suffix ||= SecureRandom.random_bytes(rpad_size)

  # NOTE PLEASE rewrite ME !!!
  msg = @prefix + msg if prepend?
  msg = msg + @suffix if append?
  
  msg
end
prepend?() click to toggle source
# File lib/crypto-toolbox/utils/ecb_oracle.rb, line 60
def prepend?
  @prepend == true
end