Ejemplo n.º 1
0
        public DSACryptoServiceProvider(bool genKey)
        {
            IntPtr prov = IntPtr.Zero;
            IntPtr key  = IntPtr.Zero;

            //for bad key state
            //Crypto.CryptAcquireContext(out prov, "dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, (uint)ProvType.DSS_DH, (uint)ContextFlag.DELETEKEYSET);
            //Crypto.CryptAcquireContext(out prov, "dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, (uint)ProvType.DSS_DH, (uint)ContextFlag.NEWKEYSET);
            try
            {
                if (genKey == false)                //faster
                {
                    prov = Context.AcquireContext("dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE);
                    key  = Key.GetUserKey(prov, KeySpec.SIGNATURE);
                }
                else                 //create new one
                {
                    prov = Context.AcquireContext("dSaContainerImp", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE);
                    key  = Key.GenKey(prov, Calg.DSS_SIGN, GenKeyParam.EXPORTABLE);
                }
                byte [] baPriKey = Key.ExportKey(key, IntPtr.Zero, KeyBlob.PRIVATEKEYBLOB);
                byte [] baPubKey = Key.ExportKey(key, IntPtr.Zero, KeyBlob.PUBLICKEYBLOB);
                _dsa = new Dsa(baPriKey);                 //X is returned
                Dsa dsaPub = new Dsa(baPubKey);           //Y is only from public call
                _dsa.Y = (byte[])dsaPub.Y.Clone();
            }
            finally
            {
                Key.DestroyKey(key);
                Context.ReleaseContext(prov);
            }
        }
		public DSACryptoServiceProvider(bool genKey)
		{
			IntPtr prov = IntPtr.Zero;
			IntPtr key = IntPtr.Zero;
			//for bad key state
			//Crypto.CryptAcquireContext(out prov, "dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, (uint)ProvType.DSS_DH, (uint)ContextFlag.DELETEKEYSET);
			//Crypto.CryptAcquireContext(out prov, "dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, (uint)ProvType.DSS_DH, (uint)ContextFlag.NEWKEYSET);
			try
			{
				if(genKey == false) //faster
				{
					prov = Context.AcquireContext("dSaContainer", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE);
					key = Key.GetUserKey(prov, KeySpec.SIGNATURE);
				}
				else //create new one
				{
					prov = Context.AcquireContext("dSaContainerImp", ProvName.MS_ENH_DSS_DH_PROV, ProvType.DSS_DH, ContextFlag.NONE);
					key = Key.GenKey(prov, Calg.DSS_SIGN, GenKeyParam.EXPORTABLE);

				}
				byte [] baPriKey = Key.ExportKey(key, IntPtr.Zero, KeyBlob.PRIVATEKEYBLOB);
				byte [] baPubKey = Key.ExportKey(key, IntPtr.Zero, KeyBlob.PUBLICKEYBLOB);
				_dsa = new Dsa(baPriKey); //X is returned
				Dsa dsaPub = new Dsa(baPubKey); //Y is only from public call
				_dsa.Y = (byte[])dsaPub.Y.Clone();
			}
			finally
			{
				Key.DestroyKey(key);
				Context.ReleaseContext(prov);
			}
		}
Ejemplo n.º 3
0
        public Form1()
        {
            InitializeComponent();

            dsa = new Dsa();
            dsa.GenerateKey();

            InitUI();
        }
Ejemplo n.º 4
0
        protected override void GenerateKasKeyNonceInformation()
        {
            if (DomainParameters == null)
            {
                GenerateDomainParameters();
            }

            StaticKeyPair = Dsa.GenerateKeyPair(DomainParameters).KeyPair;

            // DKM Nonce required when party U and KdfNoKc or KdfKc
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.InitiatorPartyU &&
                SchemeParameters.KasMode != KasMode.NoKdfNoKc)
            {
                try
                {
                    DkmNonce = EntropyProvider.GetEntropy(new BitString(DomainParameters.Q).BitLength / 2);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e);
                    throw;
                }
            }

            // When party V, KC, Bilateral, generate ephemeral nonce
            // When party V, KC, Unilateral, and the recipient of key confirmation, ephemeral nonce
            // Otherwise, no ephemeral nonce.
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.ResponderPartyV &&
                SchemeParameters.KasMode == KasMode.KdfKc)
            {
                if (SchemeParameters.KeyConfirmationDirection == KeyConfirmationDirection.Bilateral ||
                    (
                        SchemeParameters.KeyConfirmationDirection == KeyConfirmationDirection.Unilateral &&
                        SchemeParameters.KeyConfirmationRole == KeyConfirmationRole.Recipient
                    )
                    )
                {
                    EphemeralNonce = EntropyProvider.GetEntropy(new BitString(DomainParameters.P).BitLength);
                }
            }

            // when party U and KdfNoKc, a NoKeyConfirmationNonce is needed.
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.InitiatorPartyU &&
                SchemeParameters.KasMode == KasMode.KdfNoKc)
            {
                NoKeyConfirmationNonce = EntropyProvider.GetEntropy(128);
            }
        }
        protected override void GenerateKasKeyNonceInformation()
        {
            if (DomainParameters == null)
            {
                GenerateDomainParameters();
            }

            EphemeralKeyPair = Dsa.GenerateKeyPair(DomainParameters).KeyPair;

            // when party U and KdfNoKc, a NoKeyConfirmationNonce is needed.
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.InitiatorPartyU &&
                SchemeParameters.KasMode == KasMode.KdfNoKc)
            {
                NoKeyConfirmationNonce = EntropyProvider.GetEntropy(128);
            }
        }
