// important notes on this: // input buf unused bytes needs to be set to zero // input buf needs to be in algorithm native byte order (md5 = LE, sha1 = BE, etc) // input buf needs to be 64 byte aligned when using md5_update() typedef struct sha1_ctx { u32 h[5]; u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; int len; } sha1_ctx_t; DECLSPEC void sha1_transform (const u32 w0[4], const u32 w1[4], const u32 w2[4], const u32 w3[4], u32 digest[5]) { u32 a = digest[0]; u32 b = digest[1]; u32 c = digest[2]; u32 d = digest[3]; u32 e = digest[4]; u32 w0_t = w0[0]; u32 w1_t = w0[1]; u32 w2_t = w0[2]; u32 w3_t = w0[3]; u32 w4_t = w1[0]; u32 w5_t = w1[1]; u32 w6_t = w1[2]; u32 w7_t = w1[3]; u32 w8_t = w2[0]; u32 w9_t = w2[1]; u32 wa_t = w2[2]; u32 wb_t = w2[3]; u32 wc_t = w3[0]; u32 wd_t = w3[1]; u32 we_t = w3[2]; u32 wf_t = w3[3]; #define K SHA1C00 SHA1_STEP_S (SHA1_F0o, a, b, c, d, e, w0_t); SHA1_STEP_S (SHA1_F0o, e, a, b, c, d, w1_t); SHA1_STEP_S (SHA1_F0o, d, e, a, b, c, w2_t); SHA1_STEP_S (SHA1_F0o, c, d, e, a, b, w3_t); SHA1_STEP_S (SHA1_F0o, b, c, d, e, a, w4_t); SHA1_STEP_S (SHA1_F0o, a, b, c, d, e, w5_t); SHA1_STEP_S (SHA1_F0o, e, a, b, c, d, w6_t); SHA1_STEP_S (SHA1_F0o, d, e, a, b, c, w7_t); SHA1_STEP_S (SHA1_F0o, c, d, e, a, b, w8_t); SHA1_STEP_S (SHA1_F0o, b, c, d, e, a, w9_t); SHA1_STEP_S (SHA1_F0o, a, b, c, d, e, wa_t); SHA1_STEP_S (SHA1_F0o, e, a, b, c, d, wb_t); SHA1_STEP_S (SHA1_F0o, d, e, a, b, c, wc_t); SHA1_STEP_S (SHA1_F0o, c, d, e, a, b, wd_t); SHA1_STEP_S (SHA1_F0o, b, c, d, e, a, we_t); SHA1_STEP_S (SHA1_F0o, a, b, c, d, e, wf_t); w0_t = rotl32_S ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP_S (SHA1_F0o, e, a, b, c, d, w0_t); w1_t = rotl32_S ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP_S (SHA1_F0o, d, e, a, b, c, w1_t); w2_t = rotl32_S ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP_S (SHA1_F0o, c, d, e, a, b, w2_t); w3_t = rotl32_S ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP_S (SHA1_F0o, b, c, d, e, a, w3_t); #undef K #define K SHA1C01 w4_t = rotl32_S ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, w4_t); w5_t = rotl32_S ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, w5_t); w6_t = rotl32_S ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, w6_t); w7_t = rotl32_S ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, w7_t); w8_t = rotl32_S ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, w8_t); w9_t = rotl32_S ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, w9_t); wa_t = rotl32_S ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, wa_t); wb_t = rotl32_S ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, wb_t); wc_t = rotl32_S ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, wc_t); wd_t = rotl32_S ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, wd_t); we_t = rotl32_S ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, we_t); wf_t = rotl32_S ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, wf_t); w0_t = rotl32_S ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, w0_t); w1_t = rotl32_S ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, w1_t); w2_t = rotl32_S ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, w2_t); w3_t = rotl32_S ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, w3_t); w4_t = rotl32_S ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, w4_t); w5_t = rotl32_S ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, w5_t); w6_t = rotl32_S ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, w6_t); w7_t = rotl32_S ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, w7_t); #undef K #define K SHA1C02 w8_t = rotl32_S ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP_S (SHA1_F2o, a, b, c, d, e, w8_t); w9_t = rotl32_S ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP_S (SHA1_F2o, e, a, b, c, d, w9_t); wa_t = rotl32_S ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP_S (SHA1_F2o, d, e, a, b, c, wa_t); wb_t = rotl32_S ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP_S (SHA1_F2o, c, d, e, a, b, wb_t); wc_t = rotl32_S ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP_S (SHA1_F2o, b, c, d, e, a, wc_t); wd_t = rotl32_S ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP_S (SHA1_F2o, a, b, c, d, e, wd_t); we_t = rotl32_S ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP_S (SHA1_F2o, e, a, b, c, d, we_t); wf_t = rotl32_S ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP_S (SHA1_F2o, d, e, a, b, c, wf_t); w0_t = rotl32_S ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP_S (SHA1_F2o, c, d, e, a, b, w0_t); w1_t = rotl32_S ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP_S (SHA1_F2o, b, c, d, e, a, w1_t); w2_t = rotl32_S ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP_S (SHA1_F2o, a, b, c, d, e, w2_t); w3_t = rotl32_S ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP_S (SHA1_F2o, e, a, b, c, d, w3_t); w4_t = rotl32_S ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP_S (SHA1_F2o, d, e, a, b, c, w4_t); w5_t = rotl32_S ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP_S (SHA1_F2o, c, d, e, a, b, w5_t); w6_t = rotl32_S ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP_S (SHA1_F2o, b, c, d, e, a, w6_t); w7_t = rotl32_S ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP_S (SHA1_F2o, a, b, c, d, e, w7_t); w8_t = rotl32_S ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP_S (SHA1_F2o, e, a, b, c, d, w8_t); w9_t = rotl32_S ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP_S (SHA1_F2o, d, e, a, b, c, w9_t); wa_t = rotl32_S ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP_S (SHA1_F2o, c, d, e, a, b, wa_t); wb_t = rotl32_S ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP_S (SHA1_F2o, b, c, d, e, a, wb_t); #undef K #define K SHA1C03 wc_t = rotl32_S ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, wc_t); wd_t = rotl32_S ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, wd_t); we_t = rotl32_S ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, we_t); wf_t = rotl32_S ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, wf_t); w0_t = rotl32_S ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, w0_t); w1_t = rotl32_S ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, w1_t); w2_t = rotl32_S ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, w2_t); w3_t = rotl32_S ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, w3_t); w4_t = rotl32_S ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, w4_t); w5_t = rotl32_S ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, w5_t); w6_t = rotl32_S ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, w6_t); w7_t = rotl32_S ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, w7_t); w8_t = rotl32_S ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, w8_t); w9_t = rotl32_S ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, w9_t); wa_t = rotl32_S ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, wa_t); wb_t = rotl32_S ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP_S (SHA1_F1, a, b, c, d, e, wb_t); wc_t = rotl32_S ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP_S (SHA1_F1, e, a, b, c, d, wc_t); wd_t = rotl32_S ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP_S (SHA1_F1, d, e, a, b, c, wd_t); we_t = rotl32_S ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP_S (SHA1_F1, c, d, e, a, b, we_t); wf_t = rotl32_S ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP_S (SHA1_F1, b, c, d, e, a, wf_t); #undef K digest[0] += a; digest[1] += b; digest[2] += c; digest[3] += d; digest[4] += e; } DECLSPEC void sha1_init (sha1_ctx_t *ctx) { ctx->h[0] = SHA1M_A; ctx->h[1] = SHA1M_B; ctx->h[2] = SHA1M_C; ctx->h[3] = SHA1M_D; ctx->h[4] = SHA1M_E; 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 sha1_update_64 (sha1_ctx_t *ctx, u32 w0[4], u32 w1[4], u32 w2[4], u32 w3[4], const int len) { const int pos = ctx->len & 63; ctx->len += len; if ((pos + len) < 64) { switch_buffer_by_offset_be_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_be_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]; sha1_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 sha1_update (sha1_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]; sha1_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]; sha1_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void sha1_update_swap (sha1_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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void sha1_update_utf16le (sha1_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 - 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); sha1_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); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_utf16le_swap (sha1_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 - 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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_utf16be (sha1_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 - 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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); sha1_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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_utf16be_swap (sha1_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 - 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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); w0[0] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); w0[0] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_global (sha1_ctx_t *ctx, const __global 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]; sha1_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]; sha1_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void sha1_update_global_swap (sha1_ctx_t *ctx, const __global 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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_update_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void sha1_update_global_utf16le (sha1_ctx_t *ctx, const __global 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 - 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); sha1_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); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_global_utf16le_swap (sha1_ctx_t *ctx, const __global 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 - 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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_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] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_global_utf16be (sha1_ctx_t *ctx, const __global 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 - 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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); sha1_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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_global_utf16be_swap (sha1_ctx_t *ctx, const __global 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 - 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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); w0[0] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_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_utf16be_S (w1, w2, w3); make_utf16be_S (w0, w0, w1); w0[0] = swap32_S (w0[0]); w0[1] = swap32_S (w0[1]); w0[2] = swap32_S (w0[2]); w0[3] = swap32_S (w0[3]); w1[0] = swap32_S (w1[0]); w1[1] = swap32_S (w1[1]); w1[2] = swap32_S (w1[2]); w1[3] = swap32_S (w1[3]); w2[0] = swap32_S (w2[0]); w2[1] = swap32_S (w2[1]); w2[2] = swap32_S (w2[2]); w2[3] = swap32_S (w2[3]); w3[0] = swap32_S (w3[0]); w3[1] = swap32_S (w3[1]); w3[2] = swap32_S (w3[2]); w3[3] = swap32_S (w3[3]); sha1_update_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_final (sha1_ctx_t *ctx) { const int pos = ctx->len & 63; append_0x80_4x4_S (ctx->w0, ctx->w1, ctx->w2, ctx->w3, pos ^ 3); if (pos >= 56) { sha1_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] = 0; ctx->w3[3] = ctx->len * 8; sha1_transform (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); } // sha1_hmac typedef struct sha1_hmac_ctx { sha1_ctx_t ipad; sha1_ctx_t opad; } sha1_hmac_ctx_t; DECLSPEC void sha1_hmac_init_64 (sha1_hmac_ctx_t *ctx, const u32 w0[4], const u32 w1[4], const u32 w2[4], const u32 w3[4]) { 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; sha1_init (&ctx->ipad); sha1_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; sha1_init (&ctx->opad); sha1_update_64 (&ctx->opad, t0, t1, t2, t3, 64); } DECLSPEC void sha1_hmac_init (sha1_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) { sha1_ctx_t tmp; sha1_init (&tmp); sha1_update (&tmp, w, len); sha1_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = tmp.h[4]; 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]; } sha1_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void sha1_hmac_init_swap (sha1_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) { sha1_ctx_t tmp; sha1_init (&tmp); sha1_update_swap (&tmp, w, len); sha1_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = tmp.h[4]; 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] = swap32_S (w[ 0]); w0[1] = swap32_S (w[ 1]); w0[2] = swap32_S (w[ 2]); w0[3] = swap32_S (w[ 3]); w1[0] = swap32_S (w[ 4]); w1[1] = swap32_S (w[ 5]); w1[2] = swap32_S (w[ 6]); w1[3] = swap32_S (w[ 7]); w2[0] = swap32_S (w[ 8]); w2[1] = swap32_S (w[ 9]); w2[2] = swap32_S (w[10]); w2[3] = swap32_S (w[11]); w3[0] = swap32_S (w[12]); w3[1] = swap32_S (w[13]); w3[2] = swap32_S (w[14]); w3[3] = swap32_S (w[15]); } sha1_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void sha1_hmac_init_global (sha1_hmac_ctx_t *ctx, __global const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; if (len > 64) { sha1_ctx_t tmp; sha1_init (&tmp); sha1_update_global (&tmp, w, len); sha1_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = tmp.h[4]; 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]; } sha1_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void sha1_hmac_init_global_swap (sha1_hmac_ctx_t *ctx, __global const u32 *w, const int len) { u32 w0[4]; u32 w1[4]; u32 w2[4]; u32 w3[4]; if (len > 64) { sha1_ctx_t tmp; sha1_init (&tmp); sha1_update_global_swap (&tmp, w, len); sha1_final (&tmp); w0[0] = tmp.h[0]; w0[1] = tmp.h[1]; w0[2] = tmp.h[2]; w0[3] = tmp.h[3]; w1[0] = tmp.h[4]; 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] = swap32_S (w[ 0]); w0[1] = swap32_S (w[ 1]); w0[2] = swap32_S (w[ 2]); w0[3] = swap32_S (w[ 3]); w1[0] = swap32_S (w[ 4]); w1[1] = swap32_S (w[ 5]); w1[2] = swap32_S (w[ 6]); w1[3] = swap32_S (w[ 7]); w2[0] = swap32_S (w[ 8]); w2[1] = swap32_S (w[ 9]); w2[2] = swap32_S (w[10]); w2[3] = swap32_S (w[11]); w3[0] = swap32_S (w[12]); w3[1] = swap32_S (w[13]); w3[2] = swap32_S (w[14]); w3[3] = swap32_S (w[15]); } sha1_hmac_init_64 (ctx, w0, w1, w2, w3); } DECLSPEC void sha1_hmac_update_64 (sha1_hmac_ctx_t *ctx, u32 w0[4], u32 w1[4], u32 w2[4], u32 w3[4], const int len) { sha1_update_64 (&ctx->ipad, w0, w1, w2, w3, len); } DECLSPEC void sha1_hmac_update (sha1_hmac_ctx_t *ctx, const u32 *w, const int len) { sha1_update (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_update_swap (sha1_hmac_ctx_t *ctx, const u32 *w, const int len) { sha1_update_swap (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_update_utf16le (sha1_hmac_ctx_t *ctx, const u32 *w, const int len) { sha1_update_utf16le (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_update_utf16le_swap (sha1_hmac_ctx_t *ctx, const u32 *w, const int len) { sha1_update_utf16le_swap (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_update_global (sha1_hmac_ctx_t *ctx, const __global u32 *w, const int len) { sha1_update_global (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_update_global_swap (sha1_hmac_ctx_t *ctx, const __global u32 *w, const int len) { sha1_update_global_swap (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_update_global_utf16le (sha1_hmac_ctx_t *ctx, const __global u32 *w, const int len) { sha1_update_global_utf16le (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_update_global_utf16le_swap (sha1_hmac_ctx_t *ctx, const __global u32 *w, const int len) { sha1_update_global_utf16le_swap (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_final (sha1_hmac_ctx_t *ctx) { sha1_final (&ctx->ipad); u32 t0[4]; u32 t1[4]; u32 t2[4]; u32 t3[4]; t0[0] = ctx->ipad.h[0]; t0[1] = ctx->ipad.h[1]; t0[2] = ctx->ipad.h[2]; t0[3] = ctx->ipad.h[3]; t1[0] = ctx->ipad.h[4]; t1[1] = 0; t1[2] = 0; t1[3] = 0; t2[0] = 0; t2[1] = 0; t2[2] = 0; t2[3] = 0; t3[0] = 0; t3[1] = 0; t3[2] = 0; t3[3] = 0; sha1_update_64 (&ctx->opad, t0, t1, t2, t3, 20); sha1_final (&ctx->opad); } // while input buf can be a vector datatype, the length of the different elements can not typedef struct sha1_ctx_vector { u32x h[5]; u32x w0[4]; u32x w1[4]; u32x w2[4]; u32x w3[4]; int len; } sha1_ctx_vector_t; DECLSPEC void sha1_transform_vector (const u32x w0[4], const u32x w1[4], const u32x w2[4], const u32x w3[4], u32x digest[5]) { u32x a = digest[0]; u32x b = digest[1]; u32x c = digest[2]; u32x d = digest[3]; u32x e = digest[4]; u32x w0_t = w0[0]; u32x w1_t = w0[1]; u32x w2_t = w0[2]; u32x w3_t = w0[3]; u32x w4_t = w1[0]; u32x w5_t = w1[1]; u32x w6_t = w1[2]; u32x w7_t = w1[3]; u32x w8_t = w2[0]; u32x w9_t = w2[1]; u32x wa_t = w2[2]; u32x wb_t = w2[3]; u32x wc_t = w3[0]; u32x wd_t = w3[1]; u32x we_t = w3[2]; u32x wf_t = w3[3]; #define K SHA1C00 SHA1_STEP (SHA1_F0o, a, b, c, d, e, w0_t); SHA1_STEP (SHA1_F0o, e, a, b, c, d, w1_t); SHA1_STEP (SHA1_F0o, d, e, a, b, c, w2_t); SHA1_STEP (SHA1_F0o, c, d, e, a, b, w3_t); SHA1_STEP (SHA1_F0o, b, c, d, e, a, w4_t); SHA1_STEP (SHA1_F0o, a, b, c, d, e, w5_t); SHA1_STEP (SHA1_F0o, e, a, b, c, d, w6_t); SHA1_STEP (SHA1_F0o, d, e, a, b, c, w7_t); SHA1_STEP (SHA1_F0o, c, d, e, a, b, w8_t); SHA1_STEP (SHA1_F0o, b, c, d, e, a, w9_t); SHA1_STEP (SHA1_F0o, a, b, c, d, e, wa_t); SHA1_STEP (SHA1_F0o, e, a, b, c, d, wb_t); SHA1_STEP (SHA1_F0o, d, e, a, b, c, wc_t); SHA1_STEP (SHA1_F0o, c, d, e, a, b, wd_t); SHA1_STEP (SHA1_F0o, b, c, d, e, a, we_t); SHA1_STEP (SHA1_F0o, a, b, c, d, e, wf_t); w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F0o, e, a, b, c, d, w0_t); w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F0o, d, e, a, b, c, w1_t); w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F0o, c, d, e, a, b, w2_t); w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F0o, b, c, d, e, a, w3_t); #undef K #define K SHA1C01 w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w4_t); w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w5_t); w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w6_t); w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w7_t); w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w8_t); w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w9_t); wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wa_t); wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, wb_t); wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, wc_t); wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, wd_t); we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, we_t); wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wf_t); w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w0_t); w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w1_t); w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w2_t); w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w3_t); w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w4_t); w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w5_t); w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w6_t); w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w7_t); #undef K #define K SHA1C02 w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, w8_t); w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, w9_t); wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, wa_t); wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, wb_t); wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, wc_t); wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, wd_t); we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, we_t); wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, wf_t); w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, w0_t); w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, w1_t); w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, w2_t); w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, w3_t); w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, w4_t); w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, w5_t); w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, w6_t); w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F2o, a, b, c, d, e, w7_t); w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F2o, e, a, b, c, d, w8_t); w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F2o, d, e, a, b, c, w9_t); wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F2o, c, d, e, a, b, wa_t); wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F2o, b, c, d, e, a, wb_t); #undef K #define K SHA1C03 wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, wc_t); wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wd_t); we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, we_t); wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, wf_t); w0_t = rotl32 ((wd_t ^ w8_t ^ w2_t ^ w0_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w0_t); w1_t = rotl32 ((we_t ^ w9_t ^ w3_t ^ w1_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w1_t); w2_t = rotl32 ((wf_t ^ wa_t ^ w4_t ^ w2_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w2_t); w3_t = rotl32 ((w0_t ^ wb_t ^ w5_t ^ w3_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w3_t); w4_t = rotl32 ((w1_t ^ wc_t ^ w6_t ^ w4_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w4_t); w5_t = rotl32 ((w2_t ^ wd_t ^ w7_t ^ w5_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, w5_t); w6_t = rotl32 ((w3_t ^ we_t ^ w8_t ^ w6_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, w6_t); w7_t = rotl32 ((w4_t ^ wf_t ^ w9_t ^ w7_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, w7_t); w8_t = rotl32 ((w5_t ^ w0_t ^ wa_t ^ w8_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, w8_t); w9_t = rotl32 ((w6_t ^ w1_t ^ wb_t ^ w9_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, w9_t); wa_t = rotl32 ((w7_t ^ w2_t ^ wc_t ^ wa_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, wa_t); wb_t = rotl32 ((w8_t ^ w3_t ^ wd_t ^ wb_t), 1u); SHA1_STEP (SHA1_F1, a, b, c, d, e, wb_t); wc_t = rotl32 ((w9_t ^ w4_t ^ we_t ^ wc_t), 1u); SHA1_STEP (SHA1_F1, e, a, b, c, d, wc_t); wd_t = rotl32 ((wa_t ^ w5_t ^ wf_t ^ wd_t), 1u); SHA1_STEP (SHA1_F1, d, e, a, b, c, wd_t); we_t = rotl32 ((wb_t ^ w6_t ^ w0_t ^ we_t), 1u); SHA1_STEP (SHA1_F1, c, d, e, a, b, we_t); wf_t = rotl32 ((wc_t ^ w7_t ^ w1_t ^ wf_t), 1u); SHA1_STEP (SHA1_F1, b, c, d, e, a, wf_t); #undef K digest[0] += a; digest[1] += b; digest[2] += c; digest[3] += d; digest[4] += e; } DECLSPEC void sha1_init_vector (sha1_ctx_vector_t *ctx) { ctx->h[0] = SHA1M_A; ctx->h[1] = SHA1M_B; ctx->h[2] = SHA1M_C; ctx->h[3] = SHA1M_D; ctx->h[4] = SHA1M_E; 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 sha1_init_vector_from_scalar (sha1_ctx_vector_t *ctx, sha1_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->h[4] = ctx0->h[4]; 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 sha1_update_vector_64 (sha1_ctx_vector_t *ctx, u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], const int len) { const int pos = ctx->len & 63; ctx->len += len; if ((pos + len) < 64) { switch_buffer_by_offset_be (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_be (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]; sha1_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 sha1_update_vector (sha1_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]; sha1_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]; sha1_update_vector_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void sha1_update_vector_swap (sha1_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] = swap32 (w0[0]); w0[1] = swap32 (w0[1]); w0[2] = swap32 (w0[2]); w0[3] = swap32 (w0[3]); w1[0] = swap32 (w1[0]); w1[1] = swap32 (w1[1]); w1[2] = swap32 (w1[2]); w1[3] = swap32 (w1[3]); w2[0] = swap32 (w2[0]); w2[1] = swap32 (w2[1]); w2[2] = swap32 (w2[2]); w2[3] = swap32 (w2[3]); w3[0] = swap32 (w3[0]); w3[1] = swap32 (w3[1]); w3[2] = swap32 (w3[2]); w3[3] = swap32 (w3[3]); sha1_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] = swap32 (w0[0]); w0[1] = swap32 (w0[1]); w0[2] = swap32 (w0[2]); w0[3] = swap32 (w0[3]); w1[0] = swap32 (w1[0]); w1[1] = swap32 (w1[1]); w1[2] = swap32 (w1[2]); w1[3] = swap32 (w1[3]); w2[0] = swap32 (w2[0]); w2[1] = swap32 (w2[1]); w2[2] = swap32 (w2[2]); w2[3] = swap32 (w2[3]); w3[0] = swap32 (w3[0]); w3[1] = swap32 (w3[1]); w3[2] = swap32 (w3[2]); w3[3] = swap32 (w3[3]); sha1_update_vector_64 (ctx, w0, w1, w2, w3, len - pos1); } DECLSPEC void sha1_update_vector_utf16le (sha1_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); sha1_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); sha1_update_vector_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_vector_utf16le_swap (sha1_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] = swap32 (w0[0]); w0[1] = swap32 (w0[1]); w0[2] = swap32 (w0[2]); w0[3] = swap32 (w0[3]); w1[0] = swap32 (w1[0]); w1[1] = swap32 (w1[1]); w1[2] = swap32 (w1[2]); w1[3] = swap32 (w1[3]); w2[0] = swap32 (w2[0]); w2[1] = swap32 (w2[1]); w2[2] = swap32 (w2[2]); w2[3] = swap32 (w2[3]); w3[0] = swap32 (w3[0]); w3[1] = swap32 (w3[1]); w3[2] = swap32 (w3[2]); w3[3] = swap32 (w3[3]); sha1_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] = swap32 (w0[0]); w0[1] = swap32 (w0[1]); w0[2] = swap32 (w0[2]); w0[3] = swap32 (w0[3]); w1[0] = swap32 (w1[0]); w1[1] = swap32 (w1[1]); w1[2] = swap32 (w1[2]); w1[3] = swap32 (w1[3]); w2[0] = swap32 (w2[0]); w2[1] = swap32 (w2[1]); w2[2] = swap32 (w2[2]); w2[3] = swap32 (w2[3]); w3[0] = swap32 (w3[0]); w3[1] = swap32 (w3[1]); w3[2] = swap32 (w3[2]); w3[3] = swap32 (w3[3]); sha1_update_vector_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_vector_utf16leN (sha1_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_utf16leN (w1, w2, w3); make_utf16leN (w0, w0, w1); sha1_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_utf16leN (w1, w2, w3); make_utf16leN (w0, w0, w1); sha1_update_vector_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_update_vector_utf16beN (sha1_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_utf16beN (w1, w2, w3); make_utf16beN (w0, w0, w1); sha1_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_utf16beN (w1, w2, w3); make_utf16beN (w0, w0, w1); sha1_update_vector_64 (ctx, w0, w1, w2, w3, (len - pos1) * 2); } DECLSPEC void sha1_final_vector (sha1_ctx_vector_t *ctx) { const int pos = ctx->len & 63; append_0x80_4x4 (ctx->w0, ctx->w1, ctx->w2, ctx->w3, pos ^ 3); if (pos >= 56) { sha1_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] = 0; ctx->w3[3] = ctx->len * 8; sha1_transform_vector (ctx->w0, ctx->w1, ctx->w2, ctx->w3, ctx->h); } // HMAC + Vector typedef struct sha1_hmac_ctx_vector { sha1_ctx_vector_t ipad; sha1_ctx_vector_t opad; } sha1_hmac_ctx_vector_t; DECLSPEC void sha1_hmac_init_vector_64 (sha1_hmac_ctx_vector_t *ctx, const u32x w0[4], const u32x w1[4], const u32x w2[4], const u32x w3[4]) { 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; sha1_init_vector (&ctx->ipad); sha1_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; sha1_init_vector (&ctx->opad); sha1_update_vector_64 (&ctx->opad, t0, t1, t2, t3, 64); } DECLSPEC void sha1_hmac_init_vector (sha1_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) { sha1_ctx_vector_t tmp; sha1_init_vector (&tmp); sha1_update_vector (&tmp, w, len); sha1_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] = tmp.h[4]; 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]; } sha1_hmac_init_vector_64 (ctx, w0, w1, w2, w3); } DECLSPEC void sha1_hmac_update_vector_64 (sha1_hmac_ctx_vector_t *ctx, u32x w0[4], u32x w1[4], u32x w2[4], u32x w3[4], const int len) { sha1_update_vector_64 (&ctx->ipad, w0, w1, w2, w3, len); } DECLSPEC void sha1_hmac_update_vector (sha1_hmac_ctx_vector_t *ctx, const u32x *w, const int len) { sha1_update_vector (&ctx->ipad, w, len); } DECLSPEC void sha1_hmac_final_vector (sha1_hmac_ctx_vector_t *ctx) { sha1_final_vector (&ctx->ipad); u32x t0[4]; u32x t1[4]; u32x t2[4]; u32x t3[4]; t0[0] = ctx->ipad.h[0]; t0[1] = ctx->ipad.h[1]; t0[2] = ctx->ipad.h[2]; t0[3] = ctx->ipad.h[3]; t1[0] = ctx->ipad.h[4]; t1[1] = 0; t1[2] = 0; t1[3] = 0; t2[0] = 0; t2[1] = 0; t2[2] = 0; t2[3] = 0; t3[0] = 0; t3[1] = 0; t3[2] = 0; t3[3] = 0; sha1_update_vector_64 (&ctx->opad, t0, t1, t2, t3, 20); sha1_final_vector (&ctx->opad); }