| | 1 | | // |
| | 2 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 3 | | // Licensed under the MIT License. See License.txt in the project root for |
| | 4 | | // license information. |
| | 5 | | // |
| | 6 | |
|
| | 7 | | using System; |
| | 8 | | using System.Security.Cryptography; |
| | 9 | |
|
| | 10 | | namespace Microsoft.Azure.KeyVault.Cryptography.Algorithms |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Abstract Elliptic Curve Digital Signature Algorithm (ECDSA). |
| | 14 | | /// </summary> |
| | 15 | | public abstract class Ecdsa : AsymmetricSignatureAlgorithm |
| | 16 | | { |
| | 17 | | protected Ecdsa( string name ) : base( name ) |
| | 18 | | { |
| | 19 | | } |
| | 20 | |
|
| | 21 | | protected static ISignatureTransform CreateSignatureTransform( AsymmetricAlgorithm key, string algorithmName ) |
| | 22 | | { |
| | 23 | | if ( key == null ) |
| | 24 | | throw new ArgumentNullException( nameof( key ) ); |
| | 25 | |
|
| | 26 | | var ecdsa = key as ECDsa; |
| | 27 | | if ( ecdsa == null ) |
| | 28 | | throw new ArgumentException( "Invalid key type." ); |
| | 29 | |
|
| | 30 | | #if FullNetFx |
| | 31 | | if ( ecdsa.SignatureAlgorithm != algorithmName ) |
| | 32 | | throw new ArgumentException( $"Invalid key algorithm. Expected {algorithmName}, found {ecdsa.SignatureAl |
| | 33 | | #endif |
| | 34 | |
|
| | 35 | | return new EcdsaSignatureTransform( ecdsa ); |
| | 36 | | } |
| | 37 | | } |
| | 38 | |
|
| | 39 | | internal sealed class EcdsaSignatureTransform : ISignatureTransform |
| | 40 | | { |
| | 41 | | private readonly ECDsa _key; |
| | 42 | |
|
| 64 | 43 | | public EcdsaSignatureTransform(ECDsa key) |
| | 44 | | { |
| 64 | 45 | | _key = key; |
| 64 | 46 | | } |
| | 47 | |
|
| | 48 | | public byte[] Sign(byte[] digest) |
| | 49 | | { |
| 16 | 50 | | return _key.SignHash(digest); |
| | 51 | | } |
| | 52 | |
|
| | 53 | | public bool Verify(byte[] digest, byte[] signature) |
| | 54 | | { |
| 48 | 55 | | return _key.VerifyHash(digest, signature); |
| | 56 | | } |
| | 57 | | } |
| | 58 | |
|
| | 59 | | } |