Пример #1
0
        static bool Is(Cipher algo, KernelMode mode)
        {
            Session session = new Session();

            switch (algo)
            {
            case Cipher.AES_CBC:
            case Cipher.AES_ECB:
                session.cipher = algo;
                session.keylen = 32;

                fixed(byte *k = &null_key[0])
                session.key = (IntPtr)k;

                break;

            case Cipher.SHA1:
                session.mac = algo;
                break;

            // accept both SHA256 and SHA2_256 and use the correct one
            case Cipher.SHA256:
            case Cipher.SHA2_256:
                if (mode == KernelMode.Ocf)
                {
                    session.mac = Cipher.SHA2_256;
                }
                else
                {
                    session.mac = Cipher.SHA256;
                }
                break;

            default:
                return(false);
            }

            ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
            bool  result;

            if (IntPtr.Size == 4)
            {
                result = ioctl32(fildes, (int)ciocgsession, ref session) == 0;
            }
            else
            {
                result = ioctl64(fildes, ciocgsession, ref session) == 0;
            }

            if (result)
            {
                Mode = mode;
            }
            return(result);
        }
Пример #2
0
 static Helper()
 {
     try {
         fildes = Helper.open ("/dev/crypto", 2 /* O_RDWR */);
         mode = (fildes == -1) ? KernelMode.NotAvailable : KernelMode.Unknown;
     }
     catch (DllNotFoundException) {
         // libc is not available on Windows (e.g. MS.NET) and we
         // do not want to crash with a TypeInitializationException
         mode = KernelMode.NotAvailable;
     }
 }
Пример #3
0
 static Helper()
 {
     try {
         fildes = Helper.open("/dev/crypto", 2 /* O_RDWR */);
         mode   = (fildes == -1) ? KernelMode.NotAvailable : KernelMode.Unknown;
     }
     catch (DllNotFoundException) {
         // libc is not available on Windows (e.g. MS.NET) and we
         // do not want to crash with a TypeInitializationException
         mode = KernelMode.NotAvailable;
     }
 }
Пример #4
0
        static bool Is(Cipher algo, KernelMode mode)
        {
            // asking the kernel for availability turns out to be very costly
            long key = (((long) algo << 32) | (long) mode);
            if (availability.Contains (key))
                return true;

            bool result = false;
            Session session = new Session ();
            fixed (byte* k = &null_key [0]) {
                switch (algo) {
                case Cipher.AES_CBC:
                case Cipher.AES_ECB:
                    session.cipher = algo;
                    session.keylen = 32;
                    session.key = (IntPtr)k;
                    break;
                case Cipher.SHA1:
                    // OCF requires a single buffer that include room for the digest at the end. Its not
                    // compatible with how HashAlgorithm works.
                    if (mode == KernelMode.Ocf)
                        return false;

                    session.mac = algo;
                    break;
                // accept both SHA256 and SHA2_256 and use the correct one
                case Cipher.SHA256:
                case Cipher.SHA256_NEW:
                case Cipher.SHA2_256:
                    if (mode == KernelMode.Ocf)
                        return false;

                    if (sha256.HasValue) {
                        session.mac = sha256.Value;
                    } else {
                        if (mode == KernelMode.Ocf)
                            session.mac = Cipher.SHA2_256;
                        else
                            session.mac = IsNewCryptoDev() ? Cipher.SHA256_NEW : Cipher.SHA256;

                        sha256 = session.mac;
                    }
                    break;
                default:
                    return false;
                }

                ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
                if (IntPtr.Size == 4) {
                    result = ioctl32 (fildes, (int)ciocgsession, ref session) == 0;
                    ioctl32 (fildes, (int)CD_CIOCFSESSION, ref session);
                } else {
                    result = ioctl64 (fildes, ciocgsession, ref session) == 0;
                    ioctl64 (fildes, CD_CIOCFSESSION, ref session);
                }
            }
            if (result) {
                Mode = mode;
                availability.Add (key);
            }
            return result;
        }
