Ruby OpenSSL Library - IV Reuse in GCM Mode
The Ruby OpenSSL library reuses a static IV when encrypting with AES-GCM if the IV is set before the key, despite calling random_iv. An attacker who obtains multiple ciphertexts can decrypt them using a two-time pad attack.
Originally disclosed on HackerOne
- Fixed in 11 months
- Paid in 11 months
- Disclosed after 4.5 years
Hello,
An IV reuse bug was discovered in Ruby's OpenSSL library when using aes-gcm. When encrypting data with aes-*-gcm, if the IV is set before setting the key, the cipher will default to using a static IV. This creates a static nonce and since aes-gcm is a stream cipher, this can lead to known cryptographic issues.
The documentation does not appear to specify the order of operations when setting the key and IV [1]. As an example, see the following insecure code snippet below:
Vulnerable Code:
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new('aes-256-gcm')
iv = cipher.random_iv # Notice here the IV is set before the key
cipher.key = '11111111111111111111111111111111'
cipher.auth_data = ""
ciphertext = cipher.update(plaintext) + cipher.final
tag = cipher.auth_tag
puts "[+] Encrypting: #{plaintext}"
puts "[+] CipherMessage (IV | Tag | Ciphertext): #{bin2hex(iv)} |
#{bin2hex(tag)} | #{bin2hex(ciphertext)}"
end
A developer that uses the code above may incorrectly assume that their code is secure from the pitfalls associated with IV reuse in aes-*-gcm, since the ‘cipher.random_iv’ method is used. According to the documentation, this should generate a random IV each time the encryption method is called.
When the code above is run with the same key and same plaintext message, the following results are obtained:
Output:
# Run 1
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): e32594080cca2b37f7d7e968 |
8c676db7551cf046266252ee776ecaa9 | 81092d16b62902d9985656253891dc
800a5bb48fb1c4ad0b7bdf6054
# Run 2
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 431d70714f5e5f876d1c7830 |
8c676db7551cf046266252ee776ecaa9 | 81092d16b62902d9985656253891dc
800a5bb48fb1c4ad0b7bdf6054
Notice that in the output above a unique IV is returned for both runs, but with the same ciphertext. This proves that even though the random_iv method is called, the code is defaulting to a static IV. If an attacker can retrieve multiple ciphertext messages, it is possible to decrypt the ciphertexts by applying the same attack one would use in a two-time pad (XOR ciphertexts and crib drag).
Next review the following code snippet and output, which depicts a secure implementation of the code:
Valid Code:
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.key = '11111111111111111111111111111111'
iv = cipher.random_iv # Notice here the IV is set after the key
cipher.auth_data = ""
ciphertext = cipher.update(plaintext) + cipher.final
tag = cipher.auth_tag
puts "[+] Encrypting: #{plaintext}"
puts "[+] CipherMessage (IV | Tag | Ciphertext): #{bin2hex(iv)} |
#{bin2hex(tag)} | #{bin2hex(ciphertext)}"
end
Output:
# Run 1
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 8beb4aa05533e90f4f4eddd3 |
ea1b015958a9b8bd2aafa61887309caf | 19574a9c9869b92140a57a5fd43a14
9a5eaa7e5beefdff5d56cc4136
# Run 2
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 87361b3f1e32291602ac7b40 |
bce7093daa10cc9d2fad0f2b91e077f2 | 47f9a5ba55631204233ace70f169e6
65846e877dca11a6e13a659540
Notice that this time both the IV and ciphertexts are both different for the same plaintext. This is the intended result a developer would expect to happen when using this library.
Context
Ruby's OpenSSL library exposes authenticated encryption through OpenSSL::Cipher, and the report concerns the AES-GCM modes (aes-*-gcm). GCM is an AEAD mode: it produces a ciphertext plus an authentication tag, and it relies on a nonce (IV) that must never repeat under the same key. Because GCM behaves like a stream cipher internally, reusing an IV with the same key is a serious cryptographic mistake.
The feature under scrutiny is the random_iv method on the cipher object, which the documentation describes as generating a random IV each time it is called. The researcher found that setting the IV before the key causes the cipher to use a static IV.
Setup
The report does not describe any accounts, roles, tooling versions or remote infrastructure. The work is a local experiment: a short Ruby script that encrypts a plaintext with aes-256-gcm, run twice with the same key and the same plaintext, first with the IV set before the key and then with the IV set after the key. The only external reference cited is the Ruby standard library documentation for OpenSSL::Cipher.
Finding it
The researcher noticed that when encrypting with aes-*-gcm, if the IV is set before the key, the cipher falls back to using a static IV. That produces a static nonce, and since AES-GCM is a stream cipher, this leads to known cryptographic issues.
The documentation does not appear to specify the order of operations when setting the key and IV. A developer following the documented behaviour of random_iv would reasonably assume that calling it guarantees a fresh IV on every encryption, and therefore that the code is safe from IV reuse. The experiment below shows that assumption is wrong.
Exploitation
Step 1: encrypt with the IV set before the key
The vulnerable snippet sets random_iv first and the key second:
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new('aes-256-gcm')
iv = cipher.random_iv # Notice here the IV is set before the key
cipher.key = '11111111111111111111111111111111'
cipher.auth_data = ""
ciphertext = cipher.update(plaintext) + cipher.final
tag = cipher.auth_tag
puts "[+] Encrypting: #{plaintext}"
puts "[+] CipherMessage (IV | Tag | Ciphertext): #{bin2hex(iv)} |
#{bin2hex(tag)} | #{bin2hex(ciphertext)}"
end
The cipher is created for aes-256-gcm, random_iv is called before key is assigned, auth_data is set to an empty string, and the script prints the IV, tag and ciphertext in hex. The comment marks the ordering that matters: the IV is set before the key.
Step 2: run it twice with the same key and plaintext
First run:
# Run 1
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): e32594080cca2b37f7d7e968 |
[redacted] | 81092d16b62902d9985656253891dc
800a5bb48fb1c4ad0b7bdf6054
Second run:
# Run 2
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 431d70714f5e5f876d1c7830 |
[redacted] | 81092d16b62902d9985656253891dc
800a5bb48fb1c4ad0b7bdf6054
The IVs differ between the two runs (e32594080cca2b37f7d7e968 and 431d70714f5e5f876d1c7830), but the tag and the ciphertext are identical in both. That is the proof: although random_iv was called, the cipher actually used a static IV, so the same plaintext under the same key produced the same ciphertext and tag.
Step 3: encrypt with the IV set after the key
The researcher then reordered the two assignments:
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.key = '11111111111111111111111111111111'
iv = cipher.random_iv # Notice here the IV is set after the key
cipher.auth_data = ""
ciphertext = cipher.update(plaintext) + cipher.final
tag = cipher.auth_tag
puts "[+] Encrypting: #{plaintext}"
puts "[+] CipherMessage (IV | Tag | Ciphertext): #{bin2hex(iv)} |
#{bin2hex(tag)} | #{bin2hex(ciphertext)}"
end
Everything is the same except that key is assigned before random_iv is called.
Step 4: run the reordered version twice
First run:
# Run 1
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 8beb4aa05533e90f4f4eddd3 |
[redacted] | 19574a9c9869b92140a57a5fd43a14
9a5eaa7e5beefdff5d56cc4136
Second run:
# Run 2
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 87361b3f1e32291602ac7b40 |
[redacted] | 47f9a5ba55631204233ace70f169e6
65846e877dca11a6e13a659540
This time both the IVs and the ciphertexts differ between runs for the same plaintext, which is the intended result a developer would expect from the library.
Root cause
The mistake is an ordering dependency: setting the IV before the key causes the cipher to default to a static IV, so the same plaintext under the same key produces the same ciphertext and tag. The documentation does not appear to specify the required order of operations when setting the key and IV, so the safe usage is not obvious from the docs, and the random_iv call gives a false sense of security. The random_iv method, documented to generate a random IV per call, did not prevent the reuse in this ordering.
Impact
If an attacker can retrieve multiple ciphertext messages produced this way, it is possible to decrypt the ciphertexts by applying the same attack one would use against a two-time pad: XOR the ciphertexts together and crib drag. The report frames this conditionally, on the attacker obtaining multiple ciphertexts, and does not describe any specific deployment, victim or exfiltration path.
Fix
The report does not state what was changed. The safe pattern it demonstrates is to assign the key before calling random_iv, as in the valid snippet above, which yields a different IV and ciphertext on each run. Whether the library or its documentation was updated is not described.
Variants to test
-
Reorder key and IV assignments in any Ruby AES-GCM encryption path and encrypt the same plaintext twice. A positive result is identical ciphertext and tag across runs despite different printed IVs.
-
Audit code that calls
random_ivand check whetherkeyis assigned before or after it. A positive result is any call site whererandom_ivprecedes the key assignment. -
Where multiple ciphertexts under one key are observable, attempt the two-time-pad approach the report describes (XOR the ciphertexts and crib drag). A positive result is recoverable plaintext.
What happened, in plain words
A security researcher found a bug in Ruby's OpenSSL library, the part of Ruby that lets programs do encryption. The bug affects one particular way of encrypting data called AES-GCM. In short: if a programmer sets up the encryption in a certain order, the library quietly stops using a fresh random value for each message and reuses the same one every time. That is a well-known cryptographic mistake, and it can let someone who collects several encrypted messages work out what they say.
The researcher did not attack a website or a server. The whole thing is a small experiment run on a local computer: a short Ruby script, run twice, with the two settings swapped around. The report was disclosed on 7 March 2021 on HackerOne under the Ruby programme.
Before going further, a few ideas from scratch:
- Encryption is scrambling a message so that only someone with the right secret can unscramble it. The scrambled result is called ciphertext; the original message is called plaintext.
- A key is that secret. In the examples here it is a string of characters,
11111111111111111111111111111111. - AES is a widely used encryption algorithm. GCM is a mode of using AES that also produces a tag, a short value that lets the receiver check the message was not tampered with. Modes that do both jobs are called AEAD (Authenticated Encryption with Associated Data).
- An IV (initialisation vector), also called a nonce, is a value that must be different for every message encrypted under the same key. Think of it like the unique serial number stamped on each padlock from a set that all open with the same key: if two padlocks share a serial number, someone can tell they belong together, and worse follows.
- A stream cipher is an encryption method that works by generating a long stream of pseudo-random bytes from the key and IV and combining them with the message. If the same key and IV are used twice, the same stream comes out twice, and combining two ciphertexts cancels the stream out, leaving a relationship between the two plaintexts that can be attacked.
- An API is the set of commands a library offers to programmers. Here the API is Ruby's
OpenSSL::Cipherclass, and the feature in question is itsrandom_ivmethod, which the documentation says generates a random IV each time it is called.
Context
Ruby's OpenSSL library exposes authenticated encryption through OpenSSL::Cipher, and the report concerns the AES-GCM modes (aes-*-gcm). GCM is an AEAD mode: it produces a ciphertext plus an authentication tag, and it relies on a nonce (IV) that must never repeat under the same key. Because GCM behaves like a stream cipher internally, reusing an IV with the same key is a serious cryptographic mistake.
The feature under scrutiny is the random_iv method on the cipher object, which the documentation describes as generating a random IV each time it is called. The researcher found that setting the IV before the key causes the cipher to use a static IV.
Setup
The report does not describe any accounts, roles, tooling versions or remote infrastructure. The work is a local experiment: a short Ruby script that encrypts a plaintext with aes-256-gcm, run twice with the same key and the same plaintext, first with the IV set before the key and then with the IV set after the key. The only external reference cited is the Ruby standard library documentation for OpenSSL::Cipher.
Why run it twice? Because the whole question is whether two encryptions of the same message under the same key look different. If they do, each message got its own IV, as intended. If they look the same, the IV was effectively reused.
Finding it
The researcher noticed that when encrypting with aes-*-gcm, if the IV is set before the key, the cipher falls back to using a static IV. That produces a static nonce, and since AES-GCM is a stream cipher, this leads to known cryptographic issues.
The documentation does not appear to specify the order of operations when setting the key and IV. A developer following the documented behaviour of random_iv would reasonably assume that calling it guarantees a fresh IV on every encryption, and therefore that the code is safe from IV reuse. The experiment below shows that assumption is wrong.
Exploitation
Step 1: encrypt with the IV set before the key
The vulnerable snippet sets random_iv first and the key second:
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new('aes-256-gcm')
iv = cipher.random_iv # Notice here the IV is set before the key
cipher.key = '11111111111111111111111111111111'
cipher.auth_data = ""
ciphertext = cipher.update(plaintext) + cipher.final
tag = cipher.auth_tag
puts "[+] Encrypting: #{plaintext}"
puts "[+] CipherMessage (IV | Tag | Ciphertext): #{bin2hex(iv)} |
#{bin2hex(tag)} | #{bin2hex(ciphertext)}"
end
Line by line, in plain words:
def encrypt(plaintext)starts a function calledencryptthat takes the message to scramble.cipher = OpenSSL::Cipher.new('aes-256-gcm')creates a cipher object configured for AES-256 in GCM mode. This is the tool that will do the scrambling.iv = cipher.random_ivasks the library for a random IV and stores it. The comment flags the important part: this happens before the key is set.cipher.key = '11111111111111111111111111111111'sets the secret key.cipher.auth_data = ""sets the associated data to an empty string. Associated data is extra information GCM can authenticate without encrypting it; here there is none.ciphertext = cipher.update(plaintext) + cipher.finaldoes the actual encryption and collects the result.tag = cipher.auth_tagretrieves the authentication tag.- The two
putslines print the plaintext and then the IV, tag and ciphertext, each converted to hex (a two-character-per-byte text form) by a helper calledbin2hex.
The order of steps 3 and 4 is the whole point of the experiment.
Step 2: run it twice with the same key and plaintext
First run:
# Run 1
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): e32594080cca2b37f7d7e968 |
[redacted] | 81092d16b62902d9985656253891dc
800a5bb48fb1c4ad0b7bdf6054
Second run:
# Run 2
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 431d70714f5e5f876d1c7830 |
[redacted] | 81092d16b62902d9985656253891dc
800a5bb48fb1c4ad0b7bdf6054
The command line runs the script and passes the same message each time. The output prints three hex values separated by pipes: the IV, the tag and the ciphertext.
The IVs differ between the two runs (e32594080cca2b37f7d7e968 and 431d70714f5e5f876d1c7830), but the tag and the ciphertext are identical in both. That is the proof: although random_iv was called, the cipher actually used a static IV, so the same plaintext under the same key produced the same ciphertext and tag.
In other words, the printed IV changed between runs, yet the ciphertext and tag stayed the same. The printed IV changed, which is exactly what would fool a developer into thinking all was well, while the real nonce stayed the same.
Step 3: encrypt with the IV set after the key
The researcher then reordered the two assignments:
def encrypt(plaintext)
cipher = OpenSSL::Cipher.new('aes-256-gcm')
cipher.key = '11111111111111111111111111111111'
iv = cipher.random_iv # Notice here the IV is set after the key
cipher.auth_data = ""
ciphertext = cipher.update(plaintext) + cipher.final
tag = cipher.auth_tag
puts "[+] Encrypting: #{plaintext}"
puts "[+] CipherMessage (IV | Tag | Ciphertext): #{bin2hex(iv)} |
#{bin2hex(tag)} | #{bin2hex(ciphertext)}"
end
Everything is the same except that key is assigned before random_iv is called. The cipher is still created for aes-256-gcm, the associated data is still empty, and the same three values are still printed in hex. Only the order of the key and IV lines has changed, and the comment now marks that the IV is set after the key.
Step 4: run the reordered version twice
First run:
# Run 1
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 8beb4aa05533e90f4f4eddd3 |
[redacted] | 19574a9c9869b92140a57a5fd43a14
9a5eaa7e5beefdff5d56cc4136
Second run:
# Run 2
./gcm_encrypt.rb 'This is some secret message.'
[+] Encrypting: This is some secret message.
[+] CipherMessage (IV | Tag | Ciphertext): 87361b3f1e32291602ac7b40 |
[redacted] | 47f9a5ba55631204233ace70f169e6
65846e877dca11a6e13a659540
This time both the IVs and the ciphertexts differ between runs for the same plaintext, which is the intended result a developer would expect from the library. The tag differs too. Two encryptions of the same message now look completely unrelated, which is what a fresh nonce per message is supposed to achieve.
Root cause
The mistake is an ordering dependency: setting the IV before the key causes the cipher to default to a static IV, so the same plaintext under the same key produces the same ciphertext and tag. The documentation does not appear to specify the required order of operations when setting the key and IV, so the safe usage is not obvious from the docs, and the random_iv call gives a false sense of security. The random_iv method, documented to generate a random IV per call, did not prevent the reuse in this ordering.
Impact
If an attacker can retrieve multiple ciphertext messages produced this way, it is possible to decrypt the ciphertexts by applying the same attack one would use against a two-time pad: XOR the ciphertexts together and crib drag. The report frames this conditionally, on the attacker obtaining multiple ciphertexts, and does not describe any specific deployment, victim or exfiltration path.
Two terms in that paragraph need unpacking:
- XOR is a simple operation on two values, done bit by bit. Its useful property here is that combining two ciphertexts made with the same stream cancels the stream out, leaving a combination of the two plaintexts.
- Crib dragging is a guessing technique. The attacker slides a guessed word or phrase (a crib) along the combined result and checks whether the rest of the message becomes readable. It is the classic way to break a two-time pad, which is the name for the mistake of using the same keystream twice.
So the danger is not that a single message can be read. It is that once the same nonce is reused, messages stop being independent of each other, and patterns across them become exploitable.
Fix
The report does not state what was changed. The safe pattern it demonstrates is to assign the key before calling random_iv, as in the valid snippet above, which yields a different IV and ciphertext on each run. Whether the library or its documentation was updated is not described.
Variants to test
-
Reorder key and IV assignments in any Ruby AES-GCM encryption path and encrypt the same plaintext twice. A positive result is identical ciphertext and tag across runs despite different printed IVs.
-
Audit code that calls
random_ivand check whetherkeyis assigned before or after it. A positive result is any call site whererandom_ivprecedes the key assignment. -
Where multiple ciphertexts under one key are observable, attempt the two-time-pad approach the report describes (XOR the ciphertexts and crib drag). A positive result is recoverable plaintext.
Glossary
- AEAD (Authenticated Encryption with Associated Data): an encryption mode that both scrambles the message and produces a tag proving it was not altered.
- AES: a widely used symmetric encryption algorithm.
- AES-GCM: a mode of AES that provides authenticated encryption and needs a nonce that never repeats under the same key.
- API: the set of commands and objects a library offers to programmers.
- Authentication tag: a short value produced alongside the ciphertext that lets the receiver verify integrity.
- Cipher: the object or algorithm that performs encryption and decryption.
- Ciphertext: the scrambled form of a message.
- Crib drag: a technique for recovering plaintext from two messages encrypted with the same keystream, by sliding a guessed phrase along the result.
- Hex: a way of writing bytes as pairs of characters from 0 to 9 and a to f.
- IV (initialisation vector): a value that must be unique per message under a given key; also called a nonce.
- Key: the secret value that controls encryption and decryption.
- Nonce: another word for IV, a number used once.
- OpenSSL: a widely used cryptography library; Ruby exposes it through
OpenSSL::Cipher. - Plaintext: the original, readable message.
random_iv: a Ruby method documented to generate a random IV each time it is called.- Static IV: a fixed IV that does not change between encryptions, which defeats the purpose of having one.
- Stream cipher: an encryption method that generates a keystream from the key and IV and combines it with the message; reusing the same key and IV repeats the keystream.
- Symmetric encryption: encryption where the same secret key is used to encrypt and decrypt.
- Tag: see authentication tag.
- Two-time pad: the mistake of encrypting two messages with the same keystream, which makes both recoverable.
- XOR: a bit-by-bit operation that, among other uses, lets an attacker cancel out a reused keystream when combining two ciphertexts.