Exemplo n.º 1
0
 //修改key or iv后需调用。
 private void Reset()
 {
     if (MbedTLS.cipher_reset(_ctx) != 0)
     {
         throw new EncryptorException("Cannot finalize mbed TLS cipher context.");
     }
     Finished = true;
 }
Exemplo n.º 2
0
 public void SetMethod(string method)
 {
     Method = method;
     Info   = _ciphers[method];
     if (MbedTLS.cipher_setup(_ctx, MbedTLS.cipher_info_from_string(Info.InnerLibName)) != 0)
     {
         throw new EncryptorException("Cannot initialize mbed TLS cipher  context.");
     }
 }
Exemplo n.º 3
0
        private void Rc4Md5_UpdateKey()
        {
            Contract.Assert(Key != null);
            Contract.Assert(IV != null);

            byte[] temp = new byte[KeySize + IVSize];
            Array.Copy(Key, 0, temp, 0, KeySize);
            Array.Copy(IV, 0, temp, KeySize, IVSize);
            MbedTLS.MD5(temp, Key);
        }
Exemplo n.º 4
0
 private void CipherUpdate(bool isCipher, int length, byte[] buf, byte[] outBuf)
 {
     if (_disposed)
     {
         throw new ObjectDisposedException(this.ToString());
     }
     if (MbedTLS.cipher_update(isCipher ? _encryptCtx : _decryptCtx, buf, length, outBuf, ref length) != 0)
     {
         throw new EncryptorException("Cannot update mbed TLS cipher context");
     }
 }
Exemplo n.º 5
0
        private void Init()
        {
            IntPtr ctx = Marshal.AllocHGlobal(MbedTLS.cipher_get_size_ex());

            MbedTLS.cipher_init(ctx);
            if (MbedTLS.cipher_setup(ctx, MbedTLS.cipher_info_from_string(FormalName)) != 0)
            {
                throw new EncryptorException("Cannot initialize mbed TLS cipher  context.");
            }
            _ctx = ctx;
        }
Exemplo n.º 6
0
 private void CipherUpdate(byte[] buf, int length, byte[] outBuf)
 {
     Contract.Requires(buf != outBuf);//outBuf不能跟buf相同,outBuf的长度不小于length+block_size;
     if (_disposed)
     {
         throw new ObjectDisposedException(this.ToString());
     }
     if (MbedTLS.cipher_update(_ctx, buf, length, outBuf, ref length) != 0)
     {
         throw new EncryptorException("Cannot update mbed TLS cipher context");
     }
     //int ret = MbedTLS.cipher_update(_ctx, buf, length, outBuf, ref length);
 }
Exemplo n.º 7
0
 public void SetKey(string password)
 {
     Key = CreateKey(password, Info.KeySize);
     if (Method == "rc4-md5")
     {
         Rc4Md5_UpdateKey();
     }
     if (MbedTLS.cipher_setkey(_ctx, Key, KeySize * 8, IsCipher ? MbedTLS.MbedTLS_Encrypt :
                               MbedTLS.MbedTLS_Decrypt) != 0)
     {
         throw new EncryptorException("Cannot set mbed TLS cipher key.");
     }
     Reset();
 }
Exemplo n.º 8
0
        public void SetIV(byte[] iv)
        {
            Contract.Requires(Initialized);
            Contract.Requires(iv != null && iv.Length >= IVSize);

            IV = iv;
            if (Method == "rc4-md5")
            {
                Rc4Md5_UpdateKey();
            }
            if (MbedTLS.cipher_set_iv(_ctx, iv, IVSize) != 0)
            {
                throw new EncryptorException("Cannot set mbed TLS cipher IV.");
            }
            Reset();
        }
Exemplo n.º 9
0
        public void InitCipher(byte[] key, byte[] iv, bool isCipher)
        {
            IntPtr ctx = Marshal.AllocHGlobal(MbedTLS.cipher_get_size_ex());

            if (isCipher)
            {
                _encryptIV  = iv;
                _encryptCtx = ctx;
            }
            else
            {
                _decryptIV  = iv;
                _decryptCtx = ctx;
            }
            byte[] realkey = key;
            if (Method == "rc4-md5")
            {
                byte[] temp = new byte[KeySize + IVSize];
                //realkey = new byte[KeySize];
                Array.Copy(key, 0, temp, 0, KeySize);
                Array.Copy(iv, 0, temp, KeySize, IVSize);
                realkey = MbedTLS.MD5(temp);
            }
            Key = realkey;
            MbedTLS.cipher_init(ctx);
            if (MbedTLS.cipher_setup(ctx, MbedTLS.cipher_info_from_string(FormalName)) != 0)
            {
                throw new EncryptorException("Cannot initialize mbed TLS cipher  context.");
            }
            if (MbedTLS.cipher_setkey(ctx, realkey, KeySize * 8, isCipher ? MbedTLS.MbedTLS_Encrypt :
                                      MbedTLS.MbedTLS_Decrypt) != 0)
            {
                throw new EncryptorException("Cannot set mbed TLS cipher key.");
            }
            if (MbedTLS.cipher_set_iv(ctx, iv, IVSize) != 0)
            {
                throw new EncryptorException("Cannot set mbed TLS cipher IV.");
            }
            if (MbedTLS.cipher_reset(ctx) != 0)
            {
                throw new EncryptorException("Cannot finalize mbed TLS cipher context.");
            }
        }
Exemplo n.º 10
0
        protected virtual void Disponse(bool disposing)
        {
            if (_disposed)
            {
                return;
            }
            _disposed = true;
            if (disposing)
            {
                // free managed objects
            }

            //free unmanaged objects
            if (_ctx != IntPtr.Zero)
            {
                MbedTLS.cipher_free(_ctx);
                Marshal.FreeHGlobal(_ctx);
                _ctx = IntPtr.Zero;
            }
        }
Exemplo n.º 11
0
        public static void BytesToKey(byte[] bytes, byte[] key)
        {
            byte[] result = new byte[bytes.Length + 16];
            int    i      = 0;

            byte[] md5sum = null;
            while (i < key.Length)
            {
                if (i == 0)
                {
                    md5sum = MbedTLS.MD5(bytes);
                }
                else
                {
                    md5sum.CopyTo(result, 0);
                    bytes.CopyTo(result, md5sum.Length);
                    md5sum = MbedTLS.MD5(result);
                }
                md5sum.CopyTo(key, i);
                i += md5sum.Length;
            }
        }
Exemplo n.º 12
0
        public void SetKV(string password, byte[] iv)
        {
            Contract.Requires(Initialized);
            Contract.Requires(password != null);
            Contract.Requires(iv != null && iv.Length >= IVSize);

            Key = CreateKey(password, Info.KeySize);
            IV  = iv;
            if (Method == "rc4-md5")
            {
                Rc4Md5_UpdateKey();
            }
            if (MbedTLS.cipher_setkey(_ctx, Key, KeySize * 8, IsCipher ? MbedTLS.MbedTLS_Encrypt :
                                      MbedTLS.MbedTLS_Decrypt) != 0)
            {
                throw new EncryptorException("Cannot set mbed TLS cipher key.");
            }
            if (MbedTLS.cipher_set_iv(_ctx, iv, IVSize) != 0)
            {
                throw new EncryptorException("Cannot set mbed TLS cipher IV.");
            }
            Reset();
        }