Fix XMM scanning on Mac x86.
[mono.git] / mcs / class / System.IO.Compression / SharpCompress / Common / Zip / WinzipAesEncryptionData.cs
1 using System;
2
3 namespace SharpCompress.Common.Zip
4 {
5     internal class WinzipAesEncryptionData
6     {
7         private const int RFC2898_ITERATIONS = 1000;
8
9         private byte[] salt;
10         private WinzipAesKeySize keySize;
11         private byte[] passwordVerifyValue;
12         private string password;
13
14         private byte[] generatedVerifyValue;
15
16         internal WinzipAesEncryptionData(WinzipAesKeySize keySize, byte[] salt, byte[] passwordVerifyValue,
17                                          string password)
18         {
19             this.keySize = keySize;
20             this.salt = salt;
21             this.passwordVerifyValue = passwordVerifyValue;
22             this.password = password;
23             Initialize();
24         }
25
26         internal byte[] IvBytes { get; set; }
27         internal byte[] KeyBytes { get; set; }
28
29         private int KeySizeInBytes
30         {
31             get { return KeyLengthInBytes(keySize); }
32         }
33
34         internal static int KeyLengthInBytes(WinzipAesKeySize keySize)
35         {
36             switch (keySize)
37             {
38                 case WinzipAesKeySize.KeySize128:
39                     return 16;
40                 case WinzipAesKeySize.KeySize192:
41                     return 24;
42                 case WinzipAesKeySize.KeySize256:
43                     return 32;
44             }
45             throw new InvalidOperationException();
46         }
47
48         private void Initialize()
49         {
50             System.Security.Cryptography.Rfc2898DeriveBytes rfc2898 =
51                 new System.Security.Cryptography.Rfc2898DeriveBytes(password, salt, RFC2898_ITERATIONS);
52
53             KeyBytes = rfc2898.GetBytes(KeySizeInBytes); // 16 or 24 or 32 ???
54             IvBytes = rfc2898.GetBytes(KeySizeInBytes);
55             generatedVerifyValue = rfc2898.GetBytes(2);
56
57             short verify = BitConverter.ToInt16(passwordVerifyValue, 0);
58             if (password != null)
59             {
60                 short generated = BitConverter.ToInt16(generatedVerifyValue, 0);
61                 if (verify != generated)
62                     throw new InvalidFormatException("bad password");
63             }
64         }
65     }
66 }