static void DCC(unsigned char *salt, unsigned int username_len, unsigned int *dcc_hash, unsigned int count) { unsigned int id ; unsigned int buffer[64] ; unsigned int nt_hash[69] ; // large enough to handle 128 byte user name (when we expand to that size). int password_len; MD4_CTX ctx; for (id = 0; id < count; id++) { /* Proper Unicode conversion from UTF-8 or codepage */ password_len = enc_to_utf16((UTF16*)buffer, MAX_PLAINTEXT_LENGTH, (UTF8*)key_host[id], strlen((const char*)key_host[id])); /* Handle truncation */ if (password_len < 0) password_len = strlen16((UTF16*)buffer); // generate MD4 hash of the password (NT hash) MD4_Init(&ctx); MD4_Update(&ctx, buffer, password_len<<1); MD4_Final((unsigned char*)nt_hash, &ctx); // concatenate NT hash and the username (salt) memcpy((unsigned char *)nt_hash + 16, salt, username_len << 1) ; MD4_Init(&ctx); MD4_Update(&ctx, nt_hash, (username_len<<1)+16); MD4_Final((unsigned char*)(dcc_hash+4*id), &ctx); } }
int ed2k (int fd) { int length; int chunknum, chunklength, curlength; MD4_CTX root; MD4_CTX chunk; MD4_Init(&root); MD4_Init(&chunk); chunknum = 0; chunklength = 0; file_length = 0; while((length = read(fd, buff, BUFF_SIZE)) > 0) { file_length += length; if (length + chunklength > CHUNK_SIZE) { curlength = CHUNK_SIZE - chunklength; length = length - curlength; } else { curlength = length; length = 0; } MD4_Update(&chunk, buff, curlength); chunklength += curlength; if (chunklength == CHUNK_SIZE) { MD4_Final(md, &chunk); MD4_Init(&chunk); MD4_Update(&root, md, MD4_DIGEST_LENGTH); MD4_Update(&chunk, &buff[curlength], length); chunklength = length; chunknum ++; } } if (length < 0) { return -1; } if (chunknum > 0) { if (chunklength > 0) { MD4_Final(md, &chunk); MD4_Update(&root, md, MD4_DIGEST_LENGTH); } MD4_Final(md, &root); } else MD4_Final(md, &chunk); return 0; }
unsigned char *MD4(const unsigned char *d, size_t n, unsigned char *md) { MD4_CTX c; static unsigned char m[MD4_DIGEST_LENGTH]; if (md == NULL) md = m; if (!MD4_Init(&c)) return NULL; #ifndef CHARSET_EBCDIC MD4_Update(&c, d, n); #else { char temp[1024]; unsigned long chunk; while (n > 0) { chunk = (n > sizeof(temp)) ? sizeof(temp) : n; ebcdic2ascii(temp, d, chunk); MD4_Update(&c, temp, chunk); n -= chunk; d += chunk; } } #endif MD4_Final(md, &c); OPENSSL_cleanse(&c, sizeof(c)); /* security consideration */ return (md); }
void MD4Encode(unsigned char *output, const unsigned char *input, unsigned int len){ #ifdef USE_COMMON_CRYPTO CC_MD4_CTX context = {}; CC_MD4_Init (&context); CC_MD4_Update (&context, (unsigned char *)input, len); CC_MD4_Final (output, &context); #else MD4_CTX context = {}; MD4_Init (&context); MD4_Update (&context, (unsigned char *)input, len); MD4_Final (output, &context); /* mbedtls_md4_context ctx; mbedtls_md4_init( &ctx ); mbedtls_md4_starts( &ctx ); mbedtls_md4_update( &ctx, (unsigned char *)input, len ); mbedtls_md4_finish( &ctx, output ); mbedtls_md4_free( &ctx ); */ #endif }
static int _do_loop(void) { unsigned char buf[_BUFSIZ]; unsigned char digest[MD4_DIGEST_LENGTH + 1]; int n = 0; do { n = read(_g_infd, buf, _BUFSIZ); if (n > 0) MD4_Update(&_g_md4ctx, buf, n); } while (n > 0); MD4_Final(digest, &_g_md4ctx); digest[MD4_DIGEST_LENGTH] = 0; if (_g_outfd >= 0) { write(_g_outfd, digest, MD4_DIGEST_LENGTH); close(_g_outfd); _g_outfd = -1; } else { int i; printf("MD4(%s)= ", _g_infile); for (i = 0; i < MD4_DIGEST_LENGTH; i++) printf("%.2x", digest[i]); printf("\n"); } return 0; }
uint8_t *MD4(const uint8_t *data, size_t len, uint8_t *out) { MD4_CTX ctx; MD4_Init(&ctx); MD4_Update(&ctx, data, len); MD4_Final(out, &ctx); return out; }
void winpr_MD4_Final(WINPR_MD4_CTX* ctx, BYTE* output) { #if defined(WITH_OPENSSL) MD4_Final(output, (MD4_CTX*) ctx); #elif defined(WITH_MBEDTLS) && defined(MBEDTLS_MD4_C) mbedtls_md4_finish((mbedtls_md4_context*) ctx, output); mbedtls_md4_free((mbedtls_md4_context*) ctx); #endif }
void Util::md_final(int protocol, md_struct *context, char digest[16]) { if (protocol >= 30) { MD5_Final(reinterpret_cast<unsigned char *>(digest), &context->md5); } else { MD4_Final(reinterpret_cast<unsigned char *>(digest), &context->md4); } }
void md4_keyed_mac(unsigned char *message, int mlen, unsigned char *key, int klen, unsigned char *out) { MD4_CTX c; MD4_Init(&c); MD4_Update(&c, key, klen); MD4_Update(&c, message, mlen); MD4_Final(out, &c); }
uint8* crypto_md4_hash(rdpBlob* blob) { MD4_CTX md4_ctx; uint8* hash; hash = (uint8*)xzalloc(16); MD4_Init(&md4_ctx); MD4_Update(&md4_ctx, blob->data, blob->length); MD4_Final(hash, &md4_ctx); return hash; }
// create NTLM hash using md4 void ntlm_v1 (uint8_t *out, char *pwd) { MD4_CTX ctx; size_t pwd_len; wchar_t wcs_pwd[128]; pwd_len=mbstowcs (wcs_pwd, pwd, 128); MD4_Init (&ctx); MD4_Update (&ctx, pwd, pwd_len*2); MD4_Final (out, &ctx); }
unsigned char *MD4(const unsigned char *d, size_t n, unsigned char *md) { MD4_CTX c; static unsigned char m[MD4_DIGEST_LENGTH]; if (md == NULL) md=m; if (!MD4_Init(&c)) return NULL; MD4_Update(&c,d,n); MD4_Final(md,&c); explicit_bzero(&c,sizeof(c)); return(md); }
void ntlm1_password(char *password, u8 *digest) { u16 uni_pwd[128]={0}; u32 pass_len = strlen(password); MD4_CTX ctx; for(int i = 0;i < pass_len;i++) uni_pwd[i] = password[i]; MD4_Init(&ctx); MD4_Update(&ctx,uni_pwd,pass_len*2); MD4_Final(digest,&ctx); }
NOEXPORT char *ntlm3(char *username, char *password, char *phase2) { MD4_CTX md4; char *decoded; /* decoded reply from proxy */ char phase3[146]; unsigned char md4_hash[21]; unsigned int userlen=strlen(username); unsigned int phase3len=s_min(88+userlen, sizeof phase3); /* setup phase3 structure */ memset(phase3, 0, sizeof phase3); strcpy(phase3, "NTLMSSP"); phase3[8]=3; /* type: 3 */ phase3[16]=phase3len; /* LM-resp off */ phase3[20]=24; /* NT-resp len */ phase3[22]=24; /* NT-Resp len */ phase3[24]=64; /* NT-resp off */ phase3[32]=phase3len; /* domain offset */ phase3[36]=userlen; /* user length */ phase3[38]=userlen; /* user length */ phase3[40]=88; /* user offset */ phase3[48]=phase3len; /* host offset */ phase3[56]=phase3len; /* message len */ phase3[60]=2; /* flag: negotiate OEM */ phase3[61]=2; /* flag: negotiate NTLM */ /* calculate MD4 of UTF-16 encoded password */ MD4_Init(&md4); while(*password) { MD4_Update(&md4, password++, 1); MD4_Update(&md4, "", 1); /* UTF-16 */ } MD4_Final(md4_hash, &md4); memset(md4_hash+16, 0, 5); /* pad to 21 bytes */ /* decode challenge and calculate response */ decoded=base64(0, phase2, strlen(phase2)); /* decode */ if(!decoded) return NULL; crypt_DES((unsigned char *)phase3+64, (unsigned char *)decoded+24, md4_hash); crypt_DES((unsigned char *)phase3+72, (unsigned char *)decoded+24, md4_hash+7); crypt_DES((unsigned char *)phase3+80, (unsigned char *)decoded+24, md4_hash+14); str_free(decoded); strncpy(phase3+88, username, sizeof phase3-88); return base64(1, phase3, phase3len); /* encode */ }
void md4_length_extension(unsigned char *hash, int state_len, unsigned char *append, int append_len, unsigned char *out) { MD4_CTX c; MD4_Init(&c); MD4_Update(&c, hash, state_len); c.A = htole32(((uint32_t *)hash)[0]); c.B = htole32(((uint32_t *)hash)[1]); c.C = htole32(((uint32_t *)hash)[2]); c.D = htole32(((uint32_t *)hash)[3]); MD4_Update(&c, append, append_len); MD4_Final(out, &c); }
BYTE* NTOWFv1W(LPWSTR Password, UINT32 PasswordLength, BYTE* NtHash) { MD4_CTX md4_ctx; if (!Password) return NULL; if (!NtHash) NtHash = malloc(16); MD4_Init(&md4_ctx); MD4_Update(&md4_ctx, Password, PasswordLength); MD4_Final((void*) NtHash, &md4_ctx); return NtHash; }
void smb_ntlm_hash(const char *password, smb_ntlmh *hash) { MD4_CTX ctx; char *ucs2le_pass; size_t sz; assert(password != NULL && hash != NULL); sz = smb_to_utf16(password, strlen(password), &ucs2le_pass); memset((void *)hash, 0, SMB_NTLM_HASH_SIZE); MD4_Init(&ctx); MD4_Update(&ctx, (uint8_t *)ucs2le_pass, sz); MD4_Final((uint8_t *)hash, &ctx); free(ucs2le_pass); }
ikptr ikrt_openssl_md4_final (ikptr s_ctx, ikpcb * pcb) { #ifdef HAVE_MD4_FINAL ikptr s_pointer = IK_MD4_CTX_POINTER(s_ctx); MD4_CTX * ctx = IK_POINTER_DATA_VOIDP(s_pointer); unsigned char sum[MD4_DIGEST_LENGTH]; int rv = 0; if (ctx) { rv = MD4_Final(sum, ctx); free(ctx); IK_POINTER_SET_NULL(s_pointer); } return (rv)? ika_bytevector_from_memory_block(pcb, sum, MD4_DIGEST_LENGTH) : IK_FALSE; #else feature_failure(__func__); #endif }
static void MD4_File(FILE *file, unsigned char **output, int *outlength) { *output = new unsigned char[MD4_DIGEST_LENGTH]; *outlength = MD4_DIGEST_LENGTH; MD4_CTX c; int i; unsigned char buf[MD4_FILE_BUFFER_SIZE]; MD4_Init(&c); for (;;) { i = fread(buf,1,MD4_FILE_BUFFER_SIZE,file); if(i <= 0) break; MD4_Update(&c,buf,(unsigned long)i); } MD4_Final(*output, &c); }
void do_fp(FILE *f) { MD4_CTX c; unsigned char md[MD4_DIGEST_LENGTH]; int fd; int i; static unsigned char buf[BUFSIZE]; fd=fileno(f); MD4_Init(&c); for (;;) { i=read(fd,buf,sizeof buf); if (i <= 0) break; MD4_Update(&c,buf,(unsigned long)i); } MD4_Final(&(md[0]),&c); pt(md); }
/** Completes computation of the MD4 digest value. This function completes MD4 hash computation and retrieves the digest value into the specified memory. After this function has been called, the MD4 context cannot be used again. MD4 context should be already correctly intialized by Md4Init(), and should not be finalized by Md4Final(). Behavior with invalid MD4 context is undefined. If Md4Context is NULL, then return FALSE. If HashValue is NULL, then return FALSE. @param[in, out] Md4Context Pointer to the MD4 context. @param[out] HashValue Pointer to a buffer that receives the MD4 digest value (16 bytes). @retval TRUE MD4 digest computation succeeded. @retval FALSE MD4 digest computation failed. **/ BOOLEAN EFIAPI Md4Final ( IN OUT VOID *Md4Context, OUT UINT8 *HashValue ) { // // Check input parameters. // if (Md4Context == NULL || HashValue == NULL) { return FALSE; } // // OpenSSL MD4 Hash Finalization // return (BOOLEAN) (MD4_Final (HashValue, (MD4_CTX *) Md4Context)); }
void E_md4hash(uchar *passwd, uchar *p16) { int len; int16 wpwd[129]; MD4_CTX ctx; /* Password cannot be longer than 128 characters */ len = strlen((char *)passwd); if(len > 128) len = 128; /* Password must be converted to NT unicode */ _my_mbstowcs(wpwd, passwd, len); wpwd[len] = 0; /* Ensure string is null terminated */ /* Calculate length in bytes */ len = _my_wcslen(wpwd) * sizeof(int16); MD4_Init(&ctx); MD4_Update(&ctx, (unsigned char *)wpwd, len); MD4_Final(p16, &ctx); }
ULONG AnscCryptoMd4Digest ( PVOID buffer, ULONG size, PANSC_CRYPTO_HASH hash ) { #ifdef _ANSC_MD4_USED_ MD4_CTX context; MD4_Init (&context); MD4_Update(&context, (PUCHAR)buffer, size); MD4_Final (hash->Value, &context); hash->Length = ANSC_MD4_OUTPUT_SIZE; #else AnscTrace("WARNING: MD4 digest is disabled!!!\n"); #endif return hash->Length; }
int main(int argc, char**argv){ FILE *fp; int i, j; MD4_CTX ctx; unsigned char md5_sum[16]; long lSize; unsigned char* buffer; int rounds = atoi(argv[2]); unsigned char* enc_msg; //message that will receive the encryption of each part of the message. unsigned char msg_part[16]; fp = fopen ( argv[1] , "rb" ); if( !fp ) perror(argv[1]),exit(1); fseek( fp , 0L , SEEK_END); lSize = ftell( fp ); rewind( fp ); /* allocate memory for entire content */ buffer = calloc( 1, lSize+1 ); enc_msg = calloc(1, lSize+1); if( !buffer ) fclose(fp),fputs("memory alloc fails",stderr),exit(1); /* copy the file into the buffer */ if( 1!=fread( buffer, lSize, 1 , fp) ) fclose(fp),free(buffer),fputs("entire read fails",stderr),exit(1); i=0; while(i<=lSize){ //copy part of the message that will be "encrypted" ////receive 16 bytes = 128 bits memcpy(msg_part, buffer, 16); //initiate sha256 array MD4_Init ( &ctx ); //update ctx with the message MD4_Update( &ctx, msg_part, 16, rounds); //finish the processing and save the has into sha256sum MD4_Final( md5_sum, &ctx, rounds); memcpy(&enc_msg[i], md5_sum, 16); buffer += 16; i += 16; } //print the final result for( j = 0; j < lSize; j++ ) { printf( "%02x", enc_msg[j] ); } return( 0 ); }
void MD4_Digester::Finish() { MD4_Final(m_result, &m_ctx); }
int ed2k_link(char *fname, char ed2k_hash[]) { /* It will do the ed2k hashing of the file inputted * and it will finfo struct finfo.ed2k_hash * and return the pounter to the changed finfo. */ static const char hashzero[33] = HASHZERO; unsigned int a, b; unsigned char *parthash; unsigned char *hash; int fl; int64_t filesize; MD4_CTX contents; int chunks; int len, start; void *map; filesize = get_file_size(fname); if (filesize == -1){ /* We did not get file size * err and quit */ fprintf(stderr, "We failed to get file size; abouting...\n"); return -1; } if(filesize == 0){ /* Refuse to work on zero byte files and * return md4 of "" */ for(a = 0; a < 33; a++){ ed2k_hash[a] = hashzero[a]; } return 1; } chunks = filesize/CHUNKSIZE; if (filesize % CHUNKSIZE > 0){ chunks++; } parthash = (unsigned char*) malloc(16); if (!parthash){ return 0; } parthash = (unsigned char*) malloc(chunks*16); hash = (unsigned char* ) malloc(16); fl = open(fname, O_RDONLY); if (fl < 0){ fprintf(stderr, "Failded to open the file"); return +1; } for (a = 0; a < chunks; a++){ len = CHUNKSIZE; MD4_CTX content; if (a == chunks - 1){ len = filesize % CHUNKSIZE; } start = b*CHUNKSIZE; map = mmap(NULL, len, PROT_READ, MAP_SHARED, fl, a*CHUNKSIZE); if (map == NULL){ fprintf(stderr, "mmap() failed:, %s",strerror(errno)); return 0; } MD4_Init(&content); MD4_Update(&content, map, len); MD4_Final(parthash+(b*16), &content); munmap(map, len); printf("partial hash block %i\n", a); } if(chunks > 1){ MD4_Init(&contents); MD4_Update(&contents, parthash,b*16); MD4_Final(hash, &contents); }else { memcpy(hash, parthash, 16); } memset(ed2k_hash, 0x00, 33*sizeof(char)); for(b=0; b<16; b++){ ed2k_hash[(b<<1)] = hexdigits[(((hash[b]) & 0xf0) >> 4)]; ed2k_hash[(b<<1) + 1] = hexdigits[(((hash[b]) & 0x0f))]; } return -1; }
/* HashNTLM Function: Create a NTLM hash from the challenge Variables: ntlmhash = the hash created from this function pass = users password challenge = the challenge recieved from the server */ void HashNTLM(unsigned char **ntlmhash, unsigned char *pass, unsigned char *challenge, char *miscptr) { MD4_CTX md4Context; unsigned char hash[16]; /* MD4_SIGNATURE_SIZE = 16 */ unsigned char unicodePassword[256 * 2]; /* MAX_NT_PASSWORD = 256 */ unsigned char p21[21]; unsigned char ntlm_response[24]; int i = 0, j = 0; int mdlen; unsigned char *p; char HexChar; int HexValue; /* Use NTLM Hash instead of password */ if (hashFlag == 1) { /* 1000:D42E35E1A1E4C22BD32E2170E4857C20:5E20780DD45857A68402938C7629D3B2::: */ p = pass; while ((*p != '\0') && (i < 2)) { if (*p == ':') i++; p++; } if (*p == '\0') { fprintf(stderr, "Error reading PWDUMP file.\n"); hydra_child_exit(0); exit(1); } for (i = 0; i < 16; i++) { HexValue = 0x0; for (j = 0; j < 2; j++) { HexChar = (char) p[2 * i + j]; if (HexChar > 0x39) HexChar = HexChar | 0x20; /* convert upper case to lower */ if (!(((HexChar >= 0x30) && (HexChar <= 0x39)) || /* 0 - 9 */ ((HexChar >= 0x61) && (HexChar <= 0x66)))) { /* a - f */ /* * fprintf(stderr, "Error invalid char (%c) for hash.\n", HexChar); * hydra_child_exit(0); * exit(1); */ HexChar = 0x30; } HexChar -= 0x30; if (HexChar > 0x09) /* HexChar is "a" - "f" */ HexChar -= 0x27; HexValue = (HexValue << 4) | (char) HexChar; } hash[i] = (unsigned char) HexValue; } } else { /* Password == Machine Name */ if (hashFlag == 2) { for (i = 0; i < 16; i++) { if (machine_name[i] > 0x39) machine_name[i] = machine_name[i] | 0x20; /* convert upper case to lower */ pass = machine_name; } } /* Initialize the Unicode version of the secret (== password). */ /* This implicitly supports 8-bit ISO8859/1 characters. */ bzero(unicodePassword, sizeof(unicodePassword)); for (i = 0; i < strlen((char *) pass); i++) unicodePassword[i * 2] = (unsigned char) pass[i]; mdlen = strlen((char *) pass) * 2; /* length in bytes */ MD4_Init(&md4Context); MD4_Update(&md4Context, unicodePassword, mdlen); MD4_Final(hash, &md4Context); /* Tell MD4 we're done */ } memset(p21, '\0', 21); memcpy(p21, hash, 16); DesEncrypt(challenge, p21 + 0, ntlm_response + 0); DesEncrypt(challenge, p21 + 7, ntlm_response + 8); DesEncrypt(challenge, p21 + 14, ntlm_response + 16); memcpy(*ntlmhash, ntlm_response, 24); }
//MD4 hash function bool __fastcall MD4_Hash( FILE *Input) { //Parameters check if ((HashFamilyID != HASH_ID_MD4 && HashFamilyID != HASH_ID_ED2K) || Input == nullptr) { fwprintf_s(stderr, L"Parameters error.\n"); return false; } //Initialization size_t ReadBlockSize = FILE_BUFFER_SIZE, ReadLength = 0, RoundCount = 0; if (HashFamilyID == HASH_ID_ED2K) ReadBlockSize = ED2K_SIZE_BLOCK; std::shared_ptr<char> Buffer(new char[ReadBlockSize]()), StringBuffer(new char[FILE_BUFFER_SIZE]()), BufferED2K(new char[MD4_SIZE_DIGEST]()); memset(Buffer.get(), 0, ReadBlockSize); memset(StringBuffer.get(), 0, FILE_BUFFER_SIZE); memset(BufferED2K.get(), 0, MD4_SIZE_DIGEST); MD4_CTX HashInstance, HashInstanceED2K; memset(&HashInstance, 0, sizeof(MD4_CTX)); memset(&HashInstanceED2K, 0, sizeof(MD4_CTX)); //MD4 initialization MD4_Init(&HashInstance); if (HashFamilyID == HASH_ID_ED2K) MD4_Init(&HashInstanceED2K); //Hash process while (!feof(Input)) { memset(Buffer.get(), 0, ReadBlockSize); _set_errno(0); ReadLength = fread_s(Buffer.get(), ReadBlockSize, sizeof(char), ReadBlockSize, Input); if (ReadLength == 0) { fwprintf_s(stderr, L"Hash process error"); if (errno > 0) fwprintf_s(stderr, L", error code is %d.\n", errno); else fwprintf_s(stderr, L".\n"); return false; } else { MD4_Update(&HashInstance, Buffer.get(), ReadLength); if (HashFamilyID == HASH_ID_ED2K) { MD4_Final((unsigned char *)Buffer.get(), &HashInstance); memcpy_s(BufferED2K.get(), MD4_SIZE_DIGEST, Buffer.get(), MD4_SIZE_DIGEST); MD4_Update(&HashInstanceED2K, Buffer.get(), MD4_SIZE_DIGEST); MD4_Init(&HashInstance); } ++RoundCount; } } //Binary to hex memset(Buffer.get(), 0, ReadBlockSize); if (HashFamilyID == HASH_ID_MD4) { MD4_Final((unsigned char *)Buffer.get(), &HashInstance); } else if (HashFamilyID == HASH_ID_ED2K) { if (RoundCount > 1U) MD4_Final((unsigned char *)Buffer.get(), &HashInstanceED2K); else memcpy_s(Buffer.get(), MD4_SIZE_DIGEST, BufferED2K.get(), MD4_SIZE_DIGEST); } else { return false; } if (sodium_bin2hex(StringBuffer.get(), FILE_BUFFER_SIZE, (const unsigned char *)Buffer.get(), MD4_SIZE_DIGEST) == nullptr) { fwprintf_s(stderr, L"Convert binary to hex error.\n"); return false; } else { //Print to screen. std::string HashResult = StringBuffer.get(); CaseConvert(true, HashResult); for (size_t Index = 0;Index < HashResult.length();++Index) fwprintf_s(stderr, L"%c", HashResult.c_str()[Index]); fwprintf_s(stderr, L"\n"); } return true; }
static void client_mschapv2(const void *server_nonce, size_t snoncelen, const void *client_nonce, size_t cnoncelen, const char *username, const char *password) { SHA_CTX ctx; MD4_CTX hctx; unsigned char md[SHA_DIGEST_LENGTH], challange[SHA_DIGEST_LENGTH]; unsigned char hmd[MD4_DIGEST_LENGTH]; struct ntlm_buf answer; int i, len, ret; char *h; SHA1_Init(&ctx); SHA1_Update(&ctx, client_nonce, cnoncelen); SHA1_Update(&ctx, server_nonce, snoncelen); SHA1_Update(&ctx, username, strlen(username)); SHA1_Final(md, &ctx); MD4_Init(&hctx); len = strlen(password); for (i = 0; i < len; i++) { MD4_Update(&hctx, &password[i], 1); MD4_Update(&hctx, &password[len], 1); } MD4_Final(hmd, &hctx); /* ChallengeResponse */ ret = heim_ntlm_calculate_ntlm1(hmd, sizeof(hmd), md, &answer); if (ret) errx(1, "heim_ntlm_calculate_ntlm1"); hex_encode(answer.data, answer.length, &h); printf("responseData=%s\n", h); free(h); /* PasswordHash */ MD4_Init(&hctx); MD4_Update(&hctx, hmd, sizeof(hmd)); MD4_Final(hmd, &hctx); /* GenerateAuthenticatorResponse */ SHA1_Init(&ctx); SHA1_Update(&ctx, hmd, sizeof(hmd)); SHA1_Update(&ctx, answer.data, answer.length); SHA1_Update(&ctx, ms_chap_v2_magic1, sizeof(ms_chap_v2_magic1)); SHA1_Final(md, &ctx); /* ChallengeHash */ SHA1_Init(&ctx); SHA1_Update(&ctx, client_nonce, cnoncelen); SHA1_Update(&ctx, server_nonce, snoncelen); SHA1_Update(&ctx, username, strlen(username)); SHA1_Final(challange, &ctx); SHA1_Init(&ctx); SHA1_Update(&ctx, md, sizeof(md)); SHA1_Update(&ctx, challange, 8); SHA1_Update(&ctx, ms_chap_v2_magic2, sizeof(ms_chap_v2_magic2)); SHA1_Final(md, &ctx); hex_encode(md, sizeof(md), &h); printf("AuthenticatorResponse=%s\n", h); free(h); /* get_master, rfc 3079 3.4 */ SHA1_Init(&ctx); SHA1_Update(&ctx, hmd, sizeof(hmd)); SHA1_Update(&ctx, answer.data, answer.length); SHA1_Update(&ctx, ms_rfc3079_magic1, sizeof(ms_rfc3079_magic1)); SHA1_Final(md, &ctx); free(answer.data); hex_encode(md, 16, &h); printf("session-key=%s\n", h); free(h); }
static char * ed2k_hash (char *filename) { unsigned long size; int fd, b, j, blocks; unsigned char *parthashes, *ed2k_hash; char *ed2k_hash_str; if ((fd = open(filename, O_RDONLY)) < 0) { ANIDBFS_DEBUG("Failed to open file (%s)", filename); return NULL; } size = get_file_size(fd); if (size <= 0) { ANIDBFS_DEBUG("Error getting filesize (%s)", filename); return NULL; } blocks = size / BLOCKSIZE; if (size % BLOCKSIZE > 0) blocks++; ed2k_hash = (unsigned char *) malloc(16); ed2k_hash_str = (char *) malloc(33); parthashes = (unsigned char *) malloc(blocks * 16); if ((!parthashes) || (!ed2k_hash) || (!ed2k_hash_str) || (!filename)) { ANIDBFS_DEBUG("Failed to allocate memory (%s)", filename); return NULL; } for (b = 0; b < blocks; b++) { MD4_CTX context; int len, start; void *map; len = BLOCKSIZE; if (b == blocks - 1) len = size % BLOCKSIZE; start = b * BLOCKSIZE; map = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, b * BLOCKSIZE); if (map == NULL) { ANIDBFS_DEBUG("mmap failed (%s)", filename); } MD4_Init(&context); MD4_Update(&context, map, len); MD4_Final(parthashes + (b * 16), &context); munmap(map, len); /* int percent = (int) (((float) (b+1) / (float) blocks) * 100); ANIDBFS_DEBUG("Hashing %s %d%%", filename, percent);*/ } close(fd); if (blocks > 1) { MD4_CTX context; MD4_Init(&context); MD4_Update(&context, parthashes, 16 * b); MD4_Final(ed2k_hash, &context); } else { memcpy(ed2k_hash, parthashes, 16); } memset(ed2k_hash_str, 0x00, 33 * sizeof(char)); for (j = 0; j < 16; j++) { ed2k_hash_str[(j<<1)] = hexdigits[(((ed2k_hash[j]) & 0xf0) >> 4)]; ed2k_hash_str[(j<<1)+1] = hexdigits[(((ed2k_hash[j]) & 0x0f))]; } free(ed2k_hash); free(parthashes); return ed2k_hash_str; }