Skip to content

Commit d448fca

Browse files
committed
Add RsaOaep256 algorithm
1 parent d7dc137 commit d448fca

File tree

4 files changed

+47
-1
lines changed

4 files changed

+47
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ Only a subset of these algorithms is implemented in this gem. Striked elements a
9696
Key management:
9797
* RSA1_5
9898
* RSA-OAEP (default)
99-
* ~~RSA-OAEP-256~~
99+
* RSA-OAEP-256 (if OpenSSL::VERSION >= '3.0')
100100
* A128KW
101101
* A192KW
102102
* A256KW

lib/jwe/alg.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require 'jwe/alg/a256_kw'
66
require 'jwe/alg/dir'
77
require 'jwe/alg/rsa_oaep'
8+
require 'jwe/alg/rsa_oaep_256' if OpenSSL::VERSION >= '3.0'
89
require 'jwe/alg/rsa15'
910

1011
module JWE

lib/jwe/alg/rsa_oaep_256.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# frozen_string_literal: true
2+
3+
module JWE
4+
module Alg
5+
# RSA-OAEP-256 key encryption algorithm.
6+
class RsaOaep256
7+
attr_accessor :key
8+
9+
def initialize(key)
10+
self.key = key
11+
end
12+
13+
def encrypt(cek)
14+
key.encrypt(cek, { rsa_padding_mode: 'oaep', rsa_oaep_md: 'sha256' })
15+
end
16+
17+
def decrypt(encrypted_cek)
18+
key.decrypt(encrypted_cek, { rsa_padding_mode: 'oaep', rsa_oaep_md: 'sha256' })
19+
end
20+
end
21+
end
22+
end

spec/jwe/alg_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,29 @@
5555
end
5656
end
5757

58+
if OpenSSL::VERSION >= '3.0'
59+
describe JWE::Alg::RsaOaep256 do
60+
let(:alg) { JWE::Alg::RsaOaep256.new(key) }
61+
62+
describe '#encrypt' do
63+
it 'returns an encrypted string' do
64+
expect(alg.encrypt('random key')).to_not eq 'random key'
65+
end
66+
end
67+
68+
it 'decrypts the encrypted key to the original key' do
69+
ciphertext = alg.encrypt('random key')
70+
expect(alg.decrypt(ciphertext)).to eq 'random key'
71+
end
72+
end
73+
else
74+
describe JWE::Alg do
75+
it 'raises an error for a rsa-oaep-256 if openssl < 3.0' do
76+
expect { JWE::Alg.for('rsa-oaep-256') }.to raise_error(JWE::NotImplementedError)
77+
end
78+
end
79+
end
80+
5881
describe JWE::Alg::Rsa15 do
5982
let(:alg) { JWE::Alg::Rsa15.new(key) }
6083

0 commit comments

Comments
 (0)