Пример #1
0
            private KeyGenerator(FipsAlgorithm algorithm, KeyGenerationParameters keyGenParams, SecureRandom random)
            {
                int keySizeInBits = keyGenParams.KeySize;

                if (CryptoServicesRegistrar.IsInApprovedOnlyMode())
                {
                    Utils.ValidateKeyGenRandom(random, 112, algorithm);

                    if (keySizeInBits != 168 && keySizeInBits != 192)
                    {
                        throw new ArgumentException("Attempt to create key with unapproved key size [" + keySizeInBits + "]: " + algorithm.Name);
                    }
                }
                else
                {
                    if (keySizeInBits != 112 && keySizeInBits != 168 && keySizeInBits != 128 && keySizeInBits != 192)
                    {
                        throw new ArgumentException("Attempt to create key with invalid key size [" + keySizeInBits + "]: " + algorithm.Name);
                    }
                }

                this.algorithm     = algorithm;
                this.keySizeInBits = keySizeInBits;
                this.random        = random;
            }
Пример #2
0
            private KeyGenerationParameters(FipsAlgorithm algorithm, BigInteger publicExponent, int keySize, int certainty) : base(algorithm)
            {
                this.publicExponent = publicExponent;
                this.keySize        = keySize;
                this.certainty      = certainty;

                Validate();
            }
Пример #3
0
            internal Builder(FipsAlgorithm algorithm, FipsDigestAlgorithm digestAlg, Variations variation, SecureRandom random, IEntropySourceProvider entropySourceProvider)
            {
                CryptoStatus.IsReady();

                this.algorithm             = algorithm;
                this.digestAlg             = digestAlg;
                this.variation             = variation;
                this.random                = random;
                this.entropySourceProvider = entropySourceProvider;
            }
Пример #4
0
            private KeyGenerator(FipsAlgorithm algorithm, int keySizeInBits, SecureRandom random)
            {
                if (CryptoServicesRegistrar.IsInApprovedOnlyMode())
                {
                    Utils.ValidateKeyGenRandom(random, keySizeInBits, algorithm);
                }

                this.algorithm     = algorithm;
                this.keySizeInBits = keySizeInBits;
                this.random        = random;
            }
Пример #5
0
            private FipsSecureRandom Build(FipsAlgorithm algorithm, byte[] nonce, bool predictionResistant, byte[] additionalInput)
            {
                if (digestAlg != null)
                {
                    switch (digestAlg.Mode)
                    {
                    case AlgorithmMode.NONE:
                        return(new FipsSecureRandom(random, new DrbgPseudoRandom(algorithm, entropySourceProvider.Get(entropyBitsRequired), new HashDRBGProvider(digestAlg, Arrays.Clone(nonce), personalizationString, securityStrength, additionalInput)), predictionResistant));

                    case AlgorithmMode.HMAC:
                        return(new FipsSecureRandom(random, new DrbgPseudoRandom(algorithm, entropySourceProvider.Get(entropyBitsRequired), new HMacDRBGProvider(digestAlg, Arrays.Clone(nonce), personalizationString, securityStrength, additionalInput)), predictionResistant));

                    default:
                        throw new ArgumentException("unknown algorithm passed to Build(): " + algorithm.Name);
                    }
                }
                else
                {
                    Internal.IBlockCipher cipher;
                    int keySizeInBits;

                    switch (variation)
                    {
                    case Variations.CTR_AES_128:
                        cipher        = FipsAes.ENGINE_PROVIDER.CreateEngine(EngineUsage.GENERAL);
                        keySizeInBits = 128;
                        break;

                    case Variations.CTR_AES_192:
                        cipher        = FipsAes.ENGINE_PROVIDER.CreateEngine(EngineUsage.GENERAL);
                        keySizeInBits = 192;
                        break;

                    case Variations.CTR_AES_256:
                        cipher        = FipsAes.ENGINE_PROVIDER.CreateEngine(EngineUsage.GENERAL);
                        keySizeInBits = 256;
                        break;

                    case Variations.CTR_Triple_DES_168:
                        cipher        = FipsTripleDes.ENGINE_PROVIDER.CreateEngine(EngineUsage.GENERAL);
                        keySizeInBits = 168;
                        break;

                    default:
                        throw new ArgumentException("unknown algorithm passed to Build(): " + algorithm.Name);
                    }

                    return(new FipsSecureRandom(random, new DrbgPseudoRandom(algorithm, entropySourceProvider.Get(entropyBitsRequired), new CTRDRBGProvider(cipher, keySizeInBits, Arrays.Clone(nonce), personalizationString, securityStrength, additionalInput)), predictionResistant));
                }
            }
