Пример #1
0
        public override int Read(
            byte[]  buffer,
            int offset,
            int count)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "CipherStream");

            if (inCipher == null)
            {
                return(stream.Read(buffer, offset, count));
            }

            int num = 0;

            while (num < count)
            {
                if (mInBuf == null || mInPos >= mInBuf.Length)
                {
                    if (!FillInBuf())
                    {
                        break;
                    }
                }

                int numToCopy = System.Math.Min(count - num, mInBuf.Length - mInPos);
                Array.Copy(mInBuf, mInPos, buffer, offset + num, numToCopy);
                mInPos += numToCopy;
                num    += numToCopy;
            }

            return(num);
        }
Пример #2
0
        public override void Flush()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "CipherStream");

            // Note: outCipher.DoFinal is only called during Close()
            stream.Flush();
        }
Пример #3
0
        public override void Write(
            byte[]  buffer,
            int offset,
            int count)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "CipherStream");

            Debug.Assert(buffer != null);
            Debug.Assert(0 <= offset && offset <= buffer.Length);
            Debug.Assert(count >= 0);

            int end = offset + count;

            Debug.Assert(0 <= end && end <= buffer.Length);

            if (outCipher == null)
            {
                stream.Write(buffer, offset, count);
                return;
            }

            byte[] data = outCipher.ProcessBytes(buffer, offset, count);
            if (data != null)
            {
                stream.Write(data, 0, data.Length);
            }
        }
Пример #4
0
        public override void Close()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            Platform.Dispose(stream);
            base.Close();
        }
Пример #5
0
        public override void SetLength(
            long length)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            stream.SetLength(length);
        }
Пример #6
0
        public override long Seek(
            long offset,
            SeekOrigin origin)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            return(stream.Seek(offset, origin));
        }
Пример #7
0
            public IMacFactory <A> CreateMacFactory <A>(A algorithmDetails) where A : IAuthenticationParameters <A, Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "Service");

                ValidateKeyUse(keyAlg, keyBytes, algorithmDetails.Algorithm);

                return(((IMacFactoryProvider <A>)prov).CreateMacFactory(algorithmDetails));
            }
Пример #8
0
            public IKeyWrapper <A> CreateKeyWrapper <A>(A algorithmDetails) where A : ISymmetricWrapParameters <A, Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "Service");

                ValidateKeyUse(keyAlg, keyBytes, algorithmDetails.Algorithm);

                return(((IKeyWrapperProvider <A>)prov).CreateKeyWrapper(algorithmDetails));
            }
Пример #9
0
            public IAeadCipherBuilder <A> CreateAeadEncryptorBuilder <A>(A algorithmDetails) where A : IParameters <Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "Service");

                ValidateKeyUse(keyAlg, keyBytes, algorithmDetails.Algorithm);

                return(((IAeadEncryptorBuilderProvider <A>)prov).CreateAeadEncryptorBuilder(algorithmDetails));
            }
Пример #10
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

                Platform.Dispose(stream);
            }
            base.Dispose(disposing);
        }
Пример #11
0
        public ICipherBuilder <A> CreateEncryptorBuilder <A>(A algorithmDetails) where A : IParameters <Algorithm>
        {
            CryptoServicesRegistrar.ApprovedModeCheck(false, "Service");

            if (algorithmDetails is IParametersWithIV <IParameters <GeneralAlgorithm>, GeneralAlgorithm> )
            {
                return(((IEncryptorBuilderProvider <A>)provWithIV).CreateEncryptorBuilder(algorithmDetails));
            }
            return(((IEncryptorBuilderProvider <A>)prov).CreateEncryptorBuilder(algorithmDetails));
        }
Пример #12
0
        public IMacFactory <A> CreateMacFactory <A>(A algorithmDetails) where A : IAuthenticationParameters <A, Algorithm>
        {
            CryptoServicesRegistrar.ApprovedModeCheck(false, "Service");

            if (algorithmDetails is IAuthenticationParametersWithIV <A, Algorithm> )
            {
                return(((IMacFactoryProvider <A>)provWithIV).CreateMacFactory(algorithmDetails));
            }

            return(((IMacFactoryProvider <A>)prov).CreateMacFactory(algorithmDetails));
        }
