Module: JOSE::JWA::AES_KW

Extended by:
AES_KW
Included in:
AES_KW
Defined in:
lib/jose/jwa/aes_kw.rb

Constant Summary collapse

DEFAULT_IV =
OpenSSL::BN.new(0xA6A6A6A6A6A6A6A6).to_s(2)

Instance Method Summary collapse

Instance Method Details

#unwrap(cipher_text, kek, iv = nil) ⇒ Object


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/jose/jwa/aes_kw.rb', line 7

def unwrap(cipher_text, kek, iv = nil)
  iv ||= DEFAULT_IV
  bits = kek.bytesize * 8
  if cipher_text.bytesize % 8 == 0 and (bits == 128 or bits == 192 or bits == 256)
    block_count = cipher_text.bytesize.div(8) - 1
    buffer = do_unwrap(cipher_text, 5, block_count, kek, bits)
    buffer_s = StringIO.new(buffer)
    if buffer_s.read(iv.bytesize) != iv
      raise ArgumentError, "iv does not match"
    else
      plain_text = buffer_s.read
      return plain_text
    end
  else
    raise ArgumentError, "bad cipher_text, kek, or iv"
  end
end

#wrap(plain_text, kek, iv = nil) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/jose/jwa/aes_kw.rb', line 25

def wrap(plain_text, kek, iv = nil)
  iv ||= DEFAULT_IV
  bits = kek.bytesize * 8
  if plain_text.bytesize % 8 == 0 and (bits == 128 or bits == 192 or bits == 256)
    buffer = [iv, plain_text].join
    block_count = buffer.bytesize.div(8) - 1
    return do_wrap(buffer, 0, block_count, kek, bits)
  else
    raise ArgumentError, "bad plain_text, kek, or iv"
  end
end