Module: JWE::Alg::AesKw
Overview
Generic AES Key Wrapping algorithm for any key size.
Instance Attribute Summary collapse
-
#iv ⇒ Object
Returns the value of attribute iv.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
- #a_ri(b) ⇒ Object
- #cipher ⇒ Object
- #decrypt(encrypted_cek) ⇒ Object
- #decrypt_round(data) ⇒ Object
- #encrypt(cek) ⇒ Object
- #encrypt_round(data) ⇒ Object
- #initialize(key = nil, iv = "\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6") ⇒ Object
- #kw_decrypt_round(j, a, r) ⇒ Object
- #kw_encrypt_round(j, a, r) ⇒ Object
- #xor(data, t) ⇒ Object
Instance Attribute Details
#iv ⇒ Object
Returns the value of attribute iv.
10 11 12 |
# File 'lib/jwe/alg/aes_kw.rb', line 10 def iv @iv end |
#key ⇒ Object
Returns the value of attribute key.
9 10 11 |
# File 'lib/jwe/alg/aes_kw.rb', line 9 def key @key end |
Instance Method Details
#a_ri(b) ⇒ Object
67 68 69 |
# File 'lib/jwe/alg/aes_kw.rb', line 67 def a_ri(b) [b.first(8).join, b.last(8).join] end |
#cipher ⇒ Object
71 72 73 |
# File 'lib/jwe/alg/aes_kw.rb', line 71 def cipher @cipher ||= Enc::Cipher.for(cipher_name) end |
#decrypt(encrypted_cek) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/jwe/alg/aes_kw.rb', line 40 def decrypt(encrypted_cek) c = encrypted_cek.b.scan(/.{8}/m) a, *r = c 5.downto(0) do |j| a, r = kw_decrypt_round(j, a, r) end if a != iv raise StandardError.new('The encrypted key has been tampered. Do not use this key.') end r.join end |
#decrypt_round(data) ⇒ Object
82 83 84 85 86 87 |
# File 'lib/jwe/alg/aes_kw.rb', line 82 def decrypt_round(data) cipher.decrypt cipher.key = key cipher.padding = 0 cipher.update(data) + cipher.final end |
#encrypt(cek) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jwe/alg/aes_kw.rb', line 17 def encrypt(cek) a = iv r = cek.b.scan(/.{8}/m) 6.times do |j| a, r = kw_encrypt_round(j, a, r) end ([a] + r).join end |
#encrypt_round(data) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/jwe/alg/aes_kw.rb', line 75 def encrypt_round(data) cipher.encrypt cipher.key = key cipher.padding = 0 cipher.update(data) + cipher.final end |
#initialize(key = nil, iv = "\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6") ⇒ Object
12 13 14 15 |
# File 'lib/jwe/alg/aes_kw.rb', line 12 def initialize(key = nil, iv = "\xA6\xA6\xA6\xA6\xA6\xA6\xA6\xA6") self.iv = iv.b self.key = key.b end |
#kw_decrypt_round(j, a, r) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jwe/alg/aes_kw.rb', line 55 def kw_decrypt_round(j, a, r) r.length.downto(1) do |i| a = xor(a, (r.length * j) + i) b = decrypt_round(a + r[i - 1]).chars a, r[i - 1] = a_ri(b) end [a, r] end |
#kw_encrypt_round(j, a, r) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/jwe/alg/aes_kw.rb', line 28 def kw_encrypt_round(j, a, r) r.length.times do |i| b = encrypt_round(a + r[i]).chars a, r[i] = a_ri(b) a = xor(a, (r.length * j) + i + 1) end [a, r] end |
#xor(data, t) ⇒ Object
89 90 91 92 93 94 |
# File 'lib/jwe/alg/aes_kw.rb', line 89 def xor(data, t) t = ([0] * (data.length - 1)) + [t] data = data.chars.map(&:ord) data.zip(t).map { |a, b| (a ^ b).chr }.join end |