상속: IBlockCipher
예제 #1
0
 private int[] generateWorkingKey(bool forEncryption, byte[] userKey)
 {
     this.forEncryption = forEncryption;
     if (userKey.Length != 32)
     {
         throw new ArgumentException("Key length invalid. Key needs to be 32 byte - 256 bit!!!");
     }
     int[] array = new int[8];
     for (int num = 0; num != 8; num++)
     {
         array[num] = Gost28147Engine.bytesToint(userKey, num * 4);
     }
     return(array);
 }
예제 #2
0
        private void Gost28147Func(int[] workingKey, byte[] inBytes, int inOff, byte[] outBytes, int outOff)
        {
            int num  = Gost28147Engine.bytesToint(inBytes, inOff);
            int num2 = Gost28147Engine.bytesToint(inBytes, inOff + 4);

            if (this.forEncryption)
            {
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 8; j++)
                    {
                        int num3 = num;
                        int num4 = this.Gost28147_mainStep(num, workingKey[j]);
                        num  = (num2 ^ num4);
                        num2 = num3;
                    }
                }
                for (int k = 7; k > 0; k--)
                {
                    int num3 = num;
                    num  = (num2 ^ this.Gost28147_mainStep(num, workingKey[k]));
                    num2 = num3;
                }
            }
            else
            {
                for (int l = 0; l < 8; l++)
                {
                    int num3 = num;
                    num  = (num2 ^ this.Gost28147_mainStep(num, workingKey[l]));
                    num2 = num3;
                }
                for (int m = 0; m < 3; m++)
                {
                    int num5 = 7;
                    while (num5 >= 0 && (m != 2 || num5 != 0))
                    {
                        int num3 = num;
                        num  = (num2 ^ this.Gost28147_mainStep(num, workingKey[num5]));
                        num2 = num3;
                        num5--;
                    }
                }
            }
            num2 ^= this.Gost28147_mainStep(num, workingKey[0]);
            Gost28147Engine.intTobytes(num, outBytes, outOff);
            Gost28147Engine.intTobytes(num2, outBytes, outOff + 4);
        }
예제 #3
0
        public static IBufferedCipher GetCipher(
            string algorithm)
        {
            if (algorithm == null)
                throw new ArgumentNullException("algorithm");

            algorithm = Platform.ToUpperInvariant(algorithm);

            {
                string aliased = (string) algorithms[algorithm];

                if (aliased != null)
                    algorithm = aliased;
            }

            IBasicAgreement iesAgreement = null;
            if (algorithm == "IES")
            {
                iesAgreement = new DHBasicAgreement();
            }
            else if (algorithm == "ECIES")
            {
                iesAgreement = new ECDHBasicAgreement();
            }

            if (iesAgreement != null)
            {
                return new BufferedIesCipher(
                    new IesEngine(
                    iesAgreement,
                    new Kdf2BytesGenerator(
                    new Sha1Digest()),
                    new HMac(
                    new Sha1Digest())));
            }



            if (algorithm.StartsWith("PBE"))
            {
                if (algorithm.EndsWith("-CBC"))
                {
                    if (algorithm == "PBEWITHSHA1ANDDES-CBC")
                    {
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new DesEngine()));
                    }
                    else if (algorithm == "PBEWITHSHA1ANDRC2-CBC")
                    {
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new RC2Engine()));
                    }
                    else if (Strings.IsOneOf(algorithm,
                        "PBEWITHSHAAND2-KEYTRIPLEDES-CBC", "PBEWITHSHAAND3-KEYTRIPLEDES-CBC"))
                    {
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new DesEdeEngine()));
                    }
                    else if (Strings.IsOneOf(algorithm,
                        "PBEWITHSHAAND128BITRC2-CBC", "PBEWITHSHAAND40BITRC2-CBC"))
                    {
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new RC2Engine()));
                    }
                }
                else if (algorithm.EndsWith("-BC") || algorithm.EndsWith("-OPENSSL"))
                {
                    if (Strings.IsOneOf(algorithm,
                        "PBEWITHSHAAND128BITAES-CBC-BC",
                        "PBEWITHSHAAND192BITAES-CBC-BC",
                        "PBEWITHSHAAND256BITAES-CBC-BC",
                        "PBEWITHSHA256AND128BITAES-CBC-BC",
                        "PBEWITHSHA256AND192BITAES-CBC-BC",
                        "PBEWITHSHA256AND256BITAES-CBC-BC",
                        "PBEWITHMD5AND128BITAES-CBC-OPENSSL",
                        "PBEWITHMD5AND192BITAES-CBC-OPENSSL",
                        "PBEWITHMD5AND256BITAES-CBC-OPENSSL"))
                    {
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new AesFastEngine()));
                    }
                }
            }



            string[] parts = algorithm.Split('/');

            IBlockCipher blockCipher = null;
            IAsymmetricBlockCipher asymBlockCipher = null;
            IStreamCipher streamCipher = null;

            string algorithmName = parts[0];

            {
                string aliased = (string)algorithms[algorithmName];

                if (aliased != null)
                    algorithmName = aliased;
            }

            CipherAlgorithm cipherAlgorithm;
            try
            {
                cipherAlgorithm = (CipherAlgorithm)Enums.GetEnumValue(typeof(CipherAlgorithm), algorithmName);
            }
            catch (ArgumentException)
            {
                throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
            }

            switch (cipherAlgorithm)
            {
                case CipherAlgorithm.AES:
                    blockCipher = new AesFastEngine();
                    break;
                case CipherAlgorithm.ARC4:
                    streamCipher = new RC4Engine();
                    break;
                case CipherAlgorithm.BLOWFISH:
                    blockCipher = new BlowfishEngine();
                    break;
                case CipherAlgorithm.CAMELLIA:
                    blockCipher = new CamelliaEngine();
                    break;
                case CipherAlgorithm.CAST5:
                    blockCipher = new Cast5Engine();
                    break;
                case CipherAlgorithm.CAST6:
                    blockCipher = new Cast6Engine();
                    break;
                case CipherAlgorithm.DES:
                    blockCipher = new DesEngine();
                    break;
                case CipherAlgorithm.DESEDE:
                    blockCipher = new DesEdeEngine();
                    break;
                case CipherAlgorithm.ELGAMAL:
                    asymBlockCipher = new ElGamalEngine();
                    break;
                case CipherAlgorithm.GOST28147:
                    blockCipher = new Gost28147Engine();
                    break;
                case CipherAlgorithm.HC128:
                    streamCipher = new HC128Engine();
                    break;
                case CipherAlgorithm.HC256:
                    streamCipher = new HC256Engine();
                    break;
                case CipherAlgorithm.IDEA:
                    blockCipher = new IdeaEngine();
                    break;
                case CipherAlgorithm.NOEKEON:
                    blockCipher = new NoekeonEngine();
                    break;
                case CipherAlgorithm.PBEWITHSHAAND128BITRC4:
                case CipherAlgorithm.PBEWITHSHAAND40BITRC4:
                    streamCipher = new RC4Engine();
                    break;
                case CipherAlgorithm.RC2:
                    blockCipher = new RC2Engine();
                    break;
                case CipherAlgorithm.RC5:
                    blockCipher = new RC532Engine();
                    break;
                case CipherAlgorithm.RC5_64:
                    blockCipher = new RC564Engine();
                    break;
                case CipherAlgorithm.RC6:
                    blockCipher = new RC6Engine();
                    break;
                case CipherAlgorithm.RIJNDAEL:
                    blockCipher = new RijndaelEngine();
                    break;
                case CipherAlgorithm.RSA:
                    asymBlockCipher = new RsaBlindedEngine();
                    break;
                case CipherAlgorithm.SALSA20:
                    streamCipher = new Salsa20Engine();
                    break;
                case CipherAlgorithm.SEED:
                    blockCipher = new SeedEngine();
                    break;
                case CipherAlgorithm.SERPENT:
                    blockCipher = new SerpentEngine();
                    break;
                case CipherAlgorithm.SKIPJACK:
                    blockCipher = new SkipjackEngine();
                    break;
                case CipherAlgorithm.TEA:
                    blockCipher = new TeaEngine();
                    break;
                case CipherAlgorithm.TWOFISH:
                    blockCipher = new TwofishEngine();
                    break;
                case CipherAlgorithm.VMPC:
                    streamCipher = new VmpcEngine();
                    break;
                case CipherAlgorithm.VMPC_KSA3:
                    streamCipher = new VmpcKsa3Engine();
                    break;
                case CipherAlgorithm.XTEA:
                    blockCipher = new XteaEngine();
                    break;
                default:
                    throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
            }

            if (streamCipher != null)
            {
                if (parts.Length > 1)
                    throw new ArgumentException("Modes and paddings not used for stream ciphers");

                return new BufferedStreamCipher(streamCipher);
            }


            bool cts = false;
            bool padded = true;
            IBlockCipherPadding padding = null;
            IAeadBlockCipher aeadBlockCipher = null;

            if (parts.Length > 2)
            {
                if (streamCipher != null)
                    throw new ArgumentException("Paddings not used for stream ciphers");

                string paddingName = parts[2];

                CipherPadding cipherPadding;
                if (paddingName == "")
                {
                    cipherPadding = CipherPadding.RAW;
                }
                else if (paddingName == "X9.23PADDING")
                {
                    cipherPadding = CipherPadding.X923PADDING;
                }
                else
                {
                    try
                    {
                        cipherPadding = (CipherPadding)Enums.GetEnumValue(typeof(CipherPadding), paddingName);
                    }
                    catch (ArgumentException)
                    {
                        throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                    }
                }

                switch (cipherPadding)
                {
                    case CipherPadding.NOPADDING:
                        padded = false;
                        break;
                    case CipherPadding.RAW:
                        break;
                    case CipherPadding.ISO10126PADDING:
                    case CipherPadding.ISO10126D2PADDING:
                    case CipherPadding.ISO10126_2PADDING:
                        padding = new ISO10126d2Padding();
                        break;
                    case CipherPadding.ISO7816_4PADDING:
                    case CipherPadding.ISO9797_1PADDING:
                        padding = new ISO7816d4Padding();
                        break;
                    case CipherPadding.ISO9796_1:
                    case CipherPadding.ISO9796_1PADDING:
                        asymBlockCipher = new ISO9796d1Encoding(asymBlockCipher);
                        break;
                    case CipherPadding.OAEP:
                    case CipherPadding.OAEPPADDING:
                        asymBlockCipher = new OaepEncoding(asymBlockCipher);
                        break;
                    case CipherPadding.OAEPWITHMD5ANDMGF1PADDING:
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new MD5Digest());
                        break;
                    case CipherPadding.OAEPWITHSHA1ANDMGF1PADDING:
                    case CipherPadding.OAEPWITHSHA_1ANDMGF1PADDING:
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha1Digest());
                        break;
                    case CipherPadding.OAEPWITHSHA224ANDMGF1PADDING:
                    case CipherPadding.OAEPWITHSHA_224ANDMGF1PADDING:
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha224Digest());
                        break;
                    case CipherPadding.OAEPWITHSHA256ANDMGF1PADDING:
                    case CipherPadding.OAEPWITHSHA_256ANDMGF1PADDING:
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha256Digest());
                        break;
                    case CipherPadding.OAEPWITHSHA384ANDMGF1PADDING:
                    case CipherPadding.OAEPWITHSHA_384ANDMGF1PADDING:
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha384Digest());
                        break;
                    case CipherPadding.OAEPWITHSHA512ANDMGF1PADDING:
                    case CipherPadding.OAEPWITHSHA_512ANDMGF1PADDING:
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha512Digest());
                        break;
                    case CipherPadding.PKCS1:
                    case CipherPadding.PKCS1PADDING:
                        asymBlockCipher = new Pkcs1Encoding(asymBlockCipher);
                        break;
                    case CipherPadding.PKCS5:
                    case CipherPadding.PKCS5PADDING:
                    case CipherPadding.PKCS7:
                    case CipherPadding.PKCS7PADDING:
                        padding = new Pkcs7Padding();
                        break;
                    case CipherPadding.TBCPADDING:
                        padding = new TbcPadding();
                        break;
                    case CipherPadding.WITHCTS:
                        cts = true;
                        break;
                    case CipherPadding.X923PADDING:
                        padding = new X923Padding();
                        break;
                    case CipherPadding.ZEROBYTEPADDING:
                        padding = new ZeroBytePadding();
                        break;
                    default:
                        throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                }
            }

            string mode = "";
            if (parts.Length > 1)
            {
                mode = parts[1];

                int di = GetDigitIndex(mode);
                string modeName = di >= 0 ? mode.Substring(0, di) : mode;

                try
                {
                    CipherMode cipherMode = modeName == ""
                        ? CipherMode.NONE
                        : (CipherMode)Enums.GetEnumValue(typeof(CipherMode), modeName);

                    switch (cipherMode)
                    {
                        case CipherMode.ECB:
                        case CipherMode.NONE:
                            break;
                        case CipherMode.CBC:
                            blockCipher = new CbcBlockCipher(blockCipher);
                            break;
                        case CipherMode.CCM:
                            aeadBlockCipher = new CcmBlockCipher(blockCipher);
                            break;
                        case CipherMode.CFB:
                        {
                            int bits = (di < 0)
                                ?	8 * blockCipher.GetBlockSize()
                                :	int.Parse(mode.Substring(di));
    
                            blockCipher = new CfbBlockCipher(blockCipher, bits);
                            break;
                        }
                        case CipherMode.CTR:
                            blockCipher = new SicBlockCipher(blockCipher);
                            break;
                        case CipherMode.CTS:
                            cts = true;
                            blockCipher = new CbcBlockCipher(blockCipher);
                            break;
                        case CipherMode.EAX:
                            aeadBlockCipher = new EaxBlockCipher(blockCipher);
                            break;
                        case CipherMode.GCM:
                            aeadBlockCipher = new GcmBlockCipher(blockCipher);
                            break;
                        case CipherMode.GOFB:
                            blockCipher = new GOfbBlockCipher(blockCipher);
                            break;
                        case CipherMode.OCB:
                            aeadBlockCipher = new OcbBlockCipher(blockCipher, CreateBlockCipher(cipherAlgorithm));
                            break;
                        case CipherMode.OFB:
                        {
                            int bits = (di < 0)
                                ?	8 * blockCipher.GetBlockSize()
                                :	int.Parse(mode.Substring(di));
    
                            blockCipher = new OfbBlockCipher(blockCipher, bits);
                            break;
                        }
                        case CipherMode.OPENPGPCFB:
                            blockCipher = new OpenPgpCfbBlockCipher(blockCipher);
                            break;
                        case CipherMode.SIC:
                            if (blockCipher.GetBlockSize() < 16)
                            {
                                throw new ArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
                            }
                            blockCipher = new SicBlockCipher(blockCipher);
                            break;
                        default:
                            throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                    }
                }
                catch (ArgumentException)
                {
                    throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                }
            }

            if (aeadBlockCipher != null)
            {
                if (cts)
                    throw new SecurityUtilityException("CTS mode not valid for AEAD ciphers.");
                if (padded && parts.Length > 2 && parts[2] != "")
                    throw new SecurityUtilityException("Bad padding specified for AEAD cipher.");

                return new BufferedAeadBlockCipher(aeadBlockCipher);
            }

            if (blockCipher != null)
            {
                if (cts)
                {
                    return new CtsBlockCipher(blockCipher);
                }

                if (padding != null)
                {
                    return new PaddedBufferedBlockCipher(blockCipher, padding);
                }

                if (!padded || blockCipher.IsPartialBlockOkay)
                {
                    return new BufferedBlockCipher(blockCipher);
                }

                return new PaddedBufferedBlockCipher(blockCipher);
            }

            if (asymBlockCipher != null)
            {
                return new BufferedAsymmetricBlockCipher(asymBlockCipher);
            }

            throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
        }
