コード例 #1
1
 public ElGamalManaged()
 {
     // create the key struct
     o_key_struct = new ElGamalKeyStruct();
     // set all of the big integers to zero
     o_key_struct.P = new BigInteger(0);
     o_key_struct.G = new BigInteger(0);
     o_key_struct.Y = new BigInteger(0);
     o_key_struct.X = new BigInteger(0);
     // set the default key size value
     KeySizeValue = 1024;
     // set the range of legal keys
     LegalKeySizesValue = new KeySizes[] { new KeySizes(384, 1088, 8) };
 }
コード例 #2
0
ファイル: DSAKeyGeneration.cs プロジェクト: shmao/corefx
        private static int GetSecondMin(KeySizes[] keySizes)
        {
            int secondMin = int.MaxValue;
            int min = secondMin;

            foreach (var keySize in keySizes)
            {
                int localMin = keySize.MinSize;

                if (localMin < min)
                {
                    secondMin = min;
                    min = localMin;
                }
                else if (localMin < secondMin)
                {
                    secondMin = localMin;
                }

                if (keySize.MaxSize != keySize.MinSize)
                {
                    int secondLocal = localMin + keySize.SkipSize;

                    if (secondLocal < secondMin)
                    {
                        secondMin = secondLocal;
                    }
                }
            }

            return secondMin;
        }
コード例 #3
0
		public DSACryptoServiceProvider (int dwKeySize, CspParameters parameters)
		{
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);

			// will throw an exception is key size isn't supported
			KeySize = dwKeySize;
			dsa = new DSAManaged (dwKeySize);
			dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);

			persistKey = (parameters != null);
			if (parameters == null) {
				parameters = new CspParameters (PROV_DSS_DH);
				if (useMachineKeyStore)
					parameters.Flags |= CspProviderFlags.UseMachineKeyStore;
				store = new KeyPairPersistence (parameters);
				// no need to load - it cannot exists
			}
			else {
				store = new KeyPairPersistence (parameters);
				store.Load ();
				if (store.KeyValue != null) {
					persisted = true;
					this.FromXmlString (store.KeyValue);
				}
			}
		}
コード例 #4
0
ファイル: EcDsaOpenSslTests.cs プロジェクト: SGuyGe/corefx
        private static bool IsLegalSize(int size, KeySizes[] legalSizes)
        {
            for (int i = 0; i < legalSizes.Length; i++)
            {
                KeySizes currentSizes = legalSizes[i];

                // If a cipher has only one valid key size, MinSize == MaxSize and SkipSize will be 0
                if (currentSizes.SkipSize == 0)
                {
                    if (currentSizes.MinSize == size)
                        return true;
                }
                else if (size >= currentSizes.MinSize && size <= currentSizes.MaxSize)
                {
                    // If the number is in range, check to see if it's a legal increment above MinSize
                    int delta = size - currentSizes.MinSize;

                    // While it would be unusual to see KeySizes { 10, 20, 5 } and { 11, 14, 1 }, it could happen.
                    // So don't return false just because this one doesn't match.
                    if (delta % currentSizes.SkipSize == 0)
                    {
                        return true;
                    }
                }
            }
            return false;
        }
コード例 #5
0
        protected Aes() {
            KeySizeValue = 256;
            BlockSizeValue = 128;
            FeedbackSizeValue = 128;

            LegalKeySizesValue = new KeySizes[1];
            LegalKeySizesValue[0] = new KeySizes(128, 256, 64);

            LegalBlockSizesValue = new KeySizes[1];
            LegalBlockSizesValue[0] = new KeySizes(128, 128, 0);
        }
コード例 #6
0
ファイル: Null.cs プロジェクト: Jakosa/MonoLibraries
		public Null () 
		{
			KeySizeValue = 128;
			BlockSizeValue = 128;
			FeedbackSizeValue = 128;
	
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (0, 1024, 8);

			LegalBlockSizesValue = new KeySizes [1];
			LegalBlockSizesValue [0] = new KeySizes (0, 1024, 8);
		}
コード例 #7
0
ファイル: TripleDES.cs プロジェクト: kabili207/SharpPrivacy
        public TripleDES()
        {
            // from SymmetricAlgorithm
            KeySizeValue = 192;
            BlockSizeValue = 64;
            FeedbackSizeValue = 64;

            LegalKeySizesValue = new KeySizes [1];
            LegalKeySizesValue [0] = new KeySizes (128, 192, 64);

            LegalBlockSizesValue = new KeySizes [1];
            LegalBlockSizesValue [0] = new KeySizes (64, 64, 0);
        }
