static int test_answer(const struct ccdigest_info *di, test_vector *vector, void*answer) { uint8_t *correct_answer = NULL; if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_MD2)) correct_answer = vector->md2_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_MD4)) correct_answer = vector->md4_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_MD5)) correct_answer = vector->md5_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_SHA1)) correct_answer = vector->sha1_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_SHA224)) correct_answer = vector->sha224_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_SHA256)) correct_answer = vector->sha256_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_SHA384)) correct_answer = vector->sha384_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_SHA512)) correct_answer = vector->sha512_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_RMD128)) correct_answer = vector->rmd128_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_RMD160)) correct_answer = vector->rmd160_answer; else if(ccdigest_oid_equal(di, (ccoid_t) CC_DIGEST_OID_RMD256)) correct_answer = vector->rmd256_answer; else correct_answer = vector->rmd320_answer; // hack byteBuffer answer_bb = bytesToBytes(answer, di->output_size); if(correct_answer == NULL) { printByteBuffer(answer_bb, "Answer Provided"); return 1; } byteBuffer correct_answer_bb = hexStringToBytes((char *) correct_answer); ok(bytesAreEqual(correct_answer_bb, answer_bb), "compare memory of answer"); if(bytesAreEqual(correct_answer_bb, answer_bb) == 0) { printByteBuffer(correct_answer_bb, "Correct Answer"); printByteBuffer(answer_bb, "Provided Answer"); } free(correct_answer_bb); free(answer_bb); return 1; }
static int showBytesAreEqual(byteBuffer bb1, byteBuffer bb2, char *label) { ok(bytesAreEqual(bb1, bb2), label); if(bytesAreEqual(bb1, bb2) == 0) { printByteBuffer(bb1, "Want"); printByteBuffer(bb2, "Got "); return 0; } return 1; }
static int testData() { CCStatus status; char *hexstring = "1002030405060708090021222324252627282920"; byteBuffer bb = hexStringToBytes(hexstring); CCBigNumRef num1 = CCBigNumFromData(&status, bb->bytes, bb->len); char *output; ok(status == 0, "BigNum Created"); output = CCBigNumToHexString(&status, num1); ok(status == 0, "Value retrieved"); ok(strcmp(output, hexstring) == 0, "strings are equal"); if(strcmp(output, hexstring)) { printf("output: %s\n", output); printf("input : %s\n", hexstring); } free(output); byteBuffer outbuf = mallocByteBuffer(64); outbuf->len = CCBigNumToData(&status, num1, outbuf->bytes); ok(status == 0, "Value retrieved 2"); ok(bytesAreEqual(bb, outbuf), "input == output"); free(bb); free(outbuf); CCBigNumFree(num1); return 0; }
int CommonHKDF(int argc, char *const *argv) { size_t i, n; int err; plan_tests(10 * 2); n = sizeof(hkdf_sha256_tests) / sizeof(*hkdf_sha256_tests); for(i = 0; i < n; ++i) { const test_vector_t * tv = &hkdf_sha256_tests[ i ]; byteBuffer ikm = hexStringToBytes(tv->ikm); byteBuffer salt = hexStringToBytes(tv->salt); byteBuffer info = hexStringToBytes(tv->info); byteBuffer okmActual = mallocByteBuffer(tv->len); byteBuffer okmExpected = hexStringToBytes(tv->okm); CCDigestAlgorithm digestType; if( tv->type == type_sha1) digestType = kCCDigestSHA1; else if(tv->type == type_sha256) digestType = kCCDigestSHA256; else if(tv->type == type_sha512) digestType = kCCDigestSHA512; else abort(); err = CCKeyDerivationHMac(kCCKDFAlgorithmHKDF, digestType, 0, ikm->bytes, ikm->len, NULL, 0, info->bytes, info->len, NULL, 0, salt->bytes, salt->len, okmActual->bytes, okmActual->len ); ok(!err, "check return value"); ok(bytesAreEqual(okmActual, okmExpected), "compare memory of answer"); free(ikm); free(salt); free(info); free(okmActual); free(okmExpected); } return 0; }
static int test_answer(char *mode_name, test_vector *vector, void*answer, char *test_type) { byteBuffer answer_bb = bytesToBytes(answer, CMAC_BLOCKSIZE); if(vector->outStr == NULL) { diag("/* CMAC-128 test %d */", vector->cnt); diag("\t\t\"%s\",\n", bytesToHexString(answer_bb)); return 1; } byteBuffer correct_answer_bb = hexStringToBytes((char *) vector->outStr); ok(bytesAreEqual(correct_answer_bb, answer_bb), "compare memory of answer"); if(bytesAreEqual(correct_answer_bb, answer_bb) == 0) { diag("Failed Test (%d) for CMAC-128-%s %s\n", vector->cnt, mode_name, test_type); printByteBuffer(correct_answer_bb, "Correct Answer"); printByteBuffer(answer_bb, "Provided Answer"); } free(correct_answer_bb); free(answer_bb); return 1; }
int cchkdftest(TM_UNUSED int argc, TM_UNUSED char *const *argv) { size_t i, n; int err; plan_tests(10 * 2); if(verbose) diag("Starting hkdf tests\n"); n = sizeof(hkdf_sha256_tests) / sizeof(*hkdf_sha256_tests); for(i = 0; i < n; ++i) { const test_vector_t * tv = &hkdf_sha256_tests[ i ]; byteBuffer ikm = hexStringToBytes(tv->ikm); byteBuffer salt = hexStringToBytes(tv->salt); byteBuffer info = hexStringToBytes(tv->info); byteBuffer okmActual = mallocByteBuffer(tv->len); byteBuffer okmExpected = hexStringToBytes(tv->okm); const struct ccdigest_info * di; if( tv->type == type_sha1) di = ccsha1_di(); else if(tv->type == type_sha256) di = ccsha256_di(); else if(tv->type == type_sha512) di = ccsha512_di(); else abort(); err = cchkdf(di, ikm->len, ikm->bytes, salt->len, salt->bytes, info->len, info->bytes, okmActual->len, okmActual->bytes); ok(!err, "check return value"); ok(bytesAreEqual(okmActual, okmExpected), "compare memory of answer"); free(ikm); free(salt); free(info); free(okmActual); free(okmExpected); } return 0; }
int CommonEC(int argc, char *const *argv) { CCCryptorStatus retval; size_t keysize; CCECCryptorRef publicKey, privateKey; CCECCryptorRef publicKey2; // byteBuffer keydata, dekeydata; byteBuffer hash; char encryptedKey[8192]; size_t encryptedKeyLen = 8192; // char decryptedKey[8192]; // size_t decryptedKeyLen = 8192; char signature[8192]; size_t signatureLen = 8192; char importexport[8192]; size_t importexportLen = 8192; uint32_t valid; int accum = 0; int debug = 0; plan_tests(kTestTestCount); keysize = 256; retval = CCECCryptorGeneratePair(keysize, &publicKey, &privateKey); if(debug) printf("Keys Generated\n"); ok(retval == 0, "Generate an EC Key Pair"); accum |= retval; #ifdef ECDH keydata = hexStringToBytes("000102030405060708090a0b0c0d0e0f"); retval = CCECCryptorWrapKey(publicKey, keydata->bytes, keydata->len, encryptedKey, &encryptedKeyLen, kCCDigestSHA1); ok(retval == 0, "Wrap Key Data with EC Encryption - ccPKCS1Padding"); accum |= retval; retval = CCECCryptorUnwrapKey(privateKey, encryptedKey, encryptedKeyLen, decryptedKey, &decryptedKeyLen); ok(retval == 0, "Unwrap Key Data with EC Encryption - ccPKCS1Padding"); accum |= retval; dekeydata = bytesToBytes(decryptedKey, decryptedKeyLen); ok(bytesAreEqual(dekeydata, keydata), "Round Trip CCECCryptorWrapKey/CCECCryptorUnwrapKey"); accum |= retval; #endif hash = hexStringToBytes("000102030405060708090a0b0c0d0e0f"); retval = CCECCryptorSignHash(privateKey, hash->bytes, hash->len, signature, &signatureLen); ok(retval == 0, "EC Signing"); valid = 0; accum |= retval; if(debug) printf("Signing Complete\n"); retval = CCECCryptorVerifyHash(publicKey, hash->bytes, hash->len, signature, signatureLen, &valid); ok(retval == 0, "EC Verifying"); accum |= retval; ok(valid, "EC Validity"); accum |= retval; if(debug) printf("Verify Complete\n"); // Mess with the sig - see what happens signature[signatureLen-3] += 3; retval = CCECCryptorVerifyHash(publicKey, hash->bytes, hash->len, signature, signatureLen, &valid); ok(retval == 0, "EC Verifying"); accum |= retval; ok(!valid, "EC Invalid Signature"); accum |= retval; if(debug) printf("Verify2 Complete\n"); encryptedKeyLen = 8192; retval = CCECCryptorExportPublicKey(publicKey, importexport, &importexportLen); ok(retval == 0, "EC Export Public Key"); accum |= retval; retval = CCECCryptorImportPublicKey(importexport, importexportLen, &publicKey2); ok(retval == 0, "EC Import Public Key"); accum |= retval; encryptedKeyLen = 8192; retval = CCECCryptorComputeSharedSecret(privateKey, publicKey, encryptedKey, &encryptedKeyLen); ok(retval == 0, "EC Shared Secret"); accum |= retval; return accum; }