/** * Author......: See docs/credits.txt * License.....: MIT */ #include "inc_vendor.h" #include "inc_types.h" #include "inc_platform.h" #include "inc_common.h" #include "inc_hash_md4.h" // important notes on this: // input buf unused bytes needs to be set to zero // input buf needs to be in algorithm native byte order (md4 = LE, sha1 = BE, etc) // input buf needs to be 64 byte aligned when using md4_update() DECLSPEC void md4_transform (const u32 *w0, const u32 *w1, const u32 *w2, const u32 *w3, u32 *digest) { u32 a = digest[0]; u32 b = digest[1]; u32 c = digest[2]; u32 d = digest[3]; MD4_STEP_S (MD4_Fo, a, b, c, d, w0[0], MD4C00, MD4S00); MD4_STEP_S (MD4_Fo, d, a, b, c, w0[1], MD4C00, MD4S01); MD4_STEP_S (MD4_Fo, c, d, a, b, w0[2], MD4C00, MD4S02); MD4_STEP_S (MD4_Fo, b, c, d, a, w0[3], MD4C00, MD4S03); MD4_STEP_S (MD4_Fo, a, b, c, d, w1[0], MD4C00, MD4S00); MD4_STEP_S (MD4_Fo, d, a, b, c, w1[1], MD4C00, MD4S01); MD4_STEP_S (MD4_Fo, c, d, a, b, w1[2], MD4C00, MD4S02); MD4_STEP_S (MD4_Fo, b, c, d, a, w1[3], MD4C00, MD4S03); MD4_STEP_S (MD4_Fo, a, b, c, d, w2[0], MD4C00, MD4S00); MD4_STEP_S (MD4_Fo, d, a, b, c, w2[1], MD4C00, MD4S01); MD4_STEP_S (MD4_Fo, c, d, a, b, w2[2], MD4C00, MD4S02); MD4_STEP_S (MD4_Fo, b, c, d, a, w2[3], MD4C00, MD4S03); MD4_STEP_S (MD4_Fo, a, b, c, d, w3[0], MD4C00, MD4S00); MD4_STEP_S (MD4_Fo, d, a, b, c, w3[1], MD4C00, MD4S01); MD4_STEP_S (MD4_Fo, c, d, a, b, w3[2], MD4C00, MD4S02); MD4_STEP_S (MD4_Fo, b, c, d, a, w3[3], MD4C00, MD4S03); MD4_STEP_S (MD4_Go, a, b, c, d, w0[0], MD4C01, MD4S10); MD4_STEP_S (MD4_Go, d, a, b, c, w1[0], MD4C01, MD4S11); MD4_STEP_S (MD4_Go, c, d, a, b, w2[0], MD4C01, MD4S12); MD4_STEP_S (MD4_Go, b, c, d, a, w3[0], MD4C01, MD4S13); MD4_STEP_S (MD4_Go, a, b, c, d, w0[1], MD4C01, MD4S10); MD4_STEP_S (MD4_Go, d, a, b, c, w1[1], MD4C01, MD4S11); MD4_STEP_S (MD4_Go, c, d, a, b, w2[1], MD4C01, MD4S12); MD4_STEP_S (MD4_Go, b, c, d, a, w3[1], MD4C01, MD4S13); MD4_STEP_S (MD4_Go, a, b, c, d, w0[2], MD4C01, MD4S10); MD4_STEP_S (MD4_Go, d, a, b, c, w1[2], MD4C01, MD4S11); MD4_STEP_S (MD4_Go, c, d, a, b, w2[2], MD4C01, MD4S12); MD4_STEP_S (MD4_Go, b, c, d, a, w3[2], MD4C01, MD4S13); MD4_STEP_S (MD4_Go, a, b, c, d, w0[3], MD4C01, MD4S10); MD4_STEP_S (MD4_Go, d, a, b, c, w1[3], MD4C01, MD4S11); MD4_STEP_S (MD4_Go, c, d, a, b, w2[3], MD4C01, MD4S12); MD4_STEP_S (MD4_Go, b, c, d, a, w3[3], MD4C01, MD4S13); MD4_STEP_S (MD4_H , a, b, c, d, w0[0], MD4C02, MD4S20); MD4_STEP_S (MD4_H , d, a, b, c, w2[0], MD4C02, MD4S21); MD4_STEP_S (MD4_H , c, d, a, b, w1[0], MD4C02, MD4S22); MD4_STEP_S (MD4_H , b, c, d, a, w3[0], MD4C02, MD4S23); MD4_STEP_S (MD4_H , a, b, c, d, w0[2], MD4C02, MD4S20); MD4_STEP_S (MD4_H , d, a, b, c, w2[2], MD4C02, MD4S21); MD4_STEP_S (MD4_H , c, d, a, b, w1[2], MD4C02, MD4S22); MD4_STEP_S (MD4_H , b, c, d, a, w3[2], MD4C02, MD4S23); MD4_STEP_S (MD4_H , a, b, c, d, w0[1], MD4C02, MD4S20); MD4_STEP_S (MD4_H , d, a, b, c, w2[1], MD4C02, MD4S21); MD4_STEP_S (MD4_H , c, d, a, b, w1[1], MD4C02, MD4S22); MD4_STEP_S (MD4_H , b, c, d, a, w3[1], MD4C02, MD4S23); MD4_STEP_S (MD4_H , a, b, c, d, w0[3], MD4C02, MD4S20); MD4_STEP_S (MD4_H , d, a, b, c, w2[3], MD4C02, MD4S21); MD4_STEP_S (MD4_H , c, d, a, b, w1[3], MD4C02, MD4S22); MD4_STEP_S (MD4_H , b, c, d, a, w3[3], MD4C02, MD4S23); digest[0] += a; digest[1] += b; digest[2] += c; digest[3] += d; } DECLSPEC void md4_init (md4_ctx_t *ctx) { ctx->h[0] = MD4M_A; ctx->h[1] = MD4M_B; ctx->h[2] = MD4M_C; ctx->h[3] = MD4M_D; ctx->w0[0] = 0; ctx->w0[1] = 0; ctx->w0[2] = 0; ctx->w0[3] = 0; ctx->w1[0] = 0; ctx->w1[1] = 0; ctx->w1[2] = 0; ctx->w1[3] = 0; ctx->w2[0] = 0; ctx->w2[1] = 0; ctx->w2[2] = 0; ctx->w2[3] = 0; ctx->w3[0] = 0; ctx->w3[1] = 0; ctx->w3[2] = 0; ctx->w3[3] = 0; ctx->len = 0; } DECLSPEC void md4_update_64 (md4_ctx_t *ctx, u32 *w0, u32 *w1, u32 *w2, u32 *w3, const int len) { MAYBE_VOLATILE const int pos = ctx->len & 63; ctx->len += len; if (pos == 0) { ctx->w0[0] = w0[0]; ctx->w0[1] = w0[1]; ctx->w0[2] = w0[2]; ctx->w0[3] = w0[3]; ctx->w1[0] = w1[0]; ctx->w1[1] = w1[1]; ctx->w1[2] = w1[2]; ctx->w1[3] = w1[3]; ctx->w2[0] = w2[0]; ctx->w2[1] = w2[1]; ctx->w2[2] = w2[2]; ctx->w2[3] = w2[3]; ctx->w3[0] = w3[0]; ctx->w3[1] = w3[1]; ctx->w3[2] = w3[2]; ctx->w3[3] = w3[3]; if (len == 64) { md4_transform (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); ctx->w0[0] = 0; ctx->w0[1] = 0; ctx->w0[2] = 0; ctx->w0[3] = 0; ctx->w1[0] = 0; ctx->w1[1] = 0; ctx->w1[2] = 0; ctx->w1[3] = 0; ctx->w2[0] = 0; ctx->w2[1] = 0; ctx->w2[2] = 0; ctx->w2[3] = 0; ctx->w3[0] = 0; ctx->w3[1] = 0; ctx->w3[2] = 0; ctx->w3[3] = 0; } } else { if ((pos + len) < 64) { switch_buffer_by_offset_le_S (w0, w1, w2, w3, pos); ctx->w0[0] |= w0[0]; ctx->w0[1] |= w0[1]; ctx->w0[2] |= w0[2]; ctx->w0[3] |= w0[3]; ctx->w1[0] |= w1[0]; ctx->w1[1] |= w1[1]; ctx->w1[2] |= w1[2]; ctx->w1[3] |= w1[3]; ctx->w2[0] |= w2[0]; ctx->w2[1] |= w2[1]; ctx->w2[2] |= w2[2]; ctx->w2[3] |= w2[3]; ctx->w3[0] |= w3[0]; ctx->w3[1] |= w3[1]; ctx->w3[2] |= w3[2]; ctx->w3[3] |= w3[3]; } else { u32 c0[4] = { 0 }; u32 c1[4] = { 0 }; u32 c2[4] = { 0 }; u32 c3[4] = { 0 }; switch_buffer_by_offset_carry_le_S (w0, w1, w2, w3, c0, c1, c2, c3, pos); ctx->w0[0] |= w0[0]; ctx->w0[1] |= w0[1]; ctx->w0[2] |= w0[2]; ctx->w0[3] |= w0[3]; ctx->w1[0] |= w1[0]; ctx->w1[1] |= w1[1]; ctx->w1[2] |= w1[2]; ctx->w1[3] |= w1[3]; ctx->w2[0] |= w2[0]; ctx->w2[1] |= w2[1]; ctx->w2[2] |= w2[2]; ctx->w2[3] |= w2[3]; ctx->w3[0] |= w3[0]; ctx->w3[1] |= w3[1]; ctx->w3[2] |= w3[2]; ctx->w3[3] |= w3[3]; md4_transform (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); ctx->w0[0] = c0[0]; ctx->w0[1] = c0[1]; ctx->w0[2] = c0[2]; ctx->w0[3] = c0[3]; ctx->w1[0] = c1[0]; ctx->w1[1] = c1[1]; ctx->w1[2] = c1[2]; ctx->w1[3] = c1[3]; ctx->w2[0] = c2[0]; ctx->w2[1] = c2[1]; ctx->w2[2] = c2[2]; ctx->w2[3] = c2[3]; ctx->w3[0] = c3[0]; ctx->w3[1] = c3[1]; ctx->w3[2] = c3[2]; ctx->w3[3] = c3[3]; } } } DECLSPEC void md4_update (md4_ctx_t *ctx, const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 64; pos1 += 64, pos4 += 16) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; md4_update_64 (ctx, w0, w1, w2, w3, 64); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; md4_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void md4_update_swap (md4_ctx_t *ctx, const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 64; pos1 += 64, pos4 += 16) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, 64); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void md4_update_utf16le (md4_ctx_t *ctx, const u32 *w, const int len) { if (test_any_8th_bit (w, len) == 1) { u32 w_utf16_buf[256]; const int w_utf16_len = utf8_to_utf16le (w, len, 256, w_utf16_buf, sizeof (w_utf16_buf)); const int blkoff = (w_utf16_len / 64) * 16; u32 *w_ptr = w_utf16_buf + blkoff; truncate_block_16x4_le_S (w_ptr + 0, w_ptr + 4, w_ptr + 8, w_ptr + 12, w_utf16_len & 63); md4_update (ctx, w_utf16_buf, w_utf16_len); return; } u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 32; pos1 += 32, pos4 += 8) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); md4_update_64 (ctx, w0, w1, w2, w3, 32 * 2); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); md4_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void md4_update_utf16le_swap (md4_ctx_t *ctx, const u32 *w, const int len) { if (test_any_8th_bit (w, len) == 1) { u32 w_utf16_buf[256]; const int w_utf16_len = utf8_to_utf16le (w, len, 256, w_utf16_buf, sizeof (w_utf16_buf)); const int blkoff = (w_utf16_len / 64) * 16; u32 *w_ptr = w_utf16_buf + blkoff; truncate_block_16x4_le_S (w_ptr + 0, w_ptr + 4, w_ptr + 8, w_ptr + 12, w_utf16_len & 63); md4_update_swap (ctx, w_utf16_buf, w_utf16_len); return; } u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 32; pos1 += 32, pos4 += 8) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, 32 * 2); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void md4_update_global (md4_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 64; pos1 += 64, pos4 += 16) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; md4_update_64 (ctx, w0, w1, w2, w3, 64); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; md4_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void md4_update_global_swap (md4_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 64; pos1 += 64, pos4 += 16) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, 64); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void md4_update_global_utf16le (md4_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { if (test_any_8th_bit (w, len) == 1) { u32 w_utf16_buf[256]; const int w_utf16_len = utf8_to_utf16le_global (w, len, 256, w_utf16_buf, sizeof (w_utf16_buf)); const int blkoff = (w_utf16_len / 64) * 16; u32 *w_ptr = w_utf16_buf + blkoff; truncate_block_16x4_le_S (w_ptr + 0, w_ptr + 4, w_ptr + 8, w_ptr + 12, w_utf16_len & 63); md4_update (ctx, w_utf16_buf, w_utf16_len); return; } u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 32; pos1 += 32, pos4 += 8) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); md4_update_64 (ctx, w0, w1, w2, w3, 32 * 2); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); md4_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void md4_update_global_utf16le_swap (md4_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { if (test_any_8th_bit (w, len) == 1) { u32 w_utf16_buf[256]; const int w_utf16_len = utf8_to_utf16le_global (w, len, 256, w_utf16_buf, sizeof (w_utf16_buf)); const int blkoff = (w_utf16_len / 64) * 16; u32 *w_ptr = w_utf16_buf + blkoff; truncate_block_16x4_le_S (w_ptr + 0, w_ptr + 4, w_ptr + 8, w_ptr + 12, w_utf16_len & 63); md4_update_swap (ctx, w_utf16_buf, w_utf16_len); return; } u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 32; pos1 += 32, pos4 += 8) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, 32 * 2); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le_S (w1, w2, w3); make_utf16le_S (w0, w0, w1); w0[0] = hc_swap32_S (w0[0]); w0[1] = hc_swap32_S (w0[1]); w0[2] = hc_swap32_S (w0[2]); w0[3] = hc_swap32_S (w0[3]); w1[0] = hc_swap32_S (w1[0]); w1[1] = hc_swap32_S (w1[1]); w1[2] = hc_swap32_S (w1[2]); w1[3] = hc_swap32_S (w1[3]); w2[0] = hc_swap32_S (w2[0]); w2[1] = hc_swap32_S (w2[1]); w2[2] = hc_swap32_S (w2[2]); w2[3] = hc_swap32_S (w2[3]); w3[0] = hc_swap32_S (w3[0]); w3[1] = hc_swap32_S (w3[1]); w3[2] = hc_swap32_S (w3[2]); w3[3] = hc_swap32_S (w3[3]); md4_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void md4_final (md4_ctx_t *ctx) { MAYBE_VOLATILE const int pos = ctx->len & 63; append_0x80_4x4_S (ctx->w0, ctx->w1, ctx->w2, ctx->w3, pos); if (pos >= 56) { md4_transform (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); ctx->w0[0] = 0; ctx->w0[1] = 0; ctx->w0[2] = 0; ctx->w0[3] = 0; ctx->w1[0] = 0; ctx->w1[1] = 0; ctx->w1[2] = 0; ctx->w1[3] = 0; ctx->w2[0] = 0; ctx->w2[1] = 0; ctx->w2[2] = 0; ctx->w2[3] = 0; ctx->w3[0] = 0; ctx->w3[1] = 0; ctx->w3[2] = 0; ctx->w3[3] = 0; } ctx->w3[2] = ctx->len * 8; ctx->w3[3] = 0; md4_transform (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); } // md4_hmac DECLSPEC void md4_hmac_init_64 (md4_hmac_ctx_t *ctx, const u32 *w0, const u32 *w1, const u32 *w2, const u32 *w3) { u32 t0[4]; u32 t1[4]; u32 t2[4]; u32 t3[4]; // ipad t0[0] = w0[0] ^ 0x36363636; t0[1] = w0[1] ^ 0x36363636; t0[2] = w0[2] ^ 0x36363636; t0[3] = w0[3] ^ 0x36363636; t1[0] = w1[0] ^ 0x36363636; t1[1] = w1[1] ^ 0x36363636; t1[2] = w1[2] ^ 0x36363636; t1[3] = w1[3] ^ 0x36363636; t2[0] = w2[0] ^ 0x36363636; t2[1] = w2[1] ^ 0x36363636; t2[2] = w2[2] ^ 0x36363636; t2[3] = w2[3] ^ 0x36363636; t3[0] = w3[0] ^ 0x36363636; t3[1] = w3[1] ^ 0x36363636; t3[2] = w3[2] ^ 0x36363636; t3[3] = w3[3] ^ 0x36363636; md4_init (&ctx->ipad); md4_update_64 (&ctx->ipad, t0, t1, t2, t3, 64); // opad t0[0] = w0[0] ^ 0x5c5c5c5c; t0[1] = w0[1] ^ 0x5c5c5c5c; t0[2] = w0[2] ^ 0x5c5c5c5c; t0[3] = w0[3] ^ 0x5c5c5c5c; t1[0] = w1[0] ^ 0x5c5c5c5c; t1[1] = w1[1] ^ 0x5c5c5c5c; t1[2] = w1[2] ^ 0x5c5c5c5c; t1[3] = w1[3] ^ 0x5c5c5c5c; t2[0] = w2[0] ^ 0x5c5c5c5c; t2[1] = w2[1] ^ 0x5c5c5c5c; t2[2] = w2[2] ^ 0x5c5c5c5c; t2[3] = w2[3] ^ 0x5c5c5c5c; t3[0] = w3[0] ^ 0x5c5c5c5c; t3[1] = w3[1] ^ 0x5c5c5c5c; t3[2] = w3[2] ^ 0x5c5c5c5c; t3[3] = w3[3] ^ 0x5c5c5c5c; md4_init (&ctx->opad); md4_update_64 (&ctx->opad, t0, t1, t2, t3, 64); } DECLSPEC void md4_hmac_init (md4_hmac_ctx_t *ctx, const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; if (len > 64) { md4_ctx_t tmp; md4_init (&tmp); md4_update (&tmp, w, len); md4_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = 0; w1[1] = 0; w1[2] = 0; w1[3] = 0; w2[0] = 0; w2[1] = 0; w2[2] = 0; w2[3] = 0; w3[0] = 0; w3[1] = 0; w3[2] = 0; w3[3] = 0; } else { w0[0] = w[ 0]; w0[1] = w[ 1]; w0[2] = w[ 2]; w0[3] = w[ 3]; w1[0] = w[ 4]; w1[1] = w[ 5]; w1[2] = w[ 6]; w1[3] = w[ 7]; w2[0] = w[ 8]; w2[1] = w[ 9]; w2[2] = w[10]; w2[3] = w[11]; w3[0] = w[12]; w3[1] = w[13]; w3[2] = w[14]; w3[3] = w[15]; } md4_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void md4_hmac_init_swap (md4_hmac_ctx_t *ctx, const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; if (len > 64) { md4_ctx_t tmp; md4_init (&tmp); md4_update_swap (&tmp, w, len); md4_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = 0; w1[1] = 0; w1[2] = 0; w1[3] = 0; w2[0] = 0; w2[1] = 0; w2[2] = 0; w2[3] = 0; w3[0] = 0; w3[1] = 0; w3[2] = 0; w3[3] = 0; } else { w0[0] = hc_swap32_S (w[ 0]); w0[1] = hc_swap32_S (w[ 1]); w0[2] = hc_swap32_S (w[ 2]); w0[3] = hc_swap32_S (w[ 3]); w1[0] = hc_swap32_S (w[ 4]); w1[1] = hc_swap32_S (w[ 5]); w1[2] = hc_swap32_S (w[ 6]); w1[3] = hc_swap32_S (w[ 7]); w2[0] = hc_swap32_S (w[ 8]); w2[1] = hc_swap32_S (w[ 9]); w2[2] = hc_swap32_S (w[10]); w2[3] = hc_swap32_S (w[11]); w3[0] = hc_swap32_S (w[12]); w3[1] = hc_swap32_S (w[13]); w3[2] = hc_swap32_S (w[14]); w3[3] = hc_swap32_S (w[15]); } md4_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void md4_hmac_init_global (md4_hmac_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; if (len > 64) { md4_ctx_t tmp; md4_init (&tmp); md4_update_global (&tmp, w, len); md4_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = 0; w1[1] = 0; w1[2] = 0; w1[3] = 0; w2[0] = 0; w2[1] = 0; w2[2] = 0; w2[3] = 0; w3[0] = 0; w3[1] = 0; w3[2] = 0; w3[3] = 0; } else { w0[0] = w[ 0]; w0[1] = w[ 1]; w0[2] = w[ 2]; w0[3] = w[ 3]; w1[0] = w[ 4]; w1[1] = w[ 5]; w1[2] = w[ 6]; w1[3] = w[ 7]; w2[0] = w[ 8]; w2[1] = w[ 9]; w2[2] = w[10]; w2[3] = w[11]; w3[0] = w[12]; w3[1] = w[13]; w3[2] = w[14]; w3[3] = w[15]; } md4_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void md4_hmac_init_global_swap (md4_hmac_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; if (len > 64) { md4_ctx_t tmp; md4_init (&tmp); md4_update_global_swap (&tmp, w, len); md4_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = 0; w1[1] = 0; w1[2] = 0; w1[3] = 0; w2[0] = 0; w2[1] = 0; w2[2] = 0; w2[3] = 0; w3[0] = 0; w3[1] = 0; w3[2] = 0; w3[3] = 0; } else { w0[0] = hc_swap32_S (w[ 0]); w0[1] = hc_swap32_S (w[ 1]); w0[2] = hc_swap32_S (w[ 2]); w0[3] = hc_swap32_S (w[ 3]); w1[0] = hc_swap32_S (w[ 4]); w1[1] = hc_swap32_S (w[ 5]); w1[2] = hc_swap32_S (w[ 6]); w1[3] = hc_swap32_S (w[ 7]); w2[0] = hc_swap32_S (w[ 8]); w2[1] = hc_swap32_S (w[ 9]); w2[2] = hc_swap32_S (w[10]); w2[3] = hc_swap32_S (w[11]); w3[0] = hc_swap32_S (w[12]); w3[1] = hc_swap32_S (w[13]); w3[2] = hc_swap32_S (w[14]); w3[3] = hc_swap32_S (w[15]); } md4_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void md4_hmac_update_64 (md4_hmac_ctx_t *ctx, u32 *w0, u32 *w1, u32 *w2, u32 *w3, const int len) { md4_update_64 (&ctx->ipad, w0, w1, w2, w3, len); } DECLSPEC void md4_hmac_update (md4_hmac_ctx_t *ctx, const u32 *w, const int len) { md4_update (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_update_swap (md4_hmac_ctx_t *ctx, const u32 *w, const int len) { md4_update_swap (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_update_utf16le (md4_hmac_ctx_t *ctx, const u32 *w, const int len) { md4_update_utf16le (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_update_utf16le_swap (md4_hmac_ctx_t *ctx, const u32 *w, const int len) { md4_update_utf16le_swap (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_update_global (md4_hmac_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { md4_update_global (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_update_global_swap (md4_hmac_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { md4_update_global_swap (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_update_global_utf16le (md4_hmac_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { md4_update_global_utf16le (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_update_global_utf16le_swap (md4_hmac_ctx_t *ctx, GLOBAL_AS const u32 *w, const int len) { md4_update_global_utf16le_swap (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_final (md4_hmac_ctx_t *ctx) { md4_final (&ctx->ipad); ctx->opad.w0[0] = ctx->ipad.h[0]; ctx->opad.w0[1] = ctx->ipad.h[1]; ctx->opad.w0[2] = ctx->ipad.h[2]; ctx->opad.w0[3] = ctx->ipad.h[3]; ctx->opad.w1[0] = 0; ctx->opad.w1[1] = 0; ctx->opad.w1[2] = 0; ctx->opad.w1[3] = 0; ctx->opad.w2[0] = 0; ctx->opad.w2[1] = 0; ctx->opad.w2[2] = 0; ctx->opad.w2[3] = 0; ctx->opad.w3[0] = 0; ctx->opad.w3[1] = 0; ctx->opad.w3[2] = 0; ctx->opad.w3[3] = 0; ctx->opad.len += 16; md4_final (&ctx->opad); } // while input buf can be a vector datatype, the length of the different elements can not DECLSPEC void md4_transform_vector (const u32x *w0, const u32x *w1, const u32x *w2, const u32x *w3, u32x *digest) { u32x a = digest[0]; u32x b = digest[1]; u32x c = digest[2]; u32x d = digest[3]; MD4_STEP (MD4_Fo, a, b, c, d, w0[0], MD4C00, MD4S00); MD4_STEP (MD4_Fo, d, a, b, c, w0[1], MD4C00, MD4S01); MD4_STEP (MD4_Fo, c, d, a, b, w0[2], MD4C00, MD4S02); MD4_STEP (MD4_Fo, b, c, d, a, w0[3], MD4C00, MD4S03); MD4_STEP (MD4_Fo, a, b, c, d, w1[0], MD4C00, MD4S00); MD4_STEP (MD4_Fo, d, a, b, c, w1[1], MD4C00, MD4S01); MD4_STEP (MD4_Fo, c, d, a, b, w1[2], MD4C00, MD4S02); MD4_STEP (MD4_Fo, b, c, d, a, w1[3], MD4C00, MD4S03); MD4_STEP (MD4_Fo, a, b, c, d, w2[0], MD4C00, MD4S00); MD4_STEP (MD4_Fo, d, a, b, c, w2[1], MD4C00, MD4S01); MD4_STEP (MD4_Fo, c, d, a, b, w2[2], MD4C00, MD4S02); MD4_STEP (MD4_Fo, b, c, d, a, w2[3], MD4C00, MD4S03); MD4_STEP (MD4_Fo, a, b, c, d, w3[0], MD4C00, MD4S00); MD4_STEP (MD4_Fo, d, a, b, c, w3[1], MD4C00, MD4S01); MD4_STEP (MD4_Fo, c, d, a, b, w3[2], MD4C00, MD4S02); MD4_STEP (MD4_Fo, b, c, d, a, w3[3], MD4C00, MD4S03); MD4_STEP (MD4_Go, a, b, c, d, w0[0], MD4C01, MD4S10); MD4_STEP (MD4_Go, d, a, b, c, w1[0], MD4C01, MD4S11); MD4_STEP (MD4_Go, c, d, a, b, w2[0], MD4C01, MD4S12); MD4_STEP (MD4_Go, b, c, d, a, w3[0], MD4C01, MD4S13); MD4_STEP (MD4_Go, a, b, c, d, w0[1], MD4C01, MD4S10); MD4_STEP (MD4_Go, d, a, b, c, w1[1], MD4C01, MD4S11); MD4_STEP (MD4_Go, c, d, a, b, w2[1], MD4C01, MD4S12); MD4_STEP (MD4_Go, b, c, d, a, w3[1], MD4C01, MD4S13); MD4_STEP (MD4_Go, a, b, c, d, w0[2], MD4C01, MD4S10); MD4_STEP (MD4_Go, d, a, b, c, w1[2], MD4C01, MD4S11); MD4_STEP (MD4_Go, c, d, a, b, w2[2], MD4C01, MD4S12); MD4_STEP (MD4_Go, b, c, d, a, w3[2], MD4C01, MD4S13); MD4_STEP (MD4_Go, a, b, c, d, w0[3], MD4C01, MD4S10); MD4_STEP (MD4_Go, d, a, b, c, w1[3], MD4C01, MD4S11); MD4_STEP (MD4_Go, c, d, a, b, w2[3], MD4C01, MD4S12); MD4_STEP (MD4_Go, b, c, d, a, w3[3], MD4C01, MD4S13); MD4_STEP (MD4_H , a, b, c, d, w0[0], MD4C02, MD4S20); MD4_STEP (MD4_H , d, a, b, c, w2[0], MD4C02, MD4S21); MD4_STEP (MD4_H , c, d, a, b, w1[0], MD4C02, MD4S22); MD4_STEP (MD4_H , b, c, d, a, w3[0], MD4C02, MD4S23); MD4_STEP (MD4_H , a, b, c, d, w0[2], MD4C02, MD4S20); MD4_STEP (MD4_H , d, a, b, c, w2[2], MD4C02, MD4S21); MD4_STEP (MD4_H , c, d, a, b, w1[2], MD4C02, MD4S22); MD4_STEP (MD4_H , b, c, d, a, w3[2], MD4C02, MD4S23); MD4_STEP (MD4_H , a, b, c, d, w0[1], MD4C02, MD4S20); MD4_STEP (MD4_H , d, a, b, c, w2[1], MD4C02, MD4S21); MD4_STEP (MD4_H , c, d, a, b, w1[1], MD4C02, MD4S22); MD4_STEP (MD4_H , b, c, d, a, w3[1], MD4C02, MD4S23); MD4_STEP (MD4_H , a, b, c, d, w0[3], MD4C02, MD4S20); MD4_STEP (MD4_H , d, a, b, c, w2[3], MD4C02, MD4S21); MD4_STEP (MD4_H , c, d, a, b, w1[3], MD4C02, MD4S22); MD4_STEP (MD4_H , b, c, d, a, w3[3], MD4C02, MD4S23); digest[0] += a; digest[1] += b; digest[2] += c; digest[3] += d; } DECLSPEC void md4_init_vector (md4_ctx_vector_t *ctx) { ctx->h[0] = MD4M_A; ctx->h[1] = MD4M_B; ctx->h[2] = MD4M_C; ctx->h[3] = MD4M_D; ctx->w0[0] = 0; ctx->w0[1] = 0; ctx->w0[2] = 0; ctx->w0[3] = 0; ctx->w1[0] = 0; ctx->w1[1] = 0; ctx->w1[2] = 0; ctx->w1[3] = 0; ctx->w2[0] = 0; ctx->w2[1] = 0; ctx->w2[2] = 0; ctx->w2[3] = 0; ctx->w3[0] = 0; ctx->w3[1] = 0; ctx->w3[2] = 0; ctx->w3[3] = 0; ctx->len = 0; } DECLSPEC void md4_init_vector_from_scalar (md4_ctx_vector_t *ctx, md4_ctx_t *ctx0) { ctx->h[0] = ctx0->h[0]; ctx->h[1] = ctx0->h[1]; ctx->h[2] = ctx0->h[2]; ctx->h[3] = ctx0->h[3]; ctx->w0[0] = ctx0->w0[0]; ctx->w0[1] = ctx0->w0[1]; ctx->w0[2] = ctx0->w0[2]; ctx->w0[3] = ctx0->w0[3]; ctx->w1[0] = ctx0->w1[0]; ctx->w1[1] = ctx0->w1[1]; ctx->w1[2] = ctx0->w1[2]; ctx->w1[3] = ctx0->w1[3]; ctx->w2[0] = ctx0->w2[0]; ctx->w2[1] = ctx0->w2[1]; ctx->w2[2] = ctx0->w2[2]; ctx->w2[3] = ctx0->w2[3]; ctx->w3[0] = ctx0->w3[0]; ctx->w3[1] = ctx0->w3[1]; ctx->w3[2] = ctx0->w3[2]; ctx->w3[3] = ctx0->w3[3]; ctx->len = ctx0->len; } DECLSPEC void md4_update_vector_64 (md4_ctx_vector_t *ctx, u32x *w0, u32x *w1, u32x *w2, u32x *w3, const int len) { MAYBE_VOLATILE const int pos = ctx->len & 63; ctx->len += len; if (pos == 0) { ctx->w0[0] = w0[0]; ctx->w0[1] = w0[1]; ctx->w0[2] = w0[2]; ctx->w0[3] = w0[3]; ctx->w1[0] = w1[0]; ctx->w1[1] = w1[1]; ctx->w1[2] = w1[2]; ctx->w1[3] = w1[3]; ctx->w2[0] = w2[0]; ctx->w2[1] = w2[1]; ctx->w2[2] = w2[2]; ctx->w2[3] = w2[3]; ctx->w3[0] = w3[0]; ctx->w3[1] = w3[1]; ctx->w3[2] = w3[2]; ctx->w3[3] = w3[3]; if (len == 64) { md4_transform_vector (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); ctx->w0[0] = 0; ctx->w0[1] = 0; ctx->w0[2] = 0; ctx->w0[3] = 0; ctx->w1[0] = 0; ctx->w1[1] = 0; ctx->w1[2] = 0; ctx->w1[3] = 0; ctx->w2[0] = 0; ctx->w2[1] = 0; ctx->w2[2] = 0; ctx->w2[3] = 0; ctx->w3[0] = 0; ctx->w3[1] = 0; ctx->w3[2] = 0; ctx->w3[3] = 0; } } else { if ((pos + len) < 64) { switch_buffer_by_offset_le (w0, w1, w2, w3, pos); ctx->w0[0] |= w0[0]; ctx->w0[1] |= w0[1]; ctx->w0[2] |= w0[2]; ctx->w0[3] |= w0[3]; ctx->w1[0] |= w1[0]; ctx->w1[1] |= w1[1]; ctx->w1[2] |= w1[2]; ctx->w1[3] |= w1[3]; ctx->w2[0] |= w2[0]; ctx->w2[1] |= w2[1]; ctx->w2[2] |= w2[2]; ctx->w2[3] |= w2[3]; ctx->w3[0] |= w3[0]; ctx->w3[1] |= w3[1]; ctx->w3[2] |= w3[2]; ctx->w3[3] |= w3[3]; } else { u32x c0[4] = { 0 }; u32x c1[4] = { 0 }; u32x c2[4] = { 0 }; u32x c3[4] = { 0 }; switch_buffer_by_offset_carry_le (w0, w1, w2, w3, c0, c1, c2, c3, pos); ctx->w0[0] |= w0[0]; ctx->w0[1] |= w0[1]; ctx->w0[2] |= w0[2]; ctx->w0[3] |= w0[3]; ctx->w1[0] |= w1[0]; ctx->w1[1] |= w1[1]; ctx->w1[2] |= w1[2]; ctx->w1[3] |= w1[3]; ctx->w2[0] |= w2[0]; ctx->w2[1] |= w2[1]; ctx->w2[2] |= w2[2]; ctx->w2[3] |= w2[3]; ctx->w3[0] |= w3[0]; ctx->w3[1] |= w3[1]; ctx->w3[2] |= w3[2]; ctx->w3[3] |= w3[3]; md4_transform_vector (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); ctx->w0[0] = c0[0]; ctx->w0[1] = c0[1]; ctx->w0[2] = c0[2]; ctx->w0[3] = c0[3]; ctx->w1[0] = c1[0]; ctx->w1[1] = c1[1]; ctx->w1[2] = c1[2]; ctx->w1[3] = c1[3]; ctx->w2[0] = c2[0]; ctx->w2[1] = c2[1]; ctx->w2[2] = c2[2]; ctx->w2[3] = c2[3]; ctx->w3[0] = c3[0]; ctx->w3[1] = c3[1]; ctx->w3[2] = c3[2]; ctx->w3[3] = c3[3]; } } } DECLSPEC void md4_update_vector (md4_ctx_vector_t *ctx, const u32x *w, const int len) { u32x w0[4]; u32x w1[4]; u32x w2[4]; u32x w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 64; pos1 += 64, pos4 += 16) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; md4_update_vector_64 (ctx, w0, w1, w2, w3, 64); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; md4_update_vector_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void md4_update_vector_swap (md4_ctx_vector_t *ctx, const u32x *w, const int len) { u32x w0[4]; u32x w1[4]; u32x w2[4]; u32x w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 64; pos1 += 64, pos4 += 16) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; w0[0] = hc_swap32 (w0[0]); w0[1] = hc_swap32 (w0[1]); w0[2] = hc_swap32 (w0[2]); w0[3] = hc_swap32 (w0[3]); w1[0] = hc_swap32 (w1[0]); w1[1] = hc_swap32 (w1[1]); w1[2] = hc_swap32 (w1[2]); w1[3] = hc_swap32 (w1[3]); w2[0] = hc_swap32 (w2[0]); w2[1] = hc_swap32 (w2[1]); w2[2] = hc_swap32 (w2[2]); w2[3] = hc_swap32 (w2[3]); w3[0] = hc_swap32 (w3[0]); w3[1] = hc_swap32 (w3[1]); w3[2] = hc_swap32 (w3[2]); w3[3] = hc_swap32 (w3[3]); md4_update_vector_64 (ctx, w0, w1, w2, w3, 64); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; w2[0] = w[pos4 + 8]; w2[1] = w[pos4 + 9]; w2[2] = w[pos4 + 10]; w2[3] = w[pos4 + 11]; w3[0] = w[pos4 + 12]; w3[1] = w[pos4 + 13]; w3[2] = w[pos4 + 14]; w3[3] = w[pos4 + 15]; w0[0] = hc_swap32 (w0[0]); w0[1] = hc_swap32 (w0[1]); w0[2] = hc_swap32 (w0[2]); w0[3] = hc_swap32 (w0[3]); w1[0] = hc_swap32 (w1[0]); w1[1] = hc_swap32 (w1[1]); w1[2] = hc_swap32 (w1[2]); w1[3] = hc_swap32 (w1[3]); w2[0] = hc_swap32 (w2[0]); w2[1] = hc_swap32 (w2[1]); w2[2] = hc_swap32 (w2[2]); w2[3] = hc_swap32 (w2[3]); w3[0] = hc_swap32 (w3[0]); w3[1] = hc_swap32 (w3[1]); w3[2] = hc_swap32 (w3[2]); w3[3] = hc_swap32 (w3[3]); md4_update_vector_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void md4_update_vector_utf16le (md4_ctx_vector_t *ctx, const u32x *w, const int len) { u32x w0[4]; u32x w1[4]; u32x w2[4]; u32x w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 32; pos1 += 32, pos4 += 8) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le (w1, w2, w3); make_utf16le (w0, w0, w1); md4_update_vector_64 (ctx, w0, w1, w2, w3, 32 * 2); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le (w1, w2, w3); make_utf16le (w0, w0, w1); md4_update_vector_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void md4_update_vector_utf16le_swap (md4_ctx_vector_t *ctx, const u32x *w, const int len) { u32x w0[4]; u32x w1[4]; u32x w2[4]; u32x w3[4]; int pos1; int pos4; for (pos1 = 0, pos4 = 0; pos1 < len - 32; pos1 += 32, pos4 += 8) { w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le (w1, w2, w3); make_utf16le (w0, w0, w1); w0[0] = hc_swap32 (w0[0]); w0[1] = hc_swap32 (w0[1]); w0[2] = hc_swap32 (w0[2]); w0[3] = hc_swap32 (w0[3]); w1[0] = hc_swap32 (w1[0]); w1[1] = hc_swap32 (w1[1]); w1[2] = hc_swap32 (w1[2]); w1[3] = hc_swap32 (w1[3]); w2[0] = hc_swap32 (w2[0]); w2[1] = hc_swap32 (w2[1]); w2[2] = hc_swap32 (w2[2]); w2[3] = hc_swap32 (w2[3]); w3[0] = hc_swap32 (w3[0]); w3[1] = hc_swap32 (w3[1]); w3[2] = hc_swap32 (w3[2]); w3[3] = hc_swap32 (w3[3]); md4_update_vector_64 (ctx, w0, w1, w2, w3, 32 * 2); } w0[0] = w[pos4 + 0]; w0[1] = w[pos4 + 1]; w0[2] = w[pos4 + 2]; w0[3] = w[pos4 + 3]; w1[0] = w[pos4 + 4]; w1[1] = w[pos4 + 5]; w1[2] = w[pos4 + 6]; w1[3] = w[pos4 + 7]; make_utf16le (w1, w2, w3); make_utf16le (w0, w0, w1); w0[0] = hc_swap32 (w0[0]); w0[1] = hc_swap32 (w0[1]); w0[2] = hc_swap32 (w0[2]); w0[3] = hc_swap32 (w0[3]); w1[0] = hc_swap32 (w1[0]); w1[1] = hc_swap32 (w1[1]); w1[2] = hc_swap32 (w1[2]); w1[3] = hc_swap32 (w1[3]); w2[0] = hc_swap32 (w2[0]); w2[1] = hc_swap32 (w2[1]); w2[2] = hc_swap32 (w2[2]); w2[3] = hc_swap32 (w2[3]); w3[0] = hc_swap32 (w3[0]); w3[1] = hc_swap32 (w3[1]); w3[2] = hc_swap32 (w3[2]); w3[3] = hc_swap32 (w3[3]); md4_update_vector_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void md4_final_vector (md4_ctx_vector_t *ctx) { MAYBE_VOLATILE const int pos = ctx->len & 63; append_0x80_4x4 (ctx->w0, ctx->w1, ctx->w2, ctx->w3, pos); if (pos >= 56) { md4_transform_vector (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); ctx->w0[0] = 0; ctx->w0[1] = 0; ctx->w0[2] = 0; ctx->w0[3] = 0; ctx->w1[0] = 0; ctx->w1[1] = 0; ctx->w1[2] = 0; ctx->w1[3] = 0; ctx->w2[0] = 0; ctx->w2[1] = 0; ctx->w2[2] = 0; ctx->w2[3] = 0; ctx->w3[0] = 0; ctx->w3[1] = 0; ctx->w3[2] = 0; ctx->w3[3] = 0; } ctx->w3[2] = ctx->len * 8; ctx->w3[3] = 0; md4_transform_vector (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); } // HMAC + Vector DECLSPEC void md4_hmac_init_vector_64 (md4_hmac_ctx_vector_t *ctx, const u32x *w0, const u32x *w1, const u32x *w2, const u32x *w3) { u32x t0[4]; u32x t1[4]; u32x t2[4]; u32x t3[4]; // ipad t0[0] = w0[0] ^ 0x36363636; t0[1] = w0[1] ^ 0x36363636; t0[2] = w0[2] ^ 0x36363636; t0[3] = w0[3] ^ 0x36363636; t1[0] = w1[0] ^ 0x36363636; t1[1] = w1[1] ^ 0x36363636; t1[2] = w1[2] ^ 0x36363636; t1[3] = w1[3] ^ 0x36363636; t2[0] = w2[0] ^ 0x36363636; t2[1] = w2[1] ^ 0x36363636; t2[2] = w2[2] ^ 0x36363636; t2[3] = w2[3] ^ 0x36363636; t3[0] = w3[0] ^ 0x36363636; t3[1] = w3[1] ^ 0x36363636; t3[2] = w3[2] ^ 0x36363636; t3[3] = w3[3] ^ 0x36363636; md4_init_vector (&ctx->ipad); md4_update_vector_64 (&ctx->ipad, t0, t1, t2, t3, 64); // opad t0[0] = w0[0] ^ 0x5c5c5c5c; t0[1] = w0[1] ^ 0x5c5c5c5c; t0[2] = w0[2] ^ 0x5c5c5c5c; t0[3] = w0[3] ^ 0x5c5c5c5c; t1[0] = w1[0] ^ 0x5c5c5c5c; t1[1] = w1[1] ^ 0x5c5c5c5c; t1[2] = w1[2] ^ 0x5c5c5c5c; t1[3] = w1[3] ^ 0x5c5c5c5c; t2[0] = w2[0] ^ 0x5c5c5c5c; t2[1] = w2[1] ^ 0x5c5c5c5c; t2[2] = w2[2] ^ 0x5c5c5c5c; t2[3] = w2[3] ^ 0x5c5c5c5c; t3[0] = w3[0] ^ 0x5c5c5c5c; t3[1] = w3[1] ^ 0x5c5c5c5c; t3[2] = w3[2] ^ 0x5c5c5c5c; t3[3] = w3[3] ^ 0x5c5c5c5c; md4_init_vector (&ctx->opad); md4_update_vector_64 (&ctx->opad, t0, t1, t2, t3, 64); } DECLSPEC void md4_hmac_init_vector (md4_hmac_ctx_vector_t *ctx, const u32x *w, const int len) { u32x w0[4]; u32x w1[4]; u32x w2[4]; u32x w3[4]; if (len > 64) { md4_ctx_vector_t tmp; md4_init_vector (&tmp); md4_update_vector (&tmp, w, len); md4_final_vector (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = 0; w1[1] = 0; w1[2] = 0; w1[3] = 0; w2[0] = 0; w2[1] = 0; w2[2] = 0; w2[3] = 0; w3[0] = 0; w3[1] = 0; w3[2] = 0; w3[3] = 0; } else { w0[0] = w[ 0]; w0[1] = w[ 1]; w0[2] = w[ 2]; w0[3] = w[ 3]; w1[0] = w[ 4]; w1[1] = w[ 5]; w1[2] = w[ 6]; w1[3] = w[ 7]; w2[0] = w[ 8]; w2[1] = w[ 9]; w2[2] = w[10]; w2[3] = w[11]; w3[0] = w[12]; w3[1] = w[13]; w3[2] = w[14]; w3[3] = w[15]; } md4_hmac_init_vector_64 (ctx, w0, w1, w2, w3); } DECLSPEC void md4_hmac_update_vector_64 (md4_hmac_ctx_vector_t *ctx, u32x *w0, u32x *w1, u32x *w2, u32x *w3, const int len) { md4_update_vector_64 (&ctx->ipad, w0, w1, w2, w3, len); } DECLSPEC void md4_hmac_update_vector (md4_hmac_ctx_vector_t *ctx, const u32x *w, const int len) { md4_update_vector (&ctx->ipad, w, len); } DECLSPEC void md4_hmac_final_vector (md4_hmac_ctx_vector_t *ctx) { md4_final_vector (&ctx->ipad); ctx->opad.w0[0] = ctx->ipad.h[0]; ctx->opad.w0[1] = ctx->ipad.h[1]; ctx->opad.w0[2] = ctx->ipad.h[2]; ctx->opad.w0[3] = ctx->ipad.h[3]; ctx->opad.w1[0] = 0; ctx->opad.w1[1] = 0; ctx->opad.w1[2] = 0; ctx->opad.w1[3] = 0; ctx->opad.w2[0] = 0; ctx->opad.w2[1] = 0; ctx->opad.w2[2] = 0; ctx->opad.w2[3] = 0; ctx->opad.w3[0] = 0; ctx->opad.w3[1] = 0; ctx->opad.w3[2] = 0; ctx->opad.w3[3] = 0; ctx->opad.len += 16; md4_final_vector (&ctx->opad); }