⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test.cpp

📁 SSL加密库源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
        0x4f,0x0f,0xeb,0xf3,0xfb,0x5f,0x86,0x73,
        0x15,0x85,0xb3,0x22,0x4b,0x86,0x2b,0x4b
    };

    if (memcmp(cipher, verify2, sz))
        return -53;

    // EDE3 CBC mode
    DES_EDE3_CBC_Encryption enc3;
    DES_EDE3_CBC_Decryption dec3;

    const byte key3[] = 
    {
        0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
        0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
        0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
    };
    const byte iv3[] = 
    {
        0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
        0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
        0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
        
    };

    enc3.SetKey(key3, sizeof(key3), iv3);
    enc3.Process(cipher, msg, sz);
    dec3.SetKey(key3, sizeof(key3), iv3);
    dec3.Process(plain, cipher, sz);

    if (memcmp(plain, msg, sz))
        return -54;

    const byte verify3[] = 
    {
        0x08,0x8a,0xae,0xe6,0x9a,0xa9,0xc1,0x13,
        0x93,0x7d,0xf7,0x3a,0x11,0x56,0x66,0xb3,
        0x18,0xbc,0xbb,0x6d,0xd2,0xb1,0x16,0xda
    };

    if (memcmp(cipher, verify3, sz))
        return -55;

    return 0;
}


int aes_test()
{
    AES_CBC_Encryption enc;
    AES_CBC_Decryption dec;
    const int bs(TaoCrypt::AES::BLOCK_SIZE);

    byte key[] = "0123456789abcdef   ";  // align
    byte iv[]  = "1234567890abcdef   ";  // align

    enc.SetKey(key, bs, iv);
    dec.SetKey(key, bs, iv);

    enc.Process(cipher, msg, bs);
    dec.Process(plain, cipher, bs);

    if (memcmp(plain, msg, bs))
        return -60;

    const byte verify[] = 
    {
        0x95,0x94,0x92,0x57,0x5f,0x42,0x81,0x53,
        0x2c,0xcc,0x9d,0x46,0x77,0xa2,0x33,0xcb
    };

    if (memcmp(cipher, verify, bs))
        return -61;

    AES_ECB_Encryption enc2;
    AES_ECB_Decryption dec2;

    enc2.SetKey(key, bs, iv);
    dec2.SetKey(key, bs, iv);

    enc2.Process(cipher, msg, bs);
    dec2.Process(plain, cipher, bs);

    if (memcmp(plain, msg, bs))
        return -62;

    const byte verify2[] = 
    {
        0xd0,0xc9,0xd9,0xc9,0x40,0xe8,0x97,0xb6,
        0xc8,0x8c,0x33,0x3b,0xb5,0x8f,0x85,0xd1
    };

    if (memcmp(cipher, verify2, bs))
        return -63;

    return 0;
}


int twofish_test()
{
    Twofish_CBC_Encryption enc;
    Twofish_CBC_Decryption dec;
    const int bs(TaoCrypt::Twofish::BLOCK_SIZE);

    byte key[] = "0123456789abcdef   ";  // align
    byte iv[]  = "1234567890abcdef   ";  // align

    enc.SetKey(key, bs, iv);
    dec.SetKey(key, bs, iv);

    enc.Process(cipher, msg, bs);
    dec.Process(plain, cipher, bs);

    if (memcmp(plain, msg, bs))
        return -60;

    const byte verify[] = 
    {
        0xD2,0xD7,0x47,0x47,0x4A,0x65,0x4E,0x16,
        0x21,0x03,0x58,0x79,0x5F,0x02,0x27,0x2C
    };

    if (memcmp(cipher, verify, bs))
        return -61;

    Twofish_ECB_Encryption enc2;
    Twofish_ECB_Decryption dec2;

    enc2.SetKey(key, bs, iv);
    dec2.SetKey(key, bs, iv);

    enc2.Process(cipher, msg, bs);
    dec2.Process(plain, cipher, bs);

    if (memcmp(plain, msg, bs))
        return -62;

    const byte verify2[] = 
    {
        0x3B,0x6C,0x63,0x10,0x34,0xAB,0xB2,0x87,
        0xC4,0xCD,0x6B,0x91,0x14,0xC5,0x3A,0x09
    };

    if (memcmp(cipher, verify2, bs))
        return -63;

    return 0;
}


