diff --git a/README.md b/README.md index 64db0bc..1697b71 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ Only a subset of these algorithms is implemented in this gem. Striked elements a Key management: * RSA1_5 * RSA-OAEP (default) -* ~~RSA-OAEP-256~~ +* RSA-OAEP-256 (if OpenSSL::VERSION >= '3.0') * A128KW * A192KW * A256KW diff --git a/lib/jwe/alg.rb b/lib/jwe/alg.rb index b320c6d..6158cc7 100644 --- a/lib/jwe/alg.rb +++ b/lib/jwe/alg.rb @@ -5,6 +5,7 @@ require 'jwe/alg/a256_kw' require 'jwe/alg/dir' require 'jwe/alg/rsa_oaep' +require 'jwe/alg/rsa_oaep_256' if OpenSSL::VERSION >= '3.0' require 'jwe/alg/rsa15' module JWE diff --git a/lib/jwe/alg/rsa_oaep_256.rb b/lib/jwe/alg/rsa_oaep_256.rb new file mode 100644 index 0000000..de25252 --- /dev/null +++ b/lib/jwe/alg/rsa_oaep_256.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +module JWE + module Alg + # RSA-OAEP-256 key encryption algorithm. + class RsaOaep256 + attr_accessor :key + + def initialize(key) + self.key = key + end + + def encrypt(cek) + key.encrypt(cek, { rsa_padding_mode: 'oaep', rsa_oaep_md: 'sha256' }) + end + + def decrypt(encrypted_cek) + key.decrypt(encrypted_cek, { rsa_padding_mode: 'oaep', rsa_oaep_md: 'sha256' }) + end + end + end +end diff --git a/spec/jwe/alg_spec.rb b/spec/jwe/alg_spec.rb index 467eac1..a112163 100644 --- a/spec/jwe/alg_spec.rb +++ b/spec/jwe/alg_spec.rb @@ -55,6 +55,29 @@ end end +if OpenSSL::VERSION >= '3.0' + describe JWE::Alg::RsaOaep256 do + let(:alg) { JWE::Alg::RsaOaep256.new(key) } + + describe '#encrypt' do + it 'returns an encrypted string' do + expect(alg.encrypt('random key')).to_not eq 'random key' + end + end + + it 'decrypts the encrypted key to the original key' do + ciphertext = alg.encrypt('random key') + expect(alg.decrypt(ciphertext)).to eq 'random key' + end + end +else + describe JWE::Alg do + it 'raises an error for rsa-oaep-256 if openssl < 3.0' do + expect { JWE::Alg.for('rsa-oaep-256') }.to raise_error(JWE::NotImplementedError) + end + end +end + describe JWE::Alg::Rsa15 do let(:alg) { JWE::Alg::Rsa15.new(key) }