|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'openssl' |
| 4 | + |
| 5 | +module Zip |
| 6 | + module AESEncryption # :nodoc: |
| 7 | + VERIFIER_LENGTH = 2 |
| 8 | + BLOCK_SIZE = 16 |
| 9 | + AUTHENTICATION_CODE_LENGTH = 10 |
| 10 | + |
| 11 | + VERSION_AE_1 = 0x01 |
| 12 | + VERSION_AE_2 = 0x02 |
| 13 | + |
| 14 | + VERSIONS = [ |
| 15 | + VERSION_AE_1, |
| 16 | + VERSION_AE_2 |
| 17 | + ].freeze |
| 18 | + |
| 19 | + STRENGTH_128_BIT = 0x01 |
| 20 | + STRENGTH_192_BIT = 0x02 |
| 21 | + STRENGTH_256_BIT = 0x03 |
| 22 | + |
| 23 | + STRENGTHS = [ |
| 24 | + STRENGTH_128_BIT, |
| 25 | + STRENGTH_192_BIT, |
| 26 | + STRENGTH_256_BIT |
| 27 | + ].freeze |
| 28 | + |
| 29 | + BITS = { |
| 30 | + STRENGTH_128_BIT => 128, |
| 31 | + STRENGTH_192_BIT => 192, |
| 32 | + STRENGTH_256_BIT => 256 |
| 33 | + }.freeze |
| 34 | + |
| 35 | + KEY_LENGTHS = { |
| 36 | + STRENGTH_128_BIT => 16, |
| 37 | + STRENGTH_192_BIT => 24, |
| 38 | + STRENGTH_256_BIT => 32 |
| 39 | + }.freeze |
| 40 | + |
| 41 | + SALT_LENGTHS = { |
| 42 | + STRENGTH_128_BIT => 8, |
| 43 | + STRENGTH_192_BIT => 12, |
| 44 | + STRENGTH_256_BIT => 16 |
| 45 | + }.freeze |
| 46 | + |
| 47 | + def initialize(password, strength) |
| 48 | + @password = password |
| 49 | + @strength = strength |
| 50 | + @bits = BITS[@strength] |
| 51 | + @key_length = KEY_LENGTHS[@strength] |
| 52 | + @salt_length = SALT_LENGTHS[@strength] |
| 53 | + end |
| 54 | + |
| 55 | + def header_bytesize |
| 56 | + @salt_length + VERIFIER_LENGTH |
| 57 | + end |
| 58 | + |
| 59 | + def gp_flags |
| 60 | + 0x0001 |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + class AESDecrypter < Decrypter # :nodoc: |
| 65 | + include AESEncryption |
| 66 | + |
| 67 | + def decrypt(encrypted_data) |
| 68 | + @hmac.update(encrypted_data) |
| 69 | + |
| 70 | + idx = 0 |
| 71 | + decrypted_data = +'' |
| 72 | + amount_to_read = encrypted_data.size |
| 73 | + |
| 74 | + while amount_to_read.positive? |
| 75 | + @cipher.iv = [@counter + 1].pack('Vx12') |
| 76 | + begin_index = BLOCK_SIZE * idx |
| 77 | + end_index = begin_index + [BLOCK_SIZE, amount_to_read].min |
| 78 | + decrypted_data << @cipher.update(encrypted_data[begin_index...end_index]) |
| 79 | + amount_to_read -= BLOCK_SIZE |
| 80 | + @counter += 1 |
| 81 | + idx += 1 |
| 82 | + end |
| 83 | + |
| 84 | + decrypted_data |
| 85 | + end |
| 86 | + |
| 87 | + def reset!(header) |
| 88 | + raise Error, "Unsupported encryption AES-#{@bits}" unless STRENGTHS.include? @strength |
| 89 | + |
| 90 | + salt = header[0...@salt_length] |
| 91 | + pwd_verify = header[-VERIFIER_LENGTH..] |
| 92 | + key_material = OpenSSL::KDF.pbkdf2_hmac( |
| 93 | + @password, |
| 94 | + salt: salt, |
| 95 | + iterations: 1000, |
| 96 | + length: (2 * @key_length) + VERIFIER_LENGTH, |
| 97 | + hash: 'sha1' |
| 98 | + ) |
| 99 | + enc_key = key_material[0...@key_length] |
| 100 | + enc_hmac_key = key_material[@key_length...(2 * @key_length)] |
| 101 | + enc_pwd_verify = key_material[-VERIFIER_LENGTH..] |
| 102 | + |
| 103 | + raise Error, 'Bad password' if enc_pwd_verify != pwd_verify |
| 104 | + |
| 105 | + @counter = 0 |
| 106 | + @cipher = OpenSSL::Cipher::AES.new(@bits, :CTR) |
| 107 | + @cipher.decrypt |
| 108 | + @cipher.key = enc_key |
| 109 | + @hmac = OpenSSL::HMAC.new(enc_hmac_key, OpenSSL::Digest.new('SHA1')) |
| 110 | + end |
| 111 | + |
| 112 | + def check_integrity(auth_code) |
| 113 | + raise Error, 'Integrity fault' if @hmac.digest[0...AUTHENTICATION_CODE_LENGTH] != auth_code |
| 114 | + end |
| 115 | + end |
| 116 | +end |
0 commit comments