ECMqvBasicAgreement.cs :  » PDF » iTextSharp » Org » BouncyCastle » Crypto » Agreement » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » PDF » iTextSharp 
iTextSharp » Org » BouncyCastle » Crypto » Agreement » ECMqvBasicAgreement.cs
using System;

using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math;
using Org.BouncyCastle.Math.EC;

namespace Org.BouncyCastle.Crypto.Agreement{
  public class ECMqvBasicAgreement
    : IBasicAgreement
  {
    protected internal MqvPrivateParameters privParams;

    public void Init(
      ICipherParameters parameters)
    {
      if (parameters is ParametersWithRandom)
      {
        parameters = ((ParametersWithRandom)parameters).Parameters;
      }

      this.privParams = (MqvPrivateParameters)parameters;
    }

    public virtual BigInteger CalculateAgreement(
      ICipherParameters pubKey)
    {
      MqvPublicParameters pubParams = (MqvPublicParameters)pubKey;

      ECPrivateKeyParameters staticPrivateKey = privParams.StaticPrivateKey;

      ECPoint agreement = calculateMqvAgreement(staticPrivateKey.Parameters, staticPrivateKey,
        privParams.EphemeralPrivateKey, privParams.EphemeralPublicKey,
        pubParams.StaticPublicKey, pubParams.EphemeralPublicKey);

      return agreement.X.ToBigInteger();
    }
    
    // The ECMQV Primitive as described in SEC-1, 3.4
    private static ECPoint calculateMqvAgreement(
      ECDomainParameters    parameters,
      ECPrivateKeyParameters  d1U,
      ECPrivateKeyParameters  d2U,
      ECPublicKeyParameters  Q2U,
      ECPublicKeyParameters  Q1V,
      ECPublicKeyParameters  Q2V)
    {
      BigInteger n = parameters.N;
      int e = (n.BitLength + 1) / 2;
      BigInteger powE = BigInteger.One.ShiftLeft(e);

      // The Q2U public key is optional
      ECPoint q;
      if (Q2U == null)
      {
        q = parameters.G.Multiply(d2U.D);
      }
      else
      {
        q = Q2U.Q;
      }

      BigInteger x = q.X.ToBigInteger();
      BigInteger xBar = x.Mod(powE);
      BigInteger Q2UBar = xBar.SetBit(e);
      BigInteger s = d1U.D.Multiply(Q2UBar).Mod(n).Add(d2U.D).Mod(n);

      BigInteger xPrime = Q2V.Q.X.ToBigInteger();
      BigInteger xPrimeBar = xPrime.Mod(powE);
      BigInteger Q2VBar = xPrimeBar.SetBit(e);

      BigInteger hs = parameters.H.Multiply(s).Mod(n);

      //ECPoint p = Q1V.Q.Multiply(Q2VBar).Add(Q2V.Q).Multiply(hs);
      ECPoint p = ECAlgorithms.SumOfTwoMultiplies(
        Q1V.Q, Q2VBar.Multiply(hs).Mod(n), Q2V.Q, hs);

      if (p.IsInfinity)
        throw new InvalidOperationException("Infinity is not a valid agreement value for MQV");

      return p;
    }
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.