*: pass md5/shaN context pointer as 1st arg, not last

function                                             old     new   delta
md5_hash_block                                       458     459      +1
filter_rename_config                                 252     250      -2
md5_crypt                                            591     587      -4

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2010-10-16 20:45:27 +02:00
parent 1a5e11c874
commit c0683acce8
7 changed files with 40 additions and 40 deletions

View File

@ -1524,8 +1524,8 @@ static char FAST_FUNC filter_rename_config(archive_handle_t *archive_handle)
buf = xmalloc(4096); buf = xmalloc(4096);
md5_begin(&md5); md5_begin(&md5);
while ((count = safe_read(fd, buf, 4096)) > 0) while ((count = safe_read(fd, buf, 4096)) > 0)
md5_hash(buf, count, &md5); md5_hash(&md5, buf, count);
md5_end(buf, &md5); /* using buf as result storage */ md5_end(&md5, buf); /* using buf as result storage */
close(fd); close(fd);
md5line = xmalloc(16 * 2 + 2 + strlen(name_ptr) + 1); md5line = xmalloc(16 * 2 + 2 + strlen(name_ptr) + 1);

View File

@ -43,7 +43,7 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
} context; } context;
uint8_t *hash_value = NULL; uint8_t *hash_value = NULL;
RESERVE_CONFIG_UBUFFER(in_buf, 4096); RESERVE_CONFIG_UBUFFER(in_buf, 4096);
void FAST_FUNC (*update)(const void*, size_t, void*); void FAST_FUNC (*update)(void*, const void*, size_t);
void FAST_FUNC (*final)(void*, void*); void FAST_FUNC (*final)(void*, void*);
hash_algo_t hash_algo = applet_name[3]; hash_algo_t hash_algo = applet_name[3];
@ -78,11 +78,11 @@ static uint8_t *hash_file(const char *filename /*, hash_algo_t hash_algo*/)
} }
while (0 < (count = safe_read(src_fd, in_buf, 4096))) { while (0 < (count = safe_read(src_fd, in_buf, 4096))) {
update(in_buf, count, &context); update(&context, in_buf, count);
} }
if (count == 0) { if (count == 0) {
final(in_buf, &context); final(&context, in_buf);
hash_value = hash_bin_to_hex(in_buf, hash_len); hash_value = hash_bin_to_hex(in_buf, hash_len);
} }

View File

