Beispiel #1
0
 // Constructors.
 public HMACSHA256()
 {
     HashName      = "SHA256";
     HashSizeValue = 256;
     byte[] key = new byte [64];
     CryptoMethods.GenerateRandom(key, 0, 64);
 }
Beispiel #2
0
 // Constructors.
 public HMACRIPEMD160()
 {
     HashName      = "RIPEMD160";
     HashSizeValue = 160;
     byte[] key = new byte [64];
     CryptoMethods.GenerateRandom(key, 0, 64);
 }
Beispiel #3
0
 // Constructors.
 public HMACMD5()
 {
     HashName      = "MD5";
     HashSizeValue = 128;
     byte[] key = new byte [64];
     CryptoMethods.GenerateRandom(key, 0, 64);
 }
Beispiel #4
0
 // Constructors.
 public MACTripleDES()
 {
     HashSizeValue = 64;
     KeyValue      = new byte [24];
     CryptoMethods.GenerateRandom(KeyValue, 0, 24);
     SetupAlgorithm(CryptoConfig.TripleDESDefault);
 }
Beispiel #5
0
 // Constructors.
 public HMACSHA1()
 {
     HashSizeValue = 160;
     KeyValue      = new byte [64];
     CryptoMethods.GenerateRandom(KeyValue, 0, 64);
     alg     = null;
     algName = CryptoConfig.SHA1Default;
 }
Beispiel #6
0
 // Generate a random key value.
 public override void GenerateKey()
 {
     byte[] key = new byte [KeySizeValue / 8];
     CryptoMethods.GenerateRandom(key, 0, KeySizeValue / 8);
     if (KeyValue != null)
     {
         // Clear the previous key value.
         Array.Clear(KeyValue, 0, KeyValue.Length);
     }
     KeyValue = key;
 }
Beispiel #7
0
 // Generate a random salt of a particular size.
 private static byte[] GenerateSalt(int saltSize)
 {
     if (saltSize < 0)
     {
         throw new ArgumentOutOfRangeException
                   ("saltSize", _("ArgRange_NonNegative"));
     }
     byte[] salt = new byte [saltSize];
     CryptoMethods.GenerateRandom(salt, 0, saltSize);
     return(salt);
 }
 // Generate a random key value.
 public override void GenerateKey()
 {
     byte[] key = new byte [8];
     do
     {
         CryptoMethods.GenerateRandom(key, 0, 8);
     }while(CryptoMethods.IsSemiWeakKey(key, 0) ||
            CryptoMethods.IsWeakKey(key, 0));
     if (KeyValue != null)
     {
         // Clear the previous key value.
         Array.Clear(KeyValue, 0, KeyValue.Length);
     }
     KeyValue = key;
 }
        // Get non-zero random data.
        public override void GetNonZeroBytes(byte[] data)
        {
            int index;

            // Get the initial random data.
            CryptoMethods.GenerateRandom(data, 0, data.Length);

            // Replace zero bytes with new random data.
            for (index = 0; index < data.Length; ++index)
            {
                while (data[index] == 0)
                {
                    CryptoMethods.GenerateRandom(data, index, 1);
                }
            }
        }
Beispiel #10
0
 // Generate a random initialization vector.
 public override void GenerateIV()
 {
     byte[] iv = new byte [16];
     CryptoMethods.GenerateRandom(iv, 0, 16);
     IVValue = iv;
 }
 // Get random data.
 public override void GetBytes(byte[] data)
 {
     CryptoMethods.GenerateRandom(data, 0, data.Length);
 }
Beispiel #12
0
        // Create a DSA signature for the specified data.
        public override byte[] CreateSignature(byte[] rgbHash)
        {
            // Validate the parameter.
            if (rgbHash == null)
            {
                throw new ArgumentNullException("rgbHash");
            }

            // Check that we have sufficient DSA parameters to sign.
            if (dsaParams.G == null)
            {
                throw new CryptographicException
                          (_("Crypto_DSAParamsNotSet"));
            }
            else if (dsaParams.X == null)
            {
                throw new CryptographicException
                          (_("Crypto_CannotSignWithPublic"));
            }

            // Generate a random K less than Q to use in
            // signature generation.  We guarantee less than
            // by setting the high byte of K to at least one
            // less than the high byte of Q.
            int len = dsaParams.Q.Length;

            byte[] K = new byte [len];
            CryptoMethods.GenerateRandom(K, 1, K.Length - 1);
            int index = 0;

            while (index < len && K[index] >= dsaParams.Q[index])
            {
                if (dsaParams.Q[index] == 0)
                {
                    K[index] = (byte)0;
                    ++index;
                }
                else
                {
                    K[index] = (byte)(dsaParams.Q[index] - 1);
                    break;
                }
            }

            // Compute R = ((G^K mod P) mod Q)
            byte[] temp1 = CryptoMethods.NumPow
                               (dsaParams.G, K, dsaParams.P);
            byte[] R = CryptoMethods.NumMod(temp1, dsaParams.Q);
            Array.Clear(temp1, 0, temp1.Length);

            // Compute S = ((K^-1 * (hash + X * R)) mod Q)
            temp1 = CryptoMethods.NumInv(K, dsaParams.Q);
            byte[] temp2 = CryptoMethods.NumMul
                               (dsaParams.X, R, dsaParams.Q);
            byte[] temp3 = CryptoMethods.NumAdd
                               (rgbHash, temp2, dsaParams.Q);
            byte[] S = CryptoMethods.NumMul(temp1, temp3, dsaParams.Q);
            Array.Clear(temp1, 0, temp1.Length);
            Array.Clear(temp2, 0, temp2.Length);
            Array.Clear(temp3, 0, temp3.Length);
            Array.Clear(K, 0, K.Length);

            // Pack R and S into a signature blob and return it.
            ASN1Builder builder = new ASN1Builder();

            builder.AddBigInt(R);
            builder.AddBigInt(S);
            byte[] sig = builder.ToByteArray();
            Array.Clear(R, 0, R.Length);
            Array.Clear(S, 0, S.Length);
            return(sig);
        }