Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / rsaoaepkeyexchangeformatter.cs
1 using System.Diagnostics.Contracts;
2 // ==++==
3 // 
4 //   Copyright (c) Microsoft Corporation.  All rights reserved.
5 // 
6 // ==--==
7 // <OWNER>Microsoft</OWNER>
8 // 
9
10 namespace System.Security.Cryptography {
11     [System.Runtime.InteropServices.ComVisible(true)]
12     public class RSAOAEPKeyExchangeFormatter : AsymmetricKeyExchangeFormatter {
13         private byte[] ParameterValue;
14         private RSA _rsaKey;
15         private bool?  _rsaOverridesEncrypt;
16         private RandomNumberGenerator RngValue;
17
18         //
19         // public constructors
20         //
21
22         public RSAOAEPKeyExchangeFormatter() {}
23         public RSAOAEPKeyExchangeFormatter(AsymmetricAlgorithm key) {
24             if (key == null) 
25                 throw new ArgumentNullException("key");
26             Contract.EndContractBlock();
27             _rsaKey = (RSA) key;
28         }
29
30         //
31         // public properties
32         //
33
34         /// <internalonly/>
35         public byte[] Parameter {
36             get {
37                 if (ParameterValue != null)
38                     return (byte[]) ParameterValue.Clone(); 
39                 return null;
40             }
41             set {
42                 if (value != null)
43                     ParameterValue = (byte[]) value.Clone();
44                 else 
45                     ParameterValue = null;
46             }
47         }
48
49         /// <internalonly/>
50         public override String Parameters {
51             get { return null; }
52         }
53
54         public RandomNumberGenerator Rng {
55             get { return RngValue; }
56             set { RngValue = value; }
57         }
58
59         //
60         // public methods
61         //
62
63         public override void SetKey(AsymmetricAlgorithm key) {
64             if (key == null) 
65                 throw new ArgumentNullException("key");
66             Contract.EndContractBlock();
67             _rsaKey = (RSA) key;
68             _rsaOverridesEncrypt = default(bool?);
69         }
70
71         [System.Security.SecuritySafeCritical]  // auto-generated
72         public override byte[] CreateKeyExchange(byte[] rgbData) {
73             if (_rsaKey == null)
74                 throw new CryptographicUnexpectedOperationException(Environment.GetResourceString("Cryptography_MissingKey"));
75
76             if (OverridesEncrypt) {
77                 return _rsaKey.Encrypt(rgbData, RSAEncryptionPadding.OaepSHA1);
78             } else {
79                 return Utils.RsaOaepEncrypt(_rsaKey, SHA1.Create(), new PKCS1MaskGenerationMethod(), RandomNumberGenerator.Create(), rgbData);
80             }
81         }
82
83         public override byte[] CreateKeyExchange(byte[] rgbData, Type symAlgType) {
84             return CreateKeyExchange(rgbData);
85         }
86
87         private bool OverridesEncrypt {
88             get {
89                 if (!_rsaOverridesEncrypt.HasValue) {
90                     _rsaOverridesEncrypt = Utils.DoesRsaKeyOverride(_rsaKey, "Encrypt", new Type[] { typeof(byte[]), typeof(RSAEncryptionPadding) });
91                 }
92                 return _rsaOverridesEncrypt.Value;
93             }
94         }
95     }
96 }