Ejemplo n.º 1
0
 public int Generate(byte[] output, byte[] additionalInput, bool predictionResistant)
 {
     if (mIsTdea)
     {
         if (mReseedCounter > TDEA_RESEED_MAX)
         {
             return(-1);
         }
         if (DrbgUtilities.IsTooLarge(output, TDEA_MAX_BITS_REQUEST / 8))
         {
             throw new ArgumentException("Number of bits per request limited to " + TDEA_MAX_BITS_REQUEST, "output");
         }
     }
     else
     {
         if (mReseedCounter > AES_RESEED_MAX)
         {
             return(-1);
         }
         if (DrbgUtilities.IsTooLarge(output, AES_MAX_BITS_REQUEST / 8))
         {
             throw new ArgumentException("Number of bits per request limited to " + AES_MAX_BITS_REQUEST, "output");
         }
     }
     if (predictionResistant)
     {
         CTR_DRBG_Reseed_algorithm(additionalInput);
         additionalInput = null;
     }
     if (additionalInput != null)
     {
         additionalInput = Block_Cipher_df(additionalInput, mSeedLength);
         CTR_DRBG_Update(additionalInput, mKey, mV);
     }
     else
     {
         additionalInput = new byte[mSeedLength];
     }
     byte[] array = new byte[mV.Length];
     mEngine.Init(forEncryption: true, new KeyParameter(ExpandKey(mKey)));
     for (int i = 0; i <= output.Length / array.Length; i++)
     {
         int num = (output.Length - i * array.Length > array.Length) ? array.Length : (output.Length - i * mV.Length);
         if (num != 0)
         {
             AddOneTo(mV);
             mEngine.ProcessBlock(mV, 0, array, 0);
             Array.Copy(array, 0, output, i * array.Length, num);
         }
     }
     CTR_DRBG_Update(additionalInput, mKey, mV);
     mReseedCounter++;
     return(output.Length * 8);
 }