00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __UT_ECurve__
00020 #define __UT_ECurve__
00021
00022 #include "UT_API.h"
00023 #include <SYS/SYS_Types.h>
00024 #include "UT_VeryLong.h"
00025 #include "UT_PolyField.h"
00026
00027 #define UT_ECURVE_MAXBASIS 6
00028
00029 class UT_API UT_ECurve {
00030 public:
00031 UT_ECurve() { myBits = 0; }
00032 UT_ECurve(const uint q[],
00033 const char *r,
00034 const char *a,
00035 const char *b,
00036 const char *gx,
00037 const char *gy)
00038 {
00039 setQ(q);
00040 myR = r;
00041 myA = a;
00042 myB = b;
00043 myGx = gx;
00044 myGy = gy;
00045 computeBits();
00046 }
00047
00048 void setQ(const uint q[])
00049 {
00050 int i;
00051 UT_VeryLong shift;
00052 myQ = 1u;
00053 for (i = 0; q[i]; i++)
00054 {
00055 myBasis[i] = q[i];
00056 shift = 1;
00057 shift <<= q[i];
00058 myQ += shift;
00059 }
00060 myBasis[i] = 1;
00061 for (i = i+1; i < UT_ECURVE_MAXBASIS; i++)
00062 myBasis[i] = 0;
00063 }
00064 void setR(int x) { myR = x; computeBits(); }
00065 void setR(const char *x) { myR = x; computeBits(); }
00066 void setR(const UT_VeryLong &x) { myR = x; computeBits(); }
00067 void setA(int x) { myA = x; }
00068 void setA(const char *x) { myA = x; }
00069 void setA(const UT_PolyField &x) { myA = x; }
00070 void setB(int x) { myB = x; }
00071 void setB(const char *x) { myB = x; }
00072 void setB(const UT_PolyField &x) { myB = x; }
00073 void setGx(int x) { myGx = x; }
00074 void setGx(const char *x) { myGx = x; }
00075 void setGx(const UT_PolyField &x) { myGx = x; }
00076 void setGy(int x) { myGy = x; }
00077 void setGy(const char *x) { myGy = x; }
00078 void setGy(const UT_PolyField &x) { myGy = x; }
00079
00080 const UT_VeryLong &getR() const { return myR; }
00081 const UT_PolyField &getQ() const { return myQ; }
00082 const UT_PolyField &getA() const { return myA; }
00083 const UT_PolyField &getB() const { return myB; }
00084 const UT_PolyField &getGx() const { return myGx; }
00085 const UT_PolyField &getGy() const { return myGy; }
00086
00087
00088 void generateRandom(UT_VeryLong &r,
00089 const uint *seed, uint seedlen) const;
00090 bool generateRandomRaw(UT_VeryLong &r,
00091 const uint *seed, uint len) const;
00092 void generatePublic(UT_PolyField &pubx, UT_PolyField &puby,
00093 const UT_VeryLong &key) const;
00094
00095
00096
00097 bool dsa(UT_VeryLong &acode, UT_VeryLong &bcode,
00098 const UT_VeryLong &key, const UT_VeryLong &r,
00099 const UT_VeryLong &m) const;
00100
00101 void partialDSA(UT_VeryLong &code,
00102 const UT_VeryLong &key, const UT_VeryLong &rnd) const;
00103
00104
00105 bool verify(const UT_VeryLong &m,
00106 const UT_VeryLong &code, const UT_VeryLong &codeb,
00107 const UT_PolyField &pubx, const UT_PolyField &puby) const;
00108
00109 void dump(int base=16) const;
00110
00111 private:
00112 void computeBits();
00113 void mult(UT_PolyField &x, UT_PolyField &y,
00114 const UT_VeryLong &m) const
00115 {
00116 mult(x, y, m, myGx, myGy);
00117 }
00118 void mult(UT_PolyField &x, UT_PolyField &y, const UT_VeryLong &m,
00119 const UT_PolyField &gx, const UT_PolyField &gy) const;
00120
00121 void curveDouble(UT_PolyField &x, UT_PolyField &y) const;
00122 void curveAdd(UT_PolyField &dx, UT_PolyField &dy,
00123 const UT_PolyField &sx, const UT_PolyField &sy,
00124 const UT_PolyField &gx, const UT_PolyField &gy) const;
00125
00126 UT_VeryLong myR;
00127 UT_PolyField myQ;
00128 UT_PolyField myA, myB;
00129 UT_PolyField myGx, myGy;
00130 int myBits;
00131 int myBasis[6];
00132 };
00133
00134 #endif
00135