2 // DiffieHellmanManagedTest.cs - NUnit Test Cases for DH (PKCS#3)
5 // Pieter Philippaerts (Pieter@mentalis.org)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2003 The Mentalis.org Team (http://www.mentalis.org/)
9 // (C) 2004 Novell (http://www.novell.com)
12 using NUnit
.Framework
;
15 using Mono
.Security
.Cryptography
;
18 namespace MonoTests
.Mono
.Security
.Cryptography
{
21 // a. PKCS #3: Diffie-Hellman Key-Agreement Standard (version 1.4)
22 // ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-3.asc
23 // b. Diffie-Hellman Key Agreement Method
24 // http://www.ietf.org/rfc/rfc2631.txt
27 public class DiffieHellmanManagedTest
: Assertion
{
29 // because most crypto stuff works with byte[] buffers
30 static public void AssertEquals (string msg
, byte[] array1
, byte[] array2
)
32 if ((array1
== null) && (array2
== null))
35 Assertion
.Fail (msg
+ " -> First array is NULL");
37 Assertion
.Fail (msg
+ " -> Second array is NULL");
39 bool a
= (array1
.Length
== array2
.Length
);
41 for (int i
= 0; i
< array1
.Length
; i
++) {
42 if (array1
[i
] != array2
[i
]) {
48 if (array1
.Length
> 0) {
49 msg
+= " -> Expected " + BitConverter
.ToString (array1
, 0);
50 msg
+= " is different than " + BitConverter
.ToString (array2
, 0);
52 Assertion
.Assert (msg
, a
);
56 public void KeyExchange ()
58 // create a new DH instance
59 DiffieHellman dh1
= new DiffieHellmanManaged ();
60 // export the public parameters of the first DH instance
61 DHParameters dhp
= dh1
.ExportParameters (false);
62 // create a second DH instance and initialize it with the public parameters of the first instance
63 DiffieHellman dh2
= new DiffieHellmanManaged (dhp
.P
, dhp
.G
, 160);
64 // generate the public key of the first DH instance
65 byte[] ke1
= dh1
.CreateKeyExchange ();
66 // generate the public key of the second DH instance
67 byte[] ke2
= dh2
.CreateKeyExchange ();
68 // let the first DH instance compute the shared secret using the second DH public key
69 byte[] dh1k
= dh1
.DecryptKeyExchange (ke2
);
70 // let the second DH instance compute the shared secret using the first DH public key
71 byte[] dh2k
= dh2
.DecryptKeyExchange (ke1
);
72 // both shared secrets are the same
73 AssertEquals ("Shared Secret", dh1k
, dh2k
);
76 // TODO: More is needed !