void sha1_pad(struct sha1_ctxt *ctxt) { size_t padlen; /*pad length in bytes*/ size_t padstart; PUTPAD(0x80); padstart = COUNT % 64; padlen = 64 - padstart; if (padlen < 8) { memset(&ctxt->m.b8[padstart], 0, padlen); COUNT += padlen; COUNT %= 64; sha1_step(ctxt); padstart = COUNT % 64; /* should be 0 */ padlen = 64 - padstart; /* should be 64 */ } memset(&ctxt->m.b8[padstart], 0, padlen - 8); COUNT += (padlen - 8); COUNT %= 64; #if WORDS_BIGENDIAN PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]); #else PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]); #endif }
void sha1_loop(struct sha1_ctxt *ctxt, const void *input, size_t len) { const unsigned char *input_c = input; size_t gaplen; size_t gapstart; size_t off; size_t copysiz; off = 0; while (off < len) { gapstart = COUNT % 64; gaplen = 64 - gapstart; copysiz = (gaplen < len - off) ? gaplen : len - off; memmove(&ctxt->m.b8[gapstart], &input_c[off], copysiz); COUNT += copysiz; COUNT %= 64; ctxt->c.b64[0] += copysiz * 8; if (COUNT % 64 == 0) sha1_step(ctxt); off += copysiz; } }
void sha1_loop(sha1_ctxt* ctxt, const uint8_t *input0, size_t len) { const uint8_t *input; uint8_t gaplen; uint8_t gapstart; size_t off; uint8_t copysiz; input = (const uint8_t *) input0; off = 0; while (off < len) { gapstart = COUNT % 64; gaplen = 64 - gapstart; copysiz = (gaplen < len - off) ? gaplen : (uint8_t) (len - off); memmove(&ctxt->m.b8[gapstart], &input[off], copysiz); COUNT += copysiz; COUNT %= 64; ctxt->c.b64[0] += copysiz * 8; if (COUNT % 64 == 0) sha1_step(ctxt); off += copysiz; } }
static inline void sha1_loop_tmpl(Iterator &start, const Iterator &end, sha1_ctxt* ctxt) { uint8_t gaplen; uint8_t gapstart; uint8_t copysiz; while (start < end) { gapstart = COUNT % 64; gaplen = 64 - gapstart; uint8_t i = 0; for (i = 0; i < gaplen && start < end; ++i, ++start ) { ctxt->m.b8[gapstart + i] = (uint8_t) *start; } copysiz = i; COUNT += copysiz; COUNT %= 64; ctxt->c.b64[0] += copysiz * 8; if (COUNT % 64 == 0) sha1_step(ctxt); } }
void sha1_pad(struct sha1_ctxt *ctxt) { size_t padlen; /*pad length in bytes*/ size_t padstart; PUTPAD(0x80); padstart = COUNT % 64; padlen = 64 - padstart; if (padlen < 8) { bzero(&ctxt->m.b8[padstart], padlen); COUNT += (unsigned char)padlen; COUNT %= 64; sha1_step(ctxt); padstart = COUNT % 64; /* should be 0 */ padlen = 64 - padstart; /* should be 64 */ } bzero(&ctxt->m.b8[padstart], padlen - 8); COUNT += ((unsigned char)padlen - 8); COUNT %= 64; #if BYTE_ORDER == BIG_ENDIAN PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]); #else PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]); #endif }
int test(const uint32_t *message, uint32_t num_steps, const uint32_t *H_expected) { unsigned i; printf("num_steps=%lu\n", (unsigned long)num_steps); init_H(); sha1_step(H, message, num_steps); printf("\nmessage:"); for (i = 0; i < num_steps * SHA1_STEP_SIZE; ++i) { if (i % 8 == 0) { printf("\n\t"); } printf(" %08x", SWAP(message[i])); } printf("\nexpected:"); dump_H(H_expected); printf("\nresult :"); dump_H(H); printf("\n"); if (memcmp(H, H_expected, SHA1_HASH_SIZE*sizeof(uint32_t))) { printf("==> FAILURE\n"); return 1; } printf("==> PASS\n"); return 0; }
void SHA1Update(SHA1_CTXT* ctxt, const os_byte *input0, size_t len) { const os_byte *input; size_t gaplen; size_t gapstart; size_t off; size_t copysiz; input = (const os_byte *) input0; off = 0; while (off < len) { gapstart = COUNT % 64; gaplen = 64 - gapstart; copysiz = (gaplen < len - off) ? gaplen : len - off; memmove(&ctxt->m.b8[gapstart], &input[off], copysiz); COUNT += copysiz; COUNT %= 64; ctxt->c.b64[0] += copysiz * 8; if (COUNT % 64 == 0) sha1_step(ctxt); off += copysiz; } }
static void sha1_loop( struct sha1_ctxt *ctxt, const u_int8_t *input, size_t len) { size_t gaplen; size_t gapstart; size_t off; size_t copysiz; off = 0; while (off < len) { gapstart = COUNT % 64; gaplen = 64 - gapstart; copysiz = (gaplen < len - off) ? gaplen : len - off; bcopy(&input[off], &ctxt->m.b8[gapstart], copysiz); COUNT += copysiz; COUNT %= 64; ctxt->c.b64[0] += copysiz * 8; if (COUNT % 64 == 0) sha1_step(ctxt); off += copysiz; } }
void one(unsigned n) { unsigned i, j; struct timeval tv_start, tv_end; double delta; double best; unsigned n_iter; n_iter = 1000*(8192/n); best = INFINITY; for (j = 0; j < N_SAMPLE; ++j) { gettimeofday(&tv_start, 0); for (i = 0; i < n_iter; ++i) { sha1_step(H, buf, n/SHA1_STEP_SIZE); } gettimeofday(&tv_end, 0); __asm volatile("emms"); delta = (double)(tv_end.tv_sec - tv_start.tv_sec) + (double)(tv_end.tv_usec - tv_start.tv_usec) / 1000000.0; if (delta < best) { best = delta; } } /* print a number similar to what openssl reports */ printf("%.2f KB/s (for %u byte buffer)\n", (double)(n * sizeof(uint32_t) * n_iter) / best / 1000.0 + 0.005, n*sizeof(uint32_t)); }
static void sha1_pad(sha1_ctxt * ctxt) { uint8_t padlen; /* pad length in bytes */ uint8_t padstart; PUTPAD(0x80); padstart = COUNT % 64; padlen = 64 - padstart; if (padlen < 8) { memset(&ctxt->m.b8[padstart], 0, padlen); COUNT += padlen; COUNT %= 64; sha1_step(ctxt); padstart = COUNT % 64; /* should be 0 */ padlen = 64 - padstart; /* should be 64 */ } memset(&ctxt->m.b8[padstart], 0, padlen - 8); COUNT += (padlen - 8); COUNT %= 64; #ifdef BIG_ENDIAN_ORDER PUTPAD(ctxt->c.b8[0]); PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[7]); #else PUTPAD(ctxt->c.b8[7]); PUTPAD(ctxt->c.b8[6]); PUTPAD(ctxt->c.b8[5]); PUTPAD(ctxt->c.b8[4]); PUTPAD(ctxt->c.b8[3]); PUTPAD(ctxt->c.b8[2]); PUTPAD(ctxt->c.b8[1]); PUTPAD(ctxt->c.b8[0]); #endif }
inline void test_compress(const uint32 ihv[5], uint32 me[80]) { uint32 Q[85]; uint32 ihv1[5]; uint32 ihv2[5]; for (unsigned i = 0; i < 5; ++i) ihv1[i] = ihv[i]; sha1compress_me(ihv1, me); Q[0] = rotate_right(ihv[4], 30); Q[1] = rotate_right(ihv[3], 30); Q[2] = rotate_right(ihv[2], 30); Q[3] = ihv[1]; Q[4] = ihv[0]; for (unsigned t = 0; t < 80; ++t) sha1_step(t, Q, me); ihv2[0] = ihv[0] + Q[84]; ihv2[1] = ihv[1] + Q[83]; ihv2[2] = ihv[2] + rotate_left(Q[82],30); ihv2[3] = ihv[3] + rotate_left(Q[81],30); ihv2[4] = ihv[4] + rotate_left(Q[80],30); exit(0); }