Module: JWE::Enc::AesGcm

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

Overview

Abstract AES in Galois Counter mode for different key sizes.

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.



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

def cek
  @cek
end

#ivObject

Returns the value of attribute iv.



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

def iv
  @iv
end

#tagObject

Returns the value of attribute tag.



11
12
13
# File 'lib/jwe/enc/aes_gcm.rb', line 11

def tag
  @tag
end

Class Method Details

.included(base) ⇒ Object



65
66
67
# File 'lib/jwe/enc/aes_gcm.rb', line 65

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

Instance Method Details

#cipherObject



57
58
59
# File 'lib/jwe/enc/aes_gcm.rb', line 57

def cipher
  @cipher ||= Cipher.for(cipher_name)
end

#decrypt(ciphertext, authenticated_data) ⇒ Object



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

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

  setup_cipher(:decrypt, authenticated_data)
  cipher.update(ciphertext) + cipher.final
rescue OpenSSL::Cipher::CipherError
  raise JWE::InvalidData, 'Invalid ciphertext or authentication tag'
end

#encrypt(cleartext, authenticated_data) ⇒ Object

Raises:



18
19
20
21
22
23
24
25
26
# File 'lib/jwe/enc/aes_gcm.rb', line 18

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

  setup_cipher(:encrypt, authenticated_data)
  ciphertext = cipher.update(cleartext) + cipher.final
  self.tag = cipher.auth_tag

  ciphertext
end

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



13
14
15
16
# File 'lib/jwe/enc/aes_gcm.rb', line 13

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

#setup_cipher(direction, auth_data) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/jwe/enc/aes_gcm.rb', line 37

def setup_cipher(direction, auth_data)
  cipher.send(direction)
  cipher.key = cek
  cipher.iv = iv
  if direction == :decrypt
    raise JWE::InvalidData, 'Invalid ciphertext or authentication tag' unless tag.bytesize == 16

    cipher.auth_tag = tag
  end
  cipher.auth_data = auth_data
end