Skip to content

Commit 87ae969

Browse files
committed
Move SHA2 routines to a new generic API layer for crypto hashes
Two new routines to allocate a hash context and to free it are created, as these become necessary for the goal behind this refactoring: switch the all cryptohash implementations for OpenSSL to use EVP (for FIPS and also because upstream does not recommend the use of low-level cryptohash functions for 20 years). Note that OpenSSL hides the internals of cryptohash contexts since 1.1.0, so it is necessary to leave the allocation to OpenSSL itself, explaining the need for those two new routines. This part is going to require more work to properly track hash contexts with resource owners, but this not introduced here. Still, this refactoring makes the move possible. This reduces the number of routines for all SHA2 implementations from twelve (SHA{224,256,386,512} with init, update and final calls) to five (create, free, init, update and final calls) by incorporating the hash type directly into the hash context data. The new cryptohash routines are moved to a new file, called cryptohash.c for the fallback implementations, with SHA2 specifics becoming a part internal to src/common/. OpenSSL specifics are part of cryptohash_openssl.c. This infrastructure is usable for more hash types, like MD5 or HMAC. Any code paths using the internal SHA2 routines are adapted to report correctly errors, which are most of the changes of this commit. The zones mostly impacted are checksum manifests, libpq and SCRAM. Note that e21cbb4 was a first attempt to switch SHA2 to EVP, but it lacked the refactoring needed for libpq, as done here. This patch has been tested on Linux and Windows, with and without OpenSSL, and down to 1.0.1, the oldest version supported on HEAD. Author: Michael Paquier Reviewed-by: Daniel Gustafsson Discussion: https://postgr.es/m/[email protected]
1 parent 888671a commit 87ae969

23 files changed

+1039
-549
lines changed

contrib/pgcrypto/internal-sha2.c

+41-147
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
#include <time.h>
3535

36+
#include "common/cryptohash.h"
3637
#include "common/sha2.h"
3738
#include "px.h"
3839

@@ -42,7 +43,6 @@ void init_sha384(PX_MD *h);
4243
void init_sha512(PX_MD *h);
4344

4445
/* SHA224 */
45-
4646
static unsigned
4747
int_sha224_len(PX_MD *h)
4848
{
@@ -55,42 +55,7 @@ int_sha224_block_len(PX_MD *h)
5555
return PG_SHA224_BLOCK_LENGTH;
5656
}
5757

58-
static void
59-
int_sha224_update(PX_MD *h, const uint8 *data, unsigned dlen)
60-
{
61-
pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
62-
63-
pg_sha224_update(ctx, data, dlen);
64-
}
65-
66-
static void
67-
int_sha224_reset(PX_MD *h)
68-
{
69-
pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
70-
71-
pg_sha224_init(ctx);
72-
}
73-
74-
static void
75-
int_sha224_finish(PX_MD *h, uint8 *dst)
76-
{
77-
pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
78-
79-
pg_sha224_final(ctx, dst);
80-
}
81-
82-
static void
83-
int_sha224_free(PX_MD *h)
84-
{
85-
pg_sha224_ctx *ctx = (pg_sha224_ctx *) h->p.ptr;
86-
87-
px_memset(ctx, 0, sizeof(*ctx));
88-
pfree(ctx);
89-
pfree(h);
90-
}
91-
9258
/* SHA256 */
93-
9459
static unsigned
9560
int_sha256_len(PX_MD *h)
9661
{
@@ -103,42 +68,7 @@ int_sha256_block_len(PX_MD *h)
10368
return PG_SHA256_BLOCK_LENGTH;
10469
}
10570

106-
static void
107-
int_sha256_update(PX_MD *h, const uint8 *data, unsigned dlen)
108-
{
109-
pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
110-
111-
pg_sha256_update(ctx, data, dlen);
112-
}
113-
114-
static void
115-
int_sha256_reset(PX_MD *h)
116-
{
117-
pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
118-
119-
pg_sha256_init(ctx);
120-
}
121-
122-
static void
123-
int_sha256_finish(PX_MD *h, uint8 *dst)
124-
{
125-
pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
126-
127-
pg_sha256_final(ctx, dst);
128-
}
129-
130-
static void
131-
int_sha256_free(PX_MD *h)
132-
{
133-
pg_sha256_ctx *ctx = (pg_sha256_ctx *) h->p.ptr;
134-
135-
px_memset(ctx, 0, sizeof(*ctx));
136-
pfree(ctx);
137-
pfree(h);
138-
}
139-
14071
/* SHA384 */
141-
14272
static unsigned
14373
int_sha384_len(PX_MD *h)
14474
{
@@ -151,42 +81,7 @@ int_sha384_block_len(PX_MD *h)
15181
return PG_SHA384_BLOCK_LENGTH;
15282
}
15383

154-
static void
155-
int_sha384_update(PX_MD *h, const uint8 *data, unsigned dlen)
156-
{
157-
pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
158-
159-
pg_sha384_update(ctx, data, dlen);
160-
}
161-
162-
static void
163-
int_sha384_reset(PX_MD *h)
164-
{
165-
pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
166-
167-
pg_sha384_init(ctx);
168-
}
169-
170-
static void
171-
int_sha384_finish(PX_MD *h, uint8 *dst)
172-
{
173-
pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
174-
175-
pg_sha384_final(ctx, dst);
176-
}
177-
178-
static void
179-
int_sha384_free(PX_MD *h)
180-
{
181-
pg_sha384_ctx *ctx = (pg_sha384_ctx *) h->p.ptr;
182-
183-
px_memset(ctx, 0, sizeof(*ctx));
184-
pfree(ctx);
185-
pfree(h);
186-
}
187-
18884
/* SHA512 */
189-
19085
static unsigned
19186
int_sha512_len(PX_MD *h)
19287
{
@@ -199,37 +94,40 @@ int_sha512_block_len(PX_MD *h)
19994
return PG_SHA512_BLOCK_LENGTH;
20095
}
20196

