diff --git a/src/_ecdsa.c b/src/_ecdsa.c index a4e5fdd..c947283 100644 --- a/src/_ecdsa.c +++ b/src/_ecdsa.c @@ -5,7 +5,7 @@ void signZZ_p(Sig * sig, char * msg, mpz_t d, mpz_t k, const CurveZZ_p * curve) { mpz_t e, kinv; - + int orderBits, digestBits; // R = k * G, r = R[x] PointZZ_p R; pointZZ_pMul(&R, curve->g, k, curve); @@ -14,8 +14,8 @@ void signZZ_p(Sig * sig, char * msg, mpz_t d, mpz_t k, const CurveZZ_p * curve) // convert digest to integer (digest is computed as hex in ecdsa.py) mpz_init_set_str(e, msg, 16); - int orderBits = mpz_sizeinbase(curve->q, 2); - int digestBits = strlen(msg) * 4; + orderBits = mpz_sizeinbase(curve->q, 2); + digestBits = strlen(msg) * 4; if(digestBits > orderBits) { mpz_fdiv_q_2exp(e, e, digestBits - orderBits); @@ -36,12 +36,14 @@ void signZZ_p(Sig * sig, char * msg, mpz_t d, mpz_t k, const CurveZZ_p * curve) int verifyZZ_p(Sig * sig, char * msg, PointZZ_p * Q, const CurveZZ_p * curve) { mpz_t e, w, u1, u2; PointZZ_p tmp; + int orderBits, digestBits, equal; + mpz_inits(w, u1, u2, tmp.x, tmp.y, NULL); // convert digest to integer (digest is computed as hex in ecdsa.py) mpz_init_set_str(e, msg, 16); - int orderBits = mpz_sizeinbase(curve->q, 2); - int digestBits = strlen(msg) * 4; + orderBits = mpz_sizeinbase(curve->q, 2); + digestBits = strlen(msg) * 4; if(digestBits > orderBits) { mpz_fdiv_q_2exp(e, e, digestBits - orderBits); @@ -56,25 +58,28 @@ int verifyZZ_p(Sig * sig, char * msg, PointZZ_p * Q, const CurveZZ_p * curve) { pointZZ_pShamirsTrick(&tmp, curve->g, u1, Q, u2, curve); mpz_mod(tmp.x, tmp.x, curve->q); - int equal = (mpz_cmp(tmp.x, sig->r) == 0); + equal = (mpz_cmp(tmp.x, sig->r) == 0); mpz_clears(e, w, u1, u2, tmp.x, tmp.y, NULL); return equal; } /****************************************************************************** - PYTHON BINDINGS - ******************************************************************************/ +PYTHON BINDINGS +******************************************************************************/ static PyObject * _ecdsa_sign(PyObject *self, PyObject *args) { char * msg, * d, * k, * p, * a, * b, * q, * gx, * gy; - + char * resultR; + char * resultS; + mpz_t privKey, nonce; + Sig sig; + CurveZZ_p * curve; + PyObject * ret; if (!PyArg_ParseTuple(args, "sssssssss", &msg, &d, &k, &p, &a, &b, &q, &gx, &gy)) { return NULL; } - mpz_t privKey, nonce; - CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10); - Sig sig; + curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10); mpz_init_set_str(privKey, d, 10); mpz_init_set_str(nonce, k, 10); @@ -82,11 +87,11 @@ static PyObject * _ecdsa_sign(PyObject *self, PyObject *args) { signZZ_p(&sig, msg, privKey, nonce, curve); destroyCurveZZ_p(curve); - char * resultR = mpz_get_str(NULL, 10, sig.r); - char * resultS = mpz_get_str(NULL, 10, sig.s); + resultR = mpz_get_str(NULL, 10, sig.r); + resultS = mpz_get_str(NULL, 10, sig.s); mpz_clears(sig.r, sig.s, privKey, NULL); - PyObject * ret = Py_BuildValue("ss", resultR, resultS); + ret = Py_BuildValue("ss", resultR, resultS); free(resultR); free(resultS); return ret; @@ -95,19 +100,21 @@ static PyObject * _ecdsa_sign(PyObject *self, PyObject *args) { static PyObject * _ecdsa_verify(PyObject *self, PyObject *args) { char * r, * s, * msg, * qx, * qy, * p, * a, * b, * q, * gx, * gy; - + Sig sig; + CurveZZ_p * curve; + int valid = 0; + PointZZ_p * Q; + if (!PyArg_ParseTuple(args, "sssssssssss", &r, &s, &msg, &qx, &qy, &p, &a, &b, &q, &gx, &gy)) { return NULL; } - Sig sig; mpz_init_set_str(sig.r, r, 10); mpz_init_set_str(sig.s, s, 10); - CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10); - int valid = 0; + curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10); - PointZZ_p * Q = buildPointZZ_p(qx, qy, 10); + Q = buildPointZZ_p(qx, qy, 10); valid = verifyZZ_p(&sig, msg, Q, curve); destroyCurveZZ_p(curve); diff --git a/src/curveMath.c b/src/curveMath.c index 10160a7..52ab664 100644 --- a/src/curveMath.c +++ b/src/curveMath.c @@ -73,12 +73,13 @@ void pointZZ_pAdd(PointZZ_p * rop, const PointZZ_p * op1, const PointZZ_p * op2, void pointZZ_pMul(PointZZ_p * rop, const PointZZ_p * point, const mpz_t scalar, const CurveZZ_p * curve) { PointZZ_p R0, R1, tmp; + int dbits, i; mpz_inits(R1.x, R1.y, tmp.x, tmp.y, NULL); mpz_init_set(R0.x, point->x); mpz_init_set(R0.y, point->y); pointZZ_pDouble(&R1, point, curve); - int dbits = mpz_sizeinbase(scalar, 2), i; + dbits = mpz_sizeinbase(scalar, 2), i; for(i = dbits - 2; i >= 0; i--) { if(mpz_tstbit(scalar, i)) { @@ -106,15 +107,16 @@ void pointZZ_pMul(PointZZ_p * rop, const PointZZ_p * point, const mpz_t scalar, void pointZZ_pShamirsTrick(PointZZ_p * rop, const PointZZ_p * point1, const mpz_t scalar1, - const PointZZ_p * point2, const mpz_t scalar2, const CurveZZ_p * curve) +const PointZZ_p * point2, const mpz_t scalar2, const CurveZZ_p * curve) { PointZZ_p sum, tmp; + int scalar1Bits, scalar2Bits, l; mpz_inits(sum.x, sum.y, tmp.x, tmp.y, NULL); pointZZ_pAdd(&sum, point1, point2, curve); - int scalar1Bits = mpz_sizeinbase(scalar1, 2); - int scalar2Bits = mpz_sizeinbase(scalar2, 2); - int l = (scalar1Bits > scalar2Bits ? scalar1Bits : scalar2Bits) - 1; + scalar1Bits = mpz_sizeinbase(scalar1, 2); + scalar2Bits = mpz_sizeinbase(scalar2, 2); + l = (scalar1Bits > scalar2Bits ? scalar1Bits : scalar2Bits) - 1; if(mpz_tstbit(scalar1, l) && mpz_tstbit(scalar2, l)) { mpz_set(rop->x, sum.x); @@ -150,30 +152,35 @@ void pointZZ_pShamirsTrick(PointZZ_p * rop, const PointZZ_p * point1, const mpz_ /****************************************************************************** - PYTHON BINDINGS - ******************************************************************************/ +PYTHON BINDINGS +******************************************************************************/ static PyObject * curvemath_mul(PyObject *self, PyObject *args) { char * x, * y, * d, * p, * a, * b, * q, * gx, * gy; - + char * resultX; + char * resultY; + CurveZZ_p * curve; + PointZZ_p * point; + PointZZ_p result; + mpz_t scalar; + PyObject * ret; + if (!PyArg_ParseTuple(args, "sssssssss", &x, &y, &d, &p, &a, &b, &q, &gx, &gy)) { return NULL; } - PointZZ_p result; - mpz_t scalar; mpz_init_set_str(scalar, d, 10); - CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);; + curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);; - PointZZ_p * point = buildPointZZ_p(x, y, 10); + point = buildPointZZ_p(x, y, 10); pointZZ_pMul(&result, point, scalar, curve); destroyPointZZ_p(point); destroyCurveZZ_p(curve); - char * resultX = mpz_get_str(NULL, 10, result.x); - char * resultY = mpz_get_str(NULL, 10, result.y); + resultX = mpz_get_str(NULL, 10, result.x); + resultY = mpz_get_str(NULL, 10, result.y); mpz_clears(result.x, result.y, scalar, NULL); - PyObject * ret = Py_BuildValue("ss", resultX, resultY); + ret = Py_BuildValue("ss", resultX, resultY); free(resultX); free(resultY); return ret; @@ -181,17 +188,23 @@ static PyObject * curvemath_mul(PyObject *self, PyObject *args) { static PyObject * curvemath_add(PyObject *self, PyObject *args) { char * px, * py, * qx, * qy, * p, * a, * b, * q, * gx, * gy; - + PointZZ_p result; + CurveZZ_p * curve; + PointZZ_p * P; + PointZZ_p * Q; + char * resultX; + char * resultY; + PyObject * ret; + if (!PyArg_ParseTuple(args, "ssssssssss", &px, &py, &qx, &qy, &p, &a, &b, &q, &gx, &gy)) { return NULL; } - PointZZ_p result; mpz_inits(result.x, result.y, NULL); - CurveZZ_p * curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);; + curve = buildCurveZZ_p(p, a, b, q, gx, gy, 10);; - PointZZ_p * P = buildPointZZ_p(px, py, 10); - PointZZ_p * Q = buildPointZZ_p(qx, qy, 10); + P = buildPointZZ_p(px, py, 10); + Q = buildPointZZ_p(qx, qy, 10); if(pointZZ_pEqual(P, Q)) { pointZZ_pDouble(&result, P, curve); @@ -204,11 +217,11 @@ static PyObject * curvemath_add(PyObject *self, PyObject *args) { destroyPointZZ_p(Q); destroyCurveZZ_p(curve); - char * resultX = mpz_get_str(NULL, 10, result.x); - char * resultY = mpz_get_str(NULL, 10, result.y); + resultX = mpz_get_str(NULL, 10, result.x); + resultY = mpz_get_str(NULL, 10, result.y); mpz_clears(result.x, result.y, NULL); - PyObject * ret = Py_BuildValue("ss", resultX, resultY); + ret = Py_BuildValue("ss", resultX, resultY); free(resultX); free(resultY); return ret; diff --git a/windows_make.cmd b/windows_make.cmd new file mode 100755 index 0000000..e4a128c --- /dev/null +++ b/windows_make.cmd @@ -0,0 +1,70 @@ +if not exist aria2c.exe ( + echo "Download aria2c.exe from https://github.com/aria2/aria2/releases/tag/release-1.34.0" + goto :exit + ) + +if exist "C:\Program Files (x86)\" ( + set programfiles = "C:\Program Files (x86)\" + ) else ( set programfiles = "C:\Program Files\" ) + + +if not exist "%programfiles%\WinRAR\WinRAR.exe" ( + echo "Need some tool to extract bz2 and zip archives" + goto :exit + ) else ( set unrar="%programfiles%\WinRAR\WinRAR.exe") + + +if exist "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\bin\cl.exe" ( + goto :getyasm + ) else ( + aria2c.exe https://download.microsoft.com/download/7/9/6/796EF2E4-801B-4FC4-AB28-B59FBF6D907B/VCForPython27.msi + msiexec.exe /i VCForPython27.msi + ) + + +:getyasm + if not exist "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\bin\yasm.exe" ( + aria2c.exe http://www.tortall.net/projects/yasm/releases/yasm-1.3.0-win32.exe + copy yasm-1.3.0-win32.exe "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\bin\yasm.exe" + ) + +:getmpir + if exist "mpir-2.6.0.tar.bz2" ( + goto :make_mpir + ) else ( + aria2c.exe http://www.mpir.org/mpir-2.6.0.tar.bz2 + %unrar% x mpir-2.6.0.tar.bz2 mpir-2.6.0 + ) + +:make_mpir + if not exist "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\lib\gmp.lib" ( + cd mpir-2.6.0\win + call "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat" + call configure.bat ABI 32 + call make.bat + REM make check + call gen_mpir_h.bat + copy mpir.lib "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\lib\gmp.lib" + cd .. + copy gmp.h "C:\Users\%username%\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\include\gmp.h" + cd .. + ) + +REM have to be replaced to https://github.com/AntonKueltz/fastecdsa/archive/master.zip if commited +aria2c.exe https://github.com/shikuk/fastecdsa/archive/master.zip +%unrar% x fastecdsa-master.zip +cd fastecdsa-master +python setup.py build +python setup.py install + +cd.. + +python -m fastecdsa.test + +:exit +exit /b 1 + + + + + \ No newline at end of file