Пример #13
0
        public override void WriteByte(
            byte b)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            if (outDigest != null)
            {
                outDigest.Update(b);
            }
            stream.WriteByte(b);
        }
Пример #14
0
        public override void Close()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "CipherStream");

            if (outCipher != null)
            {
                byte[] data = outCipher.DoFinal();
                stream.Write(data, 0, data.Length);
                stream.Flush();
            }
            Platform.Dispose(stream);
            base.Close();
        }
            public IKeyWrapper <A> CreateKeyWrapper <A>(A algorithmDetails) where A : IParameters <Algorithm>
            {
                Rsa.Pkcs1v15WrapParameters pkcsP = algorithmDetails as Rsa.Pkcs1v15WrapParameters;
                if (pkcsP != null)
                {
                    CryptoServicesRegistrar.ApprovedModeCheck(false, "RSA");
                    General.Utils.ApprovedModeCheck("service", algorithmDetails.Algorithm);

                    return((IKeyWrapper <A>) new Rsa.Pkcs1v15KeyWrapper(pkcsP, publicKey));
                }

                return((IKeyWrapper <A>) new Fips.FipsRsa.RsaKeyWrapper(algorithmDetails as Fips.FipsRsa.OaepWrapParameters, publicKey));
            }
Пример #16
0
            public ICipherBuilder <A> CreateDecryptorBuilder <A>(A algorithmDetails) where A : IParameters <Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "Service");

                ValidateKeyUse(keyAlg, keyBytes, algorithmDetails.Algorithm, true);

                if (algorithmDetails is TripleDes.ParametersWithIV)
                {
                    CryptoServicesRegistrar.GeneralModeCheck(approvedOnlyMode, algorithmDetails.Algorithm);

                    return(((IDecryptorBuilderProvider <A>)genProvForIV).CreateDecryptorBuilder(algorithmDetails));
                }
                return(((IDecryptorBuilderProvider <A>)prov).CreateDecryptorBuilder(algorithmDetails));
            }
Пример #17
0
            public ISignatureFactory <A> CreateSignatureFactory <A>(A algorithmDetails) where A : IParameters <Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "RSA");

                Fips.FipsRsa.PssSignatureParameters pssParams = algorithmDetails as Fips.FipsRsa.PssSignatureParameters;
                if (pssParams != null)
                {
                    return((ISignatureFactory <A>) new SignatureFactory <Fips.FipsRsa.PssSignatureParameters>(pssParams, new Fips.FipsRsa.PssSignerProvider(pssParams, privateKey)));
                }

                Fips.FipsRsa.SignatureParameters sigParams = algorithmDetails as Fips.FipsRsa.SignatureParameters;

                return((ISignatureFactory <A>) new SignatureFactory <Fips.FipsRsa.SignatureParameters>(sigParams, new Fips.FipsRsa.SignerProvider(sigParams, privateKey)));
            }
Пример #18
0
        public override int ReadByte()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            int b = stream.ReadByte();

            if (inDigest != null)
            {
                if (b >= 0)
                {
                    inDigest.Update((byte)b);
                }
            }
            return(b);
        }
Пример #19
0
            public ISignatureFactory<A> CreateSignatureFactory<A>(A algorithmDetails) where A : IParameters<Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "EC");

                if (algorithmDetails is Fips.FipsEC.SignatureParameters)
                {
                    return (ISignatureFactory<A>)new SignatureFactory<Fips.FipsEC.SignatureParameters>(algorithmDetails as Fips.FipsEC.SignatureParameters, new Fips.FipsEC.SignerProvider(algorithmDetails as Fips.FipsEC.SignatureParameters, privateKey));
                }
                else
                {
                    General.Utils.ApprovedModeCheck("service", algorithmDetails.Algorithm);

                    return (ISignatureFactory<A>)new SignatureFactory<General.EC.SignatureParameters>(algorithmDetails as General.EC.SignatureParameters, new General.EC.SignerProvider(algorithmDetails as General.EC.SignatureParameters, privateKey));
                }
            }
