< Summary

Class:Microsoft.Azure.KeyVault.Cryptography.Algorithms.Aes128Cbc
Assembly:Microsoft.Azure.KeyVault.Cryptography
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Cryptography\src\Algorithms\AesCbc.cs
Covered lines:8
Uncovered lines:4
Coverable lines:12
Total lines:175
Line coverage:66.6% (8 of 12)
Covered branches:4
Total branches:8
Branch coverage:50% (4 of 8)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor()-100%100%
CreateDecryptor(...)-60%50%
CreateEncryptor(...)-60%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Microsoft.Azure.KeyVault.Cryptography\src\Algorithms\AesCbc.cs

#LineLine coverage
 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
 5using System.Security.Cryptography;
 6
 7namespace 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()
 1075            : base( AlgorithmName )
 76        {
 1077        }
 78
 79        public override ICryptoTransform CreateDecryptor( byte[] key, byte[] iv, byte[] authenticationData, byte[] authe
 80        {
 881            if ( key == null )
 082                throw new CryptographicException( "key" );
 83
 884            if ( key.Length < KeySizeInBytes )
 085                throw new CryptographicException( "key", "key must be at least 128 bits" );
 86
 887            return base.CreateDecryptor( key.Take( KeySizeInBytes ), iv, authenticationData, authenticationTag );
 88        }
 89
 90        public override ICryptoTransform CreateEncryptor( byte[] key, byte[] iv, byte[] authenticationData )
 91        {
 892            if ( key == null )
 093                throw new CryptographicException( "key" );
 94
 895            if ( key.Length < KeySizeInBytes )
 096                throw new CryptographicException( "key", "key must be at least 128 bits long" );
 97
 898            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()
 149            : base( AlgorithmName )
 150        {
 151        }
 152
 153        public override ICryptoTransform CreateDecryptor( byte[] key, byte[] iv, byte[] authenticationData, byte[] authe
 154        {
 155            if ( key == null )
 156                throw new CryptographicException( "key" );
 157
 158            if ( key.Length < KeySizeInBytes )
 159                throw new CryptographicException( "key", "key must be at least 256 bits" );
 160
 161            return base.CreateDecryptor( key.Take( KeySizeInBytes ), iv, authenticationData, authenticationTag );
 162        }
 163
 164        public override ICryptoTransform CreateEncryptor( byte[] key, byte[] iv, byte[] authenticationData )
 165        {
 166            if ( key == null )
 167                throw new CryptographicException( "key" );
 168
 169            if ( key.Length < KeySizeInBytes )
 170                throw new CryptographicException( "key", "key must be at least 256 bits long" );
 171
 172            return base.CreateEncryptor( key.Take( KeySizeInBytes ), iv, authenticationData );
 173        }
 174    }
 175}