Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,28 @@
//
// MonoTests.Mono.Math.BigIntegerSetTest.cs
//
// Authors:
// Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//
using System;
using Mono.Math;
using NUnit.Framework;
namespace MonoTests.Mono.Math {
[TestFixture]
public abstract class BigIntegerTestSet {
protected string Name {
get { return this.GetType ().Name; }
}
protected void Expect (BigInteger actual, BigInteger expected)
{
Assertion.AssertEquals (Name, expected, actual);
}
}
}

View File

@@ -0,0 +1,132 @@
//
// BigIntegerTest.cs - NUnit Test Cases for BigInteger
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004, 2007 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using myalias = System;
using Mono.Math;
namespace MonoTests.Mono.Math {
[TestFixture]
public class BigIntegerTest {
[Test]
public void DefaultBitCount ()
{
BigInteger bi = new BigInteger ();
Assert.AreEqual (0, bi.BitCount (), "default BitCount");
// note: not bit are set so BitCount is zero
}
[Test]
public void DefaultRandom ()
{
// based on bugzilla entry #68452
BigInteger bi = new BigInteger ();
Assert.AreEqual (0, bi.BitCount (), "before randomize");
bi.Randomize ();
// Randomize returns a random number of BitCount length
// so in this case it will ALWAYS return 0
Assert.AreEqual (0, bi.BitCount (), "after randomize");
Assert.AreEqual (new BigInteger (0), bi, "Zero");
}
[Test]
public void ModPow_0_Even ()
{
BigInteger x = new BigInteger (1);
BigInteger y = new BigInteger (0);
BigInteger z = x.ModPow (y, 1024);
Assert.AreEqual ("1", z.ToString (), "1 pow 0 == 1");
}
[Test]
public void ModPow_Big_Even ()
{
// http://gcc.gnu.org/ml/java/2001-01/msg00150.html
BigInteger x = BigInteger.Parse ("222556259477882361118129720038750144464896096345697329917462180806109470940281821580712930114298080816996240075704780895407778416354633927929850543336844729388676722554712356733107888579404671103423966348754128720372408391573576775380281687780687492527566938517625657849775850241884119610654472761291507970934");
BigInteger y = BigInteger.Parse ("110319153937683287453746757581772092163629769182044007837690319614087550020383807943886070460712008994638849038231331120616035703719955147238394349941968802357224177878230564379014395900786093465543114548034361805469457605783731382574787980771957640613447628351175959168798011343064123908688343944150028709336");
BigInteger z = BigInteger.Parse ("211455809992703561445401788842734346323873054957006050135582190157359001703882707072169880651159563587522668850959539052488297197610540840476872693108381476249027986010074543599432542677282684917897250864056294311624311681558854158430574409491081490219256907243905496547813878640883064959346343865887971384185");
BigInteger a = z.ModPow (x, y);
Assert.AreEqual ("89040229313686098274750802637193802904787850353791629688385431482589769348345172944539658366893587456857347312314974124445695423885005533414559099801699612294235861570065774222911180890417009385455826560773741520297884850460324781620974467560905975577765401911117379967692495136423710471201230243826129276993", a.ToString ());
}
[Test]
public void ModPow_2 ()
{
// #70169
BigInteger b = new BigInteger (10);
BigInteger m = new BigInteger (32);
// after 40 we start loosing double precision and result will differ
for (int i=1; i < 40; i++) {
BigInteger e = new BigInteger (i);
BigInteger r = e.ModPow (b, m);
long expected = (long) myalias.Math.Pow (i, 10) % 32;
Assert.AreEqual (expected.ToString (), r.ToString (), i.ToString ());
}
}
[Test]
public void ModPow_3 ()
{
BigInteger b = new BigInteger (2);
BigInteger m = new BigInteger (myalias.Int32.MaxValue);
// after 62 we start loosing double precision and result will differ
for (int i = 1; i < 62; i++) {
long expected = (long) myalias.Math.Pow (2, i) % myalias.Int32.MaxValue;
BigInteger e = new BigInteger (i);
BigInteger r = b.ModPow (e, m);
Assert.AreEqual (expected.ToString (), r.ToString (), i.ToString ());
}
}
[Test]
public void Bug81857 ()
{
BigInteger b = BigInteger.Parse ("18446744073709551616");
BigInteger exp = new BigInteger (2);
BigInteger mod = BigInteger.Parse ("48112959837082048697");
BigInteger expected = BigInteger.Parse ("4970597831480284165");
BigInteger manual = b * b % mod;
Assert.AreEqual (expected, manual, "b * b % mod");
// fails (inside Barrett reduction)
// BigInteger actual = b.ModPow (exp, mod);
// Assert.AreEqual (expected, actual, "b.ModPow (exp, mod)");
}
[Test]
public void IsProbablePrime_Small ()
{
// last of the small prime tables
Assert.IsTrue (new BigInteger (5987).IsProbablePrime (), "5987");
// small value with exponent == 1
Assert.IsTrue (new BigInteger (65537).IsProbablePrime (), "65537");
}
}
}

View File

@@ -0,0 +1,159 @@
//
// MonoTests.Mono.Math.BitwiseTest.cs
//
// Authors:
// Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//
using System;
using Mono.Math;
using NUnit.Framework;
namespace MonoTests.Mono.Math {
public abstract class Bitwise_Base : BigIntegerTestSet {
BigInteger N, expectedNls, expectedNrs;
int shiftAmount;
public Bitwise_Base()
{
N = new BigInteger(n);
expectedNls = new BigInteger(ExpectedNLeftShift);
expectedNrs = new BigInteger(ExpectedNRightShift);
shiftAmount = ShiftAmount;
}
public abstract uint[] n {
get;
}
public abstract int ShiftAmount {
get;
}
public abstract uint[] ExpectedNLeftShift {
get;
}
public abstract uint[] ExpectedNRightShift {
get;
}
[Test]
public void ShiftLeft()
{
Expect( N << shiftAmount, expectedNls );
}
[Test]
public void ShiftRight()
{
Expect( N >> shiftAmount, expectedNrs );
}
}
public class Bitwise_Rand512n : Bitwise_Base {
public override uint[] n {
get {
return new uint[] {
0xb29696b0, 0x8a8674b9, 0xabf0f8b7, 0xb873579f, 0x88d90993, 0x697c9290,
0xbb70f64c, 0x77f4c616, 0x60bab197, 0x39884b0d, 0x37e83131, 0x8de30d79,
0xae47b2e2, 0x67718497, 0x23559359, 0xc4c9259c
};
}
}
public override int ShiftAmount {
get {
return 12;
}
}
public override uint[] ExpectedNLeftShift {
get {
return new uint[] {
0xb29, 0x696b08a8, 0x674b9abf, 0x0f8b7b87, 0x3579f88d, 0x90993697,
0xc9290bb7, 0x0f64c77f, 0x4c61660b, 0xab197398, 0x84b0d37e, 0x831318de,
0x30d79ae4, 0x7b2e2677, 0x18497235, 0x59359c4c, 0x9259c000
};
}
}
public override uint[] ExpectedNRightShift {
get {
return new uint[] {
0xb2969, 0x6b08a867, 0x4b9abf0f, 0x8b7b8735, 0x79f88d90, 0x993697c9,
0x290bb70f, 0x64c77f4c, 0x61660bab, 0x19739884, 0xb0d37e83, 0x1318de30,
0xd79ae47b, 0x2e267718, 0x49723559, 0x359c4c92
};
}
}
}
public class Bitwise_Rand2048n : Bitwise_Base {
public override uint[] n {
get {
return new uint[] {
0x27dd8385, 0xf44cf2f6, 0x2fba638e, 0x23151ade, 0xc4366886, 0x86d5ea9e,
0xd8c92f09, 0x326fd166, 0xc466ab65, 0xe163db79, 0xcc6eb808, 0xce262b4c,
0x7317b66b, 0x360c9746, 0x0464d7c2, 0x37709a34, 0x2ee973fc, 0x04c90896,
0x60790e40, 0xff5247f8, 0x864d5f5b, 0xcf8e8567, 0x59782b43, 0x27cfbb82,
0x0dc53a89, 0x8072e526, 0xcf44d0d1, 0x4eeac325, 0xcb1c91aa, 0x8d09c910,
0xe13ab08e, 0x656235ef, 0xb553255c, 0x8709557a, 0x7f809100, 0xf47c7acf,
0x9c70a17f, 0x8c1b2aa3, 0x1d6f0cec, 0x9c1ca046, 0xaac3c552, 0xd04318fc,
0x5c217295, 0x8deb3c73, 0xaa039583, 0x72ccfbc6, 0x91783ee6, 0xe3257194,
0x70e54a12, 0x0b29e5b9, 0x0b4c883c, 0xaf2fe460, 0x21f86d3b, 0x35ce8fc7,
0x9f336fc8, 0x3e0e1aa0, 0xed8ec9f1, 0xc10572d9, 0x26fb856e, 0x96f425c2,
0xf73f62f4, 0xa0690bb7, 0x35875324, 0x513e84b7
};
}
}
public override int ShiftAmount {
get {
return 48;
}
}
public override uint[] ExpectedNLeftShift {
get {
return new uint[] {
0x27dd, 0x8385f44c, 0xf2f62fba, 0x638e2315, 0x1adec436, 0x688686d5,
0xea9ed8c9, 0x2f09326f, 0xd166c466, 0xab65e163, 0xdb79cc6e, 0xb808ce26,
0x2b4c7317, 0xb66b360c, 0x97460464, 0xd7c23770, 0x9a342ee9, 0x73fc04c9,
0x08966079, 0x0e40ff52, 0x47f8864d, 0x5f5bcf8e, 0x85675978, 0x2b4327cf,
0xbb820dc5, 0x3a898072, 0xe526cf44, 0xd0d14eea, 0xc325cb1c, 0x91aa8d09,
0xc910e13a, 0xb08e6562, 0x35efb553, 0x255c8709, 0x557a7f80, 0x9100f47c,
0x7acf9c70, 0xa17f8c1b, 0x2aa31d6f, 0x0cec9c1c, 0xa046aac3, 0xc552d043,
0x18fc5c21, 0x72958deb, 0x3c73aa03, 0x958372cc, 0xfbc69178, 0x3ee6e325,
0x719470e5, 0x4a120b29, 0xe5b90b4c, 0x883caf2f, 0xe46021f8, 0x6d3b35ce,
0x8fc79f33, 0x6fc83e0e, 0x1aa0ed8e, 0xc9f1c105, 0x72d926fb, 0x856e96f4,
0x25c2f73f, 0x62f4a069, 0x0bb73587, 0x5324513e, 0x84b70000, 0x00000000
};
}
}
public override uint[] ExpectedNRightShift {
get {
return new uint[] {
0x27dd, 0x8385f44c, 0xf2f62fba, 0x638e2315, 0x1adec436, 0x688686d5,
0xea9ed8c9, 0x2f09326f, 0xd166c466, 0xab65e163, 0xdb79cc6e, 0xb808ce26,
0x2b4c7317, 0xb66b360c, 0x97460464, 0xd7c23770, 0x9a342ee9, 0x73fc04c9,
0x08966079, 0x0e40ff52, 0x47f8864d, 0x5f5bcf8e, 0x85675978, 0x2b4327cf,
0xbb820dc5, 0x3a898072, 0xe526cf44, 0xd0d14eea, 0xc325cb1c, 0x91aa8d09,
0xc910e13a, 0xb08e6562, 0x35efb553, 0x255c8709, 0x557a7f80, 0x9100f47c,
0x7acf9c70, 0xa17f8c1b, 0x2aa31d6f, 0x0cec9c1c, 0xa046aac3, 0xc552d043,
0x18fc5c21, 0x72958deb, 0x3c73aa03, 0x958372cc, 0xfbc69178, 0x3ee6e325,
0x719470e5, 0x4a120b29, 0xe5b90b4c, 0x883caf2f, 0xe46021f8, 0x6d3b35ce,
0x8fc79f33, 0x6fc83e0e, 0x1aa0ed8e, 0xc9f1c105, 0x72d926fb, 0x856e96f4,
0x25c2f73f, 0x62f4a069, 0x0bb73587
};
}
}
}
}

