Ejemplo n.º 1
0
        /**
         * Construct a SP800-90A Hash DRBG.
         * <p>
         * Minimum entropy requirement is the security strength requested.
         * </p>
         * @param digest  source digest to use for DRB stream.
         * @param securityStrength security strength required (in bits)
         * @param entropySource source of entropy to use for seeding/reseeding.
         * @param personalizationString personalization string to distinguish this DRBG (may be null).
         * @param nonce nonce to further distinguish this DRBG (may be null).
         */
        public HashSP800Drbg(IDigest digest, int securityStrength, IEntropySource entropySource, byte[] personalizationString, byte[] nonce)
        {
            if (securityStrength > DrbgUtilities.GetMaxSecurityStrength(digest))
            {
                throw new ArgumentException("Requested security strength is not supported by the derivation function");
            }
            if (entropySource.EntropySize < securityStrength)
            {
                throw new ArgumentException("Not enough entropy for security strength required");
            }

            mDigest           = digest;
            mEntropySource    = entropySource;
            mSecurityStrength = securityStrength;
            mSeedLength       = (int)seedlens[digest.AlgorithmName];

            // 1. seed_material = entropy_input || nonce || personalization_string.
            // 2. seed = Hash_df (seed_material, seedlen).
            // 3. V = seed.
            // 4. C = Hash_df ((0x00 || V), seedlen). Comment: Preceed V with a byte
            // of zeros.
            // 5. reseed_counter = 1.
            // 6. Return V, C, and reseed_counter as the initial_working_state

            byte[] entropy      = GetEntropy();
            byte[] seedMaterial = Arrays.ConcatenateAll(entropy, nonce, personalizationString);
            byte[] seed         = DrbgUtilities.HashDF(mDigest, seedMaterial, mSeedLength);

            mV = seed;
            byte[] subV = new byte[mV.Length + 1];
            Array.Copy(mV, 0, subV, 1, mV.Length);
            mC = DrbgUtilities.HashDF(mDigest, subV, mSeedLength);

            mReseedCounter = 1;
        }
Ejemplo n.º 2
0
        /**
         * Reseed the DRBG.
         *
         * @param additionalInput additional input to be added to the DRBG in this step.
         */
        public void Reseed(byte[] additionalInput)
        {
            // 1. seed_material = 0x01 || V || entropy_input || additional_input.
            //
            // 2. seed = Hash_df (seed_material, seedlen).
            //
            // 3. V = seed.
            //
            // 4. C = Hash_df ((0x00 || V), seedlen).
            //
            // 5. reseed_counter = 1.
            //
            // 6. Return V, C, and reseed_counter for the new_working_state.
            //
            // Comment: Precede with a byte of all zeros.
            byte[] entropy      = GetEntropy();
            byte[] seedMaterial = Arrays.ConcatenateAll(ONE, mV, entropy, additionalInput);
            byte[] seed         = DrbgUtilities.HashDF(mDigest, seedMaterial, mSeedLength);

            mV = seed;
            byte[] subV = new byte[mV.Length + 1];
            subV[0] = 0x00;
            Array.Copy(mV, 0, subV, 1, mV.Length);
            mC = DrbgUtilities.HashDF(mDigest, subV, mSeedLength);

            mReseedCounter = 1;
        }