(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / Mono.Security / Test / Mono.Security.Cryptography / DiffieHellmanManagedTest.cs
blob2a38eb819dc21dca2764a0f76d80c7ff007a84a0
1 //
2 // DiffieHellmanManagedTest.cs - NUnit Test Cases for DH (PKCS#3)
3 //
4 // Authors:
5 // Pieter Philippaerts (Pieter@mentalis.org)
6 // Sebastien Pouliot <sebastien@ximian.com>
7 //
8 // (C) 2003 The Mentalis.org Team (http://www.mentalis.org/)
9 // (C) 2004 Novell (http://www.novell.com)
12 using NUnit.Framework;
13 using System;
14 using System.IO;
15 using Mono.Security.Cryptography;
16 using System.Text;
18 namespace MonoTests.Mono.Security.Cryptography {
20 // References:
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
26 [TestFixture]
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))
33 return;
34 if (array1 == null)
35 Assertion.Fail (msg + " -> First array is NULL");
36 if (array2 == null)
37 Assertion.Fail (msg + " -> Second array is NULL");
39 bool a = (array1.Length == array2.Length);
40 if (a) {
41 for (int i = 0; i < array1.Length; i++) {
42 if (array1 [i] != array2 [i]) {
43 a = false;
44 break;
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);
55 [Test]
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 !