-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds support AES decryption #579
Open
jplot
wants to merge
8
commits into
rubyzip:master
Choose a base branch
from
jplot:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f0ebc03
Adds support AES decryption
jplot ee5d046
Merge remote-tracking branch 'source/master'
jplot 2f35353
Fixes gp flag on aes encryption
jplot b8dfa6d
Refacts variable names on aes encryption
jplot b8c6c39
Fixes variable error
jplot 3b620d5
Adds check aes integrity
jplot 7ff7ce7
Fixes rubocop lint
jplot d89ade3
Adds tests on aes decryption
jplot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'openssl' | ||
|
||
module Zip | ||
module AESEncryption # :nodoc: | ||
VERIFIER_LENGTH = 2 | ||
BLOCK_SIZE = 16 | ||
AUTHENTICATION_CODE_LENGTH = 10 | ||
|
||
VERSION_AE_1 = 0x01 | ||
VERSION_AE_2 = 0x02 | ||
|
||
VERSIONS = [ | ||
VERSION_AE_1, | ||
VERSION_AE_2 | ||
].freeze | ||
|
||
STRENGTH_128_BIT = 0x01 | ||
STRENGTH_192_BIT = 0x02 | ||
STRENGTH_256_BIT = 0x03 | ||
|
||
STRENGTHS = [ | ||
STRENGTH_128_BIT, | ||
STRENGTH_192_BIT, | ||
STRENGTH_256_BIT | ||
].freeze | ||
|
||
BITS = { | ||
STRENGTH_128_BIT => 128, | ||
STRENGTH_192_BIT => 192, | ||
STRENGTH_256_BIT => 256 | ||
}.freeze | ||
|
||
KEY_LENGTHS = { | ||
STRENGTH_128_BIT => 16, | ||
STRENGTH_192_BIT => 24, | ||
STRENGTH_256_BIT => 32 | ||
}.freeze | ||
|
||
SALT_LENGTHS = { | ||
STRENGTH_128_BIT => 8, | ||
STRENGTH_192_BIT => 12, | ||
STRENGTH_256_BIT => 16 | ||
}.freeze | ||
|
||
def initialize(password, strength) | ||
@password = password | ||
@strength = strength | ||
@bits = BITS[@strength] | ||
@key_length = KEY_LENGTHS[@strength] | ||
@salt_length = SALT_LENGTHS[@strength] | ||
end | ||
|
||
def header_bytesize | ||
@salt_length + VERIFIER_LENGTH | ||
end | ||
|
||
def gp_flags | ||
0x0001 | ||
end | ||
end | ||
|
||
class AESDecrypter < Decrypter # :nodoc: | ||
include AESEncryption | ||
|
||
def decrypt(encrypted_data) | ||
@hmac.update(encrypted_data) | ||
|
||
idx = 0 | ||
decrypted_data = +'' | ||
amount_to_read = encrypted_data.size | ||
|
||
while amount_to_read.positive? | ||
@cipher.iv = [@counter + 1].pack('Vx12') | ||
begin_index = BLOCK_SIZE * idx | ||
end_index = begin_index + [BLOCK_SIZE, amount_to_read].min | ||
decrypted_data << @cipher.update(encrypted_data[begin_index...end_index]) | ||
amount_to_read -= BLOCK_SIZE | ||
@counter += 1 | ||
idx += 1 | ||
end | ||
|
||
decrypted_data | ||
end | ||
|
||
def reset!(header) | ||
raise Error, "Unsupported encryption AES-#{@bits}" unless STRENGTHS.include? @strength | ||
|
||
salt = header[0...@salt_length] | ||
pwd_verify = header[-VERIFIER_LENGTH..] | ||
key_material = OpenSSL::KDF.pbkdf2_hmac( | ||
@password, | ||
salt: salt, | ||
iterations: 1000, | ||
length: (2 * @key_length) + VERIFIER_LENGTH, | ||
hash: 'sha1' | ||
) | ||
enc_key = key_material[0...@key_length] | ||
enc_hmac_key = key_material[@key_length...(2 * @key_length)] | ||
enc_pwd_verify = key_material[-VERIFIER_LENGTH..] | ||
|
||
raise Error, 'Bad password' if enc_pwd_verify != pwd_verify | ||
|
||
@counter = 0 | ||
@cipher = OpenSSL::Cipher::AES.new(@bits, :CTR) | ||
@cipher.decrypt | ||
@cipher.key = enc_key | ||
@hmac = OpenSSL::HMAC.new(enc_hmac_key, OpenSSL::Digest::SHA1.new) | ||
end | ||
|
||
def check_integrity(auth_code) | ||
raise Error, 'Integrity fault' if @hmac.digest[0...AUTHENTICATION_CODE_LENGTH] != auth_code | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# frozen_string_literal: true | ||
|
||
module Zip | ||
# Info-ZIP Extra for AES encryption | ||
class ExtraField::AES < ExtraField::Generic # :nodoc: | ||
attr_reader :vendor_version, :vendor_id, :encryption_strength, :compression_method | ||
|
||
HEADER_ID = [0x9901].pack('v') | ||
register_map | ||
|
||
def initialize(binstr = nil) | ||
@vendor_version = nil | ||
@vendor_id = nil | ||
@encryption_strength = nil | ||
@compression_method = nil | ||
binstr && merge(binstr) | ||
end | ||
|
||
def ==(other) | ||
@vendor_version == other.vendor_version && | ||
@vendor_id == other.vendor_id && | ||
@encryption_strength == other.encryption_strength && | ||
@compression_method == other.compression_method | ||
end | ||
|
||
def merge(binstr) | ||
return if binstr.empty? | ||
|
||
size, content = initial_parse(binstr) | ||
# size: 0 for central directory. 4 for local header | ||
return if !size || size.zero? | ||
|
||
@vendor_version, @vendor_id, | ||
@encryption_strength, @compression_method = content.unpack('va2Cv') | ||
end | ||
|
||
def pack_for_local | ||
[@vendor_version, @vendor_id, | ||
@encryption_strength, @compression_method].pack('va2Cv') | ||
end | ||
|
||
def pack_for_c_dir | ||
pack_for_local | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class AESDecrypterTest < MiniTest::Test | ||
def setup | ||
@decrypter_256 = ::Zip::AESDecrypter.new('password', ::Zip::AESEncryption::STRENGTH_256_BIT) | ||
@decrypter_128 = ::Zip::AESDecrypter.new('password', ::Zip::AESEncryption::STRENGTH_128_BIT) | ||
end | ||
|
||
def test_header_bytesize | ||
assert_equal 18, @decrypter_256.header_bytesize | ||
end | ||
|
||
def test_gp_flags | ||
assert_equal 1, @decrypter_256.gp_flags | ||
end | ||
|
||
def test_decrypt_aes_256 | ||
@decrypter_256.reset!([125, 138, 163, 42, 19, 1, 155, 66, 203, 174, 183, 235, 197, 122, 232, 68, 252, 225].pack('C*')) | ||
assert_equal 'a', @decrypter_256.decrypt([161].map(&:chr).join) | ||
end | ||
|
||
def test_decrypt_aes_128 | ||
@decrypter_128.reset!([127, 254, 117, 113, 255, 209, 171, 131, 179, 106].pack('C*')) | ||
assert_equal [75, 4, 0], @decrypter_128.decrypt([34, 33, 106].map(&:chr).join).chars.map(&:ord) | ||
end | ||
|
||
def test_reset! | ||
@decrypter_256.reset!([125, 138, 163, 42, 19, 1, 155, 66, 203, 174, 183, 235, 197, 122, 232, 68, 252, 225].pack('C*')) | ||
assert_equal 'a', @decrypter_256.decrypt([161].map(&:chr).join) | ||
|
||
@decrypter_256.reset!([118, 221, 166, 27, 165, 141, 24, 122, 227, 197, 52, 135, 222, 67, 221, 92, 231, 117].pack('C*')) | ||
assert_equal 'b', @decrypter_256.decrypt([135].map(&:chr).join) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
a |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
b |
Binary file not shown.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are missing the check of the CRC, we should check that the decrypted content hasn't been tampered.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've tried to integrate the AES integrity check, but it doesn't work.
If you have an idea, I'd love to hear it.
jplot@21bdb68
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've finally found the problem, I've finished the implementation and I'm going to write the tests.