int blowfish_test()
{
    Blowfish_CBC_Encryption enc;
    Blowfish_CBC_Decryption dec;
    const int bs(TaoCrypt::Blowfish::BLOCK_SIZE);

    byte key[] = "0123456789abcdef   ";  // align
    byte iv[]  = "1234567890abcdef   ";  // align

    enc.SetKey(key, 16, iv);
    dec.SetKey(key, 16, iv);

    enc.Process(cipher, msg, bs * 2);
    dec.Process(plain, cipher, bs * 2);

    if (memcmp(plain, msg, bs))
        return -60;

    const byte verify[] = 
    {
        0x0E,0x26,0xAA,0x29,0x11,0x25,0xAB,0xB5,
        0xBC,0xD9,0x08,0xC4,0x94,0x6C,0x89,0xA3
    };

    if (memcmp(cipher, verify, bs))
        return -61;

    Blowfish_ECB_Encryption enc2;
    Blowfish_ECB_Decryption dec2;

    enc2.SetKey(key, 16, iv);
    dec2.SetKey(key, 16, iv);

    enc2.Process(cipher, msg, bs * 2);
    dec2.Process(plain, cipher, bs * 2);

    if (memcmp(plain, msg, bs))
        return -62;

    const byte verify2[] = 
    {
        0xE7,0x42,0xB9,0x37,0xC8,0x7D,0x93,0xCA,
        0x8F,0xCE,0x39,0x32,0xDE,0xD7,0xBC,0x5B
    };

    if (memcmp(cipher, verify2, bs))
        return -63;

    return 0;
}


int rsa_test()
{
    Source source;
    FileSource("../certs/client-key.der", source);
    if (source.size() == 0) {
        FileSource("../../certs/client-key.der", source);  // for testsuite
        if (source.size() == 0) {
            FileSource("../../../certs/client-key.der", source); // Debug dir
            if (source.size() == 0)
                err_sys("where's your certs dir?", -79);
        }
    }
    RSA_PrivateKey priv(source);

    RSAES_Encryptor enc(priv);
    byte message[] = "Everyone gets Friday off.";
    const word32 len = (word32)strlen((char*)message);
    byte cipher[64];
    enc.Encrypt(message, len, cipher, rng);

    RSAES_Decryptor dec(priv);
    byte plain[64];
    dec.Decrypt(cipher, sizeof(plain), plain, rng);

    if (memcmp(plain, message, len))
        return -70;

    dec.SSL_Sign(message, len, cipher, rng);
    if (!enc.SSL_Verify(message, len, cipher))
        return -71;


    // test decode   
    Source source2;
    FileSource("../certs/client-cert.der", source2);
    if (source2.size() == 0) {
        FileSource("../../certs/client-cert.der", source2);  // for testsuite
        if (source2.size() == 0) {
            FileSource("../../../certs/client-cert.der", source2); // Debug dir
            if (source2.size() == 0)
                err_sys("where's your certs dir?", -79);
        }
    }
    CertDecoder cd(source2, true, 0, false, CertDecoder::CA);
    if (cd.GetError().What())
        err_sys("cert error", -80);
    Source source3(cd.GetPublicKey().GetKey(), cd.GetPublicKey().size());
    RSA_PublicKey pub(source3);
 
    return 0;
}


