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 // Copyright (C) 2004-2005, 2008 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:
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.
31 using System
.Globalization
;
32 using System
.Runtime
.InteropServices
;
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
{
45 public abstract class DSA
: AsymmetricAlgorithm
{
47 // Constructor visibility fixed in Fx 2.0
52 public static new DSA
Create ()
54 return Create ("System.Security.Cryptography.DSA");
57 public static new DSA
Create (string algName
)
59 return (DSA
) CryptoConfig
.CreateFromName (algName
);
62 public abstract byte[] CreateSignature (byte[] rgbHash
);
64 public abstract DSAParameters
ExportParameters (bool includePrivateParameters
);
66 internal void ZeroizePrivateKey (DSAParameters parameters
)
68 if (parameters
.X
!= null)
69 Array
.Clear (parameters
.X
, 0, parameters
.X
.Length
);
72 public override void FromXmlString (string xmlString
)
74 if (xmlString
== null)
75 throw new ArgumentNullException ("xmlString");
77 DSAParameters dsaParams
= new DSAParameters ();
79 dsaParams
.P
= GetNamedParam (xmlString
, "P");
80 dsaParams
.Q
= GetNamedParam (xmlString
, "Q");
81 dsaParams
.G
= GetNamedParam (xmlString
, "G");
82 dsaParams
.J
= GetNamedParam (xmlString
, "J");
83 dsaParams
.Y
= GetNamedParam (xmlString
, "Y");
84 dsaParams
.X
= GetNamedParam (xmlString
, "X");
85 dsaParams
.Seed
= GetNamedParam (xmlString
, "Seed");
86 byte[] counter
= GetNamedParam (xmlString
, "PgenCounter");
87 if (counter
!= null) {
88 byte[] counter4b
= new byte [4]; // always 4 bytes
89 Buffer
.BlockCopy (counter
, 0, counter4b
, 0, counter
.Length
);
90 dsaParams
.Counter
= BitConverterLE
.ToInt32 (counter4b
, 0);
92 ImportParameters (dsaParams
);
95 ZeroizePrivateKey (dsaParams
);
99 ZeroizePrivateKey (dsaParams
);
103 public abstract void ImportParameters (DSAParameters parameters
);
105 // note: using SecurityElement.ToXml wouldn't generate the same string as the MS implementation
106 public override string ToXmlString (bool includePrivateParameters
)
108 StringBuilder sb
= new StringBuilder ();
109 DSAParameters dsaParams
= ExportParameters (includePrivateParameters
);
111 sb
.Append ("<DSAKeyValue>");
114 sb
.Append (Convert
.ToBase64String (dsaParams
.P
));
118 sb
.Append (Convert
.ToBase64String (dsaParams
.Q
));
122 sb
.Append (Convert
.ToBase64String (dsaParams
.G
));
126 sb
.Append (Convert
.ToBase64String (dsaParams
.Y
));
129 if (dsaParams
.J
!= null) {
130 // if J wasn't imported then it's not exported and neither
131 // is part of the XML output
133 sb
.Append (Convert
.ToBase64String (dsaParams
.J
));
137 if (dsaParams
.Seed
!= null) {
138 sb
.Append ("<Seed>");
139 sb
.Append (Convert
.ToBase64String (dsaParams
.Seed
));
140 sb
.Append ("</Seed>");
142 sb
.Append ("<PgenCounter>");
143 // the number of bytes is important (no matter == 0x00)
144 if (dsaParams
.Counter
!= 0) {
145 byte[] inArr
= BitConverterLE
.GetBytes (dsaParams
.Counter
);
146 int l
= inArr
.Length
;
147 while (inArr
[l
-1] == 0x00)
150 sb
.Append (Convert
.ToBase64String (inArr
, 0, l
));
152 sb
.Append ("AA=="); // base64 encoded 0
154 sb
.Append ("</PgenCounter>");
157 if (dsaParams
.X
!= null) {
159 sb
.Append (Convert
.ToBase64String (dsaParams
.X
));
162 else if (includePrivateParameters
) {
163 throw new ArgumentNullException ("X");
166 sb
.Append ("</DSAKeyValue>");
169 ZeroizePrivateKey (dsaParams
);
173 return sb
.ToString ();
176 public abstract bool VerifySignature (byte[] rgbHash
, byte[] rgbSignature
);