예제 #1
0
        internal static ICipherBuilder <IParameters <Algorithm> > CreateDecryptorBuilder(AlgorithmIdentifier encScheme, byte[] derivedKey, IParameters <Algorithm> parameters)
        {
            DerObjectIdentifier encSchemeAlg = encScheme.Algorithm;

            if (encSchemeAlg.On(NistObjectIdentifiers.Aes))
            {
                IAeadBlockCipherService service = CryptoServicesRegistrar.CreateService(new FipsAes.Key(derivedKey));

                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Cfb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Cfb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Cfb))
                {
                    return(service.CreateDecryptorBuilder(parameters));
                }
                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Ofb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Ofb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Ofb))
                {
                    return(service.CreateDecryptorBuilder(parameters));
                }
            }

            throw new ArgumentException("cannot match decryption algorithm");
        }
예제 #2
0
        internal static IAeadCipherBuilder <IParameters <Algorithm> > CreateAeadEncryptorBuilder(DerObjectIdentifier keyEncAlgorithm, byte[] derivedKey, IParameters <Algorithm> parameters)
        {
            DerObjectIdentifier encSchemeAlg = keyEncAlgorithm;

            if (encSchemeAlg.On(NistObjectIdentifiers.Aes))
            {
                IAeadBlockCipherService service = CryptoServicesRegistrar.CreateService(new FipsAes.Key(derivedKey));

                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Ccm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Ccm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Ccm))
                {
                    return(service.CreateAeadEncryptorBuilder((FipsAes.AuthenticationParametersWithIV)parameters));
                }
                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Gcm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Gcm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Gcm))
                {
                    return(service.CreateAeadEncryptorBuilder((FipsAes.AuthenticationParametersWithIV)parameters));
                }
            }

            throw new ArgumentException("cannot match encryption algorithm");
        }
예제 #3
0
        internal static IParameters <Algorithm> GetCipherParameters(AlgorithmIdentifier encScheme)
        {
            DerObjectIdentifier encSchemeAlg = encScheme.Algorithm;

            if (encSchemeAlg.On(NistObjectIdentifiers.Aes))
            {
                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Ecb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Ecb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Ecb))
                {
                    return(FipsAes.Ecb);
                }

                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Cbc) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Cbc) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Cbc))
                {
                    byte[] iv = DerOctetString.GetInstance(encScheme.Parameters).GetOctets();

                    return(FipsAes.Cbc.WithIV(iv));
                }
                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Cfb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Cfb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Cfb))
                {
                    byte[] iv = DerOctetString.GetInstance(encScheme.Parameters).GetOctets();

                    return(FipsAes.Cfb128.WithIV(iv));
                }
                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Ofb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Ofb) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Ofb))
                {
                    byte[] iv = DerOctetString.GetInstance(encScheme.Parameters).GetOctets();

                    return(FipsAes.Ofb.WithIV(iv));
                }
                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Ccm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Ccm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Ccm))
                {
                    CcmParameters authParams = CcmParameters.GetInstance(encScheme.Parameters);

                    return(FipsAes.Ccm.WithIV(authParams.GetNonce()).WithMacSize(authParams.IcvLen * 8));
                }
                if (encSchemeAlg.Equals(NistObjectIdentifiers.IdAes128Gcm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes192Gcm) || encSchemeAlg.Equals(NistObjectIdentifiers.IdAes256Gcm))
                {
                    GcmParameters authParams = GcmParameters.GetInstance(encScheme.Parameters);

                    return(FipsAes.Ccm.WithIV(authParams.GetNonce()).WithMacSize(authParams.IcvLen * 8));
                }
            }

            if (encSchemeAlg.Equals(NttObjectIdentifiers.IdCamellia128Cbc) || encSchemeAlg.Equals(NttObjectIdentifiers.IdCamellia192Cbc) || encSchemeAlg.Equals(NttObjectIdentifiers.IdCamellia256Cbc))
            {
                byte[] iv = DerOctetString.GetInstance(encScheme.Parameters).GetOctets();

                return(Camellia.Cbc.WithIV(iv));
            }

            if (encSchemeAlg.Equals(PkcsObjectIdentifiers.DesEde3Cbc))
            {
                byte[] iv = DerOctetString.GetInstance(encScheme.Parameters).GetOctets();

                return(FipsTripleDes.Cbc.WithIV(iv));
            }

            if (encSchemeAlg.Equals(KisaObjectIdentifiers.IdSeedCbc))
            {
                byte[] iv = DerOctetString.GetInstance(encScheme.Parameters).GetOctets();

                return(Seed.Cbc.WithIV(iv));
            }


            throw new ArgumentException("cannot match algorithm");
        }