Class: Zip::AESEncrypter

Inherits:
Encrypter show all
Includes:
AESEncryption
Defined in:
lib/zip/crypto/aes_encryption.rb

Overview

:nodoc:

Constant Summary

Constants included from AESEncryption

Zip::AESEncryption::AUTHENTICATION_CODE_LENGTH, Zip::AESEncryption::BITS, Zip::AESEncryption::BLOCK_SIZE, Zip::AESEncryption::KEY_LENGTHS, Zip::AESEncryption::SALT_LENGTHS, Zip::AESEncryption::STRENGTHS, Zip::AESEncryption::STRENGTH_128_BIT, Zip::AESEncryption::STRENGTH_192_BIT, Zip::AESEncryption::STRENGTH_256_BIT, Zip::AESEncryption::VERIFIER_LENGTH, Zip::AESEncryption::VERSIONS, Zip::AESEncryption::VERSION_AE_1, Zip::AESEncryption::VERSION_AE_2

Instance Method Summary collapse

Methods included from AESEncryption

#gp_flags, #header_bytesize, #initialize

Instance Method Details

#crc(_computed_crc) ⇒ Object



123
124
125
# File 'lib/zip/crypto/aes_encryption.rb', line 123

def crc(_computed_crc)
  0
end

#data_descriptorObject



113
114
115
# File 'lib/zip/crypto/aes_encryption.rb', line 113

def data_descriptor(*)
  ''
end

#encrypt(data) ⇒ Object

Deflater/PassThruCompressor call this with whatever, arbitrarily sized (and not necessarily block-aligned) buffer they happen to have flushed, potentially many times per entry. Only whole 16-byte blocks are actually run through the cipher here; any trailing partial block is buffered in @pending until either more data completes it or trailer forces the final flush. This keeps the CTR counter aligned with the true byte offset in the plaintext stream regardless of how callers happen to chunk their writes.



106
107
108
109
110
111
# File 'lib/zip/crypto/aes_encryption.rb', line 106

def encrypt(data)
  @pending << data
  encrypted_data = encrypt_blocks
  @hmac.update(encrypted_data)
  encrypted_data
end

#header(_mtime) ⇒ Object



94
95
96
# File 'lib/zip/crypto/aes_encryption.rb', line 94

def header(_mtime)
  @salt + @pwd_verify
end

#prepare_entry(entry) ⇒ Object



139
140
141
# File 'lib/zip/crypto/aes_encryption.rb', line 139

def prepare_entry(entry)
  entry.prep_aes_extra(AESEncryption::VERSION_AE_2, @strength)
end

#reset!Object



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/zip/crypto/aes_encryption.rb', line 127

def reset!
  @salt = SecureRandom.random_bytes(@salt_length)
  enc_key, enc_hmac_key, @pwd_verify = derive_keys(@salt)

  @counter = 0
  @pending = +''.b
  @cipher = OpenSSL::Cipher::AES.new(@bits, :CTR)
  @cipher.encrypt
  @cipher.key = enc_key
  @hmac = OpenSSL::HMAC.new(enc_hmac_key, OpenSSL::Digest.new('SHA1'))
end

#trailerObject



117
118
119
120
121
# File 'lib/zip/crypto/aes_encryption.rb', line 117

def trailer
  encrypted_data = encrypt_blocks(final: true)
  @hmac.update(encrypted_data)
  encrypted_data + @hmac.digest[0...AUTHENTICATION_CODE_LENGTH]
end