Skip to content

Commit 46bb3f8

Browse files
committed
Reduced AES GCM complexity
1 parent 10f23cb commit 46bb3f8

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed

lib/jwe/enc/aes_gcm.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,30 @@ def initialize(cek = nil, iv = nil)
1414
end
1515

1616
def encrypt(cleartext, authenticated_data)
17-
raise JWE::BadCEK.new("The supplied key is too short. Required length: #{key_length}") if cek.length < key_length
18-
19-
cipher.encrypt
20-
cipher.key = cek
21-
cipher.iv = iv
22-
cipher.auth_data = authenticated_data
17+
raise JWE::BadCEK, "The supplied key is too short. Required length: #{key_length}" if cek.length < key_length
2318

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

2723
ciphertext
2824
end
2925

3026
def decrypt(ciphertext, authenticated_data)
31-
raise JWE::BadCEK.new("The supplied key is too short. Required length: #{key_length}") if cek.length < key_length
32-
33-
cipher.decrypt
34-
cipher.key = cek
35-
cipher.iv = iv
36-
cipher.auth_tag = tag
37-
cipher.auth_data = authenticated_data
27+
raise JWE::BadCEK, "The supplied key is too short. Required length: #{key_length}" if cek.length < key_length
3828

29+
setup_cipher(:decrypt, authenticated_data)
3930
cipher.update(ciphertext) + cipher.final
4031
rescue OpenSSL::Cipher::CipherError
41-
raise JWE::InvalidData.new('Invalid ciphertext or authentication tag')
32+
raise JWE::InvalidData, 'Invalid ciphertext or authentication tag'
33+
end
34+
35+
def setup_cipher(direction, auth_data)
36+
cipher.send(direction)
37+
cipher.key = cek
38+
cipher.iv = iv
39+
cipher.auth_tag = tag if direction == :decrypt
40+
cipher.auth_data = auth_data
4241
end
4342

4443
def iv

0 commit comments

Comments
 (0)