Class: JOSE::JWE::ENC_C20P
- Inherits:
-
Struct
- Object
- Struct
- JOSE::JWE::ENC_C20P
- Defined in:
- lib/jose/jwe/enc_c20p.rb
Instance Attribute Summary collapse
-
#bits ⇒ Object
Returns the value of attribute bits.
-
#cek_len ⇒ Object
Returns the value of attribute cek_len.
-
#cipher_name ⇒ Object
Returns the value of attribute cipher_name.
-
#iv_len ⇒ Object
Returns the value of attribute iv_len.
Class Method Summary collapse
-
.from_map(fields) ⇒ Object
JOSE::JWE callbacks.
Instance Method Summary collapse
-
#algorithm ⇒ Object
JOSE::JWE::ENC callbacks.
- #block_decrypt(aad_cipher_text_cipher_tag, cek, iv) ⇒ Object
- #block_encrypt(aad_plain_text, cek, iv) ⇒ Object
- #next_cek ⇒ Object
- #next_iv ⇒ Object
- #to_map(fields) ⇒ Object
Instance Attribute Details
#bits ⇒ Object
Returns the value of attribute bits
1 2 3 |
# File 'lib/jose/jwe/enc_c20p.rb', line 1 def bits @bits end |
#cek_len ⇒ Object
Returns the value of attribute cek_len
1 2 3 |
# File 'lib/jose/jwe/enc_c20p.rb', line 1 def cek_len @cek_len end |
#cipher_name ⇒ Object
Returns the value of attribute cipher_name
1 2 3 |
# File 'lib/jose/jwe/enc_c20p.rb', line 1 def cipher_name @cipher_name end |
#iv_len ⇒ Object
Returns the value of attribute iv_len
1 2 3 |
# File 'lib/jose/jwe/enc_c20p.rb', line 1 def iv_len @iv_len end |
Class Method Details
.from_map(fields) ⇒ Object
JOSE::JWE callbacks
5 6 7 8 9 10 11 12 |
# File 'lib/jose/jwe/enc_c20p.rb', line 5 def self.from_map(fields) case fields['enc'] when 'C20P' return new('chacha20-poly1305', 256, 32, 12), fields.delete('enc') else raise ArgumentError, "invalid 'enc' for JWE: #{fields['enc'].inspect}" end end |
Instance Method Details
#algorithm ⇒ Object
JOSE::JWE::ENC callbacks
20 21 22 23 24 25 26 27 |
# File 'lib/jose/jwe/enc_c20p.rb', line 20 def algorithm case cipher_name when 'chacha20-poly1305' return 'C20P' else raise ArgumentError, "unhandled JOSE::JWE::ENC_C20P cipher name: #{cipher_name.inspect}" end end |
#block_decrypt(aad_cipher_text_cipher_tag, cek, iv) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/jose/jwe/enc_c20p.rb', line 29 def block_decrypt(aad_cipher_text_cipher_tag, cek, iv) aad, cipher_text, cipher_tag = aad_cipher_text_cipher_tag cipher = OpenSSL::Cipher.new(cipher_name) cipher.decrypt cipher.key = cek cipher.iv = iv cipher.padding = 0 cipher.auth_data = aad cipher.auth_tag = cipher_tag plain_text = cipher.update(cipher_text) + cipher.final return plain_text end |
#block_encrypt(aad_plain_text, cek, iv) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/jose/jwe/enc_c20p.rb', line 42 def block_encrypt(aad_plain_text, cek, iv) aad, plain_text = aad_plain_text cipher = OpenSSL::Cipher.new(cipher_name) cipher.encrypt cipher.key = cek cipher.iv = iv cipher.padding = 0 cipher.auth_data = aad cipher_text = cipher.update(plain_text) + cipher.final return cipher_text, cipher.auth_tag end |
#next_cek ⇒ Object
54 55 56 |
# File 'lib/jose/jwe/enc_c20p.rb', line 54 def next_cek return SecureRandom.random_bytes(cek_len) end |
#next_iv ⇒ Object
58 59 60 |
# File 'lib/jose/jwe/enc_c20p.rb', line 58 def next_iv return SecureRandom.random_bytes(iv_len) end |
#to_map(fields) ⇒ Object
14 15 16 |
# File 'lib/jose/jwe/enc_c20p.rb', line 14 def to_map(fields) return fields.put('enc', algorithm) end |