- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# eccrypto.cpp

    00001 // eccrypto.cpp - written and placed in the public domain by Wei Dai
    00002
    00003 #include "pch.h"
    00004
    00005 // prevent Sun's CC compiler from including this file automatically
    00006 #if !defined(__SUNPRO_CC) || defined(CRYPTOPP_MANUALLY_INSTANTIATE_TEMPLATES)
    00007
    00008 #ifndef CRYPTOPP_IMPORTS
    00009
    00010 #include "eccrypto.h"
    00011 #include "nbtheory.h"
    00012 #include "oids.h"
    00013 #include "hex.h"
    00014 #include "argnames.h"
    00015 #include "ec2n.h"
    00016
    00017 NAMESPACE_BEGIN(CryptoPP)
    00018
    00019 #if 0
    00020 static void ECDSA_TestInstantiations()
    00021 {
    00022         ECDSA<EC2N>::Signer t1;
    00023         ECDSA<EC2N>::Verifier t2(t1);
    00024         ECNR<ECP>::Signer t3;
    00025         ECNR<ECP>::Verifier t4(t3);
    00026         ECIES<ECP>::Encryptor t5;
    00027         ECIES<EC2N>::Decryptor t6;
    00028         ECDH<ECP>::Domain t7;
    00029         ECMQV<ECP>::Domain t8;
    00030 }
    00031 #endif
    00032
    00033 // VC60 workaround: complains when these functions are put into an anonymous namespace
    00034 static Integer ConvertToInteger(const PolynomialMod2 &x)
    00035 {
    00036         unsigned int l = x.ByteCount();
    00037         SecByteBlock temp(l);
    00038         x.Encode(temp, l);
    00039         return Integer(temp, l);
    00040 }
    00041
    00042 static inline Integer ConvertToInteger(const Integer &x)
    00043 {
    00044         return x;
    00045 }
    00046
    00047 static bool CheckMOVCondition(const Integer &q, const Integer &r)
    00048 {
    00049         Integer t=1;
    00050         unsigned int n=q.BitCount(), m=r.BitCount();
    00051
    00052         for (unsigned int i=n; DiscreteLogWorkFactor(i)<m/2; i+=n)
    00053         {
    00054                 t = (t*q)%r;
    00055                 if (t == 1)
    00056                         return false;
    00057         }
    00058         return true;
    00059 }
    00060
    00061 // ******************************************************************
    00062
    00063 template <class T> struct EcRecommendedParameters;
    00064
    00065 template<> struct EcRecommendedParameters<EC2N>
    00066 {
    00067         EcRecommendedParameters(const OID &oid, unsigned int t2, unsigned int t3, unsigned int t4, const char *a, const char *b, const char *g, const char *n, unsigned int h)
    00068                 : oid(oid), t0(0), t1(0), t2(t2), t3(t3), t4(t4), a(a), b(b), g(g), n(n), h(h) {}
    00069         EcRecommendedParameters(const OID &oid, unsigned int t0, unsigned int t1, unsigned int t2, unsigned int t3, unsigned int t4, const char *a, const char *b, const char *g, const char *n, unsigned int h)
    00070                 : oid(oid), t0(t0), t1(t1), t2(t2), t3(t3), t4(t4), a(a), b(b), g(g), n(n), h(h) {}
    00071         EC2N *NewEC() const
    00072         {
    00073                 StringSource ssA(a, true, new HexDecoder);
    00074                 StringSource ssB(b, true, new HexDecoder);
    00075                 if (t0 == 0)
    00076                         return new EC2N(GF2NT(t2, t3, t4), EC2N::FieldElement(ssA, (size_t)ssA.MaxRetrievable()), EC2N::FieldElement(ssB, (size_t)ssB.MaxRetrievable()));
    00077                 else
    00078                         return new EC2N(GF2NPP(t0, t1, t2, t3, t4), EC2N::FieldElement(ssA, (size_t)ssA.MaxRetrievable()), EC2N::FieldElement(ssB, (size_t)ssB.MaxRetrievable()));
    00079         };
    00080
    00081         OID oid;
    00082         unsigned int t0, t1, t2, t3, t4;
    00083         const char *a, *b, *g, *n;
    00084         unsigned int h;
    00085 };
    00086
    00087 template<> struct EcRecommendedParameters<ECP>
    00088 {
    00089         EcRecommendedParameters(const OID &oid, const char *p, const char *a, const char *b, const char *g, const char *n, unsigned int h)
    00090                 : oid(oid), p(p), a(a), b(b), g(g), n(n), h(h) {}
    00091         ECP *NewEC() const
    00092         {
    00093                 StringSource ssP(p, true, new HexDecoder);
    00094                 StringSource ssA(a, true, new HexDecoder);
    00095                 StringSource ssB(b, true, new HexDecoder);
    00096                 return new ECP(Integer(ssP, (size_t)ssP.MaxRetrievable()), ECP::FieldElement(ssA, (size_t)ssA.MaxRetrievable()), ECP::FieldElement(ssB, (size_t)ssB.MaxRetrievable()));
    00097         };
    00098
    00099         OID oid;
    00100         const char *p;
    00101         const char *a, *b, *g, *n;
    00102         unsigned int h;
    00103 };
    00104
    00105 struct OIDLessThan
    00106 {
    00107         template <typename T>
    00108         inline bool operator()(const EcRecommendedParameters<T>& a, const OID& b) {return a.oid < b;}
    00109         template <typename T>
    00110         inline bool operator()(const OID& a, const EcRecommendedParameters<T>& b) {return a < b.oid;}
    00111         template <typename T>
    00112         inline bool operator()(const EcRecommendedParameters<T>& a, const EcRecommendedParameters<T>& b) {return a.oid < b.oid;}
    00113 };
    00114
    00115 static void GetRecommendedParameters(const EcRecommendedParameters<EC2N> *&begin, const EcRecommendedParameters<EC2N> *&end)
    00116 {
    00117         // this array must be sorted by OID
    00118         static const EcRecommendedParameters<EC2N> rec[] = {
    00119                 EcRecommendedParameters<EC2N>(ASN1::sect163k1(),
    00120                         163, 7, 6, 3, 0,
    00121                         "000000000000000000000000000000000000000001",
    00122                         "000000000000000000000000000000000000000001",
    00123                         "0402FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE80289070FB05D38FF58321F2E800536D538CCDAA3D9",
    00124                         "04000000000000000000020108A2E0CC0D99F8A5EF",
    00125                         2),
    00126                 EcRecommendedParameters<EC2N>(ASN1::sect163r1(),
    00127                         163, 7, 6, 3, 0,
    00128                         "07B6882CAAEFA84F9554FF8428BD88E246D2782AE2",
    00129                         "0713612DCDDCB40AAB946BDA29CA91F73AF958AFD9",
    00130                         "040369979697AB43897789566789567F787A7876A65400435EDB42EFAFB2989D51FEFCE3C80988F41FF883",
    00131                         "03FFFFFFFFFFFFFFFFFFFF48AAB689C29CA710279B",
    00132                         2),
    00133                 EcRecommendedParameters<EC2N>(ASN1::sect239k1(),
    00134                         239, 158, 0,
    00135                         "000000000000000000000000000000000000000000000000000000000000",
    00136                         "000000000000000000000000000000000000000000000000000000000001",
    00137                         "0429A0B6A887A983E9730988A68727A8B2D126C44CC2CC7B2A6555193035DC76310804F12E549BDB011C103089E73510ACB275FC312A5DC6B76553F0CA",
    00138                         "2000000000000000000000000000005A79FEC67CB6E91F1C1DA800E478A5",
    00139                         4),
    00140                 EcRecommendedParameters<EC2N>(ASN1::sect113r1(),
    00141                         113, 9, 0,
    00142                         "003088250CA6E7C7FE649CE85820F7",
    00143                         "00E8BEE4D3E2260744188BE0E9C723",
    00144                         "04009D73616F35F4AB1407D73562C10F00A52830277958EE84D1315ED31886",
    00145                         "0100000000000000D9CCEC8A39E56F",
    00146                         2),
    00147                 EcRecommendedParameters<EC2N>(ASN1::sect113r2(),
    00148                         113, 9, 0,
    00149                         "00689918DBEC7E5A0DD6DFC0AA55C7",
    00150                         "0095E9A9EC9B297BD4BF36E059184F",
    00151                         "0401A57A6A7B26CA5EF52FCDB816479700B3ADC94ED1FE674C06E695BABA1D",
    00152                         "010000000000000108789B2496AF93",
    00153                         2),
    00154                 EcRecommendedParameters<EC2N>(ASN1::sect163r2(),
    00155                         163, 7, 6, 3, 0,
    00156                         "000000000000000000000000000000000000000001",
    00157                         "020A601907B8C953CA1481EB10512F78744A3205FD",
    00158                         "0403F0EBA16286A2D57EA0991168D4994637E8343E3600D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
    00159                         "040000000000000000000292FE77E70C12A4234C33",
    00160                         2),
    00161                 EcRecommendedParameters<EC2N>(ASN1::sect283k1(),
    00162                         283, 12, 7, 5, 0,
    00163                         "000000000000000000000000000000000000000000000000000000000000000000000000",
    00164                         "000000000000000000000000000000000000000000000000000000000000000000000001",
    00165                         "040503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC245849283601CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
    00166                         "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
    00167                         4),
    00168                 EcRecommendedParameters<EC2N>(ASN1::sect283r1(),
    00169                         283, 12, 7, 5, 0,
    00170                         "000000000000000000000000000000000000000000000000000000000000000000000001",
    00171                         "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
    00172                         "0405F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B1205303676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
    00173                         "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
    00174                         2),
    00175                 EcRecommendedParameters<EC2N>(ASN1::sect131r1(),
    00176                         131, 8, 3, 2, 0,
    00177                         "07A11B09A76B562144418FF3FF8C2570B8",
    00178                         "0217C05610884B63B9C6C7291678F9D341",
    00179                         "040081BAF91FDF9833C40F9C181343638399078C6E7EA38C001F73C8134B1B4EF9E150",
    00180                         "0400000000000000023123953A9464B54D",
    00181                         2),
    00182                 EcRecommendedParameters<EC2N>(ASN1::sect131r2(),
    00183                         131, 8, 3, 2, 0,
    00184                         "03E5A88919D7CAFCBF415F07C2176573B2",
    00185                         "04B8266A46C55657AC734CE38F018F2192",
    00186                         "040356DCD8F2F95031AD652D23951BB366A80648F06D867940A5366D9E265DE9EB240F",
    00187                         "0400000000000000016954A233049BA98F",
    00188                         2),
    00189                 EcRecommendedParameters<EC2N>(ASN1::sect193r1(),
    00190                         193, 15, 0,
    00191                         "0017858FEB7A98975169E171F77B4087DE098AC8A911DF7B01",
    00192                         "00FDFB49BFE6C3A89FACADAA7A1E5BBC7CC1C2E5D831478814",
    00193                         "0401F481BC5F0FF84A74AD6CDF6FDEF4BF6179625372D8C0C5E10025E399F2903712CCF3EA9E3A1AD17FB0B3201B6AF7CE1B05",
    00194                         "01000000000000000000000000C7F34A778F443ACC920EBA49",
    00195                         2),
    00196                 EcRecommendedParameters<EC2N>(ASN1::sect193r2(),
    00197                         193, 15, 0,
    00198                         "0163F35A5137C2CE3EA6ED8667190B0BC43ECD69977702709B",
    00199                         "00C9BB9E8927D4D64C377E2AB2856A5B16E3EFB7F61D4316AE",
    00200                         "0400D9B67D192E0367C803F39E1A7E82CA14A651350AAE617E8F01CE94335607C304AC29E7DEFBD9CA01F596F927224CDECF6C",
    00201                         "010000000000000000000000015AAB561B005413CCD4EE99D5",
    00202                         2),
    00203                 EcRecommendedParameters<EC2N>(ASN1::sect233k1(),
    00204                         233, 74, 0,
    00205                         "000000000000000000000000000000000000000000000000000000000000",
    00206                         "000000000000000000000000000000000000000000000000000000000001",
    00207                         "04017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD612601DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
    00208                         "8000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
    00209                         4),
    00210                 EcRecommendedParameters<EC2N>(ASN1::sect233r1(),
    00211                         233, 74, 0,
    00212                         "000000000000000000000000000000000000000000000000000000000001",
    00213                         "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
    00214                         "0400FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
    00215                         "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
    00216                         2),
    00217                 EcRecommendedParameters<EC2N>(ASN1::sect409k1(),
    00218                         409, 87, 0,
    00219                         "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    00220                         "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
    00221                         "040060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE902374601E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
    00222                         "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
    00223                         4),
    00224                 EcRecommendedParameters<EC2N>(ASN1::sect409r1(),
    00225                         409, 87, 0,
    00226                         "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
    00227                         "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
    00228                         "04015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A70061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
    00229                         "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
    00230                         2),
    00231                 EcRecommendedParameters<EC2N>(ASN1::sect571k1(),
    00232                         571, 10, 5, 2, 0,
    00233                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
    00234                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
    00235                         "04026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C89720349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
    00236                         "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
    00237                         4),
    00238                 EcRecommendedParameters<EC2N>(ASN1::sect571r1(),
    00239                         571, 10, 5, 2, 0,
    00240                         "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
    00241                         "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
    00242                         "040303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
    00243                         "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
    00244                         2),
    00245         };
    00246         begin = rec;
    00247         end = rec + sizeof(rec)/sizeof(rec[0]);
    00248 }
    00249
    00250 static void GetRecommendedParameters(const EcRecommendedParameters<ECP> *&begin, const EcRecommendedParameters<ECP> *&end)
    00251 {
    00252         // this array must be sorted by OID
    00253         static const EcRecommendedParameters<ECP> rec[] = {
    00254                 EcRecommendedParameters<ECP>(ASN1::secp192r1(),
    00255                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF",
    00256                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC",
    00257                         "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1",
    00258                         "04188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF101207192B95FFC8DA78631011ED6B24CDD573F977A11E794811",
    00259                         "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831",
    00260                         1),
    00261                 EcRecommendedParameters<ECP>(ASN1::secp256r1(),
    00262                         "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF",
    00263                         "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC",
    00264                         "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B",
    00265                         "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
    00266                         "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
    00267                         1),
    00268                 EcRecommendedParameters<ECP>(ASN1::secp112r1(),
    00269                         "DB7C2ABF62E35E668076BEAD208B",
    00270                         "DB7C2ABF62E35E668076BEAD2088",
    00271                         "659EF8BA043916EEDE8911702B22",
    00272                         "0409487239995A5EE76B55F9C2F098A89CE5AF8724C0A23E0E0FF77500",
    00273                         "DB7C2ABF62E35E7628DFAC6561C5",
    00274                         1),
    00275                 EcRecommendedParameters<ECP>(ASN1::secp112r2(),
    00276                         "DB7C2ABF62E35E668076BEAD208B",
    00277                         "6127C24C05F38A0AAAF65C0EF02C",
    00278                         "51DEF1815DB5ED74FCC34C85D709",
    00279                         "044BA30AB5E892B4E1649DD0928643ADCD46F5882E3747DEF36E956E97",
    00280                         "36DF0AAFD8B8D7597CA10520D04B",
    00281                         4),
    00282                 EcRecommendedParameters<ECP>(ASN1::secp160r1(),
    00283                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF",
    00284                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC",
    00285                         "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45",
    00286                         "044A96B5688EF573284664698968C38BB913CBFC8223A628553168947D59DCC912042351377AC5FB32",
    00287                         "0100000000000000000001F4C8F927AED3CA752257",
    00288                         1),
    00289                 EcRecommendedParameters<ECP>(ASN1::secp160k1(),
    00290                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73",
    00291                         "0000000000000000000000000000000000000000",
    00292                         "0000000000000000000000000000000000000007",
    00293                         "043B4C382CE37AA192A4019E763036F4F5DD4D7EBB938CF935318FDCED6BC28286531733C3F03C4FEE",
    00294                         "0100000000000000000001B8FA16DFAB9ACA16B6B3",
    00295                         1),
    00296                 EcRecommendedParameters<ECP>(ASN1::secp256k1(),
    00297                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F",
    00298                         "0000000000000000000000000000000000000000000000000000000000000000",
    00299                         "0000000000000000000000000000000000000000000000000000000000000007",
    00300                         "0479BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8",
    00301                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141",
    00302                         1),
    00303                 EcRecommendedParameters<ECP>(ASN1::secp128r1(),
    00304                         "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF",
    00305                         "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC",
    00306                         "E87579C11079F43DD824993C2CEE5ED3",
    00307                         "04161FF7528B899B2D0C28607CA52C5B86CF5AC8395BAFEB13C02DA292DDED7A83",
    00308                         "FFFFFFFE0000000075A30D1B9038A115",
    00309                         1),
    00310                 EcRecommendedParameters<ECP>(ASN1::secp128r2(),
    00311                         "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF",
    00312                         "D6031998D1B3BBFEBF59CC9BBFF9AEE1",
    00313                         "5EEEFCA380D02919DC2C6558BB6D8A5D",
    00314                         "047B6AA5D85E572983E6FB32A7CDEBC14027B6916A894D3AEE7106FE805FC34B44",
    00315                         "3FFFFFFF7FFFFFFFBE0024720613B5A3",
    00316                         4),
    00317                 EcRecommendedParameters<ECP>(ASN1::secp160r2(),
    00318                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73",
    00319                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC70",
    00320                         "B4E134D3FB59EB8BAB57274904664D5AF50388BA",
    00321                         "0452DCB034293A117E1F4FF11B30F7199D3144CE6DFEAFFEF2E331F296E071FA0DF9982CFEA7D43F2E",
    00322                         "0100000000000000000000351EE786A818F3A1A16B",
    00323                         1),
    00324                 EcRecommendedParameters<ECP>(ASN1::secp192k1(),
    00325                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37",
    00326                         "000000000000000000000000000000000000000000000000",
    00327                         "000000000000000000000000000000000000000000000003",
    00328                         "04DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D",
    00329                         "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D",
    00330                         1),
    00331                 EcRecommendedParameters<ECP>(ASN1::secp224k1(),
    00332                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFE56D",
    00333                         "00000000000000000000000000000000000000000000000000000000",
    00334                         "00000000000000000000000000000000000000000000000000000005",
    00335                         "04A1455B334DF099DF30FC28A169A467E9E47075A90F7E650EB6B7A45C7E089FED7FBA344282CAFBD6F7E319F7C0B0BD59E2CA4BDB556D61A5",
    00336                         "010000000000000000000000000001DCE8D2EC6184CAF0A971769FB1F7",
    00337                         1),
    00338                 EcRecommendedParameters<ECP>(ASN1::secp224r1(),
    00339                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
    00340                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
    00341                         "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
    00342                         "04B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
    00343                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
    00344                         1),
    00345                 EcRecommendedParameters<ECP>(ASN1::secp384r1(),
    00346                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF",
    00347                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC",
    00348                         "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF",
    00349                         "04AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB73617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A147CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F",
    00350                         "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973",
    00351                         1),
    00352                 EcRecommendedParameters<ECP>(ASN1::secp521r1(),
    00353                         "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF",
    00354                         "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC",
    00355                         "0051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00",
    00356                         "0400C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66011839296A789A3BC0045C8A5FB42C7D1BD998F54449579B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C7086A272C24088BE94769FD16650",
    00357                         "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409",
    00358                         1),
    00359         };
    00360         begin = rec;
    00361         end = rec + sizeof(rec)/sizeof(rec[0]);
    00362 }
    00363
    00364 template <class EC> OID DL_GroupParameters_EC<EC>::GetNextRecommendedParametersOID(const OID &oid)
    00365 {
    00366         const EcRecommendedParameters<EllipticCurve> *begin, *end;
    00367         GetRecommendedParameters(begin, end);
    00368         const EcRecommendedParameters<EllipticCurve> *it = std::upper_bound(begin, end, oid, OIDLessThan());
    00369         return (it == end ? OID() : it->oid);
    00370 }
    00371
    00372 template <class EC> void DL_GroupParameters_EC<EC>::Initialize(const OID &oid)
    00373 {
    00374         const EcRecommendedParameters<EllipticCurve> *begin, *end;
    00375         GetRecommendedParameters(begin, end);
    00376         const EcRecommendedParameters<EllipticCurve> *it = std::lower_bound(begin, end, oid, OIDLessThan());
    00377         if (it == end || it->oid != oid)
    00378                 throw UnknownOID();
    00379
    00380         const EcRecommendedParameters<EllipticCurve> &param = *it;
    00381         m_oid = oid;
    00382         std::auto_ptr<EllipticCurve> ec(param.NewEC());
    00383         this->m_groupPrecomputation.SetCurve(*ec);
    00384
    00385         StringSource ssG(param.g, true, new HexDecoder);
    00386         Element G;
    00387         bool result = GetCurve().DecodePoint(G, ssG, (size_t)ssG.MaxRetrievable());
    00388         SetSubgroupGenerator(G);
    00389         assert(result);
    00390
    00391         StringSource ssN(param.n, true, new HexDecoder);
    00392         m_n.Decode(ssN, (size_t)ssN.MaxRetrievable());
    00393         m_k = param.h;
    00394 }
    00395
    00396 template <class EC>
    00397 bool DL_GroupParameters_EC<EC>::GetVoidValue(const char *name, const std::type_info &valueType, void *pValue) const
    00398 {
    00399         if (strcmp(name, Name::GroupOID()) == 0)
    00400         {
    00401                 if (m_oid.m_values.empty())
    00402                         return false;
    00403
    00404                 this->ThrowIfTypeMismatch(name, typeid(OID), valueType);
    00405                 *reinterpret_cast<OID *>(pValue) = m_oid;
    00406                 return true;
    00407         }
    00408         else
    00409                 return GetValueHelper<DL_GroupParameters<Element> >(this, name, valueType, pValue).Assignable()
    00410                         CRYPTOPP_GET_FUNCTION_ENTRY(Curve);
    00411 }
    00412
    00413 template <class EC>
    00414 void DL_GroupParameters_EC<EC>::AssignFrom(const NameValuePairs &source)
    00415 {
    00416         OID oid;
    00417         if (source.GetValue(Name::GroupOID(), oid))
    00418                 Initialize(oid);
    00419         else
    00420         {
    00421                 EllipticCurve ec;
    00422                 Point G;
    00423                 Integer n;
    00424
    00425                 source.GetRequiredParameter("DL_GroupParameters_EC<EC>", Name::Curve(), ec);
    00426                 source.GetRequiredParameter("DL_GroupParameters_EC<EC>", Name::SubgroupGenerator(), G);
    00427                 source.GetRequiredParameter("DL_GroupParameters_EC<EC>", Name::SubgroupOrder(), n);
    00428                 Integer k = source.GetValueWithDefault(Name::Cofactor(), Integer::Zero());
    00429
    00430                 Initialize(ec, G, n, k);
    00431         }
    00432 }
    00433
    00434 template <class EC>
    00435 void DL_GroupParameters_EC<EC>::GenerateRandom(RandomNumberGenerator &rng, const NameValuePairs &alg)
    00436 {
    00437         try
    00438         {
    00439                 AssignFrom(alg);
    00440         }
    00441         catch (InvalidArgument &)
    00442         {
    00443                 throw NotImplemented("DL_GroupParameters_EC<EC>: curve generation is not implemented yet");
    00444         }
    00445 }
    00446
    00447 template <class EC>
    00448 void DL_GroupParameters_EC<EC>::BERDecode(BufferedTransformation &bt)
    00449 {
    00450         byte b;
    00451         if (!bt.Peek(b))
    00452                 BERDecodeError();
    00453         if (b == OBJECT_IDENTIFIER)
    00454                 Initialize(OID(bt));
    00455         else
    00456         {
    00457                 BERSequenceDecoder seq(bt);
    00458                         word32 version;
    00459                         BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1); // check version
    00460                         EllipticCurve ec(seq);
    00461                         Point G = ec.BERDecodePoint(seq);
    00462                         Integer n(seq);
    00463                         Integer k;
    00464                         bool cofactorPresent = !seq.EndReached();
    00465                         if (cofactorPresent)
    00466                                 k.BERDecode(seq);
    00467                         else
    00468                                 k = Integer::Zero();
    00469                 seq.MessageEnd();
    00470
    00471                 Initialize(ec, G, n, k);
    00472         }
    00473 }
    00474
    00475 template <class EC>
    00476 void DL_GroupParameters_EC<EC>::DEREncode(BufferedTransformation &bt) const
    00477 {
    00478         if (m_encodeAsOID && !m_oid.m_values.empty())
    00479                 m_oid.DEREncode(bt);
    00480         else
    00481         {
    00482                 DERSequenceEncoder seq(bt);
    00483                 DEREncodeUnsigned<word32>(seq, 1);      // version
    00484                 GetCurve().DEREncode(seq);
    00485                 GetCurve().DEREncodePoint(seq, this->GetSubgroupGenerator(), m_compress);
    00486                 m_n.DEREncode(seq);
    00487                 if (m_k.NotZero())
    00488                         m_k.DEREncode(seq);
    00489                 seq.MessageEnd();
    00490         }
    00491 }
    00492
    00493 template <class EC>
    00494 Integer DL_GroupParameters_EC<EC>::GetCofactor() const
    00495 {
    00496         if (!m_k)
    00497         {
    00498                 Integer q = GetCurve().FieldSize();
    00499                 Integer qSqrt = q.SquareRoot();
    00500                 m_k = (q+2*qSqrt+1)/m_n;
    00501         }
    00502
    00503         return m_k;
    00504 }
    00505
    00506 template <class EC>
    00507 Integer DL_GroupParameters_EC<EC>::ConvertElementToInteger(const Element &element) const
    00508 {
    00509         return ConvertToInteger(element.x);
    00510 };
    00511
    00512 template <class EC>
    00513 bool DL_GroupParameters_EC<EC>::ValidateGroup(RandomNumberGenerator &rng, unsigned int level) const
    00514 {
    00515         bool pass = GetCurve().ValidateParameters(rng, level);
    00516
    00517         Integer q = GetCurve().FieldSize();
    00518         pass = pass && m_n!=q;
    00519
    00520         if (level >= 2)
    00521         {
    00522                 Integer qSqrt = q.SquareRoot();
    00523                 pass = pass && m_n>4*qSqrt;
    00524                 pass = pass && VerifyPrime(rng, m_n, level-2);
    00525                 pass = pass && (m_k.IsZero() || m_k == (q+2*qSqrt+1)/m_n);
    00526                 pass = pass && CheckMOVCondition(q, m_n);
    00527         }
    00528
    00529         return pass;
    00530 }
    00531
    00532 template <class EC>
    00533 bool DL_GroupParameters_EC<EC>::ValidateElement(unsigned int level, const Element &g, const DL_FixedBasePrecomputation<Element> *gpc) const
    00534 {
    00535         bool pass = !IsIdentity(g) && GetCurve().VerifyPoint(g);
    00536         if (level >= 1)
    00537         {
    00538                 if (gpc)
    00539                         pass = pass && gpc->Exponentiate(this->GetGroupPrecomputation(), Integer::One()) == g;
    00540         }
    00541         if (level >= 2 && pass)
    00542         {
    00543                 const Integer &q = GetSubgroupOrder();
    00544                 Element gq = gpc ? gpc->Exponentiate(this->GetGroupPrecomputation(), q) : ExponentiateElement(g, q);
    00545                 pass = pass && IsIdentity(gq);
    00546         }
    00547         return pass;
    00548 }
    00549
    00550 template <class EC>
    00551 void DL_GroupParameters_EC<EC>::SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
    00552 {
    00553         GetCurve().SimultaneousMultiply(results, base, exponents, exponentsCount);
    00554 }
    00555
    00556 template <class EC>
    00557 CPP_TYPENAME DL_GroupParameters_EC<EC>::Element DL_GroupParameters_EC<EC>::MultiplyElements(const Element &a, const Element &b) const
    00558 {
    00559         return GetCurve().Add(a, b);
    00560 }
    00561
    00562 template <class EC>
    00563 CPP_TYPENAME DL_GroupParameters_EC<EC>::Element DL_GroupParameters_EC<EC>::CascadeExponentiate(const Element &element1, const Integer &exponent1, const Element &element2, const Integer &exponent2) const
    00564 {
    00565         return GetCurve().CascadeMultiply(exponent1, element1, exponent2, element2);
    00566 }
    00567
    00568 template <class EC>
    00569 OID DL_GroupParameters_EC<EC>::GetAlgorithmID() const
    00570 {
    00571         return ASN1::id_ecPublicKey();
    00572 }
    00573
    00574 // ******************************************************************
    00575
    00576 template <class EC>
    00577 void DL_PublicKey_EC<EC>::BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
    00578 {
    00579         typename EC::Point P;
    00580         if (!this->GetGroupParameters().GetCurve().DecodePoint(P, bt, size))
    00581                 BERDecodeError();
    00582         SetPublicElement(P);
    00583 }
    00584
    00585 template <class EC>
    00586 void DL_PublicKey_EC<EC>::DEREncodePublicKey(BufferedTransformation &bt) const
    00587 {
    00588         this->GetGroupParameters().GetCurve().EncodePoint(bt, this->GetPublicElement(), this->GetGroupParameters().GetPointCompression());
    00589 }
    00590
    00591 // ******************************************************************
    00592
    00593 template <class EC>
    00594 void DL_PrivateKey_EC<EC>::BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
    00595 {
    00596         BERSequenceDecoder seq(bt);
    00597                 word32 version;
    00598                 BERDecodeUnsigned<word32>(seq, version, INTEGER, 1, 1); // check version
    00599
    00600                 BERGeneralDecoder dec(seq, OCTET_STRING);
    00601                 if (!dec.IsDefiniteLength())
    00602                         BERDecodeError();
    00603                 Integer x;
    00604                 x.Decode(dec, (size_t)dec.RemainingLength());
    00605                 dec.MessageEnd();
    00606                 if (!parametersPresent && seq.PeekByte() != (CONTEXT_SPECIFIC | CONSTRUCTED | 0))
    00607                         BERDecodeError();
    00608                 if (!seq.EndReached() && seq.PeekByte() == (CONTEXT_SPECIFIC | CONSTRUCTED | 0))
    00609                 {
    00610                         BERGeneralDecoder parameters(seq, CONTEXT_SPECIFIC | CONSTRUCTED | 0);
    00611                         this->AccessGroupParameters().BERDecode(parameters);
    00612                         parameters.MessageEnd();
    00613                 }
    00614                 if (!seq.EndReached())
    00615                 {
    00616                         // skip over the public element
    00617                         SecByteBlock subjectPublicKey;
    00618                         unsigned int unusedBits;
    00619                         BERGeneralDecoder publicKey(seq, CONTEXT_SPECIFIC | CONSTRUCTED | 1);
    00620                         BERDecodeBitString(publicKey, subjectPublicKey, unusedBits);
    00621                         publicKey.MessageEnd();
    00622                         Element Q;
    00623                         if (!(unusedBits == 0 && this->GetGroupParameters().GetCurve().DecodePoint(Q, subjectPublicKey, subjectPublicKey.size())))
    00624                                 BERDecodeError();
    00625                 }
    00626         seq.MessageEnd();
    00627
    00628         this->SetPrivateExponent(x);
    00629 }
    00630
    00631 template <class EC>
    00632 void DL_PrivateKey_EC<EC>::DEREncodePrivateKey(BufferedTransformation &bt) const
    00633 {
    00634         DERSequenceEncoder privateKey(bt);
    00635                 DEREncodeUnsigned<word32>(privateKey, 1);       // version
    00636                 // SEC 1 ver 1.0 says privateKey (m_d) has the same length as order of the curve
    00637                 // this will be changed to order of base point in a future version
    00638                 this->GetPrivateExponent().DEREncodeAsOctetString(privateKey, this->GetGroupParameters().GetSubgroupOrder().ByteCount());
    00639         privateKey.MessageEnd();
    00640 }
    00641
    00642 NAMESPACE_END
    00643
    00644 #endif
    00645
    00646 #endif