From 4d86037dd07e52b2e20cbeb7e2e4584baed0f74c Mon Sep 17 00:00:00 2001 From: anwleung Date: Wed, 24 Jan 2007 20:38:49 +0000 Subject: [PATCH] Added files need to test crypto libraries. These should also be used as examples of use git-svn-id: https://ceph.svn.sf.net/svnroot/ceph@1033 29311d96-e01e-0410-9327-a35deaab8ce9 --- .../aleung/security1/ceph/crypto/driver.cc | 153 ++++++++++++++++++ .../aleung/security1/ceph/crypto/esig1536.dat | 1 + .../aleung/security1/ceph/crypto/rsa1024.dat | 32 ++++ 3 files changed, 186 insertions(+) create mode 100644 branches/aleung/security1/ceph/crypto/driver.cc create mode 100644 branches/aleung/security1/ceph/crypto/esig1536.dat create mode 100644 branches/aleung/security1/ceph/crypto/rsa1024.dat diff --git a/branches/aleung/security1/ceph/crypto/driver.cc b/branches/aleung/security1/ceph/crypto/driver.cc new file mode 100644 index 0000000000000..fa1baab09c06c --- /dev/null +++ b/branches/aleung/security1/ceph/crypto/driver.cc @@ -0,0 +1,153 @@ +/****************************** + * Cryptographic library for Ceph. + * + * This class implements all of the cryptgraphic functions + * necessary to protect and secure Ceph. This includes + * on-wire protection, enforced access control, prevention + * of replay,M-in-M attcks, DDOS, etc... + * + * This library exports a flat cryptographic suite + * which exposes templated functions. Each function + * supports multiple + * + * Author: Andrew Leung Nov., 2006 + ******************************/ +#include"CryptoLib.h" +#include + +using namespace std; + +int main(int argc, char* argv[]) { + // message to hash + const byte* msg = (const byte*)"hash me"; + + // sha-1 + byte digest[CryptoLib::SHA1DIGESTSIZE]; + byte digestHex[2*CryptoLib::SHA1DIGESTSIZE]; + CryptoLib myCryptoLib; + myCryptoLib.sha1(msg,digest,strlen((const char*)msg)); + myCryptoLib.toHex(digest, digestHex, CryptoLib::SHA1DIGESTSIZE, 2*CryptoLib::SHA1DIGESTSIZE); + cout << "SHA-1 of " << msg << " is " << string((const char*)digestHex,2*CryptoLib::SHA1DIGESTSIZE) << endl; + + // sha-256 + byte digest256[CryptoLib::SHA256DIGESTSIZE]; + byte hex256[2*CryptoLib::SHA256DIGESTSIZE]; + myCryptoLib.sha256(msg, digest256, strlen((const char*)msg)); + myCryptoLib.toHex(digest256, hex256, CryptoLib::SHA256DIGESTSIZE, 2*CryptoLib::SHA256DIGESTSIZE); + cout << "SHA-256 of " << msg << " is " << string((const char*)hex256,2*CryptoLib::SHA256DIGESTSIZE) << endl; + + // sha-384 + byte digest384[CryptoLib::SHA384DIGESTSIZE]; + byte hex384[2*CryptoLib::SHA384DIGESTSIZE]; + myCryptoLib.sha384(msg, digest384, strlen((const char*)msg)); + myCryptoLib.toHex(digest384, hex384, CryptoLib::SHA384DIGESTSIZE, 2*CryptoLib::SHA384DIGESTSIZE); + cout << "SHA-384 of " << msg << " is " << string((const char*)hex384,2*CryptoLib::SHA384DIGESTSIZE) << endl; + + // sha-512 + byte digest512[CryptoLib::SHA512DIGESTSIZE]; + byte hex512[2*CryptoLib::SHA512DIGESTSIZE]; + myCryptoLib.sha512(msg, digest512, strlen((const char*)msg)); + myCryptoLib.toHex(digest512, hex512, CryptoLib::SHA512DIGESTSIZE, 2*CryptoLib::SHA512DIGESTSIZE); + cout << "SHA-512 of " << msg << " is " << string((const char*)hex512,2*CryptoLib::SHA512DIGESTSIZE) << endl; + + // md5 + byte digestmd5[CryptoLib::MD5DIGESTSIZE]; + byte hexmd5[2*CryptoLib::MD5DIGESTSIZE]; + myCryptoLib.md5(msg, digestmd5, strlen((const char*)msg)); + myCryptoLib.toHex(digestmd5, hexmd5, CryptoLib::MD5DIGESTSIZE, 2*CryptoLib::MD5DIGESTSIZE); + cout << "MD5 of " << msg << " is " << string((const char*)hexmd5,2*CryptoLib::MD5DIGESTSIZE) << endl; + + // esign signature + byte* signMsg = (byte *)"Message to sign"; + char* keyInput = "esig1536.dat"; + CryptoLib::esignPriv privKey = myCryptoLib.esignPrivKey(keyInput); + CryptoLib::esignPub pubKey = myCryptoLib.esignPubKey(privKey); + CryptoLib::SigBuf mySignature = myCryptoLib.esignSig(signMsg, strlen((const char*)signMsg), privKey); + if (myCryptoLib.esignVer(signMsg, strlen((const char*)signMsg), mySignature, pubKey)) + cout << "ESIGN signature verification SUCCEDED" << endl; + else + cout << "ESIGN signature verification FAILED" << endl; + + // RSA signature + byte* rsaMsg = (byte *)"Message to sign"; + char* rsaInput = "rsa1024.dat"; + CryptoLib::rsaPriv privRSAKey = myCryptoLib.rsaPrivKey(rsaInput); + CryptoLib::rsaPub pubRSAKey = myCryptoLib.rsaPubKey(privRSAKey); + CryptoLib::SigBuf myRSASignature = myCryptoLib.rsaSig(rsaMsg, strlen((const char*)rsaMsg), privRSAKey); + if (myCryptoLib.rsaVer(rsaMsg, strlen((const char*)rsaMsg), myRSASignature, pubRSAKey)) + cout << "RSA signature verification SUCCEDED" << endl; + else + cout << "RSA signature verification FAILED" << endl; + + // Rijndael encryption/decryption + byte* plainMsg = (byte *)"My message to encrypt is even longer now"; + // the +1 is because strlen doesn't capture null char + unsigned int plainLen = strlen((const char*)plainMsg)+1; + cout << "About to encrypt " << plainMsg << " of size " << plainLen << endl; + + // intializes my key and iv + byte encKey[CryptoLib::RJ128KEYSIZE]; + byte iv[CryptoLib::RJBLOCKSIZE]; + memset(encKey, 0x01, CryptoLib::RJ128KEYSIZE); + memset(iv, 0x01, CryptoLib::RJBLOCKSIZE); + + // initialize my cipher buffer + byte cipherMsg[plainLen]; + memset(cipherMsg, 0x00, plainLen); + + // setup my encryptors and decryptors + CryptoLib::cfbModeEnc myEnc = myCryptoLib.getCFBModeEnc(encKey, CryptoLib::RJ128KEYSIZE, iv); + CryptoLib::cfbModeDec myDec = myCryptoLib.getCFBModeDec(encKey, CryptoLib::RJ128KEYSIZE, iv); + + // perform encryption + myCryptoLib.encryptCFB(plainMsg, plainLen, cipherMsg, myEnc); + + // turn my cipher into hex + // the +1 is capture the end of the cipher buffer + byte cipherHex[(2*plainLen)+1]; + memset(cipherHex, 0x00, (2*plainLen)+1); + myCryptoLib.toHex(cipherMsg, cipherHex, plainLen, (2*plainLen)+1); + cout << "My ciphertext has size " << strlen((const char*)cipherMsg) << endl; + cout << "Rijndael cipher of " << plainMsg << " is " << cipherHex << endl; + + // recover my data + byte origMsg[plainLen]; + memset(origMsg, 0x00, plainLen); + myCryptoLib.decryptCFB(cipherMsg, plainLen, origMsg, myDec); + cout << "My recovered message is " << origMsg << endl; + + + // RC5 encryption/decryption + byte plainRC5[] = "My message to encrypt is even longer now"; + unsigned int plainRC5len = strlen((const char*)plainRC5)+1; + + // initialize my cipher/hex/recover buffers + byte cipherRC5[plainRC5len]; + byte hexRC5[(2*plainRC5len)+1]; + byte recoverRC5[plainRC5len]; + memset(cipherRC5, 0x00, plainRC5len); + memset(hexRC5, 0x00, (2*plainRC5len)+1); + memset(recoverRC5, 0x00, plainRC5len); + + // init key and IV + byte keyRC5[CryptoLib::RC5KEYSIZE]; + byte ivRC5[CryptoLib::RC5BLOCKSIZE]; + memset(keyRC5, 0x01, CryptoLib::RC5KEYSIZE); + memset(ivRC5, 0x01, CryptoLib::RC5BLOCKSIZE); + + // init encrpytors and decryptors + CryptoLib::cfbRC5Enc encRC5 = myCryptoLib.getRC5Enc(keyRC5, CryptoLib::RC5KEYSIZE, ivRC5); + CryptoLib::cfbRC5Dec decRC5 = myCryptoLib.getRC5Dec(keyRC5, CryptoLib::RC5KEYSIZE, ivRC5); + + // encrypt + myCryptoLib.encryptRC5(plainRC5, plainRC5len, cipherRC5, encRC5); + + // convert to hex + myCryptoLib.toHex(cipherRC5, hexRC5, plainRC5len, (2*plainRC5len)+1); + cout << "RC5 cipher of " << plainRC5 << " is " << hexRC5 << endl; + + //decrypt + myCryptoLib.decryptRC5(cipherRC5, plainRC5len, recoverRC5, decRC5); + cout << "My recovered message is " << recoverRC5 << endl; + return 0; +} diff --git a/branches/aleung/security1/ceph/crypto/esig1536.dat b/branches/aleung/security1/ceph/crypto/esig1536.dat new file mode 100644 index 0000000000000..8e10d134cdba0 --- /dev/null +++ b/branches/aleung/security1/ceph/crypto/esig1536.dat @@ -0,0 +1 @@ +3082014D0281C100E2A6788AB3CC986AEC06C51690143D3677141645D0628165EE924B9AFB7E6EDD52D90145B2F6031522C7A6CEC05E358F42B7837DACEA589F868F8DCA1C0F5FD8E5EDB8BBBAFCFF6D64CFCFBE68F46FBA6EFF45BC9D0CBB4F7F6075F5FFC2049C2F304B51C417764E18D182926E02D4116CE5C5C010E3D0AA6872A49B0D1FF4B37D54689C31F5821D04E9D4DB34D7536EE7F88B8C481B0EC1F93193A0B70567E6FD76E9FAC4F67BB47DACD356D0C8015261E068DDF8C34C0CAFCF3FA775577FEB020120024100FAF0F292EE96D4F449024F86C0A104E0633C722586EC00AD33E0234629825D2081BA337597889CAC55DC6BEBDD8F13FE3AA2133D6371601A37D195DA7BC45EF3024100EBE16F88887A425AA08E271467CC2220DC44012AB24ED4FF3512A96E8CB600C8BBCB771459FF0EE63D4B6786952A83A7143A775073F0A1D69B6D0B5817755673 \ No newline at end of file diff --git a/branches/aleung/security1/ceph/crypto/rsa1024.dat b/branches/aleung/security1/ceph/crypto/rsa1024.dat new file mode 100644 index 0000000000000..70fb1724650a1 --- /dev/null +++ b/branches/aleung/security1/ceph/crypto/rsa1024.dat @@ -0,0 +1,32 @@ +30820274020100300D06092A864886F70D010101 +05000482025E3082025A02010002818100A39D4F +72D1BCFF65A47545C2897C0464CE9181E8703421 +2EC04407C4C24D569AA20C58B8138C85E17510BC +6B861CADA9034C3ECE3B050B546E97D2BDC07A07 +CF8A612F7D3646739633041893EF18C411264E45 +C9E033A1BD5EE5FA02D95E9A9ADA2D0C6DF480E3 +2FA3FCE02889798455CE53F084AAB4C5549266F7 +CE8C77DF1D0201110281800E6FC33ED64561D443 +378627C0D63C9F7BA36D584622B7A23E241ECD98 +AC78952C6A804C7A320BD020EAE372E62FB4F853 +1D50D5F6261796823A929845B06A19B35A5227CB +C819852A9CBE588CC2D1CEE07F426D13C2BF2FCA +1C99FDEEFDFE387859E2B3F654E85A71481A71E9 +D5256583B1200F29C1AA0F437CFDC2AEAF218102 +4100D5DDB104AD074F6C1B8192D9AC8AED4DE05C +F5C6509490DA8CCFC91FDF7B3A1323E03894DCAA +B2587716D652A56904F86244E10C1B8FA597C389 +2591C55DBD65024100C3D930B583B8AD9A349218 +795C988CF0004F09DA04FFEF6FDF7CB4FA654F74 +B262521FE185693CD6290A337589F62CDEECE24E +CCB5E79865275540F3B603FB59024064A48F89BA +D6437E2B0FCCA2AB8CABE86995285D5318BCA315 +167CC3B47639726B3C56DCA41417B128FBB026E4 +6DA7FC6A7AC441EEDA2FCEF29AE480D5594A1102 +40228FBD4D355CD35772B05EAC014818DF0F1D01 +BD0FF0EE04AEF7E3B3B7867E015CA514AF53C746 +F89DD49FAB5494DABDED9159332F28DEA8705A56 +C198974A79024100D1DCA40FBD19036F0E2A9438 +7D03C090DDF0A677CDE0B8634A81F247752A355E +C1CEA2482A4887767145C2BA703C9C10228FDA1E +BB2EBEA73D23AA9C34182179 -- 2.39.5