|
20 | 20 | #include "postgres_fe.h"
|
21 | 21 | #endif
|
22 | 22 |
|
23 |
| -#include <openssl/sha.h> |
24 |
| - |
25 | 23 | #include "common/sha2.h"
|
26 | 24 |
|
| 25 | +#ifdef FRONTEND |
| 26 | +#include "common/logging.h" |
| 27 | +#else |
| 28 | +#include "miscadmin.h" |
| 29 | +#endif |
| 30 | + |
| 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 | +} |
| 54 | + |
| 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 | +} |
27 | 62 |
|
28 | 63 | /* Interface routines for SHA-256 */
|
29 | 64 | void
|
30 | 65 | pg_sha256_init(pg_sha256_ctx *ctx)
|
31 | 66 | {
|
32 |
| - SHA256_Init((SHA256_CTX *) ctx); |
| 67 | + digest_init(ctx, EVP_sha256()); |
33 | 68 | }
|
34 | 69 |
|
35 | 70 | void
|
36 | 71 | pg_sha256_update(pg_sha256_ctx *ctx, const uint8 *data, size_t len)
|
37 | 72 | {
|
38 |
| - SHA256_Update((SHA256_CTX *) ctx, data, len); |
| 73 | + digest_update(ctx, data, len); |
39 | 74 | }
|
40 | 75 |
|
41 | 76 | void
|
42 | 77 | pg_sha256_final(pg_sha256_ctx *ctx, uint8 *dest)
|
43 | 78 | {
|
44 |
| - SHA256_Final(dest, (SHA256_CTX *) ctx); |
| 79 | + digest_final(ctx, dest); |
45 | 80 | }
|
46 | 81 |
|
47 | 82 | /* Interface routines for SHA-512 */
|
48 | 83 | void
|
49 | 84 | pg_sha512_init(pg_sha512_ctx *ctx)
|
50 | 85 | {
|
51 |
| - SHA512_Init((SHA512_CTX *) ctx); |
| 86 | + digest_init(ctx, EVP_sha512()); |
52 | 87 | }
|
53 | 88 |
|
54 | 89 | void
|
55 | 90 | pg_sha512_update(pg_sha512_ctx *ctx, const uint8 *data, size_t len)
|
56 | 91 | {
|
57 |
| - SHA512_Update((SHA512_CTX *) ctx, data, len); |
| 92 | + digest_update(ctx, data, len); |
58 | 93 | }
|
59 | 94 |
|
60 | 95 | void
|
61 | 96 | pg_sha512_final(pg_sha512_ctx *ctx, uint8 *dest)
|
62 | 97 | {
|
63 |
| - SHA512_Final(dest, (SHA512_CTX *) ctx); |
| 98 | + digest_final(ctx, dest); |
64 | 99 | }
|
65 | 100 |
|
66 | 101 | /* Interface routines for SHA-384 */
|
67 | 102 | void
|
68 | 103 | pg_sha384_init(pg_sha384_ctx *ctx)
|
69 | 104 | {
|
70 |
| - SHA384_Init((SHA512_CTX *) ctx); |
| 105 | + digest_init(ctx, EVP_sha384()); |
71 | 106 | }
|
72 | 107 |
|
73 | 108 | void
|
74 | 109 | pg_sha384_update(pg_sha384_ctx *ctx, const uint8 *data, size_t len)
|
75 | 110 | {
|
76 |
| - SHA384_Update((SHA512_CTX *) ctx, data, len); |
| 111 | + digest_update(ctx, data, len); |
77 | 112 | }
|
78 | 113 |
|
79 | 114 | void
|
80 | 115 | pg_sha384_final(pg_sha384_ctx *ctx, uint8 *dest)
|
81 | 116 | {
|
82 |
| - SHA384_Final(dest, (SHA512_CTX *) ctx); |
| 117 | + digest_final(ctx, dest); |
83 | 118 | }
|
84 | 119 |
|
85 | 120 | /* Interface routines for SHA-224 */
|
86 | 121 | void
|
87 | 122 | pg_sha224_init(pg_sha224_ctx *ctx)
|
88 | 123 | {
|
89 |
| - SHA224_Init((SHA256_CTX *) ctx); |
| 124 | + digest_init(ctx, EVP_sha224()); |
90 | 125 | }
|
91 | 126 |
|
92 | 127 | void
|
93 | 128 | pg_sha224_update(pg_sha224_ctx *ctx, const uint8 *data, size_t len)
|
94 | 129 | {
|
95 |
| - SHA224_Update((SHA256_CTX *) ctx, data, len); |
| 130 | + digest_update(ctx, data, len); |
96 | 131 | }
|
97 | 132 |
|
98 | 133 | void
|
99 | 134 | pg_sha224_final(pg_sha224_ctx *ctx, uint8 *dest)
|
100 | 135 | {
|
101 |
| - SHA224_Final(dest, (SHA256_CTX *) ctx); |
| 136 | + digest_final(ctx, dest); |
102 | 137 | }
|
0 commit comments