Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* memcpy_test.c */
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
- #include <string.h>
- #include "md5.h"
- #include "tools.h"
- #define BLOCK_SIZE (8LLU * 1024LLU * 1024LLU * 1024LLU)
- static void test(const uint8_t *const data, const size_t size, uint8_t *const digest)
- {
- md5_context_t ctx;
- md5_init(&ctx);
- md5_update(&ctx, data, size);
- memcpy(digest, md5_finalize(&ctx), 16U);
- }
- static void test2(const uint8_t *const data, const size_t size, uint8_t* const digest)
- {
- const uint8_t* p;
- uint64_t temp;
- md5_context_t ctx;
- const uint8_t* const end = data + size;
- md5_init(&ctx);
- for (p = data; p < end; p += sizeof(uint64_t))
- {
- memcpy(&temp, p, sizeof(uint64_t));
- md5_update(&ctx, (const uint8_t*)&temp, sizeof(uint64_t));
- }
- memcpy(digest, md5_finalize(&ctx), 16U);
- }
- int main()
- {
- uint8_t *p;
- uint8_t digest[16U];
- size_t i = 0LLU;
- clock_t clk_0, clk_1;
- int repeat = 1;
- fputs("Allocating memory, please wait...\n", stderr);
- uint8_t* const data = allocate_memory(BLOCK_SIZE);
- if (!data)
- {
- fputs("Allocation failed!", stderr);
- exit(-1);
- }
- fputs("Done.\n\n", stderr);
- fputs("Filling memory, please wait...\n", stderr);
- srand(42U);
- for (p = data; p < data + BLOCK_SIZE; p += sizeof(uint32_t))
- {
- *((uint32_t*)p) = (rand() & 0xFFF) | ((rand() & 0xFFF) << 12) | ((rand() & 0xFFF) << 24);
- if (++i >= 9973U)
- {
- fprintf(stderr, "\r%.1f%%", 100.0 * (((double)(p - data)) / ((double)BLOCK_SIZE)));
- i = 0U;
- }
- }
- fputs("\r100.0%\n\n", stderr);
- repeat:
- /* -------------------------- TEST #1 -------------------------- */
- fputs("Computing hash #1, please wait...\n", stderr);
- clk_0 = clock();
- test(data, BLOCK_SIZE, digest);
- clk_1 = clock();
- fputs("Done.\n\n", stderr);
- for (i = 0U; i < 16U; i++)
- {
- fprintf(stderr, "%02X", digest[i]);
- }
- fprintf(stderr, "\nTime #1: %lu\n\n", clk_1 - clk_0);
- /* -------------------------- TEST #2 -------------------------- */
- fputs("Computing hash #2, please wait...\n", stderr);
- clk_0 = clock();
- test2(data, BLOCK_SIZE, digest);
- clk_1 = clock();
- fputs("Done.\n\n", stderr);
- for (i = 0U; i < 16U; i++)
- {
- fprintf(stderr, "%02X", digest[i]);
- }
- fprintf(stderr, "\nTime #2: %lu\n\n", clk_1 - clk_0);
- /* -------------------------- FINISH -------------------------- */
- if (repeat > 0)
- {
- --repeat;
- goto repeat;
- }
- }
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* md5.h */
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- #ifndef MD5_H
- #define MD5_H
- #include <stdint.h>
- typedef struct
- {
- uint64_t size;
- uint32_t buffer[4U];
- uint8_t input[64U];
- uint8_t digest[16U];
- }
- md5_context_t;
- void md5_init(md5_context_t *const ctx);
- void md5_update(md5_context_t *const ctx, const uint8_t *const input_buffer, const size_t input_len);
- const uint8_t* md5_finalize(md5_context_t *const ctx);
- #endif
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* md5.c */
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- #include "md5.h"
- #define A 0x67452301
- #define B 0xefcdab89
- #define C 0x98badcfe
- #define D 0x10325476
- #pragma warning(disable: 6386)
- static uint32_t S[] =
- {
- 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22,
- 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20,
- 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23,
- 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21
- };
- static uint32_t K[] =
- {
- 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee,
- 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
- 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be,
- 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
- 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa,
- 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8,
- 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed,
- 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a,
- 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c,
- 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70,
- 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05,
- 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665,
- 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039,
- 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1,
- 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1,
- 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391
- };
- #define F(X, Y, Z) ((X & Y) | (~X & Z))
- #define G(X, Y, Z) ((X & Z) | (Y & ~Z))
- #define H(X, Y, Z) (X ^ Y ^ Z)
- #define I(X, Y, Z) (Y ^ (X | ~Z))
- static uint8_t PADDING[] =
- {
- 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- static uint32_t rotate_left(const uint32_t x, const uint32_t n)
- {
- return (x << n) | (x >> (32 - n));
- }
- static void md5_step(uint32_t *const buffer, const uint32_t *const input)
- {
- uint32_t AA = buffer[0];
- uint32_t BB = buffer[1];
- uint32_t CC = buffer[2];
- uint32_t DD = buffer[3];
- uint32_t E;
- unsigned int j;
- for (unsigned int i = 0; i < 64; ++i) {
- switch (i / 16) {
- case 0:
- E = F(BB, CC, DD);
- j = i;
- break;
- case 1:
- E = G(BB, CC, DD);
- j = ((i * 5) + 1) % 16;
- break;
- case 2:
- E = H(BB, CC, DD);
- j = ((i * 3) + 5) % 16;
- break;
- default:
- E = I(BB, CC, DD);
- j = (i * 7) % 16;
- break;
- }
- uint32_t temp = DD;
- DD = CC;
- CC = BB;
- BB = BB + rotate_left(AA + E + K[i] + input[j], S[i]);
- AA = temp;
- }
- buffer[0] += AA;
- buffer[1] += BB;
- buffer[2] += CC;
- buffer[3] += DD;
- }
- void md5_init(md5_context_t *const ctx)
- {
- ctx->size = (uint64_t)0;
- ctx->buffer[0] = (uint32_t)A;
- ctx->buffer[1] = (uint32_t)B;
- ctx->buffer[2] = (uint32_t)C;
- ctx->buffer[3] = (uint32_t)D;
- }
- void md5_update(md5_context_t *const ctx, const uint8_t *const input_buffer, const size_t input_len)
- {
- uint32_t input[16];
- unsigned int offset = ctx->size % 64;
- ctx->size += (uint64_t)input_len;
- for (size_t i = 0; i < input_len; ++i)
- {
- ctx->input[offset++] = (uint8_t)*(input_buffer + i);
- if (offset % 64 == 0)
- {
- for (unsigned int j = 0; j < 16; ++j)
- {
- input[j] =
- (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
- (uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
- (uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
- (uint32_t)(ctx->input[(j * 4)]);
- }
- md5_step(ctx->buffer, input);
- offset = 0;
- }
- }
- }
- const uint8_t *md5_finalize(md5_context_t *const ctx)
- {
- uint32_t input[16];
- unsigned int offset = ctx->size % 64;
- unsigned int padding_length = offset < 56 ? 56 - offset : (56 + 64) - offset;
- md5_update(ctx, PADDING, padding_length);
- ctx->size -= (uint64_t)padding_length;
- for (unsigned int j = 0; j < 14; ++j)
- {
- input[j] =
- (uint32_t)(ctx->input[(j * 4) + 3]) << 24 |
- (uint32_t)(ctx->input[(j * 4) + 2]) << 16 |
- (uint32_t)(ctx->input[(j * 4) + 1]) << 8 |
- (uint32_t)(ctx->input[(j * 4)]);
- }
- input[14] = (uint32_t)(ctx->size * 8);
- input[15] = (uint32_t)((ctx->size * 8) >> 32);
- md5_step(ctx->buffer, input);
- for (unsigned int i = 0; i < 4; ++i)
- {
- ctx->digest[(i * 4) + 0] = (uint8_t)((ctx->buffer[i] & 0x000000FF));
- ctx->digest[(i * 4) + 1] = (uint8_t)((ctx->buffer[i] & 0x0000FF00) >> 8);
- ctx->digest[(i * 4) + 2] = (uint8_t)((ctx->buffer[i] & 0x00FF0000) >> 16);
- ctx->digest[(i * 4) + 3] = (uint8_t)((ctx->buffer[i] & 0xFF000000) >> 24);
- }
- return ctx->digest;
- }
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* tools.h */
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- #ifndef TOOLS_H
- #define TOOLS_H
- #include <stdint.h>
- uint8_t* allocate_memory(const size_t size);
- #endif
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- /* tools.c */
- /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
- #define WIN32_LEAN_AND_MEAN 1
- #include "tools.h"
- #include <Windows.h>
- static volatile SIZE_T allocated = 204800LLU;
- uint8_t* allocate_memory(const size_t size)
- {
- if (SetProcessWorkingSetSize(GetCurrentProcess(), allocated + size, allocated + size))
- {
- const PVOID memory = VirtualAllocEx(GetCurrentProcess(), NULL, size, 0x3000, 0x204);
- if (memory)
- {
- if (VirtualLock(memory, size))
- {
- allocated += size;
- return (uint8_t*) memory;
- }
- else
- {
- VirtualFree(memory, 0U, MEM_RELEASE);
- }
- }
- }
- return NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment