| | 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.Security.Cryptography; |
| | 6 | |
|
| | 7 | | namespace Microsoft.Azure.KeyVault.Cryptography.Algorithms |
| | 8 | | { |
| | 9 | | /// <summary> |
| | 10 | | /// Abstract base class for vanilla AESCBC |
| | 11 | | /// </summary> |
| | 12 | | public abstract class AesCbc : SymmetricEncryptionAlgorithm |
| | 13 | | { |
| | 14 | | private static Aes Create( byte[] key, byte[] iv ) |
| | 15 | | { |
| | 16 | | var aes = Aes.Create(); |
| | 17 | |
|
| | 18 | | aes.Mode = CipherMode.CBC; |
| | 19 | | aes.Padding = PaddingMode.PKCS7; |
| | 20 | | aes.KeySize = key.Length * 8; |
| | 21 | | aes.Key = key; |
| | 22 | | aes.IV = iv; |
| | 23 | |
|
| | 24 | | return aes; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | protected AesCbc( string name ) |
| | 28 | | : base( name ) |
| | 29 | | { |
| | 30 | | } |
| | 31 | |
|
| | 32 | | public override ICryptoTransform CreateDecryptor( byte[] key, byte[] iv, byte[] authenticationData, byte[] authe |
| | 33 | | { |
| | 34 | | if ( key == null ) |
| | 35 | | throw new CryptographicException( "No key material" ); |
| | 36 | |
|
| | 37 | | if ( iv == null ) |
| | 38 | | throw new CryptographicException( "No initialization vector" ); |
| | 39 | |
|
| | 40 | | // Note that authenticationData and authenticationTag are ignored. |
| | 41 | |
|
| | 42 | | // Create the AES provider |
| | 43 | | using ( var aes = Create( key, iv ) ) |
| | 44 | | { |
| | 45 | | return aes.CreateDecryptor(); |
| | 46 | | } |
| | 47 | | } |
| | 48 | |
|
| | 49 | | public override ICryptoTransform CreateEncryptor( byte[] key, byte[] iv, byte[] authenticationData ) |
| | 50 | | { |
| | 51 | | if ( key == null ) |
| | 52 | | throw new CryptographicException( "No key material" ); |
| | 53 | |
|
| | 54 | | if ( iv == null ) |
| | 55 | | throw new CryptographicException( "No initialization vector" ); |
| | 56 | |
|
| | 57 | | // Create the AES provider |
| | 58 | | using ( var aes = Create(key, iv ) ) |
| | 59 | | { |
| | 60 | | return aes.CreateEncryptor(); |
| | 61 | | } |
| | 62 | | } |
| | 63 | | } |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// AESCBC 128bit key with PKCS7 Padding |
| | 67 | | /// </summary> |
| | 68 | | public class Aes128Cbc : AesCbc |
| | 69 | | { |
| | 70 | | public const string AlgorithmName = "A128CBC"; |
| | 71 | |
|
| | 72 | | const int KeySizeInBytes = 128 >> 3; |
| | 73 | |
|
| | 74 | | public Aes128Cbc() |
| | 75 | | : base( AlgorithmName ) |
| | 76 | | { |
| | 77 | | } |
| | 78 | |
|
| | 79 | | public override ICryptoTransform CreateDecryptor( byte[] key, byte[] iv, byte[] authenticationData, byte[] authe |
| | 80 | | { |
| | 81 | | if ( key == null ) |
| | 82 | | throw new CryptographicException( "key" ); |
| | 83 | |
|
| | 84 | | if ( key.Length < KeySizeInBytes ) |
| | 85 | | throw new CryptographicException( "key", "key must be at least 128 bits" ); |
| | 86 | |
|
| | 87 | | return base.CreateDecryptor( key.Take( KeySizeInBytes ), iv, authenticationData, authenticationTag ); |
| | 88 | | } |
| | 89 | |
|
| | 90 | | public override ICryptoTransform CreateEncryptor( byte[] key, byte[] iv, byte[] authenticationData ) |
| | 91 | | { |
| | 92 | | if ( key == null ) |
| | 93 | | throw new CryptographicException( "key" ); |
| | 94 | |
|
| | 95 | | if ( key.Length < KeySizeInBytes ) |
| | 96 | | throw new CryptographicException( "key", "key must be at least 128 bits long" ); |
| | 97 | |
|
| | 98 | | return base.CreateEncryptor( key.Take( KeySizeInBytes ), iv, authenticationData ); |
| | 99 | | } |
| | 100 | | } |
| | 101 | |
|
| | 102 | | /// <summary> |
| | 103 | | /// AESCBC 192bit key with PKCS7 Padding |
| | 104 | | /// </summary> |
| | 105 | | public class Aes192Cbc : AesCbc |
| | 106 | | { |
| | 107 | | public const string AlgorithmName = "A192CBC"; |
| | 108 | |
|
| | 109 | | const int KeySizeInBytes = 192 >> 3; |
| | 110 | |
|
| | 111 | | public Aes192Cbc() |
| | 112 | | : base( AlgorithmName ) |
| | 113 | | { |
| | 114 | | } |
| | 115 | |
|
| | 116 | | public override ICryptoTransform CreateDecryptor( byte[] key, byte[] iv, byte[] authenticationData, byte[] authe |
| | 117 | | { |
| | 118 | | if ( key == null ) |
| | 119 | | throw new CryptographicException( "key" ); |
| | 120 | |
|
| | 121 | | if ( key.Length < KeySizeInBytes ) |
| | 122 | | throw new CryptographicException( "key", "key must be at least 192 bits" ); |
| | 123 | |
|
| | 124 | | return base.CreateDecryptor( key.Take( KeySizeInBytes ), iv, authenticationData, authenticationTag ); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | public override ICryptoTransform CreateEncryptor( byte[] key, byte[] iv, byte[] authenticationData ) |
| | 128 | | { |
| | 129 | | if ( key == null ) |
| | 130 | | throw new CryptographicException( "key" ); |
| | 131 | |
|
| | 132 | | if ( key.Length < KeySizeInBytes ) |
| | 133 | | throw new CryptographicException( "key", "key must be at least 192 bits long" ); |
| | 134 | |
|
| | 135 | | return base.CreateEncryptor( key.Take( KeySizeInBytes ), iv, authenticationData ); |
| | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary> |
| | 140 | | /// AESCBC 256bit key with PKCS7 Padding |
| | 141 | | /// </summary> |
| | 142 | | public class Aes256Cbc : AesCbc |
| | 143 | | { |
| | 144 | | public const string AlgorithmName = "A256CBC"; |
| | 145 | |
|
| | 146 | | const int KeySizeInBytes = 256 >> 3; |
| | 147 | |
|
| | 148 | | public Aes256Cbc() |
| 2 | 149 | | : base( AlgorithmName ) |
| | 150 | | { |
| 2 | 151 | | } |
| | 152 | |
|
| | 153 | | public override ICryptoTransform CreateDecryptor( byte[] key, byte[] iv, byte[] authenticationData, byte[] authe |
| | 154 | | { |
| 0 | 155 | | if ( key == null ) |
| 0 | 156 | | throw new CryptographicException( "key" ); |
| | 157 | |
|
| 0 | 158 | | if ( key.Length < KeySizeInBytes ) |
| 0 | 159 | | throw new CryptographicException( "key", "key must be at least 256 bits" ); |
| | 160 | |
|
| 0 | 161 | | return base.CreateDecryptor( key.Take( KeySizeInBytes ), iv, authenticationData, authenticationTag ); |
| | 162 | | } |
| | 163 | |
|
| | 164 | | public override ICryptoTransform CreateEncryptor( byte[] key, byte[] iv, byte[] authenticationData ) |
| | 165 | | { |
| 0 | 166 | | if ( key == null ) |
| 0 | 167 | | throw new CryptographicException( "key" ); |
| | 168 | |
|
| 0 | 169 | | if ( key.Length < KeySizeInBytes ) |
| 0 | 170 | | throw new CryptographicException( "key", "key must be at least 256 bits long" ); |
| | 171 | |
|
| 0 | 172 | | return base.CreateEncryptor( key.Take( KeySizeInBytes ), iv, authenticationData ); |
| | 173 | | } |
| | 174 | | } |
| | 175 | | } |