Пример #20
0
        public override void Write(
            byte[]  buffer,
            int offset,
            int count)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            if (outDigest != null)
            {
                if (count > 0)
                {
                    outDigest.BlockUpdate(buffer, offset, count);
                }
            }
            stream.Write(buffer, offset, count);
        }
Пример #21
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "CipherStream");

                if (outCipher != null)
                {
                    byte[] data = outCipher.DoFinal();
                    stream.Write(data, 0, data.Length);
                    stream.Flush();
                }
                Platform.Dispose(stream);
            }
            base.Dispose(disposing);
        }
Пример #22
0
        public override void WriteByte(
            byte b)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "CipherStream");

            if (outCipher == null)
            {
                stream.WriteByte(b);
                return;
            }

            byte[] data = outCipher.ProcessByte(b);
            if (data != null)
            {
                stream.Write(data, 0, data.Length);
            }
        }
Пример #23
0
        public override int Read(
            byte[]  buffer,
            int offset,
            int count)
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            int n = stream.Read(buffer, offset, count);

            if (inDigest != null)
            {
                if (n > 0)
                {
                    inDigest.BlockUpdate(buffer, offset, n);
                }
            }
            return(n);
        }
Пример #24
0
        public override int ReadByte()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "CipherStream");

            if (inCipher == null)
            {
                return(stream.ReadByte());
            }

            if (mInBuf == null || mInPos >= mInBuf.Length)
            {
                if (!FillInBuf())
                {
                    return(-1);
                }
            }

            return(mInBuf[mInPos++]);
        }
Пример #25
0
            public IBlockCipherBuilder <A> CreateBlockEncryptorBuilder <A>(A algorithmDetails) where A : IParameters <Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "Service");

                ValidateKeyUse(keyAlg, keyBytes, algorithmDetails.Algorithm);

                try
                {
                    return(((IBlockEncryptorBuilderProvider <A>)prov).CreateBlockEncryptorBuilder(algorithmDetails));
                }
                catch (InvalidCastException e)
                {
                    if (!AlgorithmModeUtils.isBlockCipherMode(algorithmDetails.Algorithm))
                    {
                        throw new NotSupportedException("cannot create a block encryptor from non-block mode", e);
                    }
                    throw e;
                }
            }
            public IKeyWrapper <A> CreateKeyWrapper <A>(A algorithmDetails) where A : IParameters <Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "DH");
                General.Utils.ApprovedModeCheck("service", algorithmDetails.Algorithm);

                ElGamal.OaepWrapParameters oaepP = algorithmDetails as ElGamal.OaepWrapParameters;
                if (oaepP != null)
                {
                    return((IKeyWrapper <A>) new ElGamal.OaepKeyWrapper(oaepP, publicKey));
                }

                ElGamal.Pkcs1v15WrapParameters pkcsP = algorithmDetails as ElGamal.Pkcs1v15WrapParameters;
                if (pkcsP != null)
                {
                    return((IKeyWrapper <A>) new ElGamal.Pkcs1v15KeyWrapper(pkcsP, publicKey));
                }

                throw new ArgumentException("unknown algorithm parameters");
            }
Пример #27
0
        internal virtual IDigest WriteDigest()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            return(outDigest);
        }
Пример #28
0
            public IAgreementCalculator<A> CreateAgreementCalculator<A>(A algorithmDetails) where A : IParameters<Algorithm>
            {
                CryptoServicesRegistrar.ApprovedModeCheck(approvedOnlyMode, "EC");

                return (IAgreementCalculator<A>)new FipsEC.AgreementCalculator(algorithmDetails as FipsEC.AgreementParameters, privateKey);
            }
Пример #29
0
        public override void Flush()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "DigestStream");

            stream.Flush();
        }
Пример #30
0
        internal virtual ISigner WriteSigner()
        {
            CryptoServicesRegistrar.ApprovedModeCheck(isApprovedModeOnly, "SignerStream");

            return(outSigner);
        }