//The algorithms are implemented with the help of "Guide To Elliptic Curve Cryptography".// //.......................................................................................// #include #include "commctrl.h" #include "shellapi.h" #include #include #include #include #include #define NTL_NO_MIN_MAX #include #include #include NTL_CLIENT HINSTANCE hInst; using namespace std; ZZ SquareMod(ZZ a, ZZ prime) { ZZ res; res = to_ZZ(0); res = MulMod(a, a, prime); return res; } string ReverseString(string inData) { int i, len; string outData = ""; len = inData.length(); for(i = len - 1; i >= 0; i--) { outData += inData[i]; } return outData; } string BinaryEncode(ZZ num) { string bin, res; ZZ bNum = num; ZZ rem = to_ZZ(0); ZZ base = to_ZZ(2); stringstream ss; while (bNum != 1) { bNum = bNum / 2; rem = bNum % 2; ss< Scalar Multiplication of an EC point. */ ECPoint EC_KMult(ZZ K, ECPoint P, ZZ prime, ZZ a) { string binK = ""; char ch = ' '; binK = BinaryEncode(K); int blen = 0; blen = binK.length(); ECPoint multiple, tmp; multiple.Infinity = true; multiple.XCoordinate = to_ZZ(0); multiple.YCoordinate = to_ZZ(0); for(int i = 0; i < blen; i++) { ch = binK[i]; if (ch == '1') { tmp = EC_AddPoints(multiple, P, prime, a); multiple.XCoordinate.kill(); multiple.YCoordinate.kill(); multiple = PointCopy(tmp); tmp.XCoordinate.kill(); tmp.YCoordinate.kill(); } if (i < blen-1) { tmp = EC_PointDouble(multiple, prime, a); multiple.XCoordinate.kill(); multiple.YCoordinate.kill(); multiple = PointCopy(tmp); tmp.XCoordinate.kill(); tmp.YCoordinate.kill(); } } if (multiple.XCoordinate < 0) { multiple.XCoordinate = prime + multiple.XCoordinate; } if (multiple.YCoordinate < 0) { multiple.YCoordinate = prime + multiple.YCoordinate; } return multiple; } //......................................................................................................................// void ECDSA_Sign(char *Data, char *r, char *s) { int eq = 1; ZZ zero = to_ZZ(0); ZZ e = to_ZZ(Data); ZZ n = to_ZZ("40"); ZZ p = to_ZZ("29"); ZZ a = to_ZZ("-3"); ZZ k = to_ZZ("4"); // private key Sign: SetSeed(to_ZZ(clock())); ZZ K = RandomBnd(n); ECPoint R, x; x.Infinity = false; ZZ tmp = to_ZZ(0); ZZ tmp1, tmp2, tmp3, S; tmp1 = tmp2 = tmp3 = to_ZZ(0); R.Infinity = false; R.XCoordinate = to_ZZ("2"); R.YCoordinate = to_ZZ("10"); x = EC_KMult(K, R, p, a); // K*R= x tmp = x.XCoordinate % n; // tmp = xCoordinate mod n eq = compare(tmp, zero); if (eq == 0) { eq = 1; goto Sign; } stringstream ss, sss; string st, str; ss << tmp; st = ss.str(); strcpy(r, st.c_str()); S = InvMod(K, n); tmp1 = MulMod(k, tmp, n); tmp2 = AddMod(tmp1, e, n); tmp3 = MulMod(tmp2, S, n); eq = compare(tmp, zero); if (eq == 0) { eq = 1; goto Sign; } sss << tmp3; str = sss.str(); strcpy(s, str.c_str()); tmp1.kill(); tmp2.kill(); tmp3.kill(); tmp.kill(); zero.kill(); } int main() { char m[100], r[100], s[100]; memset(m, 0, 100); memset(r, 0, 100); memset(s, 0, 100); string in; cout<<"Enter the data to sign : \n"; cin>>in; strcpy(m,in.c_str()); ECDSA_Sign(m, r, s); cout<