validate.cpp

来自「各种加密算法的源代码」· C++ 代码 · 共 910 行 · 第 1/2 页

CPP
910
字号
    fail=0;
    delete rc4;

    return pass;
}

boolean RC5Validate()
{
    cout << "\nRC5 validation suite running...\n\n";

    FileSource valdata("rc5val.dat", TRUE, new HexDecoder);
    HexEncoder output(new FileSink(cout));
    byte plain[8], cipher[8], out[8], outplain[8];
    byte key[16];
    boolean pass=TRUE, fail;

    while (valdata.MaxRetrieveable() >= 32)
    {
        valdata.Get(key, 16);
        valdata.Get(plain, 8);
        valdata.Get(cipher, 8);

        BlockTransformation *rc5 = new RC5Encryption(key);
        rc5->ProcessBlock(plain, out);
        delete rc5;
        fail=memcmp(out, cipher, 8);

        rc5 = new RC5Decryption(key);
        rc5->ProcessBlock(out, outplain);
        delete rc5;
        fail=fail || memcmp(outplain, plain, 8);

        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        output.Put(key, 16);
        cout << "  ";
        output.Put(outplain, 8);
        cout << "  ";
        output.Put(out, 8);
        cout << endl;
    }
    return pass;
}

boolean BlowfishValidate()
{
    cout << "\nBlowfish validation suite running...\n\n";

    HexEncoder output(new FileSink(cout));
    char *key[]={"abcdefghijklmnopqrstuvwxyz", "Who is John Galt?"};
    byte *plain[]={(byte *)"BLOWFISH", (byte *)"\xfe\xdc\xba\x98\x76\x54\x32\x10"};
    byte *cipher[]={(byte *)"\x32\x4e\xd0\xfe\xf4\x13\xa2\x03", (byte *)"\xcc\x91\x73\x2b\x80\x22\xf6\x84"};
    byte out[8], outplain[8];
    boolean pass=TRUE, fail;

    for (int i=0; i<2; i++)
    {
        BlockTransformation *bf = new BlowfishEncryption((byte *)key[i], strlen(key[i]));
        bf->ProcessBlock(plain[i], out);
        delete bf;
        fail = memcmp(out, cipher[i], 8);

        bf = new BlowfishDecryption((byte *)key[i], strlen(key[i]));
        bf->ProcessBlock(cipher[i], outplain);
        delete bf;
        fail = fail || memcmp(outplain, plain[i], 8);
        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        cout << '\"' << key[i] << '\"';
        for (int j=0; j<(30-strlen(key[i])); j++)
            cout << ' ';
        output.Put(outplain, 8);
        cout << "  ";
        output.Put(out, 8);
        cout << endl;
    }
    return pass;
}

boolean DiamondValidate()
{
    cout << "\nDiamond validation suite running...\n\n";

    FileSource valdata("diamond.dat", TRUE, new HexDecoder);
    HexEncoder output(new FileSink(cout));
    byte key[32], plain[16], cipher[16], out[16], outplain[16];
    byte blocksize, rounds, keysize;
    boolean pass=TRUE, fail;

    while (valdata.MaxRetrieveable() >= 1)
    {
        valdata.Get(blocksize);
        valdata.Get(rounds);
        valdata.Get(keysize);
        valdata.Get(key, keysize);
        valdata.Get(plain, blocksize);
        valdata.Get(cipher, blocksize);

        BlockTransformation *diamond;
        if (blocksize==16)
            diamond = new DiamondEncryption(key, keysize, rounds);
        else
            diamond = new DiamondLiteEncryption(key, keysize, rounds);

        diamond->ProcessBlock(plain, out);
        delete diamond;
        fail=memcmp(out, cipher, blocksize);

        if (blocksize==16)
            diamond = new DiamondDecryption(key, keysize, rounds);
        else
            diamond = new DiamondLiteDecryption(key, keysize, rounds);

        diamond->ProcessBlock(out, outplain);
        delete diamond;
        fail=fail || memcmp(outplain, plain, blocksize);

        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        output.Put(key, keysize);
        cout << "\n          ";
        output.Put(outplain, blocksize);
        cout << "  ";
        output.Put(out, blocksize);
        cout << endl;
    }
    return pass;
}

