Module: JWE::Enc::AesGcm

Included in:
A128gcm, A192gcm, A256gcm
Defined in:
lib/jwe/enc/aes_gcm.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cekObject

Returns the value of attribute cek.



4
5
6
# File 'lib/jwe/enc/aes_gcm.rb', line 4

def cek
  @cek
end

#ivObject

Returns the value of attribute iv.



5
6
7
# File 'lib/jwe/enc/aes_gcm.rb', line 5

def iv
  @iv
end

#tagObject

Returns the value of attribute tag.



6
7
8
# File 'lib/jwe/enc/aes_gcm.rb', line 6

def tag
  @tag
end

Class Method Details

.included(base) ⇒ Object



59
60
61
# File 'lib/jwe/enc/aes_gcm.rb', line 59

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#cipherObject



49
50
51
52
53
# File 'lib/jwe/enc/aes_gcm.rb', line 49

def cipher
  @cipher ||= OpenSSL::Cipher.new(cipher_name)
rescue RuntimeError
  raise JWE::NotImplementedError.new("The version of OpenSSL linked to your Ruby does not support the cipher #{cipher_name}.")
end

#decrypt(ciphertext, authenticated_data) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/jwe/enc/aes_gcm.rb', line 27

def decrypt(ciphertext, authenticated_data)
  raise JWE::BadCEK.new("The supplied key is too short. Required length: #{key_length}") if cek.length < key_length

  cipher.decrypt
  cipher.key = cek
  cipher.iv = iv
  cipher.auth_tag = tag
  cipher.auth_data = authenticated_data

  cipher.update(ciphertext) + cipher.final
rescue OpenSSL::Cipher::CipherError
  raise JWE::InvalidData.new("Invalid ciphertext or authentication tag")
end

#encrypt(cleartext, authenticated_data) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jwe/enc/aes_gcm.rb', line 13

def encrypt(cleartext, authenticated_data)
  raise JWE::BadCEK.new("The supplied key is too short. Required length: #{key_length}") if cek.length < key_length

  cipher.encrypt
  cipher.key = cek
  cipher.iv = iv
  cipher.auth_data = authenticated_data

  ciphertext = cipher.update(cleartext) + cipher.final
  self.tag = cipher.auth_tag

  ciphertext
end

#initialize(cek = nil, iv = nil) ⇒ Object



8
9
10
11
# File 'lib/jwe/enc/aes_gcm.rb', line 8

def initialize(cek = nil, iv = nil)
  self.iv = iv
  self.cek = cek
end