From: tedu Date: Sun, 28 Dec 2014 10:04:35 +0000 (+0000) Subject: convert bcopy to memcpy in md5 and sha1. also be consistent about clearing X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=2f7c17dc487c24a3b8b19d81470c8a5cbc302ce6;p=openbsd convert bcopy to memcpy in md5 and sha1. also be consistent about clearing context and making digest required to Final. --- diff --git a/sys/crypto/md5.c b/sys/crypto/md5.c index 1b85f1badbb..a896aa1f230 100644 --- a/sys/crypto/md5.c +++ b/sys/crypto/md5.c @@ -1,4 +1,4 @@ -/* $OpenBSD: md5.c,v 1.3 2014/11/16 17:39:09 tedu Exp $ */ +/* $OpenBSD: md5.c,v 1.4 2014/12/28 10:04:35 tedu Exp $ */ /* * This code implements the MD5 message-digest algorithm. @@ -76,7 +76,7 @@ MD5Update(MD5_CTX *ctx, const void *inputptr, size_t len) if (len >= need) { if (have != 0) { - bcopy(input, ctx->buffer + have, need); + memcpy(ctx->buffer + have, input, need); MD5Transform(ctx->state, ctx->buffer); input += need; len -= need; @@ -93,7 +93,7 @@ MD5Update(MD5_CTX *ctx, const void *inputptr, size_t len) /* Handle any remaining bytes of data. */ if (len != 0) - bcopy(input, ctx->buffer + have, len); + memcpy(ctx->buffer + have, input, len); } /* @@ -118,10 +118,8 @@ MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *ctx) MD5Update(ctx, PADDING, padlen - 8); /* padlen - 8 <= 64 */ MD5Update(ctx, count, 8); - if (digest != NULL) { - for (i = 0; i < 4; i++) - PUT_32BIT_LE(digest + i * 4, ctx->state[i]); - } + for (i = 0; i < 4; i++) + PUT_32BIT_LE(digest + i * 4, ctx->state[i]); explicit_bzero(ctx, sizeof(*ctx)); /* in case it's sensitive */ } @@ -149,7 +147,7 @@ MD5Transform(u_int32_t state[4], const u_int8_t block[MD5_BLOCK_LENGTH]) u_int32_t a, b, c, d, in[MD5_BLOCK_LENGTH / 4]; #if BYTE_ORDER == LITTLE_ENDIAN - bcopy(block, in, sizeof(in)); + memcpy(in, block, sizeof(in)); #else for (a = 0; a < MD5_BLOCK_LENGTH / 4; a++) { in[a] = (u_int32_t)( diff --git a/sys/crypto/sha1.c b/sys/crypto/sha1.c index 8e624aeaf82..1e910ae5a97 100644 --- a/sys/crypto/sha1.c +++ b/sys/crypto/sha1.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sha1.c,v 1.10 2014/11/16 17:39:09 tedu Exp $ */ +/* $OpenBSD: sha1.c,v 1.11 2014/12/28 10:04:35 tedu Exp $ */ /* * SHA-1 in C @@ -59,7 +59,7 @@ SHA1Transform(u_int32_t state[5], const unsigned char buffer[SHA1_BLOCK_LENGTH]) unsigned char workspace[SHA1_BLOCK_LENGTH]; block = (CHAR64LONG16 *)workspace; - bcopy(buffer, block, SHA1_BLOCK_LENGTH); + memcpy(block, buffer, SHA1_BLOCK_LENGTH); #else block = (CHAR64LONG16 *)buffer; #endif @@ -130,7 +130,7 @@ SHA1Update(SHA1_CTX *context, const void *dataptr, unsigned int len) j = (u_int32_t)((context->count >> 3) & 63); context->count += (len << 3); if ((j + len) > 63) { - bcopy(data, &context->buffer[j], (i = 64 - j)); + memcpy(&context->buffer[j], data, (i = 64 - j)); SHA1Transform(context->state, context->buffer); for ( ; i + 63 < len; i += 64) { SHA1Transform(context->state, &data[i]); @@ -138,7 +138,7 @@ SHA1Update(SHA1_CTX *context, const void *dataptr, unsigned int len) j = 0; } else i = 0; - bcopy(&data[i], &context->buffer[j], len - i); + memcpy(&context->buffer[j], &data[i], len - i); } @@ -160,20 +160,10 @@ SHA1Final(unsigned char digest[SHA1_DIGEST_LENGTH], SHA1_CTX *context) } SHA1Update(context, finalcount, 8); /* Should cause a SHA1Transform() */ - if (digest) - for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { - digest[i] = (unsigned char)((context->state[i >> 2] >> - ((3 - (i & 3)) * 8)) & 255); - } - explicit_bzero(&finalcount, 8); -#if 0 /* We want to use this for "keyfill" */ - /* Wipe variables */ - i = 0; - bzero(context->buffer, 64); - bzero(context->state, 20); - bzero(context->count, 8); -#ifdef SHA1HANDSOFF /* make SHA1Transform overwrite its own static vars */ - SHA1Transform(context->state, context->buffer); -#endif -#endif + for (i = 0; i < SHA1_DIGEST_LENGTH; i++) { + digest[i] = (unsigned char)((context->state[i >> 2] >> + ((3 - (i & 3)) * 8)) & 255); + } + explicit_bzero(&finalcount, sizeof(finalcount)); + explicit_bzero(context, sizeof(*context)); }