public byte[] PerformKeyAgreement (byte[] otherPublicKey, int offset, int length) { Number otherPub = new Number (otherPublicKey, offset, length, false); Number sharedKey = new Classical (_p).Pow (otherPub, _private); byte[] buf = new byte[KeySizeValue >> 3]; sharedKey.CopyToBigEndian (buf, 0, buf.Length); return buf; }
public byte[] PerformKeyAgreement(byte[] otherPublicKey, int offset, int length) { Number otherPub = new Number(otherPublicKey, offset, length, false); Number sharedKey = new Classical(_p).Pow(otherPub, _private); byte[] buf = new byte[KeySizeValue >> 3]; sharedKey.CopyToBigEndian(buf, 0, buf.Length); return(buf); }
public DiffieHellman () { KeySizeValue = 1024; // TODO: ビット数を指定可能にする Number max = Number.One << KeySizeValue; // TODO: 法と生成元を動的生成可能にする _g = new Number (new byte[] { 0x86, 0x41, 0x6a, 0x6f, 0xec, 0xdd, 0xaf, 0x20, 0x23, 0xd6, 0x01, 0x10, 0xb7, 0xd0, 0xc7, 0x1d, 0xfe, 0xfc, 0x16, 0x9d, 0xba, 0x82, 0xf2, 0xb0, 0x9e, 0x7e, 0x40, 0x0e, 0xb5, 0x0c, 0x04, 0x01, 0xdf, 0xe1, 0xc3, 0x3a, 0x45, 0xe2, 0xf1, 0x47, 0x4f, 0xd1, 0x35, 0x5b, 0x2e, 0x59, 0x91, 0xdb, 0x1d, 0xeb, 0xa0, 0xa8, 0x7c, 0xd3, 0x56, 0x32, 0xd7, 0xd4, 0x52, 0x86, 0xfc, 0xc5, 0xba, 0x60, 0xbe, 0x70, 0x45, 0x23, 0x8b, 0xdc, 0x27, 0x3c, 0x06, 0xb1, 0x23, 0xf1, 0x7f, 0xc2, 0x2a, 0x15, 0xb6, 0x2f, 0xbd, 0x9e, 0x0b, 0x6f, 0x57, 0xa3, 0xb3, 0x31, 0x0e, 0xd2, 0xd5, 0xdc, 0xf8, 0x6f, 0x51, 0xd7, 0x3d, 0x03, 0x9a, 0x1e, 0xf8, 0xf0, 0xbd, 0x57, 0x36, 0xe4, 0x95, 0xcf, 0x09, 0xdb, 0x49, 0x7e, 0x96, 0x45, 0x12, 0x6d, 0xfd, 0xff, 0xba, 0x2f, 0xd6, 0x55, 0xc3, 0x76, 0x2b, 0x9a }, false); _p = new Number (new byte[] { 0xc7, 0x8c, 0x4d, 0x21, 0x12, 0x1f, 0x84, 0x3d, 0x43, 0xc1, 0xd7, 0xba, 0xf9, 0xd8, 0x97, 0x56, 0x7a, 0xc4, 0xed, 0x3a, 0xff, 0x53, 0x9d, 0x2c, 0x97, 0xf9, 0x25, 0x38, 0xb6, 0xdc, 0x54, 0xb4, 0x86, 0xd1, 0x55, 0xf9, 0x43, 0xd1, 0xd1, 0x1a, 0x21, 0x7e, 0x89, 0x49, 0xea, 0xa2, 0x7b, 0x15, 0x46, 0x60, 0x1b, 0xa4, 0x69, 0x9f, 0xe1, 0x63, 0x27, 0xc6, 0x00, 0x90, 0x7a, 0x9e, 0x7e, 0x20, 0x95, 0xfe, 0xa0, 0x08, 0xef, 0xc5, 0x73, 0x38, 0x8d, 0xc9, 0x04, 0xb7, 0x8d, 0xe6, 0x44, 0xd7, 0x2b, 0x37, 0x74, 0x81, 0x84, 0x62, 0x09, 0x84, 0xa9, 0xf2, 0x60, 0x32, 0x02, 0xa5, 0xc7, 0x90, 0xce, 0xdc, 0x32, 0x94, 0x15, 0x6b, 0x7e, 0x90, 0xc8, 0x14, 0xb4, 0x06, 0x6f, 0x44, 0xee, 0x35, 0xe0, 0x40, 0xcd, 0xe0, 0x76, 0x48, 0x79, 0x9d, 0x72, 0xf6, 0xc8, 0xef, 0x86, 0x5e, 0x45, 0xcf }, false); while (true) { _private = Number.CreateRandomElement (max); if (_private.BitCount () == KeySizeValue) break; } _pub = new Classical (_p).Pow (_g, _private); }
public DiffieHellman() { KeySizeValue = 1024; // TODO: ビット数を指定可能にする Number max = Number.One << KeySizeValue; // TODO: 法と生成元を動的生成可能にする _g = new Number(new byte[] { 0x86, 0x41, 0x6a, 0x6f, 0xec, 0xdd, 0xaf, 0x20, 0x23, 0xd6, 0x01, 0x10,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20, 0x95, 0xfe, 0xa0, 0x08, 0xef, 0xc5, 0x73, 0x38, 0x8d, 0xc9, 0x04, 0xb7, 0x8d, 0xe6, 0x44, 0xd7, 0x2b, 0x37, 0x74, 0x81, 0x84, 0x62, 0x09, 0x84, 0xa9, 0xf2, 0x60, 0x32, 0x02, 0xa5, 0xc7, 0x90, 0xce, 0xdc, 0x32, 0x94, 0x15, 0x6b, 0x7e, 0x90, 0xc8, 0x14, 0xb4, 0x06, 0x6f, 0x44, 0xee, 0x35, 0xe0, 0x40, 0xcd, 0xe0, 0x76, 0x48, 0x79, 0x9d, 0x72, 0xf6, 0xc8, 0xef, 0x86, 0x5e, 0x45, 0xcf }, false); while (true) { _private = Number.CreateRandomElement(max); if (_private.BitCount() == KeySizeValue) { break; } } _pub = new Classical(_p).Pow(_g, _private); }
/// <summary> /// TODO: 未実装のValidationステップを実装する /// </summary> public bool Validate () { IFiniteField ff = _group.FiniteField; // Step1: Check that p is an odd prime // Step2: Check that a,b,Gx and Gy are integers in the interval [0, p - 1] ECPoint ExportedG = _G.Export (); Number Gx = ff.ToElement (ExportedG.X); Number Gy = ff.ToElement (ExportedG.Y); if (A > P || B > P || Gx > P || Gy > P) return false; // Step3: Check that 4*a^3 + 27*b^2 != 0 (mod p) Number Apow3 = ff.Multiply (A, ff.Multiply (A, A)); Number Bpow2 = ff.Multiply (B, B); Number ret = ff.Add (ff.Multiply (ff.ToElement (Number.Four), ff.ToElement (Apow3)), ff.Multiply (ff.ToElement (Number.TwentySeven), Bpow2)); if (ret.IsZero ()) return false; // Step4: Gy^2 = Gx^3 + a*Gx + b Number aGx = ff.Multiply (A, Gx); Number Xpow3 = ff.Multiply (Gx, ff.Multiply (Gx, Gx)); Number Ypow2 = ff.Multiply (Gy, Gy); ret = ff.Add (Xpow3, ff.Add (aGx, B)); if (ret.CompareTo (Ypow2) != 0) return false; // Step5: Check that n is prime. // Step6: Check that h <= 4, and that h = (sqrt(p)+1)^2 / n // Step7: Check that nG = O ECPoint nG = _G.Multiply (N).Export (); if (!nG.IsInifinity ()) return false; // Step8: Check that q^B != 1 (mod n) for any 1 <= B <= 20, and that nh != p Number p = Number.One; Classical c = new Classical (N); for (int i = 0; i <= 20; i ++) { p = c.Multiply (p, P); if (p.IsOne ()) return false; } if (c.Multiply (N, new Number (new uint[] {H}, 1)).CompareTo (P) == 0) return false; return true; }