Skip to content

Commit 9b58495

Browse files
committed
Improve some code around cryptohash functions
This adjusts some code related to recent changes for cryptohash functions: - Add a variable in md5.h to track down the size of a computed result, moved from pgcrypto. Note that pg_md5_hash() assumed a result of this size already. - Call explicit_bzero() on the hashed data when freeing the context for fallback implementations. For MD5, particularly, it would be annoying to leave some non-zeroed data around. - Clean up some code related to recent changes of uuid-ossp. .gitignore still included md5.c and a comment was incorrect. Discussion: https://postgr.es/m/[email protected]
1 parent df9274a commit 9b58495

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

contrib/pgcrypto/internal.c

-4
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@
4141
#include "common/cryptohash.h"
4242
#include "common/md5.h"
4343

44-
#ifndef MD5_DIGEST_LENGTH
45-
#define MD5_DIGEST_LENGTH 16
46-
#endif
47-
4844
#ifndef SHA1_DIGEST_LENGTH
4945
#ifdef SHA1_RESULTLEN
5046
#define SHA1_DIGEST_LENGTH SHA1_RESULTLEN

contrib/uuid-ossp/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/md5.c
21
/sha1.c
32
# Generated subdirectories
43
/log/

contrib/uuid-ossp/uuid-ossp.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
#undef uuid_hash
4242

4343
/*
44-
* Some BSD variants offer md5 and sha1 implementations but Linux does not,
45-
* so we use a copy of the ones from pgcrypto. Not needed with OSSP, though.
44+
* Some BSD variants offer sha1 implementation but Linux does not, so we use
45+
* a copy from pgcrypto. Not needed with OSSP, though.
4646
*/
4747
#ifndef HAVE_UUID_OSSP
4848
#include "sha1.h"

src/common/cryptohash.c

+20
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,26 @@ pg_cryptohash_free(pg_cryptohash_ctx *ctx)
197197
{
198198
if (ctx == NULL)
199199
return;
200+
201+
switch (ctx->type)
202+
{
203+
case PG_MD5:
204+
explicit_bzero(ctx->data, sizeof(pg_md5_ctx));
205+
break;
206+
case PG_SHA224:
207+
explicit_bzero(ctx->data, sizeof(pg_sha224_ctx));
208+
break;
209+
case PG_SHA256:
210+
explicit_bzero(ctx->data, sizeof(pg_sha256_ctx));
211+
break;
212+
case PG_SHA384:
213+
explicit_bzero(ctx->data, sizeof(pg_sha384_ctx));
214+
break;
215+
case PG_SHA512:
216+
explicit_bzero(ctx->data, sizeof(pg_sha512_ctx));
217+
break;
218+
}
219+
200220
FREE(ctx->data);
201221
explicit_bzero(ctx, sizeof(pg_cryptohash_ctx));
202222
FREE(ctx);

src/common/md5_common.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ bytesToHex(uint8 b[16], char *s)
6969
bool
7070
pg_md5_hash(const void *buff, size_t len, char *hexsum)
7171
{
72-
uint8 sum[16];
72+
uint8 sum[MD5_DIGEST_LENGTH];
7373
pg_cryptohash_ctx *ctx;
7474

7575
ctx = pg_cryptohash_create(PG_MD5);

src/include/common/md5.h

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
#ifndef PG_MD5_H
1717
#define PG_MD5_H
1818

19+
/* Size of result generated by MD5 computation */
20+
#define MD5_DIGEST_LENGTH 16
21+
22+
/* password-related data */
1923
#define MD5_PASSWD_CHARSET "0123456789abcdef"
2024
#define MD5_PASSWD_LEN 35
2125

0 commit comments

Comments
 (0)