1 /* ----------------------------------------------------------------------------------------------- */
2 /* MHash-384 - Generate tables utility program */
3 /* Copyright(c) 2016 LoRd_MuldeR <mulder2@gmx.de> */
5 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software */
6 /* and associated documentation files(the "Software"), to deal in the Software without */
7 /* restriction, including without limitation the rights to use, copy, modify, merge, publish, */
8 /* distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the */
9 /* Software is furnished to do so, subject to the following conditions: */
11 /* The above copyright notice and this permission notice shall be included in all copies or */
12 /* substantial portions of the Software. */
14 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING */
15 /* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
16 /* NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, */
17 /* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
18 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
19 /* ----------------------------------------------------------------------------------------------- */
22 #include "thread_utils.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
37 #define ROW_NUM (UINT8_MAX+1) /*total number of rows*/
38 #define ROW_LEN (HASH_LEN / CHAR_BIT) /*number of bits per row*/
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 static uint8_t g_table[ROW_NUM][HASH_LEN];
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 static inline void build_permutation(uint8_t *const row_buffer, twister_t *const rand)
52 for (uint32_t i = 0; i < ROW_LEN; i++)
54 row_buffer[i] = (uint8_t)(i + (next_rand_range(rand, ROW_LEN - i)));
58 static inline void apply_permutation(uint8_t *const row_buffe, const uint8_t *const permutation)
61 for (size_t i = 0; i < ROW_LEN; ++i)
63 tmp = row_buffe[permutation[i]];
64 row_buffe[permutation[i]] = row_buffe[i];
69 static inline bool test_permutation(const uint8_t *const permutation)
71 uint8_t row_buffe[ROW_LEN];
72 for (size_t i = 0; i < ROW_LEN; ++i)
74 row_buffe[i] = ((uint8_t)i);
77 apply_permutation(row_buffe, permutation);
78 for (size_t i = 0; i < ROW_LEN; ++i)
80 if (row_buffe[i] == ((uint8_t)i))
89 static dump_table(FILE *out)
91 fputs("uint8_t MHASH_384_TABLE_MIX[UINT8_MAX+1][MHASH_384_LEN] =\n{\n", out);
92 for (size_t i = 0; i < ROW_NUM; i++)
95 for (size_t j = 0; j < ROW_LEN; j++)
101 fprintf(out, "0x%02X", g_table[i][j]);
103 fprintf(out, " }%s /*%02X*/\n", (i != (ROW_NUM - 1)) ? "," : " ", (uint32_t)(i % 0x100));
108 //-----------------------------------------------------------------------------
110 //-----------------------------------------------------------------------------
114 FILE *file_out = NULL;
116 printf("MHash GenTableMIX [%s]\n\n", __DATE__);
117 printf("HashLen: %d\n\n", HASH_LEN);
119 if ((HASH_LEN % (8 * sizeof(uint32_t))) != 0)
121 crit_exit("FATAL: Hash length must be a multiple of 32 bits!");
125 rand_init(&rand, make_seed());
127 for (size_t i = 0; i < ROW_NUM; ++i)
129 printf("Row %03u of %03u...", (uint32_t)(i + 1U), ROW_NUM);
133 build_permutation(&g_table[i][0], &rand);
135 while(!test_permutation(&g_table[i][0]));
139 printf("\n-----\n\n");
142 if (fopen_s(&file_out, "table_out.MIX.txt", "w") == 0)
144 dump_table(file_out);
148 printf("\n-----\n\n");
150 printf("COMPLETED.\n\n");