예제 #4
0
        public static IBufferedCipher GetCipher(
            string algorithm)
        {
            if (algorithm == null)
                throw new ArgumentNullException("algorithm");

            algorithm = algorithm.ToUpper(CultureInfo.InvariantCulture);

            string aliased = (string) algorithms[algorithm];

            if (aliased != null)
                algorithm = aliased;

            IBasicAgreement iesAgreement = null;
            if (algorithm == "IES")
            {
                iesAgreement = new DHBasicAgreement();
            }
            else if (algorithm == "ECIES")
            {
                iesAgreement = new ECDHBasicAgreement();
            }

            if (iesAgreement != null)
            {
                return new BufferedIesCipher(
                    new IesEngine(
                    iesAgreement,
                    new Kdf2BytesGenerator(
                    new Sha1Digest()),
                    new HMac(
                    new Sha1Digest())));
            }

            if (algorithm.StartsWith("PBE"))
            {
                switch (algorithm)
                {
                    case "PBEWITHSHAAND2-KEYTRIPLEDES-CBC":
                    case "PBEWITHSHAAND3-KEYTRIPLEDES-CBC":
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new DesEdeEngine()));

                    case "PBEWITHSHAAND128BITRC2-CBC":
                    case "PBEWITHSHAAND40BITRC2-CBC":
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new RC2Engine()));

                    case "PBEWITHSHAAND128BITAES-CBC-BC":
                    case "PBEWITHSHAAND192BITAES-CBC-BC":
                    case "PBEWITHSHAAND256BITAES-CBC-BC":
                    case "PBEWITHSHA256AND128BITAES-CBC-BC":
                    case "PBEWITHSHA256AND192BITAES-CBC-BC":
                    case "PBEWITHSHA256AND256BITAES-CBC-BC":
                    case "PBEWITHMD5AND128BITAES-CBC-OPENSSL":
                    case "PBEWITHMD5AND192BITAES-CBC-OPENSSL":
                    case "PBEWITHMD5AND256BITAES-CBC-OPENSSL":
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new AesFastEngine()));

                    case "PBEWITHSHA1ANDDES-CBC":
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new DesEngine()));

                    case "PBEWITHSHA1ANDRC2-CBC":
                        return new PaddedBufferedBlockCipher(
                            new CbcBlockCipher(new RC2Engine()));
                }
            }

            string[] parts = algorithm.Split('/');

            IBlockCipher blockCipher = null;
            IAsymmetricBlockCipher asymBlockCipher = null;
            IStreamCipher streamCipher = null;

            switch (parts[0])
            {
                case "AES":
                    blockCipher = new AesFastEngine();
                    break;
                case "ARC4":
                    streamCipher = new RC4Engine();
                    break;
                case "BLOWFISH":
                    blockCipher = new BlowfishEngine();
                    break;
                case "CAMELLIA":
                    blockCipher = new CamelliaEngine();
                    break;
                case "CAST5":
                    blockCipher = new Cast5Engine();
                    break;
                case "CAST6":
                    blockCipher = new Cast6Engine();
                    break;
                case "DES":
                    blockCipher = new DesEngine();
                    break;
                case "DESEDE":
                    blockCipher = new DesEdeEngine();
                    break;
                case "ELGAMAL":
                    asymBlockCipher = new ElGamalEngine();
                    break;
                case "GOST28147":
                    blockCipher = new Gost28147Engine();
                    break;
                case "HC128":
                    streamCipher = new HC128Engine();
                    break;
                case "HC256":
                    streamCipher = new HC256Engine();
                    break;
            #if INCLUDE_IDEA
                case "IDEA":
                    blockCipher = new IdeaEngine();
                    break;
            #endif
                case "NOEKEON":
                    blockCipher = new NoekeonEngine();
                    break;
                case "PBEWITHSHAAND128BITRC4":
                case "PBEWITHSHAAND40BITRC4":
                    streamCipher = new RC4Engine();
                    break;
                case "RC2":
                    blockCipher = new RC2Engine();
                    break;
                case "RC5":
                    blockCipher = new RC532Engine();
                    break;
                case "RC5-64":
                    blockCipher = new RC564Engine();
                    break;
                case "RC6":
                    blockCipher = new RC6Engine();
                    break;
                case "RIJNDAEL":
                    blockCipher = new RijndaelEngine();
                    break;
                case "RSA":
                    asymBlockCipher = new RsaBlindedEngine();
                    break;
                case "SALSA20":
                    streamCipher = new Salsa20Engine();
                    break;
                case "SEED":
                    blockCipher = new SeedEngine();
                    break;
                case "SERPENT":
                    blockCipher = new SerpentEngine();
                    break;
                case "SKIPJACK":
                    blockCipher = new SkipjackEngine();
                    break;
                case "TEA":
                    blockCipher = new TeaEngine();
                    break;
                case "TWOFISH":
                    blockCipher = new TwofishEngine();
                    break;
                case "VMPC":
                    streamCipher = new VmpcEngine();
                    break;
                case "VMPC-KSA3":
                    streamCipher = new VmpcKsa3Engine();
                    break;
                case "XTEA":
                    blockCipher = new XteaEngine();
                    break;
                default:
                    throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
            }

            if (streamCipher != null)
            {
                if (parts.Length > 1)
                    throw new ArgumentException("Modes and paddings not used for stream ciphers");

                return new BufferedStreamCipher(streamCipher);
            }

            bool cts = false;
            bool padded = true;
            IBlockCipherPadding padding = null;
            IAeadBlockCipher aeadBlockCipher = null;

            if (parts.Length > 2)
            {
                if (streamCipher != null)
                    throw new ArgumentException("Paddings not used for stream ciphers");

                switch (parts[2])
                {
                    case "NOPADDING":
                        padded = false;
                        break;
                    case "":
                    case "RAW":
                        break;
                    case "ISO10126PADDING":
                    case "ISO10126D2PADDING":
                    case "ISO10126-2PADDING":
                        padding = new ISO10126d2Padding();
                        break;
                    case "ISO7816-4PADDING":
                    case "ISO9797-1PADDING":
                        padding = new ISO7816d4Padding();
                        break;
                    case "ISO9796-1":
                    case "ISO9796-1PADDING":
                        asymBlockCipher = new ISO9796d1Encoding(asymBlockCipher);
                        break;
                    case "OAEP":
                    case "OAEPPADDING":
                        asymBlockCipher = new OaepEncoding(asymBlockCipher);
                        break;
                    case "OAEPWITHMD5ANDMGF1PADDING":
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new MD5Digest());
                        break;
                    case "OAEPWITHSHA1ANDMGF1PADDING":
                    case "OAEPWITHSHA-1ANDMGF1PADDING":
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha1Digest());
                        break;
                    case "OAEPWITHSHA224ANDMGF1PADDING":
                    case "OAEPWITHSHA-224ANDMGF1PADDING":
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha224Digest());
                        break;
                    case "OAEPWITHSHA256ANDMGF1PADDING":
                    case "OAEPWITHSHA-256ANDMGF1PADDING":
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha256Digest());
                        break;
                    case "OAEPWITHSHA384ANDMGF1PADDING":
                    case "OAEPWITHSHA-384ANDMGF1PADDING":
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha384Digest());
                        break;
                    case "OAEPWITHSHA512ANDMGF1PADDING":
                    case "OAEPWITHSHA-512ANDMGF1PADDING":
                        asymBlockCipher = new OaepEncoding(asymBlockCipher, new Sha512Digest());
                        break;
                    case "PKCS1":
                    case "PKCS1PADDING":
                        asymBlockCipher = new Pkcs1Encoding(asymBlockCipher);
                        break;
                    case "PKCS5":
                    case "PKCS5PADDING":
                    case "PKCS7":
                    case "PKCS7PADDING":
                        padding = new Pkcs7Padding();
                        break;
                    case "TBCPADDING":
                        padding = new TbcPadding();
                        break;
                    case "WITHCTS":
                        cts = true;
                        break;
                    case "X9.23PADDING":
                    case "X923PADDING":
                        padding = new X923Padding();
                        break;
                    case "ZEROBYTEPADDING":
                        padding = new ZeroBytePadding();
                        break;
                    default:
                        throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                }
            }

            string mode = "";
            if (parts.Length > 1)
            {
                mode = parts[1];

                int di = GetDigitIndex(mode);
                string modeName = di >= 0 ? mode.Substring(0, di) : mode;

                switch (modeName)
                {
                    case "":
                    case "ECB":
                    case "NONE":
                        break;
                    case "CBC":
                        blockCipher = new CbcBlockCipher(blockCipher);
                        break;
                    case "CCM":
                        aeadBlockCipher = new CcmBlockCipher(blockCipher);
                        break;
                    case "CFB":
                    {
                        int bits = (di < 0)
                            ?	8 * blockCipher.GetBlockSize()
                            :	int.Parse(mode.Substring(di));

                        blockCipher = new CfbBlockCipher(blockCipher, bits);
                        break;
                    }
                    case "CTR":
                        blockCipher = new SicBlockCipher(blockCipher);
                        break;
                    case "CTS":
                        cts = true;
                        blockCipher = new CbcBlockCipher(blockCipher);
                        break;
                    case "EAX":
                        aeadBlockCipher = new EaxBlockCipher(blockCipher);
                        break;
                    case "GCM":
                        aeadBlockCipher = new GcmBlockCipher(blockCipher);
                        break;
                    case "GOFB":
                        blockCipher = new GOfbBlockCipher(blockCipher);
                        break;
                    case "OFB":
                    {
                        int bits = (di < 0)
                            ?	8 * blockCipher.GetBlockSize()
                            :	int.Parse(mode.Substring(di));

                        blockCipher = new OfbBlockCipher(blockCipher, bits);
                        break;
                    }
                    case "OPENPGPCFB":
                        blockCipher = new OpenPgpCfbBlockCipher(blockCipher);
                        break;
                    case "SIC":
                        if (blockCipher.GetBlockSize() < 16)
                        {
                            throw new ArgumentException("Warning: SIC-Mode can become a twotime-pad if the blocksize of the cipher is too small. Use a cipher with a block size of at least 128 bits (e.g. AES)");
                        }
                        blockCipher = new SicBlockCipher(blockCipher);
                        break;
                    default:
                        throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
                }
            }

            if (aeadBlockCipher != null)
            {
                if (cts)
                    throw new SecurityUtilityException("CTS mode not valid for AEAD ciphers.");
                if (padded && parts.Length > 2 && parts[2] != "")
                    throw new SecurityUtilityException("Bad padding specified for AEAD cipher.");

                return new BufferedAeadBlockCipher(aeadBlockCipher);
            }

            if (blockCipher != null)
            {
                if (cts)
                {
                    return new CtsBlockCipher(blockCipher);
                }

                if (padding != null)
                {
                    return new PaddedBufferedBlockCipher(blockCipher, padding);
                }

                if (!padded || blockCipher.IsPartialBlockOkay)
                {
                    return new BufferedBlockCipher(blockCipher);
                }

                return new PaddedBufferedBlockCipher(blockCipher);
            }

            if (asymBlockCipher != null)
            {
                return new BufferedAsymmetricBlockCipher(asymBlockCipher);
            }

            throw new SecurityUtilityException("Cipher " + algorithm + " not recognised.");
        }
