Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SJCL AES-128-CCM decryption in Ruby

I am trying to implement the SJCL decryption of aes-128-ccm encrypted strings in Ruby. After reading a similar question I see that this should be supported in newer versions of the OpenSSL library, and so i have installed the development version from github to /opt

After doing this I don't see the aes-128-ccm cipher in the list when I run /opt/bin/openssl ciphers:

ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:SRP-DSS-AES-256-CBC-SHA:SRP-RSA-AES-256-CBC-SHA:DH-DSS-AES256-GCM-SHA384:DHE-DSS-AES256-GCM-SHA384:DH-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA256:DH-RSA-AES256-SHA256:DH-DSS-AES256-SHA256:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:DH-RSA-AES256-SHA:DH-DSS-AES256-SHA:DHE-RSA-CAMELLIA256-SHA:DHE-DSS-CAMELLIA256-SHA:DH-RSA-CAMELLIA256-SHA:DH-DSS-CAMELLIA256-SHA:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES256-SHA:AES256-GCM-SHA384:AES256-SHA256:AES256-SHA:CAMELLIA256-SHA:PSK-AES256-CBC-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:SRP-DSS-3DES-EDE-CBC-SHA:SRP-RSA-3DES-EDE-CBC-SHA:EDH-RSA-DES-CBC3-SHA:EDH-DSS-DES-CBC3-SHA:DH-RSA-DES-CBC3-SHA:DH-DSS-DES-CBC3-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-DES-CBC3-SHA:DES-CBC3-SHA:PSK-3DES-EDE-CBC-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:SRP-DSS-AES-128-CBC-SHA:SRP-RSA-AES-128-CBC-SHA:DH-DSS-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:DH-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:DHE-DSS-AES128-SHA256:DH-RSA-AES128-SHA256:DH-DSS-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA:DH-RSA-AES128-SHA:DH-DSS-AES128-SHA:DHE-RSA-SEED-SHA:DHE-DSS-SEED-SHA:DH-RSA-SEED-SHA:DH-DSS-SEED-SHA:DHE-RSA-CAMELLIA128-SHA:DHE-DSS-CAMELLIA128-SHA:DH-RSA-CAMELLIA128-SHA:DH-DSS-CAMELLIA128-SHA:ECDH-RSA-AES128-GCM-SHA256:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDH-RSA-AES128-SHA:ECDH-ECDSA-AES128-SHA:AES128-GCM-SHA256:AES128-SHA256:AES128-SHA:SEED-SHA:CAMELLIA128-SHA:IDEA-CBC-SHA:PSK-AES128-CBC-SHA:ECDHE-RSA-RC4-SHA:ECDHE-ECDSA-RC4-SHA:ECDH-RSA-RC4-SHA:ECDH-ECDSA-RC4-SHA:RC4-SHA:RC4-MD5:PSK-RC4-SHA:EDH-RSA-DES-CBC-SHA:EDH-DSS-DES-CBC-SHA:DH-RSA-DES-CBC-SHA:DH-DSS-DES-CBC-SHA:DES-CBC-SHA:EXP-EDH-RSA-DES-CBC-SHA:EXP-EDH-DSS-DES-CBC-SHA:EXP-DH-RSA-DES-CBC-SHA:EXP-DH-DSS-DES-CBC-SHA:EXP-DES-CBC-SHA:EXP-RC2-CBC-MD5:EXP-RC4-MD5

But if I run /opt/bin/openssl enc -help 2>&1 I see "-aes-128-ccm":

