Exemplo n.º 1
0
 public BigInt GenPriv(RandomNumberGenerator rng)
 {
     EllipticCurve.BigInt priv = null;
     do
     {
         if (priv != null)
         {
             priv.Clear();
         }
         var bytes = new byte[curveByteLen];
         rng.GetBytes(bytes);
         var byteMask = (1 << (curveLen & 7)) - 1;
         if (byteMask != 0)
         {
             bytes[0] &= (byte)byteMask;
         }
         priv = new EllipticCurve.BigInt(bytes, 0, bytes.Length);
         Utils.ClearArray(bytes);
     } while (priv >= p || priv.IsZero());
     return(priv);
 }
Exemplo n.º 2
0
        HandshakeType SendClientKeyExchangeEcdh(ref int offset)
        {
            var pkParameters = _handshakeData.CertList[0].GetKeyAlgorithmParameters();
            var pkKey = _handshakeData.CertList[0].GetPublicKey();

            var curve = EllipticCurve.GetCurveFromParameters(pkParameters);
            if (curve == null)
                SendAlertFatal(AlertDescription.IllegalParameter);

            var Qax = new EllipticCurve.BigInt(pkKey, 1, curve.curveByteLen);
            var Qay = new EllipticCurve.BigInt(pkKey, 1 + curve.curveByteLen, curve.curveByteLen);

            byte[] preMasterSecret;
            EllipticCurve.Affine publicPoint;
            curve.Ecdh(Qax, Qay, _rng, out preMasterSecret, out publicPoint);

            SetMasterSecret(preMasterSecret);
            _buf[offset++] = (byte)(1 + 2 * curve.curveByteLen); // Point length
            _buf[offset++] = 4; // Uncompressed
            offset += publicPoint.x.ExportToBigEndian(_buf, offset, curve.curveByteLen);
            offset += publicPoint.y.ExportToBigEndian(_buf, offset, curve.curveByteLen);
            publicPoint.Clear();

            return HandshakeType.ClientKeyExchange;
        }
Exemplo n.º 3
0
 public BigInt GenPriv(RandomNumberGenerator rng)
 {
     EllipticCurve.BigInt priv = null;
     do
     {
         if (priv != null)
             priv.Clear();
         var bytes = new byte[curveByteLen];
         rng.GetBytes(bytes);
         var byteMask = (1 << (curveLen & 7)) - 1;
         if (byteMask != 0)
             bytes[0] &= (byte)byteMask;
         priv = new EllipticCurve.BigInt(bytes, 0, bytes.Length);
         Utils.ClearArray(bytes);
     } while (priv >= p || priv.IsZero());
     return priv;
 }