Exemple #1
0
        public bool Verify(byte[] signature, byte[] data, uint160 nonce)
        {
            byte[]       output    = new byte[256];
            var          msg       = Utils.Combine(nonce.ToBytes(), data);
            Sha512Digest sha512    = new Sha512Digest();
            var          generator = new Mgf1BytesGenerator(sha512);

            generator.Init(new MgfParameters(msg));
            generator.GenerateBytes(output, 0, output.Length);
            var input = new BigInteger(1, output);

            if (input.CompareTo(_Key.Modulus) >= 0)
            {
                return(false);
            }
            if (signature.Length > 256)
            {
                return(false);
            }
            var signatureInt = new BigInteger(1, signature);

            if (signatureInt.CompareTo(_Key.Modulus) >= 0)
            {
                return(false);
            }
            var engine = new RsaBlindedEngine();

            engine.Init(false, _Key);
            return(input.Equals(engine.ProcessBlock(signatureInt)));
        }
Exemple #2
0
        private void checkForPkcs1Exception(RsaKeyParameters pubParameters, RsaKeyParameters privParameters, byte[] inputData, string expectedMessage)
        {
            IAsymmetricBlockCipher eng = new RsaBlindedEngine();

            eng.Init(true, privParameters);

            byte[] data = null;

            try
            {
                data = eng.ProcessBlock(inputData, 0, inputData.Length);
            }
            catch (Exception e)
            {
                Fail("RSA: failed - exception " + e.ToString(), e);
            }

            eng = new Pkcs1Encoding(eng);

            eng.Init(false, pubParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);

                Fail("missing data block not recognised");
            }
            catch (InvalidCipherTextException e)
            {
                if (!e.Message.Equals(expectedMessage))
                {
                    Fail("RSA: failed - exception " + e.ToString(), e);
                }
            }
        }
Exemple #3
0
        private void doTestStrictPkcs1Length(RsaKeyParameters pubParameters, RsaKeyParameters privParameters)
        {
            IAsymmetricBlockCipher eng = new RsaBlindedEngine();

            eng.Init(true, privParameters);

            byte[] data = null;

            try
            {
                data = eng.ProcessBlock(oversizedSig, 0, oversizedSig.Length);
            }
            catch (Exception e)
            {
                Fail("RSA: failed - exception " + e.ToString(), e);
            }

            eng = new Pkcs1Encoding(eng);

            eng.Init(false, pubParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);

                Fail("oversized signature block not recognised");
            }
            catch (InvalidCipherTextException e)
            {
                if (!e.Message.Equals("block incorrect size"))
                {
                    Fail("RSA: failed - exception " + e.ToString(), e);
                }
            }


            // Create the encoding with StrictLengthEnabled=false (done thru environment in Java version)
            Pkcs1Encoding.StrictLengthEnabled = false;

            eng = new Pkcs1Encoding(new RsaBlindedEngine());

            eng.Init(false, pubParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (InvalidCipherTextException e)
            {
                Fail("RSA: failed - exception " + e.ToString(), e);
            }

            Pkcs1Encoding.StrictLengthEnabled = true;
        }
        public static byte[] RsaProcessMessage(
            bool forEncryption,
            byte[] message,
            AsymmetricKeyParameter key)
        {
            IAsymmetricBlockCipher cipher = new RsaBlindedEngine();

            cipher.Init(forEncryption, key);

            return(cipher.ProcessBlock(message, 0, message.Length));
        }
Exemple #5
0
            public bool HasTestPassed(AsymmetricCipherKeyPair kp)
            {
                byte[] data = Hex.Decode("576a1f885e3420128c8a656097ba7d8bb4c6f1b1853348cf2ba976971dbdbefc");

                RsaBlindedEngine rsaEngine = new RsaBlindedEngine();

                rsaEngine.Init(true, kp.Public);

                byte[] encrypted = rsaEngine.ProcessBlock(data, 0, data.Length);

                if (Arrays.AreEqual(data, encrypted))
                {
                    return(false);
                }

                rsaEngine.Init(false, new ParametersWithRandom(kp.Private, Utils.testRandom));

                byte[] decrypted = rsaEngine.ProcessBlock(encrypted, 0, encrypted.Length);

                return(Arrays.AreEqual(FipsKats.Values[FipsKats.Vec.RsaKeyPairConsistencyCheck], data));
            }
Exemple #6
0
        internal BigInteger Decrypt(BigInteger encrypted)
        {
            if (encrypted == null)
            {
                throw new ArgumentNullException(nameof(encrypted));
            }
            if (encrypted.CompareTo(_Key.Modulus) >= 0)
            {
                throw new DataLengthException("input too large for RSA cipher.");
            }

            RsaBlindedEngine engine = new RsaBlindedEngine();

            engine.Init(false, _Key);
            return(engine.ProcessBlock(encrypted));
        }
