| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Security.Cryptography; |
| | 6 | | using System.Threading; |
| | 7 | | using Azure.Core; |
| | 8 | |
|
| | 9 | | namespace Azure.Security.KeyVault.Keys.Cryptography |
| | 10 | | { |
| | 11 | | internal class AesCryptographyProvider : LocalCryptographyProvider |
| | 12 | | { |
| 18 | 13 | | internal AesCryptographyProvider(KeyVaultKey key) : base(key) |
| | 14 | | { |
| 18 | 15 | | } |
| | 16 | |
|
| | 17 | | public override bool SupportsOperation(KeyOperation operation) |
| | 18 | | { |
| 16 | 19 | | if (KeyMaterial != null) |
| | 20 | | { |
| 16 | 21 | | if (operation == KeyOperation.WrapKey || operation == KeyOperation.UnwrapKey) |
| | 22 | | { |
| 16 | 23 | | return KeyMaterial.SupportsOperation(operation); |
| | 24 | | } |
| | 25 | | } |
| | 26 | |
|
| 0 | 27 | | return false; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public override UnwrapResult UnwrapKey(KeyWrapAlgorithm algorithm, byte[] encryptedKey, CancellationToken cancel |
| | 31 | | { |
| 8 | 32 | | Argument.AssertNotNull(encryptedKey, nameof(encryptedKey)); |
| | 33 | |
|
| 8 | 34 | | int algorithmKeySizeBytes = algorithm.GetKeySizeInBytes(); |
| 8 | 35 | | if (algorithmKeySizeBytes == 0) |
| | 36 | | { |
| 4 | 37 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(UnwrapKey), algorithm); |
| 4 | 38 | | return null; |
| | 39 | | } |
| | 40 | |
|
| 4 | 41 | | int keySizeBytes = GetKeySizeInBytes(); |
| 4 | 42 | | if (keySizeBytes < algorithmKeySizeBytes) |
| | 43 | | { |
| 0 | 44 | | throw new ArgumentException($"Key wrap algorithm {algorithm} key size {algorithmKeySizeBytes} is greater |
| | 45 | | } |
| | 46 | |
|
| 4 | 47 | | byte[] sizedKey = (keySizeBytes == algorithmKeySizeBytes) ? KeyMaterial.K : KeyMaterial.K.Take(algorithmKeyS |
| | 48 | |
|
| 4 | 49 | | using ICryptoTransform decryptor = AesKw.CreateDecryptor(sizedKey); |
| | 50 | |
|
| 4 | 51 | | byte[] key = decryptor.TransformFinalBlock(encryptedKey, 0, encryptedKey.Length); |
| 4 | 52 | | return new UnwrapResult |
| 4 | 53 | | { |
| 4 | 54 | | Algorithm = algorithm, |
| 4 | 55 | | Key = key, |
| 4 | 56 | | KeyId = KeyMaterial.Id, |
| 4 | 57 | | }; |
| 4 | 58 | | } |
| | 59 | |
|
| | 60 | | public override WrapResult WrapKey(KeyWrapAlgorithm algorithm, byte[] key, CancellationToken cancellationToken) |
| | 61 | | { |
| 12 | 62 | | Argument.AssertNotNull(key, nameof(key)); |
| | 63 | |
|
| 12 | 64 | | ThrowIfTimeInvalid(); |
| | 65 | |
|
| 8 | 66 | | int algorithmKeySizeBytes = algorithm.GetKeySizeInBytes(); |
| 8 | 67 | | if (algorithmKeySizeBytes == 0) |
| | 68 | | { |
| 4 | 69 | | KeysEventSource.Singleton.AlgorithmNotSupported(nameof(WrapKey), algorithm); |
| 4 | 70 | | return null; |
| | 71 | | } |
| | 72 | |
|
| 4 | 73 | | int keySizeBytes = GetKeySizeInBytes(); |
| 4 | 74 | | if (keySizeBytes < algorithmKeySizeBytes) |
| | 75 | | { |
| 0 | 76 | | throw new ArgumentException($"Key wrap algorithm {algorithm} key size {algorithmKeySizeBytes} is greater |
| | 77 | | } |
| | 78 | |
|
| 4 | 79 | | byte[] sizedKey = (keySizeBytes == algorithmKeySizeBytes) ? KeyMaterial.K : KeyMaterial.K.Take(algorithmKeyS |
| | 80 | |
|
| 4 | 81 | | using ICryptoTransform encryptor = AesKw.CreateEncryptor(sizedKey); |
| | 82 | |
|
| 4 | 83 | | byte[] encryptedKey = encryptor.TransformFinalBlock(key, 0, key.Length); |
| 4 | 84 | | return new WrapResult |
| 4 | 85 | | { |
| 4 | 86 | | Algorithm = algorithm, |
| 4 | 87 | | EncryptedKey = encryptedKey, |
| 4 | 88 | | KeyId = KeyMaterial.Id, |
| 4 | 89 | | }; |
| 4 | 90 | | } |
| | 91 | |
|
| | 92 | | private int GetKeySizeInBits() |
| | 93 | | { |
| 0 | 94 | | return GetKeySizeInBytes() << 3; |
| | 95 | | } |
| | 96 | |
|
| | 97 | | private int GetKeySizeInBytes() |
| | 98 | | { |
| 8 | 99 | | if (KeyMaterial.K != null) |
| | 100 | | { |
| 8 | 101 | | return KeyMaterial.K.Length; |
| | 102 | | } |
| | 103 | |
|
| 0 | 104 | | return 0; |
| | 105 | |
|
| | 106 | | } |
| | 107 | | } |
| | 108 | | } |