< Summary

Information
Class: System.IO.Compression.WinZipAesKeyMaterial
Assembly: System.IO.Compression
File(s): D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\WinZipAesKeyMaterial.cs
Line coverage
92%
Covered lines: 52
Uncovered lines: 4
Coverable lines: 56
Total lines: 99
Line coverage: 92.8%
Branch coverage
62%
Covered branches: 5
Total branches: 8
Branch coverage: 62.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)50%2281.81%
Create(...)66.66%6694.87%
GetSaltSize(...)100%11100%

File(s)

D:\runner\runtime\src\libraries\System.IO.Compression\src\System\IO\Compression\WinZipAesKeyMaterial.cs

#LineLine coverage
 1// Licensed to the .NET Foundation under one or more agreements.
 2// The .NET Foundation licenses this file to you under the MIT license.
 3
 4using System.Buffers;
 5using System.Diagnostics;
 6using System.Runtime.Versioning;
 7using System.Security.Cryptography;
 8using System.Text;
 9
 10namespace System.IO.Compression
 11{
 12    /// <summary>
 13    /// Represents the parsed components of WinZip AES key material.
 14    /// The key material layout is: [salt][encryption key][HMAC key][password verifier (2 bytes)].
 15    /// </summary>
 16    internal readonly struct WinZipAesKeyMaterial
 17    {
 19218        public byte[] Salt { get; }
 9619        public byte[] EncryptionKey { get; }
 9620        public byte[] HmacKey { get; }
 19221        public byte[] PasswordVerifier { get; }
 22        public int KeySizeBits { get; }
 14423        public int SaltSize { get; }
 24
 25        private WinZipAesKeyMaterial(byte[] salt, byte[] encryptionKey, byte[] hmacKey, byte[] passwordVerifier, int key
 14426        {
 14427            if (OperatingSystem.IsBrowser())
 028            {
 029                throw new PlatformNotSupportedException(SR.WinZipEncryptionNotSupportedOnPlatform);
 30            }
 14431            Salt = salt;
 14432            EncryptionKey = encryptionKey;
 14433            HmacKey = hmacKey;
 14434            PasswordVerifier = passwordVerifier;
 14435            KeySizeBits = keySizeBits;
 14436            SaltSize = GetSaltSize(keySizeBits);
 14437        }
 38
 39        /// <summary>
 40        /// Derives key material from a password and optional salt using PBKDF2-SHA1.
 41        /// </summary>
 42        internal static unsafe WinZipAesKeyMaterial Create(ReadOnlySpan<char> password, byte[]? salt, int keySizeBits)
 14443        {
 14444            if (OperatingSystem.IsBrowser())
 045            {
 046                throw new PlatformNotSupportedException(SR.WinZipEncryptionNotSupportedOnPlatform);
 47            }
 14448            int saltSize = GetSaltSize(keySizeBits);
 14449            int keySizeBytes = keySizeBits / 8;
 14450            int totalKeySize = checked(keySizeBytes + keySizeBytes + 2);
 51
 52            byte[] saltBytes;
 14453            if (salt is null)
 4854            {
 4855                saltBytes = new byte[saltSize];
 4856                RandomNumberGenerator.Fill(saltBytes);
 4857            }
 58            else
 9659            {
 9660                Debug.Assert(salt.Length == saltSize, $"Salt must be {saltSize} bytes for AES-{keySizeBits}.");
 9661                saltBytes = salt;
 9662            }
 63
 14464            int maxPasswordByteCount = Encoding.UTF8.GetMaxByteCount(password.Length);
 14465            byte[] rentedPasswordBytes = ArrayPool<byte>.Shared.Rent(maxPasswordByteCount);
 14466            Debug.Assert(totalKeySize <= 66, "totalKeySize should be at most 66 bytes (AES-256: 32 + 32 + 2)");
 14467            Span<byte> derivedKey = stackalloc byte[totalKeySize];
 68
 69            try
 14470            {
 14471                int actualByteCount = Encoding.UTF8.GetBytes(password, rentedPasswordBytes);
 14472                Span<byte> passwordSpan = rentedPasswordBytes.AsSpan(0, actualByteCount);
 73
 14474                Rfc2898DeriveBytes.Pbkdf2(
 14475                    passwordSpan,
 14476                    saltBytes,
 14477                    derivedKey,
 14478                    1000, // iteration count specified by the WinZip AE-1/AE-2 specification
 14479                    HashAlgorithmName.SHA1);
 80
 81                // Slice the derived key directly into its components instead of
 82                // round-tripping through a combined array and Parse.
 14483                byte[] encryptionKey = derivedKey.Slice(0, keySizeBytes).ToArray();
 14484                byte[] hmacKey = derivedKey.Slice(keySizeBytes, keySizeBytes).ToArray();
 14485                byte[] passwordVerifier = derivedKey.Slice(keySizeBytes + keySizeBytes, 2).ToArray();
 86
 14487                return new WinZipAesKeyMaterial(saltBytes, encryptionKey, hmacKey, passwordVerifier, keySizeBits);
 88            }
 89            finally
 14490            {
 14491                CryptographicOperations.ZeroMemory(rentedPasswordBytes);
 14492                CryptographicOperations.ZeroMemory(derivedKey);
 14493                ArrayPool<byte>.Shared.Return(rentedPasswordBytes);
 14494            }
 14495        }
 96
 33697        internal static int GetSaltSize(int keySizeBits) => keySizeBits / 16;
 98    }
 99}