/* note that this call terminates the key input phase */ void hmac_sha1_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]) { unsigned int i; if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */ { if(cx->klen > IN_BLOCK_LENGTH) /* if key is being hashed */ { /* complete the hash and */ sha1_end((unsigned char *)cx->key, cx->ctx); /* store the result as the */ cx->klen = OUT_BLOCK_LENGTH; /* key and set new length */ } /* pad the key if necessary */ memset(cx->key + cx->klen, 0, IN_BLOCK_LENGTH - cx->klen); /* xor ipad into key value */ for(i = 0; i < IN_BLOCK_LENGTH / sizeof(ARCH_WORD_32); ++i) cx->key[i] ^= 0x36363636; /* and start hash operation */ sha1_begin(cx->ctx); sha1_hash(cx->key, IN_BLOCK_LENGTH, cx->ctx); /* mark as now in data mode */ cx->klen = HMAC_IN_DATA; } /* hash the data (if any) */ if(data_len) sha1_hash(data, data_len, cx->ctx); }
int _mesa_sha1_final(struct mesa_sha1 *ctx, unsigned char result[20]) { sha1_end(result, (sha1_ctx *) ctx); free(ctx); return 1; }
/* compute and output the MAC value */ void hmac_sha1_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]) { unsigned char dig[OUT_BLOCK_LENGTH]; unsigned int i; /* if no data has been entered perform a null data phase */ if(cx->klen != HMAC_IN_DATA) hmac_sha1_data((const unsigned char*)0, 0, cx); sha1_end(dig, cx->ctx); /* complete the inner hash */ /* set outer key value using opad and removing ipad */ for(i = 0; i < IN_BLOCK_LENGTH / sizeof(ARCH_WORD_32); ++i) cx->key[i] ^= 0x36363636 ^ 0x5c5c5c5c; /* perform the outer hash operation */ sha1_begin(cx->ctx); sha1_hash(cx->key, IN_BLOCK_LENGTH, cx->ctx); sha1_hash(dig, OUT_BLOCK_LENGTH, cx->ctx); sha1_end(dig, cx->ctx); /* output the hash value */ for(i = 0; i < mac_len; ++i) mac[i] = dig[i]; }
static void create_hash(char hash_str[SHA1_RESULT_LEN*2 + 1], const char *pInput) { unsigned char hash_bytes[SHA1_RESULT_LEN]; sha1_ctx_t sha1ctx; sha1_begin(&sha1ctx); sha1_hash(&sha1ctx, pInput, strlen(pInput)); sha1_end(&sha1ctx, hash_bytes); unsigned len = SHA1_RESULT_LEN; unsigned char *s = hash_bytes; char *d = hash_str; while (len) { *d++ = "0123456789abcdef"[*s >> 4]; *d++ = "0123456789abcdef"[*s & 0xf]; s++; len--; } *d = '\0'; //log("hash:%s str:'%s'", hash_str, pInput); }
std::string vmsHash::Hash_SHA1(LPCSTR pszFile) { sha1_ctx sha1; vmsFile file; __int64 trb = 0; BYTE abBuf [16000]; sha1_begin (&sha1); try { file.Open (pszFile, TRUE); __int64 uSize = file.get_Size (); DWORD dwRead; while (dwRead = file.Read (abBuf, sizeof (abBuf))) { sha1_hash (abBuf, dwRead, &sha1); trb += dwRead; if (m_pEvents) { double f = (double)trb / uSize * 100; if (false == m_pEvents->OnProgressChanged (f)) { trb = 0; break; } } } BYTE abRes [256]; sha1_end (abRes, &sha1); if (trb == 0 && uSize != 0) return ""; return Hash_ResultToStr (abRes, 20); }catch (...) {return "";} }
static foreign_t pl_sha_hash_ctx(term_t old_ctx, term_t from, term_t new_ctx, term_t hash) { char *data; size_t datalen; struct context *cp; size_t clen; unsigned char hval[SHA2_MAX_DIGEST_SIZE]; if ( !PL_get_nchars(from, &datalen, &data, CVT_ATOM|CVT_STRING|CVT_LIST|CVT_EXCEPTION) ) return FALSE; if ( !PL_get_string_chars(old_ctx, (char **)&cp, &clen) ) return FALSE; if ( clen != sizeof (*cp) || cp->magic != CONTEXT_MAGIC ) { return pl_error(NULL, 0, "Invalid OldContext passed", ERR_DOMAIN, old_ctx, "algorithm"); } if ( cp->opts.algorithm == ALGORITHM_SHA1 ) { sha1_ctx *c1p = &(cp->context.sha1); sha1_hash((unsigned char*)data, (unsigned long)datalen, c1p); if ( !PL_unify_string_nchars(new_ctx, sizeof(*cp), (char*)cp) ) return FALSE; sha1_end((unsigned char *)hval, c1p); } else { sha2_ctx *c1p = &(cp->context.sha2); sha2_hash((unsigned char*)data, (unsigned long)datalen, c1p); if ( !PL_unify_string_nchars(new_ctx, sizeof(*cp), (char*)cp) ) return FALSE; sha2_end((unsigned char *)hval, c1p); } /* . */ return PL_unify_list_ncodes(hash, cp->opts.digest_size, (char*)hval); }
int main(int argc, char **argv) { abrt_init(argv); const char *dump_dir_name = "."; /* Can't keep these strings/structs static: _() doesn't support that */ const char *program_usage_string = _( "\b [-v] -d DIR\n" "\n" "Calculates and saves UUID and DUPHASH of python crash dumps" ); enum { OPT_v = 1 << 0, OPT_d = 1 << 1, }; /* Keep enum above and order of options below in sync! */ struct options program_options[] = { OPT__VERBOSE(&g_verbose), OPT_STRING('d', NULL, &dump_dir_name, "DIR", _("Dump directory")), OPT_END() }; /*unsigned opts =*/ parse_opts(argc, argv, program_options, program_usage_string); export_abrt_envvars(0); struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0); if (!dd) return 1; char *bt = dd_load_text(dd, FILENAME_BACKTRACE); /* Hash 1st line of backtrace and save it as UUID and DUPHASH */ /* "example.py:1:<module>:ZeroDivisionError: integer division or modulo by zero" */ unsigned char hash_bytes[SHA1_RESULT_LEN]; sha1_ctx_t sha1ctx; sha1_begin(&sha1ctx); const char *bt_end = strchrnul(bt, '\n'); sha1_hash(&sha1ctx, bt, bt_end - bt); sha1_end(&sha1ctx, hash_bytes); free(bt); char hash_str[SHA1_RESULT_LEN*2 + 1]; unsigned len = SHA1_RESULT_LEN; unsigned char *s = hash_bytes; char *d = hash_str; while (len) { *d++ = "0123456789abcdef"[*s >> 4]; *d++ = "0123456789abcdef"[*s & 0xf]; s++; len--; } *d = '\0'; dd_save_text(dd, FILENAME_UUID, hash_str); dd_save_text(dd, FILENAME_DUPHASH, hash_str); dd_close(dd); return 0; }
void problem_data_add_basics(problem_data_t *pd) { const char *analyzer = problem_data_get_content_or_NULL(pd, FILENAME_ANALYZER); const char *type = problem_data_get_content_or_NULL(pd, FILENAME_TYPE); if (analyzer == NULL) { analyzer = type ? type : "libreport"; problem_data_add_text_noteditable(pd, FILENAME_ANALYZER, analyzer); } if (type == NULL) problem_data_add_text_noteditable(pd, FILENAME_TYPE, analyzer); /* If application didn't provide dupe hash, we generate it * from all components, so we at least eliminate the exact same * reports * * We don't want to generate DUPHASH file because it is usually generated * later in some "analyze_*" event. DUPHASH was originally designed as * global problem identifier and generating of global identifier requires * more space and data. On the contrary UUID was originally designed as * local problem identifier. It means that this identifier is weaker (e.g. * a hash generated from a coredump without debuginfo - there can be many * similar backtraces without line numbers and function names). */ if (problem_data_get_content_or_NULL(pd, FILENAME_UUID) == NULL) { /* If application provided DUPHASH, we should use it in UUID as well. * Otherwise we compute hash from all problem's data. */ const char *const duphash = problem_data_get_content_or_NULL(pd, FILENAME_DUPHASH); if (duphash != NULL) problem_data_add_text_noteditable(pd, FILENAME_UUID, duphash); else { /* start hash */ sha1_ctx_t sha1ctx; sha1_begin(&sha1ctx); /* * To avoid spurious hash differences, sort keys so that elements are * always processed in the same order: */ GList *list = g_hash_table_get_keys(pd); list = g_list_sort(list, (GCompareFunc)strcmp); GList *l = list; while (l) { const char *key = l->data; l = l->next; struct problem_item *item = g_hash_table_lookup(pd, key); /* do not hash items which are binary (item->flags & CD_FLAG_BIN). * Their ->content is full file name, with path. Path is always * different and will make hash differ even if files are the same. */ if (item->flags & CD_FLAG_BIN) continue; sha1_hash(&sha1ctx, item->content, strlen(item->content)); } g_list_free(list); /* end hash */ char hash_bytes[SHA1_RESULT_LEN]; sha1_end(&sha1ctx, hash_bytes); char hash_str[SHA1_RESULT_LEN*2 + 1]; bin2hex(hash_str, hash_bytes, SHA1_RESULT_LEN)[0] = '\0'; problem_data_add_text_noteditable(pd, FILENAME_UUID, hash_str); } } }
void Sha1::GetDigest (const BufferPtr &buffer) { if_debug (ValidateDigestParameters (buffer)); sha1_end (buffer, (sha1_ctx *) Context.Ptr()); }
int CHashManager::HashFile(char *pszFile) { FILE *fp = NULL; unsigned char pBuf[SIZE_HASH_BUFFER]; unsigned long uRead = 0; unsigned char pTemp[256]; char szTemp[RH_MAX_BUFFER]; int i = 0; printf("File: <"); printf(pszFile); printf(">"); printf(CPS_NEWLINE); fp = fopen(pszFile, "rb"); if(fp == NULL) return RH_CANNOT_OPEN_FILE; if(m_bAlgorithm[HASHID_CRC16]) crc16_init(&m_crc16); if(m_bAlgorithm[HASHID_CRC16CCITT]) crc16ccitt_init(&m_crc16ccitt); if(m_bAlgorithm[HASHID_CRC32]) crc32Init(&m_crc32); if(m_bAlgorithm[HASHID_FCS_16]) fcs16_init(&m_fcs16); if(m_bAlgorithm[HASHID_FCS_32]) fcs32_init(&m_fcs32); if(m_bAlgorithm[HASHID_GHASH_32_3] || m_bAlgorithm[HASHID_GHASH_32_5]) m_ghash.Init(); if(m_bAlgorithm[HASHID_GOST]) gosthash_reset(&m_gost); if(m_bAlgorithm[HASHID_HAVAL]) haval_start(&m_haval); if(m_bAlgorithm[HASHID_MD2]) m_md2.Init(); if(m_bAlgorithm[HASHID_MD4]) MD4Init(&m_md4); if(m_bAlgorithm[HASHID_MD5]) MD5Init(&m_md5, 0); if(m_bAlgorithm[HASHID_SHA1]) sha1_begin(&m_sha1); if(m_bAlgorithm[HASHID_SHA2_256]) sha256_begin(&m_sha256); if(m_bAlgorithm[HASHID_SHA2_384]) sha384_begin(&m_sha384); if(m_bAlgorithm[HASHID_SHA2_512]) sha512_begin(&m_sha512); if(m_bAlgorithm[HASHID_SIZE_32]) sizehash32_begin(&m_uSizeHash32); if(m_bAlgorithm[HASHID_TIGER]) tiger_init(&m_tiger); while(1) { uRead = fread(pBuf, 1, SIZE_HASH_BUFFER, fp); if(uRead != 0) { if(m_bAlgorithm[HASHID_CRC16]) crc16_update(&m_crc16, pBuf, uRead); if(m_bAlgorithm[HASHID_CRC16CCITT]) crc16ccitt_update(&m_crc16ccitt, pBuf, uRead); if(m_bAlgorithm[HASHID_CRC32]) crc32Update(&m_crc32, pBuf, uRead); if(m_bAlgorithm[HASHID_FCS_16]) fcs16_update(&m_fcs16, pBuf, uRead); if(m_bAlgorithm[HASHID_FCS_32]) fcs32_update(&m_fcs32, pBuf, uRead); if(m_bAlgorithm[HASHID_GHASH_32_3] || m_bAlgorithm[HASHID_GHASH_32_5]) m_ghash.Update(pBuf, uRead); if(m_bAlgorithm[HASHID_GOST]) gosthash_update(&m_gost, pBuf, uRead); if(m_bAlgorithm[HASHID_HAVAL]) haval_hash(&m_haval, pBuf, uRead); if(m_bAlgorithm[HASHID_MD2]) m_md2.Update(pBuf, uRead); if(m_bAlgorithm[HASHID_MD4]) MD4Update(&m_md4, pBuf, uRead); if(m_bAlgorithm[HASHID_MD5]) MD5Update(&m_md5, pBuf, uRead); if(m_bAlgorithm[HASHID_SHA1]) sha1_hash(pBuf, uRead, &m_sha1); if(m_bAlgorithm[HASHID_SHA2_256]) sha256_hash(pBuf, uRead, &m_sha256); if(m_bAlgorithm[HASHID_SHA2_384]) sha384_hash(pBuf, uRead, &m_sha384); if(m_bAlgorithm[HASHID_SHA2_512]) sha512_hash(pBuf, uRead, &m_sha512); if(m_bAlgorithm[HASHID_SIZE_32]) sizehash32_hash(&m_uSizeHash32, uRead); if(m_bAlgorithm[HASHID_TIGER]) tiger_process(&m_tiger, pBuf, uRead); } if(uRead != SIZE_HASH_BUFFER) break; } fclose(fp); fp = NULL; // SizeHash-32 is the first hash, because it's the simplest one, // the fastest, and most widely used one. ;-) if(m_bAlgorithm[HASHID_SIZE_32]) { sizehash32_end(&m_uSizeHash32); printf(SZ_SIZEHASH_32); printf(SZ_HASHPRE); printf("%08X", m_uSizeHash32); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_CRC16]) { crc16_final(&m_crc16); printf(SZ_CRC16); printf(SZ_HASHPRE); printf("%04X", m_crc16); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_CRC16CCITT]) { crc16ccitt_final(&m_crc16ccitt); printf(SZ_CRC16CCITT); printf(SZ_HASHPRE); printf("%04X", m_crc16ccitt); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_CRC32]) { crc32Finish(&m_crc32); printf(SZ_CRC32); printf(SZ_HASHPRE); printf("%08X", m_crc32); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_FCS_16]) { fcs16_final(&m_fcs16); printf(SZ_FCS_16); printf(SZ_HASHPRE); printf("%04X", m_fcs16); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_FCS_32]) { fcs32_final(&m_fcs32); printf(SZ_FCS_32); printf(SZ_HASHPRE); printf("%08X", m_fcs32); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_GHASH_32_3]) { m_ghash.FinalToStr(szTemp, 3); printf(SZ_GHASH_32_3); printf(SZ_HASHPRE); printf(szTemp); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_GHASH_32_5]) { m_ghash.FinalToStr(szTemp, 5); printf(SZ_GHASH_32_5); printf(SZ_HASHPRE); printf(szTemp); printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_GOST]) { gosthash_final(&m_gost, pTemp); printf(SZ_GOST); printf(SZ_HASHPRE); for(i = 0; i < 32; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_HAVAL]) { haval_end(&m_haval, pTemp); printf(SZ_HAVAL); printf(SZ_HASHPRE); for(i = 0; i < 32; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_MD2]) { m_md2.TruncatedFinal(pTemp, 16); printf(SZ_MD2); printf(SZ_HASHPRE); for(i = 0; i < 16; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_MD4]) { MD4Final(pTemp, &m_md4); printf(SZ_MD4); printf(SZ_HASHPRE); for(i = 0; i < 16; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_MD5]) { MD5Final(&m_md5); printf(SZ_MD5); printf(SZ_HASHPRE); for(i = 0; i < 16; i++) { fmtFixHashOutput(i); printf("%02X", m_md5.digest[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_SHA1]) { sha1_end(pTemp, &m_sha1); printf(SZ_SHA1); printf(SZ_HASHPRE); for(i = 0; i < 20; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_SHA2_256]) { sha256_end(pTemp, &m_sha256); printf(SZ_SHA2_256); printf(SZ_HASHPRE); for(i = 0; i < 32; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_SHA2_384]) { sha384_end(pTemp, &m_sha384); printf(SZ_SHA2_384); printf(SZ_HASHPRE); for(i = 0; i < 48; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_SHA2_512]) { sha512_end(pTemp, &m_sha512); printf(SZ_SHA2_512); printf(SZ_HASHPRE); for(i = 0; i < 64; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[i]); } printf(CPS_NEWLINE); } if(m_bAlgorithm[HASHID_TIGER]) { tiger_done(&m_tiger, pTemp); printf(SZ_TIGER); printf(SZ_HASHPRE); for(i = 0; i < 8; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[7-i]); } for(i = 8; i < 16; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[23-i]); } for(i = 16; i < 24; i++) { fmtFixHashOutput(i); printf("%02X", pTemp[39-i]); } printf(CPS_NEWLINE); } return RH_SUCCESS; }
/* Deprecated/legacy */ void hmac_sha1 ( char *k, /* secret key */ int lk, /* length of the key in bytes */ char *d, /* data */ int ld, /* length of data in bytes */ char *out, /* output buffer, at least "t" bytes */ int t ) { sha1_ctx ictx, octx; char isha[SHA1_DIGESTSIZE], osha[SHA1_DIGESTSIZE]; char key[SHA1_DIGESTSIZE]; char buf[SHA1_BLOCKSIZE]; int i; /* If the key is longer than the hash algorithm block size, let key = sha1(key), as per HMAC specifications. */ if (lk > SHA1_BLOCKSIZE) { sha1_ctx tctx; sha1_begin (&tctx); sha1_hash ((unsigned char *) k, lk, &tctx); sha1_end ((unsigned char *) key, &tctx); k = key; lk = SHA1_DIGESTSIZE; burn (&tctx, sizeof(tctx)); // Prevent leaks } /**** Inner Digest ****/ sha1_begin (&ictx); /* Pad the key for inner digest */ for (i = 0; i < lk; ++i) buf[i] = (char) (k[i] ^ 0x36); for (i = lk; i < SHA1_BLOCKSIZE; ++i) buf[i] = 0x36; sha1_hash ((unsigned char *) buf, SHA1_BLOCKSIZE, &ictx); sha1_hash ((unsigned char *) d, ld, &ictx); sha1_end ((unsigned char *) isha, &ictx); /**** Outer Digest ****/ sha1_begin (&octx); for (i = 0; i < lk; ++i) buf[i] = (char) (k[i] ^ 0x5C); for (i = lk; i < SHA1_BLOCKSIZE; ++i) buf[i] = 0x5C; sha1_hash ((unsigned char *) buf, SHA1_BLOCKSIZE, &octx); sha1_hash ((unsigned char *) isha, SHA1_DIGESTSIZE, &octx); sha1_end ((unsigned char *) osha, &octx); /* truncate and print the results */ t = t > SHA1_DIGESTSIZE ? SHA1_DIGESTSIZE : t; hmac_truncate (osha, out, t); /* Prevent leaks */ burn (&ictx, sizeof(ictx)); burn (&octx, sizeof(octx)); burn (isha, sizeof(isha)); burn (osha, sizeof(osha)); burn (buf, sizeof(buf)); burn (key, sizeof(key)); }