Esempio n. 1
0
        // helper method to reduce the amount of generate code for each cipher algorithm
        static internal IntPtr Create(CCOperation operation, CCAlgorithm algorithm, CCOptions options, byte[] key, byte[] iv)
        {
            if (key == null)
            {
                throw new CryptographicException("A null key was provided");
            }

            // unlike the .NET framework CommonCrypto does not support two-keys triple-des (128 bits) ref: #6967
            if ((algorithm == CCAlgorithm.TripleDES) && (key.Length == 16))
            {
                byte[] key3 = new byte [24];
                Buffer.BlockCopy(key, 0, key3, 0, 16);
                Buffer.BlockCopy(key, 0, key3, 16, 8);
                key = key3;
            }

            IntPtr          cryptor = IntPtr.Zero;
            CCCryptorStatus status  = Cryptor.CCCryptorCreate(operation, algorithm, options, key, (IntPtr)key.Length, iv, ref cryptor);

            if (status != CCCryptorStatus.Success)
            {
                throw new CryptographicUnexpectedOperationException();
            }
            return(cryptor);
        }
 protected virtual void Dispose(bool disposing)
 {
     if (handle != IntPtr.Zero)
     {
         Cryptor.CCCryptorRelease(handle);
         handle = IntPtr.Zero;
     }
     GC.SuppressFinalize(this);
 }
Esempio n. 3
0
        // PRO: doing this ensure all cipher modes and padding modes supported by .NET will be available with CommonCrypto (drop-in replacements)
        // CON: doing this will only process one block at the time, so it's not ideal for performance, but still a lot better than managed
        protected override void ECB(byte[] input, byte[] output)
        {
            IntPtr          len = IntPtr.Zero;
            CCCryptorStatus s   = Cryptor.CCCryptorUpdate((encrypt == encryption) ? handle : handle_e,
                                                          input, (IntPtr)input.Length, output, (IntPtr)output.Length, ref len);

            if (((int)len != output.Length) || (s != CCCryptorStatus.Success))
            {
                throw new CryptographicUnexpectedOperationException(s.ToString());
            }
        }
Esempio n. 4
0
 protected override void Dispose(bool disposing)
 {
     if (handle != IntPtr.Zero)
     {
         Cryptor.CCCryptorRelease(handle);
         handle = IntPtr.Zero;
     }
     if (handle_e != IntPtr.Zero)
     {
         Cryptor.CCCryptorRelease(handle_e);
         handle_e = IntPtr.Zero;
     }
     base.Dispose(disposing);
     GC.SuppressFinalize(this);
 }
        int Transform(byte[] input, int inputOffset, byte[] output, int outputOffset, int length)
        {
            IntPtr len     = IntPtr.Zero;
            IntPtr in_len  = (IntPtr)length;
            IntPtr out_len = (IntPtr)(output.Length - outputOffset);

            fixed(byte *inputBuffer = &input[0])
            fixed(byte *outputBuffer = &output [0])
            {
                CCCryptorStatus s = Cryptor.CCCryptorUpdate(handle, (IntPtr)(inputBuffer + inputOffset), in_len, (IntPtr)(outputBuffer + outputOffset), out_len, ref len);

                if (s != CCCryptorStatus.Success)
                {
                    throw new CryptographicException(s.ToString());
                }
            }
            return((int)len);
        }
Esempio n. 6
0
 static public byte[] IV(int size)
 {
     byte[] buffer = new byte [size];
     Cryptor.GetRandom(buffer);
     return(buffer);
 }
 private void Random(byte[] buffer, int start, int length)
 {
     byte[] random = new byte [length];
     Cryptor.GetRandom(random);
     Buffer.BlockCopy(random, 0, buffer, start, length);
 }