Exemple #7
0
        internal BigInteger Encrypt(BigInteger data)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (data.CompareTo(_Key.Modulus) >= 0)
            {
                throw new ArgumentException("input too large for RSA cipher.");
            }

            RsaBlindedEngine engine = new RsaBlindedEngine();

            engine.Init(true, _Key);
            return(engine.ProcessBlock(data));
        }
Exemple #8
0
        public byte[] Sign(byte[] data, out uint160 nonce)
        {
            while (true)
            {
                byte[] output = new byte[256];
                nonce = new uint160(RandomUtils.GetBytes(20));
                Sha512Digest sha512    = new Sha512Digest();
                var          msg       = Utils.Combine(nonce.ToBytes(), data);
                var          generator = new Mgf1BytesGenerator(sha512);
                generator.Init(new MgfParameters(msg));
                generator.GenerateBytes(output, 0, output.Length);
                var input = new BigInteger(1, output);
                if (input.CompareTo(_Key.Modulus) >= 0)
                {
                    continue;
                }
                var engine = new RsaBlindedEngine();
                engine.Init(true, _Key);

                return(engine.ConvertOutput(engine.ProcessBlock(input)));
            }
        }
Exemple #9
0
        public override void PerformTest()
        {
            RsaKeyParameters pubParameters  = new RsaKeyParameters(false, mod, pubExp);
            RsaKeyParameters privParameters = new RsaPrivateCrtKeyParameters(mod, pubExp, privExp, p, q, pExp, qExp, crtCoef);

            byte[] data = Hex.Decode(edgeInput);

            //
            // RAW
            //
            IAsymmetricBlockCipher eng = new RsaBlindedEngine();

            eng.Init(true, pubParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("RSA: failed - exception " + e.ToString(), e);
            }

            eng.Init(false, privParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            if (!edgeInput.Equals(Hex.ToHexString(data)))
            {
                Fail("failed RAW edge Test");
            }

            data = Hex.Decode(input);

            eng.Init(true, pubParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            eng.Init(false, privParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            if (!input.Equals(Hex.ToHexString(data)))
            {
                Fail("failed RAW Test");
            }

            //
            // PKCS1 - public encrypt, private decrypt
            //
            eng = new Pkcs1Encoding(eng);

            eng.Init(true, pubParameters);

            if (eng.GetOutputBlockSize() != ((Pkcs1Encoding)eng).GetUnderlyingCipher().GetOutputBlockSize())
            {
                Fail("PKCS1 output block size incorrect");
            }

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            eng.Init(false, privParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            if (!input.Equals(Hex.ToHexString(data)))
            {
                Fail("failed PKCS1 public/private Test");
            }

            //
            // PKCS1 - private encrypt, public decrypt
            //
            eng = new Pkcs1Encoding(((Pkcs1Encoding)eng).GetUnderlyingCipher());

            eng.Init(true, privParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            eng.Init(false, pubParameters);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            if (!input.Equals(Hex.ToHexString(data)))
            {
                Fail("failed PKCS1 private/public Test");
            }

            //
            // key generation test
            //
            RsaKeyPairGenerator        pGen     = new RsaKeyPairGenerator();
            RsaKeyGenerationParameters genParam = new RsaKeyGenerationParameters(
                BigInteger.ValueOf(0x11), new SecureRandom(), 768, 25);

            pGen.Init(genParam);

            AsymmetricCipherKeyPair pair = pGen.GenerateKeyPair();

            eng = new RsaBlindedEngine();

            if (((RsaKeyParameters)pair.Public).Modulus.BitLength < 768)
            {
                Fail("failed key generation (768) length test");
            }

            eng.Init(true, pair.Public);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            eng.Init(false, pair.Private);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            if (!input.Equals(Hex.ToHexString(data)))
            {
                Fail("failed key generation (768) Test");
            }

            genParam = new RsaKeyGenerationParameters(BigInteger.ValueOf(0x11), new SecureRandom(), 1024, 25);

            pGen.Init(genParam);
            pair = pGen.GenerateKeyPair();

            eng.Init(true, pair.Public);

            if (((RsaKeyParameters)pair.Public).Modulus.BitLength < 1024)
            {
                Fail("failed key generation (1024) length test");
            }

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            eng.Init(false, pair.Private);

            try
            {
                data = eng.ProcessBlock(data, 0, data.Length);
            }
            catch (Exception e)
            {
                Fail("failed - exception " + e.ToString(), e);
            }

            if (!input.Equals(Hex.ToHexString(data)))
            {
                Fail("failed key generation (1024) test");
            }

            doTestOaep(pubParameters, privParameters);
            doTestStrictPkcs1Length(pubParameters, privParameters);
            doTestDudPkcs1Block(pubParameters, privParameters);
            doTestMissingDataPkcs1Block(pubParameters, privParameters);
            doTestTruncatedPkcs1Block(pubParameters, privParameters);
            doTestWrongPaddingPkcs1Block(pubParameters, privParameters);

            try
            {
                new RsaBlindedEngine().ProcessBlock(new byte[] { 1 }, 0, 1);
                Fail("failed initialisation check");
            }
            catch (InvalidOperationException)
            {
                // expected
            }
        }