Exemple #1
0
        public void RsaWrapUnwrapKey(KeyWrapTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RsaWrapUnwrapKey", theoryData);

            try
            {
                var    encryptProvider = new RsaKeyWrapProvider(theoryData.WrapKey, theoryData.WrapAlgorithm, false);
                var    wrappedKey      = encryptProvider.WrapKey(theoryData.KeyToWrap);
                var    decryptProvider = new DerivedRsaKeyWrapProvider(theoryData.UnwrapKey, theoryData.UnwrapAlgorithm, true);
                byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);

                if (!Utility.AreEqual(unwrappedKey, theoryData.KeyToWrap))
                {
                    context.AddDiff("theoryParams.KeyToWrap != unwrappedKey");
                }

                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
        public void Constructors(KeyWrapTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.Constructors", theoryData);

            try
            {
                RsaKeyWrapProvider provider = null;
                var keyWrapContext          = Guid.NewGuid().ToString();
                if (theoryData.WillUnwrap)
                {
                    provider = new RsaKeyWrapProvider(theoryData.UnwrapKey, theoryData.UnwrapAlgorithm, theoryData.WillUnwrap)
                    {
                        Context = keyWrapContext
                    };
                    provider.CreateAsymmetricAdapter();

                    if (!provider.Algorithm.Equals(theoryData.UnwrapAlgorithm))
                    {
                        context.AddDiff($"provider.Algorithm != theoryData.UnwrapAlgorithm: {provider.Algorithm} : {theoryData.UnwrapAlgorithm}.");
                    }

                    if (!ReferenceEquals(provider.Key, theoryData.UnwrapKey))
                    {
                        context.AddDiff($"!ReferenceEquals(provider.key, theoryData.UnwrapKey)");
                    }
                }
                else
                {
                    provider = new RsaKeyWrapProvider(theoryData.WrapKey, theoryData.WrapAlgorithm, theoryData.WillUnwrap)
                    {
                        Context = keyWrapContext
                    };

                    provider.WrapKey(Guid.NewGuid().ToByteArray());

                    if (!provider.Algorithm.Equals(theoryData.WrapAlgorithm))
                    {
                        context.AddDiff($"provider.Algorithm != theoryData.WrapAlgorithm: {provider.Algorithm} : {theoryData.WrapAlgorithm}.");
                    }

                    if (!ReferenceEquals(provider.Key, theoryData.WrapKey))
                    {
                        context.AddDiff($"!ReferenceEquals(provider.key, theoryData.WrapKey)");
                    }
                }

                theoryData.ExpectedException.ProcessNoException(context);
                if (!provider.Context.Equals(keyWrapContext))
                {
                    context.AddDiff($"provider.Context != keyWrapContext: {provider.Context} : {keyWrapContext}.");
                }
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Exemple #3
0
 public void RsaUnwrapMismatch(RsaKeyWrapTestParams theoryParams)
 {
     try
     {
         var    encryptProvider = new RsaKeyWrapProvider(theoryParams.EncryptKey, theoryParams.EncryptAlgorithm, false);
         byte[] keyToWrap       = Guid.NewGuid().ToByteArray();
         var    wrappedKey      = encryptProvider.WrapKey(keyToWrap);
         var    decryptProvider = new RsaKeyWrapProvider(theoryParams.DecryptKey, theoryParams.DecryptAlgorithm, true);
         byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);
         theoryParams.EE.ProcessNoException();
     }
     catch (Exception ex)
     {
         theoryParams.EE.ProcessException(ex);
     }
 }
Exemple #4
0
        public void RsaWrapUnwrapKey(RsaKeyWrapTestParams theoryParams)
        {
            try
            {
                var    encryptProvider = new RsaKeyWrapProvider(theoryParams.EncryptKey, theoryParams.EncryptAlgorithm, false);
                var    wrappedKey      = encryptProvider.WrapKey(theoryParams.KeyToWrap);
                var    decryptProvider = new DerivedRsaKeyWrapProvider(theoryParams.DecryptKey, theoryParams.DecryptAlgorithm, true);
                byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);

                Assert.True(Utility.AreEqual(unwrappedKey, theoryParams.KeyToWrap), "theoryParams.KeyToWrap != unwrappedKey");

                theoryParams.EE.ProcessNoException();
            }
            catch (Exception ex)
            {
                theoryParams.EE.ProcessException(ex);
            }
        }
Exemple #5
0
        public void RsaUnwrapMismatch(KeyWrapTheoryData theoryData)
        {
            var context = TestUtilities.WriteHeader($"{this}.RsaUnwrapParameterCheck", theoryData);

            try
            {
                var    encryptProvider = new RsaKeyWrapProvider(theoryData.WrapKey, theoryData.WrapAlgorithm, false);
                byte[] keyToWrap       = Guid.NewGuid().ToByteArray();
                var    wrappedKey      = encryptProvider.WrapKey(keyToWrap);
                var    decryptProvider = new RsaKeyWrapProvider(theoryData.UnwrapKey, theoryData.UnwrapAlgorithm, true);
                byte[] unwrappedKey    = decryptProvider.UnwrapKey(wrappedKey);
                theoryData.ExpectedException.ProcessNoException(context);
            }
            catch (Exception ex)
            {
                theoryData.ExpectedException.ProcessException(ex, context);
            }

            TestUtilities.AssertFailIfErrors(context);
        }
Exemple #6
0
        private static void AddUnwrapTamperedTheoryData(
            string testId,
            SecurityKey encrtyptKey,
            SecurityKey decryptKey,
            string algorithm,
            TheoryData <RsaKeyWrapTestParams> theoryData)
        {
            var keyToWrap  = Guid.NewGuid().ToByteArray();
            var provider   = new RsaKeyWrapProvider(encrtyptKey, algorithm, false);
            var wrappedKey = provider.WrapKey(keyToWrap);

            TestUtilities.XORBytes(wrappedKey);
            theoryData.Add(new RsaKeyWrapTestParams
            {
                DecryptAlgorithm = algorithm,
                DecryptKey       = decryptKey,
                EE         = ExpectedException.KeyWrapException("IDX10659:"),
                Provider   = provider,
                WrappedKey = wrappedKey
            });
        }