Ejemplo n.º 6
0
        public void DsaTest()
        {
            string publicKey;
            string privateKey;
            string testData;

            byte[] bytes;
            byte[] sign;

            testData = "Hello World! ppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppppppppppppppppppppppp" +
                       "pppppppppppppppppppppp";
            Dsa dsa = new Dsa();

            (publicKey, privateKey) = dsa.CreateKey();

            bytes = Encoding.UTF8.GetBytes(testData);

            sign = dsa.Sign(bytes, privateKey);
            if (sign.Equals(testData))
            {
                Assert.Fail("署名に失敗");
            }

            if (!dsa.Verify(bytes, sign, publicKey))
            {
                Assert.Fail("検証に失敗");
            }

            bytes[0] = 100;
            bytes[1] = 200;
            if (dsa.Verify(bytes, sign, publicKey))
            {
                Assert.Fail("改ざんされているのに検証に成功");
            }
        }
        protected override void GenerateKasKeyNonceInformation()
        {
            if (DomainParameters == null)
            {
                GenerateDomainParameters();
            }

            StaticKeyPair = Dsa.GenerateKeyPair(DomainParameters).KeyPair;

            var curveAttributes = CurveAttributesHelper.GetCurveAttribute(DomainParameters.CurveE.CurveName);

            // DKM Nonce required when party U and KdfNoKc/KdfKc
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.InitiatorPartyU &&
                SchemeParameters.KasMode != KasMode.NoKdfNoKc)
            {
                DkmNonce = EntropyProvider.GetEntropy(curveAttributes.DegreeOfPolynomial.ValueToMod(BitString.BITSINBYTE));
            }

            // When party V, KC, Bilateral, generate ephemeral nonce
            // When party V, KC, Unilateral, and the recipient of key confirmation, ephemeral nonce
            // Otherwise, no ephemeral nonce.
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.ResponderPartyV &&
                SchemeParameters.KasMode == KasMode.KdfKc)
            {
                if (SchemeParameters.KeyConfirmationDirection == KeyConfirmationDirection.Bilateral ||
                    (
                        SchemeParameters.KeyConfirmationDirection == KeyConfirmationDirection.Unilateral &&
                        SchemeParameters.KeyConfirmationRole == KeyConfirmationRole.Recipient
                    )
                    )
                {
                    EphemeralNonce = EntropyProvider.GetEntropy(curveAttributes.DegreeOfPolynomial.ValueToMod(BitString.BITSINBYTE));
                }
            }

            // when party U and KdfNoKc, a NoKeyConfirmationNonce is needed.
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.InitiatorPartyU &&
                SchemeParameters.KasMode == KasMode.KdfNoKc)
            {
                NoKeyConfirmationNonce = EntropyProvider.GetEntropy(128);
            }
        }
Ejemplo n.º 8
0
        protected override void GenerateKasKeyNonceInformation()
        {
            if (DomainParameters == null)
            {
                GenerateDomainParameters();
            }

            StaticKeyPair = Dsa.GenerateKeyPair(DomainParameters).KeyPair;

            // Only party U generates an ephemeral key
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.InitiatorPartyU)
            {
                EphemeralKeyPair = Dsa.GenerateKeyPair(DomainParameters).KeyPair;
            }

            // When party V, KC, Bilateral, generate ephemeral nonce
            // When party V, KC, Unilateral, and the recipient of key confirmation, ephemeral nonce
            // Otherwise, no ephemeral nonce.
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.ResponderPartyV &&
                SchemeParameters.KasMode == KasMode.KdfKc)
            {
                if (SchemeParameters.KeyConfirmationDirection == KeyConfirmationDirection.Bilateral ||
                    (
                        SchemeParameters.KeyConfirmationDirection == KeyConfirmationDirection.Unilateral &&
                        SchemeParameters.KeyConfirmationRole == KeyConfirmationRole.Recipient
                    )
                    )
                {
                    EphemeralNonce = EntropyProvider.GetEntropy(new BitString(DomainParameters.P).BitLength);
                }
            }

            // when party U and KdfNoKc, a NoKeyConfirmationNonce is needed.
            if (SchemeParameters.KeyAgreementRole == KeyAgreementRole.InitiatorPartyU &&
                SchemeParameters.KasMode == KasMode.KdfNoKc)
            {
                NoKeyConfirmationNonce = EntropyProvider.GetEntropy(128);
            }
        }