1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
|
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace System.IdentityModel
{
using System.Diagnostics;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
class RijndaelCryptoServiceProvider : Rijndael
{
public RijndaelCryptoServiceProvider()
{
}
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[] rgbIV)
{
if (rgbKey == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rgbKey");
if (rgbIV == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rgbIV");
if (this.ModeValue != CipherMode.CBC)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESCipherModeNotSupported, this.ModeValue)));
return new RijndaelCryptoTransform(rgbKey, rgbIV, this.PaddingValue, this.BlockSizeValue, true);
}
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[] rgbIV)
{
if (rgbKey == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rgbKey");
if (rgbIV == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("rgbIV");
if (this.ModeValue != CipherMode.CBC)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESCipherModeNotSupported, this.ModeValue)));
return new RijndaelCryptoTransform(rgbKey, rgbIV, this.PaddingValue, this.BlockSizeValue, false);
}
public override void GenerateKey()
{
this.KeyValue = new byte[this.KeySizeValue / 8];
CryptoHelper.RandomNumberGenerator.GetBytes(this.KeyValue);
}
public override void GenerateIV()
{
// IV is always 16 bytes/128 bits because block size is always 128 bits
this.IVValue = new byte[this.BlockSizeValue / 8];
CryptoHelper.RandomNumberGenerator.GetBytes(this.IVValue);
}
class RijndaelCryptoTransform : ICryptoTransform
{
SafeProvHandle provHandle = SafeProvHandle.InvalidHandle;
SafeKeyHandle keyHandle = SafeKeyHandle.InvalidHandle;
PaddingMode paddingMode;
byte[] depadBuffer = null;
int blockSize;
bool encrypt;
public unsafe RijndaelCryptoTransform(byte[] rgbKey, byte[] rgbIV, PaddingMode paddingMode, int blockSizeBits, bool encrypt)
{
if (rgbKey.Length != 16 && rgbKey.Length != 24 && rgbKey.Length != 32)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESKeyLengthNotSupported, rgbKey.Length * 8)));
if (rgbIV.Length != 16)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESIVLengthNotSupported, rgbIV.Length * 8)));
if (paddingMode != PaddingMode.PKCS7 && paddingMode != PaddingMode.ISO10126)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.GetString(SR.AESPaddingModeNotSupported, paddingMode)));
this.paddingMode = paddingMode;
DiagnosticUtility.DebugAssert((blockSizeBits % 8) == 0, "Bits must be byte aligned.");
this.blockSize = blockSizeBits / 8;
this.encrypt = encrypt;
SafeProvHandle provHandle = null;
SafeKeyHandle keyHandle = null;
try
{
#pragma warning suppress 56523
ThrowIfFalse(SR.AESCryptAcquireContextFailed, NativeMethods.CryptAcquireContextW(out provHandle, null, null, NativeMethods.PROV_RSA_AES, NativeMethods.CRYPT_VERIFYCONTEXT));
// (BLOBHEADER + keyLen) + Key
int cbData = PLAINTEXTKEYBLOBHEADER.SizeOf + rgbKey.Length;
byte[] pbData = new byte[cbData];
Buffer.BlockCopy(rgbKey, 0, pbData, PLAINTEXTKEYBLOBHEADER.SizeOf, rgbKey.Length);
fixed (void* pbDataPtr = &pbData[0])
{
PLAINTEXTKEYBLOBHEADER* pbhdr = (PLAINTEXTKEYBLOBHEADER*)pbDataPtr;
pbhdr->bType = NativeMethods.PLAINTEXTKEYBLOB;
pbhdr->bVersion = NativeMethods.CUR_BLOB_VERSION;
pbhdr->reserved = 0;
if (rgbKey.Length == 16)
pbhdr->aiKeyAlg = NativeMethods.CALG_AES_128;
else if (rgbKey.Length == 24)
pbhdr->aiKeyAlg = NativeMethods.CALG_AES_192;
else
pbhdr->aiKeyAlg = NativeMethods.CALG_AES_256;
pbhdr->keyLength = rgbKey.Length;
keyHandle = SafeKeyHandle.SafeCryptImportKey(provHandle, pbDataPtr, cbData);
}
#if DEBUG
uint ivLen = 0;
#pragma warning suppress 56523 // win32 error checked in ThrowIfFalse() method
ThrowIfFalse(SR.AESCryptGetKeyParamFailed, NativeMethods.CryptGetKeyParam(keyHandle, NativeMethods.KP_IV, IntPtr.Zero, ref ivLen, 0));
DiagnosticUtility.DebugAssert(rgbIV.Length == ivLen, "Mismatch iv size");
#endif
fixed (void* pbIVPtr = &rgbIV[0])
{
#pragma warning suppress 56523
ThrowIfFalse(SR.AESCryptSetKeyParamFailed, NativeMethods.CryptSetKeyParam(keyHandle, NativeMethods.KP_IV, pbIVPtr, 0));
}
// Save
this.keyHandle = keyHandle;
this.provHandle = provHandle;
keyHandle = null;
provHandle = null;
}
finally
{
if (keyHandle != null)
keyHandle.Close();
if (provHandle != null)
provHandle.Close();
}
}
public bool CanReuseTransform
{
get { return true; }
}
public bool CanTransformMultipleBlocks
{
get { return true; }
}
public int InputBlockSize
{
get { return this.blockSize; }
}
public int OutputBlockSize
{
get { return this.blockSize; }
}
public void Dispose()
{
try
{
this.keyHandle.Close();
}
finally
{
this.provHandle.Close();
}
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
{
if (inputBuffer == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("inputBuffer");
if (outputBuffer == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("outputBuffer");
if (inputOffset < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("inputOffset", SR.GetString(SR.ValueMustBeNonNegative)));
if (inputCount <= 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("inputCount", SR.GetString(SR.ValueMustBeGreaterThanZero)));
if (outputOffset < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("outputOffset", SR.GetString(SR.ValueMustBeNonNegative)));
if ((inputCount % this.blockSize) != 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.GetString(SR.AESInvalidInputBlockSize, inputCount, this.blockSize)));
if ((inputBuffer.Length - inputCount) < inputOffset)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("inputOffset", SR.GetString(SR.ValueMustBeInRange, 0, inputBuffer.Length - inputCount - 1)));
if (outputBuffer.Length < outputOffset)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("outputOffset", SR.GetString(SR.ValueMustBeInRange, 0, outputBuffer.Length - 1)));
if (this.encrypt)
{
return EncryptData(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset, false);
}
else
{
if (this.paddingMode == PaddingMode.PKCS7)
{
return DecryptData(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset, false);
}
else
{
// OK, now we're in the special case. Check to see if this is the *first* block we've seen
// If so, buffer it and return null zero bytes
if (this.depadBuffer == null)
{
this.depadBuffer = new byte[this.blockSize];
// copy the last InputBlockSize bytes to m_depadBuffer everything else gets processed and returned
int inputToProcess = inputCount - this.blockSize;
Buffer.BlockCopy(inputBuffer, inputOffset + inputToProcess, this.depadBuffer, 0, this.blockSize);
return ((inputToProcess <= 0) ? 0 : DecryptData(inputBuffer, inputOffset, inputToProcess, outputBuffer, outputOffset, false));
}
else
{
// we already have a depad buffer, so we need to decrypt that info first & copy it out
int dwCount = DecryptData(this.depadBuffer, 0, this.depadBuffer.Length, outputBuffer, outputOffset, false);
outputOffset += dwCount;
int inputToProcess = inputCount - this.blockSize;
Buffer.BlockCopy(inputBuffer, inputOffset + inputToProcess, this.depadBuffer, 0, this.blockSize);
return dwCount + ((inputToProcess <= 0) ? 0 : DecryptData(inputBuffer, inputOffset, inputToProcess, outputBuffer, outputOffset, false));
}
}
}
}
public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
{
if (inputBuffer == null)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("inputBuffer");
if (inputOffset < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("inputOffset", SR.GetString(SR.ValueMustBeNonNegative)));
if (inputCount < 0)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("inputCount", SR.GetString(SR.ValueMustBeNonNegative)));
if ((inputBuffer.Length - inputCount) < inputOffset)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("inputOffset", SR.GetString(SR.ValueMustBeInRange, 0, inputBuffer.Length - inputCount - 1)));
if (this.encrypt)
{
int padding = this.blockSize - (inputCount % this.blockSize);
int outputCount = inputCount + padding;
if (this.paddingMode == PaddingMode.ISO10126)
outputCount += this.blockSize;
byte[] outputBuffer = new byte[outputCount];
int dwCount = EncryptData(inputBuffer, inputOffset, inputCount, outputBuffer, 0, true);
return TruncateBuffer(outputBuffer, dwCount);
}
else
{
if (this.paddingMode == PaddingMode.PKCS7)
{
byte[] outputBuffer = new byte[inputCount];
int dwCount = DecryptData(inputBuffer, inputOffset, inputCount, outputBuffer, 0, true);
return TruncateBuffer(outputBuffer, dwCount);
}
else
{
// OK, now we're in the special case. Check to see if this is the *first* block we've seen
// If so, buffer it and return null zero bytes
if (this.depadBuffer == null)
{
byte[] outputBuffer = new byte[inputCount];
int dwCount = DecryptData(inputBuffer, inputOffset, inputCount, outputBuffer, 0, true);
return TruncateBuffer(outputBuffer, dwCount);
}
else
{
byte[] outputBuffer = new byte[this.depadBuffer.Length + inputCount];
// we already have a depad buffer, so we need to decrypt that info first & copy it out
int dwCount = DecryptData(this.depadBuffer, 0, this.depadBuffer.Length, outputBuffer, 0, false);
dwCount += DecryptData(inputBuffer, inputOffset, inputCount, outputBuffer, dwCount, true);
return TruncateBuffer(outputBuffer, dwCount);
}
}
}
}
unsafe int EncryptData(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset, bool final)
{
if ((outputBuffer.Length - outputOffset) < inputCount)
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("outputBuffer", SR.GetString(SR.AESInsufficientOutputBuffer, outputBuffer.Length - outputOffset, inputCount)));
bool doPadding = final && (this.paddingMode == PaddingMode.ISO10126);
byte[] tempBuffer = outputBuffer;
int tempOffset = outputOffset;
int dwCount = inputCount;
bool throwing = true;
Buffer.BlockCopy(inputBuffer, inputOffset, tempBuffer, tempOffset, inputCount);
try
{
if (doPadding)
DoPadding(ref tempBuffer, ref tempOffset, ref dwCount);
fixed (void* tempBufferPtr = &tempBuffer[tempOffset])
{
#pragma warning suppress 56523
ThrowIfFalse(SR.AESCryptEncryptFailed, NativeMethods.CryptEncrypt(keyHandle, IntPtr.Zero, final, 0, tempBufferPtr, ref dwCount, tempBuffer.Length - tempOffset));
}
throwing = false;
}
finally
{
if (throwing)
Array.Clear(tempBuffer, tempOffset, inputCount);
}
// Chop off native padding.
if (doPadding)
dwCount -= this.blockSize;
if (tempBuffer != outputBuffer)
Buffer.BlockCopy(tempBuffer, tempOffset, outputBuffer, outputOffset, dwCount);
return dwCount;
}
unsafe int DecryptData(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset, bool final)
{
bool bFinal = final && (this.paddingMode == PaddingMode.PKCS7);
int dwCount = inputCount;
if (dwCount > 0)
{
Buffer.BlockCopy(inputBuffer, inputOffset, outputBuffer, outputOffset, inputCount);
fixed (void* outputBufferPtr = &outputBuffer[outputOffset])
{
#pragma warning suppress 56523
ThrowIfFalse(SR.AESCryptDecryptFailed, NativeMethods.CryptDecrypt(keyHandle, IntPtr.Zero, bFinal, 0, outputBufferPtr, ref dwCount));
}
}
if (!bFinal && final)
{
byte padSize = outputBuffer[outputOffset + dwCount - 1];
DiagnosticUtility.DebugAssert(padSize <= this.blockSize, "Invalid padding size.");
dwCount -= padSize;
}
return dwCount;
}
// Since the CSP only provides PKCS7 padding. For other padding, we do it manually.
void DoPadding(ref byte[] tempBuffer, ref int tempOffset, ref int dwCount)
{
int lonelyBytes = dwCount % this.blockSize;
int padSize = this.blockSize - lonelyBytes;
// Random with last byte indicating padSize
byte[] padBytes = new byte[padSize];
CryptoHelper.RandomNumberGenerator.GetBytes(padBytes);
padBytes[padSize - 1] = (byte)padSize;
// inline if can hold manual padding and native padding (1 block)
int requiredSize = dwCount + padSize + this.blockSize;
if (tempBuffer.Length >= (tempOffset + requiredSize))
{
Buffer.BlockCopy(padBytes, 0, tempBuffer, tempOffset + dwCount, padSize);
}
else
{
byte[] ret = new byte[requiredSize];
Buffer.BlockCopy(tempBuffer, tempOffset, ret, 0, dwCount);
Buffer.BlockCopy(padBytes, 0, ret, dwCount, padSize);
Array.Clear(tempBuffer, tempOffset, dwCount);
tempBuffer = ret;
tempOffset = 0;
}
dwCount += padSize;
}
byte[] TruncateBuffer(byte[] buffer, int len)
{
if (len == buffer.Length)
return buffer;
// Truncate
byte[] tempBuffer = new byte[len];
Buffer.BlockCopy(buffer, 0, tempBuffer, 0, len);
if (!this.encrypt)
Array.Clear(buffer, 0, buffer.Length);
return tempBuffer;
}
static void ThrowIfFalse(string sr, bool ret)
{
if (!ret)
{
int err = Marshal.GetLastWin32Error();
string reason = (err != 0) ? new Win32Exception(err).Message : String.Empty;
throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new CryptographicException(SR.GetString(sr, reason)));
}
}
}
}
}
|