Пример #5
0
        static bool Is(Cipher algo, KernelMode mode)
        {
            // asking the kernel for availability turns out to be very costly
            long key = (((long) algo << 32) | (long) mode);
            if (availability.Contains (key))
                return true;

            bool result = false;
            fixed (byte* k = &null_key [0]) {
                Session session = new Session ();
                switch (algo) {
                case Cipher.AES_CBC:
                case Cipher.AES_ECB:
                    session.cipher = algo;
                    session.keylen = 32;
                    session.key = (IntPtr)k;
                    break;
                case Cipher.SHA1:
                    session.mac = algo;
                    break;
                // accept both SHA256 and SHA2_256 and use the correct one
                case Cipher.SHA256:
                case Cipher.SHA2_256:
                    if (mode == KernelMode.Ocf)
                        session.mac = Cipher.SHA2_256;
                    else
                        session.mac = Cipher.SHA256;
                    break;
                default:
                    return false;
                }

                ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
                if (IntPtr.Size == 4)
                    result = ioctl32 (fildes, (int) ciocgsession, ref session) == 0;
                else
                    result = ioctl64 (fildes, ciocgsession, ref session) == 0;
            }
            if (result) {
                Mode = mode;
                availability.Add (key);
            }
            return result;
        }
Пример #6
0
 public static IBindingNamedWithOrOnSyntax <T> InCommandScope <T>(this IBindingInSyntax <T> src, KernelMode mode)
 {
     return(mode switch
     {
         KernelMode.HangFireJob => src.InBackgroundJobScope(),
         KernelMode.Web => src.InRequestScope(),
         _ => throw new ArgumentOutOfRangeException(nameof(mode))
     });
Пример #7
0
        static bool Is(Cipher algo, KernelMode mode)
        {
            Session session = new Session ();
            switch (algo) {
            case Cipher.AES_CBC:
            case Cipher.AES_ECB:
                session.cipher = algo;
                session.keylen = 32;
                fixed (byte* k = &null_key[0])
                    session.key = (IntPtr)k;
                break;
            case Cipher.SHA1:
                session.mac = algo;
                break;
            // accept both SHA256 and SHA2_256 and use the correct one
            case Cipher.SHA256:
            case Cipher.SHA2_256:
                if (mode == KernelMode.Ocf)
                    session.mac = Cipher.SHA2_256;
                else
                    session.mac = Cipher.SHA256;
                break;
            default:
                return false;
            }

            ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;
            bool result;
            if (IntPtr.Size == 4)
                result = ioctl32 (fildes, (int) ciocgsession, ref session) == 0;
            else
                result = ioctl64 (fildes, ciocgsession, ref session) == 0;

            if (result)
                Mode = mode;
            return result;
        }
Пример #8
0
        static bool Is(Cipher algo, KernelMode mode)
        {
            // asking the kernel for availability turns out to be very costly
            long key = (((long)algo << 32) | (long)mode);

            if (availability.Contains(key))
            {
                return(true);
            }

            bool result = false;

            fixed(byte *k = &null_key[0])
            {
                Session session = new Session();

                switch (algo)
                {
                case Cipher.AES_CBC:
                case Cipher.AES_ECB:
                    session.cipher = algo;
                    session.keylen = 32;
                    session.key    = (IntPtr)k;
                    break;

                case Cipher.SHA1:
                    session.mac = algo;
                    break;

                // accept both SHA256 and SHA2_256 and use the correct one
                case Cipher.SHA256:
                case Cipher.SHA2_256:
                    if (mode == KernelMode.Ocf)
                    {
                        session.mac = Cipher.SHA2_256;
                    }
                    else
                    {
                        session.mac = Cipher.SHA256;
                    }
                    break;

                default:
                    return(false);
                }

                ulong ciocgsession = mode == KernelMode.CryptoDev ? CD_CIOCGSESSION : OCF_CIOCGSESSION;

                if (IntPtr.Size == 4)
                {
                    result = ioctl32(fildes, (int)ciocgsession, ref session) == 0;
                }
                else
                {
                    result = ioctl64(fildes, ciocgsession, ref session) == 0;
                }
            }

            if (result)
            {
                Mode = mode;
                availability.Add(key);
            }
            return(result);
        }