Skip to content

Commit

Permalink
Change default cipher to authenticated GCM mode.
Browse files Browse the repository at this point in the history
https://ruby-doc.org/stdlib-2.4.0/libdoc/openssl/rdoc/OpenSSL/Cipher.html#class-OpenSSL::Cipher-label-Authenticated+Encryption+and+Associated+Data+-28AEAD-29

> If the OpenSSL version used supports it, an Authenticated Encryption mode (such as GCM or CCM)
> should always be preferred over any unauthenticated mode. Currently, OpenSSL supports AE only
> in combination with Associated Data (AEAD) where additional associated data is included in the encryption process
> to compute a tag at the end of the encryption. This tag will also be used in the decryption process and by verifying
> its validity, the authenticity of a given ciphertext is established.

Closes #567.
  • Loading branch information
mostlyobvious committed Apr 17, 2019
1 parent 4d7167d commit a13658c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
22 changes: 13 additions & 9 deletions ruby_event_store/lib/ruby_event_store/mappers/encryption_mapper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def respond_to_missing?(*)
end

class InMemoryEncryptionKeyRepository
DEFAULT_CIPHER = 'aes-256-cbc'.freeze
DEFAULT_CIPHER = 'aes-256-gcm'.freeze

def initialize
@keys = {}
Expand Down Expand Up @@ -60,17 +60,21 @@ def initialize(cipher:, key:)
end

def encrypt(message, iv)
crypto = prepare_encrypt(cipher)
crypto.iv = iv
crypto.key = key
crypto.update(message) + crypto.final
crypto = prepare_encrypt(cipher)
crypto.iv = iv
crypto.key = key
crypto.auth_data = ""
crypto.update(message) + crypto.final + crypto.auth_tag
end

def decrypt(message, iv)
crypto = prepare_decrypt(cipher)
crypto.iv = iv
crypto.key = key
(crypto.update(message) + crypto.final).force_encoding("UTF-8")
ciphertext, auth_tag = message[0...-16], message[-16...]
crypto = prepare_decrypt(cipher)
crypto.iv = iv
crypto.key = key
crypto.auth_tag = auth_tag
crypto.auth_data = ""
(crypto.update(ciphertext) + crypto.final).force_encoding("UTF-8")
end

def random_iv
Expand Down
2 changes: 1 addition & 1 deletion ruby_event_store/spec/mappers/encryption_mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ def decrypt(r)

record = encrypt(ticket_transferred)

with_default_cipher('aes-128-cbc') do
with_default_cipher('aes-128-gcm') do
event = decrypt(record)

expect(event.event_id).to eq(event_id)
Expand Down

0 comments on commit a13658c

Please sign in to comment.