boolean BBSValidate()
{
    cout << "\nBlumBlumShub validation suite running...\n\n";

    bignum p("212004934506826557583707108431463840565872545889679278744389317666981496005411448865750399674653351");
    bignum q("100677295735404212434355574418077394581488455772477016953458064183204108039226017738610663984508231");
    bignum seed("63239752671357255800299643604761065219897634268887145610573595874544114193025997412441121667211431");
    BlumBlumShub bbs(p, q, seed);
    boolean pass = TRUE, fail;

    const byte output1[] = {
        0x49,0xEA,0x2C,0xFD,0xB0,0x10,0x64,0xA0,0xBB,0xB9,
        0x2A,0xF1,0x01,0xDA,0xC1,0x8A,0x94,0xF7,0xB7,0xCE};
    const byte output2[] = {
        0x74,0x45,0x48,0xAE,0xAC,0xB7,0x0E,0xDF,0xAF,0xD7,
        0xD5,0x0E,0x8E,0x29,0x83,0x75,0x6B,0x27,0x46,0xA1};

    byte buf[20];

    bbs.GetBlock(buf, 20);
    fail = memcmp(output1, buf, 20);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    for (int j=0;j<20;j++)
        cout << setw(2) << setfill('0') << hex << (int)buf[j];
    cout << endl;

    bbs.Seek(10);
    bbs.GetBlock(buf, 10);
    fail = memcmp(output1+10, buf, 10);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    for (j=0;j<10;j++)
        cout << setw(2) << setfill('0') << hex << (int)buf[j];
    cout << endl;

    bbs.Seek(1234567);
    bbs.GetBlock(buf, 20);
    fail = memcmp(output2, buf, 20);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    for (j=0;j<20;j++)
        cout << setw(2) << setfill('0') << hex << (int)buf[j];
    cout << endl;

    return pass;
}

boolean DHValidate()
{
    cout << "\nDH validation suite running...\n\n";

    DH dh1(FileSource("dhparams.dat", TRUE, new HexDecoder()));
#ifdef USE_RSAREF
    RSAREF_DH dh2(FileSource("dhparams.dat", TRUE, new HexDecoder()));
#else
    DH dh2(FileSource("dhparams.dat", TRUE, new HexDecoder()));
#endif

    SecByteBlock pub1(dh1.OutputLength()), pub2(dh2.OutputLength());
    SecByteBlock key1(dh1.OutputLength()), key2(dh2.OutputLength());

    LC_RNG rng(5234);
    dh1.Setup(rng, pub1);
    dh2.Setup(rng, pub2);

    dh1.Agree(pub2, key1);
    dh2.Agree(pub1, key2);

    if (memcmp(key1, key2, dh1.OutputLength()))
    {
        cout << "FAILED    keys not equal" << endl;
        return FALSE;
    }
    else
    {
        cout << "PASSED    keys agreed" << endl;
        return TRUE;
    }
}

boolean RSAValidate()
{
    cout << "\nRSA validation suite running...\n\n";

    byte *plain = (byte *)
        "\x30\x20\x30\x0c\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x02\x05\x00\x04"
        "\x10\x1d\x32\xde\x00\x9f\x9c\x56\xea\x46\x36\xd3\x9a\xaf\xfd\xae\xa1";
    byte *privCipher = (byte *)
        "\x05\xfa\x6a\x81\x2f\xc7\xdf\x8b\xf4\xf2\x54\x25\x09\xe0\x3e\x84"
        "\x6e\x11\xb9\xc6\x20\xbe\x20\x09\xef\xb4\x40\xef\xbc\xc6\x69\x21"
        "\x69\x94\xac\x04\xf3\x41\xb5\x7d\x05\x20\x2d\x42\x8f\xb2\xa2\x7b"
        "\x5c\x77\xdf\xd9\xb1\x5b\xfc\x3d\x55\x93\x53\x50\x34\x10\xc1\xe1";
    byte out[100], outPlain[100];
    unsigned int outLen;
    LC_RNG rng(765);
    boolean pass = TRUE, fail;

#ifdef THROW_EXCEPTIONS
    try
#endif
    {
        FileSource keys("rsakey.dat", TRUE, new HexDecoder);
        RSAPrivateKey rsaPriv(keys);
        RSAPublicKey rsaPub(rsaPriv);

        rsaPriv.Encrypt(rng, plain, 34, out);
        fail = memcmp(privCipher, out, 64);
        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        cout << "RSA private key encryption\n";

        outLen = rsaPub.Decrypt(privCipher, outPlain);
        fail = (outLen!=34) || memcmp(plain, outPlain, 34);
        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        cout << "RSA public key decryption\n";

        rsaPub.Encrypt(rng, plain, 34, out);
        memset(outPlain, 0, 34);
        outLen = rsaPriv.Decrypt(out, outPlain);
        fail = (outLen!=34) || memcmp(plain, outPlain, 34);
        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        cout << "RSA public key encryption and private key decryption\n";
    }
#ifdef THROW_EXCEPTIONS
    catch (BERDecodeErr)
    {
        cout << "FAILED    Error decoding RSA key\n";
        pass = FALSE;
    }
#endif

#ifdef USE_RSAREF

#ifdef THROW_EXCEPTIONS
    try
#endif
    {
        FileSource keys("rsakey.dat", TRUE, new HexDecoder);
        RSAREFPrivateKey rsaPriv(keys);
        RSAREFPublicKey rsaPub(rsaPriv);

        rsaPriv.Encrypt(rng, plain, 34, out);
        fail = memcmp(privCipher, out, 64);
        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        cout << "RSAREF private key encryption\n";

        outLen = rsaPub.Decrypt(privCipher, outPlain);
        fail = (outLen!=34) || memcmp(plain, outPlain, 34);
        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        cout << "RSAREF public key decryption\n";

        rsaPub.Encrypt(rng, plain, 34, out);
        memset(outPlain, 0, 34);
        outLen = rsaPriv.Decrypt(out, outPlain);
        fail = (outLen!=34) || memcmp(plain, outPlain, 34);
        pass = pass && !fail;

        cout << (fail ? "FAILED    " : "PASSED    ");
        cout << "RSAREF public key encryption and private key decryption\n";
    }
#ifdef THROW_EXCEPTIONS
    catch (BERDecodeErr)
    {
        cout << "FAILED    Error decoding RSAREF key\n";
        pass = FALSE;
    }
#endif

#endif  // USE_RSAREF

    return pass;
}

