| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. See License.txt in the project root for |
| | | 3 | | // license information. |
| | | 4 | | |
| | | 5 | | using System; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Security.Cryptography; |
| | | 8 | | |
| | | 9 | | namespace Microsoft.Azure.KeyVault.Cryptography.Algorithms |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// RSA-OAEP Encryption. |
| | | 13 | | /// </summary> |
| | | 14 | | public class RsaOaep : RsaEncryption |
| | | 15 | | { |
| | | 16 | | public const string AlgorithmName = "RSA-OAEP"; |
| | | 17 | | |
| | | 18 | | public RsaOaep() |
| | 2 | 19 | | : base( AlgorithmName ) |
| | | 20 | | { |
| | 2 | 21 | | } |
| | | 22 | | |
| | | 23 | | public override ICryptoTransform CreateEncryptor( AsymmetricAlgorithm key ) |
| | | 24 | | { |
| | 8 | 25 | | RSA csp = key as RSA; |
| | | 26 | | |
| | 8 | 27 | | if ( csp == null ) |
| | 0 | 28 | | throw new ArgumentException( "key must be an instance of RSA", "key" ); |
| | | 29 | | |
| | 8 | 30 | | return new RsaOaepEncryptor( csp ); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public override ICryptoTransform CreateDecryptor( AsymmetricAlgorithm key ) |
| | | 34 | | { |
| | 8 | 35 | | RSA csp = key as RSA; |
| | | 36 | | |
| | 8 | 37 | | if ( csp == null ) |
| | 0 | 38 | | throw new ArgumentException( "key must be an instance of RSA", "key" ); |
| | | 39 | | |
| | 8 | 40 | | return new RsaOaepDecryptor( csp ); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// RSA 15 Decryptor |
| | | 45 | | /// </summary> |
| | | 46 | | /// <remarks> |
| | | 47 | | /// While this class has a reference to an IDisposable object, |
| | | 48 | | /// it is not the owner of the object and will not Dispose it. |
| | | 49 | | /// </remarks> |
| | | 50 | | class RsaOaepDecryptor : ICryptoTransform |
| | | 51 | | { |
| | | 52 | | private readonly RSA _csp; |
| | | 53 | | |
| | 8 | 54 | | internal RsaOaepDecryptor( RSA csp ) |
| | | 55 | | { |
| | 8 | 56 | | _csp = csp; |
| | 8 | 57 | | } |
| | | 58 | | |
| | | 59 | | public bool CanReuseTransform |
| | | 60 | | { |
| | 0 | 61 | | get { throw new NotImplementedException(); } |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | public bool CanTransformMultipleBlocks |
| | | 65 | | { |
| | 0 | 66 | | get { throw new NotImplementedException(); } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | public int InputBlockSize |
| | | 70 | | { |
| | 0 | 71 | | get { throw new NotImplementedException(); } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public int OutputBlockSize |
| | | 75 | | { |
| | 0 | 76 | | get { throw new NotImplementedException(); } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public int TransformBlock( byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int out |
| | | 80 | | { |
| | 0 | 81 | | throw new NotImplementedException(); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public byte[] TransformFinalBlock( byte[] inputBuffer, int inputOffset, int inputCount ) |
| | | 85 | | { |
| | 8 | 86 | | byte[] block = inputBuffer.Skip( inputOffset ).Take( inputCount ).ToArray(); |
| | | 87 | | |
| | | 88 | | #if FullNetFx |
| | | 89 | | if ( _csp is RSACryptoServiceProvider ) |
| | | 90 | | { |
| | | 91 | | return ( ( RSACryptoServiceProvider )_csp ).Decrypt( block, true ); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | throw new CryptographicException( string.Format( "{0} is not supported", _csp.GetType().FullName ) ); |
| | | 95 | | #elif NETSTANDARD |
| | 8 | 96 | | return _csp.Decrypt( block, RSAEncryptionPadding.OaepSHA1 ); |
| | | 97 | | #else |
| | | 98 | | #error Unknown Framework |
| | | 99 | | #endif |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | public void Dispose() |
| | | 103 | | { |
| | | 104 | | // Intentionally empty |
| | 8 | 105 | | } |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// RSA 15 Encryptor |
| | | 110 | | /// </summary> |
| | | 111 | | /// <remarks> |
| | | 112 | | /// While this class has a reference to an IDisposable object, |
| | | 113 | | /// it is not the owner of the object and will not Dispose it. |
| | | 114 | | /// </remarks> |
| | | 115 | | class RsaOaepEncryptor : ICryptoTransform |
| | | 116 | | { |
| | | 117 | | private readonly RSA _csp; |
| | | 118 | | |
| | 8 | 119 | | internal RsaOaepEncryptor( RSA csp ) |
| | | 120 | | { |
| | 8 | 121 | | _csp = csp; |
| | 8 | 122 | | } |
| | | 123 | | |
| | | 124 | | public bool CanReuseTransform |
| | | 125 | | { |
| | 0 | 126 | | get { throw new NotImplementedException(); } |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public bool CanTransformMultipleBlocks |
| | | 130 | | { |
| | 0 | 131 | | get { throw new NotImplementedException(); } |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | public int InputBlockSize |
| | | 135 | | { |
| | 0 | 136 | | get { throw new NotImplementedException(); } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | public int OutputBlockSize |
| | | 140 | | { |
| | 0 | 141 | | get { throw new NotImplementedException(); } |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | public int TransformBlock( byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int out |
| | | 145 | | { |
| | 0 | 146 | | throw new NotImplementedException(); |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | public byte[] TransformFinalBlock( byte[] inputBuffer, int inputOffset, int inputCount ) |
| | | 150 | | { |
| | 8 | 151 | | byte[] block = inputBuffer.Skip( inputOffset ).Take( inputCount ).ToArray(); |
| | | 152 | | |
| | | 153 | | #if FullNetFx |
| | | 154 | | if ( _csp is RSACryptoServiceProvider ) |
| | | 155 | | { |
| | | 156 | | return ( ( RSACryptoServiceProvider )_csp ).Encrypt( block, true ); |
| | | 157 | | } |
| | | 158 | | |
| | | 159 | | throw new CryptographicException( string.Format( "{0} is not supported", _csp.GetType().FullName ) ); |
| | | 160 | | #elif NETSTANDARD |
| | 8 | 161 | | return _csp.Encrypt( block, RSAEncryptionPadding.OaepSHA1 ); |
| | | 162 | | #else |
| | | 163 | | #error Unknown Framework |
| | | 164 | | #endif |
| | | 165 | | } |
| | | 166 | | |
| | | 167 | | public void Dispose() |
| | | 168 | | { |
| | | 169 | | // Intentionally empty |
| | 8 | 170 | | } |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | } |