Skip to content

Commit 15b824d

Browse files
committed
Fix and simplify some code related to cryptohashes
This commit addresses two issues: - In pgcrypto, MD5 computation called pg_cryptohash_{init,update,final} without checking for the result status. - Simplify pg_checksum_raw_context to use only one variable for all the SHA2 options available in checksum manifests. Reported-by: Heikki Linnakangas Discussion: https://postgr.es/m/f62f26bb-47a5-8411-46e5-4350823e06a5@iki.fi
1 parent 9ffe227 commit 15b824d

File tree

3 files changed

+32
-41
lines changed

3 files changed

+32
-41
lines changed

contrib/pgcrypto/internal.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -96,23 +96,26 @@ int_md5_update(PX_MD *h, const uint8 *data, unsigned dlen)
9696
{
9797
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
9898

99-
pg_cryptohash_update(ctx, data, dlen);
99+
if (pg_cryptohash_update(ctx, data, dlen) < 0)
100+
elog(ERROR, "could not update %s context", "MD5");
100101
}
101102

102103
static void
103104
int_md5_reset(PX_MD *h)
104105
{
105106
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
106107

107-
pg_cryptohash_init(ctx);
108+
if (pg_cryptohash_init(ctx) < 0)
109+
elog(ERROR, "could not initialize %s context", "MD5");
108110
}
109111

110112
static void
111113
int_md5_finish(PX_MD *h, uint8 *dst)
112114
{
113115
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
114116

115-
pg_cryptohash_final(ctx, dst);
117+
if (pg_cryptohash_final(ctx, dst) < 0)
118+
elog(ERROR, "could not finalize %s context", "MD5");
116119
}
117120

118121
static void

src/common/checksum_helper.c

