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

View File

@@ -0,0 +1,285 @@
//
// Aes(CryptoServiceProvider) CFB Unit Tests
//
// Author:
// Sebastien Pouliot <sebastien@xamarin.com>
//
// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.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 System;
using System.Collections.Generic;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class AesCfbTests : CfbTests {
protected override SymmetricAlgorithm GetInstance ()
{
return Aes.Create ();
}
[Test]
public void Roundtrip ()
{
// that will return a AesCryptoServiceProvider
var aes = GetInstance ();
#if MOBILE
Assert.AreEqual ("System.Security.Cryptography.AesManaged", aes.ToString (), "Default");
Assert.AreEqual (128, aes.FeedbackSize, "FeedbackSize");
#else
Assert.AreEqual ("System.Security.Cryptography.AesCryptoServiceProvider", aes.ToString (), "Default");
Assert.AreEqual (8, aes.FeedbackSize, "FeedbackSize");
#endif
ProcessBlockSizes (aes);
}
// AesCryptoServiceProvider is not *Limited* since it supports CFB8-64
// but like all *CryptoServiceProvider implementations it refuse Padding.None
static PaddingMode[] csp_padding_modes = new [] { PaddingMode.PKCS7, PaddingMode.Zeros, PaddingMode.ANSIX923, PaddingMode.ISO10126 };
protected override PaddingMode [] PaddingModes {
get { return csp_padding_modes; }
}
protected override void CFB (SymmetricAlgorithm algo)
{
algo.Mode = CipherMode.CFB;
// limited from 8-64 bits (RijndaelManaged goes to blocksize - but is incompatible)
for (int i = 8; i <= 64; i += 8) {
algo.FeedbackSize = i;
CFB (algo, i);
}
}
protected override string GetExpectedResult (SymmetricAlgorithm algo, byte [] encryptedData)
{
#if false
return base.GetExpectedResult (algo, encryptedData);
#else
return test_vectors [GetId (algo)];
#endif
}
static Dictionary<int, string> test_vectors = new Dictionary<int, string> () {
// padding None : The input data is not a complete block.
// block size: 128, key size: 128, padding: PKCS7, feedback: 8
{ -2139094520, "99-69-66-99-00-71-BD-07-C1-51-7A-60-DD-3C-03-A6" },
// block size: 128, key size: 128, padding: PKCS7, feedback: 16
{ -2139094512, "99-98-A2-5F-57-12-44-8B-38-01-A6-01-AD-0B-B8-59" },
// block size: 128, key size: 128, padding: PKCS7, feedback: 24
{ -2139094504, "99-98-51-14-92-57-8A-B9-F5-B7-3D-CC-C9-C0-1D-0E" },
// block size: 128, key size: 128, padding: PKCS7, feedback: 32
{ -2139094496, "99-98-51-E5-E2-1D-47-05-A5-A6-5C-A4-FF-EC-30-E5" },
// block size: 128, key size: 128, padding: PKCS7, feedback: 40
{ -2139094488, "99-98-51-E5-15-6E-C4-AE-D1-53-58-D1-D7-68-EA-FD" },
// block size: 128, key size: 128, padding: PKCS7, feedback: 48
{ -2139094480, "99-98-51-E5-15-9F-DC-EE-C9-47-73-AB-74-B8-32-08" },
// block size: 128, key size: 128, padding: PKCS7, feedback: 56
{ -2139094472, "99-98-51-E5-15-9F-2F-57-BD-43-B4-63-71-18-39-B6" },
// block size: 128, key size: 128, padding: PKCS7, feedback: 64
{ -2139094464, "99-98-51-E5-15-9F-2F-A6-09-DC-C8-55-6F-EA-FC-21" },
// block size: 128, key size: 128, padding: Zeros, feedback: 8
{ -2139094264, "99-66-48-F8-D6-AB-E6-A6-A6-D8-B3-32-66-2F-44-6E" },
// block size: 128, key size: 128, padding: Zeros, feedback: 16
{ -2139094256, "99-98-AC-6E-83-98-A2-FF-4B-20-E9-79-31-9A-66-81" },
// block size: 128, key size: 128, padding: Zeros, feedback: 24
{ -2139094248, "99-98-51-19-42-6B-6F-34-29-48-1C-F4-2D-66-2E-48" },
// block size: 128, key size: 128, padding: Zeros, feedback: 32
{ -2139094240, "99-98-51-E5-EE-9B-3F-56-C7-47-3A-E5-5A-2D-6E-2E" },
// block size: 128, key size: 128, padding: Zeros, feedback: 40
{ -2139094232, "99-98-51-E5-15-65-32-85-14-5A-C6-74-13-0A-F5-DC" },
// block size: 128, key size: 128, padding: Zeros, feedback: 48
{ -2139094224, "99-98-51-E5-15-9F-D6-2F-2D-31-57-7C-AC-8C-53-B9" },
// block size: 128, key size: 128, padding: Zeros, feedback: 56
{ -2139094216, "99-98-51-E5-15-9F-2F-5E-F4-F8-DF-50-AE-70-DB-E9" },
// block size: 128, key size: 128, padding: Zeros, feedback: 64
{ -2139094208, "99-98-51-E5-15-9F-2F-A6-01-E3-E0-EB-87-35-30-74" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 8
{ -2139094008, "99-66-48-F8-D6-AB-E6-A6-A6-D8-B3-32-66-2F-44-61" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 16
{ -2139094000, "99-98-AC-6E-83-98-A2-FF-4B-20-E9-79-31-9A-66-8F" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 24
{ -2139093992, "99-98-51-19-42-6B-6F-34-29-48-1C-F4-2D-66-2E-45" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 32
{ -2139093984, "99-98-51-E5-EE-9B-3F-56-C7-47-3A-E5-5A-2D-6E-22" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 40
{ -2139093976, "99-98-51-E5-15-65-32-85-14-5A-C6-74-13-0A-F5-D7" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 48
{ -2139093968, "99-98-51-E5-15-9F-D6-2F-2D-31-57-7C-AC-8C-53-B3" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 56
{ -2139093960, "99-98-51-E5-15-9F-2F-5E-F4-F8-DF-50-AE-70-DB-E0" },
// block size: 128, key size: 128, padding: ANSIX923, feedback: 64
{ -2139093952, "99-98-51-E5-15-9F-2F-A6-01-E3-E0-EB-87-35-30-7C" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 8
{ -2139093752, "99-1B-4F-28-42-3F-FE-50-C4-1A-E3-27-7A-BF-95-EB" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 16
{ -2139093744, "99-98-E4-AE-6B-9D-EC-6A-4E-52-E9-60-30-26-E0-01" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 24
{ -2139093736, "99-98-51-BF-A0-E9-53-CD-4E-50-35-A3-73-48-F1-E1" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 32
{ -2139093728, "99-98-51-E5-73-AA-BD-FC-D8-28-E0-5D-CB-B5-3C-70" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 40
{ -2139093720, "99-98-51-E5-15-DF-BF-29-0B-30-44-52-B6-FD-5E-66" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 48
{ -2139093712, "99-98-51-E5-15-9F-54-26-F7-10-58-54-5A-EB-6D-07" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 56
{ -2139093704, "99-98-51-E5-15-9F-2F-6D-F7-54-EC-5E-63-DE-42-4F" },
// block size: 128, key size: 128, padding: ISO10126, feedback: 64
{ -2139093696, "99-98-51-E5-15-9F-2F-A6-7B-00-DA-C3-BC-C3-79-96" },
// padding None : The input data is not a complete block.
// block size: 128, key size: 192, padding: PKCS7, feedback: 8
{ -2134900216, "55-1E-15-41-27-60-35-C7-73-7F-23-4F-75-0E-AF-FB" },
// block size: 128, key size: 192, padding: PKCS7, feedback: 16
{ -2134900208, "55-EF-7E-C3-9E-85-57-7B-10-47-8C-CB-89-2E-47-76" },
// block size: 128, key size: 192, padding: PKCS7, feedback: 24
{ -2134900200, "55-EF-8D-2B-1A-10-5E-6A-D8-D0-93-61-3D-47-E4-E4" },
// block size: 128, key size: 192, padding: PKCS7, feedback: 32
{ -2134900192, "55-EF-8D-DA-C8-DB-63-C4-44-4F-F4-B9-D3-D6-49-FE" },
// block size: 128, key size: 192, padding: PKCS7, feedback: 40
{ -2134900184, "55-EF-8D-DA-3F-66-3B-8F-9E-56-CE-FB-9D-1C-D2-57" },
// block size: 128, key size: 192, padding: PKCS7, feedback: 48
{ -2134900176, "55-EF-8D-DA-3F-97-F7-50-D1-C0-8C-1C-8A-23-72-80" },
// block size: 128, key size: 192, padding: PKCS7, feedback: 56
{ -2134900168, "55-EF-8D-DA-3F-97-04-72-4C-8B-56-79-92-0E-DD-64" },
// block size: 128, key size: 192, padding: PKCS7, feedback: 64
{ -2134900160, "55-EF-8D-DA-3F-97-04-83-3B-83-33-8C-CD-B0-D3-F9" },
// block size: 128, key size: 192, padding: Zeros, feedback: 8
{ -2134899960, "55-11-47-22-49-48-50-3E-D1-F9-E7-86-20-CC-0A-97" },
// block size: 128, key size: 192, padding: Zeros, feedback: 16
{ -2134899952, "55-EF-70-F3-BB-AA-FD-C8-A7-86-43-2C-4E-95-99-43" },
// block size: 128, key size: 192, padding: Zeros, feedback: 24
{ -2134899944, "55-EF-8D-26-F9-A2-3A-A2-E8-AD-93-34-53-56-B5-54" },
// block size: 128, key size: 192, padding: Zeros, feedback: 32
{ -2134899936, "55-EF-8D-DA-C4-D3-27-45-07-BF-C8-0E-EB-06-10-86" },
// block size: 128, key size: 192, padding: Zeros, feedback: 40
{ -2134899928, "55-EF-8D-DA-3F-6D-6A-BB-66-D5-AF-2E-C8-DF-BC-19" },
// block size: 128, key size: 192, padding: Zeros, feedback: 48
{ -2134899920, "55-EF-8D-DA-3F-97-FD-FA-4D-76-73-78-F6-6D-8F-0B" },
// block size: 128, key size: 192, padding: Zeros, feedback: 56
{ -2134899912, "55-EF-8D-DA-3F-97-04-7B-C7-27-61-14-44-3E-2A-88" },
// block size: 128, key size: 192, padding: Zeros, feedback: 64
{ -2134899904, "55-EF-8D-DA-3F-97-04-83-33-87-1A-F8-6A-27-BA-60" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 8
{ -2134899704, "55-11-47-22-49-48-50-3E-D1-F9-E7-86-20-CC-0A-98" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 16
{ -2134899696, "55-EF-70-F3-BB-AA-FD-C8-A7-86-43-2C-4E-95-99-4D" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 24
{ -2134899688, "55-EF-8D-26-F9-A2-3A-A2-E8-AD-93-34-53-56-B5-59" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 32
{ -2134899680, "55-EF-8D-DA-C4-D3-27-45-07-BF-C8-0E-EB-06-10-8A" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 40
{ -2134899672, "55-EF-8D-DA-3F-6D-6A-BB-66-D5-AF-2E-C8-DF-BC-12" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 48
{ -2134899664, "55-EF-8D-DA-3F-97-FD-FA-4D-76-73-78-F6-6D-8F-01" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 56
{ -2134899656, "55-EF-8D-DA-3F-97-04-7B-C7-27-61-14-44-3E-2A-81" },
// block size: 128, key size: 192, padding: ANSIX923, feedback: 64
{ -2134899648, "55-EF-8D-DA-3F-97-04-83-33-87-1A-F8-6A-27-BA-68" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 8
{ -2134899448, "55-E6-52-56-F3-5C-82-2E-04-E8-9C-72-F5-56-61-C2" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 16
{ -2134899440, "55-EF-58-DB-49-72-12-E1-2D-B2-B7-33-B3-92-76-91" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 24
{ -2134899432, "55-EF-8D-55-FF-7B-89-F7-B9-22-76-47-D8-BA-52-D7" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 32
{ -2134899424, "55-EF-8D-DA-B1-B7-68-3A-54-47-71-4D-43-48-C2-50" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 40
{ -2134899416, "55-EF-8D-DA-3F-84-6C-2C-98-E7-AE-B6-C2-97-1C-7E" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 48
{ -2134899408, "55-EF-8D-DA-3F-97-C3-8F-63-2D-6B-B3-86-D2-61-85" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 56
{ -2134899400, "55-EF-8D-DA-3F-97-04-2D-7B-E6-5A-5B-10-5F-B5-9E" },
// block size: 128, key size: 192, padding: ISO10126, feedback: 64
{ -2134899392, "55-EF-8D-DA-3F-97-04-83-95-74-A3-86-78-66-13-3A" },
// padding None : The input data is not a complete block.
// block size: 128, key size: 256, padding: PKCS7, feedback: 8
{ -2130705912, "23-D9-77-80-5B-FA-F1-6D-6D-39-98-60-DF-75-DF-49" },
// block size: 128, key size: 256, padding: PKCS7, feedback: 16
{ -2130705904, "23-28-07-BE-7A-18-9F-BC-B1-4D-F5-65-4B-5B-AD-D5" },
// block size: 128, key size: 256, padding: PKCS7, feedback: 24
{ -2130705896, "23-28-F4-C9-30-DA-57-28-5F-8F-9E-BF-05-DF-D9-26" },
// block size: 128, key size: 256, padding: PKCS7, feedback: 32
{ -2130705888, "23-28-F4-38-83-DF-89-E9-C9-5C-87-D5-FA-19-56-54" },
// block size: 128, key size: 256, padding: PKCS7, feedback: 40
{ -2130705880, "23-28-F4-38-74-E0-5C-D0-D1-05-5D-42-AA-FC-2F-EF" },
// block size: 128, key size: 256, padding: PKCS7, feedback: 48
{ -2130705872, "23-28-F4-38-74-11-19-31-30-84-5D-FB-BE-69-BB-98" },
// block size: 128, key size: 256, padding: PKCS7, feedback: 56
{ -2130705864, "23-28-F4-38-74-11-EA-29-1F-A5-02-D5-AA-78-4C-E8" },
// block size: 128, key size: 256, padding: PKCS7, feedback: 64
{ -2130705856, "23-28-F4-38-74-11-EA-D8-68-29-E3-14-6B-BF-C4-2D" },
// block size: 128, key size: 256, padding: Zeros, feedback: 8
{ -2130705656, "23-D6-E9-75-83-FA-22-B3-96-27-CF-6D-BE-23-A4-D0" },
// block size: 128, key size: 256, padding: Zeros, feedback: 16
{ -2130705648, "23-28-09-B9-8E-0B-01-57-EE-D8-4F-44-69-F0-8A-28" },
// block size: 128, key size: 256, padding: Zeros, feedback: 24
{ -2130705640, "23-28-F4-C4-B7-B5-79-63-F0-CD-35-C6-39-3B-4D-02" },
// block size: 128, key size: 256, padding: Zeros, feedback: 32
{ -2130705632, "23-28-F4-38-8F-78-58-EE-93-06-FA-CA-21-64-70-96" },
// block size: 128, key size: 256, padding: Zeros, feedback: 40
{ -2130705624, "23-28-F4-38-74-EB-52-74-A3-80-87-48-3D-18-76-19" },
// block size: 128, key size: 256, padding: Zeros, feedback: 48
{ -2130705616, "23-28-F4-38-74-11-13-09-09-B5-B9-95-A9-FF-02-EE" },
// block size: 128, key size: 256, padding: Zeros, feedback: 56
{ -2130705608, "23-28-F4-38-74-11-EA-20-2A-87-0E-39-29-3A-84-A6" },
// block size: 128, key size: 256, padding: Zeros, feedback: 64
{ -2130705600, "23-28-F4-38-74-11-EA-D8-60-04-E9-5D-7A-C1-A8-85" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 8
{ -2130705400, "23-D6-E9-75-83-FA-22-B3-96-27-CF-6D-BE-23-A4-DF" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 16
{ -2130705392, "23-28-09-B9-8E-0B-01-57-EE-D8-4F-44-69-F0-8A-26" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 24
{ -2130705384, "23-28-F4-C4-B7-B5-79-63-F0-CD-35-C6-39-3B-4D-0F" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 32
{ -2130705376, "23-28-F4-38-8F-78-58-EE-93-06-FA-CA-21-64-70-9A" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 40
{ -2130705368, "23-28-F4-38-74-EB-52-74-A3-80-87-48-3D-18-76-12" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 48
{ -2130705360, "23-28-F4-38-74-11-13-09-09-B5-B9-95-A9-FF-02-E4" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 56
{ -2130705352, "23-28-F4-38-74-11-EA-20-2A-87-0E-39-29-3A-84-AF" },
// block size: 128, key size: 256, padding: ANSIX923, feedback: 64
{ -2130705344, "23-28-F4-38-74-11-EA-D8-60-04-E9-5D-7A-C1-A8-8D" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 8
{ -2130705144, "23-33-4F-8B-09-74-D9-8F-1F-78-F5-BD-31-C3-02-19" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 16
{ -2130705136, "23-28-33-EE-86-CE-4B-89-A0-DE-8F-10-4E-4D-27-86" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 24
{ -2130705128, "23-28-F4-44-74-47-57-7E-18-29-5B-3B-CB-64-3E-F9" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 32
{ -2130705120, "23-28-F4-38-09-D6-8E-A7-CE-40-BA-83-6D-5D-E0-7D" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 40
{ -2130705112, "23-28-F4-38-74-AD-A8-85-C7-78-BB-15-9E-39-32-14" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 48
{ -2130705104, "23-28-F4-38-74-11-95-73-21-90-F4-B8-E0-DB-5D-6B" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 56
{ -2130705096, "23-28-F4-38-74-11-EA-F8-5D-72-DE-4D-9E-75-5F-75" },
// block size: 128, key size: 256, padding: ISO10126, feedback: 64
{ -2130705088, "23-28-F4-38-74-11-EA-D8-8A-E6-AB-F8-FD-8C-8B-19" },
};
}
}

View File

@@ -0,0 +1,170 @@
//
// TestSuite.System.Security.Cryptography.AllTests.cs
//
// Authors:
// Thomas Neidhart (tome@sbox.tugraz.at)
// Sebastien Pouliot (sebastien@ximian.com)
//
// Portions (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004 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 System;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
public class AllTests {
// 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);
}
public static byte[] dsaG = { 0x3D, 0x4F, 0xCC, 0x78, 0x6A, 0x7A, 0x23, 0xF1, 0x41, 0x76, 0xEB, 0xB0, 0x0F, 0xD5, 0x0F, 0x78, 0x21, 0x73, 0x83, 0xC2, 0x1B, 0xF4, 0x7E, 0x68, 0xB2, 0x4B,
0xB9, 0x9D, 0x3F, 0x00, 0x84, 0x74, 0xD8, 0xF3, 0x31, 0xAA, 0x7A, 0xDB, 0xAF, 0x1E, 0x1C, 0x5B, 0x11, 0x2B, 0x78, 0x94, 0xBC, 0xFC, 0x69, 0x7C, 0xD7,
0xD2, 0x82, 0x22, 0x32, 0xCB, 0xAC, 0x06, 0xBE, 0x84, 0xFD, 0xA0, 0x5D, 0xA2, 0x80, 0x48, 0xDF, 0xA7, 0xC7, 0xC1, 0x6F, 0x16, 0x9D, 0xD0, 0xA6, 0xC2,
0x40, 0x75, 0x92, 0xFE, 0xE6, 0x85, 0x12, 0x01, 0xE3, 0x7D, 0x36, 0x52, 0xB7, 0xEF, 0x0B, 0x62, 0x96, 0x42, 0xE0, 0x72, 0xA5, 0xF9, 0x74, 0xFB, 0xE6,
0xB5, 0xC2, 0x10, 0x4C, 0x0A, 0x1E, 0x55, 0x4C, 0x96, 0x07, 0xD6, 0xC0, 0x64, 0x14, 0x86, 0x95, 0x35, 0xE5, 0x1B, 0xB7, 0x94, 0x3A, 0xFD, 0x47, 0x62,
0x65, 0x56 };
public static byte[] dsaJ = { 0x00, 0x00, 0x00, 0x01, 0x0E, 0x8B, 0x4A, 0xE4, 0x62, 0x61, 0xDB, 0x28, 0x71, 0x9E, 0xAB, 0x83, 0x2A, 0x88, 0x3A, 0x91, 0x35, 0xFE, 0xE0, 0x8E, 0xD4, 0xF7,
0xC4, 0x9F, 0xBE, 0x7C, 0x2E, 0x0D, 0x95, 0x5B, 0xA3, 0x87, 0x25, 0x36, 0x07, 0x34, 0x2D, 0xF4, 0xB3, 0x48, 0x12, 0x4C, 0x6F, 0xC9, 0xB7, 0x7A, 0x07,
0x61, 0x7F, 0x92, 0xF2, 0xFA, 0xED, 0x35, 0x86, 0xFC, 0x91, 0x12, 0x7F, 0x8A, 0x98, 0x1C, 0xA1, 0xF1, 0xAA, 0xD9, 0x0A, 0x3F, 0x71, 0x82, 0x8F, 0xED,
0x15, 0xF6, 0x2B, 0xA1, 0x09, 0xC1, 0x27, 0xD6, 0x22, 0x78, 0x6C, 0x34, 0xCA, 0xF5, 0x26, 0x6C, 0xEE, 0x2B, 0x49, 0x54, 0x2A, 0x45, 0xD7, 0x5D, 0x5A,
0x35, 0xE5, 0xF2, 0xFC, 0x3B, 0x6F, 0xF5, 0xEA, 0xE1, 0x16, 0x32 };
public static byte[] dsaP = { 0xB3, 0xF3, 0x9C, 0xD2, 0xDE, 0x20, 0x8F, 0x43, 0x51, 0xAA, 0x40, 0x8A, 0x8B, 0x8C, 0xA7, 0x26, 0x73, 0x80, 0x12, 0xE9, 0x0D, 0x86, 0x39, 0x07, 0x24, 0xF3,
0xB3, 0x36, 0xC1, 0xCF, 0xEB, 0xD9, 0x32, 0x1C, 0xC5, 0x30, 0x67, 0x70, 0x33, 0x3A, 0x42, 0xC6, 0x60, 0xE5, 0xA5, 0x3A, 0x8D, 0xB3, 0x95, 0x82, 0xB3,
0x49, 0xB6, 0x72, 0x80, 0x30, 0xC8, 0xE5, 0xD0, 0x96, 0x03, 0x98, 0x10, 0x17, 0x66, 0x46, 0x1C, 0x45, 0x25, 0x58, 0x93, 0x20, 0x53, 0x21, 0xE6, 0x93,
0x26, 0xCE, 0x8B, 0x8E, 0x1C, 0xC8, 0x76, 0x97, 0xF7, 0x27, 0x86, 0x7D, 0xB9, 0xE8, 0x73, 0x7C, 0x45, 0xF6, 0xC6, 0x20, 0x90, 0x0D, 0xEF, 0x3D, 0x46,
0x5A, 0x26, 0xE6, 0x0C, 0x3A, 0xC0, 0x69, 0xDE, 0x9C, 0x8F, 0x75, 0xE3, 0x7C, 0xFE, 0x62, 0xD4, 0xF2, 0x7D, 0x22, 0xBD, 0x44, 0x72, 0x3F, 0xED, 0xC6,
0x7B, 0xD3 };
public static byte[] dsaQ = { 0xAA, 0x47, 0x13, 0x5B, 0xE0, 0x9E, 0xD0, 0xBE, 0x64, 0xF0, 0xE1, 0x93, 0x50, 0xC9, 0x11, 0xA0, 0x62, 0x83, 0x73, 0x51 };
public static byte[] dsaX = { 0x7C, 0x03, 0xB2, 0xB5, 0x9B, 0x6D, 0x51, 0x91, 0x73, 0xB7, 0xF0, 0x2F, 0xC1, 0x18, 0xA7, 0x9A, 0xF6, 0x0A, 0x2F, 0xB1 };
public static byte[] dsaY = { 0xB2, 0x12, 0x51, 0x51, 0xD1, 0xB1, 0x11, 0x8C, 0x52, 0x28, 0xCE, 0x49, 0x56, 0x89, 0x7D, 0x1C, 0x07, 0x50, 0xC0, 0x82, 0xBE, 0xC0, 0x5F, 0x57, 0xE2, 0x7F,
0x52, 0x00, 0x3F, 0xBE, 0xBD, 0xF6, 0x4E, 0x30, 0xE6, 0x22, 0xF8, 0xCD, 0x72, 0xBA, 0xF4, 0x00, 0x95, 0x0B, 0xE7, 0x4A, 0x54, 0xD3, 0x5A, 0xBE, 0xC2,
0x64, 0xF4, 0x55, 0x00, 0x80, 0x8B, 0x80, 0x30, 0x5B, 0xAA, 0x2F, 0x62, 0x37, 0xE2, 0x1D, 0xEB, 0x47, 0x1C, 0x27, 0xE8, 0x70, 0xDE, 0x91, 0x83, 0x7C,
0xBA, 0x49, 0xEC, 0x87, 0x45, 0x3E, 0xCC, 0xC9, 0x11, 0xE5, 0x0E, 0xF9, 0x26, 0x41, 0xC1, 0xFF, 0x27, 0x8C, 0x65, 0xD4, 0x2E, 0xA4, 0x8E, 0x0E, 0xCF,
0xCD, 0x4D, 0xC8, 0xA2, 0x5F, 0xB1, 0x34, 0xEE, 0xB9, 0xBE, 0x29, 0xF9, 0x71, 0x14, 0xE8, 0x34, 0xAA, 0xD5, 0x80, 0x86, 0x66, 0x0B, 0xC4, 0x89, 0x03,
0xC7, 0xBB };
public static int dsaCounter = 224;
public static byte[] dsaSeed = { 0xB9, 0x83, 0x39, 0x6F, 0x6D, 0x25, 0xBA, 0xF6, 0xEE, 0xC9, 0xEB, 0xE2, 0xF5, 0x35, 0xC7, 0xC1, 0xE9, 0x1B, 0xAF, 0x9B };
static public DSAParameters GetKey (bool includePrivateKey)
{
DSAParameters p = new DSAParameters();
p.Counter = dsaCounter;
p.G = (byte[]) dsaG.Clone ();
p.J = (byte[]) dsaJ.Clone ();
p.P = (byte[]) dsaP.Clone ();
p.Q = (byte[]) dsaQ.Clone ();
p.Seed = (byte[]) dsaSeed.Clone ();
p.Y = (byte[]) dsaY.Clone ();
if (includePrivateKey)
p.X = (byte[]) dsaX.Clone ();
else
p.X = null;
return p;
}
static byte[] rsaModulus = { 0xbb, 0xf8, 0x2f, 0x09, 0x06, 0x82, 0xce, 0x9c, 0x23, 0x38, 0xac, 0x2b, 0x9d, 0xa8, 0x71, 0xf7,
0x36, 0x8d, 0x07, 0xee, 0xd4, 0x10, 0x43, 0xa4, 0x40, 0xd6, 0xb6, 0xf0, 0x74, 0x54, 0xf5, 0x1f,
0xb8, 0xdf, 0xba, 0xaf, 0x03, 0x5c, 0x02, 0xab, 0x61, 0xea, 0x48, 0xce, 0xeb, 0x6f, 0xcd, 0x48,
0x76, 0xed, 0x52, 0x0d, 0x60, 0xe1, 0xec, 0x46, 0x19, 0x71, 0x9d, 0x8a, 0x5b, 0x8b, 0x80, 0x7f,
0xaf, 0xb8, 0xe0, 0xa3, 0xdf, 0xc7, 0x37, 0x72, 0x3e, 0xe6, 0xb4, 0xb7, 0xd9, 0x3a, 0x25, 0x84,
0xee, 0x6a, 0x64, 0x9d, 0x06, 0x09, 0x53, 0x74, 0x88, 0x34, 0xb2, 0x45, 0x45, 0x98, 0x39, 0x4e,
0xe0, 0xaa, 0xb1, 0x2d, 0x7b, 0x61, 0xa5, 0x1f, 0x52, 0x7a, 0x9a, 0x41, 0xf6, 0xc1, 0x68, 0x7f,
0xe2, 0x53, 0x72, 0x98, 0xca, 0x2a, 0x8f, 0x59, 0x46, 0xf8, 0xe5, 0xfd, 0x09, 0x1d, 0xbd, 0xcb };
static byte[] rsaExponent = { 0x11 };
static byte[] rsaP = { 0xee, 0xcf, 0xae, 0x81, 0xb1, 0xb9, 0xb3, 0xc9, 0x08, 0x81, 0x0b, 0x10, 0xa1, 0xb5, 0x60, 0x01,
0x99, 0xeb, 0x9f, 0x44, 0xae, 0xf4, 0xfd, 0xa4, 0x93, 0xb8, 0x1a, 0x9e, 0x3d, 0x84, 0xf6, 0x32,
0x12, 0x4e, 0xf0, 0x23, 0x6e, 0x5d, 0x1e, 0x3b, 0x7e, 0x28, 0xfa, 0xe7, 0xaa, 0x04, 0x0a, 0x2d,
0x5b, 0x25, 0x21, 0x76, 0x45, 0x9d, 0x1f, 0x39, 0x75, 0x41, 0xba, 0x2a, 0x58, 0xfb, 0x65, 0x99 };
static byte[] rsaQ = { 0xc9, 0x7f, 0xb1, 0xf0, 0x27, 0xf4, 0x53, 0xf6, 0x34, 0x12, 0x33, 0xea, 0xaa, 0xd1, 0xd9, 0x35,
0x3f, 0x6c, 0x42, 0xd0, 0x88, 0x66, 0xb1, 0xd0, 0x5a, 0x0f, 0x20, 0x35, 0x02, 0x8b, 0x9d, 0x86,
0x98, 0x40, 0xb4, 0x16, 0x66, 0xb4, 0x2e, 0x92, 0xea, 0x0d, 0xa3, 0xb4, 0x32, 0x04, 0xb5, 0xcf,
0xce, 0x33, 0x52, 0x52, 0x4d, 0x04, 0x16, 0xa5, 0xa4, 0x41, 0xe7, 0x00, 0xaf, 0x46, 0x15, 0x03 };
static byte[] rsaDP = { 0x54, 0x49, 0x4c, 0xa6, 0x3e, 0xba, 0x03, 0x37, 0xe4, 0xe2, 0x40, 0x23, 0xfc, 0xd6, 0x9a, 0x5a,
0xeb, 0x07, 0xdd, 0xdc, 0x01, 0x83, 0xa4, 0xd0, 0xac, 0x9b, 0x54, 0xb0, 0x51, 0xf2, 0xb1, 0x3e,
0xd9, 0x49, 0x09, 0x75, 0xea, 0xb7, 0x74, 0x14, 0xff, 0x59, 0xc1, 0xf7, 0x69, 0x2e, 0x9a, 0x2e,
0x20, 0x2b, 0x38, 0xfc, 0x91, 0x0a, 0x47, 0x41, 0x74, 0xad, 0xc9, 0x3c, 0x1f, 0x67, 0xc9, 0x81 };
static byte[] rsaDQ = { 0x47, 0x1e, 0x02, 0x90, 0xff, 0x0a, 0xf0, 0x75, 0x03, 0x51, 0xb7, 0xf8, 0x78, 0x86, 0x4c, 0xa9,
0x61, 0xad, 0xbd, 0x3a, 0x8a, 0x7e, 0x99, 0x1c, 0x5c, 0x05, 0x56, 0xa9, 0x4c, 0x31, 0x46, 0xa7,
0xf9, 0x80, 0x3f, 0x8f, 0x6f, 0x8a, 0xe3, 0x42, 0xe9, 0x31, 0xfd, 0x8a, 0xe4, 0x7a, 0x22, 0x0d,
0x1b, 0x99, 0xa4, 0x95, 0x84, 0x98, 0x07, 0xfe, 0x39, 0xf9, 0x24, 0x5a, 0x98, 0x36, 0xda, 0x3d };
static byte[] rsaInverseQ = { 0xb0, 0x6c, 0x4f, 0xda, 0xbb, 0x63, 0x01, 0x19, 0x8d, 0x26, 0x5b, 0xdb, 0xae, 0x94, 0x23, 0xb3,
0x80, 0xf2, 0x71, 0xf7, 0x34, 0x53, 0x88, 0x50, 0x93, 0x07, 0x7f, 0xcd, 0x39, 0xe2, 0x11, 0x9f,
0xc9, 0x86, 0x32, 0x15, 0x4f, 0x58, 0x83, 0xb1, 0x67, 0xa9, 0x67, 0xbf, 0x40, 0x2b, 0x4e, 0x9e,
0x2e, 0x0f, 0x96, 0x56, 0xe6, 0x98, 0xea, 0x36, 0x66, 0xed, 0xfb, 0x25, 0x79, 0x80, 0x39, 0xf7 };
static byte[] rsaD = { 0xa5, 0xda, 0xfc, 0x53, 0x41, 0xfa, 0xf2, 0x89, 0xc4, 0xb9, 0x88, 0xdb, 0x30, 0xc1, 0xcd, 0xf8,
0x3f, 0x31, 0x25, 0x1e, 0x06, 0x68, 0xb4, 0x27, 0x84, 0x81, 0x38, 0x01, 0x57, 0x96, 0x41, 0xb2,
0x94, 0x10, 0xb3, 0xc7, 0x99, 0x8d, 0x6b, 0xc4, 0x65, 0x74, 0x5e, 0x5c, 0x39, 0x26, 0x69, 0xd6,
0x87, 0x0d, 0xa2, 0xc0, 0x82, 0xa9, 0x39, 0xe3, 0x7f, 0xdc, 0xb8, 0x2e, 0xc9, 0x3e, 0xda, 0xc9,
0x7f, 0xf3, 0xad, 0x59, 0x50, 0xac, 0xcf, 0xbc, 0x11, 0x1c, 0x76, 0xf1, 0xa9, 0x52, 0x94, 0x44,
0xe5, 0x6a, 0xaf, 0x68, 0xc5, 0x6c, 0x09, 0x2c, 0xd3, 0x8d, 0xc3, 0xbe, 0xf5, 0xd2, 0x0a, 0x93,
0x99, 0x26, 0xed, 0x4f, 0x74, 0xa1, 0x3e, 0xdd, 0xfb, 0xe1, 0xa1, 0xce, 0xcc, 0x48, 0x94, 0xaf,
0x94, 0x28, 0xc2, 0xb7, 0xb8, 0x88, 0x3f, 0xe4, 0x46, 0x3a, 0x4b, 0xc8, 0x5b, 0x1c, 0xb3, 0xc1 };
static public RSAParameters GetRsaKey (bool includePrivateKey)
{
RSAParameters p = new RSAParameters();
if (includePrivateKey) {
p.D = (byte[]) rsaD.Clone();
p.DP = (byte[]) rsaDP.Clone();
p.DQ = (byte[]) rsaDQ.Clone();
p.P = (byte[]) rsaP.Clone();
p.Q = (byte[]) rsaQ.Clone();
p.InverseQ = (byte[]) rsaInverseQ.Clone();
}
else {
p.D = null;
p.DP = null;
p.DQ = null;
p.P = null;
p.Q = null;
p.InverseQ = null;
}
p.Modulus = (byte[]) rsaModulus.Clone();
p.Exponent = (byte[]) rsaExponent.Clone();
return p;
}
}
}

View File

@@ -0,0 +1,40 @@
//
// TestSuite.System.Security.Cryptography.AsymmetricAlgorithmTest.cs
//
// Author:
// Thomas Neidhart (tome@sbox.tugraz.at)
//
using System;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class AsymmetricAlgorithmTest
{
private AsymmetricAlgorithm _algo;
[SetUp]
public void SetUp() {
_algo = AsymmetricAlgorithm.Create();
}
private void SetDefaultData() {
}
[Test]
public void TestProperties() {
Assert.IsNotNull(_algo, "Properties (1)");
KeySizes[] keys = _algo.LegalKeySizes;
foreach (KeySizes myKey in keys) {
for (int i = myKey.MinSize; i <= myKey.MaxSize; i += myKey.SkipSize) {
_algo.KeySize = i;
}
}
}
}
}

View File

@@ -0,0 +1,197 @@
//
// CFB Unit Tests
//
// Author:
// Sebastien Pouliot <sebastien@xamarin.com>
//
// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.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 System;
using System.Collections.Generic;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
public abstract class CfbTests {
protected abstract SymmetricAlgorithm GetInstance ();
protected void ProcessBlockSizes (SymmetricAlgorithm algo)
{
algo.Padding = PaddingMode.None;
foreach (KeySizes bs in algo.LegalBlockSizes) {
for (int blockSize = bs.MinSize; blockSize <= bs.MaxSize; blockSize += bs.SkipSize) {
algo.BlockSize = blockSize;
ProcessKeySizes (algo);
// SkipSize can be 0 (e.g. DES) if only one block size is available
if (blockSize == bs.MaxSize)
break;
}
}
}
protected void ProcessKeySizes (SymmetricAlgorithm algo)
{
foreach (KeySizes ks in algo.LegalKeySizes) {
for (int keySize = ks.MinSize; keySize <= ks.MaxSize; keySize += ks.SkipSize) {
algo.KeySize = keySize;
algo.Key = GetKey (algo);
algo.IV = new byte [algo.BlockSize / 8];
ProcessPadding (algo);
// SkipSize can be 0 (e.g. DES) if only one key size is available
if (keySize == ks.MaxSize)
break;
}
}
}
protected abstract PaddingMode[] PaddingModes { get; }
protected void ProcessPadding (SymmetricAlgorithm algo)
{
foreach (var padding in PaddingModes) {
algo.Padding = padding;
CFB (algo);
}
}
protected virtual byte [] GetKey (SymmetricAlgorithm algo)
{
return new byte [algo.KeySize / 8];
}
protected abstract void CFB (SymmetricAlgorithm algo);
protected int GetId (SymmetricAlgorithm algo)
{
return (algo.BlockSize << 24) + (algo.KeySize << 16) + ((int) algo.Padding << 8) + algo.FeedbackSize;
}
protected virtual string GetExpectedResult (SymmetricAlgorithm algo, byte [] encryptedData)
{
int id = GetId (algo);
string expected = BitConverter.ToString (encryptedData);
Console.WriteLine ("// block size: {0}, key size: {1}, padding: {2}, feedback: {3}", algo.BlockSize, algo.KeySize, algo.Padding, algo.FeedbackSize);
Console.WriteLine ("{{ {0}, \"{1}\" }},", id, expected);
return expected;
}
protected void CFB (SymmetricAlgorithm algo, int feedbackSize)
{
byte [] data = new byte [feedbackSize >> 3];
for (int i = 0; i < data.Length; i++)
data [i] = (byte) (0xff - i);
byte [] encdata = Encryptor (algo, data);
string expected = GetExpectedResult (algo, encdata);
string actual = null;
if (algo.Padding == PaddingMode.ISO10126) {
// ISO10126 uses random data so we can't compare the last bytes with a test vector
actual = BitConverter.ToString (encdata, 0, data.Length);
expected = expected.Substring (0, actual.Length);
} else {
actual = BitConverter.ToString (encdata);
}
Assert.AreEqual (expected, actual, "encrypted value");
byte [] decdata = Decryptor (algo, encdata);
if (algo.Padding == PaddingMode.Zeros) {
// this requires manually unpadding the decrypted data - but unlike ISO10126
// we know the rest of the data will be 0 (not random) so we check that
byte [] resize = new byte [data.Length];
Array.Copy (decdata, 0, resize, 0, resize.Length);
// all zeros afterward!
for (int i = resize.Length; i < decdata.Length; i++)
Assert.AreEqual (0, decdata [i], "padding zero {0}", i);
decdata = resize;
}
Assert.AreEqual (data, decdata, "Roundtrip {0} {1}", algo.Mode, algo.FeedbackSize);
}
protected virtual int GetTransformBlockSize (SymmetricAlgorithm algo)
{
return algo.BlockSize / 8;
}
byte [] Encryptor (SymmetricAlgorithm algo, byte [] data)
{
using (ICryptoTransform t = algo.CreateEncryptor (algo.Key, algo.IV)) {
int size = GetTransformBlockSize (algo);
Assert.That (t.InputBlockSize == size, "Encryptor InputBlockSize {0} {1}", algo.Mode, algo.FeedbackSize);
Assert.That (t.OutputBlockSize == size, "Encryptor OutputBlockSize {0} {1}", algo.Mode, algo.FeedbackSize);
return t.TransformFinalBlock (data, 0, data.Length);
}
}
byte [] Decryptor (SymmetricAlgorithm algo, byte [] encdata)
{
using (ICryptoTransform t = algo.CreateDecryptor (algo.Key, algo.IV)) {
int size = GetTransformBlockSize (algo);
Assert.That (t.InputBlockSize == size, "Decryptor InputBlockSize {0} {1}", algo.Mode, algo.FeedbackSize);
Assert.That (t.OutputBlockSize == size, "Decryptor OutputBlockSize {0} {1}", algo.Mode, algo.FeedbackSize);
return t.TransformFinalBlock (encdata, 0, encdata.Length);
}
}
}
// most algorithms are "limited" and only support CFB8
public abstract class LimitedCfbTests : CfbTests {
// all *CryptoServiceProvider implementation refuse Padding.None
static PaddingMode[] csp_padding_modes = new [] { PaddingMode.PKCS7, PaddingMode.Zeros, PaddingMode.ANSIX923, PaddingMode.ISO10126 };
protected override PaddingMode [] PaddingModes {
get { return csp_padding_modes; }
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Cfb_None ()
{
using (var algo = GetInstance ()) {
algo.Padding = PaddingMode.None;
CFB (algo, 8);
}
}
protected override void CFB (SymmetricAlgorithm algo)
{
algo.Mode = CipherMode.CFB;
// System.Security.Cryptography.CryptographicException : Feedback size for the cipher feedback mode (CFB) must be 8 bits.
algo.FeedbackSize = 8;
CFB (algo, algo.FeedbackSize);
}
}
// DES and 3DES won't accept a key with all zero (since it's a weak key for them)
public abstract class WeakKeyCfbTests : LimitedCfbTests {
protected override byte [] GetKey (SymmetricAlgorithm algo)
{
var key = base.GetKey (algo);
for (byte i = 0; i < key.Length; i++)
key [i] = i;
return key;
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,251 @@
//
// CipherModeTest.cs - NUnit Test Cases for CipherMode
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004 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 System;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class CipherModeTest {
// Enum tests
[Test]
public void CipherModeEnum ()
{
Assert.AreEqual (1, (int)CipherMode.CBC, "CBC");
Assert.AreEqual (4, (int)CipherMode.CFB, "CFB");
Assert.AreEqual (5, (int)CipherMode.CTS, "CTS");
Assert.AreEqual (2, (int)CipherMode.ECB, "ECB");
Assert.AreEqual (3, (int)CipherMode.OFB, "OFB");
}
// SymmetricAlgorithm tests
private byte[] GetKey (SymmetricAlgorithm sa)
{
byte[] key = new byte [sa.KeySize >> 3];
// no weak key this way (DES, TripleDES)
for (byte i = 0; i < key.Length; i++)
key [i] = i;
return key;
}
private byte[] GetIV (SymmetricAlgorithm sa)
{
return new byte [sa.BlockSize >> 3];
}
private string Roundtrip (SymmetricAlgorithm sa, CipherMode mode)
{
sa.Key = GetKey (sa);
sa.IV = GetIV (sa);
sa.Mode = mode;
// two full blocks
int bs = (sa.BlockSize >> 3) * 2;
byte[] data = new byte [bs]; // in bytes
ICryptoTransform enc = sa.CreateEncryptor ();
byte[] encdata = enc.TransformFinalBlock (data, 0, data.Length);
string result = BitConverter.ToString (encdata);
ICryptoTransform dec = sa.CreateDecryptor ();
byte[] decdata = dec.TransformFinalBlock (encdata, 0, encdata.Length);
for (int i = 0; i < bs; i++)
Assert.AreEqual (data [i], decdata [i], i.ToString ());
return result;
}
[Test]
public void DES_ECB ()
{
Assert.AreEqual ("A5-17-3A-D5-95-7B-43-70-A5-17-3A-D5-95-7B-43-70-E4-81-A8-D3-97-14-D0-DE",
Roundtrip (DES.Create (), CipherMode.ECB), "Encrypted data");
}
[Test]
public void DES_CBC ()
{
Assert.AreEqual ("A5-17-3A-D5-95-7B-43-70-79-6F-FD-B4-90-21-70-9D-FF-C8-76-01-24-7C-C3-82",
Roundtrip (DES.Create (), CipherMode.CBC), "Encrypted data");
}
[Test]
public void DES_CFB ()
{
Assert.AreEqual ("A5-AA-9B-16-02-77-16-A1-86-BC-38-B6-E5-BA-53-4C-A5-F9-49-21-A9-8E-84-A0",
Roundtrip (DES.Create (), CipherMode.CFB), "Encrypted data");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void DES_OFB ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (DES.Create (), CipherMode.OFB), "Encrypted data");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void DES_CTS ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (DES.Create (), CipherMode.CTS), "Encrypted data");
}
[Test]
public void RC2_ECB ()
{
Assert.AreEqual ("9C-4B-FE-6D-FE-73-9C-2B-9C-4B-FE-6D-FE-73-9C-2B-AB-C5-6E-FB-C4-0E-63-34",
Roundtrip (RC2.Create (), CipherMode.ECB), "Encrypted data");
}
[Test]
public void RC2_CBC ()
{
Assert.AreEqual ("9C-4B-FE-6D-FE-73-9C-2B-52-8F-C8-47-2B-66-F9-70-B2-67-CF-23-D7-D7-6D-A6",
Roundtrip (RC2.Create (), CipherMode.CBC), "Encrypted data");
}
[Test]
public void RC2_CFB ()
{
Assert.AreEqual ("9C-5A-41-95-9A-15-12-C2-54-1C-9C-6C-4B-65-A0-36-DD-7F-2B-0D-D5-D2-C0-CD",
Roundtrip (RC2.Create (), CipherMode.CFB), "Encrypted data");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void RC2_OFB ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (RC2.Create (), CipherMode.OFB), "Encrypted data");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void RC2_CTS ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (RC2.Create (), CipherMode.CTS), "Encrypted data");
}
[Test]
public void Rijndael_ECB ()
{
Assert.AreEqual ("F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-9F-3B-75-04-92-6F-8B-D3-6E-31-18-E9-03-A4-CD-4A",
Roundtrip (Rijndael.Create (), CipherMode.ECB), "Encrypted data");
}
[Test]
public void Rijndael_CBC ()
{
Assert.AreEqual ("F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-D4-E9-69-25-C0-BF-CF-FB-52-F8-A1-87-EE-77-4A-AB-C3-28-91-ED-46-6E-6F-98-C1-4D-65-14-ED-9D-1E-5B",
Roundtrip (Rijndael.Create (), CipherMode.CBC), "Encrypted data");
}
#if NET_2_0
[Test]
public void Rijndael_CFB ()
{
Assert.AreEqual ("F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-D4-E9-69-25-C0-BF-CF-FB-52-F8-A1-87-EE-77-4A-AB-26-03-7F-B7-B5-88-AE-0A-F8-AF-1E-CF-9C-F5-3A-8A",
Roundtrip (Rijndael.Create (), CipherMode.CFB), "Encrypted data");
}
#else
// CFB was confused with OFB in Fx 1.0 and 1.1 (and wasn't supported because of that)
// However Mono does support it (because the same code is executed for all managed ciphers).
[Test]
public void Rijndael_CFB ()
{
try {
Assert.AreEqual ("F2-90-00-B6-2A-49-9F-D0-A9-F3-9A-6A-DD-2E-77-80-D4-E9-69-25-C0-BF-CF-FB-52-F8-A1-87-EE-77-4A-AB-26-03-7F-B7-B5-88-AE-0A-F8-AF-1E-CF-9C-F5-3A-8A",
Roundtrip (Rijndael.Create (), CipherMode.CFB), "Encrypted data");
}
catch (CryptographicException) {
// we assume this is the bugged MS implementation
}
}
#endif
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Rijndael_OFB ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (Rijndael.Create (), CipherMode.OFB), "Encrypted data");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Rijndael_CTS ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (Rijndael.Create (), CipherMode.CTS), "Encrypted data");
}
[Test]
public void TripleDES_ECB ()
{
Assert.AreEqual ("89-4B-C3-08-54-26-A4-41-89-4B-C3-08-54-26-A4-41-A3-CF-6E-C8-8B-D9-7D-73",
Roundtrip (TripleDES.Create (), CipherMode.ECB), "Encrypted data");
}
[Test]
public void TripleDES_CBC ()
{
Assert.AreEqual ("89-4B-C3-08-54-26-A4-41-06-8E-DF-B5-F0-23-AB-B4-76-40-68-9A-26-7D-8D-6E",
Roundtrip (TripleDES.Create (), CipherMode.CBC), "Encrypted data");
}
[Test]
public void TripleDES_CFB ()
{
Assert.AreEqual ("89-9F-00-9D-26-BB-21-59-85-6D-A2-BF-15-FE-73-53-1F-CE-35-26-5B-DF-43-26",
Roundtrip (TripleDES.Create (), CipherMode.CFB), "Encrypted data");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void TripleDES_OFB ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (TripleDES.Create (), CipherMode.OFB), "Encrypted data");
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void TripleDES_CTS ()
{
Assert.AreEqual ("not implemented in any released framework",
Roundtrip (TripleDES.Create (), CipherMode.CTS), "Encrypted data");
}
}
}

View File

@@ -0,0 +1,72 @@
//
// CryptoAPITransformCas.cs -
// CAS unit tests for System.Security.Cryptography.CryptoAPITransform
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 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.Reflection;
using System.Security;
using System.Security.Cryptography;
using System.Security.Permissions;
namespace MonoCasTests.System.Security.Cryptography {
[TestFixture]
[Category ("CAS")]
public class CryptoAPITransformCas {
[SetUp]
public void SetUp ()
{
if (!SecurityManager.SecurityEnabled)
Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
}
[Test]
[SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
[ExpectedException (typeof (SecurityException))]
public void KeyHandle ()
{
CryptoAPITransform transform = null;
Type t = typeof (CryptoAPITransform);
// Mono has a default, internal, constructor
ConstructorInfo ci = t.GetConstructor (BindingFlags.Instance | BindingFlags.NonPublic, null, Type.EmptyTypes, null);
if (ci != null) {
transform = (CryptoAPITransform) ci.Invoke (null);
} else {
// while MS use CryptoAPITransform for all unmanaged crypto
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
transform = (CryptoAPITransform) des.CreateEncryptor ();
}
Assert.AreEqual (IntPtr.Zero, transform.KeyHandle, "KeyHandle");
}
}
}

View File

@@ -0,0 +1,107 @@
//
// CryptoConfigCas.cs -
// CAS unit tests for System.Security.Cryptography.CryptoConfig
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 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.Reflection;
using System.Security;
using System.Security.Cryptography;
using System.Security.Permissions;
namespace MonoCasTests.System.Security.Cryptography {
[TestFixture]
[Category ("CAS")]
public class CryptoConfigCas {
private MethodInfo create1;
private MethodInfo create2;
[SetUp]
public void SetUp ()
{
if (!SecurityManager.SecurityEnabled)
Assert.Ignore ("SecurityManager.SecurityEnabled is OFF");
Type t = typeof (CryptoConfig);
Type[] one = new Type [1] { typeof (string) };
create1 = t.GetMethod ("CreateFromName", one);
Type[] two = new Type [2] { typeof (string), typeof (object[]) };
create2 = t.GetMethod ("CreateFromName", two);
}
// Partial Trust Tests - i.e. call "normal" unit with reduced privileges
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void PartialTrust_DenyUnrestricted_Success ()
{
MonoTests.System.Security.Cryptography.CryptoConfigTest cct = new MonoTests.System.Security.Cryptography.CryptoConfigTest ();
// call most (all but arguments checking) unit tests from CryptoConfigTest
cct.CCToString ();
cct.CreateFromName ();
cct.EncodeOID ();
cct.MapNameToOID ();
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
[Category ("NotWorking")]
public void PartialTrust_DenyUnrestricted_CreateFromURL ()
{
MonoTests.System.Security.Cryptography.CryptoConfigTest cct = new MonoTests.System.Security.Cryptography.CryptoConfigTest ();
// this test must load System.Security to create some types
cct.CreateFromURL ();
}
// we use reflection to call CryptoConfig as the CreateFromName methods are
// protected by LinkDemand (which will be converted into full demand, i.e.
// a stack walk) when reflection is used (i.e. it gets testable).
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
public void CreateFromName1 ()
{
Assert.IsNotNull (create1.Invoke (null, new object [1] { "DES" }), "1");
// No restriction for the string only version
}
[Test]
[PermissionSet (SecurityAction.Deny, Unrestricted = true)]
[ExpectedException (typeof (SecurityException))]
public void CreateFromName2 ()
{
Assert.IsNotNull (create2.Invoke (null, new object [2] { "DES", new object [0] }), "2");
}
}
}

View File

@@ -0,0 +1,459 @@
//
// CryptoConfigTest.cs - NUnit Test Cases for CryptoConfig
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004-2007,2011 Novell, Inc (http://www.novell.com)
// Copyright 2011 Xamarin Inc (http://www.xamarin.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.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class CryptoConfigTest {
void CreateFromName (string name, string objectname)
{
object o = CryptoConfig.CreateFromName (name);
if (objectname == null)
Assert.IsNull (o, name);
else
Assert.AreEqual (objectname, o.ToString (), name);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void CreateFromNameNull ()
{
object o = CryptoConfig.CreateFromName (null);
}
// validate that CryptoConfig create the exact same implementation between mono and MS
[Test]
public void CreateFromName ()
{
CreateFromName ("SHA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
// FIXME: We need to support the machine.config file to get exact same results
// with the MS .NET Framework
CreateFromName ("SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
CreateFromName( "System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
// after installing the WSDK - changes to the machine.config file (not documented)
// CreateFromName ("SHA1", "System.Security.Cryptography.SHA1Managed");
// CreateFromName ("System.Security.Cryptography.SHA1", "System.Security.Cryptography.SHA1Managed");
CreateFromName ("System.Security.Cryptography.HashAlgorithm", "System.Security.Cryptography.SHA1CryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.SHA1CryptoServiceProvider", "System.Security.Cryptography.SHA1CryptoServiceProvider");
CreateFromName ("MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.MD5", "System.Security.Cryptography.MD5CryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.MD5CryptoServiceProvider", "System.Security.Cryptography.MD5CryptoServiceProvider");
CreateFromName ("SHA256", "System.Security.Cryptography.SHA256Managed");
CreateFromName ("SHA-256", "System.Security.Cryptography.SHA256Managed");
CreateFromName ("System.Security.Cryptography.SHA256", "System.Security.Cryptography.SHA256Managed");
CreateFromName ("SHA384", "System.Security.Cryptography.SHA384Managed");
CreateFromName ("SHA-384", "System.Security.Cryptography.SHA384Managed");
CreateFromName ("System.Security.Cryptography.SHA384", "System.Security.Cryptography.SHA384Managed");
CreateFromName ("SHA512", "System.Security.Cryptography.SHA512Managed");
CreateFromName ("SHA-512", "System.Security.Cryptography.SHA512Managed");
CreateFromName ("System.Security.Cryptography.SHA512", "System.Security.Cryptography.SHA512Managed");
CreateFromName ("RSA", "System.Security.Cryptography.RSACryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.RSA", "System.Security.Cryptography.RSACryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.AsymmetricAlgorithm", "System.Security.Cryptography.RSACryptoServiceProvider");
CreateFromName ("DSA", "System.Security.Cryptography.DSACryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.DSA", "System.Security.Cryptography.DSACryptoServiceProvider");
CreateFromName ("DES", "System.Security.Cryptography.DESCryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.DES", "System.Security.Cryptography.DESCryptoServiceProvider");
CreateFromName ("3DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
CreateFromName ("TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
CreateFromName ("Triple DES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.TripleDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
// LAMESPEC SymmetricAlgorithm documented as TripleDESCryptoServiceProvider
CreateFromName ("System.Security.Cryptography.SymmetricAlgorithm", "System.Security.Cryptography.RijndaelManaged");
CreateFromName ("RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.RC2", "System.Security.Cryptography.RC2CryptoServiceProvider");
CreateFromName ("Rijndael", "System.Security.Cryptography.RijndaelManaged");
CreateFromName ("System.Security.Cryptography.Rijndael", "System.Security.Cryptography.RijndaelManaged");
// LAMESPEC Undocumented Names in CryptoConfig
CreateFromName ("RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.RandomNumberGenerator", "System.Security.Cryptography.RNGCryptoServiceProvider");
CreateFromName ("System.Security.Cryptography.KeyedHashAlgorithm", "System.Security.Cryptography.HMACSHA1");
CreateFromName ("HMACSHA1", "System.Security.Cryptography.HMACSHA1");
CreateFromName ("System.Security.Cryptography.HMACSHA1", "System.Security.Cryptography.HMACSHA1");
CreateFromName ("MACTripleDES", "System.Security.Cryptography.MACTripleDES");
CreateFromName ("System.Security.Cryptography.MACTripleDES", "System.Security.Cryptography.MACTripleDES");
// new HMAC - new base class doesn't return anything with it's short name
Assert.IsNull (CryptoConfig.CreateFromName ("HMAC"), "HMAC");
CreateFromName ("System.Security.Cryptography.HMAC", "System.Security.Cryptography.HMACSHA1");
CreateFromName ("HMACMD5", "System.Security.Cryptography.HMACMD5");
CreateFromName ("System.Security.Cryptography.HMACMD5", "System.Security.Cryptography.HMACMD5");
CreateFromName ("HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160");
CreateFromName ("System.Security.Cryptography.HMACRIPEMD160", "System.Security.Cryptography.HMACRIPEMD160");
CreateFromName ("HMACSHA256", "System.Security.Cryptography.HMACSHA256");
CreateFromName ("System.Security.Cryptography.HMACSHA256", "System.Security.Cryptography.HMACSHA256");
CreateFromName ("HMACSHA384", "System.Security.Cryptography.HMACSHA384");
CreateFromName ("System.Security.Cryptography.HMACSHA384", "System.Security.Cryptography.HMACSHA384");
CreateFromName ("HMACSHA512", "System.Security.Cryptography.HMACSHA512");
CreateFromName ("System.Security.Cryptography.HMACSHA512", "System.Security.Cryptography.HMACSHA512");
// new hash algorithm
CreateFromName ("RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed");
CreateFromName ("RIPEMD-160", "System.Security.Cryptography.RIPEMD160Managed");
CreateFromName ("System.Security.Cryptography.RIPEMD160", "System.Security.Cryptography.RIPEMD160Managed");
// x.509 stuff
#if !MOBILE
CreateFromName ("X509Chain", "System.Security.Cryptography.X509Certificates.X509Chain");
// note: CryptoConfig can create any object !
CreateFromName ("System.Security.Cryptography.CryptoConfig", "System.Security.Cryptography.CryptoConfig");
CreateFromName ("System.IO.MemoryStream", "System.IO.MemoryStream");
#endif
// non existing algo should return null (without exception)
Assert.IsNull (CryptoConfig.CreateFromName ("NonExistingAlgorithm"), "NonExistingAlgorithm");
}
// additional names (URL) used for XMLDSIG (System.Security.Cryptography.Xml)
// URL taken from http://www.w3.org/TR/2002/REC-xmldsig-core-20020212/
[Test]
#if NET_2_1
[Ignore ("System.Security.dll is not part of Moonlight, MonoTouch and Mono for Android")]
#endif
public void CreateFromURL ()
{
// URL used in SignatureMethod element
CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1", "System.Security.Cryptography.DSASignatureDescription");
CreateFromName ("http://www.w3.org/2000/09/xmldsig#rsa-sha1", "System.Security.Cryptography.RSAPKCS1SHA1SignatureDescription");
CreateFromName ("http://www.w3.org/2000/09/xmldsig#hmac-sha1", null);
// URL used in DigestMethod element
CreateFromName ("http://www.w3.org/2000/09/xmldsig#sha1", "System.Security.Cryptography.SHA1CryptoServiceProvider");
#if !TARGET_JVM //TargetJvmNotWorking - algorithms from System.Security assembly
// URL used in Canonicalization or Transform elements
CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315", "System.Security.Cryptography.Xml.XmlDsigC14NTransform");
CreateFromName ("http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments", "System.Security.Cryptography.Xml.XmlDsigC14NWithCommentsTransform");
// URL used in Transform element
CreateFromName ("http://www.w3.org/2000/09/xmldsig#base64", "System.Security.Cryptography.Xml.XmlDsigBase64Transform");
// after installing the WSDK - changes to the machine.config file (not documented)
// CreateFromName ("http://www.w3.org/TR/1999/REC-xpath-19991116", "Microsoft.WSDK.Security.XmlDsigXPathTransform");
CreateFromName ("http://www.w3.org/TR/1999/REC-xpath-19991116", "System.Security.Cryptography.Xml.XmlDsigXPathTransform");
CreateFromName ("http://www.w3.org/TR/1999/REC-xslt-19991116", "System.Security.Cryptography.Xml.XmlDsigXsltTransform");
CreateFromName ("http://www.w3.org/2000/09/xmldsig#enveloped-signature", "System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform");
// URL used in Reference element
CreateFromName ("http://www.w3.org/2000/09/xmldsig#Object", null);
CreateFromName ("http://www.w3.org/2000/09/xmldsig#Manifest", null);
CreateFromName ("http://www.w3.org/2000/09/xmldsig#SignatureProperties", null);
// LAMESPEC: only documentated in ".NET Framework Security" book
CreateFromName ("http://www.w3.org/2000/09/xmldsig# X509Data", "System.Security.Cryptography.Xml.KeyInfoX509Data");
CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyName", "System.Security.Cryptography.Xml.KeyInfoName");
CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/DSAKeyValue", "System.Security.Cryptography.Xml.DSAKeyValue");
CreateFromName ("http://www.w3.org/2000/09/xmldsig# KeyValue/RSAKeyValue", "System.Security.Cryptography.Xml.RSAKeyValue");
CreateFromName ("http://www.w3.org/2000/09/xmldsig# RetrievalMethod", "System.Security.Cryptography.Xml.KeyInfoRetrievalMethod");
#if NET_2_0
CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha256", "System.Security.Cryptography.SHA256Managed");
CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha384", null);
CreateFromName ("http://www.w3.org/2001/04/xmlenc#sha512", "System.Security.Cryptography.SHA512Managed");
CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha256", "System.Security.Cryptography.HMACSHA256");
CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha384", "System.Security.Cryptography.HMACSHA384");
CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-sha512", "System.Security.Cryptography.HMACSHA512");
CreateFromName ("http://www.w3.org/2001/04/xmldsig-more#hmac-ripemd160", "System.Security.Cryptography.HMACRIPEMD160");
#endif
#endif
}
[Test]
public void CreateFromName_UpperCase ()
{
CreateFromName ("SHA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
#if NET_2_0
CreateFromName ("SYSTEM.SECURITY.CRYPTOGRAPHY.TRIPLEDES", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
CreateFromName ("HTTP://WWW.W3.ORG/2000/09/XMLDSIG#DSA-SHA1", "System.Security.Cryptography.DSASignatureDescription");
#else
CreateFromName ("SYSTEM.SECURITY.CRYPTOGRAPHY.TRIPLEDES", null);
CreateFromName ("HTTP://WWW.W3.ORG/2000/09/XMLDSIG#DSA-SHA1", null);
#endif
}
[Test]
public void CreateFromName_LowerCase ()
{
#if NET_2_0
CreateFromName ("sha", "System.Security.Cryptography.SHA1CryptoServiceProvider");
CreateFromName ("system.security.cryptography.tripledes", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
#else
CreateFromName ("sha", null);
CreateFromName ("system.security.cryptography.tripledes", null);
#endif
CreateFromName ("http://www.w3.org/2000/09/xmldsig#dsa-sha1", "System.Security.Cryptography.DSASignatureDescription");
}
[Test]
public void CreateFromName_MixedCase ()
{
#if NET_2_0
CreateFromName ("ShA", "System.Security.Cryptography.SHA1CryptoServiceProvider");
CreateFromName ("SyStEm.SeCuRiTy.CrYpToGrApHy.TrIpLeDeS", "System.Security.Cryptography.TripleDESCryptoServiceProvider");
CreateFromName ("hTtP://wWw.W3.oRg/2000/09/xMlDsIg#dSa-sHa1", "System.Security.Cryptography.DSASignatureDescription");
#else
CreateFromName ("ShA", null);
CreateFromName ("SyStEm.SeCuRiTy.CrYpToGrApHy.TrIpLeDeS", null);
CreateFromName ("hTtP://wWw.W3.oRg/2000/09/xMlDsIg#dSa-sHa1", null);
#endif
}
// Tests created using "A Layer Man Guide to ASN.1" from RSA, page 19-20
// Need to find an OID ? goto http://www.alvestrand.no/~hta/objectid/top.html
static byte[] oidETSI = { 0x06, 0x03, 0x04, 0x00, 0x00 };
static byte[] oidSHA1 = { 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A };
static byte[] oidASN1CharacterModule = { 0x06, 0x04, 0x51, 0x00, 0x00, 0x00 };
static byte[] oidmd5withRSAEncryption = { 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x04 };
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public void EncodeOIDNull ()
{
byte[] o = CryptoConfig.EncodeOID (null);
}
[Test]
public void EncodeOID ()
{
// OID starts with 0, 1 or 2
Assert.AreEqual (oidETSI, CryptoConfig.EncodeOID ("0.4.0.0"), "OID starting with 0.");
Assert.AreEqual (oidSHA1, CryptoConfig.EncodeOID ("1.3.14.3.2.26"), "OID starting with 1.");
Assert.AreEqual (oidASN1CharacterModule, CryptoConfig.EncodeOID ("2.1.0.0.0"), "OID starting with 2.");
// OID numbers can span multiple bytes
Assert.AreEqual (oidmd5withRSAEncryption, CryptoConfig.EncodeOID ("1.2.840.113549.1.1.4"), "OID with numbers spanning multiple bytes");
}
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
// LAMESPEC: OID greater that 0x7F (127) bytes aren't supported by the MS Framework
public void EncodeOID_BiggerThan127bytes ()
{
// "ms"-invalid OID - greater than 127 bytes (length encoding)
// OID longer than 127 bytes (so length must be encoded on multiple bytes)
string baseOID = "1.3.6.1.4.1.11071.0.";
string lastPart = "1111111111"; // must fit in int32
for (int i = 1; i < 30; i++) {
baseOID += lastPart + ".";
}
baseOID += "0";
byte[] tooLongOID = CryptoConfig.EncodeOID (baseOID);
}
[Test]
[ExpectedException (typeof (OverflowException))]
// LAMESPEC: OID with numbers > Int32 aren't supported by the MS BCL
public void EncodeOID_BiggerThanInt32 ()
{
// "ms"-invalid OID - where a number of the OID > Int32
byte[] tooLongOID = CryptoConfig.EncodeOID ("1.1.4294967295");
}
[Test]
public void EncodeOID_InvalidStart ()
{
// invalid OID - must start with 0, 1 or 2
// however it works with MS BCL
byte[] oid3 = CryptoConfig.EncodeOID ("3.0");
byte[] res3 = { 0x06, 0x01, 0x78 };
Assert.AreEqual (res3, oid3, "OID: 3.0");
}
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void EncodeOID_TooShort ()
{
// invalid OID - must have at least 2 parts (according to X.208)
byte[] tooShortOID = CryptoConfig.EncodeOID ("0");
}
[Test]
public void EncodeOID_InvalidSecondPart ()
{
// invalid OID - second value < 40 for 0. and 1. (modulo 40)
// however it works with MS BCL
byte[] tooBigSecondPartOID = CryptoConfig.EncodeOID ("0.40");
byte[] tooBigSecondPartRes = { 0x06, 0x01, 0x28 };
Assert.AreEqual (tooBigSecondPartRes, tooBigSecondPartOID, "OID: 0.40");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void MapNameToOIDNull ()
{
CryptoConfig.MapNameToOID (null);
}
private void MapNameToOID (string name, string oid)
{
Assert.AreEqual (oid, CryptoConfig.MapNameToOID (name), "oid(" + name + ")");
}
// LAMESPEC: doesn't support all names defined in CryptoConfig
// non supported names (in MSFW) are commented or null-ed
// LAMESPEC: undocumented but full class name is supported
[Test]
public void MapNameToOID()
{
// MapNameToOID ("SHA", "1.3.14.3.2.26");
MapNameToOID ("SHA1", "1.3.14.3.2.26");
MapNameToOID ("System.Security.Cryptography.SHA1", "1.3.14.3.2.26");
// MapNameToOID ("System.Security.Cryptography.HashAlgorithm", "1.3.14.3.2.26");
MapNameToOID ("System.Security.Cryptography.SHA1CryptoServiceProvider", "1.3.14.3.2.26");
MapNameToOID ("System.Security.Cryptography.SHA1Managed", "1.3.14.3.2.26");
MapNameToOID ("MD5", "1.2.840.113549.2.5");
MapNameToOID ("System.Security.Cryptography.MD5", "1.2.840.113549.2.5");
MapNameToOID ("System.Security.Cryptography.MD5CryptoServiceProvider", "1.2.840.113549.2.5");
#if NET_2_0
MapNameToOID ("SHA256", "2.16.840.1.101.3.4.2.1");
MapNameToOID ("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.2.1");
MapNameToOID ("System.Security.Cryptography.SHA256Managed", "2.16.840.1.101.3.4.2.1");
MapNameToOID ("SHA384", "2.16.840.1.101.3.4.2.2");
MapNameToOID ("System.Security.Cryptography.SHA384", "2.16.840.1.101.3.4.2.2");
MapNameToOID ("System.Security.Cryptography.SHA384Managed", "2.16.840.1.101.3.4.2.2");
MapNameToOID ("SHA512", "2.16.840.1.101.3.4.2.3");
MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.2.3");
MapNameToOID ("System.Security.Cryptography.SHA512Managed", "2.16.840.1.101.3.4.2.3");
MapNameToOID ("RIPEMD160", "1.3.36.3.2.1");
MapNameToOID ("System.Security.Cryptography.RIPEMD160", "1.3.36.3.2.1");
MapNameToOID ("System.Security.Cryptography.RIPEMD160Managed", "1.3.36.3.2.1");
#else
MapNameToOID ("SHA256", "2.16.840.1.101.3.4.1");
// MapNameToOID ("SHA-256", "2.16.840.1.101.3.4.1");
MapNameToOID ("System.Security.Cryptography.SHA256", "2.16.840.1.101.3.4.1");
MapNameToOID ("System.Security.Cryptography.SHA256Managed", "2.16.840.1.101.3.4.1");
MapNameToOID ("SHA384", "2.16.840.1.101.3.4.2");
// MapNameToOID ("SHA-384", "2.16.840.1.101.3.4.2");
MapNameToOID ("System.Security.Cryptography.SHA384", "2.16.840.1.101.3.4.2");
MapNameToOID ("System.Security.Cryptography.SHA384Managed", "2.16.840.1.101.3.4.2");
MapNameToOID ("SHA512", "2.16.840.1.101.3.4.3");
// MapNameToOID ("SHA-512", "2.16.840.1.101.3.4.3");
MapNameToOID ("System.Security.Cryptography.SHA512", "2.16.840.1.101.3.4.3");
MapNameToOID ("System.Security.Cryptography.SHA512Managed", "2.16.840.1.101.3.4.3");
#endif
// LAMESPEC: only documentated in ".NET Framework Security" book
MapNameToOID ("TripleDESKeyWrap", "1.2.840.113549.1.9.16.3.6");
#if NET_2_0
// new OID defined in Fx 2.0
MapNameToOID ("DES", "1.3.14.3.2.7");
MapNameToOID ("TripleDES", "1.2.840.113549.3.7");
MapNameToOID ("RC2", "1.2.840.113549.3.2");
#else
// no OID defined before Fx 2.0
MapNameToOID ("DES", null);
MapNameToOID ("TripleDES", null);
MapNameToOID ("RC2", null);
#endif
MapNameToOID ("RSA", null);
MapNameToOID ("DSA", null);
MapNameToOID ("3DES", null);
// no OID defined ?
MapNameToOID ("System.Security.Cryptography.RSA", null);
MapNameToOID ("System.Security.Cryptography.AsymmetricAlgorithm", null);
MapNameToOID ("System.Security.Cryptography.DSA", null);
MapNameToOID ("System.Security.Cryptography.DES", null);
MapNameToOID ("Triple DES", null);
MapNameToOID ("System.Security.Cryptography.TripleDES", null);
MapNameToOID ("System.Security.Cryptography.RC2", null);
MapNameToOID ("Rijndael", null);
MapNameToOID ("System.Security.Cryptography.Rijndael", null);
MapNameToOID ("System.Security.Cryptography.SymmetricAlgorithm", null);
// LAMESPEC Undocumented Names in CryptoConfig
MapNameToOID ("RandomNumberGenerator", null);
MapNameToOID ("System.Security.Cryptography.RandomNumberGenerator", null);
MapNameToOID ("System.Security.Cryptography.KeyedHashAlgorithm", null);
#if NET_2_0
MapNameToOID ("HMAC", null);
MapNameToOID ("System.Security.Cryptography.HMAC", null);
MapNameToOID ("HMACMD5", null);
MapNameToOID ("System.Security.Cryptography.HMACMD5", null);
MapNameToOID ("HMACRIPEMD160", null);
MapNameToOID ("System.Security.Cryptography.HMACRIPEMD160", null);
MapNameToOID ("HMACSHA256", null);
MapNameToOID ("System.Security.Cryptography.HMACSHA256", null);
MapNameToOID ("HMACSHA384", null);
MapNameToOID ("System.Security.Cryptography.HMACSHA384", null);
MapNameToOID ("HMACSHA512", null);
MapNameToOID ("System.Security.Cryptography.HMACSHA512", null);
#endif
MapNameToOID ("HMACSHA1", null);
MapNameToOID ("System.Security.Cryptography.HMACSHA1", null);
MapNameToOID ("MACTripleDES", null);
MapNameToOID ("System.Security.Cryptography.MACTripleDES", null);
// non existing algo should return null (without exception)
MapNameToOID ("NonExistingAlgorithm", null);
}
[Test]
public void MapNameToOID_UpperCase ()
{
MapNameToOID ("SHA1", "1.3.14.3.2.26");
#if NET_2_0
MapNameToOID ("SYSTEM.SECURITY.CRYPTOGRAPHY.MD5CRYPTOSERVICEPROVIDER", "1.2.840.113549.2.5");
#else
MapNameToOID ("SYSTEM.SECURITY.CRYPTOGRAPHY.MD5CRYPTOSERVICEPROVIDER", null);
#endif
}
[Test]
public void MapNameToOID_LowerCase ()
{
#if NET_2_0
MapNameToOID ("sha1", "1.3.14.3.2.26");
MapNameToOID ("system.security.cryptography.md5cryptoserviceprovider", "1.2.840.113549.2.5");
#else
MapNameToOID ("sha1", null);
MapNameToOID ("system.security.cryptography.md5cryptoserviceprovider", null);
#endif
}
[Test]
public void MapNameToOID_MixedCase ()
{
#if NET_2_0
MapNameToOID ("sHa1", "1.3.14.3.2.26");
MapNameToOID ("SySteM.SeCuRiTy.CrYpToGrApHy.Md5cRyPtOsErViCePrOvIdEr", "1.2.840.113549.2.5");
#else
MapNameToOID ("sHa1", null);
MapNameToOID ("SySteM.SeCuRiTy.CrYpToGrApHy.Md5cRyPtOsErViCePrOvIdEr", null);
#endif
}
[Test]
public void CCToString ()
{
// under normal circumstance there are no need to create a CryptoConfig object
// because all interesting stuff are in static methods
CryptoConfig cc = new CryptoConfig ();
Assert.AreEqual ("System.Security.Cryptography.CryptoConfig", cc.ToString ());
}
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,72 @@
//
// CspProviderFlagsTest.cs - NUnit Test Cases for CspProviderFlags
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// Copyright (C) 2004 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.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class CspProviderFlagsTest {
[Test]
public void Values ()
{
#if NET_2_0
Assert.AreEqual ((int)CspProviderFlags.NoFlags, 0, "NoFlags");
#endif
Assert.AreEqual ((int)CspProviderFlags.UseMachineKeyStore, 1, "UseMachineKeyStore");
Assert.AreEqual ((int)CspProviderFlags.UseDefaultKeyContainer, 2, "UseDefaultKeyContainer");
#if NET_2_0
Assert.AreEqual ((int)CspProviderFlags.NoPrompt, 64, "NoPrompt");
Assert.AreEqual ((int)CspProviderFlags.UseArchivableKey, 16, "UseArchivableKey");
Assert.AreEqual ((int)CspProviderFlags.UseExistingKey, 8, "UseExistingKey");
Assert.AreEqual ((int)CspProviderFlags.UseNonExportableKey, 4, "UseNonExportableKey");
Assert.AreEqual ((int)CspProviderFlags.UseUserProtectedKey, 32, "UseUserProtectedKey");
#endif
}
[Test]
public void Flags ()
{
CspProviderFlags cpf = CspProviderFlags.UseDefaultKeyContainer | CspProviderFlags.UseMachineKeyStore
#if NET_2_0
| CspProviderFlags.NoFlags | CspProviderFlags.NoPrompt
| CspProviderFlags.UseArchivableKey | CspProviderFlags.UseExistingKey
| CspProviderFlags.UseNonExportableKey | CspProviderFlags.UseUserProtectedKey;
int expected = 127;
#else
;
int expected = 3;
#endif
Assert.AreEqual (expected, (int)cpf, "All");
}
}
}

View File

@@ -0,0 +1,265 @@
//
// TestSuite.System.Security.Cryptography.DESCryptoServiceProviderTest.cs
//
// 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 System;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class DESCryptoServiceProviderTest : DESFIPS81Test {
[SetUp]
public void SetUp ()
{
des = new DESCryptoServiceProvider ();
}
[Test]
public void KeyChecks ()
{
byte[] key = des.Key;
Assert.AreEqual (8, key.Length, "Key");
Assert.IsFalse (DES.IsWeakKey (key), "IsWeakKey");
Assert.IsFalse (DES.IsSemiWeakKey (key), "IsSemiWeakKey");
}
[Test]
public void IV ()
{
byte[] iv = des.IV;
Assert.AreEqual (8, iv.Length, "IV");
}
// other tests (test vectors) are inherited from DESFIPS81Test
// (in DESTest.cs) but executed here
[Test]
#if NET_2_0
[ExpectedException (typeof (CryptographicException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public void CreateEncryptor_KeyNull ()
{
ICryptoTransform encryptor = des.CreateEncryptor (null, des.IV);
byte[] data = new byte[encryptor.InputBlockSize];
byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
ICryptoTransform decryptor = des.CreateDecryptor (des.Key, des.IV);
byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
// null key != SymmetricAlgorithm.Key
// in about 1 out of 256 runs the exception will not be thrown because the padding will be
// the same as expected - however this still won't produce the right results (next check)
Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
// so this case is ok too, throw the expected exception to make the unit test succeed
throw new CryptographicException ("1/256");
}
[Test]
public void CreateEncryptor_IvNull ()
{
ICryptoTransform encryptor = des.CreateEncryptor (des.Key, null);
byte[] data = new byte[encryptor.InputBlockSize];
byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
ICryptoTransform decryptor = des.CreateDecryptor (des.Key, des.IV);
byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
// null iv != SymmetricAlgorithm.IV
}
[Test]
public void CreateEncryptor_KeyIv ()
{
byte[] originalKey = des.Key;
byte[] originalIV = des.IV;
byte[] key = (byte[]) des.Key.Clone ();
Array.Reverse (key);
byte[] iv = (byte[]) des.IV.Clone ();
Array.Reverse (iv);
Assert.IsNotNull (des.CreateEncryptor (key, iv), "CreateEncryptor");
Assert.AreEqual (originalKey, des.Key, "Key");
Assert.AreEqual (originalIV, des.IV, "IV");
// SymmetricAlgorithm Key and IV not changed by CreateEncryptor
}
[Test]
#if NET_2_0
[ExpectedException (typeof (CryptographicException))]
#else
[ExpectedException (typeof (NullReferenceException))]
#endif
public void CreateDecryptor_KeyNull ()
{
ICryptoTransform encryptor = des.CreateEncryptor (des.Key, des.IV);
byte[] data = new byte[encryptor.InputBlockSize];
byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
ICryptoTransform decryptor = des.CreateDecryptor (null, des.IV);
byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
// null key != SymmetricAlgorithm.Key
// in about 1 out of 256 runs the exception will not be thrown because the padding will be
// the same as expected - however this still won't produce the right results (next check)
Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
// so this case is ok too, throw the expected exception to make the unit test succeed
throw new CryptographicException ("1/256");
}
[Test]
public void CreateDecryptor_IvNull ()
{
ICryptoTransform encryptor = des.CreateEncryptor (des.Key, des.IV);
byte[] data = new byte[encryptor.InputBlockSize];
byte[] encdata = encryptor.TransformFinalBlock (data, 0, data.Length);
ICryptoTransform decryptor = des.CreateDecryptor (des.Key, null);
byte[] decdata = decryptor.TransformFinalBlock (encdata, 0, encdata.Length);
Assert.IsFalse (BitConverter.ToString (data) == BitConverter.ToString (decdata), "Compare");
// null iv != SymmetricAlgorithm.IV
}
[Test]
public void CreateDecryptor_KeyIv ()
{
byte[] originalKey = des.Key;
byte[] originalIV = des.IV;
byte[] key = (byte[]) des.Key.Clone ();
Array.Reverse (key);
byte[] iv = (byte[]) des.IV.Clone ();
Array.Reverse (iv);
Assert.IsNotNull (des.CreateEncryptor (key, iv), "CreateDecryptor");
Assert.AreEqual (originalKey, des.Key, "Key");
Assert.AreEqual (originalIV, des.IV, "IV");
// SymmetricAlgorithm Key and IV not changed by CreateDecryptor
}
// Setting the IV is more restrictive than supplying an IV to
// CreateEncryptor and CreateDecryptor. See bug #76483
private ICryptoTransform CreateEncryptor_IV (int size)
{
byte[] iv = (size == -1) ? null : new byte[size];
return des.CreateEncryptor (des.Key, iv);
}
[Test]
public void CreateEncryptor_IV_Null ()
{
CreateEncryptor_IV (-1);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (CryptographicException))]
#endif
public void CreateEncryptor_IV_Zero ()
{
CreateEncryptor_IV (0);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (CryptographicException))]
#endif
public void CreateEncryptor_IV_TooSmall ()
{
int size = (des.BlockSize >> 3) - 1;
CreateEncryptor_IV (size);
}
[Test]
public void CreateEncryptor_IV_BlockSize ()
{
int size = (des.BlockSize >> 3);
CreateEncryptor_IV (size);
}
[Test]
public void CreateEncryptor_IV_TooBig ()
{
int size = des.BlockSize; // 8 times too big
CreateEncryptor_IV (size);
}
private ICryptoTransform CreateDecryptor_IV (int size)
{
byte[] iv = (size == -1) ? null : new byte[size];
return des.CreateDecryptor (des.Key, iv);
}
[Test]
public void CreateDecryptor_IV_Null ()
{
CreateDecryptor_IV (-1);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (CryptographicException))]
#endif
public void CreateDecryptor_IV_Zero ()
{
CreateDecryptor_IV (0);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (CryptographicException))]
#endif
public void CreateDecryptor_IV_TooSmall ()
{
int size = (des.BlockSize >> 3) - 1;
CreateDecryptor_IV (size);
}
[Test]
public void CreateDecryptor_IV_BlockSize ()
{
int size = (des.BlockSize >> 3);
CreateDecryptor_IV (size);
}
[Test]
public void CreateDecryptor_IV_TooBig ()
{
int size = des.BlockSize; // 8 times too big
CreateDecryptor_IV (size);
}
}
}

View File

@@ -0,0 +1 @@
b10cbf3693449292ac103b05ea7691685ac44c3b

View File

@@ -0,0 +1,178 @@
//
// DSASignatureDeformatterTest.cs - NUnit Test Cases for DSASignatureDeformatter
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004 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.Security;
using System.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class DSASignatureDeformatterTest {
protected DSASignatureDeformatter def;
protected static DSA dsa;
protected static RSA rsa;
static byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
static byte[] sign = { 0x50, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
public DSASignatureDeformatterTest ()
{
// key generation is VERY long so one time is enough
dsa = DSA.Create ();
rsa = RSA.Create ();
}
[SetUp]
public void SetUp ()
{
def = new DSASignatureDeformatter ();
}
[Test]
public void Constructor_Empty ()
{
DSASignatureDeformatter def = new DSASignatureDeformatter ();
Assert.IsNotNull (def);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public void Constructor_Null ()
{
DSASignatureDeformatter def = new DSASignatureDeformatter (null);
Assert.IsNotNull (def);
}
[Test]
public void Constructor_DSA ()
{
DSASignatureDeformatter def = new DSASignatureDeformatter (dsa);
Assert.IsNotNull (def);
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void Constructor_RSA ()
{
DSASignatureDeformatter def = new DSASignatureDeformatter (rsa);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SetHash_Null ()
{
def.SetHashAlgorithm (null);
}
[Test]
public void SetHash_SHA1 ()
{
def.SetHashAlgorithm ("SHA1");
}
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void SetHash_MD5 ()
{
def.SetHashAlgorithm ("MD5");
}
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public void SetKey_Null ()
{
def.SetKey (null);
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void SetKey_RSA ()
{
def.SetKey (rsa);
}
[Test]
public void SetKey_DSA ()
{
def.SetKey (dsa);
}
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void Verify_NoKeyPair ()
{
def.VerifySignature (hash, sign);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Verify_NullSignature ()
{
dsa.ImportParameters (AllTests.GetKey (false));
def.SetKey (dsa);
def.VerifySignature (hash, null);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Verify_NullHash ()
{
dsa.ImportParameters (AllTests.GetKey (false));
def.SetKey (dsa);
byte[] s = null; // overloaded method
def.VerifySignature (s, sign);
}
[Test]
public void Verify ()
{
dsa.ImportParameters (AllTests.GetKey (false));
def.SetKey (dsa);
Assert.IsTrue (def.VerifySignature (hash, sign));
}
[Test]
public void Verify_Bad ()
{
dsa.ImportParameters (AllTests.GetKey (false));
def.SetKey (dsa);
byte[] badSign = { 0x49, 0xd2, 0xb0, 0x8b, 0xcd, 0x5e, 0xb2, 0xc2, 0x35, 0x82, 0xd3, 0x76, 0x07, 0x79, 0xbb, 0x55, 0x98, 0x72, 0x43, 0xe8,
0x74, 0xc9, 0x35, 0xf8, 0xc9, 0xbd, 0x69, 0x2f, 0x08, 0x34, 0xfa, 0x5a, 0x59, 0x23, 0x2a, 0x85, 0x7b, 0xa3, 0xb3, 0x82 };
Assert.IsFalse (def.VerifySignature (hash, badSign));
}
}
}

View File

@@ -0,0 +1,173 @@
//
// DSASignatureFormatterTest.cs - NUnit Test Cases for DSASignatureFormatter
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2004 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.Security;
using System.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class DSASignatureFormatterTest {
protected DSASignatureFormatter fmt;
protected static DSA dsa;
protected static RSA rsa;
public DSASignatureFormatterTest ()
{
// key generation is VERY long so one time is enough
dsa = DSA.Create ();
rsa = RSA.Create ();
}
[SetUp]
public void SetUp ()
{
fmt = new DSASignatureFormatter ();
}
[Test]
public void Constructor_Empty ()
{
DSASignatureFormatter fmt = new DSASignatureFormatter ();
Assert.IsNotNull (fmt);
}
[Test]
public void Constructor_DSA ()
{
DSASignatureFormatter fmt = new DSASignatureFormatter (dsa);
Assert.IsNotNull (fmt);
}
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public void Constructor_Null ()
{
DSASignatureFormatter fmt = new DSASignatureFormatter (null);
Assert.IsNotNull (fmt);
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void Constructor_RSA ()
{
DSASignatureFormatter fmt = new DSASignatureFormatter (rsa);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void SetHash_Null ()
{
fmt.SetHashAlgorithm (null);
}
[Test]
public void SetHash_SHA1 ()
{
fmt.SetHashAlgorithm ("SHA1");
}
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void SetHash_MD5 ()
{
fmt.SetHashAlgorithm ("MD5");
}
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#endif
public void SetKey_Null ()
{
fmt.SetKey (null);
}
[Test]
[ExpectedException (typeof (InvalidCastException))]
public void SetKey_RSA ()
{
fmt.SetKey (rsa);
}
[Test]
public void SetKey_DSA ()
{
fmt.SetKey (dsa);
}
// note: There's a bug in MS Framework where you can't re-import a key into
// the same object
[Test]
[ExpectedException (typeof (CryptographicUnexpectedOperationException))]
public void Signature_NoKeyPair ()
{
byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
byte[] sign = fmt.CreateSignature (hash);
}
[Test]
[ExpectedException (typeof (CryptographicException))]
public void Signature_OnlyPublicKey ()
{
byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
dsa.ImportParameters (AllTests.GetKey (false));
fmt.SetKey (dsa);
byte[] sign = fmt.CreateSignature (hash);
}
[Test]
public void Signature ()
{
byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
dsa.ImportParameters (AllTests.GetKey (true));
fmt.SetKey (dsa);
byte[] sign = fmt.CreateSignature (hash);
Assert.IsTrue (dsa.VerifySignature (hash, sign));
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void Signature_NullHash ()
{
byte[] hash = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13 };
dsa.ImportParameters (AllTests.GetKey (true));
fmt.SetKey (dsa);
byte[] h = null; // overloaded method
byte[] sign = fmt.CreateSignature (h);
}
}
}

View File

@@ -0,0 +1,278 @@
//
// DSATest.cs - NUnit Test Cases for DSA
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2005, 2008 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.Security.Cryptography;
namespace MonoTests.System.Security.Cryptography {
#if NET_2_0
public class NonAbstractDSAForUnitTests : DSA {
protected DSAParameters dsa;
// not tested here - but we must implemented all abstract properties
public override string KeyExchangeAlgorithm
{
get { return null; }
}
// not tested here - but we must implemented all abstract properties
public override string SignatureAlgorithm
{
get { return null; }
}
// not tested here - but we must implemented all abstract methods
public override byte [] CreateSignature (byte [] rgbHash)
{
return null;
}
// basic implementation for tests
public override DSAParameters ExportParameters (bool includePrivateParameters)
{
DSAParameters dsaParams = dsa;
if (!includePrivateParameters)
dsaParams.X = null;
return dsaParams;
}
// basic implementation for tests
public override void ImportParameters (DSAParameters parameters)
{
dsa.P = parameters.P;
dsa.Q = parameters.Q;
dsa.G = parameters.G;
dsa.J = parameters.J;
dsa.Y = parameters.Y;
if (parameters.X != null) {
// watch out for private key zeroification
dsa.X = (byte []) parameters.X.Clone ();
}
dsa.Seed = parameters.Seed;
dsa.Counter = parameters.Counter;
}
// not tested here - but we must implemented all abstract methods
public override bool VerifySignature (byte [] rgbHash, byte [] rgbSignature)
{
return false;
}
protected override void Dispose (bool disposing) { }
}
#endif
[TestFixture]
public class DSATest {
protected DSA dsa;
static string xmlPrivate = "<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter><X>fAOytZttUZFzt/AvwRinmvYKL7E=</X></DSAKeyValue>";
static string xmlPublic = "<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter></DSAKeyValue>";
[SetUp]
public void SetUp ()
{
#if NET_2_0
dsa = new NonAbstractDSAForUnitTests ();
#else
dsa = new DSACryptoServiceProvider ();
#endif
}
public void AssertEquals (string msg, byte [] array1, byte [] array2)
{
Assert.AreEqual (array1, array2, msg);
}
// may also help for DSA descendants
public void AssertEquals (string message, DSAParameters expectedKey, DSAParameters actualKey, bool checkPrivateKey)
{
Assert.AreEqual (expectedKey.Counter, actualKey.Counter, message + " Counter");
AssertEquals (message + " G", expectedKey.G, actualKey.G);
AssertEquals (message + " J", expectedKey.J, actualKey.J);
AssertEquals (message + " P", expectedKey.P, actualKey.P);
AssertEquals (message + " Q", expectedKey.Q, actualKey.Q);
AssertEquals (message + " Seed", expectedKey.Seed, actualKey.Seed);
AssertEquals (message + " Y", expectedKey.Y, actualKey.Y);
if (checkPrivateKey)
AssertEquals (message + " X", expectedKey.X, actualKey.X);
}
// LAMESPEC: ImportParameters inverse the byte arrays inside DSAParameters !!!
// importing and exporting a DSA key (including private key)
[Test]
public void DSAImportPrivateExportPrivate ()
{
DSAParameters input = AllTests.GetKey (true);
dsa.ImportParameters (input);
string xmlDSA = dsa.ToXmlString (true);
dsa.FromXmlString (xmlDSA);
Assert.AreEqual (xmlPrivate, xmlDSA, "DSA Import Private Export Private (xml)");
DSAParameters output = dsa.ExportParameters (true);
AssertEquals ("DSA Import Private Export Private (binary)", AllTests.GetKey (true), output, true);
}
// importing and exporting a DSA key (without private key)
[Test]
public void DSAImportPrivateExportPublic ()
{
DSAParameters input = AllTests.GetKey (true);
dsa.ImportParameters (input);
string xmlDSA = dsa.ToXmlString (false);
dsa.FromXmlString (xmlDSA);
Assert.AreEqual (xmlPublic, xmlDSA, "DSA Import Private Export Public (xml)");
DSAParameters output = dsa.ExportParameters (false);
AssertEquals ("DSA Import Private Export Public (binary)", AllTests.GetKey (true), output, false);
}
// importing and exporting a DSA key (including private key)
[Test]
#if NET_2_0
[ExpectedException (typeof (ArgumentNullException))]
#else
[ExpectedException (typeof (CryptographicException))]
#endif
public void DSAImportPublicExportPrivate ()
{
DSAParameters input = AllTests.GetKey (false);
dsa.ImportParameters (input);
string xmlDSA = dsa.ToXmlString (true);
}
// importing and exporting a DSA key (without private key)
[Test]
public void DSAImportPublicExportPublic ()
{
DSAParameters input = AllTests.GetKey (false);
dsa.ImportParameters (input);
string xmlDSA = dsa.ToXmlString (false);
dsa.FromXmlString (xmlDSA);
Assert.AreEqual (xmlPublic, xmlDSA, "DSA Import Public Export Public (xml)");
DSAParameters output = dsa.ExportParameters (false);
AssertEquals ("DSA Import Public Export Public (binary)", AllTests.GetKey (false), output, true);
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void FromXmlStringNull ()
{
dsa.FromXmlString (null);
}
[Test]
public void ToXmlStringWithoutSeed ()
{
DSA d = DSA.Create ();
d.FromXmlString ("<DSAKeyValue><P>vb95327o8+f5lbrS9qSXxLQYTkcP/WTlJnI0fuw/vFaf7DFQe/ORdTqpa0I3okDOcRiUihzr0y58aQarlNf58MMhMcx/XqRzB2UOVZ/bt2EpfAC3CISwXHlHFoW6+dCHpc72aJOXpreWV6k0oZUg71tKMsPVUP1I8xgELArxAUE=</P><Q>5ul/yRjQ8hFv4w94ZHsP337ebjk=</Q><G>NunCU4DkWaq6IKKhRPCMBBmMgILU8Zqd3aHe0UyKZLYFSOjcKkOIPJ9iWtfDtErHcxb3yjHRV6/EndR+wX8rNsTjYDeUGg5vC6IV4Es+rRCmhVXQ7Y2N+bAH71VxPRbNC90NjgYqKwXZHf2l6c+W4XRvRvNiM5puwz+ubWcm5AA=</G><Y>hQinH+upZPNtTS2o7bi03EOybn9eHC8U61/Rax+oe00YPG+0Md7Okup6CMxZmww0n2F8W7YRZeI7Pltm8TlpmUdMmGSAiILUX585vFM19GR4XeSecqpj1BFO/x4T9tGeakoWxquEjFl4JqEuvDQwnvM76jWDmkUTI4U8kJPnHcw=</Y><J>0l0NjQKpwTJt+h8qmlXhbt4jL+OnaSZkM1zdyIPmOpNavJz7slGtoDAneoQ8STNiT+RrNqGdPbs5glAP8sXS0mdKJ6dGQuySGwGZTP9cWCq81YjRJJ74QuPJUYUruuhN0RTkiukqGzkJYQtA</J></DSAKeyValue>");
d.ToXmlString (false);
}
[Test]
public void ToXmlStringWithZeroCounter ()
{
DSA d = DSA.Create ();
// <PgenCounter>AA==</PgenCounter> == 0
d.FromXmlString ("<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>AA==</PgenCounter><X>fAOytZttUZFzt/AvwRinmvYKL7E=</X></DSAKeyValue>");
d.ToXmlString (false);
}
[Test]
public void FromXmlString_InvalidTop ()
{
string xml = "<a><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><J>AAAAAQ6LSuRiYdsocZ6rgyqIOpE1/uCO1PfEn758Lg2VW6OHJTYHNC30s0gSTG/Jt3oHYX+S8vrtNYb8kRJ/ipgcofGq2Qo/cYKP7RX2K6EJwSfWInhsNMr1JmzuK0lUKkXXXVo15fL8O2/16uEWMg==</J><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter></a>";
dsa.FromXmlString (xml);
Assert.AreEqual (xmlPublic, dsa.ToXmlString (false), "InvalidTop");
}
[Test]
public void FromXmlString_Embedded ()
{
// from bug #355464
string xml = "<SigningKey version=\"1.0\">" + xmlPublic + "</SigningKey>";
dsa.FromXmlString (xml);
Assert.AreEqual (xmlPublic, dsa.ToXmlString (false), "Embedded");
}
[Test]
public void FromXmlString_EmbeddedTwoLevelWithExtraElement ()
{
string xml = "<b><u>" + xmlPublic + "</u></b><i>why not ?</i>";
dsa.FromXmlString (xml);
Assert.AreEqual (xmlPublic, dsa.ToXmlString (false), "Deep");
}
[Test]
public void FromXmlString_TwoKeys ()
{
DSA second = DSA.Create ();
string xml = "<two>" + xmlPublic + second.ToXmlString (false) + "</two>";
dsa.FromXmlString (xml);
Assert.AreEqual (xmlPublic, dsa.ToXmlString (false), "TwoKeys");
}
[Test]
public void FromXmlString_InvalidXml ()
{
string xml = "<open>" + xmlPublic + "</close>";
dsa.FromXmlString (xml);
Assert.AreEqual (xmlPublic, dsa.ToXmlString (false), "Embedded");
}
[Test]
public void ImportExportWithoutJ ()
{
DSA d = DSA.Create ();
DSAParameters input = AllTests.GetKey (false);
input.J = null;
// J is calculated (usually pre-calculated)
d.ImportParameters (input);
input = d.ExportParameters (false);
// if J isn't imported, then it's not exportable and not part of the xml
Assert.IsNull (input.J, "exported-J");
Assert.AreEqual ("<DSAKeyValue><P>s/Oc0t4gj0NRqkCKi4ynJnOAEukNhjkHJPOzNsHP69kyHMUwZ3AzOkLGYOWlOo2zlYKzSbZygDDI5dCWA5gQF2ZGHEUlWJMgUyHmkybOi44cyHaX9yeGfbnoc3xF9sYgkA3vPUZaJuYMOsBp3pyPdeN8/mLU8n0ivURyP+3Ge9M=</P><Q>qkcTW+Ce0L5k8OGTUMkRoGKDc1E=</Q><G>PU/MeGp6I/FBduuwD9UPeCFzg8Ib9H5osku5nT8AhHTY8zGqetuvHhxbESt4lLz8aXzX0oIiMsusBr6E/aBdooBI36fHwW8WndCmwkB1kv7mhRIB4302UrfvC2KWQuBypfl0++a1whBMCh5VTJYH1sBkFIaVNeUbt5Q6/UdiZVY=</G><Y>shJRUdGxEYxSKM5JVol9HAdQwIK+wF9X4n9SAD++vfZOMOYi+M1yuvQAlQvnSlTTWr7CZPRVAICLgDBbqi9iN+Id60ccJ+hw3pGDfLpJ7IdFPszJEeUO+SZBwf8njGXULqSODs/NTciiX7E07rm+KflxFOg0qtWAhmYLxIkDx7s=</Y><Seed>uYM5b20luvbuyevi9TXHwekbr5s=</Seed><PgenCounter>4A==</PgenCounter></DSAKeyValue>", d.ToXmlString (false), "xml");
}
[Test]
public void ImportExportWithoutY ()
{
DSA d = DSA.Create ();
DSAParameters input = AllTests.GetKey (true);
input.Y = null;
// Y is calculated from X
d.ImportParameters (input);
Assert.AreEqual (xmlPrivate, d.ToXmlString (true), "xmlPrivate");
Assert.AreEqual (xmlPublic, d.ToXmlString (false), "xmlPublic");
}
}
}

View File

@@ -0,0 +1,72 @@
//
// DES CFB Unit Tests
//
// Author:
// Sebastien Pouliot <sebastien@xamarin.com>
//
// Copyright (C) 2013 Xamarin Inc (http://www.xamarin.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 System;
using System.Collections.Generic;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class DesCfbTests : WeakKeyCfbTests {
protected override SymmetricAlgorithm GetInstance ()
{
return DES.Create ();
}
[Test]
public void Roundtrip ()
{
ProcessBlockSizes (GetInstance ());
}
static Dictionary<int, string> test_vectors = new Dictionary<int, string> () {
// padding None : Length of the data to encrypt is invalid.
// block size: 64, key size: 64, padding: PKCS7, feedback: 8
{ 1077936648, "5A-44-C0-F3-21-56-A4-8E" },
// block size: 64, key size: 64, padding: Zeros, feedback: 8
{ 1077936904, "5A-43-7C-5D-A9-15-AB-5A" },
// block size: 64, key size: 64, padding: ANSIX923, feedback: 8
{ 1077937160, "5A-43-7C-5D-A9-15-AB-5D" },
// block size: 64, key size: 64, padding: ISO10126, feedback: 8
{ 1077937416, "5A-E6-7D-EF-3B-F8-E9-1C" },
};
protected override string GetExpectedResult (SymmetricAlgorithm algo, byte [] encryptedData)
{
#if false
return base.GetExpectedResult (algo, encryptedData);
#else
return test_vectors [GetId (algo)];
#endif
}
}
}

View File

@@ -0,0 +1,334 @@
//
// TestSuite.System.Security.Cryptography.FromBase64Transform.cs
//
// Author:
// Martin Baulig (martin@gnome.org)
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2002 Ximian, Inc. http://www.ximian.com
// (C) 2004 Novell http://www.novell.com
//
using System;
using System.Security.Cryptography;
using NUnit.Framework;
namespace MonoTests.System.Security.Cryptography {
[TestFixture]
public class FromBase64TransformTest {
private FromBase64Transform _algo;
[SetUp]
public void SetUp ()
{
_algo = new FromBase64Transform ();
}
protected void TransformFinalBlock (string name, byte[] input, byte[] expected,
int inputOffset, int inputCount)
{
byte[] output = _algo.TransformFinalBlock (input, inputOffset, inputCount);
Assert.AreEqual (expected.Length, output.Length, name);
for (int i = 0; i < expected.Length; i++)
Assert.AreEqual (expected [i], output [i], name + "(" + i + ")");
}
protected void TransformFinalBlock (string name, byte[] input, byte[] expected)
{
TransformFinalBlock (name, input, expected, 0, input.Length);
}
[Test]
public void Properties ()
{
Assert.IsTrue (_algo.CanReuseTransform, "CanReuseTransform");
Assert.IsTrue (!_algo.CanTransformMultipleBlocks, "CanTransformMultipleBlocks");
Assert.AreEqual (1, _algo.InputBlockSize, "InputBlockSize");
Assert.AreEqual (3, _algo.OutputBlockSize, "OutputBlockSize");
}
[Test]
public void A0 ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 110, 69 };
byte[] expected = { 174, 90, 123, 66, 201, 196 };
_algo = new FromBase64Transform (FromBase64TransformMode.DoNotIgnoreWhiteSpaces);
TransformFinalBlock ("#A0", input, expected);
}
[Test]
public void A1 ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 110, 69 };
byte[] expected = { 174, 90, 123, 66, 201, 196 };
TransformFinalBlock ("#A1", input, expected);
}
[Test]
public void A2 ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
byte[] expected = { 174, 90, 123, 66 };
TransformFinalBlock ("#A2", input, expected);
}
[Test]
public void A3 ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
byte[] expected = { 150, 158, 208 };
TransformFinalBlock ("#A3", input, expected, 1, 5);
}
[Test]
public void IgnoreTAB ()
{
byte[] input = { 9, 114, 108, 112, 55, 9, 81, 115, 61, 61, 9 };
byte[] expected = { 174, 90, 123, 66 };
TransformFinalBlock ("IgnoreTAB", input, expected);
}
[Test]
public void IgnoreLF ()
{
byte[] input = { 10, 114, 108, 112, 55, 10, 81, 115, 61, 61, 10 };
byte[] expected = { 174, 90, 123, 66 };
TransformFinalBlock ("IgnoreLF", input, expected);
}
[Test]
public void IgnoreCR ()
{
byte[] input = { 13, 114, 108, 112, 55, 13, 81, 115, 61, 61, 13 };
byte[] expected = { 174, 90, 123, 66 };
TransformFinalBlock ("IgnoreCR", input, expected);
}
[Test]
public void IgnoreSPACE ()
{
byte[] input = { 32, 114, 108, 112, 55, 32, 81, 115, 61, 61, 32 };
byte[] expected = { 174, 90, 123, 66 };
TransformFinalBlock ("IgnoreSPACE", input, expected);
}
[Test]
[ExpectedException (typeof (FormatException))]
public void DontIgnore ()
{
byte[] input = { 7, 114, 108, 112, 55, 81, 115, 61, 61 };
byte[] expected = { 174, 90, 123, 66 };
TransformFinalBlock ("DontIgnore", input, expected);
}
[Test]
public void ReuseTransform ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
byte[] expected = { 174, 90, 123, 66 };
TransformFinalBlock ("UseTransform", input, expected);
TransformFinalBlock ("ReuseTransform", input, expected);
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void ReuseDisposedTransform ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
byte[] output = new byte [16];
_algo.Clear ();
_algo.TransformBlock (input, 0, input.Length, output, 0);
}
[Test]
[ExpectedException (typeof (ObjectDisposedException))]
public void ReuseDisposedTransformFinal ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
_algo.Clear ();
_algo.TransformFinalBlock (input, 0, input.Length);
}
[Test]
public void InvalidLength ()
{
byte[] input = { 114, 108, 112 };
byte[] result = _algo.TransformFinalBlock (input, 0, input.Length);
Assert.AreEqual (0, result.Length);
}
[Test]
public void InvalidData ()
{
byte[] input = { 114, 108, 112, 32 };
byte[] result = _algo.TransformFinalBlock (input, 0, input.Length);
Assert.AreEqual (0, result.Length);
}
[Test]
public void Dispose ()
{
byte[] input = { 114, 108, 112, 55, 81, 115, 61, 61 };
byte[] expected = { 174, 90, 123, 66 };
byte[] output = null;
using (ICryptoTransform t = new FromBase64Transform ()) {
output = t.TransformFinalBlock (input, 0, input.Length);
}
Assert.AreEqual (expected.Length, output.Length, "IDisposable");
for (int i = 0; i < expected.Length; i++)
Assert.AreEqual (expected[i], output[i], "IDisposable(" + i + ")");
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformBlock_Input_Null ()
{
byte[] output = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (null, 0, output.Length, output, 0);
}
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformBlock_InputOffset_Negative ()
{
byte[] input = new byte [16];
byte[] output = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (input, -1, input.Length, output, 0);
}
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformBlock_InputOffset_Overflow ()
{
byte[] input = new byte [16];
byte[] output = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (input, Int32.MaxValue, input.Length, output, 0);
}
}
[Test]
[ExpectedException (typeof (OverflowException))]
public void TransformBlock_InputCount_Negative ()
{
byte[] input = new byte [16];
byte[] output = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (input, 0, -1, output, 0);
}
}
[Test]
[ExpectedException (typeof (OutOfMemoryException))]
public void TransformBlock_InputCount_Overflow ()
{
byte[] input = new byte [16];
byte[] output = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (input, 0, Int32.MaxValue, output, 0);
}
}
[Test]
[ExpectedException (typeof (FormatException))]
public void TransformBlock_Output_Null ()
{
byte[] input = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (input, 0, input.Length, null, 0);
}
}
[Test]
[ExpectedException (typeof (FormatException))]
public void TransformBlock_OutputOffset_Negative ()
{
byte[] input = new byte [16];
byte[] output = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (input, 0, input.Length, output, -1);
}
}
[Test]
[ExpectedException (typeof (FormatException))]
public void TransformBlock_OutputOffset_Overflow ()
{
byte[] input = new byte [16];
byte[] output = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformBlock (input, 0, input.Length, output, Int32.MaxValue);
}
}
[Test]
[ExpectedException (typeof (ArgumentNullException))]
public void TransformFinalBlock_Input_Null ()
{
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformFinalBlock (null, 0, 16);
}
}
[Test]
[ExpectedException (typeof (ArgumentOutOfRangeException))]
public void TransformFinalBlock_InputOffset_Negative ()
{
byte[] input = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformFinalBlock (input, -1, input.Length);
}
}
[Test]
[ExpectedException (typeof (ArgumentException))]
public void TransformFinalBlock_InputOffset_Overflow ()
{
byte[] input = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformFinalBlock (input, Int32.MaxValue, input.Length);
}
}
[Test]
[ExpectedException (typeof (OverflowException))]
public void TransformFinalBlock_InputCount_Negative ()
{
byte[] input = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformFinalBlock (input, 0, -1);
}
}
[Test]
[ExpectedException (typeof (OutOfMemoryException))]
public void TransformFinalBlock_InputCount_Overflow ()
{
byte[] input = new byte [16];
using (ICryptoTransform t = new FromBase64Transform ()) {
t.TransformFinalBlock (input, 0, Int32.MaxValue);
}
}
}
}

View File

@@ -0,0 +1,217 @@
//
// HMACMD5Test.cs - NUnit Test Cases for HMACMD5
//
// Author:
// Sebastien Pouliot <sebastien@ximian.com>
//
// (C) 2003 Motus Technologies Inc. (http://www.motus.com)
// Copyright (C) 2006, 2007 Novell, Inc (http://www.novell.com)
//
#if NET_2_0
using NUnit.Framework;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace MonoTests.System.Security.Cryptography {
public class HM5 : HMACMD5 {
public int BlockSize {
get { return base.BlockSizeValue; }
set { base.BlockSizeValue = value; }
}
}
// References:
// a. IETF RFC2202: Test Cases for HMAC-MD5 and HMAC-SHA-1
// http://www.ietf.org/rfc/rfc2202.txt
[TestFixture]
public class HMACMD5Test : KeyedHashAlgorithmTest {
protected HMACMD5 algo;
[SetUp]
public override void SetUp ()
{
algo = new HMACMD5 ();
algo.Key = new byte [8];
hash = algo;
}
[Test]
public void Constructors ()
{
algo = new HMACMD5 ();
Assert.IsNotNull (algo, "HMACMD5 ()");
byte[] key = new byte [8];
algo = new HMACMD5 (key);
Assert.IsNotNull (algo, "HMACMD5 (key)");
}
[Test]
[ExpectedException (typeof (NullReferenceException))]
public void Constructor_Null ()
{
new HMACMD5 (null);
}
[Test]
public void Invariants ()
{
algo = new HMACMD5 ();
Assert.IsTrue (algo.CanReuseTransform, "HMACMD5.CanReuseTransform");
Assert.IsTrue (algo.CanTransformMultipleBlocks, "HMACMD5.CanTransformMultipleBlocks");
Assert.AreEqual ("MD5", algo.HashName, "HMACMD5.HashName");
Assert.AreEqual (128, algo.HashSize, "HMACMD5.HashSize");
Assert.AreEqual (1, algo.InputBlockSize, "HMACMD5.InputBlockSize");
Assert.AreEqual (1, algo.OutputBlockSize, "HMACMD5.OutputBlockSize");
Assert.AreEqual ("System.Security.Cryptography.HMACMD5", algo.ToString (), "HMACMD5.ToString()");
}
[Test]
public void BlockSize ()
{
HM5 hmac = new HM5 ();
Assert.AreEqual (64, hmac.BlockSize, "BlockSizeValue");
}
public void Check (string testName, byte[] key, byte[] data, byte[] result)
{
string classTestName = "HMACMD5-" + testName;
CheckA (testName, key, data, result);
CheckB (testName, key, data, result);
CheckC (testName, key, data, result);
CheckD (testName, key, data, result);
CheckE (testName, key, data, result);
}
public void CheckA (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new HMACMD5 ();
algo.Key = key;
byte[] hmac = algo.ComputeHash (data);
Assert.AreEqual (result, hmac, testName + "a1");
Assert.AreEqual (result, algo.Hash, testName + "a2");
}
public void CheckB (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new HMACMD5 ();
algo.Key = key;
byte[] hmac = algo.ComputeHash (data, 0, data.Length);
Assert.AreEqual (result, hmac, testName + "b1");
Assert.AreEqual (result, algo.Hash, testName + "b2");
}
public void CheckC (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new HMACMD5 ();
algo.Key = key;
MemoryStream ms = new MemoryStream (data);
byte[] hmac = algo.ComputeHash (ms);
Assert.AreEqual (result, hmac, testName + "c1");
Assert.AreEqual (result, algo.Hash, testName + "c2");
}
public void CheckD (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new HMACMD5 ();
algo.Key = key;
// LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
algo.TransformFinalBlock (data, 0, data.Length);
Assert.AreEqual (result, algo.Hash, testName + "d");
}
public void CheckE (string testName, byte[] key, byte[] data, byte[] result)
{
algo = new HMACMD5 ();
algo.Key = key;
byte[] copy = new byte [data.Length];
// LAMESPEC or FIXME: TransformFinalBlock doesn't return HashValue !
for (int i=0; i < data.Length - 1; i++)
algo.TransformBlock (data, i, 1, copy, i);
algo.TransformFinalBlock (data, data.Length - 1, 1);
Assert.AreEqual (result, algo.Hash, testName + "e");
}
[Test]
public void RFC2202_TC1 ()
{
byte[] key = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b };
byte[] data = Encoding.Default.GetBytes ("Hi There");
byte[] digest = { 0x92, 0x94, 0x72, 0x7a, 0x36, 0x38, 0xbb, 0x1c, 0x13, 0xf4, 0x8e, 0xf8, 0x15, 0x8b, 0xfc, 0x9d };
Check ("RFC2202-TC1", key, data, digest);
}
[Test]
public void RFC2202_TC2 ()
{
byte[] key = Encoding.Default.GetBytes ("Jefe");
byte[] data = Encoding.Default.GetBytes ("what do ya want for nothing?");
byte[] digest = { 0x75, 0x0c, 0x78, 0x3e, 0x6a, 0xb0, 0xb5, 0x03, 0xea, 0xa8, 0x6e, 0x31, 0x0a, 0x5d, 0xb7, 0x38 };
Check ("RFC2202-TC2", key, data, digest);
}
[Test]
public void RFC2202_TC3 ()
{
byte[] key = { 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa, 0xaa };
byte[] data = new byte [50];
for (int i = 0; i < data.Length; i++)
data[i] = 0xdd;
byte[] digest = { 0x56, 0xbe, 0x34, 0x52, 0x1d, 0x14, 0x4c, 0x88, 0xdb, 0xb8, 0xc7, 0x33, 0xf0, 0xe8, 0xb3, 0xf6 };
Check ("RFC2202-TC3", key, data, digest);
}
[Test]
public void RFC2202_TC4 ()
{
byte[] key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19 };
byte[] data = new byte [50];
for (int i = 0; i < data.Length; i++)
data[i] = 0xcd;
byte[] digest = { 0x69, 0x7e, 0xaf, 0x0a, 0xca, 0x3a, 0x3a, 0xea, 0x3a, 0x75, 0x16, 0x47, 0x46, 0xff, 0xaa, 0x79 };
Check ("RFC2202-TC4", key, data, digest);
}
[Test]
public void RFC2202_TC5 ()
{
byte[] key = { 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c, 0x0c };
byte[] data = Encoding.Default.GetBytes ("Test With Truncation");
byte[] digest = { 0x56, 0x46, 0x1e, 0xf2, 0x34, 0x2e, 0xdc, 0x00, 0xf9, 0xba, 0xb9, 0x95, 0x69, 0x0e, 0xfd, 0x4c };
Check ("RFC2202-TC5", key, data, digest);
}
[Test]
public void RFC2202_TC6 ()
{
byte[] key = new byte [80];
for (int i = 0; i < key.Length; i++)
key[i] = 0xaa;
byte[] data = Encoding.Default.GetBytes ("Test Using Larger Than Block-Size Key - Hash Key First");
byte[] digest = { 0x6b, 0x1a, 0xb7, 0xfe, 0x4b, 0xd7, 0xbf, 0x8f, 0x0b, 0x62, 0xe6, 0xce, 0x61, 0xb9, 0xd0, 0xcd };
Check ("RFC2202-TC6", key, data, digest);
}
[Test]
public void RFC2202_TC7 ()
{
byte[] key = new byte [80];
for (int i = 0; i < key.Length; i++)
key[i] = 0xaa;
byte[] data = Encoding.Default.GetBytes ("Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
byte[] digest = { 0x6f, 0x63, 0x0f, 0xad, 0x67, 0xcd, 0xa0, 0xee, 0x1f, 0xb1, 0xf5, 0x62, 0xdb, 0x3a, 0xa5, 0x3e };
Check ("RFC2202-TC7", key, data, digest);
}
}
}
#endif

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