Skip to content

Commit b08ae7e

Browse files
bdewaterioquatix
authored andcommitted
Look up cipher by name instead of constant
1 parent eae30d2 commit b08ae7e

File tree

2 files changed

+8
-24
lines changed

2 files changed

+8
-24
lines changed

ext/openssl/ossl_cipher.c

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -851,22 +851,6 @@ Init_ossl_cipher(void)
851851
*
852852
* cipher = OpenSSL::Cipher.new('AES-128-CBC')
853853
*
854-
* For each algorithm supported, there is a class defined under the
855-
* Cipher class that goes by the name of the cipher, e.g. to obtain an
856-
* instance of AES, you could also use
857-
*
858-
* # these are equivalent
859-
* cipher = OpenSSL::Cipher::AES.new(128, :CBC)
860-
* cipher = OpenSSL::Cipher::AES.new(128, 'CBC')
861-
* cipher = OpenSSL::Cipher::AES.new('128-CBC')
862-
*
863-
* Finally, due to its wide-spread use, there are also extra classes
864-
* defined for the different key sizes of AES
865-
*
866-
* cipher = OpenSSL::Cipher::AES128.new(:CBC)
867-
* cipher = OpenSSL::Cipher::AES192.new(:CBC)
868-
* cipher = OpenSSL::Cipher::AES256.new(:CBC)
869-
*
870854
* === Choosing either encryption or decryption mode
871855
*
872856
* Encryption and decryption are often very similar operations for
@@ -895,7 +879,7 @@ Init_ossl_cipher(void)
895879
* without processing the password further. A simple and secure way to
896880
* create a key for a particular Cipher is
897881
*
898-
* cipher = OpenSSL::Cipher::AES256.new(:CFB)
882+
* cipher = OpenSSL::Cipher.new('AES-256-CFB')
899883
* cipher.encrypt
900884
* key = cipher.random_key # also sets the generated key on the Cipher
901885
*
@@ -963,14 +947,14 @@ Init_ossl_cipher(void)
963947
*
964948
* data = "Very, very confidential data"
965949
*
966-
* cipher = OpenSSL::Cipher::AES.new(128, :CBC)
950+
* cipher = OpenSSL::Cipher.new('AES-128-CBC')
967951
* cipher.encrypt
968952
* key = cipher.random_key
969953
* iv = cipher.random_iv
970954
*
971955
* encrypted = cipher.update(data) + cipher.final
972956
* ...
973-
* decipher = OpenSSL::Cipher::AES.new(128, :CBC)
957+
* decipher = OpenSSL::Cipher.new('AES-128-CBC')
974958
* decipher.decrypt
975959
* decipher.key = key
976960
* decipher.iv = iv
@@ -1006,7 +990,7 @@ Init_ossl_cipher(void)
1006990
* not to reuse the _key_ and _nonce_ pair. Reusing an nonce ruins the
1007991
* security guarantees of GCM mode.
1008992
*
1009-
* cipher = OpenSSL::Cipher::AES.new(128, :GCM).encrypt
993+
* cipher = OpenSSL::Cipher.new('AES-128-GCM').encrypt
1010994
* cipher.key = key
1011995
* cipher.iv = nonce
1012996
* cipher.auth_data = auth_data
@@ -1022,7 +1006,7 @@ Init_ossl_cipher(void)
10221006
* ciphertext with a probability of 1/256.
10231007
*
10241008
* raise "tag is truncated!" unless tag.bytesize == 16
1025-
* decipher = OpenSSL::Cipher::AES.new(128, :GCM).decrypt
1009+
* decipher = OpenSSL::Cipher.new('AES-128-GCM').decrypt
10261010
* decipher.key = key
10271011
* decipher.iv = nonce
10281012
* decipher.auth_tag = tag

test/openssl/test_cipher.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,12 @@ def test_ciphers
148148
def test_AES
149149
pt = File.read(__FILE__)
150150
%w(ECB CBC CFB OFB).each{|mode|
151-
c1 = OpenSSL::Cipher::AES256.new(mode)
151+
c1 = OpenSSL::Cipher.new("AES-256-#{mode}")
152152
c1.encrypt
153153
c1.pkcs5_keyivgen("passwd")
154154
ct = c1.update(pt) + c1.final
155155

156-
c2 = OpenSSL::Cipher::AES256.new(mode)
156+
c2 = OpenSSL::Cipher.new("AES-256-#{mode}")
157157
c2.decrypt
158158
c2.pkcs5_keyivgen("passwd")
159159
assert_equal(pt, c2.update(ct) + c2.final)
@@ -163,7 +163,7 @@ def test_AES
163163
def test_update_raise_if_key_not_set
164164
assert_raise(OpenSSL::Cipher::CipherError) do
165165
# it caused OpenSSL SEGV by uninitialized key [Bug #2768]
166-
OpenSSL::Cipher::AES128.new("ECB").update "." * 17
166+
OpenSSL::Cipher.new("AES-128-ECB").update "." * 17
167167
end
168168
end
169169

0 commit comments

Comments
 (0)