示例#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;
 }
 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);
     }
 }