| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using System.Security.Cryptography; |
| | 7 | |
|
| | 8 | | namespace Azure.Security.KeyVault.Keys.Cryptography |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// An algorithm used for key wrap and unwrap. |
| | 12 | | /// </summary> |
| | 13 | | public readonly struct KeyWrapAlgorithm : IEquatable<KeyWrapAlgorithm> |
| | 14 | | { |
| | 15 | | internal const string RsaOaepValue = "RSA-OAEP"; |
| | 16 | | internal const string Rsa15Value = "RSA1_5"; |
| | 17 | | internal const string RsaOaep256Value = "RSA-OAEP-256"; |
| | 18 | | internal const string A128KWValue = "A128KW"; |
| | 19 | | internal const string A192KWValue = "A192KW"; |
| | 20 | | internal const string A256KWValue = "A256KW"; |
| | 21 | |
|
| | 22 | | private readonly string _value; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of the <see cref="KeyWrapAlgorithm"/> structure. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="value">The string value of the instance.</param> |
| | 28 | | public KeyWrapAlgorithm(string value) |
| | 29 | | { |
| 40 | 30 | | _value = value ?? throw new ArgumentNullException(nameof(value)); |
| 40 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Gets an RSA-OAEP <see cref="KeyWrapAlgorithm"/>. |
| | 35 | | /// </summary> |
| 26 | 36 | | public static KeyWrapAlgorithm RsaOaep { get; } = new KeyWrapAlgorithm(RsaOaepValue); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Gets an RSA1_5 <see cref="KeyWrapAlgorithm"/>. |
| | 40 | | /// </summary> |
| 10 | 41 | | public static KeyWrapAlgorithm Rsa15 { get; } = new KeyWrapAlgorithm(Rsa15Value); |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Gets an RSA-OAEP-256 <see cref="KeyWrapAlgorithm"/>. |
| | 45 | | /// </summary> |
| 14 | 46 | | public static KeyWrapAlgorithm RsaOaep256 { get; } = new KeyWrapAlgorithm(RsaOaep256Value); |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Gets an AES 128 Key Wrap <see cref="KeyWrapAlgorithm"/>. |
| | 50 | | /// </summary> |
| 6 | 51 | | public static KeyWrapAlgorithm A128KW { get; } = new KeyWrapAlgorithm(A128KWValue); |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Gets an AES 192 Key Wrap <see cref="KeyWrapAlgorithm"/>. |
| | 55 | | /// </summary> |
| 0 | 56 | | public static KeyWrapAlgorithm A192KW { get; } = new KeyWrapAlgorithm(A192KWValue); |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Gets an AES 256 Key Wrap <see cref="KeyWrapAlgorithm"/>. |
| | 60 | | /// </summary> |
| 14 | 61 | | public static KeyWrapAlgorithm A256KW { get; } = new KeyWrapAlgorithm(A256KWValue); |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Determines if two <see cref="KeyWrapAlgorithm"/> values are the same. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="left">The first <see cref="KeyWrapAlgorithm"/> to compare.</param> |
| | 67 | | /// <param name="right">The second <see cref="KeyWrapAlgorithm"/> to compare.</param> |
| | 68 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are the same; otherwise, false.</retur |
| 0 | 69 | | public static bool operator ==(KeyWrapAlgorithm left, KeyWrapAlgorithm right) => left.Equals(right); |
| | 70 | |
|
| | 71 | | /// <summary> |
| | 72 | | /// Determines if two <see cref="KeyWrapAlgorithm"/> values are different. |
| | 73 | | /// </summary> |
| | 74 | | /// <param name="left">The first <see cref="KeyWrapAlgorithm"/> to compare.</param> |
| | 75 | | /// <param name="right">The second <see cref="KeyWrapAlgorithm"/> to compare.</param> |
| | 76 | | /// <returns>True if <paramref name="left"/> and <paramref name="right"/> are different; otherwise, false.</retu |
| 0 | 77 | | public static bool operator !=(KeyWrapAlgorithm left, KeyWrapAlgorithm right) => !left.Equals(right); |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Converts a string to a <see cref="KeyWrapAlgorithm"/>. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="value">The string value to convert.</param> |
| 28 | 83 | | public static implicit operator KeyWrapAlgorithm(string value) => new KeyWrapAlgorithm(value); |
| | 84 | |
|
| | 85 | | /// <inheritdoc/> |
| | 86 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 87 | | public override bool Equals(object obj) => obj is KeyWrapAlgorithm other && Equals(other); |
| | 88 | |
|
| | 89 | | /// <inheritdoc/> |
| | 90 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 24 | 91 | | public bool Equals(KeyWrapAlgorithm other) => string.Equals(_value, other._value, StringComparison.Ordinal); |
| | 92 | |
|
| | 93 | | /// <inheritdoc/> |
| | 94 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 95 | | public override int GetHashCode() => _value?.GetHashCode() ?? 0; |
| | 96 | |
|
| | 97 | | /// <inheritdoc/> |
| | 98 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 120 | 99 | | public override string ToString() => _value; |
| | 100 | |
|
| 12 | 101 | | internal RSAEncryptionPadding GetRsaEncryptionPadding() => _value switch |
| 12 | 102 | | { |
| 0 | 103 | | Rsa15Value => RSAEncryptionPadding.Pkcs1, |
| 16 | 104 | | RsaOaepValue => RSAEncryptionPadding.OaepSHA1, |
| 0 | 105 | | RsaOaep256Value => RSAEncryptionPadding.OaepSHA256, |
| 20 | 106 | | _ => null, |
| 12 | 107 | | }; |
| | 108 | |
|
| 16 | 109 | | internal int GetKeySizeInBits() => _value switch |
| 16 | 110 | | { |
| 0 | 111 | | A128KWValue => 128, |
| 0 | 112 | | A192KWValue => 192, |
| 24 | 113 | | A256KWValue => 256, |
| 24 | 114 | | _ => 0, |
| 16 | 115 | | }; |
| | 116 | |
|
| 16 | 117 | | internal int GetKeySizeInBytes() => GetKeySizeInBits() >> 3; |
| | 118 | | } |
| | 119 | | } |