コード例 #8
0
ファイル: KeyGeneration.cs プロジェクト: AustinWise/corefx
        private static int GetMin(KeySizes[] keySizes)
        {
            int min = int.MaxValue;

            foreach (var keySize in keySizes)
            {
                if (keySize.MinSize < min)
                {
                    min = keySize.MinSize;
                }
            }

            return min;
        }
コード例 #9
0
 public static bool IsKeySizeValid(this KeySizes keySizes, int keySize)
 {
     if (keySizes.SkipSize == 0)
     {
         return(keySizes.MinSize == keySize || keySizes.MaxSize == keySize);
     }
     for (int minSize = keySizes.MinSize; minSize <= keySizes.MaxSize; minSize += keySizes.SkipSize)
     {
         if (keySize == minSize)
         {
             return(true);
         }
     }
     return(false);
 }
コード例 #10
0
        public static void LegalBlockSizes()
        {
            using (Aes aes = AesFactory.Create())
            {
                KeySizes[] blockSizes = aes.LegalBlockSizes;

                Assert.NotNull(blockSizes);
                Assert.Equal(1, blockSizes.Length);

                KeySizes blockSizeLimits = blockSizes[0];

                Assert.Equal(128, blockSizeLimits.MinSize);
                Assert.Equal(128, blockSizeLimits.MaxSize);
                Assert.Equal(0, blockSizeLimits.SkipSize);
            }
        }
コード例 #11
0
ファイル: RC4.cs プロジェクト: DoubleDJspb/RC4-Net
 /// <summary>
 /// Initializes a new instance of the RC4 class.
 /// </summary>
 internal RC4()
 {
     // Recommended length for use in the US is 128 bits.
     KeySizeValue = 128;
     // Usually, it is 256 bits. However, to increase safety, it is necessary to increase this value.
     BlockSizeValue = 256;
     // Response size cannot be larger than block size.
     FeedbackSizeValue = BlockSizeValue;
     // The algorithm has no max limit on the size of the S-box. Limitations only in performance and x86/x64 system.
     LegalBlockSizesValue = new KeySizes[] { new KeySizes(256, 256, 0) };
     // The key length can be 8-2048 bits (1 - 256 bytes).
     LegalKeySizesValue = new KeySizes[] { new KeySizes(8, 2048, 0) };
     // The CBC mode may also be considered a stream cipher with n-bit blocks playing the role of very large characters.
     ModeValue    = CipherMode.CBC;
     PaddingValue = PaddingMode.None;
 }
コード例 #12
0
        public static void LegalKeySizes()
        {
            using (Aes aes = AesFactory.Create())
            {
                KeySizes[] keySizes = aes.LegalKeySizes;

                Assert.NotNull(keySizes);
                Assert.Equal(1, keySizes.Length);

                KeySizes keySizeLimits = keySizes[0];

                Assert.Equal(128, keySizeLimits.MinSize);
                Assert.Equal(256, keySizeLimits.MaxSize);
                Assert.Equal(64, keySizeLimits.SkipSize);
            }
        }
