Module: JWE::Enc::AesGcm
Overview
Abstract AES in Galois Counter mode for different key sizes.
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#cek ⇒ Object
Returns the value of attribute cek.
-
#iv ⇒ Object
Returns the value of attribute iv.
-
#tag ⇒ Object
Returns the value of attribute tag.
Class Method Summary collapse
Instance Method Summary collapse
- #cipher ⇒ Object
- #decrypt(ciphertext, authenticated_data) ⇒ Object
- #encrypt(cleartext, authenticated_data) ⇒ Object
- #initialize(cek = nil, iv = nil) ⇒ Object
- #setup_cipher(direction, auth_data) ⇒ Object
Instance Attribute Details
#cek ⇒ Object
Returns the value of attribute cek.
7 8 9 |
# File 'lib/jwe/enc/aes_gcm.rb', line 7 def cek @cek end |
#iv ⇒ Object
Returns the value of attribute iv.
8 9 10 |
# File 'lib/jwe/enc/aes_gcm.rb', line 8 def iv @iv end |
#tag ⇒ Object
Returns the value of attribute tag.
9 10 11 |
# File 'lib/jwe/enc/aes_gcm.rb', line 9 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
#cipher ⇒ Object
51 52 53 |
# File 'lib/jwe/enc/aes_gcm.rb', line 51 def cipher @cipher ||= Cipher.for(cipher_name) end |
#decrypt(ciphertext, authenticated_data) ⇒ Object
26 27 28 29 30 31 32 33 |
# File 'lib/jwe/enc/aes_gcm.rb', line 26 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
16 17 18 19 20 21 22 23 24 |
# File 'lib/jwe/enc/aes_gcm.rb', line 16 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
11 12 13 14 |
# File 'lib/jwe/enc/aes_gcm.rb', line 11 def initialize(cek = nil, iv = nil) self.iv = iv self.cek = cek end |
#setup_cipher(direction, auth_data) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/jwe/enc/aes_gcm.rb', line 35 def setup_cipher(direction, auth_data) cipher.send(direction) cipher.key = cek cipher.iv = iv cipher.auth_tag = tag if direction == :decrypt cipher.auth_data = auth_data end |