~zooko/cryptopp/trunk

« back to all changes in this revision

Viewing changes to eccrypto.cpp

  • Committer: weidai
  • Date: 2010-06-18 01:52:34 UTC
  • Revision ID: svn-v4:57ff6487-cd31-0410-9ec3-f628ee90f5f0:trunk/c5:485
avoid SecBlock of arrays

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
 
44
44
static bool CheckMOVCondition(const Integer &q, const Integer &r)
45
45
{
46
 
        Integer t=1;
47
 
        unsigned int n=q.BitCount(), m=r.BitCount();
 
46
        // see "Updated standards for validating elliptic curves", http://eprint.iacr.org/2007/343
 
47
        Integer t = 1;
 
48
        unsigned int n = q.IsEven() ? 1 : q.BitCount(), m = r.BitCount();
48
49
 
49
50
        for (unsigned int i=n; DiscreteLogWorkFactor(i)<m/2; i+=n)
50
51
        {
51
 
                t = (t*q)%r;
 
52
                if (q.IsEven())
 
53
                        t = (t+t)%r;
 
54
                else
 
55
                        t = (t*q)%r;
52
56
                if (t == 1)
53
57
                        return false;
54
58
        }
262
266
                        "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5",
263
267
                        "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551",
264
268
                        1),
 
269
                EcRecommendedParameters<ECP>(ASN1::brainpoolP160r1(),
 
270
                        "E95E4A5F737059DC60DFC7AD95B3D8139515620F",
 
271
                        "340E7BE2A280EB74E2BE61BADA745D97E8F7C300",
 
272
                        "1E589A8595423412134FAA2DBDEC95C8D8675E58",
 
273
                        "04BED5AF16EA3F6A4F62938C4631EB5AF7BDBCDBC31667CB477A1A8EC338F94741669C976316DA6321",
 
274
                        "E95E4A5F737059DC60DF5991D45029409E60FC09",
 
275
                        1),
 
276
                EcRecommendedParameters<ECP>(ASN1::brainpoolP192r1(),
 
277
                        "C302F41D932A36CDA7A3463093D18DB78FCE476DE1A86297",
 
278
                        "6A91174076B1E0E19C39C031FE8685C1CAE040E5C69A28EF",
 
279
                        "469A28EF7C28CCA3DC721D044F4496BCCA7EF4146FBF25C9",
 
280
                        "04C0A0647EAAB6A48753B033C56CB0F0900A2F5C4853375FD614B690866ABD5BB88B5F4828C1490002E6773FA2FA299B8F",
 
281
                        "C302F41D932A36CDA7A3462F9E9E916B5BE8F1029AC4ACC1",
 
282
                        1),
 
283
                EcRecommendedParameters<ECP>(ASN1::brainpoolP224r1(),
 
284
                        "D7C134AA264366862A18302575D1D787B09F075797DA89F57EC8C0FF",
 
285
                        "68A5E62CA9CE6C1C299803A6C1530B514E182AD8B0042A59CAD29F43",
 
286
                        "2580F63CCFE44138870713B1A92369E33E2135D266DBB372386C400B",
 
287
                        "040D9029AD2C7E5CF4340823B2A87DC68C9E4CE3174C1E6EFDEE12C07D58AA56F772C0726F24C6B89E4ECDAC24354B9E99CAA3F6D3761402CD",
 
288
                        "D7C134AA264366862A18302575D0FB98D116BC4B6DDEBCA3A5A7939F",
 
289
                        1),
 
290
                EcRecommendedParameters<ECP>(ASN1::brainpoolP256r1(),
 
291
                        "A9FB57DBA1EEA9BC3E660A909D838D726E3BF623D52620282013481D1F6E5377",
 
292
                        "7D5A0975FC2C3057EEF67530417AFFE7FB8055C126DC5C6CE94A4B44F330B5D9",
 
293
                        "26DC5C6CE94A4B44F330B5D9BBD77CBF958416295CF7E1CE6BCCDC18FF8C07B6",
 
294
                        "048BD2AEB9CB7E57CB2C4B482FFC81B7AFB9DE27E1E3BD23C23A4453BD9ACE3262547EF835C3DAC4FD97F8461A14611DC9C27745132DED8E545C1D54C72F046997",
 
295
                        "A9FB57DBA1EEA9BC3E660A909D838D718C397AA3B561A6F7901E0E82974856A7",
 
296
                        1),
 
297
                EcRecommendedParameters<ECP>(ASN1::brainpoolP320r1(),
 
298
                        "D35E472036BC4FB7E13C785ED201E065F98FCFA6F6F40DEF4F92B9EC7893EC28FCD412B1F1B32E27",
 
299
                        "3EE30B568FBAB0F883CCEBD46D3F3BB8A2A73513F5EB79DA66190EB085FFA9F492F375A97D860EB4",
 
300
                        "520883949DFDBC42D3AD198640688A6FE13F41349554B49ACC31DCCD884539816F5EB4AC8FB1F1A6",
 
301
                        "0443BD7E9AFB53D8B85289BCC48EE5BFE6F20137D10A087EB6E7871E2A10A599C710AF8D0D39E2061114FDD05545EC1CC8AB4093247F77275E0743FFED117182EAA9C77877AAAC6AC7D35245D1692E8EE1",
 
302
                        "D35E472036BC4FB7E13C785ED201E065F98FCFA5B68F12A32D482EC7EE8658E98691555B44C59311",
 
303
                        1),
 
