X-Git-Url: https://git.saurik.com/apple/security.git/blobdiff_plain/80e2389990082500d76eb566d4946be3e786c3ef..d8f41ccd20de16f8ebe2ccc84d47bf1cb2b26bbb:/SecurityTests/clxutils/threadTest/symTestThr.cpp diff --git a/SecurityTests/clxutils/threadTest/symTestThr.cpp b/SecurityTests/clxutils/threadTest/symTestThr.cpp new file mode 100644 index 00000000..405db6cb --- /dev/null +++ b/SecurityTests/clxutils/threadTest/symTestThr.cpp @@ -0,0 +1,150 @@ +/* + * Simple symmetric encrypt/decrypt test + */ +#include "testParams.h" +#include +#include +#include +#include +#include +#include +#include +#include + +/* for memory leak debug only, with only one thread running */ +#define DO_PAUSE 0 + +#define PTEXT_SIZE 1024 +#define USAGE_DEF "noUsage" + +int symTestInit(TestParams *testParams) +{ + /* nothing for now */ + return 0; +} + +int symTest(TestParams *testParams) +{ + unsigned loop; + CSSM_RETURN crtn; + CSSM_DATA ptext = {0, NULL}; + CSSM_DATA ctext = {0, NULL}; + CSSM_DATA rptext = {0, NULL}; + CSSM_KEY_PTR symKey; + CSSM_PADDING padding; + CSSM_ALGORITHMS keyAlg; + CSSM_ALGORITHMS encrAlg; + CSSM_ENCRYPT_MODE encrMode; + CSSM_BOOL keyIsRef; + CSSM_DATA initVector; + + initVector.Data = (uint8 *)"someStrangeInitVect"; + ptext.Data = (uint8 *)CSSM_MALLOC(PTEXT_SIZE); + ptext.Length = PTEXT_SIZE; + + for(loop=0; loopnumLoops; loop++) { + if(testParams->verbose) { + printf("symTest thread %d: loop %d\n", + testParams->threadNum, loop); + } + else if(!testParams->quiet) { + printChar(testParams->progressChar); + } + + /* random plaintext */ + crtn = threadGetRandData(testParams, &ptext, PTEXT_SIZE); + if(crtn) { + return 1; + } + + /* pick algorithm and params */ + if(loop & 1) { + keyAlg = CSSM_ALGID_DES; + encrAlg = CSSM_ALGID_DES; + encrMode = CSSM_ALGMODE_CBCPadIV8; + padding = CSSM_PADDING_PKCS1; + initVector.Length = 8; + } + else { + keyAlg = CSSM_ALGID_AES; + encrAlg = CSSM_ALGID_AES; + encrMode = CSSM_ALGMODE_CBCPadIV8; + padding = CSSM_PADDING_PKCS5; + initVector.Length = 16; + } + if(loop & 2) { + keyIsRef = CSSM_TRUE; + } + else { + keyIsRef = CSSM_FALSE; + } + + /* cook up a key */ + symKey = cspGenSymKey(testParams->cspHand, + keyAlg, + USAGE_DEF, + strlen(USAGE_DEF), + CSSM_KEYUSE_ENCRYPT | CSSM_KEYUSE_DECRYPT, + CSP_KEY_SIZE_DEFAULT, + keyIsRef); + if(symKey == NULL) { + return 1; + } + + /* encrypt, decrypt */ + crtn = cspEncrypt(testParams->cspHand, + encrAlg, + encrMode, + padding, + symKey, + NULL, // second key unused + 0, // efectiveKeySizeInBits, + 0, // rounds + &initVector, + &ptext, + &ctext, + CSSM_TRUE); // mallocCtext + if(crtn) { + return 1; + } + crtn = cspDecrypt(testParams->cspHand, + encrAlg, + encrMode, + padding, + symKey, + NULL, // second key unused + 0, // efectiveKeySizeInBits + 0, // rounds + &initVector, + &ctext, + &rptext, + CSSM_TRUE); // mallocCtext + if(crtn) { + return 1; + } + + /* compare */ + if(ptext.Length != rptext.Length) { + printf("Ptext length mismatch: expect %lu, got %lu\n", + ptext.Length, rptext.Length); + return 1; + } + if(memcmp(ptext.Data, rptext.Data, ptext.Length)) { + printf("***data miscompare\n"); + return 1; + } + + /* free everything */ + appFreeCssmData(&ctext, CSSM_FALSE); + appFreeCssmData(&rptext, CSSM_FALSE); + cspFreeKey(testParams->cspHand, symKey); + CSSM_FREE(symKey); + #if DO_PAUSE + fpurge(stdin); + printf("Hit CR to proceed: "); + getchar(); + #endif + } + return 0; +} +