< Summary

Class:Azure.Security.KeyVault.Keys.Cryptography.RsaCryptographyProvider
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\RsaCryptographyProvider.cs
Covered lines:83
Uncovered lines:38
Coverable lines:121
Total lines:235
Line coverage:68.5% (83 of 121)
Covered branches:31
Total branches:44
Branch coverage:70.4% (31 of 44)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
SupportsOperation(...)-75%85.71%
Encrypt(...)-37.5%25%
Decrypt(...)-44.44%50%
Sign(...)-90.91%83.33%
Verify(...)-89.47%75%
WrapKey(...)-100%100%
UnwrapKey(...)-44.44%50%
Encrypt(...)-100%100%
Decrypt(...)-0%100%

File(s)

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

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System.Security.Cryptography;
 5using System.Threading;
 6using Azure.Core;
 7
 8namespace Azure.Security.KeyVault.Keys.Cryptography
 9{
 10    internal class RsaCryptographyProvider : LocalCryptographyProvider
 11    {
 10812        internal RsaCryptographyProvider(KeyVaultKey key) : base(key)
 13        {
 10814        }
 15
 16        public override bool SupportsOperation(KeyOperation operation)
 17        {
 11618            if (KeyMaterial != null)
 19            {
 11620                if (operation == KeyOperation.Encrypt || operation == KeyOperation.Decrypt || operation == KeyOperation.
 21                {
 11622                    return KeyMaterial.SupportsOperation(operation);
 23                }
 24            }
 25
 026            return false;
 27        }
 28
 29        public override EncryptResult Encrypt(EncryptionAlgorithm algorithm, byte[] plaintext, CancellationToken cancell
 30        {
 831            Argument.AssertNotNull(plaintext, nameof(plaintext));
 32
 833            ThrowIfTimeInvalid();
 34
 435            RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding();
 436            if (padding is null)
 37            {
 438                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Encrypt), algorithm);
 439                return null;
 40            }
 41
 042            byte[] ciphertext = Encrypt(plaintext, padding);
 043            EncryptResult result = null;
 44
 045            if (ciphertext != null)
 46            {
 047                result = new EncryptResult
 048                {
 049                    Algorithm = algorithm,
 050                    Ciphertext = ciphertext,
 051                    KeyId = KeyMaterial.Id,
 052                };
 53            }
 54
 055            return result;
 56        }
 57
 58        public override DecryptResult Decrypt(EncryptionAlgorithm algorithm, byte[] ciphertext, CancellationToken cancel
 59        {
 860            Argument.AssertNotNull(ciphertext, nameof(ciphertext));
 61
 862            if (MustRemote)
 63            {
 64                // A private key is required to decrypt. Send to the server.
 465                KeysEventSource.Singleton.PrivateKeyRequired(nameof(Decrypt));
 466                return null;
 67            }
 68
 469            RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding();
 470            if (padding is null)
 71            {
 472                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Decrypt), algorithm);
 473                return null;
 74            }
 75
 076            byte[] plaintext = Decrypt(ciphertext, padding);
 077            DecryptResult result = null;
 78
 079            if (plaintext != null)
 80            {
 081                result = new DecryptResult
 082                {
 083                    Algorithm = algorithm,
 084                    KeyId = KeyMaterial.Id,
 085                    Plaintext = plaintext,
 086                };
 87            }
 88
 089            return result;
 90        }
 91
 92        public override SignResult Sign(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancellationToken
 93        {
 6094            Argument.AssertNotNull(digest, nameof(digest));
 95
 6096            ThrowIfTimeInvalid();
 97
 98            // A private key is required to sign. Send to the server.
 5699            if (MustRemote)
 100            {
 28101                KeysEventSource.Singleton.PrivateKeyRequired(nameof(Sign));
 28102                return null;
 103            }
 104
 28105            HashAlgorithmName hashAlgorithm = algorithm.GetHashAlgorithmName();
 28106            if (hashAlgorithm == default)
 107            {
 4108                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Sign), algorithm);
 4109                return null;
 110            }
 111
 24112            RSASignaturePadding padding = algorithm.GetRsaSignaturePadding();
 24113            if (padding is null)
 114            {
 0115                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Sign), algorithm);
 0116                return null;
 117            }
 118
 24119            using RSA rsa = KeyMaterial.ToRSA(true);
 24120            byte[] signature = rsa.SignHash(digest, hashAlgorithm, padding);
 121
 24122            return new SignResult
 24123            {
 24124                Algorithm = algorithm,
 24125                KeyId = KeyMaterial.Id,
 24126                Signature = signature,
 24127            };
 24128        }
 129
 130        public override VerifyResult Verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, CancellationT
 131        {
 28132            Argument.AssertNotNull(digest, nameof(digest));
 28133            Argument.AssertNotNull(signature, nameof(signature));
 134
 28135            HashAlgorithmName hashAlgorithm = algorithm.GetHashAlgorithmName();
 28136            if (hashAlgorithm == default)
 137            {
 4138                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Verify), algorithm);
 4139                return null;
 140            }
 141
 24142            RSASignaturePadding padding = algorithm.GetRsaSignaturePadding();
 24143            if (padding is null)
 144            {
 0145                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Verify), algorithm);
 0146                return null;
 147            }
 148
 24149            using RSA rsa = KeyMaterial.ToRSA();
 24150            bool isValid = rsa.VerifyHash(digest, signature, hashAlgorithm, padding);
 151
 24152            return new VerifyResult
 24153            {
 24154                Algorithm = algorithm,
 24155                IsValid = isValid,
 24156                KeyId = KeyMaterial.Id,
 24157            };
 24158        }
 159
 160        public override WrapResult WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellationToken)
 161        {
 12162            Argument.AssertNotNull(key, nameof(key));
 163
 12164            ThrowIfTimeInvalid();
 165
 8166            RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding();
 8167            if (padding is null)
 168            {
 4169                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(WrapKey), algorithm);
 4170                return null;
 171            }
 172
 4173            byte[] encryptedKey = Encrypt(key, padding);
 4174            WrapResult result = null;
 175
 4176            if (encryptedKey != null)
 177            {
 4178                result = new WrapResult
 4179                {
 4180                    Algorithm = algorithm,
 4181                    EncryptedKey = encryptedKey,
 4182                    KeyId = KeyMaterial.Id,
 4183                };
 184            }
 185
 4186            return result;
 187        }
 188
 189        public override UnwrapResult UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationToken cancel
 190        {
 12191            Argument.AssertNotNull(encryptedKey, nameof(encryptedKey));
 192
 12193            if (MustRemote)
 194            {
 195                // A private key is required to decrypt. Send to the server.
 8196                KeysEventSource.Singleton.PrivateKeyRequired(nameof(UnwrapKey));
 8197                return null;
 198            }
 199
 4200            RSAEncryptionPadding padding = algorithm.GetRsaEncryptionPadding();
 4201            if (padding is null)
 202            {
 4203                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(UnwrapKey), algorithm);
 4204                return null;
 205            }
 206
 0207            byte[] key = Decrypt(encryptedKey, padding);
 0208            UnwrapResult result = null;
 209
 0210            if (key != null)
 211            {
 0212                result = new UnwrapResult
 0213                {
 0214                    Algorithm = algorithm,
 0215                    Key = key,
 0216                    KeyId = KeyMaterial.Id,
 0217                };
 218            }
 219
 0220            return result;
 221        }
 222
 223        private byte[] Encrypt(byte[] data, RSAEncryptionPadding padding)
 224        {
 4225            using RSA rsa = KeyMaterial.ToRSA(true);
 4226            return rsa.Encrypt(data, padding);
 4227        }
 228
 229        private byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
 230        {
 0231            using RSA rsa = KeyMaterial.ToRSA();
 0232            return rsa.Decrypt(data, padding);
 0233        }
 234    }
 235}