예제 #1
0
        /// <summary>
        /// Update the
        /// <see cref="encryptor"/>
        /// : calculate counter and
        /// <see cref="padding"/>
        /// .
        /// </summary>
        /// <exception cref="System.IO.IOException"/>
        private void UpdateEncryptor()
        {
            long counter = streamOffset / codec.GetCipherSuite().GetAlgorithmBlockSize();

            padding = unchecked ((byte)(streamOffset % codec.GetCipherSuite().GetAlgorithmBlockSize
                                            ()));
            inBuffer.Position(padding);
            // Set proper position for input data.
            codec.CalculateIV(initIV, counter, iv);
            encryptor.Init(key, iv);
        }
예제 #2
0
 /// <summary>AES/CTR/NoPadding is required</summary>
 public static void CheckCodec(CryptoCodec codec)
 {
     if (codec.GetCipherSuite() != CipherSuite.AesCtrNopadding)
     {
         throw new UnsupportedCodecException("AES/CTR/NoPadding is required");
     }
 }
예제 #3
0
        /// <summary>Get crypto codec for specified algorithm/mode/padding.</summary>
        /// <param name="conf">the configuration</param>
        /// <param name="cipherSuite">algorithm/mode/padding</param>
        /// <returns>
        /// CryptoCodec the codec object. Null value will be returned if no
        /// crypto codec classes with cipher suite configured.
        /// </returns>
        public static CryptoCodec GetInstance(Configuration conf, CipherSuite cipherSuite
                                              )
        {
            IList <Type> klasses = GetCodecClasses(conf, cipherSuite);

            if (klasses == null)
            {
                return(null);
            }
            CryptoCodec codec = null;

            foreach (Type klass in klasses)
            {
                try
                {
                    CryptoCodec c = ReflectionUtils.NewInstance(klass, conf);
                    if (c.GetCipherSuite().GetName().Equals(cipherSuite.GetName()))
                    {
                        if (codec == null)
                        {
                            PerformanceAdvisory.Log.Debug("Using crypto codec {}.", klass.FullName);
                            codec = c;
                        }
                    }
                    else
                    {
                        PerformanceAdvisory.Log.Debug("Crypto codec {} doesn't meet the cipher suite {}."
                                                      , klass.FullName, cipherSuite.GetName());
                    }
                }
                catch (Exception)
                {
                    PerformanceAdvisory.Log.Debug("Crypto codec {} is not available.", klass.FullName
                                                  );
                }
            }
            return(codec);
        }
예제 #4
0
 /// <summary>Check and floor buffer size</summary>
 public static int CheckBufferSize(CryptoCodec codec, int bufferSize)
 {
     Preconditions.CheckArgument(bufferSize >= MinBufferSize, "Minimum value of buffer size is "
                                 + MinBufferSize + ".");
     return(bufferSize - bufferSize % codec.GetCipherSuite().GetAlgorithmBlockSize());
 }
예제 #5
0
 private long GetCounter(long position)
 {
     return(position / codec.GetCipherSuite().GetAlgorithmBlockSize());
 }