< Summary

Class:Azure.Security.KeyVault.Keys.Cryptography.AesCryptographyProvider
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\AesCryptographyProvider.cs
Covered lines:42
Uncovered lines:5
Coverable lines:47
Total lines:108
Line coverage:89.3% (42 of 47)
Covered branches:13
Total branches:20
Branch coverage:65% (13 of 20)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
SupportsOperation(...)-75%66.67%
UnwrapKey(...)-94.44%66.67%
WrapKey(...)-94.74%66.67%
GetKeySizeInBits()-0%100%
GetKeySizeInBytes()-66.67%50%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\AesCryptographyProvider.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Security.Cryptography;
 6using System.Threading;
 7using Azure.Core;
 8
 9namespace Azure.Security.KeyVault.Keys.Cryptography
 10{
 11    internal class AesCryptographyProvider : LocalCryptographyProvider
 12    {
 1813        internal AesCryptographyProvider(KeyVaultKey key) : base(key)
 14        {
 1815        }
 16
 17        public override bool SupportsOperation(KeyOperation operation)
 18        {
 1619            if (KeyMaterial != null)
 20            {
 1621                if (operation == KeyOperation.WrapKey || operation == KeyOperation.UnwrapKey)
 22                {
 1623                    return KeyMaterial.SupportsOperation(operation);
 24                }
 25            }
 26
 027            return false;
 28        }
 29
 30        public override UnwrapResult UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationToken cancel
 31        {
 832            Argument.AssertNotNull(encryptedKey, nameof(encryptedKey));
 33
 834            int algorithmKeySizeBytes = algorithm.GetKeySizeInBytes();
 835            if (algorithmKeySizeBytes == 0)
 36            {
 437                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(UnwrapKey), algorithm);
 438                return null;
 39            }
 40
 441            int keySizeBytes = GetKeySizeInBytes();
 442            if (keySizeBytes < algorithmKeySizeBytes)
 43            {
 044                throw new ArgumentException($"Key wrap algorithm {algorithm} key size {algorithmKeySizeBytes} is greater
 45            }
 46
 447            byte[] sizedKey = (keySizeBytes == algorithmKeySizeBytes) ? KeyMaterial.K : KeyMaterial.K.Take(algorithmKeyS
 48
 449            using ICryptoTransform decryptor = AesKw.CreateDecryptor(sizedKey);
 50
 451            byte[] key = decryptor.TransformFinalBlock(encryptedKey, 0, encryptedKey.Length);
 452            return new UnwrapResult
 453            {
 454                Algorithm = algorithm,
 455                Key = key,
 456                KeyId = KeyMaterial.Id,
 457            };
 458        }
 59
 60        public override WrapResult WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellationToken)
 61        {
 1262            Argument.AssertNotNull(key, nameof(key));
 63
 1264            ThrowIfTimeInvalid();
 65
 866            int algorithmKeySizeBytes = algorithm.GetKeySizeInBytes();
 867            if (algorithmKeySizeBytes == 0)
 68            {
 469                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(WrapKey), algorithm);
 470                return null;
 71            }
 72
 473            int keySizeBytes = GetKeySizeInBytes();
 474            if (keySizeBytes < algorithmKeySizeBytes)
 75            {
 076                throw new ArgumentException($"Key wrap algorithm {algorithm} key size {algorithmKeySizeBytes} is greater
 77            }
 78
 479            byte[] sizedKey = (keySizeBytes == algorithmKeySizeBytes) ? KeyMaterial.K : KeyMaterial.K.Take(algorithmKeyS
 80
 481            using ICryptoTransform encryptor = AesKw.CreateEncryptor(sizedKey);
 82
 483            byte[] encryptedKey = encryptor.TransformFinalBlock(key, 0, key.Length);
 484            return new WrapResult
 485            {
 486                Algorithm = algorithm,
 487                EncryptedKey = encryptedKey,
 488                KeyId = KeyMaterial.Id,
 489            };
 490        }
 91
 92        private int GetKeySizeInBits()
 93        {
 094            return GetKeySizeInBytes() << 3;
 95        }
 96
 97        private int GetKeySizeInBytes()
 98        {
 899            if (KeyMaterial.K != null)
 100            {
 8101                return KeyMaterial.K.Length;
 102            }
 103
 0104            return 0;
 105
 106        }
 107    }
 108}