97+
/* Generic interface for all SHA2 methods */
20298
static void
203-
int_sha512_update(PX_MD *h, const uint8 *data, unsigned dlen)
99+
int_sha2_update(PX_MD *h, const uint8 *data, unsigned dlen)
204100
{
205-
pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
101+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
206102

207-
pg_sha512_update(ctx, data, dlen);
103+
if (pg_cryptohash_update(ctx, data, dlen) < 0)
104+
elog(ERROR, "could not update %s context", "SHA2");
208105
}
209106

210107
static void
211-
int_sha512_reset(PX_MD *h)
108+
int_sha2_reset(PX_MD *h)
212109
{
213-
pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
110+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
214111

215-
pg_sha512_init(ctx);
112+
if (pg_cryptohash_init(ctx) < 0)
113+
elog(ERROR, "could not initialize %s context", "SHA2");
216114
}
217115

218116
static void
219-
int_sha512_finish(PX_MD *h, uint8 *dst)
117+
int_sha2_finish(PX_MD *h, uint8 *dst)
220118
{
221-
pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
119+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
222120

223-
pg_sha512_final(ctx, dst);
121+
if (pg_cryptohash_final(ctx, dst) < 0)
122+
elog(ERROR, "could not finalize %s context", "SHA2");
224123
}
225124

226125
static void
227-
int_sha512_free(PX_MD *h)
126+
int_sha2_free(PX_MD *h)
228127
{
229-
pg_sha512_ctx *ctx = (pg_sha512_ctx *) h->p.ptr;
128+
pg_cryptohash_ctx *ctx = (pg_cryptohash_ctx *) h->p.ptr;
230129

231-
px_memset(ctx, 0, sizeof(*ctx));
232-
pfree(ctx);
130+
pg_cryptohash_free(ctx);
233131
pfree(h);
234132
}
235133

@@ -238,75 +136,71 @@ int_sha512_free(PX_MD *h)
238136
void
239137
init_sha224(PX_MD *md)
240138
{
241-
pg_sha224_ctx *ctx;
242-
243-
ctx = palloc0(sizeof(*ctx));
139+
pg_cryptohash_ctx *ctx;
244140

141+
ctx = pg_cryptohash_create(PG_SHA224);
245142
md->p.ptr = ctx;
246143

247144
md->result_size = int_sha224_len;
248145
md->block_size = int_sha224_block_len;
249-
md->reset = int_sha224_reset;
250-
md->update = int_sha224_update;
251-
md->finish = int_sha224_finish;
252-
md->free = int_sha224_free;
146+
md->reset = int_sha2_reset;
147+
md->update = int_sha2_update;
148+
md->finish = int_sha2_finish;
149+
md->free = int_sha2_free;
253150

254151
md->reset(md);
255152
}
256153

257154
void
258155
init_sha256(PX_MD *md)
259156
{
260-
pg_sha256_ctx *ctx;
261-
262-
ctx = palloc0(sizeof(*ctx));
157+
pg_cryptohash_ctx *ctx;
263158

159+
ctx = pg_cryptohash_create(PG_SHA256);
264160
md->p.ptr = ctx;
265161

266162
md->result_size = int_sha256_len;
267163
md->block_size = int_sha256_block_len;
268-
md->reset = int_sha256_reset;
269-
md->update = int_sha256_update;
270-
md->finish = int_sha256_finish;
271-
md->free = int_sha256_free;
164+
md->reset = int_sha2_reset;
165+
md->update = int_sha2_update;
166+
md->finish = int_sha2_finish;
167+
md->free = int_sha2_free;
272168

273169
md->reset(md);
274170
}
275171

276172
void
277173
init_sha384(PX_MD *md)
278174
{
279-
pg_sha384_ctx *ctx;
280-
281-
ctx = palloc0(sizeof(*ctx));
175+
pg_cryptohash_ctx *ctx;
282176

177+
ctx = pg_cryptohash_create(PG_SHA384);
283178
md->p.ptr = ctx;
284179

285180
md->result_size = int_sha384_len;
286181
md->block_size = int_sha384_block_len;
287-
md->reset = int_sha384_reset;
288-
md->update = int_sha384_update;
289-
md->finish = int_sha384_finish;
290-
md->free = int_sha384_free;
182+
md->reset = int_sha2_reset;
183+
md->update = int_sha2_update;
184+
md->finish = int_sha2_finish;
185+
md->free = int_sha2_free;
291186

292187
md->reset(md);
293188
}
294189

295190
void
296191
init_sha512(PX_MD *md)
297192
{
298-
pg_sha512_ctx *ctx;
299-
300-
ctx = palloc0(sizeof(*ctx));
193+
pg_cryptohash_ctx *ctx;
301194

195+
ctx = pg_cryptohash_create(PG_SHA512);
302196
md->p.ptr = ctx;
303197

304198
md->result_size = int_sha512_len;
305199
md->block_size = int_sha512_block_len;
306-
md->reset = int_sha512_reset;
307-
md->update = int_sha512_update;
308-
md->finish = int_sha512_finish;
309-
md->free = int_sha512_free;
200+
md->reset = int_sha2_reset;
201+
md->update = int_sha2_update;
202+
md->finish = int_sha2_finish;
203+
md->free = int_sha2_free;
310204

311205
md->reset(md);
312206
}

0 commit comments

Comments
 (0)