예제 #5
0
 static Gost28147Engine()
 {
     Gost28147Engine.Sbox_Default = new byte[]
     {
         4,
         10,
         9,
         2,
         13,
         8,
         0,
         14,
         6,
         11,
         1,
         12,
         7,
         15,
         5,
         3,
         14,
         11,
         4,
         12,
         6,
         13,
         15,
         10,
         2,
         3,
         8,
         1,
         0,
         7,
         5,
         9,
         5,
         8,
         1,
         13,
         10,
         3,
         4,
         2,
         14,
         15,
         12,
         7,
         6,
         0,
         9,
         11,
         7,
         13,
         10,
         1,
         0,
         8,
         9,
         15,
         14,
         4,
         6,
         12,
         11,
         2,
         5,
         3,
         6,
         12,
         7,
         1,
         5,
         15,
         13,
         8,
         4,
         10,
         9,
         14,
         0,
         3,
         11,
         2,
         4,
         11,
         10,
         0,
         7,
         2,
         1,
         13,
         3,
         6,
         8,
         5,
         9,
         12,
         15,
         14,
         13,
         11,
         4,
         1,
         3,
         15,
         5,
         9,
         0,
         10,
         14,
         7,
         6,
         8,
         2,
         12,
         1,
         15,
         13,
         0,
         5,
         7,
         10,
         4,
         9,
         2,
         3,
         14,
         6,
         11,
         8,
         12
     };
     Gost28147Engine.ESbox_Test = new byte[]
     {
         4,
         2,
         15,
         5,
         9,
         1,
         0,
         8,
         14,
         3,
         11,
         12,
         13,
         7,
         10,
         6,
         12,
         9,
         15,
         14,
         8,
         1,
         3,
         10,
         2,
         7,
         4,
         13,
         6,
         0,
         11,
         5,
         13,
         8,
         14,
         12,
         7,
         3,
         9,
         10,
         1,
         5,
         2,
         4,
         6,
         15,
         0,
         11,
         14,
         9,
         11,
         2,
         5,
         15,
         7,
         1,
         0,
         13,
         12,
         6,
         10,
         4,
         3,
         8,
         3,
         14,
         5,
         9,
         6,
         8,
         0,
         13,
         10,
         11,
         7,
         12,
         2,
         1,
         15,
         4,
         8,
         15,
         6,
         11,
         1,
         9,
         12,
         5,
         13,
         3,
         7,
         10,
         0,
         14,
         2,
         4,
         9,
         11,
         12,
         0,
         3,
         6,
         7,
         5,
         4,
         8,
         14,
         15,
         1,
         10,
         2,
         13,
         12,
         6,
         5,
         2,
         11,
         0,
         9,
         13,
         3,
         14,
         7,
         10,
         15,
         4,
         1,
         8
     };
     Gost28147Engine.ESbox_A = new byte[]
     {
         9,
         6,
         3,
         2,
         8,
         11,
         1,
         7,
         10,
         4,
         14,
         15,
         12,
         0,
         13,
         5,
         3,
         7,
         14,
         9,
         8,
         10,
         15,
         0,
         5,
         2,
         6,
         12,
         11,
         4,
         13,
         1,
         14,
         4,
         6,
         2,
         11,
         3,
         13,
         8,
         12,
         15,
         5,
         10,
         0,
         7,
         1,
         9,
         14,
         7,
         10,
         12,
         13,
         1,
         3,
         9,
         0,
         2,
         11,
         4,
         15,
         8,
         5,
         6,
         11,
         5,
         1,
         9,
         8,
         13,
         15,
         0,
         14,
         4,
         2,
         3,
         12,
         7,
         10,
         6,
         3,
         10,
         13,
         12,
         1,
         2,
         0,
         11,
         7,
         5,
         9,
         4,
         8,
         15,
         14,
         6,
         1,
         13,
         2,
         9,
         7,
         10,
         6,
         0,
         8,
         12,
         4,
         5,
         15,
         3,
         11,
         14,
         11,
         10,
         15,
         5,
         0,
         12,
         14,
         8,
         6,
         2,
         3,
         9,
         1,
         7,
         13,
         4
     };
     Gost28147Engine.ESbox_B = new byte[]
     {
         8,
         4,
         11,
         1,
         3,
         5,
         0,
         9,
         2,
         14,
         10,
         12,
         13,
         6,
         7,
         15,
         0,
         1,
         2,
         10,
         4,
         13,
         5,
         12,
         9,
         7,
         3,
         15,
         11,
         8,
         6,
         14,
         14,
         12,
         0,
         10,
         9,
         2,
         13,
         11,
         7,
         5,
         8,
         15,
         3,
         6,
         1,
         4,
         7,
         5,
         0,
         13,
         11,
         6,
         1,
         2,
         3,
         10,
         12,
         15,
         4,
         14,
         9,
         8,
         2,
         7,
         12,
         15,
         9,
         5,
         10,
         11,
         1,
         4,
         0,
         13,
         6,
         8,
         14,
         3,
         8,
         3,
         2,
         6,
         4,
         13,
         14,
         11,
         12,
         1,
         7,
         15,
         10,
         0,
         9,
         5,
         5,
         2,
         10,
         11,
         9,
         1,
         12,
         3,
         7,
         4,
         13,
         0,
         6,
         15,
         8,
         14,
         0,
         4,
         11,
         14,
         8,
         3,
         7,
         1,
         10,
         2,
         9,
         6,
         15,
         13,
         5,
         12
     };
     Gost28147Engine.ESbox_C = new byte[]
     {
         1,
         11,
         12,
         2,
         9,
         13,
         0,
         15,
         4,
         5,
         8,
         14,
         10,
         7,
         6,
         3,
         0,
         1,
         7,
         13,
         11,
         4,
         5,
         2,
         8,
         14,
         15,
         12,
         9,
         10,
         6,
         3,
         8,
         2,
         5,
         0,
         4,
         9,
         15,
         10,
         3,
         7,
         12,
         13,
         6,
         14,
         1,
         11,
         3,
         6,
         0,
         1,
         5,
         13,
         10,
         8,
         11,
         2,
         9,
         7,
         14,
         15,
         12,
         4,
         8,
         13,
         11,
         0,
         4,
         5,
         1,
         2,
         9,
         3,
         12,
         14,
         6,
         15,
         10,
         7,
         12,
         9,
         11,
         1,
         8,
         14,
         2,
         4,
         7,
         3,
         6,
         5,
         10,
         0,
         15,
         13,
         10,
         9,
         6,
         8,
         13,
         14,
         2,
         0,
         15,
         3,
         5,
         11,
         4,
         1,
         12,
         7,
         7,
         4,
         0,
         5,
         10,
         2,
         15,
         14,
         12,
         6,
         1,
         11,
         13,
         9,
         3,
         8
     };
     Gost28147Engine.ESbox_D = new byte[]
     {
         15,
         12,
         2,
         10,
         6,
         4,
         5,
         0,
         7,
         9,
         14,
         13,
         1,
         11,
         8,
         3,
         11,
         6,
         3,
         4,
         12,
         15,
         14,
         2,
         7,
         13,
         8,
         0,
         5,
         10,
         9,
         1,
         1,
         12,
         11,
         0,
         15,
         14,
         6,
         5,
         10,
         13,
         4,
         8,
         9,
         3,
         7,
         2,
         1,
         5,
         14,
         12,
         10,
         7,
         0,
         13,
         6,
         2,
         11,
         4,
         9,
         3,
         15,
         8,
         0,
         12,
         8,
         9,
         13,
         2,
         10,
         11,
         7,
         3,
         6,
         5,
         4,
         14,
         15,
         1,
         8,
         0,
         15,
         3,
         2,
         5,
         14,
         11,
         1,
         10,
         4,
         7,
         12,
         9,
         13,
         6,
         3,
         0,
         6,
         15,
         1,
         14,
         9,
         2,
         13,
         8,
         12,
         4,
         11,
         10,
         5,
         7,
         1,
         10,
         6,
         8,
         15,
         11,
         0,
         4,
         12,
         3,
         5,
         9,
         7,
         13,
         2,
         14
     };
     Gost28147Engine.DSbox_Test = new byte[]
     {
         4,
         10,
         9,
         2,
         13,
         8,
         0,
         14,
         6,
         11,
         1,
         12,
         7,
         15,
         5,
         3,
         14,
         11,
         4,
         12,
         6,
         13,
         15,
         10,
         2,
         3,
         8,
         1,
         0,
         7,
         5,
         9,
         5,
         8,
         1,
         13,
         10,
         3,
         4,
         2,
         14,
         15,
         12,
         7,
         6,
         0,
         9,
         11,
         7,
         13,
         10,
         1,
         0,
         8,
         9,
         15,
         14,
         4,
         6,
         12,
         11,
         2,
         5,
         3,
         6,
         12,
         7,
         1,
         5,
         15,
         13,
         8,
         4,
         10,
         9,
         14,
         0,
         3,
         11,
         2,
         4,
         11,
         10,
         0,
         7,
         2,
         1,
         13,
         3,
         6,
         8,
         5,
         9,
         12,
         15,
         14,
         13,
         11,
         4,
         1,
         3,
         15,
         5,
         9,
         0,
         10,
         14,
         7,
         6,
         8,
         2,
         12,
         1,
         15,
         13,
         0,
         5,
         7,
         10,
         4,
         9,
         2,
         3,
         14,
         6,
         11,
         8,
         12
     };
     Gost28147Engine.DSbox_A = new byte[]
     {
         10,
         4,
         5,
         6,
         8,
         1,
         3,
         7,
         13,
         12,
         14,
         0,
         9,
         2,
         11,
         15,
         5,
         15,
         4,
         0,
         2,
         13,
         11,
         9,
         1,
         7,
         6,
         3,
         12,
         14,
         10,
         8,
         7,
         15,
         12,
         14,
         9,
         4,
         1,
         0,
         3,
         11,
         5,
         2,
         6,
         10,
         8,
         13,
         4,
         10,
         7,
         12,
         0,
         15,
         2,
         8,
         14,
         1,
         6,
         5,
         13,
         11,
         9,
         3,
         7,
         6,
         4,
         11,
         9,
         12,
         2,
         10,
         1,
         8,
         0,
         14,
         15,
         13,
         3,
         5,
         7,
         6,
         2,
         4,
         13,
         9,
         15,
         0,
         10,
         1,
         5,
         11,
         8,
         14,
         12,
         3,
         13,
         14,
         4,
         1,
         7,
         0,
         5,
         10,
         3,
         12,
         8,
         15,
         6,
         2,
         9,
         11,
         1,
         3,
         10,
         9,
         5,
         11,
         4,
         15,
         8,
         6,
         7,
         14,
         13,
         0,
         2,
         12
     };
     Gost28147Engine.sBoxes = Platform.CreateHashtable();
     Gost28147Engine.AddSBox("Default", Gost28147Engine.Sbox_Default);
     Gost28147Engine.AddSBox("E-TEST", Gost28147Engine.ESbox_Test);
     Gost28147Engine.AddSBox("E-A", Gost28147Engine.ESbox_A);
     Gost28147Engine.AddSBox("E-B", Gost28147Engine.ESbox_B);
     Gost28147Engine.AddSBox("E-C", Gost28147Engine.ESbox_C);
     Gost28147Engine.AddSBox("E-D", Gost28147Engine.ESbox_D);
     Gost28147Engine.AddSBox("D-TEST", Gost28147Engine.DSbox_Test);
     Gost28147Engine.AddSBox("D-A", Gost28147Engine.DSbox_A);
 }
