static int test_answer(const struct ccdigest_info *di, test_vector *vector, size_t answer_len, void*answer) { char *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, answer_len); if(correct_answer == NULL) { diag("\t\t\"%s\", // %s\n", bytesToHexString(answer_bb), digest_name(di)); 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; }
void MainWindow::handle_pushButton_decrypt_onClick() { int size = 0; QString q_key = ui->textEdit_key->toPlainText(); string s_key = string((const char *)q_key.toLocal8Bit()); size = formatString(s_key); byte key[size]; hexStringToBytes(s_key,key); //pnt_byte(key,16); QString q_in = ui->textEdit_input->toPlainText(); string s_in = string((const char *)q_in.toLocal8Bit()); size = formatString(s_in); byte in[size]; hexStringToBytes(s_in,in); //pnt_byte(in,70); byte *out = NULL; CRYPTER *crypter = new CRYPTER(key); int len = crypter->decrypt(in,size,out); //pnt_byte(out,len); string s_out = bytesToHexString(out,len); QString q_out = QString(QString::fromLocal8Bit(s_out.c_str())); ui->textEdit_output->setPlainText(q_out); //std::cout << "handle_pushButton_decrypt_onClick" << std::endl; delete out; delete crypter; }
void Data::verify(LPCTSTR Description_, const uint8_t* const expectedDigest_) { memset(actualDigest, 0, sizeof(actualDigest)); bool mdUnmatch = false; calcSHA256(); for (int i = 0; i < 32; i++) { if (expectedDigest_[i] != actualDigest[i]) mdUnmatch = true; } if (mdUnmatch) { CodeConv::tostringstream o; o << Description_ << _T("のSHA256ハッシュ値が一致しませんでした。") << _T("ファイルが壊れている虞があります。") << std::endl << _T("期待されるハッシュ値: ") << CodeConv::EnsureTStr(bytesToHexString(std::vector<uint8_t>(expectedDigest_, expectedDigest_ + 32))) << std::endl << _T("実際のハッシュ値: ") << CodeConv::EnsureTStr(bytesToHexString(std::vector<uint8_t>(actualDigest, actualDigest + 32))); Raise(EXCEPTION_MJCORE_HASH_MISMATCH, o.str().c_str()); } else { CodeConv::tostringstream o; o << Description_ << _T("のSHA256ハッシュ値の照合に成功しました。"); info(o.str().c_str()); } }
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; }
char *bytesToHexStringWithSpaces(byteBuffer bb, int breaks) { char *retval; size_t i, j; if(breaks == 0) { return bytesToHexString(bb); } breaks /= 2; retval = malloc(bb->len*2 + 1 + (bb->len*2 / breaks) + 10); for(i=0, j=0; i<bb->len; i++, j+=2) { retval[j] = nibbleToChar(bb->bytes[i] >> 4); retval[j+1] = nibbleToChar(bb->bytes[i] & 0x0f); if(((i+1) % breaks) == 0) { retval[j+2] = ' '; retval[j+3] = 0; j++; } } return retval; }
std::string hashToString(const Hash& in) { return bytesToHexString((unsigned char*)&in, sizeof(in)); }
static int Integrity_POST(kmod_info_t* pkmod, void* d, int verbose) { int result = -1; // Set to zero for sucesses until it all works size_t sha256DigestBufferLength = 32; if (NULL == d) { if (verbose) { kprintf("The AppleTEXTHash_t pointer was NOT passed to the Integrity_POST function\n"); } return result; } AppleTEXTHash_t* pHashData = (AppleTEXTHash_t*)d; if (pHashData->ath_version != 1 || pHashData->ath_length != (int)sha256DigestBufferLength) { if (verbose) { kprintf("The AppleTEXTHash_t pointer passed to Integrity_POST function, is invalid\n"); } return result; } if (NULL == pHashData->ath_hash) { if (verbose) { kprintf("The AppleTEXTHash_t pointer passed to Integrity_POST function,has a null HASH pointer\n"); } return result; } unsigned long plist_hash_output_buffer_size = (sha256DigestBufferLength * 2) + 1; unsigned char plist_hash_output_buffer[plist_hash_output_buffer_size]; char* pPlistHexBuf = (char*)bytesToHexString(pHashData->ath_hash, pHashData->ath_length, plist_hash_output_buffer, plist_hash_output_buffer_size); if (verbose) { kprintf("Plist hmac value is %s\n", pPlistHexBuf); } // Now calcuate the HMAC struct mach_header* pmach_header = (struct mach_header*)pkmod->address; struct load_command* pLoadCommand = NULL; uint32_t num_load_commands = 0; if (pmach_header->magic == MH_MAGIC_64) { struct mach_header_64* pmach64_header = (struct mach_header_64*)pmach_header; num_load_commands = pmach64_header->ncmds; pLoadCommand = (struct load_command*)(((unsigned char*)pmach_header) + sizeof(struct mach_header_64)); } else if (pmach_header->magic == MH_MAGIC) { num_load_commands = pmach_header->ncmds; pLoadCommand = (struct load_command*)(((unsigned char*)pmach_header) + sizeof(struct mach_header)); } if (NULL == pLoadCommand) { if (verbose) { kprintf("pLoadCommand is NULL!\n"); } return result; } const struct ccdigest_info* di = ccsha256_di(); unsigned char hmac_key = 0; cchmac_ctx_decl(di->state_size, di->block_size, ctx); cchmac_init(di, ctx, 1, &hmac_key); int hashCreated = 0; unsigned long iCnt; unsigned long jCnt; struct segment_command* pSniffPtr = (struct segment_command*)pLoadCommand; // Loop through the Segments to find the __TEXT, __text segment for (iCnt = 0; iCnt < num_load_commands; iCnt++) { // The struct segment_command and the struct segment_command_64 have the same // first three fields so sniff the name by casting to a struct segment_command if (strncmp("__TEXT", pSniffPtr->segname, strlen("__TEXT"))) { // These are not the droids we are looking for // MOve the SniffPtr to the next segment; if (LC_SEGMENT_64 == pSniffPtr->cmd) { struct segment_command_64* pSegmentPtr = (struct segment_command_64*)pSniffPtr; pSniffPtr = (struct segment_command*)(((unsigned char *)pSegmentPtr) + pSegmentPtr->cmdsize); } else if (LC_SEGMENT == pSniffPtr->cmd) { pSniffPtr = (struct segment_command*)(((unsigned char *)pSniffPtr) + pSniffPtr->cmdsize); } // Go back to the top of the loop and look again continue; } // Bingo! We found the __TEXT segment! // Deal with a 64 bit segment if (LC_SEGMENT_64 == pLoadCommand->cmd) { struct segment_command_64* pSegmentPtr = NULL; // This is a 64 bit load segment command pSegmentPtr = (struct segment_command_64*)pSniffPtr; unsigned int numSections = (unsigned int)pSegmentPtr->nsects; struct section_64* pSectionPtr = (struct section_64*)(((unsigned char*)pSegmentPtr) + sizeof(struct segment_command_64)); int texttextsectionprocessed = 0; // Need to find the __text __TEXT section for (jCnt = 0; jCnt < numSections; jCnt++) { if ( !strcmp(pSectionPtr->sectname, "__text") && !strcmp(pSectionPtr->segname, "__TEXT")) { // Found it unsigned char* pSectionData = (unsigned char*)(((unsigned char*)pmach_header) + pSectionPtr->offset); cchmac_update(di, ctx, (unsigned long)pSectionPtr->size, pSectionData); hashCreated = 1; texttextsectionprocessed = 1; break; } else { // Move to the next section record pSectionPtr++; } } if (texttextsectionprocessed) { // The text text section was found and processed break; } } else if (LC_SEGMENT == pLoadCommand->cmd) // Deal with a 32 bit segment { struct segment_command* pSegmentPtr = NULL; // This is a 32 bit load segment command pSegmentPtr = (struct segment_command*)pLoadCommand; unsigned int numSections = (unsigned int)pSegmentPtr->nsects; struct section* pSectionPtr = (struct section*)(((unsigned char*)pSegmentPtr) + sizeof(struct segment_command)); int texttextsectionprocessed = 0; // Need to find the __text __TEXT section for (jCnt = 0; jCnt < numSections; jCnt++) { if ( !strcmp(pSectionPtr->sectname, "__text") && !strcmp(pSectionPtr->segname, "__TEXT")) { // Found it unsigned char* pSectionData = (unsigned char*)(((unsigned char*)pmach_header) + pSectionPtr->offset); cchmac_update(di, ctx, (unsigned long)pSectionPtr->size, pSectionData); hashCreated = 1; texttextsectionprocessed = 1; break; } else { // Move to the next section record pSectionPtr++; } } if (texttextsectionprocessed) { // The text text section was found and processed // Time to bail on the loop break; } } } unsigned long hash_output_buffer_size = (sha256DigestBufferLength * 2) + 1; unsigned char hash_output_buffer[hash_output_buffer_size]; unsigned char hmac_buffer[sha256DigestBufferLength]; memset(hmac_buffer, 0, sha256DigestBufferLength); // Check to see if the hash was created if (hashCreated) { // finalize the HMAC cchmac_final(di, ctx, hmac_buffer); char* pHexBuf = (char*)bytesToHexString(hmac_buffer, sha256DigestBufferLength, hash_output_buffer, hash_output_buffer_size); if (verbose) { kprintf("Computed hmac value is %s\n", pHexBuf); } } else { if (verbose) { kprintf("Integrity_POST: WARNING! could not create the hash!\n"); } return -1; } #ifdef FORCE_FAIL // futz with the generated hmac hash_output_buffer[0] = 0; // This will always work because it is the charter representation of the // hash that is being checked. #endif result = memcmp(hash_output_buffer, plist_hash_output_buffer, hash_output_buffer_size); return result; }