2 // DiffieHellman.cs: Defines a base class from which all Diffie-Hellman implementations inherit
5 // Pieter Philippaerts (Pieter@mentalis.org)
7 // (C) 2003 The Mentalis.org Team (http://www.mentalis.org/)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Security;
34 using System.Security.Cryptography;
38 namespace Mono.Security.Cryptography {
40 /// Defines a base class from which all Diffie-Hellman implementations inherit.
42 public abstract class DiffieHellman : AsymmetricAlgorithm {
44 /// Creates an instance of the default implementation of the <see cref="DiffieHellman"/> algorithm.
46 /// <returns>A new instance of the default implementation of DiffieHellman.</returns>
47 public static new DiffieHellman Create () {
48 return Create ("Mono.Security.Cryptography.DiffieHellman");
51 /// Creates an instance of the specified implementation of <see cref="DiffieHellman"/>.
53 /// <param name="algName">The name of the implementation of DiffieHellman to use.</param>
54 /// <returns>A new instance of the specified implementation of DiffieHellman.</returns>
55 public static new DiffieHellman Create (string algName) {
56 return (DiffieHellman) CryptoConfig.CreateFromName (algName);
60 /// When overridden in a derived class, creates the key exchange data.
62 /// <returns>The key exchange data to be sent to the intended recipient.</returns>
63 public abstract byte[] CreateKeyExchange();
65 /// When overridden in a derived class, extracts secret information from the key exchange data.
67 /// <param name="keyex">The key exchange data within which the secret information is hidden.</param>
68 /// <returns>The secret information derived from the key exchange data.</returns>
69 public abstract byte[] DecryptKeyExchange (byte[] keyex);
72 /// When overridden in a derived class, exports the <see cref="DHParameters"/>.
74 /// <param name="includePrivate"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
75 /// <returns>The parameters for Diffie-Hellman.</returns>
76 public abstract DHParameters ExportParameters (bool includePrivate);
78 /// When overridden in a derived class, imports the specified <see cref="DHParameters"/>.
80 /// <param name="parameters">The parameters for Diffie-Hellman.</param>
81 public abstract void ImportParameters (DHParameters parameters);
83 private byte[] GetNamedParam(SecurityElement se, string param) {
84 SecurityElement sep = se.SearchForChildByTag(param);
87 return Convert.FromBase64String(sep.Text);
90 /// Reconstructs a <see cref="DiffieHellman"/> object from an XML string.
92 /// <param name="xmlString">The XML string to use to reconstruct the DiffieHellman object.</param>
93 /// <exception cref="CryptographicException">One of the values in the XML string is invalid.</exception>
94 public override void FromXmlString (string xmlString) {
95 if (xmlString == null)
96 throw new ArgumentNullException ("xmlString");
98 DHParameters dhParams = new DHParameters();
100 SecurityParser sp = new SecurityParser();
101 sp.LoadXml(xmlString);
102 SecurityElement se = sp.ToXml();
103 if (se.Tag != "DHKeyValue")
104 throw new CryptographicException();
105 dhParams.P = GetNamedParam(se, "P");
106 dhParams.G = GetNamedParam(se, "G");
107 dhParams.X = GetNamedParam(se, "X");
108 ImportParameters(dhParams);
110 if (dhParams.P != null)
111 Array.Clear(dhParams.P, 0, dhParams.P.Length);
112 if (dhParams.G != null)
113 Array.Clear(dhParams.G, 0, dhParams.G.Length);
114 if (dhParams.X != null)
115 Array.Clear(dhParams.X, 0, dhParams.X.Length);
119 /// Creates and returns an XML string representation of the current <see cref="DiffieHellman"/> object.
121 /// <param name="includePrivateParameters"><b>true</b> to include private parameters; otherwise, <b>false</b>.</param>
122 /// <returns>An XML string encoding of the current DiffieHellman object.</returns>
123 public override string ToXmlString (bool includePrivateParameters) {
124 StringBuilder sb = new StringBuilder ();
125 DHParameters dhParams = ExportParameters(includePrivateParameters);
127 sb.Append ("<DHKeyValue>");
130 sb.Append (Convert.ToBase64String (dhParams.P));
134 sb.Append (Convert.ToBase64String (dhParams.G));
137 if (includePrivateParameters) {
139 sb.Append (Convert.ToBase64String (dhParams.X));
143 sb.Append ("</DHKeyValue>");
145 Array.Clear(dhParams.P, 0, dhParams.P.Length);
146 Array.Clear(dhParams.G, 0, dhParams.G.Length);
147 if (dhParams.X != null)
148 Array.Clear(dhParams.X, 0, dhParams.X.Length);
150 return sb.ToString ();