예제 #6
0
파일: AWiz.cs 프로젝트: burstas/rmps
        private BufferedBlockCipher CreateScryptoEngine()
        {
            IBlockCipher engine;
            switch (_MSec.Algorithm)
            {
                case ESec.SCRYPTO_AES:
                    engine = new AesEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_AESFAST:
                    engine = new AesFastEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_AESLIGHT:
                    engine = new AesLightEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_BLOWFISH:
                    engine = new BlowfishEngine();
                    _MSec.KeySize = 56;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAMELLIA:
                    engine = new CamelliaEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAMELLIALIGHT:
                    engine = new CamelliaLightEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAST5:
                    engine = new Cast5Engine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAST6:
                    engine = new Cast6Engine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_DES:
                    engine = new DesEngine();
                    _MSec.KeySize = 8;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_DESEDE:
                    engine = new DesEdeEngine();
                    _MSec.KeySize = 24;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_GOST28147:
                    engine = new Gost28147Engine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_NOEKEON:
                    engine = new NoekeonEngine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_NULL:
                    engine = new NullEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 16;
                    break;
                case ESec.SCRYPTO_RC2:
                    engine = new RC2Engine();
                    _MSec.KeySize = 128;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_RC532:
                    engine = new RC532Engine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_RC564:
                    engine = new RC564Engine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_RC6:
                    engine = new RC6Engine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_RIJNDAEL:
                    engine = new RijndaelEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_SEED:
                    engine = new SeedEngine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_SERPENT:
                    engine = new SerpentEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_SKIPJACK:
                    engine = new SkipjackEngine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_TEA:
                    engine = new TeaEngine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_TWOFISH:
                    engine = new TwofishEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
                case ESec.SCRYPTO_XTEA:
                    engine = new XteaEngine();
                    _MSec.KeySize = 16;
                    _MSec.IVSize = 0;
                    break;
                default:
                    engine = new AesEngine();
                    _MSec.KeySize = 32;
                    _MSec.IVSize = 0;
                    break;
            }

            switch (_MSec.Mode)
            {
                case ESec.MODE_CBC:
                    engine = new CbcBlockCipher(engine);
                    break;
                case ESec.MODE_CFB:
                    engine = new CfbBlockCipher(engine, 8);
                    break;
                case ESec.MODE_GOFB:
                    engine = new GOfbBlockCipher(engine);
                    break;
                case ESec.MODE_OFB:
                    engine = new OfbBlockCipher(engine, 8);
                    break;
                case ESec.MODE_OPENPGPCFB:
                    engine = new OpenPgpCfbBlockCipher(engine);
                    break;
                case ESec.MODE_SIC:
                    engine = new SicBlockCipher(engine);
                    break;
                default:
                    engine = new CbcBlockCipher(engine);
                    break;
            }

            IBlockCipherPadding padding = null;
            switch (_MSec.Padding)
            {
                case ESec.PADDING_ISO10126d2:
                    padding = new ISO10126d2Padding();
                    break;
                case ESec.PADDING_ISO7816d4:
                    padding = new ISO7816d4Padding();
                    break;
                case ESec.PADDING_PKCS7:
                    padding = new Pkcs7Padding();
                    break;
                case ESec.PADDING_TBC:
                    padding = new TbcPadding();
                    break;
                case ESec.PADDING_X923:
                    padding = new X923Padding();
                    break;
                case ESec.PADDING_ZEROBYTE:
                    padding = new ZeroBytePadding();
                    break;
                default:
                    padding = new Pkcs7Padding();
                    break;
            }

            return new PaddedBufferedBlockCipher(engine, padding);
        }