boolean ElGamalValidate()
{
    cout << "\nElGamal validation suite running...\n\n";

    ElGamalPrivateKey priv(FileSource("elgamal.dat", TRUE, new HexDecoder));
    ElGamalPublicKey pub(priv);
    LC_RNG rng(9374);
    const byte *message = (byte *)"test message";
    const int messageLen = 12;
    byte buffer[256];
    boolean pass = TRUE, fail;

    priv.Sign(rng, message, messageLen, buffer);
    fail = !pub.Verify(message, messageLen, buffer);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    cout << "signature and verification\n";

    fail = pub.Verify(message, messageLen-1, buffer);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    cout << "checking invalid signature\n";

    pub.Encrypt(rng, message, messageLen, buffer);
    fail = (messageLen!=priv.Decrypt(buffer, buffer));
    fail = fail | memcmp(message, buffer, messageLen);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    cout << "encryption and decryption\n";

    return pass;
}

boolean DSAValidate()
{
    cout << "\nDSA validation suite running...\n\n";

    DSAPrivateKey priv(FileSource("dsakey.dat", TRUE, new HexDecoder()));
    DSAPublicKey pub(priv);

    byte seed[]="\xd5\x01\x4e\x4b\x60\xef\x2b\xa8\xb6\x21\x1b\x40\x62\xba\x32\x24\xe0\x42\x7d\xbd";
    bignum p("d411a4a0e393f6aab0f08b14d18458665b3e4dbdce254454"
             "3fe365cf71c8622412db6e7dd02bbe13d88c58d7263e9023"
             "6af17ac8a9fe5f249cc81f427fc543f7H");
    bignum q("b20db0b101df0c6624fc1392ba55f77d577481e5H");
    bignum k("79577ddcaafddc038b865b19f8eb1ada8a2838c6h");
    bignum h("0164b8a914cd2a5e74c4f7ff082c4d97f1edf880h");
    bignum r("9b77,f705,4c81,531c,4e46,a469,2fbf,e0f7,7f7e,bff2h");
    bignum s("95b4,f608,1f8f,890e,4b5a,199e,f10f,fe21,f52b,2d68h");

    bignum pGen, qGen, rOut, sOut;
    boolean pass = TRUE, fail;
    int c;

#ifdef NEW_SHS
    cout << "NEW_SHS defined.  Skipping prime generation test.\n";
#else
    fail = !GenerateDSAPrimes(seed, 160, c, pGen, 512, qGen);
    fail = fail || (pGen != p) || (qGen != q);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    cout << "prime generation test\n";
#endif

    priv.RawSign(k, h, rOut, sOut);
    fail = (rOut != r) || (sOut != s);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    cout << "signature test\n";

    fail = !pub.RawVerify(h, r, s);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    cout << "valid signature verification\n";

    fail = pub.RawVerify(h+1, r, s);
    pass = pass && !fail;

    cout << (fail ? "FAILED    " : "PASSED    ");
    cout << "invalid signature verification\n";

    return pass;
}

boolean ZKValidate()
{
    cout << "\nZero Knowledge validation suite running...\n\n";

    SimpleGraph g1(FileSource("graph.dat", TRUE, new HexDecoder));
    cout << "g1:\n" << g1;

    word16 g1_to_g2[20]={1, 19, 9, 0, 8, 11, 17, 5, 2, 7, 3, 14, 13, 16, 6, 4, 12, 18, 10, 15};
    cout << "g1_to_g2:";
    for (int j=0; j<20; j++)
        cout << j << '-' << g1_to_g2[j] << ' ';
    cout << endl;

    SimpleGraph g2(g1);
    g2.Permutate(g1_to_g2);
    cout << "g2:\n" << g2;

    LC_RNG rng(93753L);
    ZK_IsomorphismProver prover(g1, g1_to_g2, rng);
    ZK_IsomorphismVerifier verifier(g1, g2, rng);

    SimpleGraph h;
    word16 gi_to_h[20];
    int i;
    boolean pass=TRUE;

    for (j=0; j<40; j++)
    {
        prover.Setup(h);
        i=verifier.Setup(h);
        prover.Prove(i, gi_to_h);
        pass = verifier.Verify(gi_to_h) && pass;
    }

    cout << "data from last round:\n";
    cout << "i: " << i << endl;
    cout << "gi_to_h:";
    for (j=0; j<20; j++)
        cout << j << '-' << gi_to_h[j] << ' ';
    cout << endl;
    cout << "h:\n" << h << endl;

    cout << (pass ? "PASSED    " : "FAILED    ") << endl;
    return pass;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?