private DiffieHellmanGroup(DiffieHellmanGroupType group, int keySize, BigInteger p, BigInteger g)
 {
     _group   = group;
     _keySize = keySize;
     _p       = p;
     _g       = g;
 }
Exemplo n.º 2
0
 public DiffieHellman(DiffieHellmanPublicKey publicKey, KeyAgreementKeyDerivationFunction kdFunc, KeyAgreementKeyDerivationHashAlgorithm kdHashAlgo)
     : base(kdFunc, kdHashAlgo)
 {
     _group      = DiffieHellmanGroupType.None;
     _keySize    = publicKey.KeySize;
     _p          = publicKey.P;
     _g          = publicKey.G;
     _privateKey = GeneratePrivateKey(_p);
 }
Exemplo n.º 3
0
        public DiffieHellmanPublicKey(int keySize, BigInteger p, BigInteger g, BigInteger x)
        {
            _group   = DiffieHellmanGroupType.None;
            _keySize = keySize;
            _p       = p;
            _g       = g;
            _x       = x;

            VerifyPublicKey(_keySize, _p, _g, _x);
        }
Exemplo n.º 4
0
        public DiffieHellmanPublicKey(DiffieHellmanGroupType group, int keySize, BigInteger x)
        {
            _group   = group;
            _keySize = keySize;
            _x       = x;

            DiffieHellmanGroup dhg = DiffieHellmanGroup.GetGroup(group, keySize);

            VerifyPublicKey(_keySize, dhg.P, dhg.G, x);
        }
Exemplo n.º 5
0
        public DiffieHellman(DiffieHellmanGroupType group, KeyAgreementKeyDerivationFunction kdFunc, KeyAgreementKeyDerivationHashAlgorithm kdHashAlgo)
            : base(kdFunc, kdHashAlgo)
        {
            _group = group;

            DiffieHellmanGroup dhg = DiffieHellmanGroup.GetGroup(_group);

            _keySize    = dhg.KeySize;
            _p          = dhg.P;
            _g          = dhg.G;
            _privateKey = GeneratePrivateKey(_p);
        }
Exemplo n.º 6
0
        public static DiffieHellmanGroup GetGroup(DiffieHellmanGroupType group)
        {
            switch (group)
            {
            case DiffieHellmanGroupType.RFC3526_GROUP14_2048BIT:
                return(new DiffieHellmanGroup(group, 2048, new BigInteger(p2048), new BigInteger(2)));

            case DiffieHellmanGroupType.RFC3526_GROUP15_3072BIT:
                return(new DiffieHellmanGroup(group, 3072, new BigInteger(p3072), new BigInteger(2)));

            case DiffieHellmanGroupType.RFC3526_GROUP16_4096BIT:
                return(new DiffieHellmanGroup(group, 4096, new BigInteger(p4096), new BigInteger(2)));

            default:
                throw new CryptoException("DiffieHellman group not supported.");
            }
        }
Exemplo n.º 7
0
        public DiffieHellmanPublicKey(byte[] publicKey)
        {
            using (MemoryStream mS = new MemoryStream(publicKey, false))
            {
                BinaryReader bR = new BinaryReader(mS);

                if (Encoding.ASCII.GetString(bR.ReadBytes(2)) != "DH")
                {
                    throw new InvalidDataException("Invalid DiffieHellmanPublicKey data format.");
                }

                switch (bR.ReadByte()) //version
                {
                case 2:
                    _keySize = bR.ReadInt32();
                    _group   = (DiffieHellmanGroupType)bR.ReadByte();

                    switch (_group)
                    {
                    case DiffieHellmanGroupType.RFC3526:
                        DiffieHellmanGroup dhg = DiffieHellmanGroup.GetGroup(_group, _keySize);
                        _p = dhg.P;
                        _g = dhg.G;
                        _x = ReadPositiveNumber(bR.ReadBuffer());
                        break;

                    case DiffieHellmanGroupType.None:
                        _p = ReadPositiveNumber(bR.ReadBuffer());
                        _g = ReadPositiveNumber(bR.ReadBuffer());
                        _x = ReadPositiveNumber(bR.ReadBuffer());
                        break;

                    default:
                        throw new NotSupportedException("DiffieHellmanGroup type not supported.");
                    }

                    break;

                default:
                    throw new InvalidDataException("DiffieHellmanPublicKey data format version not supported.");
                }
            }

            VerifyPublicKey(_keySize, _p, _g, _x);
        }
        public DiffieHellmanPublicKey(byte[] publicKey)
        {
            using (MemoryStream mS = new MemoryStream(publicKey, false))
            {
                BincodingDecoder decoder = new BincodingDecoder(mS, "DH");

                switch (decoder.Version)
                {
                case 1:
                    _keySize = decoder.DecodeNext().GetIntegerValue();
                    _group   = (DiffieHellmanGroupType)decoder.DecodeNext().GetByteValue();

                    switch (_group)
                    {
                    case DiffieHellmanGroupType.RFC3526:
                        DiffieHellmanGroup dhg = DiffieHellmanGroup.GetGroup(_group, _keySize);
                        _p = dhg.P;
                        _g = dhg.G;
                        _x = ReadPositiveNumber(decoder.DecodeNext().Value);
                        break;

                    case DiffieHellmanGroupType.None:
                        _p = ReadPositiveNumber(decoder.DecodeNext().Value);
                        _g = ReadPositiveNumber(decoder.DecodeNext().Value);
                        _x = ReadPositiveNumber(decoder.DecodeNext().Value);
                        break;

                    default:
                        throw new NotSupportedException("DiffieHellmanGroup type not supported.");
                    }


                    break;

                default:
                    throw new InvalidDataException("DiffieHellmanPublicKey data format version not supported.");
                }
            }

            VerifyPublicKey(_keySize, _p, _g, _x);
        }
        public static DiffieHellmanGroup GetGroup(DiffieHellmanGroupType group, int keySize)
        {
            switch (group)
            {
            case DiffieHellmanGroupType.RFC3526:
                switch (keySize)
                {
                case 2048:
                    return(new DiffieHellmanGroup(group, keySize, new BigInteger(p2048), new BigInteger(2)));

                case 3072:
                    return(new DiffieHellmanGroup(group, keySize, new BigInteger(p3072), new BigInteger(2)));

                case 4096:
                    return(new DiffieHellmanGroup(group, keySize, new BigInteger(p4096), new BigInteger(2)));

                default:
                    throw new CryptoException("DiffieHellman key size not supported.");
                }

            default:
                throw new CryptoException("DiffieHellman group not supported.");
            }
        }