View File

@@ -0,0 +1,60 @@
2007-07-05 Sebastien Pouliot <sebastien@ximian.com>
* PrimeTestingTest.cs: Re-enable failing 20 digits primes (now that
we have a workaround for them).
2007-07-04 Sebastien Pouliot <sebastien@ximian.com>
* BigIntegerTest.cs: A few more ModPow test cases to corner the
small value bug (looks like a bad Barrett reduction).
* PrimeTestingTest.cs: Add test cases for "small" primes between
10 and 300 digits. 3 cases are failing for 20 digits primes.
2007-07-03 Sebastien Pouliot <sebastien@ximian.com>
* BigIntegerTest.cs: Add test cases for small values with
IsProbablePrime. From bug #81857, subset from patch by Kazuki.
2005-01-03 Nick Drochak <ndrochak@gol.com>
* BigIntegerTest.cs: Make tests pass on MS DotNet
2004-12-03 Sebastien Pouliot <sebastien@ximian.com>
* BigIntegerTest.cs: Added tests for ModPow when power is 0, for a
known case that was faling in classpath and when modulo is a power of
two (bug #70169).
2004-10-19 Sebastien Pouliot <sebastien@ximian.com>
* BigIntegerTest.cs: New. General unit tests for BigInteger.
2004-04-22 Sebastien Pouliot <sebastien@ximian.com>
* GcdBigTest.cs: Ajusted to changes in assembly.
* ModInverseBigTest.cs: Ajusted to changes in assembly.
* PrimeGenerationTest.cs: Ajusted to changes in assembly.
* PrimeTestingTest.cs: Ajusted to changes in assembly.
* SearchGeneratorTest.cs: Ajusted to changes in assembly.
2004-02-13 Sebastien Pouliot <sebastien@ximian.com>
* PrimeTestingTest.cs: Added well known (and tested) primes from
RFC 2412 (http://www.faqs.org/rfcs/rfc2412.html) as suggested by
Pieter Philippaerts (#51229).
* SearchGeneratorTest.cs: Modified so this still use the (previous)
SmallPrimeSppTest primality test - because RM doesn't work for
small primes.
2004-02-09 Sebastien Pouliot <sebastien@ximian.com>
* ArithmeticBigTest.cs:
* BigIntegerSetTest.cs:
* BitwiseTest.cs:
* GcdBigTest.cs:
* ModInverseBigTest.cs:
* ModRingTest.cs:
* PrimeGenerationTest.cs:
* PrimeTestingTest.cs:
* SearchGeneratorTest.cs:
New. Copied from corlib (where they cannot be executed because BigInteger is internal).

View File

@@ -0,0 +1,115 @@
//
// MonoTests.Mono.Math.GcdBigTest.cs
//
// Authors:
// Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//
using System;
using Mono.Math;
using NUnit.Framework;
namespace MonoTests.Mono.Math {
public abstract class GcdBig_Base : BigIntegerTestSet {
BigInteger A, B, aGcdB;
public GcdBig_Base()
{
A = new BigInteger(a);
B = new BigInteger(b);
aGcdB = new BigInteger(ExpectedAgcdB);
}
public abstract uint[] a {
get;
}
public abstract uint[] b {
get;
}
public abstract uint[] ExpectedAgcdB {
get;
}
[Test]
public void GcdPP()
{
Expect (A.GCD (B), aGcdB);
}
}
public class GcdBig_Rand2048a512b : GcdBig_Base {
public override uint[] a {
get {
return new uint[] {
0xaae18fa1, 0x58e1e9fc, 0x836350a0, 0x23d2a12d, 0x1aec1bdc, 0xad4a3b30,
0x40dc1d27, 0x625277fb, 0xddfbee25, 0xc1820dac, 0x4418603a, 0x5aec122c,
0x58b70181, 0x129d6b33, 0x6c4ed37e, 0x70808dd0, 0xed55b079, 0x706f15f3,
0x1a84b3ac, 0x088f1679, 0xcbf2be66, 0xb97a885e, 0xa2c95b95, 0xd44ebb83,
0x69351a38, 0x21d3cdb2, 0x30844c5c, 0x5abf8d7a, 0xb663c3de, 0x3ce2fcc5,
0x80b42a05, 0xa0a8aca3, 0x31d42948, 0x469d8ad5, 0xe0f66f7e, 0x3250dac1,
0xb4450067, 0xb247ad34, 0xbfd74c70, 0xfd1e29fa, 0x4050dc77, 0x827763b9,
0xba41410d, 0x42494b62, 0x99ef13a1, 0x55c957ec, 0x1e7dd0fc, 0x6c4cccdf,
0x981128e0, 0xc7c01688, 0x4ae5116c, 0x9b24d3c8, 0x623af290, 0x31ac1c1e,
0x0891f2f1, 0x678ab7ee, 0x6169af02, 0x2479511c, 0x7cea2114, 0xf13d541c,
0x0f54f253, 0xf5dd2553, 0xc0ea9613, 0x84a0c7b2
};
}
}
public override uint[] b {
get {
return new uint[] {
0x83ab64ef, 0x1350b335, 0xbf5f150d, 0x399a2487, 0xc1b76d63, 0xdf3e59ad,
0x2ed3b4fa, 0x9a6ad972, 0xb6e791c5, 0xdd7a8664, 0x802f8364, 0xf1a8617f,
0xb036a74a, 0x452ac130, 0x727e194f, 0x3dd8cfe8
};
}
}
public override uint[] ExpectedAgcdB {
get {
return new uint[] {
0x00000006
};
}
}
}
public class GcdBig_Rand512a512b : GcdBig_Base {
public override uint[] a {
get {
return new uint[] {
0x7bc1fb0e, 0x0335547e, 0x0e85b746, 0x1e7554d2, 0x77515958, 0xadf072c8,
0xdb03eb22, 0xdcedf7e2, 0x8f923f6a, 0xf124052d, 0x55d623f4, 0x29083f62,
0x66eaa78e, 0xfe819e63, 0xf8229bde, 0xe155c05b
};
}
}
public override uint[] b {
get {
return new uint[] {
0xe5320f2e, 0x379803d7, 0x24363d84, 0xfd61c43c, 0xa4e6ca6f, 0x16628f59,
0xddeb6557, 0xd904639c, 0x55f59c2f, 0x6ba21e54, 0x81107aa9, 0x47a4653c,
0xcc0a5889, 0xb6abcaec, 0xf6b58d60, 0xf8cc7eeb
};
}
}
public override uint[] ExpectedAgcdB {
get {
return new uint[] {
0x00000001
};
}
}
}
}

View File

@@ -0,0 +1,224 @@
//
// MonoTests.Mono.Math.ModInverseBigTest.cs
//
// Authors:
// Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//
using System;
using Mono.Math;
using NUnit.Framework;
namespace MonoTests.Mono.Math {
public abstract class ModInverseBig_Base : BigIntegerTestSet {
BigInteger A, B, AmodinvB;
public ModInverseBig_Base ()
{
A = new BigInteger (a);
B = new BigInteger (b);
AmodinvB = new BigInteger (ExpectedAmodinvB);
}
public abstract uint[] a {
get;
}
public abstract uint[] b {
get;
}
public abstract uint[] ExpectedAmodinvB {
get;
}
[Test]
public void ModInvPP ()
{
Expect (A.ModInverse (B), AmodinvB);
}
}
public class ModInverseBig_Rand512a1024b : ModInverseBig_Base {
public override uint[] a {
get {
return new uint[] {
0x48fd8f2e, 0xa791b900, 0x19e53aaa, 0x6d45758a, 0xeb8be610, 0x25c42285,
0xabff3066, 0xcdcb9969, 0xa08fd7c1, 0x1c382419, 0xcd6b685b, 0x23e8bcaf,
0x592a0f82, 0x2b54b60b, 0xbb67f3c2, 0x64313461
};
}
}
public override uint[] b {
get {
return new uint[] {
0x3617f5e4, 0xd32a40dc, 0x358f7c09, 0x976b345b, 0x4dc05c63, 0x91ed990d,
0xfc66b0a6, 0x0a36dd7e, 0x7a5ea721, 0xf28b577e, 0xf014cbf6, 0x597b4094,
0x177f8253, 0x6587a352, 0x67fedcf0, 0x61ee389e, 0xff86cc7d, 0x9817cd3a,
0x632d6730, 0x3082112a, 0x48509e74, 0x198c7802, 0x69d8c4c6, 0x727313e9,
0x1e11ae51, 0xa72a33bb, 0xc9059cc7, 0xc9fc6268, 0x34ed466b, 0x11e49879,
0x8eb7ebf5, 0x98b53108
};
}
}
public override uint[] ExpectedAmodinvB {
get {
return new uint[] {
0x045fbad9, 0x61867c14, 0xb30ff1f7, 0x5deaf4c1, 0xd19e0b62, 0xc4ed73af,
0x9501dbd4, 0x0b052e1c, 0xd3e944c3, 0xeddc333b, 0x1444c1f9, 0x38ca61b7,
0xceec8a0d, 0xec8f2814, 0xa2099df4, 0x2a0ddbd0, 0x9193985d, 0x09f89197,
0xb58e7229, 0x45c1f891, 0x93553056, 0x462dbe6a, 0xb70c95d0, 0x7cf80ae9,
0x7833e1bf, 0x88329c50, 0xdbde3ef8, 0x7a426200, 0x4335234a, 0x2556ba2c,
0x94cc2109, 0x046645c1
};
}
}
}
public class ModInverseBig_Rand256a1024b : ModInverseBig_Base {
public override uint[] a {
get {
return new uint[] {
0xcc2e79fa, 0x6901026e, 0xc4fdb0d4, 0xb4173ce7, 0xf7f96af1, 0x8339780b,
0x22268620, 0x382c75a4
};
}
}
public override uint[] b {
get {
return new uint[] {
0x4b23ac44, 0xbddfc733, 0x3ea2a85b, 0x02daa1d8, 0x2b31cf00, 0x2503a376,
0xa3d47c77, 0x829266d7, 0x1e29fad8, 0x4f0e3788, 0xc9c128d7, 0x13ea53eb,
0x85fc86b1, 0xbad2c0c4, 0xd87dcdad, 0x0a09ba8c, 0x126a0ede, 0x1fe390c7,
0x12c5a679, 0xf23557e1, 0x8e7b1934, 0xc102f83b, 0x934de6d6, 0x254aee03,
0x34f02315, 0x33edbb07, 0x97a87ecf, 0xbf534337, 0xe347ae90, 0xf2eb1176,
0x5459c63b, 0x8f3b0f75
};
}
}
public override uint[] ExpectedAmodinvB {
get {
return new uint[] {
0x12d80123, 0x9b288afc, 0x466ed241, 0x8bf1804d, 0x73cd667f, 0x8eb1eb34,
0x513df007, 0x464c7245, 0xf3b97899, 0xd5cb92c7, 0xdefdb611, 0x5258e545,
0xe8b66c76, 0xd11c58e3, 0xab1fc29a, 0x9718099e, 0xa4040d4e, 0x29980874,
0xda2b4e0d, 0xfed020de, 0x6bde01e6, 0x15b084af, 0x9657aa64, 0x760c64f0,
0x6bba8099, 0xef1a409e, 0xf80b1ec7, 0x4a69256b, 0xf867ec36, 0xd3659a2a,
0xf23ec3a5, 0x04349da9
};
}
}
}
public class ModInverseBig_Rand256a512b : ModInverseBig_Base {
public override uint[] a {
get {
return new uint[] {
0x2e9342c8, 0x1ac6b23d, 0xeb18d3f9, 0x2b076025, 0x030232ee, 0xd1cb7f22,
0xfbfe74df, 0xabadc589
};
}
}
public override uint[] b {
get {
return new uint[] {
0x7e0a43ca, 0xf05d9c52, 0x28e68cf6, 0xf168b591, 0x88c17e79, 0xcb075c3b,
0x92a16680, 0xd7dccd53, 0xe6da1248, 0xe71811b7, 0x4d0a3c42, 0x1ebb46cc,
0x71d4dd69, 0x07a642d9, 0x8eae29d0, 0xcbd278b4
};
}
}
public override uint[] ExpectedAmodinvB {
get {
return new uint[] {
0x77dc2534, 0xf81a1bc7, 0xfbd6b350, 0x809b2c31, 0x3e04ad9f, 0x5101b59f,
0xcee28213, 0x726356fe, 0xeb7d0a6b, 0x01ed7bd7, 0x27ff2f04, 0xa3cbd6a4,
0xcd6a849d, 0x029c9a79, 0xb82d5da2, 0x87af9e81
};
}
}
}
public class ModInverseBig_Rand1024a5b : ModInverseBig_Base {
public override uint[] a {
get {
return new uint[] {
0x60cc0502, 0xebe19e1b, 0xfc64fd47, 0xfc34fbac, 0x81b3b346, 0x57e9ebf8,
0x96501b67, 0xc95eb1cc, 0x2e126045, 0xa56ec13b, 0x2f812165, 0xb4391e46,
0xb245069a, 0xfeb836b6, 0xebeceb62, 0xedd9f9bc, 0x9bdd63ba, 0xac491f92,
0xb8ab6898, 0x8a5ea88d, 0xf7f24993, 0x75e86618, 0x4e939376, 0x7a2ac365,
0xb270f14c, 0x416fb9bc, 0x77af8352, 0x488e1a5f, 0xe22e8cda, 0xcaa72806,
0xf649f663, 0xefee082d
};
}
}
public override uint[] b {
get {
return new uint[] {
0x11
};
}
}
public override uint[] ExpectedAmodinvB {
get {
return new uint[] {
0x2
};
}
}
}
public class ModInverseBig_Rand3a1024b : ModInverseBig_Base {
public override uint[] a {
get {
return new uint[] {
0x5
};
}
}
public override uint[] b {
get {
return new uint[] {
0x3919ec8a, 0x8c713779, 0xb87d2db0, 0x7922df91, 0x34bf77e1, 0x49c08156,
0x9ffb8ed5, 0x522a42e9, 0xdf18b1ff, 0x1cfdc432, 0x8564555b, 0x2b800684,
0xa46bad82, 0x175a04ea, 0x87e4d513, 0xfb956ebc, 0xb74745e6, 0x85f45bf5,
0xe580bb11, 0x290bfc35, 0x8d8782d8, 0x1054ea4e, 0x93eb86bb, 0xe7ea2e42,
0x762c2945, 0x23e59e46, 0x833fe0b2, 0x3797025f, 0xabc64408, 0x94d0c8ac,
0x2a31e00b, 0xd4d28ab0
};
}
}
public override uint[] ExpectedAmodinvB {
get {
return new uint[] {
0x2dae56d5, 0x3d275f94, 0x939757c0, 0x60e8b2da, 0x90992cb4, 0x3b006778,
0x7ffc7244, 0x41bb68bb, 0x18e08e65, 0xb0cb035b, 0x9de9dde2, 0x8933386a,
0x1d22f134, 0xdf7b3722, 0x0650aa76, 0x62ddf230, 0x929f6b1e, 0xd1904991,
0x8466fc0d, 0xba6ffcf7, 0xa46c68ac, 0xd9dd883e, 0xdcbc6bc9, 0x8654f1ce,
0xc4f02104, 0x1cb7b1d2, 0x0299808e, 0x92df3519, 0x5638366d, 0x43da3a23,
0x54f4b33c, 0xaa42088d
};
}
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,93 @@
//
// MonoTests.Mono.Math.PrimeGenerationTest.cs
//
// Authors:
// Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//
using System;
using Mono.Math;
using NUnit.Framework;
namespace MonoTests.Mono.Math {
public abstract class PrimeGeneration_Base : BigIntegerTestSet {
BigInteger s, e;
public PrimeGeneration_Base ()
{
s = new BigInteger (Start);
e = new BigInteger (Expected);
}
public abstract uint[] Start {
get;
}
public abstract uint[] Expected {
get;
}
[Test]
public void GeneratePrime ()
{
BigInteger r = BigInteger.NextHighestPrime (s);
Expect (r, e);
}
}
public class PrimeGeneration_Rand1024 : PrimeGeneration_Base {
public override uint[] Start {
get {
return new uint[] {
0x7eaceb59, 0x344f3bcd, 0xffc5c003, 0xe17e9912, 0x05653294, 0x5383551f,
0x0f604c65, 0x308f77ec, 0xf86d18d8, 0xad262661, 0x35f021eb, 0x36d3c53c,
0x5e1ec5a9, 0x1c3dd738, 0x67b49061, 0x294a5a65, 0x0754a346, 0xe9ee0bf2,
0x181c5e1d, 0x1eb719fa, 0xc4372255, 0x0bdee9bb, 0xbf9cfe74, 0x1b2be7f5,
0xcf81e022, 0xa43d2698, 0x9c59f568, 0xc45d024d, 0x62eb1a3f, 0x125ad0cf,
0x11e6834e, 0x09745d93
};
}
}
public override uint[] Expected {
get {
return new uint[] {
0x7eaceb59, 0x344f3bcd, 0xffc5c003, 0xe17e9912, 0x05653294, 0x5383551f,
0x0f604c65, 0x308f77ec, 0xf86d18d8, 0xad262661, 0x35f021eb, 0x36d3c53c,
0x5e1ec5a9, 0x1c3dd738, 0x67b49061, 0x294a5a65, 0x0754a346, 0xe9ee0bf2,
0x181c5e1d, 0x1eb719fa, 0xc4372255, 0x0bdee9bb, 0xbf9cfe74, 0x1b2be7f5,
0xcf81e022, 0xa43d2698, 0x9c59f568, 0xc45d024d, 0x62eb1a3f, 0x125ad0cf,
0x11e6834e, 0x09745fe9
};
}
}
}
public class PrimeGeneration_512 : PrimeGeneration_Base {
public override uint[] Start {
get {
return new uint[] {
0x5629a00f, 0x3b672419, 0x85891da8, 0x2d63ff0c, 0xd8b91375, 0xfb57f659,
0x07e2de17, 0x7de561be, 0xc29d6912, 0x198cb7fd, 0x251d33ad, 0x6bc20a0a,
0xdd1e4060, 0x809d5fb2, 0x20fcd816, 0xafc2ddb2
};
}
}
public override uint[] Expected {
get {
return new uint[] {
0x5629a00f, 0x3b672419, 0x85891da8, 0x2d63ff0c, 0xd8b91375, 0xfb57f659,
0x07e2de17, 0x7de561be, 0xc29d6912, 0x198cb7fd, 0x251d33ad, 0x6bc20a0a,
0xdd1e4060, 0x809d5fb2, 0x20fcd816, 0xafc2dfd5
};
}
}
}
}

View File

@@ -0,0 +1,423 @@
//
// MonoTests.Mono.Math.PrimeTestingTest.cs
//
// Authors:
// Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//
using System;
using Mono.Math;
using Mono.Math.Prime;
using NUnit.Framework;
namespace MonoTests.Mono.Math {
public abstract class PrimeTesting_Base : BigIntegerTestSet {
BigInteger P1, P2, P3;
public PrimeTesting_Base ()
{
P1 = new BigInteger (p1);
P2 = new BigInteger (p2);
P3 = new BigInteger (p3);
}
public abstract uint[] p1 {
get;
}
public abstract uint[] p2 {
get;
}
public abstract uint[] p3 {
get;
}
[Test]
public void p1prime ()
{
ExpectPrime (P1);
}
[Test]
public void p2prime ()
{
ExpectPrime (P2);
}
[Test]
public void p3prime ()
{
ExpectPrime (P3);
}
[Test]
public void p1p2composite ()
{
ExpectComposite (P1 * P2);
}
[Test]
public void p1p3composite ()
{
ExpectComposite (P1 * P3);
}
[Test]
public void p2p3composite ()
{
ExpectComposite (P2 * P3);
}
[Test]
public void p1p2p3composite ()
{
ExpectComposite (P1 * P2 * P3);
}
private void ExpectComposite (BigInteger bi)
{
Assertion.AssertEquals (false, bi.IsProbablePrime ());
}
private void ExpectPrime (BigInteger bi)
{
Assertion.AssertEquals (true, bi.IsProbablePrime ());
}
}
public class PrimeTesting_Rand1024 : PrimeTesting_Base {
public override uint[] p1 {
get {
return new uint[] {
0x48595126, 0xd1e0c772, 0x87f352a7, 0xeb3c496c, 0xce17d7ff, 0xce260883,
0x4892835e, 0x4457170e, 0xb90a0893, 0x2a1bfd80, 0x56665a9c, 0x36b06f35,
0x61988d45, 0xa04e18c2, 0xa2308414, 0xa0be5e2c, 0x423fad73, 0x7117b883,
0x3977c11c, 0xf34c2c20, 0x045713c9, 0x0c82ea36, 0x3811b550, 0x7b03aafb,
0xbc31f3c4, 0x8667b5a5, 0x3a5697f7, 0x064169e8, 0xd70dbae4, 0x9bb2a4f8,
0xba6a1c1c, 0x7c6db863
};
}
}
public override uint[] p2 {
get {
return new uint[] {
0x884462b0, 0x8295cefd, 0x444cbcb7, 0xd3916039, 0x45b1e26d, 0x02b3d8d5,
0x3547b6ee, 0x0791ef10, 0x6da42d3e, 0xee537c9f, 0x339ee744, 0x97d328c7,
0xebc9055a, 0xf3e1835c, 0xd9cff3db, 0xfe5f33d8, 0x45234644, 0x4af5031b,
0x27f41403, 0x1d9d751b, 0xb711ddc7, 0xb331784f, 0x992b4148, 0x50a8ac7d,
0x5c3f1fbb, 0x209d76e3, 0xfbd05088, 0xacf87776, 0xad214d60, 0x1f2ab42d,
0xe9bc81fc, 0xe997d55b
};
}
}
public override uint[] p3 {
get {
return new uint[] {
0xf732ee, 0x019ec52e, 0xfc360881, 0x4fd07211, 0x77d44ed0, 0xc27a4b3d,
0xde2a9500, 0x2d4a2a70, 0x834e5d32, 0x715f5884, 0xc5922ca1, 0x94d48b60,
0xb0262fce, 0x72040eb9, 0x5a4fd41c, 0x4e095cba, 0x3a840a36, 0x0175b3b4,
0x64363623, 0xc03bd892, 0x39231a04, 0x521eee6c, 0x560e7c10, 0xa8476256,
0xeefc3f37, 0xadd4c5ee, 0xf8407afc, 0x30e9c52c, 0x026849d3, 0x040533df,
0xc286e00b, 0x9c377705
};
}
}
}
public class PrimeTesting_Rand512 : PrimeTesting_Base {
public override uint[] p1 {
get {
return new uint[] {
0x99d95780, 0xd02a33bb, 0x980c079b, 0xbc43c3c2, 0xca501ce0, 0x3fc4bd85,
0x51035dcc, 0x11dd4c8e, 0x59696b91, 0xcdc7cbc0, 0x29e5c884, 0xae628e88,
0x908855b7, 0xab6218f3, 0x6abd6fb5, 0x3ca12af7
};
}
}
public override uint[] p2 {
get {
return new uint[] {
0xc77a6a36, 0xfe547705, 0x98a57094, 0xc0dd1e8b, 0x78b62bc9, 0x19aea0da,
0xb91b141b, 0xe4d34402, 0xdd16b9c6, 0x0ec73ea4, 0x8ad59ae5, 0x0d4b0f09,
0x1fd1858d, 0xaac2891c, 0xbd56c29f, 0xb398ffa5
};
}
}
public override uint[] p3 {
get {
return new uint[] {
0xb98e9b3a, 0x197d7671, 0x104d6b15, 0xe8c76058, 0xed9fcb77, 0x65c38af7,
0xdd660b8e, 0x412c5bbb, 0x80b5f777, 0x70c1a458, 0xc9ad52ae, 0x489bae51,
0x795f99a7, 0x2f2cb4ae, 0xc902c3ad, 0x9d96456f
};
}
}
}
public class PrimeTesting_Rand128 : PrimeTesting_Base {
public override uint[] p1 {
get {
return new uint[] {
0x28480536, 0xeaf326bc, 0x2957b03b, 0xa1549e59
};
}
}
public override uint[] p2 {
get {
return new uint[] {
0xd9ce28be, 0x6a279407, 0x8da0afbc, 0xa57eb9b3
};
}
}
public override uint[] p3 {
get {
return new uint[] {
0x1d777a45, 0x957a0fad, 0x25d049a7, 0x4f73383b
};
}
}
}
// Adapted from http://lists.ximian.com/archives/public/mono-devel-list/2003-November/003026.html
// Note: these primes are taken from RFC 2412 [http://www.faqs.org/rfcs/rfc2412.html]
[TestFixture]
public class Rfc2412WellTestedPrimes : Assertion {
// E.1. Well-Known Group 1: A 768 bit prime
[Test]
public void Prime768 ()
{
string prime = "1552518092300708935130918131258481755631334049434514313202351194902966239949102107258669453876591642442910007680288864229150803718918046342632727613031282983744380820890196288509170691316593175367469551763119843371637221007210577919";
BigInteger bi = BigInteger.Parse (prime);
Assert ("isProbablePrime-768", bi.IsProbablePrime ());
AssertEquals ("ToString()", prime, bi.ToString ());
}
// E.2. Well-Known Group 2: A 1024 bit prime
[Test]
public void Prime1024 ()
{
string prime = "179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007";
BigInteger bi = BigInteger.Parse (prime);
Assert ("isProbablePrime-1024", bi.IsProbablePrime ());
AssertEquals ("ToString()", prime, bi.ToString ());
}
// Note: E.3 and E.4 are for Elliptic Curve Groups
// E.5. Well-Known Group 5: A 1536 bit prime
[Test]
public void Prime1536 ()
{
string prime = "2410312426921032588552076022197566074856950548502459942654116941958108831682612228890093858261341614673227141477904012196503648957050582631942730706805009223062734745341073406696246014589361659774041027169249453200378729434170325843778659198143763193776859869524088940195577346119843545301547043747207749969763750084308926339295559968882457872412993810129130294592999947926365264059284647209730384947211681434464714438488520940127459844288859336526896320919633919";
BigInteger bi = BigInteger.Parse (prime);
Assert ("isProbablePrime-1536", bi.IsProbablePrime ());
AssertEquals ("ToString()", prime, bi.ToString ());
}
}
// http://primes.utm.edu/lists/small/small.html
// note: BigInteger.IsProbablePrime defaults to Prime.ConfidenceFactor.Medium
[TestFixture]
public class SmallRandomPrimes {
private void AssertPrime (ulong value)
{
Assert.IsTrue (new BigInteger (value).IsProbablePrime (), value.ToString ());
}
private void AssertPrime (string value)
{
Assert.IsTrue (BigInteger.Parse (value).IsProbablePrime (), value);
}
[Test]
public void IsProbablePrime_10_digits ()
{
AssertPrime (5915587277);
AssertPrime (1500450271);
AssertPrime (3267000013);
AssertPrime (5754853343);
AssertPrime (4093082899);
AssertPrime (9576890767);
AssertPrime (3628273133);
AssertPrime (2860486313);
AssertPrime (5463458053);
AssertPrime (3367900313);
}
[Test]
public void IsProbablePrime_20_digits ()
{
AssertPrime ("48112959837082048697");
AssertPrime ("54673257461630679457");
AssertPrime ("29497513910652490397");
AssertPrime ("40206835204840513073");
AssertPrime (12764787846358441471);
AssertPrime ("71755440315342536873");
AssertPrime ("45095080578985454453");
AssertPrime ("27542476619900900873");
AssertPrime ("66405897020462343733");
AssertPrime ("36413321723440003717");
}
[Test]
public void IsProbablePrime_30_digits ()
{
AssertPrime ("671998030559713968361666935769");
AssertPrime ("282174488599599500573849980909");
AssertPrime ("521419622856657689423872613771");
AssertPrime ("362736035870515331128527330659");
AssertPrime ("115756986668303657898962467957");
AssertPrime ("590872612825179551336102196593");
AssertPrime ("564819669946735512444543556507");
AssertPrime ("513821217024129243948411056803");
AssertPrime ("416064700201658306196320137931");
AssertPrime ("280829369862134719390036617067");
}
[Test]
public void IsProbablePrime_40_digits ()
{
AssertPrime ("2425967623052370772757633156976982469681");
AssertPrime ("1451730470513778492236629598992166035067");
AssertPrime ("6075380529345458860144577398704761614649");
AssertPrime ("3615415881585117908550243505309785526231");
AssertPrime ("5992830235524142758386850633773258681119");
AssertPrime ("4384165182867240584805930970951575013697");
AssertPrime ("5991810554633396517767024967580894321153");
AssertPrime ("6847944682037444681162770672798288913849");
AssertPrime ("4146162919458530168953357282201621124057");
AssertPrime ("5570373270183181665098052481109678989411");
}
[Test]
public void IsProbablePrime_50_digits ()
{
AssertPrime ("22953686867719691230002707821868552601124472329079");
AssertPrime ("30762542250301270692051460539586166927291732754961");
AssertPrime ("29927402397991286489627837734179186385188296382227");
AssertPrime ("46484729803540183101830167875623788794533441216779");
AssertPrime ("95647806479275528135733781266203904794419563064407");
AssertPrime ("64495327731887693539738558691066839103388567300449");
AssertPrime ("58645563317564309847334478714939069495243200674793");
AssertPrime ("48705091355238882778842909230056712140813460157899");
AssertPrime ("15452417011775787851951047309563159388840946309807");
AssertPrime ("53542885039615245271174355315623704334284773568199");
}
[Test]
public void IsProbablePrime_60_digits ()
{
AssertPrime ("622288097498926496141095869268883999563096063592498055290461");
AssertPrime ("610692533270508750441931226384209856405876657993997547171387");
AssertPrime ("668486051696691190102895306426999370394054817506916629001851");
AssertPrime ("313539589974026666385010319707341761012894704055733952484113");
AssertPrime ("470287785858076441566723507866751092927015824834881906763507");
AssertPrime ("361720912810755408215708460645842859722715865206816237944587");
AssertPrime ("378348910233465647859184421334615532543749747185321634086219");
AssertPrime ("669483106578092405936560831017556154622901950048903016651289");
AssertPrime ("351300033958683656629281197430236951045077917074227778834807");
AssertPrime ("511704374946917490638851104912462284144240813125071454126151");
}
[Test]
public void IsProbablePrime_70_digits ()
{
AssertPrime ("4669523849932130508876392554713407521319117239637943224980015676156491");
AssertPrime ("4906275427767802358357703730938087362176142642699093827933107888253709");
AssertPrime ("2409130781894986571956777721649968801511465915451196376269177305066867");
AssertPrime ("7595009151080016652449223792726748985452052945413160073645842090827711");
AssertPrime ("3822535632033509464266159811805197854872067042990716005808372194664933");
AssertPrime ("5885903965180586669073549360644800583458138238012033647539649735017287");
AssertPrime ("5850725702766829291491370712136286009948642125131436113342815786444567");
AssertPrime ("4237080979868607742750808600846638318022863593147774739556427943294937");
AssertPrime ("3773180816219384606784189538899553110499442295782576702222280384917551");
AssertPrime ("9547848065153773335707495885453566120069130270246768806790708393909999");
}
[Test]
public void IsProbablePrime_80_digits ()
{
AssertPrime ("18532395500947174450709383384936679868383424444311405679463280782405796233163977");
AssertPrime ("39688644836832882526173831577536117815818454437810437210221644553381995813014959");
AssertPrime ("44822481511601066098713481453161748979849764719554039096395688045048053310178487");
AssertPrime ("54875133386847519273109693154204970395475080920935355580245252923343305939004903");
AssertPrime ("40979218404449071854385509743772465043384063785613460568705289173181846900181503");
AssertPrime ("56181069873486948735852120493417527485226565150317825065106074926567306630125961");
AssertPrime ("19469495355310348270990592580191998639221450743640952620236903851789700309402857");
AssertPrime ("34263233064835421125264776608163440537925705997962346596977803462033841059628723");
AssertPrime ("14759984361802021245410475928101669395348791811705709117374129427051861355011151");
AssertPrime ("67120333368520272532940669112228025474970578938046280618394371551488988323794243");
}
[Test]
public void IsProbablePrime_90_digits ()
{
AssertPrime ("282755483533707287054752184321121345766861480697448703443857012153264407439766013042402571");
AssertPrime ("370332600450952648802345609908335058273399487356359263038584017827194636172568988257769601");
AssertPrime ("463199005416013829210323411514132845972525641604435693287586851332821637442813833942427923");
AssertPrime ("374413471625854958269706803072259202131399386829497836277471117216044734280924224462969371");
AssertPrime ("664869143773196608462001772779382650311673568542237852546715913135688434614731717844868261");
AssertPrime ("309133826845331278722882330592890120369379620942948199356542318795450228858357445635314757");
AssertPrime ("976522637021306403150551933319006137720124048624544172072735055780411834104862667155922841");
AssertPrime ("635752334942676003169313626814655695963315290125751655287486460091602385142405742365191277");
AssertPrime ("625161793954624746211679299331621567931369768944205635791355694727774487677706013842058779");
AssertPrime ("204005728266090048777253207241416669051476369216501266754813821619984472224780876488344279");
}
[Test]
public void IsProbablePrime_100_digits ()
{
AssertPrime ("2074722246773485207821695222107608587480996474721117292752992589912196684750549658310084416732550077");
AssertPrime ("2367495770217142995264827948666809233066409497699870112003149352380375124855230068487109373226251983");
AssertPrime ("1814159566819970307982681716822107016038920170504391457462563485198126916735167260215619523429714031");
AssertPrime ("5371393606024775251256550436773565977406724269152942136415762782810562554131599074907426010737503501");
AssertPrime ("6513516734600035718300327211250928237178281758494417357560086828416863929270451437126021949850746381");
AssertPrime ("5628290459057877291809182450381238927697314822133923421169378062922140081498734424133112032854812293");
AssertPrime ("2908511952812557872434704820397229928450530253990158990550731991011846571635621025786879881561814989");
AssertPrime ("2193992993218604310884461864618001945131790925282531768679169054389241527895222169476723691605898517");
AssertPrime ("5202642720986189087034837832337828472969800910926501361967872059486045713145450116712488685004691423");
AssertPrime ("7212610147295474909544523785043492409969382148186765460082500085393519556525921455588705423020751421");
}
[Test]
public void IsProbablePrime_200_digits ()
{
AssertPrime ("58021664585639791181184025950440248398226136069516938232493687505822471836536824298822733710342250697739996825938232641940670857624514103125986134050997697160127301547995788468137887651823707102007839");
AssertPrime ("29072553456409183479268752003825253455672839222789445223234915115682921921621182714164684048719891059149763352939888629001652768286998932224000980861127751097886364432307005283784155195197202827350411");
AssertPrime ("41184172451867371867686906412307989908388177848827102865167949679167771021417488428983978626721272105583120243720400358313998904049755363682307706550788498535402989510396285940007396534556364659633739");
AssertPrime ("54661163828798316406139641599131347203445399912295442826728168170210404446004717881354193865401223990331513412680314853190460368937597393179445867548835085746203514200061810259071519181681661892618329");
AssertPrime ("71611195866368241734230315014260885890178941731009368469658803702463720956633120935294831101757574996161931982864195542669330457046568876289241536680683601749507786059442920003278263334056542642264651");
AssertPrime ("28591045597720075832628274729885724490653298360003309382769144463123258670807750560985604954275365591715208615509779345682419533206637382048824349415329839450792353652240682445321955199147316594996133");
AssertPrime ("49790921912819110019003521637763748399072771256062128988437189616228355821145834783451215869998723492323628198577054239101181556609916127864608488018093426129641387774385490891035446702272744866010729");
AssertPrime ("15474811206486587193258690501682404626361341756658894201908294153626080782693777003022566996735796983239343580281979005677758015801189957392350213806122307985157041153484138150252828152419133170303749");
AssertPrime ("12654646219963267405298825104551142450213038420566798208417393291567314379831789259173233506811083774527183953999862675239292185131178671317061020444490733287588383918793095608410078925861028249824377");
AssertPrime ("40992408416096028179761232532587525402909285099086220133403920525409552083528606215439915948260875718893797824735118621138192569490840098061133066650255608065609253901288801302035441884878187944219033");
}
[Test]
public void IsProbablePrime_300_digits ()
{
AssertPrime ("203956878356401977405765866929034577280193993314348263094772646453283062722701277632936616063144088173312372882677123879538709400158306567338328279154499698366071906766440037074217117805690872792848149112022286332144876183376326512083574821647933992961249917319836219304274280243803104015000563790123");
AssertPrime ("531872289054204184185084734375133399408303613982130856645299464930952178606045848877129147820387996428175564228204785846141207532462936339834139412401975338705794646595487324365194792822189473092273993580587964571659678084484152603881094176995594813302284232006001752128168901293560051833646881436219");
AssertPrime ("319705304701141539155720137200974664666792526059405792539680974929469783512821793995613718943171723765238853752439032835985158829038528214925658918372196742089464683960239919950882355844766055365179937610326127675178857306260955550407044463370239890187189750909036833976197804646589380690779463976173");
AssertPrime ("250556952327646214427246777488032351712139094643988394726193347352092526616305469220133287929222242315761834129196430398011844978805263868522770723615504744438638381670321613949280530254014602887707960375752016807510602846590492724216092721283154099469988532068424757856392563537802339735359978831013");
AssertPrime ("290245329165570025116016487217740287508837913295571609463914348778319654489118435855243301969001872061575755804802874062021927719647357060447135321577028929269578574760547268310055056867386875959045119093967972205124270441648450825188877095173754196346551952542599226295413057787340278528252358809329");
}
}
}

View File

@@ -0,0 +1,72 @@
//
// MonoTests.Mono.Math.PrimeTestingTest.cs
//
// Authors:
// Ben Maurer
//
// Copyright (c) 2003 Ben Maurer. All rights reserved
//
using System;
using Mono.Math;
using Mono.Math.Prime;
using Mono.Math.Prime.Generator;
using NUnit.Framework;
namespace MonoTests.Mono.Math {
[TestFixture]
public class SearchGenerator_Test : SequentialSearchPrimeGeneratorBase {
struct ContextData {
public ContextData (int bits, uint testData)
{
this.bits = bits; this.testData = testData;
}
public int bits;
public uint testData;
}
protected override BigInteger GenerateSearchBase (int bits, object Context)
{
BigInteger ret = base.GenerateSearchBase (bits, Context);
ContextData ctx = (ContextData)Context;
Assertion.AssertEquals (ctx.bits, bits);
uint d = ctx.testData;
for (uint i = (uint)bits - 2; d > 0; i--, d >>= 1)
ret.SetBit (i, (d&1) == 1);
return ret;
}
public override PrimalityTest PrimalityTest {
get {
return new PrimalityTest (PrimalityTests.SmallPrimeSppTest);
}
}
protected override bool IsPrimeAcceptable (BigInteger bi, object Context)
{
return bi.TestBit (1);
}
[Test]
public void TestPrimeGeneration ()
{
Random r = new Random ();
for (int i = 0; i < 5; i++) {
ContextData ctx = new ContextData (128, (uint)r.Next (int.MinValue, int.MaxValue));
BigInteger p = GenerateNewPrime (128, ctx);
Assert.IsTrue (p.TestBit (1));
uint d = ctx.testData;
for (uint j = 128 - 2; d > 0; j--, d >>= 1)
Assertion.AssertEquals ((d&1) == 1, p.TestBit (j));
}
}
}
}

View File

@@ -0,0 +1,46 @@
2008-12-23 Sebastien Pouliot <sebastien@ximian.com>
* SoftwarePublisherCertificateTest.cs: Update test syntax to nunit
2.2. Test more cases like base64, unicode base64 and PEM base64.
2004-11-05 Sebastien Pouliot <sebastien@ximian.com>
* PrivateKeyTest.cs: Fixed SaltWithoutPassword test to always use
little endian.
2004-10-28 Sebastien Pouliot <sebastien@ximian.com>
* AuthenticodeDeformatterTest.cs: Check timestamp as an UTC value so
that the test can work anywhere in the world (and not just at home ;)
2004-09-07 Sebastien Pouliot <sebastien@ximian.com>
* AuthenticodeDeformatterTest.cs: New. Unit tests to validate a valid
authenticode-signed assembly (included in test file) and an invalid
(non signed) assembly (the unit test assembly).
2004-05-11 Sebastien Pouliot <sebastien@ximian.com>
* PrivateKeyTest.cs: Added new unit tests for better coverage.
* SoftwarePublisherCertificateTest.cs: Added new unit tests for better
coverage.
2004-04-22 Sebastien Pouliot <sebastien@ximian.com>
* SoftwarePublisherCertificateTest.cs: Ajusted to changes in the
assembly.
2003-10-11 Sebastien Pouliot <spouliot@videotron.ca>
* SoftwarePublisherCertificateTest.cs: Repaired test build.
2003-03-15 Sebastien Pouliot <spouliot@videotron.ca>
* PrivateKeyTest.cs: New. Unit test for using some PVK files
generated by makecert.exe.
2003-03-06 Sebastien Pouliot <spouliot@videotron.ca>
* SoftwarePublisherCertificateTest.cs: New. Unit tests for
using some SPC files generated by cert2spc.

View File

@@ -0,0 +1,334 @@
//
// PrivateKeyTest.cs - NUnit Test Cases for Private Key File
//
// Author:
// Sebastien Pouliot (sebastien@ximian.com)
// Bernie Solomon
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004 Novell (http://www.novell.com)
//
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Mono.Security.Authenticode;
using NUnit.Framework;
namespace MonoTests.Mono.Security.Authenticode {
// partial copy of /mcs/class/Mono.Security/Mono.Security/BitConverterLE.cs
internal sealed class BitConverterLE {
private BitConverterLE ()
{
}
unsafe private static byte[] GetUIntBytes (byte *bytes)
{
if (BitConverter.IsLittleEndian)
return new byte [] { bytes [0], bytes [1], bytes [2], bytes [3] };
else
return new byte [] { bytes [3], bytes [2], bytes [1], bytes [0] };
}
unsafe internal static byte[] GetBytes (int value)
{
return GetUIntBytes ((byte *) &value);
}
unsafe private static void UIntFromBytes (byte *dst, byte[] src, int startIndex)
{
if (BitConverter.IsLittleEndian) {
dst [0] = src [startIndex];
dst [1] = src [startIndex + 1];
dst [2] = src [startIndex + 2];
dst [3] = src [startIndex + 3];
} else {
dst [0] = src [startIndex + 3];
dst [1] = src [startIndex + 2];
dst [2] = src [startIndex + 1];
dst [3] = src [startIndex];
}
}
unsafe internal static int ToInt32 (byte[] value, int startIndex)
{
int ret;
UIntFromBytes ((byte *) &ret, value, startIndex);
return ret;
}
}
// HOWTO create a PVK file (on Windows using MS tools)
// makecert -n "CN=PVK1" -sv 1.pvk 1.cer
[TestFixture]
public class PrivateKeyTest : Assertion {
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Fail (msg + " -> First array is NULL");
if (array2 == null)
Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
if (array1.Length > 0) {
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
}
Assert (msg, a);
}
string testfile;
[TestFixtureSetUp]
public void FixtureSetup ()
{
testfile = Path.Combine (Path.GetTempPath (), "test.pvk");
}
[TearDown]
public void TearDown ()
{
File.Delete (testfile);
}
private void WriteBuffer (byte[] buffer)
{
FileStream fs = File.Create (testfile);
fs.Write (buffer, 0, buffer.Length);
fs.Close ();
}
static byte[] nopwd = {
0x1E, 0xF1, 0xB5, 0xB0, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x01, 0x00, 0x00,
0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x52, 0x53, 0x41, 0x32,
0x00, 0x02, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0xDB, 0x27, 0x34, 0xCB,
0x3D, 0x27, 0xA0, 0x4F, 0x50, 0x68, 0xC7, 0x95, 0x4B, 0x7B, 0x69, 0xD1,
0xFD, 0x30, 0x58, 0x72, 0x6B, 0xFF, 0x77, 0x64, 0x96, 0x35, 0x72, 0x36,
0x98, 0xCE, 0x56, 0xDD, 0x94, 0x43, 0x7C, 0x0D, 0x61, 0x5C, 0x3A, 0xD6,
0x1E, 0xD1, 0x89, 0x6C, 0xD5, 0x9B, 0x3E, 0xD3, 0x60, 0x3E, 0x28, 0x3F,
0xC6, 0x51, 0x35, 0x0D, 0x4F, 0x7E, 0x79, 0xE6, 0xAE, 0xE4, 0xC8, 0xE9,
0xA9, 0x14, 0x6E, 0xD2, 0xBD, 0x42, 0xB2, 0x14, 0x82, 0xEE, 0x26, 0x8F,
0x21, 0x33, 0x1A, 0xD5, 0xD7, 0x6D, 0x90, 0xED, 0xC1, 0xA4, 0x1C, 0x84,
0x3F, 0xA3, 0x8A, 0xFB, 0x33, 0x30, 0x32, 0xF6, 0xE3, 0xE6, 0xC8, 0x81,
0x54, 0x88, 0x1A, 0x92, 0xF0, 0xBA, 0xB8, 0x4F, 0x52, 0x8D, 0xBD, 0x04,
0x47, 0xBC, 0x55, 0xBC, 0xD0, 0x3D, 0x2C, 0x7F, 0x4F, 0xAB, 0x99, 0xDC,
0xFB, 0x2D, 0x18, 0xF3, 0x99, 0x77, 0x10, 0x82, 0x48, 0xF3, 0xDE, 0x36,
0xD7, 0x62, 0xA9, 0xCB, 0x58, 0x01, 0x97, 0x79, 0x66, 0x0D, 0x01, 0x1F,
0xCC, 0x0B, 0xAB, 0x02, 0xA9, 0xE3, 0xF5, 0x85, 0xA8, 0x52, 0xBC, 0x10,
0xD7, 0x90, 0x60, 0x60, 0x50, 0xB1, 0x08, 0x01, 0x85, 0x52, 0xAC, 0x05,
0xF1, 0xCE, 0xF9, 0xE7, 0xBE, 0xDE, 0x46, 0x64, 0x40, 0xE5, 0x07, 0x82,
0x20, 0xDD, 0x48, 0xF1, 0xE1, 0x85, 0x29, 0x8C, 0xFE, 0x57, 0x7C, 0x65,
0xF5, 0x5C, 0x51, 0x9F, 0x63, 0xDE, 0xFC, 0x9C, 0xF9, 0x3F, 0x3D, 0xF2,
0xDC, 0x9F, 0x65, 0x27, 0xEC, 0x50, 0x54, 0xB9, 0xCE, 0xF2, 0xC3, 0x10,
0x93, 0x8B, 0xBE, 0x6A, 0xC1, 0x35, 0x19, 0xBB, 0x66, 0xA5, 0x5E, 0xEA,
0x91, 0x1D, 0xFB, 0x26, 0xF8, 0x0F, 0x5C, 0x13, 0x73, 0xCC, 0x9A, 0x68,
0x4C, 0x08, 0x9C, 0x02, 0xE5, 0xD5, 0x91, 0x37, 0x13, 0x68, 0x3D, 0xFC,
0x3E, 0xA7, 0x43, 0x94, 0xBC, 0xFC, 0x4F, 0xB1, 0x8E, 0xC5, 0x5F, 0x24,
0x9A, 0x6C, 0xDB, 0xC2, 0x49, 0x91, 0xEC, 0x2B, 0xB9, 0x3D, 0x2B, 0x96,
0xA3, 0x60, 0xE3, 0xA8, 0x8C, 0x28, 0xB7, 0x53 };
[Test]
public void MSNoPassword ()
{
WriteBuffer (nopwd);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile);
AssertNotNull ("msnopwd.RSA", pvk.RSA);
Assert ("msnopwd.Encrypted", !pvk.Encrypted);
Assert ("msnopwd.Weak", pvk.Weak);
AssertEquals ("msnopwd.KeyType", 2, pvk.KeyType);
}
// this will convert a PVK file without a password to a PVK file
// with a password (weak)
[Test]
public void ConvertToPasswordWeak ()
{
WriteBuffer (nopwd);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile);
string rsa1 = pvk.RSA.ToXmlString (true);
pvk.Save (testfile, "password");
pvk = PrivateKey.CreateFromFile (testfile, "password");
AssertNotNull ("topwd.RSA", pvk.RSA);
string rsa2 = pvk.RSA.ToXmlString (true);
AssertEquals ("topwd.RSA identical", rsa1, rsa2);
Assert ("topwd.Encrypted", pvk.Encrypted);
Assert ("topwd.Weak", pvk.Weak);
}
// this will convert a PVK file without a password to a PVK file
// with a password (strong)
[Test]
public void ConvertToPasswordStrong ()
{
WriteBuffer (nopwd);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile);
string rsa1 = pvk.RSA.ToXmlString (true);
pvk.Weak = false; // we want strong crypto
pvk.Save (testfile, "password");
pvk = PrivateKey.CreateFromFile (testfile, "password");
AssertNotNull ("topwd.RSA", pvk.RSA);
string rsa2 = pvk.RSA.ToXmlString (true);
AssertEquals ("topwd.RSA identical", rsa1, rsa2);
Assert ("topwd.Encrypted", pvk.Encrypted);
Assert ("topwd.Weak", !pvk.Weak);
}
static byte[] pwd = {
0x1E, 0xF1, 0xB5, 0xB0, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
0x01, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x34, 0x01, 0x00, 0x00,
0x37, 0x53, 0x7C, 0x99, 0x01, 0xB5, 0x50, 0xF3, 0x79, 0x6E, 0xDE, 0xD5,
0x8A, 0x1B, 0xED, 0x05, 0x07, 0x02, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00,
0x33, 0x98, 0xE8, 0x81, 0xA2, 0x9A, 0xEB, 0x36, 0xEF, 0x1B, 0x52, 0xFD,
0xC3, 0x9B, 0xB7, 0x32, 0x02, 0xC0, 0x9F, 0xE0, 0x6A, 0x50, 0x81, 0x61,
0x37, 0xBE, 0xEC, 0x1B, 0xC3, 0x34, 0x7B, 0x03, 0x0B, 0xC8, 0x31, 0x7B,
0x0D, 0xA6, 0x7A, 0x05, 0xEA, 0xD1, 0xCA, 0x9A, 0xF3, 0x71, 0x84, 0x77,
0x9E, 0x6F, 0xD1, 0xD0, 0xA0, 0x62, 0xFF, 0x3D, 0x24, 0x31, 0x01, 0xD7,
0x02, 0x38, 0x11, 0xB6, 0x5E, 0x4A, 0xCC, 0x33, 0xF0, 0xEB, 0x0B, 0x38,
0x51, 0x27, 0xCF, 0xAD, 0x20, 0x20, 0x9A, 0x80, 0x80, 0x37, 0xBE, 0x4C,
0xBC, 0xA4, 0xC8, 0xE1, 0x5B, 0x57, 0x02, 0xC9, 0x04, 0x53, 0x82, 0x6E,
0x0B, 0x06, 0x94, 0xCF, 0xC2, 0xEF, 0x1A, 0x6C, 0xC8, 0x78, 0x41, 0xB1,
0x63, 0xBD, 0x52, 0x1C, 0x05, 0x2C, 0x97, 0x83, 0x10, 0xD0, 0xFE, 0x22,
0x2F, 0x29, 0xAF, 0xC0, 0xCA, 0xC7, 0x96, 0x0A, 0x9A, 0xC8, 0x69, 0x58,
0xBF, 0xA9, 0xDD, 0x75, 0xE4, 0xAB, 0xC8, 0xFE, 0xF5, 0xFE, 0xC5, 0x18,
0x2B, 0x93, 0xC0, 0x67, 0xFF, 0xDC, 0xE3, 0xAF, 0xAC, 0x5F, 0x7E, 0x5F,
0x0D, 0xEA, 0x41, 0xEB, 0x57, 0x1A, 0x4D, 0xB3, 0x10, 0x07, 0x09, 0xDC,
0x3F, 0xC1, 0xB7, 0x9F, 0xC5, 0x79, 0xCD, 0x6E, 0x79, 0x48, 0x4F, 0x51,
0xD8, 0x4B, 0x3A, 0x32, 0x40, 0x05, 0x6B, 0x74, 0xC9, 0xF4, 0xD9, 0x67,
0x9D, 0x65, 0xFF, 0x4C, 0x4E, 0xAB, 0xC0, 0xC5, 0x65, 0x49, 0xEB, 0x6D,
0xAB, 0xB9, 0x30, 0x5A, 0xFC, 0x5D, 0xD4, 0xE7, 0xB5, 0xDB, 0xD3, 0xF1,
0xBF, 0x6F, 0xD4, 0x18, 0xD6, 0xE7, 0x76, 0x12, 0xCE, 0x57, 0xDF, 0x63,
0x2C, 0x88, 0x2F, 0x0F, 0x31, 0x3A, 0x78, 0xA0, 0xB9, 0x5A, 0x11, 0x50,
0x18, 0x98, 0xA4, 0xA3, 0x9D, 0xC7, 0xC4, 0x5C, 0xE7, 0xDF, 0xFD, 0x4B,
0x96, 0x84, 0x27, 0x4C, 0x84, 0x92, 0xE9, 0x5E, 0x93, 0x65, 0x0C, 0xC9,
0xB4, 0x5B, 0xE7, 0xC0, 0x26, 0x66, 0x36, 0x7A, 0x36, 0x56, 0xB0, 0xC8,
0x34, 0xBB, 0x4F, 0xBD, 0x0E, 0xDA, 0x04, 0x82, 0x74, 0x07, 0x3D, 0xD3,
0x1D, 0x8F, 0x9B, 0x34, 0x4F, 0xA8, 0xD5, 0x58, 0x12, 0xE8, 0x96, 0x20 };
[Test]
public void MSPasswordWeak ()
{
WriteBuffer (pwd);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile, "password");
AssertNotNull ("mspwd.RSA", pvk.RSA);
Assert ("mspwd.Encrypted", pvk.Encrypted);
Assert ("mspwd.Weak", pvk.Weak);
}
// this will convert a PVK file with a password to a PVK file
// without a password
[Test]
public void RemovePassword ()
{
WriteBuffer (nopwd);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile, "password");
string rsa1 = pvk.RSA.ToXmlString (true);
pvk.Save (testfile);
pvk = PrivateKey.CreateFromFile (testfile);
AssertNotNull ("nomorepwd.RSA", pvk.RSA);
string rsa2 = pvk.RSA.ToXmlString (true);
AssertEquals ("nomorepwd.RSA identical", rsa1, rsa2);
Assert ("nomorepwd.Encrypted", !pvk.Encrypted);
Assert ("nomorepwd.Weak", pvk.Weak);
}
[Test]
public void CreatePVK ()
{
PrivateKey pvk = new PrivateKey ();
pvk.KeyType = 2;
pvk.RSA = RSA.Create ();
string rsa1 = pvk.RSA.ToXmlString (true);
pvk.Save (testfile, "mono");
pvk = PrivateKey.CreateFromFile (testfile, "mono");
AssertNotNull ("new.RSA", pvk.RSA);
string rsa2 = pvk.RSA.ToXmlString (true);
AssertEquals ("new.RSA identical", rsa1, rsa2);
Assert ("new.Encrypted", pvk.Encrypted);
Assert ("new.Weak", !pvk.Weak);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Save_Null ()
{
PrivateKey pvk = new PrivateKey ();
pvk.Save (null, "mono");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void BadMagic ()
{
byte[] bad = (byte[]) nopwd.Clone ();
bad [0] = 0x00;
WriteBuffer (bad);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile, null);
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void BadHeader ()
{
byte[] bad = (byte[]) nopwd.Clone ();
bad [4] = 0x01;
WriteBuffer (bad);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile, null);
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void SaltWithoutPassword ()
{
byte[] bad = (byte[]) nopwd.Clone ();
int saltlen = BitConverterLE.ToInt32 (bad, 16);
int keylen = BitConverterLE.ToInt32 (bad, 20);
// preserve total length
saltlen += 8;
keylen -= 8;
// modify blob
byte[] data = BitConverterLE.GetBytes (saltlen);
Buffer.BlockCopy (data, 0, bad, 16, data.Length);
data = BitConverterLE.GetBytes (keylen);
Buffer.BlockCopy (data, 0, bad, 20, data.Length);
// save-n-load
WriteBuffer (bad);
PrivateKey pvk = PrivateKey.CreateFromFile (testfile, null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void CreateFromFile_Null ()
{
PrivateKey pvk = PrivateKey.CreateFromFile (null, "password");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructor_Null ()
{
PrivateKey pvk = new PrivateKey (null, "password");
}
}
}

View File

@@ -0,0 +1,347 @@
//
// ARC4ManagedTest.cs - NUnit Test Cases for Alleged RC4(tm)
// RC4 is a trademark of RSA Security
//
// Author:
// Sebastien Pouliot (spouliot@motus.com)
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
//
using NUnit.Framework;
using System;
using System.Security.Cryptography;
using Mono.Security.Cryptography;
namespace MonoTests.Mono.Security.Cryptography {
// References
// a. Usenet 1994 - RC4 Algorithm revealed
// http://www.qrst.de/html/dsds/rc4.htm
// b. Netscape SSL version 3 implementation details
// Export Client SSL Connection Details
// http://wp.netscape.com/eng/ssl3/traces/trc-clnt-ex.html
[TestFixture]
public class ARC4ManagedTest {
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Assert.Fail (msg + " -> First array is NULL");
if (array2 == null)
Assert.Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
Assert.IsTrue (a, msg);
}
// from ref. a
[Test]
public void Vector0 ()
{
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] input = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] expected = { 0x75, 0xb7, 0x87, 0x80, 0x99, 0xe0, 0xc5, 0x96 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 0", expected, output);
}
// from ref. a
[Test]
public void Vector1 ()
{
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] input = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] expected = { 0x74, 0x94, 0xc2, 0xe7, 0x10, 0x4b, 0x08, 0x79 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 1", expected, output);
}
// from ref. a
[Test]
public void Vector2 ()
{
byte[] key = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] input = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] expected = { 0xde, 0x18, 0x89, 0x41, 0xa3, 0x37, 0x5d, 0x3a };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 2", expected, output);
}
// from ref. a
[Test]
public void Vector3 ()
{
byte[] key = { 0xef, 0x01, 0x23, 0x45 };
byte[] input = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
byte[] expected = { 0xd6, 0xa1, 0x41, 0xa7, 0xec, 0x3c, 0x38, 0xdf, 0xbd, 0x61 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 3", expected, output);
}
// from ref. a
[Test]
public void Vector4 ()
{
byte[] key = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
byte[] input = new byte [512];
for (int i=0; i < input.Length; i++)
input [i] = 0x01;
byte[] expected = { 0x75, 0x95, 0xc3, 0xe6, 0x11, 0x4a, 0x09, 0x78, 0x0c, 0x4a, 0xd4,
0x52, 0x33, 0x8e, 0x1f, 0xfd, 0x9a, 0x1b, 0xe9, 0x49, 0x8f,
0x81, 0x3d, 0x76, 0x53, 0x34, 0x49, 0xb6, 0x77, 0x8d, 0xca,
0xd8, 0xc7, 0x8a, 0x8d, 0x2b, 0xa9, 0xac, 0x66, 0x08, 0x5d,
0x0e, 0x53, 0xd5, 0x9c, 0x26, 0xc2, 0xd1, 0xc4, 0x90, 0xc1,
0xeb, 0xbe, 0x0c, 0xe6, 0x6d, 0x1b, 0x6b, 0x1b, 0x13, 0xb6,
0xb9, 0x19, 0xb8, 0x47, 0xc2, 0x5a, 0x91, 0x44, 0x7a, 0x95,
0xe7, 0x5e, 0x4e, 0xf1, 0x67, 0x79, 0xcd, 0xe8, 0xbf, 0x0a,
0x95, 0x85, 0x0e, 0x32, 0xaf, 0x96, 0x89, 0x44, 0x4f, 0xd3,
0x77, 0x10, 0x8f, 0x98, 0xfd, 0xcb, 0xd4, 0xe7, 0x26, 0x56,
0x75, 0x00, 0x99, 0x0b, 0xcc, 0x7e, 0x0c, 0xa3, 0xc4, 0xaa,
0xa3, 0x04, 0xa3, 0x87, 0xd2, 0x0f, 0x3b, 0x8f, 0xbb, 0xcd,
0x42, 0xa1, 0xbd, 0x31, 0x1d, 0x7a, 0x43, 0x03, 0xdd, 0xa5,
0xab, 0x07, 0x88, 0x96, 0xae, 0x80, 0xc1, 0x8b, 0x0a, 0xf6,
0x6d, 0xff, 0x31, 0x96, 0x16, 0xeb, 0x78, 0x4e, 0x49, 0x5a,
0xd2, 0xce, 0x90, 0xd7, 0xf7, 0x72, 0xa8, 0x17, 0x47, 0xb6,
0x5f, 0x62, 0x09, 0x3b, 0x1e, 0x0d, 0xb9, 0xe5, 0xba, 0x53,
0x2f, 0xaf, 0xec, 0x47, 0x50, 0x83, 0x23, 0xe6, 0x71, 0x32,
0x7d, 0xf9, 0x44, 0x44, 0x32, 0xcb, 0x73, 0x67, 0xce, 0xc8,
0x2f, 0x5d, 0x44, 0xc0, 0xd0, 0x0b, 0x67, 0xd6, 0x50, 0xa0,
0x75, 0xcd, 0x4b, 0x70, 0xde, 0xdd, 0x77, 0xeb, 0x9b, 0x10,
0x23, 0x1b, 0x6b, 0x5b, 0x74, 0x13, 0x47, 0x39, 0x6d, 0x62,
0x89, 0x74, 0x21, 0xd4, 0x3d, 0xf9, 0xb4, 0x2e, 0x44, 0x6e,
0x35, 0x8e, 0x9c, 0x11, 0xa9, 0xb2, 0x18, 0x4e, 0xcb, 0xef,
0x0c, 0xd8, 0xe7, 0xa8, 0x77, 0xef, 0x96, 0x8f, 0x13, 0x90,
0xec, 0x9b, 0x3d, 0x35, 0xa5, 0x58, 0x5c, 0xb0, 0x09, 0x29,
0x0e, 0x2f, 0xcd, 0xe7, 0xb5, 0xec, 0x66, 0xd9, 0x08, 0x4b,
0xe4, 0x40, 0x55, 0xa6, 0x19, 0xd9, 0xdd, 0x7f, 0xc3, 0x16,
0x6f, 0x94, 0x87, 0xf7, 0xcb, 0x27, 0x29, 0x12, 0x42, 0x64,
0x45, 0x99, 0x85, 0x14, 0xc1, 0x5d, 0x53, 0xa1, 0x8c, 0x86,
0x4c, 0xe3, 0xa2, 0xb7, 0x55, 0x57, 0x93, 0x98, 0x81, 0x26,
0x52, 0x0e, 0xac, 0xf2, 0xe3, 0x06, 0x6e, 0x23, 0x0c, 0x91,
0xbe, 0xe4, 0xdd, 0x53, 0x04, 0xf5, 0xfd, 0x04, 0x05, 0xb3,
0x5b, 0xd9, 0x9c, 0x73, 0x13, 0x5d, 0x3d, 0x9b, 0xc3, 0x35,
0xee, 0x04, 0x9e, 0xf6, 0x9b, 0x38, 0x67, 0xbf, 0x2d, 0x7b,
0xd1, 0xea, 0xa5, 0x95, 0xd8, 0xbf, 0xc0, 0x06, 0x6f, 0xf8,
0xd3, 0x15, 0x09, 0xeb, 0x0c, 0x6c, 0xaa, 0x00, 0x6c, 0x80,
0x7a, 0x62, 0x3e, 0xf8, 0x4c, 0x3d, 0x33, 0xc1, 0x95, 0xd2,
0x3e, 0xe3, 0x20, 0xc4, 0x0d, 0xe0, 0x55, 0x81, 0x57, 0xc8,
0x22, 0xd4, 0xb8, 0xc5, 0x69, 0xd8, 0x49, 0xae, 0xd5, 0x9d,
0x4e, 0x0f, 0xd7, 0xf3, 0x79, 0x58, 0x6b, 0x4b, 0x7f, 0xf6,
0x84, 0xed, 0x6a, 0x18, 0x9f, 0x74, 0x86, 0xd4, 0x9b, 0x9c,
0x4b, 0xad, 0x9b, 0xa2, 0x4b, 0x96, 0xab, 0xf9, 0x24, 0x37,
0x2c, 0x8a, 0x8f, 0xff, 0xb1, 0x0d, 0x55, 0x35, 0x49, 0x00,
0xa7, 0x7a, 0x3d, 0xb5, 0xf2, 0x05, 0xe1, 0xb9, 0x9f, 0xcd,
0x86, 0x60, 0x86, 0x3a, 0x15, 0x9a, 0xd4, 0xab, 0xe4, 0x0f,
0xa4, 0x89, 0x34, 0x16, 0x3d, 0xdd, 0xe5, 0x42, 0xa6, 0x58,
0x55, 0x40, 0xfd, 0x68, 0x3c, 0xbf, 0xd8, 0xc0, 0x0f, 0x12,
0x12, 0x9a, 0x28, 0x4d, 0xea, 0xcc, 0x4c, 0xde, 0xfe, 0x58,
0xbe, 0x71, 0x37, 0x54, 0x1c, 0x04, 0x71, 0x26, 0xc8, 0xd4,
0x9e, 0x27, 0x55, 0xab, 0x18, 0x1a, 0xb7, 0xe9, 0x40, 0xb0,
0xc0 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = key;
ICryptoTransform stream = rc4.CreateEncryptor ();
byte[] output = stream.TransformFinalBlock (input, 0, input.Length);
AssertEquals ("RC4 - Test Vector 4", expected, output);
}
static byte[] clientWriteKey = { 0x32, 0x10, 0xcd, 0xe1, 0xd6, 0xdc, 0x07, 0x83, 0xf3, 0x75, 0x4c, 0x32, 0x2e, 0x59, 0x96, 0x61 };
static byte[] serverWriteKey = { 0xed, 0x0e, 0x56, 0xc8, 0x95, 0x12, 0x37, 0xb6, 0x21, 0x17, 0x1c, 0x72, 0x79, 0x91, 0x12, 0x1e };
// SSL3 Client's Finished Handshake (from ref. b)
[Test]
public void SSLClient ()
{
byte[] data = { 0x14, 0x00, 0x00, 0x24, 0xf2, 0x40, 0x10, 0x3f, 0x74, 0x63, 0xea, 0xe8, 0x7a, 0x27, 0x23, 0x56, 0x5f, 0x59, 0x07, 0xd2, 0xa3, 0x79, 0x5d, 0xb7, 0x8b, 0x94, 0xdb, 0xcf, 0xfa, 0xf5, 0x18, 0x22, 0x15, 0x7b, 0xf2, 0x4a, 0x96, 0x52, 0x9a, 0x0e, 0xd3, 0x09, 0xde, 0x28, 0x84, 0xa7, 0x07, 0x5c, 0x7c, 0x0c, 0x08, 0x85, 0x6b, 0x4f, 0x63, 0x04 };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = clientWriteKey;
// encrypt inplace (in same buffer)
rc4.TransformBlock (data, 0, data.Length, data, 0);
byte[] expectedData = { 0xed, 0x37, 0x7f, 0x16, 0xd3, 0x11, 0xe8, 0xa3, 0xe1, 0x2a, 0x20, 0xb7, 0x88, 0xf6, 0x11, 0xf3, 0xa6, 0x7d, 0x37, 0xf7, 0x17, 0xac, 0x67, 0x20, 0xb8, 0x0e, 0x88, 0xd1, 0xa0, 0xc6, 0x83, 0xe4, 0x80, 0xe8, 0xc7, 0xe3, 0x0b, 0x91, 0x29, 0x30, 0x29, 0xe4, 0x28, 0x47, 0xb7, 0x40, 0xa4, 0xd1, 0x3c, 0xda, 0x82, 0xb7, 0xb3, 0x9f, 0x67, 0x10 };
AssertEquals ("RC4 - Client's Finished Handshake", expectedData, data);
}
// SSL3 Server Finished Handshake (from ref. b)
[Test]
public void SSLServer ()
{
byte[] encryptedData = { 0x54, 0x3c, 0xe1, 0xe7, 0x4d, 0x77, 0x76, 0x62, 0x86, 0xfa, 0x4e, 0x0a, 0x6f, 0x5f, 0x6a, 0x3d, 0x43, 0x26, 0xf4, 0xad, 0x8d, 0x3e, 0x09, 0x0b, 0x2b, 0xf7, 0x9f, 0x49, 0x44, 0x92, 0xfb, 0xa9, 0xa4, 0xb0, 0x5a, 0xd8, 0x72, 0x77, 0x6e, 0x8b, 0xb3, 0x78, 0xfb, 0xda, 0xe0, 0x25, 0xef, 0xb3, 0xf5, 0xa7, 0x90, 0x08, 0x6d, 0x60, 0xd5, 0x4e };
ARC4Managed rc4 = new ARC4Managed ();
rc4.Key = serverWriteKey;
// decrypt inplace (in same buffer)
rc4.TransformBlock (encryptedData, 0, encryptedData.Length, encryptedData, 0);
byte[] expectedData = { 0x14, 0x00, 0x00, 0x24, 0xb7, 0xcc, 0xd6, 0x05, 0x6b, 0xfc, 0xfa, 0x6d, 0xfa, 0xdd, 0x76, 0x81, 0x45, 0x36, 0xe4, 0xf4, 0x26, 0x35, 0x72, 0x2c, 0xec, 0x87, 0x62, 0x1f, 0x55, 0x08, 0x05, 0x4f, 0xc8, 0xf5, 0x7c, 0x49, 0xe2, 0xee, 0xc5, 0xba, 0xbd, 0x69, 0x27, 0x3b, 0xd0, 0x13, 0x23, 0x52, 0xed, 0xec, 0x11, 0x55, 0xd8, 0xb9, 0x90, 0x8c };
AssertEquals ("RC4 - Server's Finished Handshake", expectedData, encryptedData);
}
[Test]
public void DefaultProperties ()
{
ARC4Managed rc4 = new ARC4Managed ();
Assert.IsFalse (rc4.CanReuseTransform, "CanReuseTransform");
Assert.IsTrue (rc4.CanTransformMultipleBlocks, "CanTransformMultipleBlocks");
Assert.AreEqual (1, rc4.InputBlockSize, "InputBlockSize");
Assert.AreEqual (1, rc4.OutputBlockSize, "OutputBlockSize");
}
[Test]
public void DefaultSizes ()
{
ARC4Managed rc4 = new ARC4Managed ();
rc4.GenerateKey ();
rc4.GenerateIV ();
Assert.AreEqual (16, rc4.Key.Length, "Key.Length");
Assert.AreEqual (128, rc4.KeySize, "KeySize");
Assert.AreEqual (0, rc4.IV.Length, "IV.Length");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformBlock_InputBuffer_Null ()
{
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (null, 0, 1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_InputOffset_Negative ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, -1, 1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputOffset_Overflow ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, Int32.MaxValue, 1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_InputCount_Negative ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, -1, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputCount_Overflow ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 1, Int32.MaxValue, output, 0);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformBlock_OutputBuffer_Null ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, 1, null, 0);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_OutputOffset_Negative ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, 1, output, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_OutputOffset_Overflow ()
{
byte[] input = new byte [1];
byte[] output = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformBlock (input, 0, 1, output, Int32.MaxValue);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformFinalBlock_InputBuffer_Null ()
{
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (null, 0, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformFinalBlock_InputOffset_Negative ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, -1, 1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputOffset_Overflow ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, Int32.MaxValue, 1);
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformFinalBlock_InputCount_Negative ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, 0, -1);
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputCount_Overflow ()
{
byte[] input = new byte [1];
ARC4Managed rc4 = new ARC4Managed ();
rc4.TransformFinalBlock (input, 1, Int32.MaxValue);
}
}
}

View File

@@ -0,0 +1,103 @@
2007-05-08 Randolph Chung <tausq@debian.org>
* CryptoConvertTest.cs: Add tests for DSA conversion functions.
2006-10-04 Sebastien Pouliot <sebastien@ximian.com>
* PKCS1Test.cs: Ensure the latest changes didn't break some SSL3
features that uses MD5SHA1 as a hash function.
* RSAManagedTest.cs: Forgot to update test cases after right padding
was added.
2006-09-05 Sebastien Pouliot <sebastien@ximian.com>
* RSAManagedTest.cs: Add a test case for #79269 (wrong exception).
2005-04-27 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Upgrade to NUnit 2.2 API to use Ignore.
Now Ignore all tests that results in UnauthorizedAccessException.
2004-12-06 Sebastien Pouliot <sebastien@ximian.com>
* RSAManagedTest.cs: New. Unit tests to validate RSA decryption when
using key blinding with or without using CRT.
2004-11-05 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Forgot to re-fix the second failing test.
2004-11-03 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Re-fix as the UnauthorizedAccessException
is the InnerException of a CryptographicException.
2004-10-28 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: Fix two tests to work even if the user
doesn't have access to the machine key store.
2004-09-22 Sebastien Pouliot <sebastien@ximian.com>
* SHA224Test.cs: Fix compilation error with NUnit 2.1.91 (CVS).
2004-09-16 Sebastien Pouliot <sebastien@ximian.com>
* SHA224ManagedTest.cs: New. Unit tests for the managed implementation
of SHA-224.
* SHA224Test.cs: New. Unit tests for SHA-224. Use test vectors from
RFC3874.
2004-05-27 Sebastien Pouliot <sebastien@ximian.com>
* ARC4ManagedTest.cs: Added unit tests for exceptions coming from
ICryptoTransform interface (TransformBlock, TransformFinalBlock).
2004-05-11 Sebastien Pouliot <sebastien@ximian.com>
* ARC4ManagedTest.cs: Added missing unit tests for complete coverage.
Completed convertion to NUnit 2.
* CryptoConvertTest.cs: Added/modified unit tests for better coverage.
* MD2ManagedTest.cs: Added unit tests for better coverage.
* MD2Test.cs: Added unit tests for better coverage.
* MD4ManagedTest.cs: Added unit tests for better coverage.
* MD4Test.cs: Added unit tests for better coverage.
* PKCS8.cs: Added unit tests for better coverage.
2004-05-01 Sebastien Pouliot <sebastien@ximian.com>
* CryptoConvertTest.cs: Added test case where a strongname key pair
(RSA 1024 bits) was truncated in the last parameter (D) (i.e. the
private key) but saved by the use of CRT (Chinese Remainder Theorem).
2004-03-23 Sebastien Pouliot <sebastien@ximian.com>
* CryptoConvertTest.cs: Added test case where the public key structure
is inside preceded by an header (like the one produced by "sn -e").
2004-03-23 Sebastien Pouliot <sebastien@ximian.com>
* CryptoConvertTest.cs: New. Unit tests for CryptoConvert.
2004-02-13 Sebastien Pouliot <sebastien@ximian.com>
* DiffieHellmanManagedTest.cs: New. Basic unit test for DH.
2004-02-07 Sebastien Pouliot <sebastien@ximian.com>
* KeyPairPersistenceTest.cs: New. Unit tests for key pair persistence.
2003-11-27 Sebastien Pouliot <spouliot@videotron.ca>
* MD4Test.cs: New. Contains all the test vectors provided in RFC1320
- but none are executed because MD4 is an abstract class.
* MD4ManagedTest.cs: New. Run the unit tests from MD4Test.cs using
the MD4Managed implementation of MD4.
* PKCS8Test.cs: New. Unit tests for PKCS8.
2003-03-07 Sebastien Pouliot <spouliot@videotron.ca>
* ARC4ManagedTest.cs: New. Test vectors for RC4(tm) taken from
usenet and Netscape SSL version 3 implementation details.
* MD2ManagedTest.cs: New. Call tests defined in MD2Test class.
* MD2Test.cs: New. Test vectors defined in RFC1319.

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,78 @@
//
// DiffieHellmanManagedTest.cs - NUnit Test Cases for DH (PKCS#3)
//
// Authors:
// Pieter Philippaerts (Pieter@mentalis.org)
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 The Mentalis.org Team (http://www.mentalis.org/)
// (C) 2004 Novell (http://www.novell.com)
//
using NUnit.Framework;
using System;
using System.IO;
using Mono.Security.Cryptography;
using System.Text;
namespace MonoTests.Mono.Security.Cryptography {
// References:
// a. PKCS #3: Diffie-Hellman Key-Agreement Standard (version 1.4)
// ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-3.asc
// b. Diffie-Hellman Key Agreement Method
// http://www.ietf.org/rfc/rfc2631.txt
[TestFixture]
public class DiffieHellmanManagedTest : Assertion {
// because most crypto stuff works with byte[] buffers
static public void AssertEquals (string msg, byte[] array1, byte[] array2)
{
if ((array1 == null) && (array2 == null))
return;
if (array1 == null)
Assertion.Fail (msg + " -> First array is NULL");
if (array2 == null)
Assertion.Fail (msg + " -> Second array is NULL");
bool a = (array1.Length == array2.Length);
if (a) {
for (int i = 0; i < array1.Length; i++) {
if (array1 [i] != array2 [i]) {
a = false;
break;
}
}
}
if (array1.Length > 0) {
msg += " -> Expected " + BitConverter.ToString (array1, 0);
msg += " is different than " + BitConverter.ToString (array2, 0);
}
Assertion.Assert (msg, a);
}
[Test]
public void KeyExchange ()
{
// create a new DH instance
DiffieHellman dh1 = new DiffieHellmanManaged ();
// export the public parameters of the first DH instance
DHParameters dhp = dh1.ExportParameters (false);
// create a second DH instance and initialize it with the public parameters of the first instance
DiffieHellman dh2 = new DiffieHellmanManaged (dhp.P, dhp.G, 160);
// generate the public key of the first DH instance
byte[] ke1 = dh1.CreateKeyExchange ();
// generate the public key of the second DH instance
byte[] ke2 = dh2.CreateKeyExchange ();
// let the first DH instance compute the shared secret using the second DH public key
byte[] dh1k = dh1.DecryptKeyExchange (ke2);
// let the second DH instance compute the shared secret using the first DH public key
byte[] dh2k = dh2.DecryptKeyExchange (ke1);
// both shared secrets are the same
AssertEquals ("Shared Secret", dh1k, dh2k);
}
// TODO: More is needed !
}
}

View File

@@ -0,0 +1,247 @@
//
// KeyPairPersistenceTest.cs: Unit tests for keypair persistence
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using NUnit.Framework;
using System;
using System.IO;
using System.Security.Cryptography;
using Mono.Security.Cryptography;
namespace MonoTests.Mono.Security.Cryptography {
[TestFixture]
public class KeyPairPersistenceTest {
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Constructor_Null ()
{
KeyPairPersistence kpp = new KeyPairPersistence (null);
}
private void Compare (KeyPairPersistence saved, KeyPairPersistence loaded)
{
// Note: there is an additional Environement.NewLine
// at the end of the loaded string - that's why we do
// not use AssertEquals (for strings)
Assert.IsTrue (loaded.Filename.StartsWith (saved.Filename), "Filename");
Assert.IsTrue (loaded.KeyValue.StartsWith (saved.KeyValue), "KeyValue");
Assert.IsTrue (loaded.Parameters.KeyContainerName.StartsWith (saved.Parameters.KeyContainerName), "Parameters.KeyContainerName");
Assert.AreEqual (saved.Parameters.KeyNumber, loaded.Parameters.KeyNumber, "Parameters.KeyNumber");
Assert.IsTrue (loaded.Parameters.ProviderName.StartsWith (saved.Parameters.ProviderName), "Parameters.ProviderName");
Assert.AreEqual (saved.Parameters.ProviderType, loaded.Parameters.ProviderType, "Parameters.ProviderType");
}
[Test]
public void CspType ()
{
try {
CspParameters cp = new CspParameters (-1);
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
// we didn't supply a name so we can't load it back
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspTypeProvider ()
{
try {
CspParameters cp = new CspParameters (-2, "Provider");
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
// we didn't supply a name so we can't load it back
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspTypeProviderContainer ()
{
try {
CspParameters cp = new CspParameters (-3, "Provider", "Container");
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspTypeProviderContainerKeyNumber ()
{
try {
CspParameters cp = new CspParameters (-4, "Provider", "Container");
cp.KeyNumber = 0;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspFlagsDefault ()
{
try {
CspParameters cp = new CspParameters (-5, "Provider", "Container");
cp.Flags = CspProviderFlags.UseDefaultKeyContainer;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspFlagsMachine ()
{
try {
CspParameters cp = new CspParameters (-6, "Provider", "Container");
cp.Flags = CspProviderFlags.UseMachineKeyStore;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (CryptographicException ce) {
// not everyone can write to the machine store
if (!(ce.InnerException is UnauthorizedAccessException))
throw;
Assert.Ignore ("Access denied to key containers files.");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspFlagsDefaultMachine ()
{
try {
CspParameters cp = new CspParameters (-7, "Provider", "Container");
cp.Flags = CspProviderFlags.UseDefaultKeyContainer | CspProviderFlags.UseMachineKeyStore;
KeyPairPersistence kpp = new KeyPairPersistence (cp, "<keypair/>");
kpp.Save ();
Assert.IsTrue (File.Exists (kpp.Filename), "Save-Exists");
KeyPairPersistence kpp2 = new KeyPairPersistence (cp);
Assert.IsTrue (kpp2.Load (), "Load");
Compare (kpp, kpp2);
kpp.Remove ();
Assert.IsFalse (File.Exists (kpp.Filename), "Remove-!Exists");
}
catch (CryptographicException ce) {
// not everyone can write to the machine store
if (!(ce.InnerException is UnauthorizedAccessException))
throw;
Assert.Ignore ("Access denied to key containers files.");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
[Test]
public void CspNoChangesPermitted ()
{
try {
CspParameters cp = new CspParameters (-8, "Provider", "Container");
cp.KeyNumber = 0;
cp.Flags = CspProviderFlags.UseMachineKeyStore;
KeyPairPersistence kpp = new KeyPairPersistence (cp);
CspParameters copy = kpp.Parameters;
copy.Flags = CspProviderFlags.UseDefaultKeyContainer;
copy.KeyContainerName = "NewContainerName";
copy.KeyNumber = 1;
copy.ProviderName = "NewProviderName";
copy.ProviderType = -9;
Assert.IsTrue (cp.Flags != copy.Flags, "Flags");
Assert.IsTrue (cp.KeyContainerName != copy.KeyContainerName, "KeyContainerName");
Assert.IsTrue (cp.KeyNumber != copy.KeyNumber, "KeyNumber");
Assert.IsTrue (cp.ProviderName != copy.ProviderName, "ProviderName");
Assert.IsTrue (cp.ProviderType != copy.ProviderType, "ProviderType");
}
catch (UnauthorizedAccessException) {
Assert.Ignore ("Access denied to key containers files.");
}
}
}
}

Some files were not shown because too many files have changed in this diff Show More