Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit fe0a1dc

Browse files
committed
Revert "Change SHA2 implementation based on OpenSSL to use EVP digest routines"
This reverts commit e21cbb4, as the switch to EVP routines requires a more careful design where we would need to have at least our wrapper routines return a status instead of issuing an error by themselves to let the caller do the error handling. The memory handling was also incorrect and could cause leaks in the backend if a failure happened, requiring most likely a callback to do the necessary cleanup as the only clean way to be able to allocate an EVP context requires the use of an allocation within OpenSSL. The potential rework of the wrappers also impacts the fallback implementation when not building with OpenSSL. Originally, prairiedog has reported a compilation failure, but after discussion with Tom Lane this needs a better design. Discussion: https://postgr.es/m/20200928073330.GC2316@paquier.xyz
1 parent 042d801 commit fe0a1dc

File tree

2 files changed

+19
-54
lines changed

2 files changed

+19
-54
lines changed

src/common/sha2_openssl.c

+14-49
Original file line numberDiff line numberDiff line change
@@ -20,118 +20,83 @@
2020
#include "postgres_fe.h"
2121
#endif
2222

23-
#include "common/sha2.h"
24-
25-
#ifdef FRONTEND
26-
#include "common/logging.h"
27-
#else
28-
#include "miscadmin.h"
29-
#endif
23+
#include <openssl/sha.h>
3024

31-
#ifdef FRONTEND
32-
#define sha2_log_and_abort(...) \
33-
do { pg_log_fatal(__VA_ARGS__); exit(1); } while(0)
34-
#else
35-
#define sha2_log_and_abort(...) elog(ERROR, __VA_ARGS__)
36-
#endif
37-
38-
static void
39-
digest_init(EVP_MD_CTX **ctx, const EVP_MD *type)
40-
{
41-
*ctx = EVP_MD_CTX_create();
42-
if (*ctx == NULL)
43-
sha2_log_and_abort("could not create EVP digest context");
44-
if (EVP_DigestInit_ex(*ctx, type, NULL) <= 0)
45-
sha2_log_and_abort("could not initialize EVP digest context");
46-
}
47-
48-
static void
49-
digest_update(EVP_MD_CTX **ctx, const uint8 *data, size_t len)
50-
{
51-
if (EVP_DigestUpdate(*ctx, data, len) <= 0)
52-
sha2_log_and_abort("could not update EVP digest context");
53-
}
25+
#include "common/sha2.h"
5426

55-
static void
56-
digest_final(EVP_MD_CTX **ctx, uint8 *dest)
57-
{
58-
if (EVP_DigestFinal_ex(*ctx, dest, 0) <= 0)
59-
sha2_log_and_abort("could not finalize EVP digest context");
60-
EVP_MD_CTX_destroy(*ctx);
61-
}
6227

6328
/* Interface routines for SHA-256 */
6429
void
6530
pg_sha256_init(pg_sha256_ctx *ctx)
6631
{
67-
digest_init(ctx, EVP_sha256());
32+
SHA256_Init((SHA256_CTX *) ctx);
6833
}
6934

7035
void
7136
pg_sha256_update(pg_sha256_ctx *ctx, const uint8 *data, size_t len)
7237
{
73-
digest_update(ctx, data, len);
38+
SHA256_Update((SHA256_CTX *) ctx, data, len);
7439
}
7540

7641
void
7742
pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest)
7843
{
79-
digest_final(ctx, dest);
44+
SHA256_Final(dest, (SHA256_CTX *) ctx);
8045
}
8146

8247
/* Interface routines for SHA-512 */
8348
void
8449
pg_sha512_init(pg_sha512_ctx *ctx)
8550
{
86-
digest_init(ctx, EVP_sha512());
51+
SHA512_Init((SHA512_CTX *) ctx);
8752
}
8853

8954
void
9055
pg_sha512_update(pg_sha512_ctx *ctx, const uint8 *data, size_t len)
9156
{
92-
digest_update(ctx, data, len);
57+
SHA512_Update((SHA512_CTX *) ctx, data, len);
9358
}
9459

9560
void
9661
pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest)
9762
{
98-
digest_final(ctx, dest);
63+
SHA512_Final(dest, (SHA512_CTX *) ctx);
9964
}
10065

10166
/* Interface routines for SHA-384 */
10267
void
10368
pg_sha384_init(pg_sha384_ctx *ctx)
10469
{
105-
digest_init(ctx, EVP_sha384());
70+
SHA384_Init((SHA512_CTX *) ctx);
10671
}
10772

10873
void
10974
pg_sha384_update(pg_sha384_ctx *ctx, const uint8 *data, size_t len)
11075
{
111-
digest_update(ctx, data, len);
76+
SHA384_Update((SHA512_CTX *) ctx, data, len);
11277
}
11378

11479
void
11580
pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest)
11681
{
117-
digest_final(ctx, dest);
82+
SHA384_Final(dest, (SHA512_CTX *) ctx);
11883
}
11984

12085
/* Interface routines for SHA-224 */
12186
void
12287
pg_sha224_init(pg_sha224_ctx *ctx)
12388
{
124-
digest_init(ctx, EVP_sha224());
89+
SHA224_Init((SHA256_CTX *) ctx);
12590
}
12691

12792
void
12893
pg_sha224_update(pg_sha224_ctx *ctx, const uint8 *data, size_t len)
12994
{
130-
digest_update(ctx, data, len);
95+
SHA224_Update((SHA256_CTX *) ctx, data, len);
13196
}
13297

13398
void
13499
pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest)
135100
{
136-
digest_final(ctx, dest);
101+
SHA224_Final(dest, (SHA256_CTX *) ctx);
137102
}

src/include/common/sha2.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#define _PG_SHA2_H_
5252

5353
#ifdef USE_OPENSSL
54-
#include <openssl/evp.h>
54+
#include <openssl/sha.h>
5555
#endif
5656

5757
/*** SHA224/256/384/512 Various Length Definitions ***********************/
@@ -70,10 +70,10 @@
7070

7171
/* Context Structures for SHA224/256/384/512 */
7272
#ifdef USE_OPENSSL
73-
typedef EVP_MD_CTX *pg_sha256_ctx;
74-
typedef EVP_MD_CTX *pg_sha512_ctx;
75-
typedef EVP_MD_CTX *pg_sha224_ctx;
76-
typedef EVP_MD_CTX *pg_sha384_ctx;
73+
typedef SHA256_CTX pg_sha256_ctx;
74+
typedef SHA512_CTX pg_sha512_ctx;
75+
typedef SHA256_CTX pg_sha224_ctx;
76+
typedef SHA512_CTX pg_sha384_ctx;
7777
#else
7878
typedef struct pg_sha256_ctx
7979
{

0 commit comments

Comments
 (0)