| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 3 | | // license information. |
| | 4 | |
|
| | 5 | | using System; |
| | 6 | | using System.Globalization; |
| | 7 | | using System.Security.Cryptography; |
| | 8 | |
|
| | 9 | | namespace Microsoft.Azure.KeyVault.Cryptography.Algorithms |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// Abstract base class for AESCBC-HMAC algorithms. |
| | 13 | | /// </summary> |
| | 14 | | public abstract class AesCbcHmacSha2 : SymmetricEncryptionAlgorithm |
| | 15 | | { |
| | 16 | | internal static Aes Create( byte[] key, byte[] iv ) |
| | 17 | | { |
| 12 | 18 | | var aes = Aes.Create(); |
| | 19 | |
|
| 12 | 20 | | aes.Mode = CipherMode.CBC; |
| 12 | 21 | | aes.Padding = PaddingMode.PKCS7; |
| 12 | 22 | | aes.KeySize = key.Length * 8; |
| 12 | 23 | | aes.Key = key; |
| 12 | 24 | | aes.IV = iv; |
| | 25 | |
|
| 12 | 26 | | return aes; |
| | 27 | | } |
| | 28 | | protected AesCbcHmacSha2( string name ) |
| 12 | 29 | | : base( name ) |
| | 30 | | { |
| 12 | 31 | | } |
| | 32 | |
|
| | 33 | | public override ICryptoTransform CreateDecryptor( byte[] key, byte[] iv, byte[] authenticationData, byte[] authe |
| | 34 | | { |
| 6 | 35 | | if ( key == null ) |
| 0 | 36 | | throw new CryptographicException( "No key material" ); |
| | 37 | |
|
| 6 | 38 | | if ( iv == null ) |
| 0 | 39 | | throw new CryptographicException( "No initialization vector" ); |
| | 40 | |
|
| 6 | 41 | | if ( authenticationData == null ) |
| 0 | 42 | | throw new CryptographicException( "No authentication data" ); |
| | 43 | |
|
| 6 | 44 | | if ( authenticationTag == null ) |
| 0 | 45 | | throw new CryptographicException( "No authentication tag" ); |
| | 46 | |
|
| | 47 | | // Create the Decryptor |
| 6 | 48 | | return new AesCbcHmacSha2Decryptor( Name, key, iv, authenticationData, authenticationTag ); |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public override ICryptoTransform CreateEncryptor( byte[] key, byte[] iv, byte[] authenticationData ) |
| | 52 | | { |
| 6 | 53 | | if ( key == null ) |
| 0 | 54 | | throw new CryptographicException( "No key material" ); |
| | 55 | |
|
| 6 | 56 | | if ( iv == null ) |
| 0 | 57 | | throw new CryptographicException( "No initialization vector" ); |
| | 58 | |
|
| 6 | 59 | | if ( authenticationData == null ) |
| 0 | 60 | | throw new CryptographicException( "No authentication data" ); |
| | 61 | |
|
| | 62 | | // Create the Encryptor |
| 6 | 63 | | return new AesCbcHmacSha2Encryptor( Name, key, iv, authenticationData ); |
| | 64 | | } |
| | 65 | |
|
| | 66 | | private static void GetAlgorithmParameters( string algorithm, byte[] key, out byte[] aes_key, out byte[] hmac_ke |
| | 67 | | { |
| | 68 | | switch ( algorithm ) |
| | 69 | | { |
| | 70 | | case Aes128CbcHmacSha256.AlgorithmName: |
| | 71 | | { |
| 4 | 72 | | if ( ( key.Length << 3 ) < 256 ) |
| 0 | 73 | | throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length |
| | 74 | |
|
| 4 | 75 | | hmac_key = new byte[128 >> 3]; |
| 4 | 76 | | aes_key = new byte[128 >> 3]; |
| 4 | 77 | | Array.Copy( key, hmac_key, 128 >> 3 ); |
| 4 | 78 | | Array.Copy( key, 128 >> 3, aes_key, 0, 128 >> 3 ); |
| | 79 | |
|
| 4 | 80 | | hmac = IncrementalHash.CreateHMAC( HashAlgorithmName.SHA256, hmac_key ); |
| | 81 | |
|
| 4 | 82 | | break; |
| | 83 | | } |
| | 84 | |
|
| | 85 | | case Aes192CbcHmacSha384.AlgorithmName: |
| | 86 | | { |
| 4 | 87 | | if ( ( key.Length << 3 ) < 384 ) |
| 0 | 88 | | throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length |
| | 89 | |
|
| 4 | 90 | | hmac_key = new byte[192 >> 3]; |
| 4 | 91 | | aes_key = new byte[192 >> 3]; |
| 4 | 92 | | Array.Copy( key, hmac_key, 192 >> 3 ); |
| 4 | 93 | | Array.Copy( key, 192 >> 3, aes_key, 0, 192 >> 3 ); |
| | 94 | |
|
| 4 | 95 | | hmac = IncrementalHash.CreateHMAC( HashAlgorithmName.SHA384, hmac_key ); |
| | 96 | |
|
| 4 | 97 | | break; |
| | 98 | | } |
| | 99 | |
|
| | 100 | | case Aes256CbcHmacSha512.AlgorithmName: |
| | 101 | | { |
| 4 | 102 | | if ( ( key.Length << 3 ) < 512 ) |
| 0 | 103 | | throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "{0} key length |
| | 104 | |
|
| 4 | 105 | | hmac_key = new byte[256 >> 3]; |
| 4 | 106 | | aes_key = new byte[256 >> 3]; |
| 4 | 107 | | Array.Copy( key, hmac_key, 256 >> 3 ); |
| 4 | 108 | | Array.Copy( key, 256 >> 3, aes_key, 0, 256 >> 3 ); |
| | 109 | |
|
| 4 | 110 | | hmac = IncrementalHash.CreateHMAC( HashAlgorithmName.SHA512, hmac_key ); |
| | 111 | |
|
| 4 | 112 | | break; |
| | 113 | | } |
| | 114 | |
|
| | 115 | | default: |
| | 116 | | { |
| 0 | 117 | | throw new CryptographicException( string.Format( CultureInfo.CurrentCulture, "Unsupported algori |
| | 118 | | } |
| | 119 | | } |
| | 120 | | } |
| | 121 | |
|
| | 122 | | class AesCbcHmacSha2Encryptor : IAuthenticatedCryptoTransform |
| | 123 | | { |
| | 124 | | readonly byte[] _hmac_key; |
| | 125 | |
|
| | 126 | | readonly byte[] _associated_data_length; |
| | 127 | |
|
| | 128 | | Aes _aes; |
| | 129 | | IncrementalHash _hmac; |
| | 130 | |
|
| | 131 | | ICryptoTransform _inner; |
| | 132 | | byte[] _tag; |
| | 133 | |
|
| 6 | 134 | | internal AesCbcHmacSha2Encryptor( string name, byte[] key, byte[] iv, byte[] associatedData ) |
| | 135 | | { |
| | 136 | | // Split the key to get the AES key, the HMAC key and the HMAC object |
| | 137 | | byte[] aesKey; |
| | 138 | |
|
| 6 | 139 | | GetAlgorithmParameters( name, key, out aesKey, out _hmac_key, out _hmac ); |
| | 140 | |
|
| | 141 | | // Create the AES provider |
| 6 | 142 | | _aes = AesCbcHmacSha2.Create( aesKey, iv ); |
| | 143 | |
|
| 6 | 144 | | _inner = _aes.CreateEncryptor(); |
| | 145 | |
|
| 6 | 146 | | _associated_data_length = ConvertToBigEndian( associatedData.Length * 8 ); |
| | 147 | |
|
| | 148 | | // Prime the hash. |
| 6 | 149 | | _hmac.AppendData( associatedData ); |
| 6 | 150 | | _hmac.AppendData( iv ); |
| 6 | 151 | | } |
| | 152 | |
|
| | 153 | | public byte[] Tag |
| | 154 | | { |
| 6 | 155 | | get { return _tag; } |
| | 156 | | } |
| | 157 | |
|
| | 158 | | public bool CanReuseTransform |
| | 159 | | { |
| 0 | 160 | | get { return _inner.CanReuseTransform; } |
| | 161 | | } |
| | 162 | |
|
| | 163 | | public bool CanTransformMultipleBlocks |
| | 164 | | { |
| 0 | 165 | | get { return _inner.CanTransformMultipleBlocks; } |
| | 166 | | } |
| | 167 | |
|
| | 168 | | public int InputBlockSize |
| | 169 | | { |
| 0 | 170 | | get { return _inner.InputBlockSize; } |
| | 171 | | } |
| | 172 | |
|
| | 173 | | public int OutputBlockSize |
| | 174 | | { |
| 0 | 175 | | get { return _inner.OutputBlockSize; } |
| | 176 | | } |
| | 177 | |
|
| | 178 | | public int TransformBlock( byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int out |
| | 179 | | { |
| | 180 | | // Encrypt the block |
| 0 | 181 | | var result = _inner.TransformBlock( inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset ); |
| | 182 | |
|
| | 183 | | // TODO: This is an inefficient copy |
| 0 | 184 | | var data = outputBuffer.Take( outputOffset, result ); |
| | 185 | |
|
| | 186 | | // Add it to the running hash |
| 0 | 187 | | _hmac.AppendData( data ); |
| | 188 | |
|
| 0 | 189 | | return result; |
| | 190 | | } |
| | 191 | |
|
| | 192 | | public byte[] TransformFinalBlock( byte[] inputBuffer, int inputOffset, int inputCount ) |
| | 193 | | { |
| | 194 | | // Encrypt the block |
| 6 | 195 | | var result = _inner.TransformFinalBlock( inputBuffer, inputOffset, inputCount ); |
| | 196 | |
|
| | 197 | | // Add it to the running hash |
| 6 | 198 | | _hmac.AppendData( result ); |
| | 199 | |
|
| | 200 | | // Add the associated_data_length bytes to the hash |
| 6 | 201 | | _hmac.AppendData( _associated_data_length ); |
| | 202 | |
|
| | 203 | | // Compute the tag |
| 6 | 204 | | _tag = _hmac.GetHashAndReset().Take( _hmac_key.Length ); |
| | 205 | |
|
| 6 | 206 | | return result; |
| | 207 | | } |
| | 208 | |
|
| | 209 | | public void Dispose() |
| | 210 | | { |
| 6 | 211 | | Dispose( true ); |
| 6 | 212 | | GC.SuppressFinalize( this ); |
| 6 | 213 | | } |
| | 214 | |
|
| | 215 | | protected virtual void Dispose( bool disposing ) |
| | 216 | | { |
| 6 | 217 | | if ( disposing ) |
| | 218 | | { |
| 6 | 219 | | if ( _inner != null ) |
| | 220 | | { |
| 6 | 221 | | _inner.Dispose(); |
| 6 | 222 | | _inner = null; |
| | 223 | | } |
| | 224 | |
|
| 6 | 225 | | if ( _hmac != null ) |
| | 226 | | { |
| 6 | 227 | | _hmac.Dispose(); |
| 6 | 228 | | _hmac = null; |
| | 229 | | } |
| | 230 | |
|
| 6 | 231 | | if ( _aes != null ) |
| | 232 | | { |
| 6 | 233 | | _aes.Dispose(); |
| 6 | 234 | | _aes = null; |
| | 235 | | } |
| | 236 | | } |
| 6 | 237 | | } |
| | 238 | | } |
| | 239 | |
|
| | 240 | | class AesCbcHmacSha2Decryptor : IAuthenticatedCryptoTransform |
| | 241 | | { |
| | 242 | | readonly byte[] _hmac_key; |
| | 243 | |
|
| | 244 | | readonly byte[] _associated_data_length; |
| | 245 | |
|
| | 246 | | Aes _aes; |
| | 247 | | IncrementalHash _hmac; |
| | 248 | |
|
| | 249 | | ICryptoTransform _inner; |
| | 250 | | byte[] _tag; |
| | 251 | |
|
| 6 | 252 | | internal AesCbcHmacSha2Decryptor( string name, byte[] key, byte[] iv, byte[] associatedData, byte[] authenti |
| | 253 | | { |
| | 254 | | // Split the key to get the AES key, the HMAC key and the HMAC object |
| | 255 | | byte[] aesKey; |
| | 256 | |
|
| 6 | 257 | | GetAlgorithmParameters( name, key, out aesKey, out _hmac_key, out _hmac ); |
| | 258 | |
|
| | 259 | | // Record the tag |
| 6 | 260 | | _tag = authenticationTag; |
| | 261 | |
|
| | 262 | | // Create the AES provider |
| 6 | 263 | | _aes = AesCbcHmacSha2.Create( aesKey, iv ); |
| 6 | 264 | | _inner = _aes.CreateDecryptor(); |
| | 265 | |
|
| 6 | 266 | | _associated_data_length = ConvertToBigEndian( associatedData.Length * 8 ); |
| | 267 | |
|
| | 268 | | // Prime the hash. |
| 6 | 269 | | _hmac.AppendData( associatedData ); |
| 6 | 270 | | _hmac.AppendData( iv ); |
| 6 | 271 | | } |
| | 272 | |
|
| | 273 | | public byte[] Tag |
| | 274 | | { |
| 6 | 275 | | get { return _tag; } |
| | 276 | | } |
| | 277 | |
|
| | 278 | | public bool CanReuseTransform |
| | 279 | | { |
| 0 | 280 | | get { return _inner.CanReuseTransform; } |
| | 281 | | } |
| | 282 | |
|
| | 283 | | public bool CanTransformMultipleBlocks |
| | 284 | | { |
| 0 | 285 | | get { return _inner.CanTransformMultipleBlocks; } |
| | 286 | | } |
| | 287 | |
|
| | 288 | | public int InputBlockSize |
| | 289 | | { |
| 0 | 290 | | get { return _inner.InputBlockSize; } |
| | 291 | | } |
| | 292 | |
|
| | 293 | | public int OutputBlockSize |
| | 294 | | { |
| 0 | 295 | | get { return _inner.OutputBlockSize; } |
| | 296 | | } |
| | 297 | |
|
| | 298 | | public int TransformBlock( byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int out |
| | 299 | | { |
| | 300 | | // TODO: This is an inefficient copy |
| 0 | 301 | | var block = inputBuffer.Take( inputOffset, inputCount ); |
| | 302 | |
|
| | 303 | | // Add the cipher text to the running hash |
| 0 | 304 | | _hmac.AppendData( block ); |
| | 305 | |
|
| | 306 | | // Decrypt the cipher text |
| 0 | 307 | | return _inner.TransformBlock( inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset ); |
| | 308 | | } |
| | 309 | |
|
| | 310 | | public byte[] TransformFinalBlock( byte[] inputBuffer, int inputOffset, int inputCount ) |
| | 311 | | { |
| | 312 | | // TODO: This is an inefficient copy |
| 6 | 313 | | var block = inputBuffer.Take( inputOffset, inputCount ); |
| | 314 | |
|
| | 315 | | // Add the cipher text to the running hash |
| 6 | 316 | | _hmac.AppendData( block ); |
| | 317 | |
|
| | 318 | | // Add the associated_data_length bytes to the hash |
| 6 | 319 | | _hmac.AppendData( _associated_data_length ); |
| | 320 | |
|
| | 321 | | // Compute the tag, then compare it against the expected value |
| | 322 | | // perform transforming the final block |
| 6 | 323 | | var tag = _hmac.GetHashAndReset().Take( _hmac_key.Length ); |
| | 324 | |
|
| 6 | 325 | | if ( !tag.SequenceEqualConstantTime( _tag ) ) |
| 0 | 326 | | throw new CryptographicException( "Data is no authentic" ); |
| | 327 | |
|
| 6 | 328 | | return _inner.TransformFinalBlock( inputBuffer, inputOffset, inputCount ); |
| | 329 | | } |
| | 330 | |
|
| | 331 | | public void Dispose() |
| | 332 | | { |
| 6 | 333 | | Dispose( true ); |
| 6 | 334 | | GC.SuppressFinalize( this ); |
| 6 | 335 | | } |
| | 336 | |
|
| | 337 | | protected virtual void Dispose( bool disposing ) |
| | 338 | | { |
| 6 | 339 | | if ( disposing ) |
| | 340 | | { |
| 6 | 341 | | if ( _inner != null ) |
| | 342 | | { |
| 6 | 343 | | _inner.Dispose(); |
| 6 | 344 | | _inner = null; |
| | 345 | | } |
| | 346 | |
|
| 6 | 347 | | if ( _hmac != null ) |
| | 348 | | { |
| 6 | 349 | | _hmac.Dispose(); |
| 6 | 350 | | _hmac = null; |
| | 351 | | } |
| | 352 | |
|
| 6 | 353 | | if ( _aes != null ) |
| | 354 | | { |
| 6 | 355 | | _aes.Dispose(); |
| 6 | 356 | | _aes = null; |
| | 357 | | } |
| | 358 | | } |
| 6 | 359 | | } |
| | 360 | | } |
| | 361 | |
|
| | 362 | | static byte[] ConvertToBigEndian( Int64 i ) |
| | 363 | | { |
| 12 | 364 | | byte[] temp = BitConverter.GetBytes( i ); |
| | 365 | |
|
| 12 | 366 | | if ( BitConverter.IsLittleEndian ) |
| | 367 | | { |
| 12 | 368 | | Array.Reverse( temp ); |
| | 369 | | } |
| | 370 | |
|
| 12 | 371 | | return temp; |
| | 372 | | } |
| | 373 | | } |
| | 374 | | } |