ZipAESStream.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // ZipAESStream.cs
  3. //
  4. // Copyright 2009 David Pierson
  5. //
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. //
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  19. //
  20. // Linking this library statically or dynamically with other modules is
  21. // making a combined work based on this library. Thus, the terms and
  22. // conditions of the GNU General Public License cover the whole
  23. // combination.
  24. //
  25. // As a special exception, the copyright holders of this library give you
  26. // permission to link this library with independent modules to produce an
  27. // executable, regardless of the license terms of these independent
  28. // modules, and to copy and distribute the resulting executable under
  29. // terms of your choice, provided that you also meet, for each linked
  30. // independent module, the terms and conditions of the license of that
  31. // module. An independent module is a module which is not derived from
  32. // or based on this library. If you modify this library, you may extend
  33. // this exception to your version of the library, but you are not
  34. // obligated to do so. If you do not wish to do so, delete this
  35. // exception statement from your version.
  36. //
  37. #if !NET_1_1 && !NETCF_2_0
  38. using System;
  39. using System.IO;
  40. using System.Security.Cryptography;
  41. namespace CommonMPQ.SharpZipLib.Encryption {
  42. // Based on information from http://www.winzip.com/aes_info.htm
  43. // and http://www.gladman.me.uk/cryptography_technology/fileencrypt/
  44. /// <summary>
  45. /// Encrypts and decrypts AES ZIP
  46. /// </summary>
  47. internal class ZipAESStream : CryptoStream {
  48. /// <summary>
  49. /// Constructor
  50. /// </summary>
  51. /// <param name="stream">The stream on which to perform the cryptographic transformation.</param>
  52. /// <param name="transform">Instance of ZipAESTransform</param>
  53. /// <param name="mode">Read or Write</param>
  54. public ZipAESStream(Stream stream, ZipAESTransform transform, CryptoStreamMode mode)
  55. : base(stream, transform, mode) {
  56. _stream = stream;
  57. _transform = transform;
  58. _slideBuffer = new byte[1024];
  59. _blockAndAuth = CRYPTO_BLOCK_SIZE + AUTH_CODE_LENGTH;
  60. // mode:
  61. // CryptoStreamMode.Read means we read from "stream" and pass decrypted to our Read() method.
  62. // Write bypasses this stream and uses the Transform directly.
  63. if (mode != CryptoStreamMode.Read) {
  64. throw new Exception("ZipAESStream only for read");
  65. }
  66. }
  67. // The final n bytes of the AES stream contain the Auth Code.
  68. private const int AUTH_CODE_LENGTH = 10;
  69. private Stream _stream;
  70. private ZipAESTransform _transform;
  71. private byte[] _slideBuffer;
  72. private int _slideBufStartPos;
  73. private int _slideBufFreePos;
  74. // Blocksize is always 16 here, even for AES-256 which has transform.InputBlockSize of 32.
  75. private const int CRYPTO_BLOCK_SIZE = 16;
  76. private int _blockAndAuth;
  77. /// <summary>
  78. /// Reads a sequence of bytes from the current CryptoStream into buffer,
  79. /// and advances the position within the stream by the number of bytes read.
  80. /// </summary>
  81. public override int Read(byte[] outBuffer, int offset, int count) {
  82. int nBytes = 0;
  83. while (nBytes < count) {
  84. // Calculate buffer quantities vs read-ahead size, and check for sufficient free space
  85. int byteCount = _slideBufFreePos - _slideBufStartPos;
  86. // Need to handle final block and Auth Code specially, but don't know total data length.
  87. // Maintain a read-ahead equal to the length of (crypto block + Auth Code).
  88. // When that runs out we can detect these final sections.
  89. int lengthToRead = _blockAndAuth - byteCount;
  90. if (_slideBuffer.Length - _slideBufFreePos < lengthToRead) {
  91. // Shift the data to the beginning of the buffer
  92. int iTo = 0;
  93. for (int iFrom = _slideBufStartPos; iFrom < _slideBufFreePos; iFrom++, iTo++) {
  94. _slideBuffer[iTo] = _slideBuffer[iFrom];
  95. }
  96. _slideBufFreePos -= _slideBufStartPos; // Note the -=
  97. _slideBufStartPos = 0;
  98. }
  99. int obtained = _stream.Read(_slideBuffer, _slideBufFreePos, lengthToRead);
  100. _slideBufFreePos += obtained;
  101. // Recalculate how much data we now have
  102. byteCount = _slideBufFreePos - _slideBufStartPos;
  103. if (byteCount >= _blockAndAuth) {
  104. // At least a 16 byte block and an auth code remains.
  105. _transform.TransformBlock(_slideBuffer,
  106. _slideBufStartPos,
  107. CRYPTO_BLOCK_SIZE,
  108. outBuffer,
  109. offset);
  110. nBytes += CRYPTO_BLOCK_SIZE;
  111. offset += CRYPTO_BLOCK_SIZE;
  112. _slideBufStartPos += CRYPTO_BLOCK_SIZE;
  113. } else {
  114. // Last round.
  115. if (byteCount > AUTH_CODE_LENGTH) {
  116. // At least one byte of data plus auth code
  117. int finalBlock = byteCount - AUTH_CODE_LENGTH;
  118. _transform.TransformBlock(_slideBuffer,
  119. _slideBufStartPos,
  120. finalBlock,
  121. outBuffer,
  122. offset);
  123. nBytes += finalBlock;
  124. _slideBufStartPos += finalBlock;
  125. }
  126. else if (byteCount < AUTH_CODE_LENGTH)
  127. throw new Exception("Internal error missed auth code"); // Coding bug
  128. // Final block done. Check Auth code.
  129. byte[] calcAuthCode = _transform.GetAuthCode();
  130. for (int i = 0; i < AUTH_CODE_LENGTH; i++) {
  131. if (calcAuthCode[i] != _slideBuffer[_slideBufStartPos + i]) {
  132. throw new Exception("AES Authentication Code does not match. This is a super-CRC check on the data in the file after compression and encryption. \r\n"
  133. + "The file may be damaged.");
  134. }
  135. }
  136. break; // Reached the auth code
  137. }
  138. }
  139. return nBytes;
  140. }
  141. /// <summary>
  142. /// Writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.
  143. /// </summary>
  144. /// <param name="buffer">An array of bytes. This method copies count bytes from buffer to the current stream. </param>
  145. /// <param name="offset">The byte offset in buffer at which to begin copying bytes to the current stream. </param>
  146. /// <param name="count">The number of bytes to be written to the current stream. </param>
  147. public override void Write(byte[] buffer, int offset, int count) {
  148. // ZipAESStream is used for reading but not for writing. Writing uses the ZipAESTransform directly.
  149. throw new NotImplementedException();
  150. }
  151. }
  152. }
  153. #endif