Пример #6
0
            /// <summary>
            /// Generate a new EC key pair.
            /// </summary>
            /// <returns>A new AsymmetricKeyPair containing an EC key pair.</returns>
            public override AsymmetricKeyPair <AsymmetricECPublicKey, AsymmetricECPrivateKey> GenerateKeyPair()
            {
                AsymmetricCipherKeyPair kp = engine.GenerateKeyPair();

                Internal.Parameters.ECPublicKeyParameters  pubKey = (Internal.Parameters.ECPublicKeyParameters)kp.Public;
                Internal.Parameters.ECPrivateKeyParameters prvKey = (Internal.Parameters.ECPrivateKeyParameters)kp.Private;

                FipsAlgorithm algorithm = this.Parameters.Algorithm;

                // FSM_STATE:5.4, "EC PAIRWISE CONSISTENCY TEST", "The module is performing EC Pairwise Consistency self-test"
                // FSM_TRANS:5.EC.0,"CONDITIONAL TEST", "EC PAIRWISE CONSISTENCY TEST", "Invoke EC Pairwise Consistency test"
                validateKeyPair(algorithm, kp);
                // FSM_TRANS:5.EC.1,"EC PAIRWISE CONSISTENCY TEST", "CONDITIONAL TEST", "EC Pairwise Consistency test successful"

                return(new AsymmetricKeyPair <AsymmetricECPublicKey, AsymmetricECPrivateKey>(new AsymmetricECPublicKey(algorithm, domainParameters, pubKey.Q), new AsymmetricECPrivateKey(algorithm, domainParameters, prvKey.D, pubKey.Q)));
            }
Пример #7
0
            /// <summary>
            /// Generate a new DSA key pair.
            /// </summary>
            /// <returns>A new AsymmetricKeyPair containing a DSA key pair.</returns>
            public override AsymmetricKeyPair <AsymmetricDsaPublicKey, AsymmetricDsaPrivateKey> GenerateKeyPair()
            {
                AsymmetricCipherKeyPair kp = engine.GenerateKeyPair();

                DsaPublicKeyParameters  pubKey = (DsaPublicKeyParameters)kp.Public;
                DsaPrivateKeyParameters prvKey = (DsaPrivateKeyParameters)kp.Private;

                FipsAlgorithm algorithm = this.Parameters.Algorithm;

                // FSM_STATE:5.3, "DSA PAIRWISE CONSISTENCY TEST", "The module is performing DSA Pairwise Consistency self-test"
                // FSM_TRANS:5.DSA.0,"CONDITIONAL TEST", "DSA PAIRWISE CONSISTENCY TEST", "Invoke DSA Pairwise Consistency test"
                validateKeyPair(kp);
                // FSM_TRANS:5.DSA.1,"DSA PAIRWISE CONSISTENCY TEST", "CONDITIONAL TEST", "DSA Pairwise Consistency test successful"

                return(new AsymmetricKeyPair <AsymmetricDsaPublicKey, AsymmetricDsaPrivateKey>(new AsymmetricDsaPublicKey(algorithm, domainParameters, pubKey.Y), new AsymmetricDsaPrivateKey(algorithm, domainParameters, prvKey.X)));
            }
Пример #8
0
 public Key(FipsAlgorithm algorithm, byte[] bytes)
     : base(algorithm, bytes)
 {
     if (algorithm == Alg112 && bytes.Length != 16)
     {
         throw new ArgumentException("Key must be 128 bits long");
     }
     else if (algorithm == Alg168 && bytes.Length != 24)
     {
         throw new ArgumentException("Key must be 192 bits long");
     }
     else
     {
         ValidateKeySize(bytes);
     }
 }
Пример #9
0
        private static void validateKeyPair(FipsAlgorithm algorithm, AsymmetricCipherKeyPair kp)
        {
            switch (algorithm.Mode)
            {
            case AlgorithmMode.NONE:
            case AlgorithmMode.DSA:
                SelfTestExecutor.Validate(algorithm, kp, new DsaConsistencyTest());
                break;

            case AlgorithmMode.CDH:
                SelfTestExecutor.Validate(algorithm, kp, new CdhConsistencyTest());
                break;

            default:
                throw new InvalidOperationException("Unhandled EC algorithm: " + algorithm.Name);
            }
        }
