2002-10-14 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / PKCS1MaskGenerationMethodTest.cs
1 //
2 // PKCS1MaskGenerationMethodTest.cs - NUnit Test Cases for PKCS1MaskGenerationMethod
3 //
4 // Author:
5 //      Sebastien Pouliot (spouliot@motus.com)
6 //
7 // (C) 2002 Motus Technologies Inc. (http://www.motus.com)
8 //
9
10 using NUnit.Framework;
11 using System;
12 using System.Security.Cryptography;
13 using System.Text;
14
15 namespace MonoTests.System.Security.Cryptography
16 {
17
18 public class PKCS1MaskGenerationMethodTest : TestCase 
19 {
20         public PKCS1MaskGenerationMethodTest () : base ("System.Security.Cryptography.PKCS1MaskGenerationMethod testsuite") {}
21         public PKCS1MaskGenerationMethodTest (string name) : base(name) {}
22
23         protected PKCS1MaskGenerationMethod pkcs1;
24
25         protected override void SetUp () 
26         {
27                 pkcs1 = new PKCS1MaskGenerationMethod();
28         }
29
30         protected override void TearDown () {}
31
32         public static ITest Suite 
33         {
34                 get { 
35                         return new TestSuite (typeof (PKCS1MaskGenerationMethodTest)); 
36                 }
37         }
38
39         public void AssertEquals (string msg, byte[] array1, byte[] array2)
40         {
41                 AllTests.AssertEquals (msg, array1, array2);
42         }
43
44         // test part of PKCS#1 v.2.1 test vector
45         public void TestPKCS1v21TestVector ()
46         {
47                 pkcs1.HashName = "SHA1";
48                 
49                 // seed     = random string of octets (well not random in the tests ;-)
50                 byte[] seed = { 0xaa, 0xfd, 0x12, 0xf6, 0x59, 0xca, 0xe6, 0x34, 0x89, 0xb4, 0x79, 0xe5, 0x07, 0x6d, 0xde, 0xc2, 0xf0, 0x6c, 0xb5, 0x8f };
51                 int LengthDB = 107;
52
53                 // dbMask     = MGF(seed, length(DB))
54                 byte[] dbMask = pkcs1.GenerateMask (seed, LengthDB);
55                 byte[] expectedDBMask =  { 0x06, 0xe1, 0xde, 0xb2, 0x36, 0x9a, 0xa5, 0xa5, 0xc7, 0x07, 0xd8, 0x2c, 0x8e, 0x4e, 0x93, 0x24, 
56                         0x8a, 0xc7, 0x83, 0xde, 0xe0, 0xb2, 0xc0, 0x46, 0x26, 0xf5, 0xaf, 0xf9, 0x3e, 0xdc, 0xfb, 0x25,
57                         0xc9, 0xc2, 0xb3, 0xff, 0x8a, 0xe1, 0x0e, 0x83, 0x9a, 0x2d, 0xdb, 0x4c, 0xdc, 0xfe, 0x4f, 0xf4, 
58                         0x77, 0x28, 0xb4, 0xa1, 0xb7, 0xc1, 0x36, 0x2b, 0xaa, 0xd2, 0x9a, 0xb4, 0x8d, 0x28, 0x69, 0xd5, 
59                         0x02, 0x41, 0x21, 0x43, 0x58, 0x11, 0x59, 0x1b, 0xe3, 0x92, 0xf9, 0x82, 0xfb, 0x3e, 0x87, 0xd0, 
60                         0x95, 0xae, 0xb4, 0x04, 0x48, 0xdb, 0x97, 0x2f, 0x3a, 0xc1, 0x4e, 0xaf, 0xf4, 0x9c, 0x8c, 0x3b,
61                         0x7c, 0xfc, 0x95, 0x1a, 0x51, 0xec, 0xd1, 0xdd, 0xe6, 0x12, 0x64 };;
62                 AssertEquals ("PKCS1MaskGenerationMethod 1", expectedDBMask, dbMask);
63
64                 // maskedDB   = DB xor dbMask
65                 byte[] DB =  { 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d, 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
66                         0xaf, 0xd8, 0x07, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
68                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
69                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
70                         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xd4, 0x36, 0xe9, 0x95, 0x69,
71                         0xfd, 0x32, 0xa7, 0xc8, 0xa0, 0x5b, 0xbc, 0x90, 0xd3, 0x2c, 0x49 };
72                 byte[] maskedDB = new byte [dbMask.Length];
73                 for (int i = 0; i < dbMask.Length; i++)
74                         maskedDB [i] = Convert.ToByte (DB [i] ^ dbMask [i]);
75
76                 // seedMask   = MGF(maskedDB, length(seed))
77                 byte[] seedMask = pkcs1.GenerateMask (maskedDB, seed.Length);
78                 byte[] expectedSeedMask = { 0x41, 0x87, 0x0b, 0x5a, 0xb0, 0x29, 0xe6, 0x57, 0xd9, 0x57, 0x50, 0xb5, 0x4c, 0x28, 0x3c, 0x08, 0x72, 0x5d, 0xbe, 0xa9 };
79                 AssertEquals ("PKCS1MaskGenerationMethod 2", expectedSeedMask, seedMask);
80         }
81
82 }
83
84 }
85
86