-aes-128-cbc               -aes-128-ccm               -aes-128-cfb              
-aes-128-cfb1              -aes-128-cfb8              -aes-128-ctr              
-aes-128-ecb               -aes-128-gcm               -aes-128-ofb              
-aes-128-xts               -aes-192-cbc               -aes-192-ccm              
-aes-192-cfb               -aes-192-cfb1              -aes-192-cfb8             
-aes-192-ctr               -aes-192-ecb               -aes-192-gcm              
-aes-192-ofb               -aes-256-cbc               -aes-256-ccm              
-aes-256-cfb               -aes-256-cfb1              -aes-256-cfb8             
-aes-256-ctr               -aes-256-ecb               -aes-256-gcm              
-aes-256-ofb               -aes-256-xts               -aes128                   
-aes192                    -aes256                    -bf                       
-bf-cbc                    -bf-cfb                    -bf-ecb                   
-bf-ofb                    -blowfish                  -camellia-128-cbc         
-camellia-128-cfb          -camellia-128-cfb1         -camellia-128-cfb8        
-camellia-128-ecb          -camellia-128-ofb          -camellia-192-cbc         
-camellia-192-cfb          -camellia-192-cfb1         -camellia-192-cfb8        
-camellia-192-ecb          -camellia-192-ofb          -camellia-256-cbc         
-camellia-256-cfb          -camellia-256-cfb1         -camellia-256-cfb8        
-camellia-256-ecb          -camellia-256-ofb          -camellia128              
-camellia192               -camellia256               -cast                     
-cast-cbc                  -cast5-cbc                 -cast5-cfb                
-cast5-ecb                 -cast5-ofb                 -des                      
-des-cbc                   -des-cfb                   -des-cfb1                 
-des-cfb8                  -des-ecb                   -des-ede                  
-des-ede-cbc               -des-ede-cfb               -des-ede-ofb              
-des-ede3                  -des-ede3-cbc              -des-ede3-cfb             
-des-ede3-cfb1             -des-ede3-cfb8             -des-ede3-ofb             
-des-ofb                   -des3                      -desx                     
-desx-cbc                  -gost89                    -gost89-cnt               
-id-aes128-CCM             -id-aes128-GCM             -id-aes192-CCM            
-id-aes192-GCM             -id-aes256-CCM             -id-aes256-GCM            
-idea                      -idea-cbc                  -idea-cfb                 
-idea-ecb                  -idea-ofb                  -rc2                      
-rc2-40-cbc                -rc2-64-cbc                -rc2-cbc                  
-rc2-cfb                   -rc2-ecb                   -rc2-ofb                  
-rc4                       -rc4-40                    -rc4-hmac-md5             
-seed                      -seed-cbc                  -seed-cfb                 
-seed-ecb                  -seed-ofb           

I reinstalled ruby 2.0.0p0 with rvm using the command: rvm reinstall 2.0.0 --with-openssl-dir=/opt (given in the aforementioned article). And then ran cipher = OpenSSL::Cipher.new('aes-128-ccm'). Which returned the following error:

unsupported cipher algorithm (aes-128-ccm)

My question is how can I add support for AES-128-CCM in Ruby / What am I doing wrong here?

like image 484
dtr Avatar asked Apr 16 '13 02:04

dtr


1 Answers

Okay I've made it work, here is what I did:

First get the OpenSSL source from github:

$ git clone https://github.com/openssl/openssl.git
$ cd openssl/

If you want the exact same version as I've used, do:

$ git checkout 5ae8d6bcbaff99423a2608559d738a3fcf7ed6dc -b tmp

Now build OpenSSL with shared libraries in some directory:

$ ./config shared --prefix=/home/jbr/local/openssl
$ make depend
$ make
$ make install

Make sure that you have ccm-support:

$ /home/jbr/local/openssl/bin/openssl enc -help  2>&1 | grep "ccm"
-aes-128-ccm               -aes-128-cfb               -aes-128-cfb1             
-aes-192-cbc               -aes-192-ccm               -aes-192-cfb              
-aes-256-ccm               -aes-256-cfb               -aes-256-cfb1   

Okay, now install a named Ruby, using rvm and your new version of OpenSSL:

$ rvm install ruby-2.0.0-p195 -n ccm --with-openssl-dir=/home/jbr/local/openssl

This command gives you a version of Ruby 2.0.0 patch level 195 surfixed with -ccm, which uses your new OpenSSL libraries.

Now use that new version of Ruby:

$ rvm use ruby-2.0.0-p195-ccm

And test it out with irb:

$ irb
2.0.0p195 :001 > require 'openssl'
 => true
2.0.0p195 :005 > OpenSSL::Cipher.ciphers.include? "aes-128-ccm"
 => true 

You now have aes-128-ccm.

like image 72
jbr Avatar answered Oct 07 '22 08:10

jbr