@ -1519,8 +1519,8 @@ typedef struct sha1_ctx_t {
void (*process_block)(struct sha1_ctx_t*) FAST_FUNC; void (*process_block)(struct sha1_ctx_t*) FAST_FUNC;
} sha1_ctx_t; } sha1_ctx_t;
void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC; void sha1_begin(sha1_ctx_t *ctx) FAST_FUNC;
void sha1_hash(const void *data, size_t length, sha1_ctx_t *ctx) FAST_FUNC; void sha1_hash(sha1_ctx_t *ctx, const void *data, size_t length) FAST_FUNC;
void sha1_end(void *resbuf, sha1_ctx_t *ctx) FAST_FUNC; void sha1_end(sha1_ctx_t *ctx, void *resbuf) FAST_FUNC;
typedef struct sha1_ctx_t sha256_ctx_t; typedef struct sha1_ctx_t sha256_ctx_t;
void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC; void sha256_begin(sha256_ctx_t *ctx) FAST_FUNC;
#define sha256_hash sha1_hash #define sha256_hash sha1_hash
@ -1531,8 +1531,8 @@ typedef struct sha512_ctx_t {
uint8_t wbuffer[128]; /* NB: always correctly aligned for uint64_t */ uint8_t wbuffer[128]; /* NB: always correctly aligned for uint64_t */
} sha512_ctx_t; } sha512_ctx_t;
void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC; void sha512_begin(sha512_ctx_t *ctx) FAST_FUNC;
void sha512_hash(const void *buffer, size_t len, sha512_ctx_t *ctx) FAST_FUNC; void sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len) FAST_FUNC;
void sha512_end(void *resbuf, sha512_ctx_t *ctx) FAST_FUNC; void sha512_end(sha512_ctx_t *ctx, void *resbuf) FAST_FUNC;
#if 1 #if 1
typedef struct md5_ctx_t { typedef struct md5_ctx_t {
uint32_t A; uint32_t A;
@ -1551,8 +1551,8 @@ typedef struct md5_ctx_t {
} md5_ctx_t; } md5_ctx_t;
#endif #endif
void md5_begin(md5_ctx_t *ctx) FAST_FUNC; void md5_begin(md5_ctx_t *ctx) FAST_FUNC;
void md5_hash(const void *data, size_t length, md5_ctx_t *ctx) FAST_FUNC; void md5_hash(md5_ctx_t *ctx, const void *data, size_t length) FAST_FUNC;
void md5_end(void *resbuf, md5_ctx_t *ctx) FAST_FUNC; void md5_end(md5_ctx_t *ctx, void *resbuf) FAST_FUNC;
uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC; uint32_t *crc32_filltable(uint32_t *tbl256, int endian) FAST_FUNC;

View File

@ -49,7 +49,7 @@ void FAST_FUNC md5_begin(md5_ctx_t *ctx)
#define rotl32(w, s) (((w) << (s)) | ((w) >> (32 - (s)))) #define rotl32(w, s) (((w) << (s)) | ((w) >> (32 - (s))))
/* Hash a single block, 64 bytes long and 4-byte aligned. */ /* Hash a single block, 64 bytes long and 4-byte aligned. */
static void md5_hash_block(const void *buffer, md5_ctx_t *ctx) static void md5_hash_block(md5_ctx_t *ctx, const void *buffer)
{ {
uint32_t correct_words[16]; uint32_t correct_words[16];
const uint32_t *words = buffer; const uint32_t *words = buffer;
@ -361,7 +361,7 @@ static void md5_hash_block(const void *buffer, md5_ctx_t *ctx)
* with chunks of data that are 4-byte aligned and a multiple of 64 bytes. * with chunks of data that are 4-byte aligned and a multiple of 64 bytes.
* This function's internal buffer remembers previous data until it has 64 * This function's internal buffer remembers previous data until it has 64
* bytes worth to pass on. Call md5_end() to flush this buffer. */ * bytes worth to pass on. Call md5_end() to flush this buffer. */
void FAST_FUNC md5_hash(const void *buffer, size_t len, md5_ctx_t *ctx) void FAST_FUNC md5_hash(md5_ctx_t *ctx, const void *buffer, size_t len)
{ {
const char *buf = buffer; const char *buf = buffer;
unsigned buflen = BUFLEN(ctx); unsigned buflen = BUFLEN(ctx);
@ -385,7 +385,7 @@ void FAST_FUNC md5_hash(const void *buffer, size_t len, md5_ctx_t *ctx)
if (buflen != 0) if (buflen != 0)
break; break;
/* Buffer is filled up, process it. */ /* Buffer is filled up, process it. */
md5_hash_block(ctx->buffer, ctx); md5_hash_block(ctx, ctx->buffer);
/*buflen = 0; - already is */ /*buflen = 0; - already is */
} }
} }
@ -395,7 +395,7 @@ void FAST_FUNC md5_hash(const void *buffer, size_t len, md5_ctx_t *ctx)
* endian byte order, so that a byte-wise output yields to the wanted * endian byte order, so that a byte-wise output yields to the wanted
* ASCII representation of the message digest. * ASCII representation of the message digest.
*/ */
void FAST_FUNC md5_end(void *resbuf, md5_ctx_t *ctx) void FAST_FUNC md5_end(md5_ctx_t *ctx, void *resbuf)
{ {
uint64_t total; uint64_t total;
char *buf = ctx->buffer; char *buf = ctx->buffer;
@ -419,8 +419,8 @@ void FAST_FUNC md5_end(void *resbuf, md5_ctx_t *ctx)
/* Process last bytes. */ /* Process last bytes. */
if (buf != ctx->buffer) if (buf != ctx->buffer)
md5_hash_block(ctx->buffer, ctx); md5_hash_block(ctx, ctx->buffer);
md5_hash_block(buf, ctx); md5_hash_block(ctx, buf);
/* The MD5 result is in little endian byte order. /* The MD5 result is in little endian byte order.
* We (ab)use the fact that A-D are consecutive in memory. * We (ab)use the fact that A-D are consecutive in memory.

View File

@ -92,10 +92,10 @@ md5_crypt(char result[MD5_OUT_BUFSIZE], const unsigned char *pw, const unsigned
/* Hash. the password first, since that is what is most unknown */ /* Hash. the password first, since that is what is most unknown */
md5_begin(&ctx); md5_begin(&ctx);
pw_len = strlen((char*)pw); pw_len = strlen((char*)pw);
md5_hash(pw, pw_len, &ctx); md5_hash(&ctx, pw, pw_len);
/* Then the salt including "$1$" */ /* Then the salt including "$1$" */
md5_hash(salt, sl, &ctx); md5_hash(&ctx, salt, sl);
/* Copy salt to result; skip "$1$" */ /* Copy salt to result; skip "$1$" */
memcpy(result, salt, sl); memcpy(result, salt, sl);
@ -105,19 +105,19 @@ md5_crypt(char result[MD5_OUT_BUFSIZE], const unsigned char *pw, const unsigned
/* Then just as many characters of the MD5(pw, salt, pw) */ /* Then just as many characters of the MD5(pw, salt, pw) */
md5_begin(&ctx1); md5_begin(&ctx1);
md5_hash(pw, pw_len, &ctx1); md5_hash(&ctx1, pw, pw_len);
md5_hash(salt, sl, &ctx1); md5_hash(&ctx1, salt, sl);
md5_hash(pw, pw_len, &ctx1); md5_hash(&ctx1, pw, pw_len);
md5_end(final, &ctx1); md5_end(&ctx1, final);
for (pl = pw_len; pl > 0; pl -= 16) for (pl = pw_len; pl > 0; pl -= 16)
md5_hash(final, pl > 16 ? 16 : pl, &ctx); md5_hash(&ctx, final, pl > 16 ? 16 : pl);
/* Then something really weird... */ /* Then something really weird... */
memset(final, 0, sizeof(final)); memset(final, 0, sizeof(final));
for (i = pw_len; i; i >>= 1) { for (i = pw_len; i; i >>= 1) {
md5_hash(((i & 1) ? final : (const unsigned char *) pw), 1, &ctx); md5_hash(&ctx, ((i & 1) ? final : (const unsigned char *) pw), 1);
} }
md5_end(final, &ctx); md5_end(&ctx, final);
/* And now, just to make sure things don't run too fast. /* And now, just to make sure things don't run too fast.
* On a 60 Mhz Pentium this takes 34 msec, so you would * On a 60 Mhz Pentium this takes 34 msec, so you would
@ -126,21 +126,21 @@ md5_crypt(char result[MD5_OUT_BUFSIZE], const unsigned char *pw, const unsigned
for (i = 0; i < 1000; i++) { for (i = 0; i < 1000; i++) {
md5_begin(&ctx1); md5_begin(&ctx1);
if (i & 1) if (i & 1)
md5_hash(pw, pw_len, &ctx1); md5_hash(&ctx1, pw, pw_len);
else else
md5_hash(final, 16, &ctx1); md5_hash(&ctx1, final, 16);
if (i % 3) if (i % 3)
md5_hash(salt, sl, &ctx1); md5_hash(&ctx1, salt, sl);
if (i % 7) if (i % 7)
md5_hash(pw, pw_len, &ctx1); md5_hash(&ctx1, pw, pw_len);
if (i & 1) if (i & 1)
md5_hash(final, 16, &ctx1); md5_hash(&ctx1, final, 16);
else else
md5_hash(pw, pw_len, &ctx1); md5_hash(&ctx1, pw, pw_len);
md5_end(final, &ctx1); md5_end(&ctx1, final);
} }
p = result + sl + 4; /* 12 bytes max (sl is up to 8 bytes) */ p = result + sl + 4; /* 12 bytes max (sl is up to 8 bytes) */

View File

@ -361,7 +361,7 @@ void FAST_FUNC sha512_begin(sha512_ctx_t *ctx)
/* Used also for sha256 */ /* Used also for sha256 */
void FAST_FUNC sha1_hash(const void *buffer, size_t len, sha1_ctx_t *ctx) void FAST_FUNC sha1_hash(sha1_ctx_t *ctx, const void *buffer, size_t len)
{ {
unsigned in_buf = ctx->total64 & 63; unsigned in_buf = ctx->total64 & 63;
unsigned add = 64 - in_buf; unsigned add = 64 - in_buf;
@ -380,7 +380,7 @@ void FAST_FUNC sha1_hash(const void *buffer, size_t len, sha1_ctx_t *ctx)
memcpy(ctx->wbuffer + in_buf, buffer, len); memcpy(ctx->wbuffer + in_buf, buffer, len);
} }
void FAST_FUNC sha512_hash(const void *buffer, size_t len, sha512_ctx_t *ctx) void FAST_FUNC sha512_hash(sha512_ctx_t *ctx, const void *buffer, size_t len)
{ {
unsigned in_buf = ctx->total64[0] & 127; unsigned in_buf = ctx->total64[0] & 127;
unsigned add = 128 - in_buf; unsigned add = 128 - in_buf;
@ -406,7 +406,7 @@ void FAST_FUNC sha512_hash(const void *buffer, size_t len, sha512_ctx_t *ctx)
/* Used also for sha256 */ /* Used also for sha256 */
void FAST_FUNC sha1_end(void *resbuf, sha1_ctx_t *ctx) void FAST_FUNC sha1_end(sha1_ctx_t *ctx, void *resbuf)
{ {
unsigned pad, in_buf; unsigned pad, in_buf;
@ -442,7 +442,7 @@ void FAST_FUNC sha1_end(void *resbuf, sha1_ctx_t *ctx)
memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * in_buf); memcpy(resbuf, ctx->hash, sizeof(ctx->hash[0]) * in_buf);
} }
void FAST_FUNC sha512_end(void *resbuf, sha512_ctx_t *ctx) void FAST_FUNC sha512_end(sha512_ctx_t *ctx, void *resbuf)
{ {
unsigned pad, in_buf; unsigned pad, in_buf;

View File

@ -109,9 +109,9 @@ int popmaildir_main(int argc UNUSED_PARAM, char **argv)
s[1] = '\0'; s[1] = '\0';
// get md5 sum of "<stamp>password" string // get md5 sum of "<stamp>password" string
md5_begin(&md5.ctx); md5_begin(&md5.ctx);
md5_hash(buf, strlen(buf), &md5.ctx); md5_hash(&md5.ctx, buf, strlen(buf));
md5_hash(G.pass, strlen(G.pass), &md5.ctx); md5_hash(&md5.ctx, G.pass, strlen(G.pass));
md5_end(res, &md5.ctx); md5_end(&md5.ctx, res);
*bin2hex(md5.hex, (char*)res, 16) = '\0'; *bin2hex(md5.hex, (char*)res, 16) = '\0';
// APOP // APOP
s = xasprintf("%s %s", G.user, md5.hex); s = xasprintf("%s %s", G.user, md5.hex);