(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Security.Cryptography / DSA.cs
bloba18506a9b5248972cdb91563eb21f86bc9101496
1 //
2 // System.Security.Cryptography.DSA.cs class implementation
3 //
4 // Authors:
5 // Thomas Neidhart (tome@sbox.tugraz.at)
6 // Sebastien Pouliot (sebastien@ximian.com)
7 //
8 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 using System;
32 using System.Globalization;
33 using System.Text;
35 using Mono.Xml;
36 using Mono.Security;
38 // References:
39 // a. FIPS PUB 186-2: Digital Signature Standard (DSS)
40 // http://csrc.nist.gov/publications/fips/fips186-2/fips186-2-change1.pdf
42 namespace System.Security.Cryptography {
44 public abstract class DSA : AsymmetricAlgorithm {
46 #if NET_2_0
47 // Constructor visibility fixed in Fx 2.0
48 protected DSA ()
49 #else
50 // LAMESPEC: It says to derive new DSA implemenation from DSA class.
51 // Well it's aint gonna be easy this way.
52 // RSA constructor is public
53 internal DSA ()
54 #endif
58 public static new DSA Create ()
60 return Create ("System.Security.Cryptography.DSA");
63 public static new DSA Create (string algName)
65 return (DSA) CryptoConfig.CreateFromName (algName);
68 public abstract byte[] CreateSignature (byte[] rgbHash);
70 public abstract DSAParameters ExportParameters (bool includePrivateParameters);
72 internal void ZeroizePrivateKey (DSAParameters parameters)
74 if (parameters.X != null)
75 Array.Clear (parameters.X, 0, parameters.X.Length);
78 private byte[] GetNamedParam (SecurityElement se, string param)
80 SecurityElement sep = se.SearchForChildByTag (param);
81 if (sep == null)
82 return null;
83 return Convert.FromBase64String (sep.Text);
86 public override void FromXmlString (string xmlString)
88 if (xmlString == null)
89 throw new ArgumentNullException ("xmlString");
91 DSAParameters dsaParams = new DSAParameters ();
92 try {
93 SecurityParser sp = new SecurityParser ();
94 sp.LoadXml (xmlString);
95 SecurityElement se = sp.ToXml ();
96 if (se.Tag != "DSAKeyValue")
97 throw new Exception ();
98 dsaParams.P = GetNamedParam (se, "P");
99 dsaParams.Q = GetNamedParam (se, "Q");
100 dsaParams.G = GetNamedParam (se, "G");
101 dsaParams.J = GetNamedParam (se, "J");
102 dsaParams.Y = GetNamedParam (se, "Y");
103 dsaParams.X = GetNamedParam (se, "X");
104 dsaParams.Seed = GetNamedParam (se, "Seed");
105 byte[] counter = GetNamedParam (se, "PgenCounter");
106 if (counter != null) {
107 byte[] counter4b = new byte [4]; // always 4 bytes
108 Buffer.BlockCopy (counter, 0, counter4b, 0, counter.Length);
109 dsaParams.Counter = BitConverterLE.ToInt32 (counter4b, 0);
111 ImportParameters (dsaParams);
113 catch {
114 ZeroizePrivateKey (dsaParams);
115 throw;
117 finally {
118 ZeroizePrivateKey (dsaParams);
122 public abstract void ImportParameters (DSAParameters parameters);
124 // note: using SecurityElement.ToXml wouldn't generate the same string as the MS implementation
125 public override string ToXmlString (bool includePrivateParameters)
127 StringBuilder sb = new StringBuilder ();
128 DSAParameters dsaParams = ExportParameters (includePrivateParameters);
129 try {
130 sb.Append ("<DSAKeyValue>");
132 sb.Append ("<P>");
133 sb.Append (Convert.ToBase64String (dsaParams.P));
134 sb.Append ("</P>");
136 sb.Append ("<Q>");
137 sb.Append (Convert.ToBase64String (dsaParams.Q));
138 sb.Append ("</Q>");
140 sb.Append ("<G>");
141 sb.Append (Convert.ToBase64String (dsaParams.G));
142 sb.Append ("</G>");
144 sb.Append ("<Y>");
145 sb.Append (Convert.ToBase64String (dsaParams.Y));
146 sb.Append( "</Y>");
148 sb.Append ("<J>");
149 sb.Append (Convert.ToBase64String (dsaParams.J));
150 sb.Append ("</J>");
152 if (dsaParams.Seed != null) {
153 sb.Append ("<Seed>");
154 sb.Append (Convert.ToBase64String (dsaParams.Seed));
155 sb.Append ("</Seed>");
157 sb.Append ("<PgenCounter>");
158 // the number of bytes is important (no matter == 0x00)
159 byte[] inArr = BitConverterLE.GetBytes (dsaParams.Counter);
160 int l = inArr.Length;
161 while (inArr[l-1] == 0x00)
162 l--;
163 byte[] c = new byte [l];
164 Buffer.BlockCopy (inArr, 0, c, 0, l);
165 sb.Append (Convert.ToBase64String (c));
166 sb.Append ("</PgenCounter>");
169 if (dsaParams.X != null) {
170 sb.Append ("<X>");
171 sb.Append (Convert.ToBase64String (dsaParams.X));
172 sb.Append ("</X>");
174 else if (includePrivateParameters) {
175 throw new CryptographicException (
176 Locale.GetText ("Missing private key informations"));
179 sb.Append ("</DSAKeyValue>");
181 catch {
182 ZeroizePrivateKey (dsaParams);
183 throw;
186 return sb.ToString ();
189 public abstract bool VerifySignature (byte[] rgbHash, byte[] rgbSignature);