304
                EcRecommendedParameters<ECP>(ASN1::brainpoolP384r1(),
 
305
                        "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B412B1DA197FB71123ACD3A729901D1A71874700133107EC53",
 
306
                        "7BC382C63D8C150C3C72080ACE05AFA0C2BEA28E4FB22787139165EFBA91F90F8AA5814A503AD4EB04A8C7DD22CE2826",
 
307
                        "04A8C7DD22CE28268B39B55416F0447C2FB77DE107DCD2A62E880EA53EEB62D57CB4390295DBC9943AB78696FA504C11",
 
308
                        "041D1C64F068CF45FFA2A63A81B7C13F6B8847A3E77EF14FE3DB7FCAFE0CBD10E8E826E03436D646AAEF87B2E247D4AF1E8ABE1D7520F9C2A45CB1EB8E95CFD55262B70B29FEEC5864E19C054FF99129280E4646217791811142820341263C5315",
 
309
                        "8CB91E82A3386D280F5D6F7E50E641DF152F7109ED5456B31F166E6CAC0425A7CF3AB6AF6B7FC3103B883202E9046565",
 
310
                        1),
 
311
                EcRecommendedParameters<ECP>(ASN1::brainpoolP512r1(),
 
312
                        "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA703308717D4D9B009BC66842AECDA12AE6A380E62881FF2F2D82C68528AA6056583A48F3",
 
313
                        "7830A3318B603B89E2327145AC234CC594CBDD8D3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CA",
 
314
                        "3DF91610A83441CAEA9863BC2DED5D5AA8253AA10A2EF1C98B9AC8B57F1117A72BF2C7B9E7C1AC4D77FC94CADC083E67984050B75EBAE5DD2809BD638016F723",
 
315
                        "0481AEE4BDD82ED9645A21322E9C4C6A9385ED9F70B5D916C1B43B62EEF4D0098EFF3B1F78E2D0D48D50D1687B93B97D5F7C6D5047406A5E688B352209BCB9F8227DDE385D566332ECC0EABFA9CF7822FDF209F70024A57B1AA000C55B881F8111B2DCDE494A5F485E5BCA4BD88A2763AED1CA2B2FA8F0540678CD1E0F3AD80892",
 
316
                        "AADD9DB8DBE9C48B3FD4E6AE33C9FC07CB308DB3B3C9D20ED6639CCA70330870553E5C414CA92619418661197FAC10471DB1D381085DDADDB58796829CA90069",
 
317
                        1),
265
318
                EcRecommendedParameters<ECP>(ASN1::secp112r1(),
266
319
                        "DB7C2ABF62E35E668076BEAD208B",
267
320
                        "DB7C2ABF62E35E668076BEAD2088",
535
588
                if (gpc)
536
589
                        pass = pass && gpc->Exponentiate(this->GetGroupPrecomputation(), Integer::One()) == g;
537
590
        }
538
 
        if (level >= 2)
 
591
        if (level >= 2 && pass)
539
592
        {
540
593
                const Integer &q = GetSubgroupOrder();
541
 
                pass = pass && IsIdentity(gpc ? gpc->Exponentiate(this->GetGroupPrecomputation(), q) : ExponentiateElement(g, q));
 
594
                Element gq = gpc ? gpc->Exponentiate(this->GetGroupPrecomputation(), q) : ExponentiateElement(g, q);
 
595
                pass = pass && IsIdentity(gq);
542
596
        }
543
597
        return pass;
544
598
}
570
624
// ******************************************************************
571
625
 
572
626
template <class EC>
573
 
void DL_PublicKey_EC<EC>::BERDecodeKey2(BufferedTransformation &bt, bool parametersPresent, size_t size)
 
627
void DL_PublicKey_EC<EC>::BERDecodePublicKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
574
628
{
575
629
        typename EC::Point P;
576
630
        if (!this->GetGroupParameters().GetCurve().DecodePoint(P, bt, size))
579
633
}
580
634
 
581
635
template <class EC>
582
 
void DL_PublicKey_EC<EC>::DEREncodeKey(BufferedTransformation &bt) const
 
636
void DL_PublicKey_EC<EC>::DEREncodePublicKey(BufferedTransformation &bt) const
583
637
{
584
638
        this->GetGroupParameters().GetCurve().EncodePoint(bt, this->GetPublicElement(), this->GetGroupParameters().GetPointCompression());
585
639
}
587
641
// ******************************************************************
588
642
 
589
643
template <class EC>
590
 
void DL_PrivateKey_EC<EC>::BERDecodeKey2(BufferedTransformation &bt, bool parametersPresent, size_t size)
 
644
void DL_PrivateKey_EC<EC>::BERDecodePrivateKey(BufferedTransformation &bt, bool parametersPresent, size_t size)
591
645
{
592
646
        BERSequenceDecoder seq(bt);
593
647
                word32 version;
625
679
}
626
680
 
627
681
template <class EC>
628
 
void DL_PrivateKey_EC<EC>::DEREncodeKey(BufferedTransformation &bt) const
 
682
void DL_PrivateKey_EC<EC>::DEREncodePrivateKey(BufferedTransformation &bt) const
629
683
{
630
684
        DERSequenceEncoder privateKey(bt);
631
685
                DEREncodeUnsigned<word32>(privateKey, 1);       // version