public void _03_ByteArrayParameterTest() { Helpers.CheckPlatform(); byte[] parameter = new byte[16]; System.Random rng = new Random(); rng.NextBytes(parameter); // Create mechanism with the byte array parameter Mechanism mechanism = new Mechanism(CKM.CKM_AES_CBC, parameter); Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC)); // We access private Mechanism member here just for the testing purposes CK_MECHANISM ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism); Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC)); Assert.IsTrue(ckMechanism.Parameter != IntPtr.Zero); Assert.IsTrue(ckMechanism.ParameterLen == NativeLongUtils.ConvertFromInt32(parameter.Length)); parameter = null; // Create mechanism with null byte array parameter mechanism = new Mechanism(CKM.CKM_AES_CBC, parameter); Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC)); // We access private Mechanism member here just for the testing purposes ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism); Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_AES_CBC)); Assert.IsTrue(ckMechanism.Parameter == IntPtr.Zero); Assert.IsTrue(ckMechanism.ParameterLen == 0); }
public void _02_EmptyParameterTest() { Helpers.CheckPlatform(); // Create mechanism without the parameter Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS); Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_RSA_PKCS)); // We access private Mechanism member just for the testing purposes CK_MECHANISM ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism); Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_RSA_PKCS)); Assert.IsTrue(ckMechanism.Parameter == IntPtr.Zero); Assert.IsTrue(ckMechanism.ParameterLen == 0); }
public void _03_EncryptAndDecryptSinglePartOaepTest() { Helpers.CheckPlatform(); using (Pkcs11 pkcs11 = new Pkcs11(Settings.Pkcs11LibraryPath, Settings.AppType)) { // Find first slot with token present Slot slot = Helpers.GetUsableSlot(pkcs11); // Open RW session using (Session session = slot.OpenSession(SessionType.ReadWrite)) { // Login as normal user session.Login(CKU.CKU_USER, Settings.NormalUserPin); // Generate key pair ObjectHandle publicKey = null; ObjectHandle privateKey = null; Helpers.GenerateKeyPair(session, out publicKey, out privateKey); // Specify mechanism parameters CkRsaPkcsOaepParams mechanismParams = new CkRsaPkcsOaepParams(NativeLongUtils.ConvertFromCKM(CKM.CKM_SHA_1), NativeLongUtils.ConvertFromCKG(CKG.CKG_MGF1_SHA1), NativeLongUtils.ConvertFromUInt32(CKZ.CKZ_DATA_SPECIFIED), null); // Specify encryption mechanism with parameters Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS_OAEP, mechanismParams); byte[] sourceData = ConvertUtils.Utf8StringToBytes("Hello world"); // Encrypt data byte[] encryptedData = session.Encrypt(mechanism, publicKey, sourceData); // Do something interesting with encrypted data // Decrypt data byte[] decryptedData = session.Decrypt(mechanism, privateKey, encryptedData); // Do something interesting with decrypted data Assert.IsTrue(ConvertUtils.BytesToBase64String(sourceData) == ConvertUtils.BytesToBase64String(decryptedData)); session.DestroyObject(privateKey); session.DestroyObject(publicKey); session.Logout(); } } }
public void _04_ObjectParameterTest() { Helpers.CheckPlatform(); byte[] data = new byte[24]; System.Random rng = new Random(); rng.NextBytes(data); // Specify mechanism parameters CkKeyDerivationStringData parameter = new CkKeyDerivationStringData(data); // Create mechanism with the object as parameter Mechanism mechanism = new Mechanism(CKM.CKM_XOR_BASE_AND_DATA, parameter); Assert.IsTrue(mechanism.Type == NativeLongUtils.ConvertFromCKM(CKM.CKM_XOR_BASE_AND_DATA)); // We access private Mechanism member here just for the testing purposes CK_MECHANISM ckMechanism = (CK_MECHANISM)typeof(Mechanism).GetField("_ckMechanism", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(mechanism); Assert.IsTrue(ckMechanism.Mechanism == NativeLongUtils.ConvertFromCKM(CKM.CKM_XOR_BASE_AND_DATA)); Assert.IsTrue(ckMechanism.Parameter != IntPtr.Zero); Assert.IsTrue(NativeLongUtils.ConvertToInt32(ckMechanism.ParameterLen) == UnmanagedMemory.SizeOf(typeof(CK_KEY_DERIVATION_STRING_DATA))); }