public MFTestResults RNG2_Test() { bool bRes = true; try { using (Session sess = new Session("", MechanismType.RSA_PKCS)) { bRes &= Test(sess); } if (m_isEmulator) { using (Session sess = new Session("Emulator_Crypto", MechanismType.RSA_PKCS)) { bRes &= Test(sess); } } } catch (Exception e) { Log.Exception("", e); bRes = false; } return bRes ? MFTestResults.Pass : MFTestResults.Fail; }
/// <summary> /// Creates a Cryptoki signature object with the specified session context, algorithm and key. /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="mechanism">The signature algorithm and parameters.</param> /// <param name="key">The key used to sign the input data.</param> public CryptokiSign(Session session, Mechanism mechanism, CryptoKey key) : base(session, false) { m_signatureLength = (key.Size + 7) / 8; m_mech = mechanism; m_key = key; }
/// <summary> /// Initializes a new instance of the HashAlgorithm class. /// </summary> /// <param name="session">The Cryptoki session context the hash algorithm will execute in.</param> /// <param name="mechanism">The hash algorithm type</param> public HashAlgorithm(HashAlgorithmType hashAlgorithm, Session session) : base(session, false) { m_mechanism = new Mechanism((MechanismType)hashAlgorithm); m_hashSize = -1; Initialize(); }
bool SessionTest_CloseWithCreateKeyObjects_internal(string svcProvider) { bool res = true; CryptoKey key; using (Session sess = new Session(svcProvider, MechanismType.AES_CBC)) { AesCryptoServiceProvider aes = new AesCryptoServiceProvider(sess); aes.GenerateKey(); key = aes.Key; SymmetricTestHelper.Test_EncryptUpdate(aes); } try { using (AesCryptoServiceProvider aes = new AesCryptoServiceProvider(svcProvider)) { aes.Key = key; SymmetricTestHelper.Test_EncryptUpdate(aes); } res = false; } catch (Exception) { } return res; }
bool testRng(Session session) { RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(session); byte[] data1 = new byte[1024]; byte[] data2 = new byte[1024]; byte[] data3 = new byte[1024]; rng.GetBytes(data1); rng.GetBytes(data2); rng.Dispose(); rng = new RNGCryptoServiceProvider(session); rng.GetBytes(data3); rng.Dispose(); int same = 0; for (int i = 0; i < data1.Length; i++) { if (data1[i] == data2[i] || data1[i] == data3[i] || data2[i] == data3[i]) same++; } return same < 32; // ~3% matching elements }
public MFTestResults X509CertificateLoadCertsFromBlob_Test() { bool bRet = true; X509CertificateLoadCertsFromBlob x509; //TestLibrary.TestFramework.BeginTestCase("X509CertificateLoadCertsFromBlob"); x509 = new X509CertificateLoadCertsFromBlob(); c_BYTES = Properties.Resources.GetBytes(Properties.Resources.BinaryResources.cacert); using(Session session = new Session("", MechanismType.RSA_PKCS)) { bRet &= x509.RunTests(session); } if(m_isEmulator) { using (Session session = new Session("Emulator_Crypto", MechanismType.RSA_PKCS)) { bRet &= x509.RunTests(session); } } return bRet ? MFTestResults.Pass : MFTestResults.Fail; }
public MFTestResults DsaTest_ImportDsaKey() { MFTestResults res; try { using (Session session = new Session("", MechanismType.DSA)) { res = Test_ImportKey(session); } if (res == MFTestResults.Pass && m_isEmulator) { using (Session session = new Session("Emulator_Crypto", MechanismType.DSA)) { res = Test_ImportKey(session); } } } catch { res = MFTestResults.Fail; } return res; }
public MFTestResults Zeros_Test() { bool bRes = true; try { Zeros tester = new Zeros(); using (Session sess = new Session("", MechanismType.AES_ECB)) { bRes &= tester.RunTests(sess, PaddingMode.Zeros); } if (m_isEmulator) { using (Session sess = new Session("Emulator_Crypto", MechanismType.AES_ECB)) { bRes &= tester.RunTests(sess, PaddingMode.Zeros); } } } catch (NotSupportedException) { return MFTestResults.Skip; } catch (Exception e) { Log.Exception("", e); bRes = false; } return bRes ? MFTestResults.Pass : MFTestResults.Fail; }
/// <summary> /// Initializes a new instance of the SymmetricAlgorithm class. /// </summary> /// <param name="session">The cryptoki session context for which the symmectric algorithm will execute.</param> /// <param name="ownsSession">true if the session should be closed by this base class, false otherwise.</param> protected SymmetricAlgorithm(Session session, bool ownsSession) : base(session, ownsSession) { // Default to cipher block chaining (CipherMode.CBC) and // PKCS-style padding (pad n bytes with value n) ModeValue = CipherMode.CBC; PaddingValue = PaddingMode.PKCS7; }
/// <summary> /// Creates a object in the given Cryptoki session context with specified object atrributes. /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="template">The object attribute template.</param> /// <returns>The cryptoki object created.</returns> public static CryptokiObject CreateObject(Session session, CryptokiAttribute[] template) { CryptokiObject ret = CreateObjectInternal(session, template); session.AddSessionObject(ret); return ret; }
/// <summary> /// Creates a CryptoKey in the specfied session context with the specified key attribute template. /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="keyTemplate">The Cryptoki attribute template that specifies key properties.</param> /// <returns></returns> public static CryptoKey LoadKey(Session session, CryptokiAttribute[] keyTemplate) { CryptoKey key = CryptokiObject.CreateObject(session, keyTemplate) as CryptoKey; key.m_keyType = KeyType.INVALID; return key; }
static Boolean Test(Session session) { Boolean bRes = true; Byte[] abData1 = { (Byte)'a', (Byte)'b', (Byte)'c' }; Byte[] abDigest1 = {0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba, 0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31, 0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2, 0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a, 0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8, 0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd, 0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e, 0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f}; String sData2 = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; Byte[] abData2 = new Byte[sData2.Length]; for (int i = 0; i < sData2.Length; i++) abData2[i] = (Byte)sData2[i]; Byte[] abDigest2 = {0x8e, 0x95, 0x9b, 0x75, 0xda, 0xe3, 0x13, 0xda, 0x8c, 0xf4, 0xf7, 0x28, 0x14, 0xfc, 0x14, 0x3f, 0x8f, 0x77, 0x79, 0xc6, 0xeb, 0x9f, 0x7f, 0xa1, 0x72, 0x99, 0xae, 0xad, 0xb6, 0x88, 0x90, 0x18, 0x50, 0x1d, 0x28, 0x9e, 0x49, 0x00, 0xf7, 0xe4, 0x33, 0x1b, 0x99, 0xde, 0xc4, 0xb5, 0x43, 0x3a, 0xc7, 0xd3, 0x29, 0xee, 0xb6, 0xdd, 0x26, 0x54, 0x5e, 0x96, 0xe5, 0x5b, 0x87, 0x4b, 0xe9, 0x09}; Log.Comment("Testing SHA1 hash..."); HashAlgorithm sha1 = new HashAlgorithm(HashAlgorithmType.SHA512, session); HashAlgorithm sha2 = new HashAlgorithm(HashAlgorithmType.SHA512, session); sha1.ComputeHash(abData1); sha2.ComputeHash(abData2); Log.Comment("The computed hash #1 is : "); PrintByteArray(sha1.Hash); Log.Comment("The correct hash #1 is : "); PrintByteArray(abDigest1); if (Compare(sha1.Hash, abDigest1)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } Log.Comment("The computed hash #2 is : "); PrintByteArray(sha2.Hash); Log.Comment("The correct hash #2 is : "); PrintByteArray(abDigest2); if (Compare(sha2.Hash, abDigest2)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } return bRes; }
/// <summary> /// Creates the encryptor object with the specified session context, decryption algorithm, key, and input/output block sizes /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="mechanism">The encryption algorithm and paramters.</param> /// <param name="key">The key that will be used to perform the encryption.</param> /// <param name="inputBlockSize">The input block size, in bits.</param> /// <param name="outputBlockSize">The output block size, in bits.</param> public Encryptor(Session session, Mechanism mechanism, CryptoKey key, int inputBlockSize, int outputBlockSize) : base(session, false) { m_inputBlockSize = (inputBlockSize + 7) / 8; m_outputBlockSize = (outputBlockSize+ 7) / 8; m_mech = mechanism; m_key = key; }
static Boolean Test(Session session) { Boolean bRes = true; Byte[] abKey1 = { 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b }; Byte[] abData1 = (new System.Text.UTF8Encoding()).GetBytes("Hi There"); Byte[] abDigest1 = { 0xb6, 0x17, 0x31, 0x86, 0x55, 0x05, 0x72, 0x64, 0xe2, 0x8b, 0xc0, 0xb6, 0xfb, 0x37, 0x8c, 0x8e, 0xf1, 0x46, 0xbe, 0x00 }; Byte[] abKey2 = (new System.Text.UTF8Encoding()).GetBytes("Jefe"); Byte[] abData2 = (new System.Text.UTF8Encoding()).GetBytes("what do ya want for nothing?"); Byte[] abDigest2 = { 0xef, 0xfc, 0xdf, 0x6a, 0xe5, 0xeb, 0x2f, 0xa2, 0xd2, 0x74, 0x16, 0xd5, 0xf1, 0x84, 0xdf, 0x9c, 0x25, 0x9a, 0x7c, 0x79 }; CryptoKey key1 = CryptoKey.LoadKey(session, new CryptokiAttribute[] { new CryptokiAttribute(CryptokiAttribute.CryptokiType.Class , Utility.ConvertToBytes((int)CryptokiClass.SECRET_KEY)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.KeyType, Utility.ConvertToBytes((int)CryptoKey.KeyType.GENERIC_SECRET)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.Value , abKey1) }); CryptoKey key2 = CryptoKey.LoadKey(session, new CryptokiAttribute[] { new CryptokiAttribute(CryptokiAttribute.CryptokiType.Class , Utility.ConvertToBytes((int)CryptokiClass.SECRET_KEY)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.KeyType, Utility.ConvertToBytes((int)CryptoKey.KeyType.GENERIC_SECRET)), new CryptokiAttribute(CryptokiAttribute.CryptokiType.Value , abKey2) }); Log.Comment("Testing rc21 hash..."); using(KeyedHashAlgorithm rc21 = new KeyedHashAlgorithm(KeyedHashAlgorithmType.HMACSHA1, key1)) using (KeyedHashAlgorithm rc22 = new KeyedHashAlgorithm(KeyedHashAlgorithmType.HMACSHA1, key2)) { rc21.ComputeHash(abData1); rc22.ComputeHash(abData2); Log.Comment("The computed hash #1 is : "); PrintByteArray(rc21.Hash); Log.Comment("The correct hash #1 is : "); PrintByteArray(abDigest1); if (Compare(rc21.Hash, abDigest1)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } Log.Comment("The computed hash #2 is : "); PrintByteArray(rc22.Hash); Log.Comment("The correct hash #2 is : "); PrintByteArray(abDigest2); if (Compare(rc22.Hash, abDigest2)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } } return bRes; }
/// <summary> /// Creates the Cryptoki digest object with specified session context, digest algorithm, and hash size. /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="mechanism">The digest algorithm and paramters.</param> /// <param name="hashSize">The size of the resulting hash value, in bits.</param> public CryptokiDigest(Session session, Mechanism mechanism, int hashSize) : base(session, false) { m_hashSize = (hashSize + 7) / 8; if (m_hashSize == 0) throw new ArgumentException(); m_mechanism = mechanism; }
/// <summary> /// Initializes a new instance of the X509Certificate2 class from a byte array /// </summary> /// <param name="session">Cryptoki session for which this certificate will be created</param> /// <param name="data">Data bytes for the certificate (PEM, DER, P12, etc.)</param> /// <param name="password">Password for decrypting the certificate data (optional)</param> public X509Certificate2(Session session, byte[] data, string password="") { if (data == null || data.Length == 0) throw new ArgumentException(); m_cert = CryptokiCertificate.LoadCertificate(session, data, password); Init(); }
/// <summary> /// Creates a session container object with the specified session context. /// </summary> /// <param name="session">The Cryptoki session context.</param> /// <param name="ownsSession">Determines if the container disposes the session object.</param> protected SessionContainer(Session session, bool ownsSession) { m_ownsSession = ownsSession; m_session = session; if (!ownsSession) { session.AddSessionObject(this); } }
/// <summary> /// Run a round trip test -- the data should encrypt and decrypt back to itself /// </summary> /// <param name="key">key to use</param> /// <param name="iv">IV to use</param> /// <param name="text">data to encrypt</param> /// <param name="padding">padding method to use</param> /// <returns>true if text encrypted and decrypted back to itself</returns> protected bool RunRoundTrip(Session session, byte[] key, byte[] iv, byte[] text, PaddingMode padding) { try { Log.Comment("Encrypting the following bytes:"); PrintByteArray(text); // setup the encryption provider AesCryptoServiceProvider aes = new AesCryptoServiceProvider(CryptoKey.ImportKey(session, key, CryptoKey.KeyClass.Secret, CryptoKey.KeyType.AES, true)); aes.IV = iv; aes.Mode = CipherMode.ECB; aes.Padding = padding; // encrypt the data ICryptoTransform sse = aes.CreateEncryptor(); //MemoryStream ms = new MemoryStream(); //CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write); //cs.Write(text, 0, text.Length); //cs.FlushFinalBlock(); byte[] cipherText = sse.TransformFinalBlock(text, 0, text.Length); //ms.ToArray(); //cs.Close(); Log.Comment("Cyphertext:"); PrintByteArray(cipherText); Log.Comment("Decrypting..."); ICryptoTransform ssd = aes.CreateDecryptor(); //cs = new CryptoStream(new MemoryStream(cipherText), ssd, CryptoStreamMode.Read); // decrypt the data byte[] newPlainText = ssd.TransformFinalBlock(cipherText, 0, cipherText.Length); // new byte[text.Length]; //cs.Read(newPlainText, 0, text.Length); Log.Comment("Plaintext:"); PrintByteArray(newPlainText); // make sure the roundtrip worked if (!Compare(text, newPlainText)) { Log.Comment("ERROR: roundtrip failed"); return false; } } catch (NotSupportedException) { throw; } catch { return false; } return true; }
public MFTestResults RsaTest_ExportImportTest() { bool testResult = true; try { using (Session session = new Session("", MechanismType.RSA_PKCS)) using (CryptoKey privateKey = CryptoKey.LoadKey(session, m_importKeyPrivate)) { string dataToSign = "This is a simple message to be encrypted"; byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(dataToSign); using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(privateKey)) { RSAParameters kp1 = rsa.ExportParameters(true); byte[] sig = rsa.SignData(data); rsa.ImportParameters(kp1); RSAParameters kp2 = rsa.ExportParameters(true); testResult &= CompareByteArray(kp1.D, kp2.D); testResult &= CompareByteArray(kp1.DP, kp2.DP); testResult &= CompareByteArray(kp1.DQ, kp2.DQ); testResult &= CompareByteArray(kp1.Exponent, kp2.Exponent); testResult &= CompareByteArray(kp1.InverseQ, kp2.InverseQ); testResult &= CompareByteArray(kp1.Modulus, kp2.Modulus); testResult &= CompareByteArray(kp1.P, kp2.P); testResult &= CompareByteArray(kp1.Q, kp2.Q); testResult &= CompareByteArray(m_importKeyPrivate[2].Value, kp1.Modulus); testResult &= CompareByteArray(m_importKeyPrivate[3].Value, kp1.Exponent); testResult &= CompareByteArray(m_importKeyPrivate[4].Value, kp1.D); testResult &= CompareByteArray(m_importKeyPrivate[5].Value, kp1.P); testResult &= CompareByteArray(m_importKeyPrivate[6].Value, kp1.Q); testResult &= CompareByteArray(m_importKeyPrivate[7].Value, kp1.DP); testResult &= CompareByteArray(m_importKeyPrivate[8].Value, kp1.DQ); testResult &= CompareByteArray(m_importKeyPrivate[9].Value, kp1.InverseQ); testResult &= rsa.VerifyData(data, sig); } } } catch (Exception ex) { Log.Exception("Unexpected Exception", ex); testResult = false; } return (testResult ? MFTestResults.Pass : MFTestResults.Fail); }
// The SHA{256,284,512}CSP algorithms are only supported on Win2k3 and higher (version 5.2+) // public static void InitAlgorithmList(Session s1, Session s2) { m_AlgorithmPairs = new AlgorithmPairs[] { new AlgorithmPairs( new HashAlgorithm(HashAlgorithmType.SHA1 , s1), new HashAlgorithm(HashAlgorithmType.SHA1 , s2) ), new AlgorithmPairs( new HashAlgorithm(HashAlgorithmType.SHA256, s1), new HashAlgorithm(HashAlgorithmType.SHA256, s2) ), new AlgorithmPairs( new HashAlgorithm(HashAlgorithmType.SHA384, s1), new HashAlgorithm(HashAlgorithmType.SHA384, s2) ), new AlgorithmPairs( new HashAlgorithm(HashAlgorithmType.SHA512, s1), new HashAlgorithm(HashAlgorithmType.SHA512, s2) ), new AlgorithmPairs( new HashAlgorithm(HashAlgorithmType.MD5 , s1), new HashAlgorithm(HashAlgorithmType.MD5 , s2) ), }; }
static Boolean Test(Session session) { Boolean bRes = true; Byte[] abData1 = { (Byte)'a', (Byte)'b', (Byte)'c' }; Byte[] abDigest1 = {0xcb, 0x00, 0x75, 0x3f, 0x45, 0xa3, 0x5e, 0x8b, 0xb5, 0xa0, 0x3d, 0x69, 0x9a, 0xc6, 0x50, 0x07, 0x27, 0x2c, 0x32, 0xab, 0x0e, 0xde, 0xd1, 0x63, 0x1a, 0x8b, 0x60, 0x5a, 0x43, 0xff, 0x5b, 0xed, 0x80, 0x86, 0x07, 0x2b, 0xa1, 0xe7, 0xcc, 0x23, 0x58, 0xba, 0xec, 0xa1, 0x34, 0xc8, 0x25, 0xa7}; String sData2 = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu"; Byte[] abData2 = new Byte[sData2.Length]; for (int i = 0; i < sData2.Length; i++) abData2[i] = (Byte)sData2[i]; Byte[] abDigest2 = {0x09, 0x33, 0x0c, 0x33, 0xf7, 0x11, 0x47, 0xe8, 0x3d, 0x19, 0x2f, 0xc7, 0x82, 0xcd, 0x1b, 0x47, 0x53, 0x11, 0x1b, 0x17, 0x3b, 0x3b, 0x05, 0xd2, 0x2f, 0xa0, 0x80, 0x86, 0xe3, 0xb0, 0xf7, 0x12, 0xfc, 0xc7, 0xc7, 0x1a, 0x55, 0x7e, 0x2d, 0xb9, 0x66, 0xc3, 0xe9, 0xfa, 0x91, 0x74, 0x60, 0x39}; Log.Comment("Testing SHA1 hash..."); HashAlgorithm sha1 = new HashAlgorithm(HashAlgorithmType.SHA384, session); HashAlgorithm sha2 = new HashAlgorithm(HashAlgorithmType.SHA384, session); sha1.ComputeHash(abData1); sha2.ComputeHash(abData2); Log.Comment("The computed hash #1 is : "); PrintByteArray(sha1.Hash); Log.Comment("The correct hash #1 is : "); PrintByteArray(abDigest1); if (Compare(sha1.Hash, abDigest1)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } Log.Comment("The computed hash #2 is : "); PrintByteArray(sha2.Hash); Log.Comment("The correct hash #2 is : "); PrintByteArray(abDigest2); if (Compare(sha2.Hash, abDigest2)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } return bRes; }
// returns all nonabstract hash classes public static HashAlgorithm[] GetHashClasses(Session session) { HashAlgorithm[] alRes = new HashAlgorithm[] { new HashAlgorithm(HashAlgorithmType.SHA1 , session), new HashAlgorithm(HashAlgorithmType.SHA256, session), new HashAlgorithm(HashAlgorithmType.SHA384, session), new HashAlgorithm(HashAlgorithmType.SHA512, session), }; return alRes; }
static Boolean Test(Session session) { Boolean bRes = true; Byte[] abData1 = { (Byte)'a', (Byte)'b', (Byte)'c' }; Byte[] abDigest1 = {0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad}; String sData2 = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"; Byte[] abData2 = new Byte[sData2.Length]; for (int i = 0; i < sData2.Length; i++) abData2[i] = (Byte)sData2[i]; Byte[] abDigest2 = {0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1}; Log.Comment("Testing SHA1 hash..."); HashAlgorithm sha1 = new HashAlgorithm(HashAlgorithmType.SHA256, session); HashAlgorithm sha2 = new HashAlgorithm(HashAlgorithmType.SHA256, session); sha1.ComputeHash(abData1); sha2.ComputeHash(abData2); Log.Comment("The computed hash #1 is : "); PrintByteArray(sha1.Hash); Log.Comment("The correct hash #1 is : "); PrintByteArray(abDigest1); if (Compare(sha1.Hash, abDigest1)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } Log.Comment("The computed hash #2 is : "); PrintByteArray(sha2.Hash); Log.Comment("The correct hash #2 is : "); PrintByteArray(abDigest2); if (Compare(sha2.Hash, abDigest2)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } return bRes; }
static int[] ArraySizes = new int[] { /*0,*/ 1, 127, 128, 129, Byte.MaxValue, Byte.MaxValue - 1, Byte.MaxValue + 1 }; //, Int16.MaxValue, Int16.MaxValue + 1, Int16.MaxValue - 1, Int32.MaxValue / 8, Int32.MaxValue / 8 + 1, Int32.MaxValue / 8 - 1 }; //static private byte[] Int32MAXDevBy8; //static private byte[] Int32MAXDevBy8Plus1; //static private byte[] Int32MAXDevBy8Minus1; public static bool Test(Session session) { bool bRet = true; //Int32MAXDevBy8 = CreateByteArray(Int32.MaxValue / 8); //Int32MAXDevBy8Plus1 = CreateByteArray(Int32.MaxValue / 8 + 1); //Int32MAXDevBy8Minus1 = CreateByteArray(Int32.MaxValue / 8 - 1); // Make sure RSAServiceProvider can handle various exception scenarios if (!RunRSATest(session)) bRet = false; return bRet; }
public MFTestResults Hash_SHA512_Test() { bool bRes = true; Random rand = new Random(); int maxLength = DefaultMaxLength; int iterations = DefaultIterations; try { if (m_isEmulator) { using (Session sess = new Session("", MechanismType.SHA512)) using (Session sess2 = new Session("Emulator_Crypto", MechanismType.SHA512)) { InitAlgorithmList(sess, sess2); for (int i = 0; i < iterations; i++) { // Create hash data -- random byte length with random data // byte[] data = new byte[rand.Next(maxLength + 1) + 1]; rand.NextBytes(data); // For the given data, compare the hash value produced by each pair of hash algorithms // for (int j = 0; j < m_AlgorithmPairs.Length; j++) { if (!CompareHashes(m_AlgorithmPairs[j].Hash1, m_AlgorithmPairs[j].Hash2, data)) { Log.Comment("Hash mismatch, test fails"); bRes = false; } } } } } else { return MFTestResults.Skip; } } catch (Exception e) { Log.Exception("", e); bRes = false; } return bRes ? MFTestResults.Pass : MFTestResults.Fail; }
static Boolean Test(Session session) { try { Byte[] PlainText = { 0, 1, 2, 3, 4, 5, 6, 7 }; //, 8, 9, 10, 11, 12, 13, 14, 15}; Byte[] Key = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }; Byte[] IV = { 0, 0, 0, 0, 0, 0, 0, 0 }; TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider(session); des.Key = CryptoKey.ImportKey(session, Key, CryptoKey.KeyClass.Secret, CryptoKey.KeyType.DES3, true); return false; } catch { return true; } }
public bool Test(Session session) { // The RSA Parameter is extracted from Desktop x509certificate provided by WLC team RSACryptoServiceProvider key = new RSACryptoServiceProvider(session); key.ImportParameters(WLCRSAData.GetKeyParameters()); bool b = VerifyTicketHelper(ticket, signature, key); if (b) { Log.Comment("Pass"); return true; } else { Log.Comment("Fail"); return false; } }
/// <summary> /// Initializes a new instance of the X509Store class using the specified StoreName value. /// </summary> /// <param name="session">Cryptoki session for which this store is to be used.</param> /// <param name="storeName">One of the enumeration values that specifies the name of the X.509 certificate store.</param> public X509Store (Session session, StoreName storeName) { m_session = session; switch (storeName) { case StoreName.Disallowed: m_storeName = "Disallowed"; break; case StoreName.My: m_storeName = "My"; break; case StoreName.CA: m_storeName = "CA"; break; default: throw new ArgumentException(); } }
public static Boolean Test(Session session) { Byte[] barr1, barr2; RNGCryptoServiceProvider rndcsp = new RNGCryptoServiceProvider(session); int l; for (int i = 0; i < 1000; i++) { l = Rnd.Next() % 1500 + 4; barr1 = new Byte[l]; barr2 = new Byte[l]; rndcsp.GetBytes(barr1); rndcsp.GetBytes(barr2); if (Compare(barr1, barr2)) return false; } return true; }
static Boolean Test(Session session) { Boolean bRes = true; Byte[] abData1 = { (Byte)'a', (Byte)'b', (Byte)'c' }; Byte[] abDigest1 = { 0x90, 0x01, 0x50, 0x98, 0x3c, 0xd2, 0x4f, 0xb0, 0xd6, 0x96, 0x3f, 0x7d, 0x28, 0xe1, 0x7f, 0x72 }; String sData2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; Byte[] abData2 = new Byte[sData2.Length]; for (int i = 0; i < sData2.Length; i++) abData2[i] = (Byte)sData2[i]; Byte[] abDigest2 = { 0xd1, 0x74, 0xab, 0x98, 0xd2, 0x77, 0xd9, 0xf5, 0xa5, 0x61, 0x1c, 0x2c, 0x9f, 0x41, 0x9d, 0x9f }; Log.Comment("Testing rc21 hash..."); HashAlgorithm rc21 = new HashAlgorithm(HashAlgorithmType.MD5, session); HashAlgorithm rc22 = new HashAlgorithm(HashAlgorithmType.MD5, session); rc21.ComputeHash(abData1); rc22.ComputeHash(abData2); Log.Comment("The computed hash #1 is : "); PrintByteArray(rc21.Hash); Log.Comment("The correct hash #1 is : "); PrintByteArray(abDigest1); if (Compare(rc21.Hash, abDigest1)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } Log.Comment("The computed hash #2 is : "); PrintByteArray(rc22.Hash); Log.Comment("The correct hash #2 is : "); PrintByteArray(abDigest2); if (Compare(rc22.Hash, abDigest2)) { Log.Comment("CORRECT"); } else { Log.Comment("INCORRECT"); bRes = false; } return bRes; }