< Summary

Class:Azure.Security.KeyVault.Keys.Cryptography.EcCryptographyProvider
Assembly:Azure.Security.KeyVault.Keys
File(s):C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\EcCryptographyProvider.cs
Covered lines:56
Uncovered lines:2
Coverable lines:58
Total lines:134
Line coverage:96.5% (56 of 58)
Covered branches:28
Total branches:30
Branch coverage:93.3% (28 of 30)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
SupportsOperation(...)-100%100%
Sign(...)-95.83%90%
Verify(...)-95.24%87.5%

File(s)

C:\Git\azure-sdk-for-net\sdk\keyvault\Azure.Security.KeyVault.Keys\src\Cryptography\EcCryptographyProvider.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 EcCryptographyProvider : LocalCryptographyProvider
 12    {
 13        private readonly KeyCurveName _curve;
 14        private readonly JsonWebKey _keyMaterial;
 15
 12416        internal EcCryptographyProvider(KeyVaultKey key) : base(key)
 17        {
 18            // Unset the KeyMaterial since we want to conditionally set it if supported.
 12419            KeyMaterial = null;
 20
 21            // Only set the JWK if we support the algorithm locally.
 12422            JsonWebKey keyMaterial = key.Key;
 12423            if (keyMaterial != null && keyMaterial.CurveName.HasValue)
 24            {
 25                // Save the key material to use for operational support validation.
 12426                _keyMaterial = keyMaterial;
 27
 12428                _curve = keyMaterial.CurveName.Value;
 12429                if (_curve.IsSupported)
 30                {
 10831                    KeyMaterial = keyMaterial;
 32                }
 33            }
 12434        }
 35
 36        public override bool SupportsOperation(KeyOperation operation)
 37        {
 6438            if (_keyMaterial != null)
 39            {
 6440                if (operation == KeyOperation.Sign || operation == KeyOperation.Verify)
 41                {
 5642                    return _keyMaterial.SupportsOperation(operation);
 43                }
 44            }
 45
 846            return false;
 47        }
 48
 49        public override SignResult Sign(SignatureAlgorithm algorithm, byte[] digest, CancellationToken cancellationToken
 50        {
 7051            Argument.AssertNotNull(digest, nameof(digest));
 52
 6853            ThrowIfTimeInvalid();
 54
 55            // The JWK is not supported by this client. Send to the server.
 6456            if (KeyMaterial is null)
 57            {
 658                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Sign), _curve);
 659                return null;
 60            }
 61
 62            // A private key is required to sign. Send to the server.
 5863            if (MustRemote)
 64            {
 1865                KeysEventSource.Singleton.PrivateKeyRequired(nameof(Sign));
 1866                return null;
 67            }
 68
 4069            KeyCurveName algorithmCurve = algorithm.GetEcKeyCurveName();
 4070            if (_curve.KeySize != algorithmCurve.KeySize)
 71            {
 2072                throw new ArgumentException($"Signature algorithm {algorithm} key size {algorithmCurve.KeySize} does not
 73            }
 74
 2075            if (_curve != algorithmCurve)
 76            {
 477                throw new ArgumentException($"Signature algorithm {algorithm} key curve name does not correspond to unde
 78            }
 79
 1680            using ECDsa ecdsa = KeyMaterial.ToECDsa(true, false);
 1681            if (ecdsa is null)
 82            {
 083                return null;
 84            }
 85
 1686            byte[] signature = ecdsa.SignHash(digest);
 1687            return new SignResult
 1688            {
 1689                Algorithm = algorithm,
 1690                KeyId = KeyMaterial.Id,
 1691                Signature = signature,
 1692            };
 1693        }
 94
 95        public override VerifyResult Verify(SignatureAlgorithm algorithm, byte[] digest, byte[] signature, CancellationT
 96        {
 97            // The JWK is not supported by this client. Send to the server.
 4898            Argument.AssertNotNull(digest, nameof(digest));
 4699            Argument.AssertNotNull(signature, nameof(signature));
 100
 101            // The JWK is not supported by this client. Send to the server.
 44102            if (KeyMaterial is null)
 103            {
 6104                KeysEventSource.Singleton.AlgorithmNotSupported(nameof(Verify), _curve);
 6105                return null;
 106            }
 107
 38108            KeyCurveName algorithmCurve = algorithm.GetEcKeyCurveName();
 38109            if (_curve.KeySize != algorithmCurve.KeySize)
 110            {
 20111                throw new ArgumentException($"Signature algorithm {algorithm} key size {algorithmCurve.KeySize} does not
 112            }
 113
 18114            if (_curve != algorithmCurve)
 115            {
 4116                throw new ArgumentException($"Signature algorithm {algorithm} key curve name does not correspond to unde
 117            }
 118
 14119            using ECDsa ecdsa = KeyMaterial.ToECDsa(false, false);
 14120            if (ecdsa is null)
 121            {
 0122                return null;
 123            }
 124
 14125            bool isValid = ecdsa.VerifyHash(digest, signature);
 14126            return new VerifyResult
 14127            {
 14128                Algorithm = algorithm,
 14129                IsValid = isValid,
 14130                KeyId = KeyMaterial.Id,
 14131            };
 14132        }
 133    }
 134}