+25-34
Original file line numberDiff line numberDiff line change
@@ -93,42 +93,42 @@ pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
9393
INIT_CRC32C(context->raw_context.c_crc32c);
9494
break;
9595
case CHECKSUM_TYPE_SHA224:
96-
context->raw_context.c_sha224 = pg_cryptohash_create(PG_SHA224);
97-
if (context->raw_context.c_sha224 == NULL)
96+
context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA224);
97+
if (context->raw_context.c_sha2 == NULL)
9898
return -1;
99-
if (pg_cryptohash_init(context->raw_context.c_sha224) < 0)
99+
if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
100100
{
101-
pg_cryptohash_free(context->raw_context.c_sha224);
101+
pg_cryptohash_free(context->raw_context.c_sha2);
102102
return -1;
103103
}
104104
break;
105105
case CHECKSUM_TYPE_SHA256:
106-
context->raw_context.c_sha256 = pg_cryptohash_create(PG_SHA256);
107-
if (context->raw_context.c_sha256 == NULL)
106+
context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA256);
107+
if (context->raw_context.c_sha2 == NULL)
108108
return -1;
109-
if (pg_cryptohash_init(context->raw_context.c_sha256) < 0)
109+
if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
110110
{
111-
pg_cryptohash_free(context->raw_context.c_sha256);
111+
pg_cryptohash_free(context->raw_context.c_sha2);
112112
return -1;
113113
}
114114
break;
115115
case CHECKSUM_TYPE_SHA384:
116-
context->raw_context.c_sha384 = pg_cryptohash_create(PG_SHA384);
117-
if (context->raw_context.c_sha384 == NULL)
116+
context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA384);
117+
if (context->raw_context.c_sha2 == NULL)
118118
return -1;
119-
if (pg_cryptohash_init(context->raw_context.c_sha384) < 0)
119+
if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
120120
{
121-
pg_cryptohash_free(context->raw_context.c_sha384);
121+
pg_cryptohash_free(context->raw_context.c_sha2);
122122
return -1;
123123
}
124124
break;
125125
case CHECKSUM_TYPE_SHA512:
126-
context->raw_context.c_sha512 = pg_cryptohash_create(PG_SHA512);
127-
if (context->raw_context.c_sha512 == NULL)
126+
context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA512);
127+
if (context->raw_context.c_sha2 == NULL)
128128
return -1;
129-
if (pg_cryptohash_init(context->raw_context.c_sha512) < 0)
129+
if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
130130
{
131-
pg_cryptohash_free(context->raw_context.c_sha512);
131+
pg_cryptohash_free(context->raw_context.c_sha2);
132132
return -1;
133133
}
134134
break;
@@ -154,19 +154,10 @@ pg_checksum_update(pg_checksum_context *context, const uint8 *input,
154154
COMP_CRC32C(context->raw_context.c_crc32c, input, len);
155155
break;
156156
case CHECKSUM_TYPE_SHA224:
157-
if (pg_cryptohash_update(context->raw_context.c_sha224, input, len) < 0)
158-
return -1;
159-
break;
160157
case CHECKSUM_TYPE_SHA256:
161-
if (pg_cryptohash_update(context->raw_context.c_sha256, input, len) < 0)
162-
return -1;
163-
break;
164158
case CHECKSUM_TYPE_SHA384:
165-
if (pg_cryptohash_update(context->raw_context.c_sha384, input, len) < 0)
166-
return -1;
167-
break;
168159
case CHECKSUM_TYPE_SHA512:
169-
if (pg_cryptohash_update(context->raw_context.c_sha512, input, len) < 0)
160+
if (pg_cryptohash_update(context->raw_context.c_sha2, input, len) < 0)
170161
return -1;
171162
break;
172163
}
@@ -207,27 +198,27 @@ pg_checksum_final(pg_checksum_context *context, uint8 *output)
207198
memcpy(output, &context->raw_context.c_crc32c, retval);
208199
break;
209200
case CHECKSUM_TYPE_SHA224:
210-
if (pg_cryptohash_final(context->raw_context.c_sha224, output) < 0)
201+
if (pg_cryptohash_final(context->raw_context.c_sha2, output) < 0)
211202
return -1;
212-
pg_cryptohash_free(context->raw_context.c_sha224);
203+
pg_cryptohash_free(context->raw_context.c_sha2);
213204
retval = PG_SHA224_DIGEST_LENGTH;
214205
break;
215206
case CHECKSUM_TYPE_SHA256:
216-
if (pg_cryptohash_final(context->raw_context.c_sha256, output) < 0)
207+
if (pg_cryptohash_final(context->raw_context.c_sha2, output) < 0)
217208
return -1;
218-
pg_cryptohash_free(context->raw_context.c_sha256);
209+
pg_cryptohash_free(context->raw_context.c_sha2);
219210
retval = PG_SHA224_DIGEST_LENGTH;
220211
break;
221212
case CHECKSUM_TYPE_SHA384:
222-
if (pg_cryptohash_final(context->raw_context.c_sha384, output) < 0)
213+
if (pg_cryptohash_final(context->raw_context.c_sha2, output) < 0)
223214
return -1;
224-
pg_cryptohash_free(context->raw_context.c_sha384);
215+
pg_cryptohash_free(context->raw_context.c_sha2);
225216
retval = PG_SHA384_DIGEST_LENGTH;
226217
break;
227218
case CHECKSUM_TYPE_SHA512:
228-
if (pg_cryptohash_final(context->raw_context.c_sha512, output) < 0)
219+
if (pg_cryptohash_final(context->raw_context.c_sha2, output) < 0)
229220
return -1;
230-
pg_cryptohash_free(context->raw_context.c_sha512);
221+
pg_cryptohash_free(context->raw_context.c_sha2);
231222
retval = PG_SHA512_DIGEST_LENGTH;
232223
break;
233224
}

src/include/common/checksum_helper.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ typedef enum pg_checksum_type
4242
typedef union pg_checksum_raw_context
4343
{
4444
pg_crc32c c_crc32c;
45-
pg_cryptohash_ctx *c_sha224;
46-
pg_cryptohash_ctx *c_sha256;
47-
pg_cryptohash_ctx *c_sha384;
48-
pg_cryptohash_ctx *c_sha512;
45+
pg_cryptohash_ctx *c_sha2;
4946
} pg_checksum_raw_context;
5047

5148
/*

0 commit comments

Comments
 (0)