Пример #10
0
            public override AsymmetricKeyPair <AsymmetricRsaPublicKey, AsymmetricRsaPrivateKey> GenerateKeyPair()
            {
                AsymmetricCipherKeyPair kp = engine.GenerateKeyPair();

                RsaKeyParameters           pubKey = (RsaKeyParameters)kp.Public;
                RsaPrivateCrtKeyParameters prvKey = (RsaPrivateCrtKeyParameters)kp.Private;

                FipsAlgorithm algorithm = this.Parameters.Algorithm;

                // FSM_STATE:5.5, "RSA PAIRWISE CONSISTENCY TEST", "The module is performing RSA Pairwise Consistency self-test"
                // FSM_TRANS:5.RSA.0,"CONDITIONAL TEST", "RSA PAIRWISE CONSISTENCY TEST", "Invoke RSA Pairwise Consistency test"
                ValidateKeyPair(kp);
                // FSM_TRANS:5.RSA.1,"RSA PAIRWISE CONSISTENCY TEST", "CONDITIONAL TEST", "RSA Pairwise Consistency test successful"

                // we register the modulus value so that is in validated modulus cache
                // otherwise the modulus will be revalidated on key construction.
                AsymmetricRsaKey.RegisterModulus(prvKey.Modulus);

                AsymmetricRsaPrivateKey privateKey = new AsymmetricRsaPrivateKey(algorithm, prvKey.Modulus, prvKey.PublicExponent, prvKey.Exponent,
                                                                                 prvKey.P, prvKey.Q, prvKey.DP, prvKey.DQ, prvKey.QInv);

                return(new AsymmetricKeyPair <AsymmetricRsaPublicKey, AsymmetricRsaPrivateKey>(new AsymmetricRsaPublicKey(algorithm, pubKey.Modulus, pubKey.Exponent),
                                                                                               privateKey));
            }
Пример #11
0
 protected DRBGSelfTest(FipsAlgorithm algorithm) : base(algorithm)
 {
 }
Пример #12
0
 internal static void ValidateKeyPairGenRandom(SecureRandom random, int securityStrength, FipsAlgorithm algorithm)
 {
     ValidateRandom(random, securityStrength, algorithm, "attempt to create key pair with unapproved RNG");
 }
Пример #13
0
 internal FipsAlgorithm(FipsAlgorithm algorithm, AlgorithmMode mode) : base(algorithm.Name, mode)
 {
 }
Пример #14
0
 internal TlsKdfWithPrfParameters(FipsAlgorithm algorithm, FipsDigestAlgorithm prf, byte[] secret, string label, byte[] seedMaterial) : base(algorithm, secret, label, seedMaterial)
 {
     this.prf = prf;
 }
Пример #15
0
 internal TlsKdfBuilder(FipsAlgorithm algorithm)
 {
     this.algorithm = algorithm;
 }
Пример #16
0
 KeyGenerationParameters(FipsAlgorithm algorithm, ECDomainParameters domainParameters) : base(algorithm)
 {
     this.domainParameters = domainParameters;
 }
Пример #17
0
 internal FipsKdfAlgorithm(FipsAlgorithm kdfAlgorithm, FipsPrfAlgorithm prfAlgorithm) : base(kdfAlgorithm, prfAlgorithm)
 {
 }
Пример #18
0
 internal DRBGCTRSelfTest(FipsAlgorithm algorithm, DRBGTestVector tv) : base(algorithm)
 {
     this.tv = tv;
 }
Пример #19
0
 internal TlsKdfWithPrfBuilder(FipsAlgorithm algorithm, FipsDigestAlgorithm prf)
 {
     this.algorithm = algorithm;
     this.prf       = prf;
 }
Пример #20
0
 public DesEdeKeyGenerator(FipsAlgorithm algorithm)
 {
     this.algorithm = algorithm;
 }
Пример #21
0
 internal AgreementKdfBuilder(FipsAlgorithm algorithm, FipsPrfAlgorithm prf, byte[] iv)
 {
     this.algorithm = algorithm;
     this.prf       = prf;
     this.iv        = iv;
 }
Пример #22
0
 internal static void ValidateRandom(SecureRandom random, int securityStrength, FipsAlgorithm algorithm, String message)
 {
     if (random is FipsSecureRandom)
     {
         if (((FipsSecureRandom)random).SecurityStrength < securityStrength)
         {
             throw new CryptoUnapprovedOperationError("FIPS SecureRandom security strength not as high as required for operation", algorithm);
         }
     }
     else
     {
         throw new CryptoUnapprovedOperationError(message, algorithm);
     }
 }