int dh_test()
{
    Source source;
    FileSource("../certs/dh1024.dat", source);
    if (source.size() == 0) {
        FileSource("../../certs/dh1024.dat", source);  // for testsuite
        if (source.size() == 0) {
            FileSource("../../../certs/dh1024.dat", source); // win32 Debug dir
            if (source.size() == 0)
                err_sys("where's your certs dir?", -79);
        }
    }
    HexDecoder hDec(source);

    DH dh(source);

    byte pub[128];
    byte priv[128];
    byte agree[128];
    byte pub2[128];
    byte priv2[128];
    byte agree2[128];

    DH dh2(dh);

    dh.GenerateKeyPair(rng, priv, pub);
    dh2.GenerateKeyPair(rng, priv2, pub2);
    dh.Agree(agree, priv, pub2); 
    dh2.Agree(agree2, priv2, pub);

    
    if ( memcmp(agree, agree2, dh.GetByteLength()) )
        return -80;

    return 0;
}


int dsa_test()
{
    Source source;
    FileSource("../certs/dsa512.der", source);
    if (source.size() == 0) {
        FileSource("../../certs/dsa512.der", source);  // for testsuite
        if (source.size() == 0) {
            FileSource("../../../certs/dsa512.der", source); // win32 Debug dir
            if (source.size() == 0)
                err_sys("where's your certs dir?", -89);
        }
    }

    const char msg[] = "this is the message";
    byte signature[40];

    DSA_PrivateKey priv(source);
    DSA_Signer signer(priv);

    SHA sha;
    byte digest[SHA::DIGEST_SIZE];
    sha.Update((byte*)msg, sizeof(msg));
    sha.Final(digest);

    signer.Sign(digest, signature, rng);

    byte encoded[sizeof(signature) + 6];
    byte decoded[40];

    word32 encSz = EncodeDSA_Signature(signer.GetR(), signer.GetS(), encoded);
    DecodeDSA_Signature(decoded, encoded, encSz);

    DSA_PublicKey pub(priv);
    DSA_Verifier verifier(pub);

    if (!verifier.Verify(digest, decoded))
        return -90;

    return 0;
}


int pwdbased_test()
{
    PBKDF2_HMAC<SHA> pb;

    byte derived[32];
    const byte pwd1[] = "password   ";  // align
    const byte salt[]  = { 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 };
    
    pb.DeriveKey(derived, 8, pwd1, 8, salt, sizeof(salt), 5);

    const byte verify1[] = { 0xD1, 0xDA, 0xA7, 0x86, 0x15, 0xF2, 0x87, 0xE6 };

    if ( memcmp(derived, verify1, sizeof(verify1)) )
        return -101;


    const byte pwd2[] = "All n-entities must communicate with other n-entities"
                        " via n-1 entiteeheehees   ";  // align

    pb.DeriveKey(derived, 24, pwd2, 76, salt, sizeof(salt), 500);

    const byte verify2[] = { 0x6A, 0x89, 0x70, 0xBF, 0x68, 0xC9, 0x2C, 0xAE,
                             0xA8, 0x4A, 0x8D, 0xF2, 0x85, 0x10, 0x85, 0x86,
                             0x07, 0x12, 0x63, 0x80, 0xCC, 0x47, 0xAB, 0x2D
    };

    if ( memcmp(derived, verify2, sizeof(verify2)) )
        return -102;

    return 0;
}


int pkcs12_test()
{
    Source cert;
    FileSource("../certs/server-cert.pem", cert);
    if (cert.size() == 0) {
        FileSource("../../certs/server-cert.pem", cert);  // for testsuite
        if (cert.size() == 0) {
            FileSource("../../../certs/server-cert.pem", cert); // Debug dir
            if (cert.size() == 0)
                err_sys("where's your certs dir?", -109);
        }
    }

    if (GetCert(cert) != 0)
        return -110;

    Source source;
    FileSource("../certs/server.p12", source);
    if (source.size() == 0) {
        FileSource("../../certs/server.p12", source);  // for testsuite
        if (source.size() == 0) {
            FileSource("../../../certs/server.p12", source); // Debug dir
            if (source.size() == 0)
                err_sys("where's your certs dir?", -111);
        }
    }

    if (GetPKCS_Cert("password", source) != 0)
        return -112;

    return 0;
}

⌨️ 快捷键说明

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