public EllipticCurve(string name, BigInteger p, BigInteger a, BigInteger b, EllipticCurvePoint g, BigInteger n, short h, uint length) { Name = name; P = p; A = a; B = b; G = g; N = n; H = h; LengthInBits = length; }
public IBuffer DerivteKeyWithHkdf(EllipticCurvePoint sharedSecret, IBuffer salt, IBuffer info) { var curve = _curves; int ikmLengthInBytes = (int)curve.LengthInBits / 8; var xBytes = sharedSecret.X.ToByteArray(); // Convert to from Little-endian to Big-endian byte[] ikmBytes = CopyReversedBytesToBytes(xBytes, ikmLengthInBytes); IBuffer inpitKeyMaterial = CryptographicBuffer.CreateFromByteArray(ikmBytes); var hkdf = new HMacBasedExtractAndExpandKeyDerivationFunction(); // ReSharper disable once ExpressionIsAlwaysNull IBuffer outputKeyMaterial = hkdf.DeriveKey(MacAlgorithmNames.HmacSha256, salt, info, inpitKeyMaterial); return(outputKeyMaterial); }
/// <summary> /// Returns True if the given point lies on the elliptic curve. /// </summary> /// <param name="point"></param> /// <returns></returns> public bool IsPointOnCurve(EllipticCurvePoint point) { if (point == EllipticCurvePoint.InfinityPoint) { return(true); } BigInteger y = point.Y; BigInteger x = point.X; BigInteger a = this.A; BigInteger b = this.B; BigInteger p = this.P; var rem = (y * y - x * x * x - a * x - b) % p; return(rem == 0); }
public EllipticCurvePoint Add(EllipticCurvePoint point1, EllipticCurvePoint point2) { _curve.EnsureOnCurve(point1); _curve.EnsureOnCurve(point2); if (EllipticCurvePoint.IsInfinityPoint(point1)) { return(point2); } if (EllipticCurvePoint.IsInfinityPoint(point2)) { return(point1); } var x1 = point1.X; var y1 = point1.Y; var x2 = point2.X; var y2 = point2.Y; var p = _curve.P; var a = _curve.A; BigInteger m; if (x1 == x2) { if (y1 != y2) // point1 + (-point1) = 0 { return(EllipticCurvePoint.InfinityPoint); } // This is the case point1 == point2. m = (3 * x1 * x1 + a) * InverseMod(2 * y1, p); } else // point1 != point2. { m = (y1 - y2) * InverseMod(x1 - x2, p); } BigInteger x3 = m * m - x1 - x2; BigInteger y3 = y1 + m * (x3 - x1); var result = new EllipticCurvePoint(Modular(x3, p), Modular(-y3, p)); return(result); }
public EllipticCurvePoint PointNeg(EllipticCurvePoint point) { _curve.EnsureOnCurve(point); // -0 = 0 if (EllipticCurvePoint.IsInfinityPoint(point)) { return(point); } var x = point.X; var y = point.Y; var p = _curve.P; var result = new EllipticCurvePoint(x, Modular(-y, p)); _curve.EnsureOnCurve(result); return(result); }
public void MakeKeyPair(out BigInteger privateKey, out EllipticCurvePoint publicKey) { var curve = _curves; var keyBytes = curve.LengthInBits / 8; var unsignedBytes = new byte[] { 0x00 }; var randomBytes = CryptographicBuffer.GenerateRandom(keyBytes).ToArray(); var positiveRandomBytes = randomBytes.Concat(unsignedBytes).ToArray(); // To avoid randomBytes treated as negtive value var randomValue = new BigInteger(positiveRandomBytes); do { privateKey = (randomValue % curve.N); }while (privateKey == 0); publicKey = _calculator.ScalarMult(privateKey, curve.G); }
private static EllipticCurve GetEllipticCurveSecp256K1() { string name = EllipticCurveNames.Secp256K1; BigInteger p = BigInteger.Parse("00fffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f", NumberStyles.HexNumber); BigInteger a = 0; BigInteger b = 7; var gX = BigInteger.Parse("79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798", NumberStyles.HexNumber); var gY = BigInteger.Parse("483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8", NumberStyles.HexNumber); EllipticCurvePoint g = new EllipticCurvePoint(gX, gY); BigInteger n = BigInteger.Parse("00fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", NumberStyles.HexNumber); Int16 h = 1; var secp256K1 = new EllipticCurve ( name: name, p: p, a: a, b: b, g: g, n: n, h: h, length: 256 ); return(secp256K1); }
private static EllipticCurve GetEllipticCurveSecp256R1() { string name = EllipticCurveNames.Secp256R1; BigInteger p = BigInteger.Parse("00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.HexNumber); BigInteger a = BigInteger.Parse("00FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", NumberStyles.HexNumber); BigInteger b = BigInteger.Parse("005AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B", NumberStyles.HexNumber); var gX = BigInteger.Parse("006B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", NumberStyles.HexNumber); var gY = BigInteger.Parse("004FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5", NumberStyles.HexNumber); EllipticCurvePoint g = new EllipticCurvePoint(gX, gY); BigInteger n = BigInteger.Parse("00FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551", NumberStyles.HexNumber); Int16 h = 1; var secp256R1 = new EllipticCurve ( name: name, p: p, a: a, b: b, g: g, n: n, h: h, length: 256 ); return(secp256R1); }
public EllipticCurvePoint DeriveSharedSecret(BigInteger myPrivateKey, EllipticCurvePoint otherPartyPublicKey) { var sharedSecret = _calculator.ScalarMult(myPrivateKey, otherPartyPublicKey); return(sharedSecret); }
public static bool IsInfinityPoint(EllipticCurvePoint point) { return(point == EllipticCurvePoint.InfinityPoint); }