2 // System.Security.Cryptography.DSA.cs class implementation
5 // Thomas Neidhart (tome@sbox.tugraz.at)
6 // Sebastien Pouliot (sebastien@ximian.com)
8 // Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
9 // (C) 2004 Novell (http://www.novell.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Globalization;
43 // a. FIPS PUB 186-2: Digital Signature Standard (DSS)
44 // http://csrc.nist.gov/publications/fips/fips186-2/fips186-2-change1.pdf
46 namespace System.Security.Cryptography {
48 public abstract class DSA : AsymmetricAlgorithm {
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
57 public static new DSA Create ()
59 return Create ("System.Security.Cryptography.DSA");
62 public static new DSA Create (string algName)
64 return (DSA) CryptoConfig.CreateFromName (algName);
67 public abstract byte[] CreateSignature (byte[] rgbHash);
69 public abstract DSAParameters ExportParameters (bool includePrivateParameters);
71 internal void ZeroizePrivateKey (DSAParameters parameters)
73 if (parameters.X != null)
74 Array.Clear (parameters.X, 0, parameters.X.Length);
77 private byte[] GetNamedParam (SecurityElement se, string param)
79 SecurityElement sep = se.SearchForChildByTag (param);
82 return Convert.FromBase64String (sep.Text);
85 public override void FromXmlString (string xmlString)
87 if (xmlString == null)
88 throw new ArgumentNullException ("xmlString");
90 DSAParameters dsaParams = new DSAParameters ();
92 SecurityParser sp = new SecurityParser ();
93 sp.LoadXml (xmlString);
94 SecurityElement se = sp.ToXml ();
95 if (se.Tag != "DSAKeyValue")
96 throw new Exception ();
97 dsaParams.P = GetNamedParam (se, "P");
98 dsaParams.Q = GetNamedParam (se, "Q");
99 dsaParams.G = GetNamedParam (se, "G");
100 dsaParams.J = GetNamedParam (se, "J");
101 dsaParams.Y = GetNamedParam (se, "Y");
102 dsaParams.X = GetNamedParam (se, "X");
103 dsaParams.Seed = GetNamedParam (se, "Seed");
104 byte[] counter = GetNamedParam (se, "PgenCounter");
105 if (counter != null) {
106 byte[] counter4b = new byte [4]; // always 4 bytes
107 Buffer.BlockCopy (counter, 0, counter4b, 0, counter.Length);
108 dsaParams.Counter = BitConverterLE.ToInt32 (counter4b, 0);
110 ImportParameters (dsaParams);
113 ZeroizePrivateKey (dsaParams);
117 ZeroizePrivateKey (dsaParams);
121 public abstract void ImportParameters (DSAParameters parameters);
123 // note: using SecurityElement.ToXml wouldn't generate the same string as the MS implementation
124 public override string ToXmlString (bool includePrivateParameters)
126 StringBuilder sb = new StringBuilder ();
127 DSAParameters dsaParams = ExportParameters (includePrivateParameters);
129 sb.Append ("<DSAKeyValue>");
132 sb.Append (Convert.ToBase64String (dsaParams.P));
136 sb.Append (Convert.ToBase64String (dsaParams.Q));
140 sb.Append (Convert.ToBase64String (dsaParams.G));
144 sb.Append (Convert.ToBase64String (dsaParams.Y));
148 sb.Append (Convert.ToBase64String (dsaParams.J));
151 if (dsaParams.Seed != null) {
152 sb.Append ("<Seed>");
153 sb.Append (Convert.ToBase64String (dsaParams.Seed));
154 sb.Append ("</Seed>");
156 sb.Append ("<PgenCounter>");
157 // the number of bytes is important (no matter == 0x00)
158 byte[] inArr = BitConverterLE.GetBytes (dsaParams.Counter);
159 int l = inArr.Length;
160 while (inArr[l-1] == 0x00)
162 byte[] c = new byte [l];
163 Buffer.BlockCopy (inArr, 0, c, 0, l);
164 sb.Append (Convert.ToBase64String (c));
165 sb.Append ("</PgenCounter>");
168 if (dsaParams.X != null) {
170 sb.Append (Convert.ToBase64String (dsaParams.X));
173 else if (includePrivateParameters) {
174 throw new CryptographicException (
175 Locale.GetText ("Missing private key informations"));
178 sb.Append ("</DSAKeyValue>");
181 ZeroizePrivateKey (dsaParams);
185 return sb.ToString ();
188 public abstract bool VerifySignature (byte[] rgbHash, byte[] rgbSignature);