コード例 #13
0
        /// <summary>
        /// Try get legal nonce sizes.
        /// </summary>
        /// <param name="mode">Symmetric algorithm cipher mode.</param>
        /// <param name="padding">Symmetric algorithm padding mode.</param>
        /// <param name="nonceSizes">Legal nonce size bits.</param>
        /// <returns></returns>
        public bool TryGetNonceSizes(SymmetricCipherMode mode, SymmetricPaddingMode padding, out KeySizes[] nonceSizes)
        {
            switch (padding)
            {
            case SymmetricPaddingMode.NoPadding: break;

            default: nonceSizes = null; return(false);
            }
            switch (mode)
            {
            case SymmetricCipherMode.CCM:
                if (this.BlockSize == 128)
                {
                    nonceSizes = new KeySizes[] { new KeySizes(56, 104, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.EAX:
                if (this.BlockSize == 64 || this.BlockSize == 128)
                {
                    nonceSizes = new KeySizes[] { new KeySizes(8, 2147483640, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.GCM:
                if (this.BlockSize == 128)
                {
                    nonceSizes = new KeySizes[] { new KeySizes(8, 2147483640, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.OCB:
                if (this.BlockSize == 128)
                {
                    nonceSizes = new KeySizes[] { new KeySizes(0, 120, 8) };
                    return(true);
                }
                break;

            default: break;
            }
            nonceSizes = null;
            return(false);
        }
コード例 #14
0
        /// <summary>
        /// Try get legal mac sizes.
        /// </summary>
        /// <param name="mode">Symmetric algorithm cipher mode.</param>
        /// <param name="padding">Symmetric algorithm padding mode.</param>
        /// <param name="macSizes">Legal mac size bits.</param>
        /// <returns></returns>
        public bool TryGetMacSizes(SymmetricCipherMode mode, SymmetricPaddingMode padding, out KeySizes[] macSizes)
        {
            switch (padding)
            {
            case SymmetricPaddingMode.NoPadding: break;

            default: macSizes = null; return(false);
            }
            switch (mode)
            {
            case SymmetricCipherMode.CCM:
                if (this.BlockSize == 128)
                {
                    macSizes = new KeySizes[] { new KeySizes(32, 128, 16) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.EAX:
                if (this.BlockSize == 64 || this.BlockSize == 128)
                {
                    macSizes = new KeySizes[] { new KeySizes(8, this.BlockSize, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.GCM:
                if (this.BlockSize == 128)
                {
                    macSizes = new KeySizes[] { new KeySizes(32, 128, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.OCB:
                if (this.BlockSize == 128)
                {
                    macSizes = new KeySizes[] { new KeySizes(64, 128, 8) };
                    return(true);
                }
                break;

            default: break;
            }
            macSizes = null;
            return(false);
        }
コード例 #15
0
        /// <summary>Default constructor. Starts as an uninitialized ECB instance.</summary>
        public BlowfishAlgorithm() : base()
        {
            KeySizeValue = BlowfishECB.MAX_KEY_LENGTH << 3;

            LegalBlockSizesValue    = new KeySizes[1];
            LegalBlockSizesValue[0] = new KeySizes(
                BlockSize,
                BlockSize,
                BlowfishECB.BLOCK_SIZE);

            LegalKeySizesValue    = new KeySizes[1];
            LegalKeySizesValue[0] = new KeySizes(
                0,
                BlowfishECB.MAX_KEY_LENGTH << 3,
                    8);

            ModeValue = CipherMode.ECB;
        }
コード例 #16
0
        public static void LegalKeySizes()
        {
            using (Aes aes = AesFactory.Create())
            {
                KeySizes[] keySizes = aes.LegalKeySizes;

                Assert.NotNull(keySizes);
                Assert.Equal(1, keySizes.Length);

                KeySizes keySizeLimits = keySizes[0];

                Assert.Equal(128, keySizeLimits.MinSize);
                Assert.Equal(256, keySizeLimits.MaxSize);

                // Browser's SubtleCrypto doesn't support AES-192
                int expectedKeySkipSize = PlatformDetection.IsBrowser ? 128 : 64;
                Assert.Equal(expectedKeySkipSize, keySizeLimits.SkipSize);
            }
        }
    public void LegalKeySizes(AsymmetricAlgorithm name, int minSize, int maxSize, int stepSize)
    {
        IAsymmetricKeyAlgorithmProvider provider = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(name);
        IReadOnlyList <KeySizes>?       result   = provider.LegalKeySizes;

        Assert.NotNull(result);
        Assert.NotEmpty(result);

        Action <int> attemptKeySize = size =>
        {
            provider.CreateKeyPair(size).Dispose();
        };

        KeySizes range = result.Single();

        Assert.Equal(minSize, range.MinSize);
        Assert.Equal(maxSize, range.MaxSize);
        Assert.Equal(stepSize, range.StepSize);
    }
コード例 #18
0
        // Initialise bi from XML element if element_name matches
        // Used by FromXmlString
        private bool CheckXmlElement(XmlReader reader, string element_name, string element_name_required, ref BigInteger bi_out)
        {
            if (element_name != element_name_required)
            {
                return(false);
            }

            string s = reader.ReadString();

            byte[] b = Convert.FromBase64String(s);
            if (element_name == "Modulus")
            {
                KeySizeValue          = b.Length * 8;
                LegalKeySizesValue[0] = new KeySizes(KeySizeValue, KeySizeValue, 0);
            }
            BigInteger bi = new BigInteger(b);

            bi_out = bi;
            return(true);
        }
コード例 #19
0
        public PaillierManaged()
        {
            // create the key struct
            o_key_struct = new PaillierKeyStruct();

            // set all of the big integers to zero
            o_key_struct.N      = new BigInteger(0);
            o_key_struct.G      = new BigInteger(0);
            o_key_struct.Lambda = new BigInteger(0);
            o_key_struct.Miu    = new BigInteger(0);

            // set the default key size value
            KeySizeValue = 1024;

            // set the default padding mode
            Padding = PaillierPaddingMode.LeadingZeros;

            // set the range of legal keys
            LegalKeySizesValue = new KeySizes[] { new KeySizes(384, 1088, 8) };
        }
コード例 #20
0
        /// <summary>
        /// constructor
        /// </summary>
        public BlowfishAlgorithm() : base()
        {
            m_bf  = null;
            m_bfc = null;

            // FIXME: are we supposed to create a default key and IV?
            IVValue      = null;
            KeyValue     = null;
            KeySizeValue = Blowfish.MAXKEYLENGTH * 8;

            LegalBlockSizesValue    = new KeySizes[1];
            LegalBlockSizesValue[0] = new KeySizes(BlockSize, BlockSize, 8);

            LegalKeySizesValue    = new KeySizes[1];
            LegalKeySizesValue[0] = new KeySizes(0, Blowfish.MAXKEYLENGTH * 8, 8);

            ModeValue = CipherMode.ECB;

            m_rng = null;
        }
コード例 #21
0
ファイル: BlowfishAlgorithm.cs プロジェクト: jonishzx/Clover
        public BlowfishAlgorithm() : base()
        {
            m_bf  = null;
            m_bfc = null;


            IVValue      = null;
            KeyValue     = null;
            KeySizeValue = Blowfish.MAXKEYLENGTH * 8;

            LegalBlockSizesValue    = new KeySizes[1];
            LegalBlockSizesValue[0] = new KeySizes(BlockSize, BlockSize, 8);

            LegalKeySizesValue    = new KeySizes[1];
            LegalKeySizesValue[0] = new KeySizes(0, Blowfish.MAXKEYLENGTH * 8, 8);

            ModeValue = CipherMode.ECB;

            m_rng = null;
        }
コード例 #22
0
        private void UpdateLegalTagSizes(SafeBCryptAlgorithmHandle algorithm)
        {
            Debug.Assert(algorithm != null, "algorithm != null");
            Debug.Assert(!algorithm.IsClosed && !algorithm.IsInvalid, "!algorithm.IsClosed && !algorithm.IsInvalid");

            // Get the authentication tag length structure.
            BCryptNative.BCRYPT_KEY_LENGTHS_STRUCT tagLengths =
                BCryptNative.GetValueTypeProperty <SafeBCryptAlgorithmHandle, BCryptNative.BCRYPT_KEY_LENGTHS_STRUCT>(
                    algorithm,
                    BCryptNative.ObjectPropertyName.AuthTagLength);

            // BCrypt returns the tag sizes in bytes, convert them to bits for the LegalTagSizes property
            LegalTagSizesValue = new KeySizes[]
            {
                new KeySizes(tagLengths.dwMinLength * 8, tagLengths.dwMaxLength * 8, tagLengths.dwIncrement * 8)
            };

            // By default, generate the maximum authentication tag length possible for this algorithm
            TagSize = tagLengths.dwMaxLength * 8;
        }
コード例 #23
0
        public ElGamalManaged()
        {
            // create the key struct
            o_key_struct = new ElGamalKeyStruct();

            // set all of the big integers to zero
            o_key_struct.P = new BigInteger(0);
            o_key_struct.G = new BigInteger(0);
            o_key_struct.Y = new BigInteger(0);
            o_key_struct.X = new BigInteger(0);

            // set the default key size value
            KeySizeValue = 1024;

            // set the default padding mode
            Padding = ElGamalPaddingMode.Zeros;

            // set the range of legal keys
            LegalKeySizesValue = new KeySizes[] { new KeySizes(384, 1088, 8) };
        }
コード例 #24
0
        //
        // Summary:
        //  Constructs an InfoCardSymmetricAlgorithm
        //
        // Parameters:
        //  cryptoHandle  - A handle to the symmetric key to base the symmetric algorithm on.
        //
        public InfoCardSymmetricAlgorithm(SymmetricCryptoHandle cryptoHandle)
            : base()
        {
            m_cryptoHandle = (SymmetricCryptoHandle)cryptoHandle.Duplicate();

            try
            {
                m_parameters = (RpcSymmetricCryptoParameters)m_cryptoHandle.Parameters;

                KeySizeValue         = m_parameters.keySize;
                BlockSizeValue       = m_parameters.blockSize;
                FeedbackSizeValue    = m_parameters.feedbackSize;
                LegalBlockSizesValue = new KeySizes[] { new KeySizes(BlockSizeValue, BlockSizeValue, 0) };
                LegalKeySizesValue   = new KeySizes[] { new KeySizes(KeySizeValue, KeySizeValue, 0) };
            }
            catch
            {
                m_cryptoHandle.Dispose();
                throw;
            }
        }
コード例 #25
0
 private static bool IsLegalSize(int size, KeySizes[] legalSizes)
 {
     for (int i = 0; i < legalSizes.Length; i++)
     {
         // If a cipher has only one valid key size, MinSize == MaxSize and SkipSize will be 0
         if (legalSizes[i].SkipSize == 0)
         {
             if (legalSizes[i].MinSize == size)
                 return true;
         }
         else
         {
             for (int j = legalSizes[i].MinSize; j <= legalSizes[i].MaxSize; j += legalSizes[i].SkipSize)
             {
                 if (j == size)
                     return true;
             }
         }
     }
     return false;
 }
コード例 #26
0
        // Display specified KeySize properties to the console.
        private static void ShowKeys(KeySizes[] keySizes, string objectName)
        {
            // Retrieve the first KeySizes in the array.
            KeySizes firstKeySize = keySizes[0];

            // Retrieve the minimum key size in bits.
            int minKeySize = firstKeySize.MinSize;

            // Retrieve the maximum key size in bits.
            int maxKeySize = firstKeySize.MaxSize;

            // Retrieve the interval between valid key size in bits.
            int skipKeySize = firstKeySize.SkipSize;

            Console.Write("\n KeySizes retrieved from the ");
            Console.WriteLine(objectName + " object.");
            Console.WriteLine("Minimum key size bits: " + minKeySize);
            Console.WriteLine("Maximum key size bits: " + maxKeySize);
            Console.WriteLine("Interval between key size bits: " +
                              skipKeySize);
        }
コード例 #27
0
        //
        // Summary:
        //  Given a pointer to a CryptoHandle create a new instance of this class.
        //
        public InfoCardRSACryptoProvider(AsymmetricCryptoHandle cryptoHandle)
            : base()
        {
            m_cryptoHandle = (AsymmetricCryptoHandle)cryptoHandle.Duplicate();

            try
            {
                m_params = (RpcAsymmetricCryptoParameters)m_cryptoHandle.Parameters;

                int keySize = m_params.keySize;

                LegalKeySizesValue    = new KeySizes[1];
                KeySizeValue          = keySize;
                LegalKeySizesValue[0] = new KeySizes(keySize, keySize, 0);
            }
            catch
            {
                m_cryptoHandle.Dispose();
                m_cryptoHandle = null;
                throw;
            }
        }
コード例 #28
0
            public Trivial()
            {
                // Desktop's SymmetricAlgorithm reads from the field overly aggressively,
                // but in Core it always reads from the property. By setting the field
                // we're still happy on Desktop tests, and we can validate the default
                // behavior of the LegalBlockSizes property.
                LegalBlockSizesValue = new KeySizes[]
                {
                    new KeySizes(5 * 8, -99 * 8, 0 * 8),
                    new KeySizes(13 * 8, 22 * 8, 6 * 8),
                    new KeySizes(101 * 8, 104 * 8, 1 * 8),
                };

                // Desktop's SymmetricAlgorithm reads from the property correctly, but
                // we'll set the field here, anyways, to validate the default behavior
                // of the LegalKeySizes property.
                LegalKeySizesValue = new KeySizes[]
                {
                    new KeySizes(5 * 8, -99 * 8, 0 * 8),
                    new KeySizes(13 * 8, 22 * 8, 6 * 8),
                    new KeySizes(101 * 8, 104 * 8, 1 * 8),
                };
            }
コード例 #29
0
ファイル: SecurityHelper.cs プロジェクト: hvppy/X
        private static Byte[] Pad(Byte[] buf, KeySizes keySize)
        {
            var psize = buf.Length * 8;
            var size  = 0;

            for (var i = keySize.MinSize; i <= keySize.MaxSize; i += keySize.SkipSize)
            {
                if (i >= psize)
                {
                    size = i / 8;
                    break;
                }

                // DES的SkipSize为0
                if (keySize.SkipSize == 0)
                {
                    break;
                }
            }

            // 所有key大小都不合适,取最大值,此时密码过长,需要截断
            if (size == 0)
            {
                size = keySize.MaxSize / 8;
            }

            if (buf.Length == size)
            {
                return(buf);
            }

            var buf2 = new Byte[size];

            buf2.Write(0, buf);

            return(buf2);
        }
コード例 #30
0
 /// <summary>
 /// Initializes a new instance of the RC4CryptoServiceProvider class.
 /// </summary>
 public RC4CryptoServiceProvider()
 {
     // Change the limits for the current implementation (5 - 256 bytes).
     LegalKeySizesValue = new KeySizes[] { new KeySizes(40, 2048, 0) };
 }
コード例 #31
0
		private bool CompareKeySizes (KeySizes k1, KeySizes k2)
		{ 
			bool result = k1.MaxSize == k2.MaxSize;
			result = result && k1.MinSize == k2.MinSize;
			result = result && k1.SkipSize == k2.SkipSize;
			return result;
		}
コード例 #32
0
		private void Common (int dwKeySize, CspParameters p) 
		{
			// Microsoft RSA CSP can do between 384 and 16384 bits keypair
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (384, 16384, 8);
			base.KeySize = dwKeySize;

			rsa = new RSAManaged (KeySize);
			rsa.KeyGenerated += new RSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);

			persistKey = (p != null);
			if (p == null) {
				p = new CspParameters (PROV_RSA_FULL);
#if NET_1_1
				if (useMachineKeyStore)
					p.Flags |= CspProviderFlags.UseMachineKeyStore;
#endif
				store = new KeyPairPersistence (p);
				// no need to load - it cannot exists
			}
			else {
				store = new KeyPairPersistence (p);
				store.Load ();
				if (store.KeyValue != null) {
					persisted = true;
					this.FromXmlString (store.KeyValue);
				}
			}
		}
コード例 #33
0
 public RijndaelLegalSizesBreaker()
 {
     LegalKeySizesValue[0]   = new KeySizes(1, 1, 0);
     LegalBlockSizesValue[0] = new KeySizes(1, 1, 0);
 }
コード例 #34
0
        /// <summary>
        /// Try get legal sizes.
        /// </summary>
        /// <param name="mode">Symmetric algorithm cipher mode.</param>
        /// <param name="padding">Symmetric algorithm padding mode.</param>
        /// <param name="ivSizes">Legal iv size bits.</param>
        /// <returns></returns>
        public bool TryGetIVSizes(SymmetricCipherMode mode, SymmetricPaddingMode padding, out KeySizes[] ivSizes)
        {
            bool pad;

            switch (padding)
            {
            case SymmetricPaddingMode.NoPadding: pad = false; break;

            case SymmetricPaddingMode.PKCS7:
            case SymmetricPaddingMode.Zeros:
            case SymmetricPaddingMode.X923:
            case SymmetricPaddingMode.ISO10126:
            case SymmetricPaddingMode.ISO7816_4:
            case SymmetricPaddingMode.TBC: pad = true; break;

            default: ivSizes = null; return(false);
            }
            switch (mode)
            {
            case SymmetricCipherMode.CBC: ivSizes = new KeySizes[] { new KeySizes(this.BlockSize, this.BlockSize, 0) }; return(true);

            case SymmetricCipherMode.ECB: ivSizes = new KeySizes[] { new KeySizes(0, 0, 0) }; return(true);

            case SymmetricCipherMode.OFB: ivSizes = new KeySizes[] { new KeySizes(8, this.BlockSize, 8) }; return(true);

            case SymmetricCipherMode.CFB: ivSizes = new KeySizes[] { new KeySizes(8, this.BlockSize, 8) }; return(true);

            case SymmetricCipherMode.CTS:
                if (!pad)
                {
                    ivSizes = new KeySizes[] { new KeySizes(this.BlockSize, this.BlockSize, 0) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.CTR:
            {
                int min = Math.Max(this.BlockSize / 2, this.BlockSize - 64);
                ivSizes = new KeySizes[] { new KeySizes(min, this.BlockSize, 8) };
                return(true);
            }

            case SymmetricCipherMode.CTS_ECB:
                if (!pad)
                {
                    ivSizes = new KeySizes[] { new KeySizes(0, 0, 0) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.GOFB:
                if (this.BlockSize == 64)
                {
                    ivSizes = new KeySizes[] { new KeySizes(this.BlockSize, this.BlockSize, 0) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.OpenPGPCFB:
                ivSizes = new KeySizes[] { new KeySizes(8, this.BlockSize, 8) };
                return(true);

            case SymmetricCipherMode.SIC:
                if (this.BlockSize >= 128)
                {
                    int min = Math.Max(this.BlockSize / 2, this.BlockSize - 64);
                    ivSizes = new KeySizes[] { new KeySizes(min, this.BlockSize, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.CCM:
                if (!pad && this.BlockSize == 128)
                {
                    ivSizes = new KeySizes[] { new KeySizes(56, 104, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.EAX:
                if (!pad && (this.BlockSize == 64 || this.BlockSize == 128))
                {
                    ivSizes = new KeySizes[] { new KeySizes(8, 2147483640, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.GCM:
                if (!pad && this.BlockSize == 128)
                {
                    ivSizes = new KeySizes[] { new KeySizes(8, 2147483640, 8) };
                    return(true);
                }
                break;

            case SymmetricCipherMode.OCB:
                if (!pad && this.BlockSize == 128)
                {
                    ivSizes = new KeySizes[] { new KeySizes(0, 120, 8) };
                    return(true);
                }
                break;

            default: break;
            }
            ivSizes = null;
            return(false);
        }
コード例 #35
0
 public static bool IsLegalSize(this int size, KeySizes legalSizes)
 {
     return(size.IsLegalSize(legalSizes, out _));
 }
コード例 #36
0
		void Common (int dwKeySize, bool parameters) 
		{
			// Microsoft RSA CSP can do between 384 and 16384 bits keypair
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (384, 16384, 8);
			base.KeySize = dwKeySize;

			rsa = new RSAManaged (KeySize);
			rsa.KeyGenerated += new RSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);

			persistKey = parameters;
			if (parameters)
				return;

			// no need to load - it cannot exists
			var p = new CspParameters (PROV_RSA_FULL);
			if (useMachineKeyStore)
				p.Flags |= CspProviderFlags.UseMachineKeyStore;
			store = new KeyPairPersistence (p);
		}
コード例 #37
0
ファイル: KeyGeneration.cs プロジェクト: AustinWise/corefx
        private static int GetMax(KeySizes[] keySizes)
        {
            int max = 0;

            foreach (var keySize in keySizes)
            {
                if (keySize.MaxSize > max)
                {
                    max = keySize.MaxSize;
                }
            }

            return max;
        }
コード例 #38
0
ファイル: RC2Tests.cs プロジェクト: dotnet/corefx
 public RC2LegalSizesBreaker()
 {
     LegalKeySizesValue[0] = new KeySizes(1, 1, 0);
     LegalBlockSizesValue[0] = new KeySizes(1, 1, 0);
 }
コード例 #39
0
		private void Common (int dwKeySize, CspParameters p) 
		{
			// Microsoft RSA CSP can do between 384 and 16384 bits keypair
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (384, 16384, 8);
			base.KeySize = dwKeySize;

			rsa = new RSAManaged (KeySize);
			rsa.KeyGenerated += new RSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);

			persistKey = (p != null);
			if (p == null) {
				p = new CspParameters (PROV_RSA_FULL);
				if (useMachineKeyStore)
					p.Flags |= CspProviderFlags.UseMachineKeyStore;
				store = new KeyPairPersistence (p);
				// no need to load - it cannot exists
			}
			else {
				store = new KeyPairPersistence (p);
				bool exists = store.Load ();
				bool required = (p.Flags & CspProviderFlags.UseExistingKey) != 0;

				if (required && !exists)
					throw new CryptographicException ("Keyset does not exist");

				if (store.KeyValue != null) {
					persisted = true;
					this.FromXmlString (store.KeyValue);
				}
			}
		}
コード例 #40
0
ファイル: DSAManaged.cs プロジェクト: jack-pappas/mono
		public DSAManaged (int dwKeySize)
		{
			KeySizeValue = dwKeySize;
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);
		}
コード例 #41
0
 public bool ValidKeySize(int bitLength)
 {
     return(KeySizes.IsLegalKeySize(LegalKeySizesValue, bitLength));
 }
コード例 #42
0
ファイル: RSAManaged.cs プロジェクト: Jakosa/MonoLibraries
		public RSAManaged (int keySize) 
		{
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (384, 16384, 8);
			base.KeySize = keySize;
		}
コード例 #43
0
	// Check that a size value is in a size list.
	private void CheckSize(String msg, KeySizes[] sizes, int value)
			{
				foreach(KeySizes size in sizes)
				{
					if(value >= size.MinSize && value <= size.MaxSize &&
					   ((value - size.MinSize) % size.SkipSize) == 0)
					{
						return;
					}
				}
				Fail(msg);
			}
コード例 #44
0
 public CryptoRSA(KeySizes KeySize)
 {
     _RSACSP    = new RSACryptoServiceProvider((int)KeySize);
     _Converter = new UTF8Encoding();
 }
コード例 #45
0
ファイル: Trivial.cs プロジェクト: ChuangYang/corefx
 public Trivial()
 {
     LegalKeySizesValue = new KeySizes[]
         {
             new KeySizes(5*8, -99*8, 0*8),
             new KeySizes(13*8, 22*8, 6*8),
             new KeySizes(101*8, 104*8, 1*8),
             new KeySizes(101*8 + 1, 101*8 + 2, 1),
         };
 }
コード例 #46
0
ファイル: DSACryptoServiceProvider.cs プロジェクト: mono/mono
		void Common (int dwKeySize, bool parameters) 
		{
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (512, 1024, 64);

			// will throw an exception is key size isn't supported
			KeySize = dwKeySize;
			dsa = new DSAManaged (dwKeySize);
			dsa.KeyGenerated += new DSAManaged.KeyGeneratedEventHandler (OnKeyGenerated);

			persistKey = parameters;
			if (parameters)
				return;

			var p = new CspParameters (PROV_DSS_DH);
			if (useMachineKeyStore)
				p.Flags |= CspProviderFlags.UseMachineKeyStore;
			store = new KeyPairPersistence (p);
			// no need to load - it cannot exists
		}
コード例 #47
0
ファイル: FormSpeedTest.cs プロジェクト: modulexcite/CEX
 private void OnKeySizeChanged(object sender, EventArgs e)
 {
     KeySizes ksize = KeySizes.K256;
     Enum.TryParse<KeySizes>(((ComboBox)sender).Text, out ksize);
     _keySize = ksize;
 }
コード例 #48
0
ファイル: Trivial.cs プロジェクト: AtsushiKan/corefx
            public Trivial()
            {
                // Desktop's SymmetricAlgorithm reads from the field overly aggressively,
                // but in Core it always reads from the property. By setting the field
                // we're still happy on Desktop tests, and we can validate the default
                // behavior of the LegalBlockSizes property.
                LegalBlockSizesValue = new KeySizes[]
                    {
                        new KeySizes(5*8, -99*8, 0*8),
                        new KeySizes(13*8, 22*8, 6*8),
                        new KeySizes(101*8, 104*8, 1*8),
                    };

                // Desktop's SymmetricAlgorithm reads from the property correctly, but
                // we'll set the field here, anyways, to validate the default behavior
                // of the LegalKeySizes property.
                LegalKeySizesValue = new KeySizes[]
                    {
                        new KeySizes(5*8, -99*8, 0*8),
                        new KeySizes(13*8, 22*8, 6*8),
                        new KeySizes(101*8, 104*8, 1*8),
                    };
            }
コード例 #49
0
 public TripleDESLegalSizesBreaker()
 {
     LegalKeySizesValue[0]   = new KeySizes(1, 1, 0);
     LegalBlockSizesValue[0] = new KeySizes(1, 1, 0);
 }
コード例 #50
0
 public RSAManaged(int keySize)
 {
     LegalKeySizesValue     = new KeySizes [1];
     LegalKeySizesValue [0] = new KeySizes(384, 16384, 8);
     base.KeySize           = keySize;
 }
コード例 #51
0
		public RSAManaged (int keySize) 
		{
			KeySizeValue = keySize;
			LegalKeySizesValue = new KeySizes [1];
			LegalKeySizesValue [0] = new KeySizes (192, 16384, 8);//duff 384 before now 192
		}
コード例 #52
0
ファイル: Transform.cs プロジェクト: todoasap/CEX-NET
        private int GetKeySize(string KeyPath)
        {
            KeySizes keySize = KeyHeader.GetKeySize(KeyPath);

            if (this.Engine == Engines.DCS)
            {
                return(96);
            }

            if (keySize == KeySizes.K128)
            {
                return(16);
            }
            else if (keySize == KeySizes.K192)
            {
                return(24);
            }
            else if (keySize == KeySizes.K192)
            {
                return(24);
            }
            else if (keySize == KeySizes.K256)
            {
                return(32);
            }
            else if (keySize == KeySizes.K384)
            {
                return(48);
            }
            else if (keySize == KeySizes.K448)
            {
                return(56);
            }
            else if (keySize == KeySizes.K512)
            {
                return(64);
            }
            else if (keySize == KeySizes.K1024)
            {
                return(128);
            }
            else if (keySize == KeySizes.K1536)
            {
                return(192);
            }
            else if (keySize == KeySizes.K2560)
            {
                return(320);
            }
            else if (keySize == KeySizes.K3584)
            {
                return(448);
            }
            else if (keySize == KeySizes.K4608)
            {
                return(576);
            }
            else
            {
                return(32);
            }
        }
コード例 #53
0
ファイル: TripleDesTests.cs プロジェクト: dotnet/corefx
 public TripleDESLegalSizesBreaker()
 {
     LegalKeySizesValue[0] = new KeySizes(1, 1, 0);
     LegalBlockSizesValue[0] = new KeySizes(1, 1, 0);
 }
コード例 #54
0
 public HostManaged()
 {
     // define the valid block and key sizes
     LegalKeySizesValue   = new KeySizes[] { new KeySizes(256, 256, 0) };
     LegalBlockSizesValue = new KeySizes[] { new KeySizes(64, 64, 0) };
 }