void FFMS_Index::CalculateFileSignature(const char *Filename, int64_t *Filesize, uint8_t Digest[20]) { FileHandle file(Filename, "rb", FFMS_ERROR_INDEX, FFMS_ERROR_FILE_READ); #if VERSION_CHECK(LIBAVUTIL_VERSION_INT, >=, 51, 43, 0, 51, 75, 100) unknown_size<AVSHA, av_sha_alloc, ffms_free_sha> ctx; #else std::vector<uint8_t> ctxmem(av_sha_size); AVSHA *ctx = (AVSHA*)(&ctxmem[0]); #endif av_sha_init(ctx, 160); try { file.Seek(0, SEEK_END); *Filesize = file.Tell(); file.Seek(0, SEEK_SET); std::vector<char> FileBuffer(static_cast<size_t>(std::min<int64_t>(1024*1024, *Filesize))); size_t BytesRead = file.Read(&FileBuffer[0], FileBuffer.size()); av_sha_update(ctx, reinterpret_cast<const uint8_t*>(&FileBuffer[0]), BytesRead); if (*Filesize > static_cast<int64_t>(FileBuffer.size())) { file.Seek(-(int)FileBuffer.size(), SEEK_END); BytesRead = file.Read(&FileBuffer[0], FileBuffer.size()); av_sha_update(ctx, reinterpret_cast<const uint8_t*>(&FileBuffer[0]), BytesRead); } } catch (...) { av_sha_final(ctx, Digest); throw; } av_sha_final(ctx, Digest); }
static void digest_key(const char *key, const char *stash, uint8_t *d) { struct AVSHA *shactx = alloca(av_sha_size); av_sha_init(shactx, 160); av_sha_update(shactx, (const uint8_t *)key, strlen(key)); av_sha_update(shactx, (const uint8_t *)stash, strlen(stash)); av_sha_final(shactx, d); }
static uint32_t get_generic_seed(void) { uint8_t tmp[120]; struct AVSHA *sha = (void*)tmp; clock_t last_t = 0; static uint64_t i = 0; static uint32_t buffer[512] = { 0 }; unsigned char digest[20]; uint64_t last_i = i; av_assert0(sizeof(tmp) >= av_sha_size); if(TEST){ memset(buffer, 0, sizeof(buffer)); last_i = i = 0; }else{ #ifdef AV_READ_TIME buffer[13] ^= AV_READ_TIME(); buffer[41] ^= AV_READ_TIME()>>32; #endif } for (;;) { clock_t t = clock(); if (last_t == t) { buffer[i & 511]++; } else { buffer[++i & 511] += (t - last_t) % 3294638521U; if (last_i && i - last_i > 4 || i - last_i > 64 || TEST && i - last_i > 8) break; } last_t = t; } if(TEST) { buffer[0] = buffer[1] = 0; } else { #ifdef AV_READ_TIME buffer[111] += AV_READ_TIME(); #endif } av_sha_init(sha, 160); av_sha_update(sha, (const uint8_t *)buffer, sizeof(buffer)); av_sha_final(sha, digest); return AV_RB32(digest) + AV_RB32(digest + 16); }
int main(void) { int i, j, k; AVSHA ctx; unsigned char digest[32]; const int lengths[3] = { 160, 224, 256 }; for (j = 0; j < 3; j++) { printf("Testing SHA-%d\n", lengths[j]); for (k = 0; k < 3; k++) { av_sha_init(&ctx, lengths[j]); if (k == 0) av_sha_update(&ctx, "abc", 3); else if (k == 1) av_sha_update(&ctx, "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 56); else for (i = 0; i < 1000*1000; i++) av_sha_update(&ctx, "a", 1); av_sha_final(&ctx, digest); for (i = 0; i < lengths[j] >> 3; i++) printf("%02X", digest[i]); putchar('\n'); } switch (j) { case 0: //test vectors (from FIPS PUB 180-1) printf("A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D\n" "84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1\n" "34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F\n"); break; case 1: //test vectors (from FIPS PUB 180-2 Appendix A) printf("23097d22 3405d822 8642a477 bda255b3 2aadbce4 bda0b3f7 e36c9da7\n" "75388b16 512776cc 5dba5da1 fd890150 b0c6455c b4f58b19 52522525\n" "20794655 980c91d8 bbb4c1ea 97618a4b f03f4258 1948b2ee 4ee7ad67\n"); break; case 2: //test vectors (from FIPS PUB 180-2) printf("ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61 f20015ad\n" "248d6a61 d20638b8 e5c02693 0c3e6039 a33ce459 64ff2167 f6ecedd4 19db06c1\n" "cdc76e5c 9914fb92 81a1c7e2 84d73e67 f1809a48 a497200e 046d39cc c7112cd0\n"); break; } } return 0; }
bool CheckFrame(const FFMS_Frame *Frame, const FFMS_FrameInfo *info, const TestFrameData *Data) { EQ_CHECK(info->PTS, Data->PTS); EQ_CHECK(!!info->KeyFrame, Data->Keyframe); EQ_CHECK(!!Frame->KeyFrame, Data->Keyframe); EQ_CHECK(Frame->EncodedWidth, Data->Width); EQ_CHECK(Frame->EncodedHeight, Data->Height); const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get((AVPixelFormat) Frame->ConvertedPixelFormat); NULL_CHECK(desc); EXPECT_STREQ(desc->name, Data->PixelFormat); if (!!strcmp(desc->name, Data->PixelFormat)) return false; struct AVSHA *sha = av_sha_alloc(); NULL_CHECK(sha); int ret = av_sha_init(sha, 256); EQ_CHECK(ret, 0); for (int i = 0; i < av_pix_fmt_count_planes((AVPixelFormat) Frame->ConvertedPixelFormat); i++) { const int subh = i == 0 ? 0 : desc->log2_chroma_h; const int subw = i == 0 ? 0 : desc->log2_chroma_w; for (int y = 0; y < Frame->EncodedHeight >> subh; y++) av_sha_update(sha, Frame->Data[i] + y * Frame->Linesize[i], Frame->EncodedWidth >> subw); } uint8_t digest[32]; av_sha_final(sha, &digest[0]); av_free(sha); bool ok; EXPECT_TRUE((ok = !memcmp(&Data->SHA256[0], &digest[0], 32))); return ok; }
static htsmsg_t * htsp_reqreply(htsp_connection_t *hc, htsmsg_t *m) { void *buf; size_t len; uint32_t seq; int r; tcpcon_t *tc = hc->hc_tc; uint32_t noaccess; htsmsg_t *reply; htsp_msg_t *hm = NULL; int retry = 0; char id[100]; char *username; char *password; struct AVSHA *shactx = alloca(av_sha_size); uint8_t d[20]; if(tc == NULL) return NULL; /* Generate a sequence number for our message */ seq = atomic_add(&hc->hc_seq_generator, 1); htsmsg_add_u32(m, "seq", seq); again: snprintf(id, sizeof(id), "htsp://%s:%d", hc->hc_hostname, hc->hc_port); r = keyring_lookup(id, &username, &password, NULL, NULL, "TV client", "Access denied", (retry ? KEYRING_QUERY_USER : 0) | KEYRING_SHOW_REMEMBER_ME | KEYRING_REMEMBER_ME_SET); if(r == -1) { /* User rejected */ return NULL; } if(r == 0) { /* Got auth credentials */ htsmsg_delete_field(m, "username"); htsmsg_delete_field(m, "digest"); if(username != NULL) htsmsg_add_str(m, "username", username); if(password != NULL) { av_sha_init(shactx, 160); av_sha_update(shactx, (const uint8_t *)password, strlen(password)); av_sha_update(shactx, hc->hc_challenge, 32); av_sha_final(shactx, d); htsmsg_add_bin(m, "digest", d, 20); } free(username); free(password); } if(htsmsg_binary_serialize(m, &buf, &len, -1) < 0) { htsmsg_destroy(m); return NULL; } if(hc->hc_is_async) { /* Async, set up a struct that will be signalled when we get a reply */ hm = malloc(sizeof(htsp_msg_t)); hm->hm_msg = NULL; hm->hm_seq = seq; hm->hm_error = 0; hts_mutex_lock(&hc->hc_rpc_mutex); TAILQ_INSERT_TAIL(&hc->hc_rpc_queue, hm, hm_link); hts_mutex_unlock(&hc->hc_rpc_mutex); } if(tc->write(tc, buf, len)) { free(buf); htsmsg_destroy(m); if(hm != NULL) { hts_mutex_lock(&hc->hc_rpc_mutex); TAILQ_REMOVE(&hc->hc_rpc_queue, hm, hm_link); hts_mutex_unlock(&hc->hc_rpc_mutex); free(hm); } return NULL; } free(buf); if(hm != NULL) { hts_mutex_lock(&hc->hc_rpc_mutex); while(1) { if(hm->hm_error != 0) { r = hm->hm_error; TAILQ_REMOVE(&hc->hc_rpc_queue, hm, hm_link); hts_mutex_unlock(&hc->hc_rpc_mutex); free(hm); htsmsg_destroy(m); return NULL; } if(hm->hm_msg != NULL) break; hts_cond_wait(&hc->hc_rpc_cond, &hc->hc_rpc_mutex); } TAILQ_REMOVE(&hc->hc_rpc_queue, hm, hm_link); hts_mutex_unlock(&hc->hc_rpc_mutex); reply = hm->hm_msg; free(hm); } else { if((reply = htsp_recv(hc)) == NULL) { htsmsg_destroy(m); return NULL; } } if(!htsmsg_get_u32(reply, "noaccess", &noaccess) && noaccess) { retry++; goto again; } htsmsg_destroy(m); /* Destroy original message */ return reply; }