Age Owner Branch data TLA Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * checksum_helper.c
4 : : * Compute a checksum of any of various types using common routines
5 : : *
6 : : * Portions Copyright (c) 2016-2026, PostgreSQL Global Development Group
7 : : *
8 : : * IDENTIFICATION
9 : : * src/common/checksum_helper.c
10 : : *
11 : : *-------------------------------------------------------------------------
12 : : */
13 : :
14 : : #ifndef FRONTEND
15 : : #include "postgres.h"
16 : : #else
17 : : #include "postgres_fe.h"
18 : : #endif
19 : :
20 : : #include "common/checksum_helper.h"
21 : :
22 : : /*
23 : : * If 'name' is a recognized checksum type, set *type to the corresponding
24 : : * constant and return true. Otherwise, set *type to CHECKSUM_TYPE_NONE and
25 : : * return false.
26 : : */
27 : : bool
2361 rhaas@postgresql.org 28 :CBC 121326 : pg_checksum_parse_type(char *name, pg_checksum_type *type)
29 : : {
30 : 121326 : pg_checksum_type result_type = CHECKSUM_TYPE_NONE;
31 : 121326 : bool result = true;
32 : :
33 [ + + ]: 121326 : if (pg_strcasecmp(name, "none") == 0)
34 : 3 : result_type = CHECKSUM_TYPE_NONE;
35 [ + + ]: 121323 : else if (pg_strcasecmp(name, "crc32c") == 0)
36 : 112586 : result_type = CHECKSUM_TYPE_CRC32C;
37 [ + + ]: 8737 : else if (pg_strcasecmp(name, "sha224") == 0)
38 : 2911 : result_type = CHECKSUM_TYPE_SHA224;
39 [ + + ]: 5826 : else if (pg_strcasecmp(name, "sha256") == 0)
40 : 1941 : result_type = CHECKSUM_TYPE_SHA256;
41 [ + + ]: 3885 : else if (pg_strcasecmp(name, "sha384") == 0)
42 : 1941 : result_type = CHECKSUM_TYPE_SHA384;
43 [ + + ]: 1944 : else if (pg_strcasecmp(name, "sha512") == 0)
44 : 1941 : result_type = CHECKSUM_TYPE_SHA512;
45 : : else
46 : 3 : result = false;
47 : :
48 : 121326 : *type = result_type;
49 : 121326 : return result;
50 : : }
51 : :
52 : : /*
53 : : * Get the canonical human-readable name corresponding to a checksum type.
54 : : */
55 : : char *
56 : 196268 : pg_checksum_type_name(pg_checksum_type type)
57 : : {
58 [ - + + + : 196268 : switch (type)
+ + - ]
59 : : {
2361 rhaas@postgresql.org 60 :UBC 0 : case CHECKSUM_TYPE_NONE:
61 : 0 : return "NONE";
2361 rhaas@postgresql.org 62 :CBC 187543 : case CHECKSUM_TYPE_CRC32C:
63 : 187543 : return "CRC32C";
64 : 2908 : case CHECKSUM_TYPE_SHA224:
65 : 2908 : return "SHA224";
66 : 1939 : case CHECKSUM_TYPE_SHA256:
67 : 1939 : return "SHA256";
68 : 1939 : case CHECKSUM_TYPE_SHA384:
69 : 1939 : return "SHA384";
70 : 1939 : case CHECKSUM_TYPE_SHA512:
71 : 1939 : return "SHA512";
72 : : }
73 : :
2361 rhaas@postgresql.org 74 :UBC 0 : Assert(false);
75 : : return "???";
76 : : }
77 : :
78 : : /*
79 : : * Initialize a checksum context for checksums of the given type.
80 : : * Returns 0 for a success, -1 for a failure.
81 : : */
82 : : int
2361 rhaas@postgresql.org 83 :CBC 268098 : pg_checksum_init(pg_checksum_context *context, pg_checksum_type type)
84 : : {
85 : 268098 : context->type = type;
86 : :
87 [ + + + + : 268098 : switch (type)
+ + - ]
88 : : {
89 : 16823 : case CHECKSUM_TYPE_NONE:
90 : : /* do nothing */
91 : 16823 : break;
92 : 233820 : case CHECKSUM_TYPE_CRC32C:
93 : 233820 : INIT_CRC32C(context->raw_context.c_crc32c);
94 : 233820 : break;
95 : 5813 : case CHECKSUM_TYPE_SHA224:
2081 michael@paquier.xyz 96 : 5813 : context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA224);
97 [ - + ]: 5813 : if (context->raw_context.c_sha2 == NULL)
2118 michael@paquier.xyz 98 :UBC 0 : return -1;
2081 michael@paquier.xyz 99 [ - + ]:CBC 5813 : if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
100 : : {
2081 michael@paquier.xyz 101 :UBC 0 : pg_cryptohash_free(context->raw_context.c_sha2);
2118 102 : 0 : return -1;
103 : : }
2361 rhaas@postgresql.org 104 :CBC 5813 : break;
105 : 3890 : case CHECKSUM_TYPE_SHA256:
2081 michael@paquier.xyz 106 : 3890 : context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA256);
107 [ - + ]: 3890 : if (context->raw_context.c_sha2 == NULL)
2118 michael@paquier.xyz 108 :UBC 0 : return -1;
2081 michael@paquier.xyz 109 [ - + ]:CBC 3890 : if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
110 : : {
2081 michael@paquier.xyz 111 :UBC 0 : pg_cryptohash_free(context->raw_context.c_sha2);
2118 112 : 0 : return -1;
113 : : }
2361 rhaas@postgresql.org 114 :CBC 3890 : break;
115 : 3876 : case CHECKSUM_TYPE_SHA384:
2081 michael@paquier.xyz 116 : 3876 : context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA384);
117 [ - + ]: 3876 : if (context->raw_context.c_sha2 == NULL)
2118 michael@paquier.xyz 118 :UBC 0 : return -1;
2081 michael@paquier.xyz 119 [ - + ]:CBC 3876 : if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
120 : : {
2081 michael@paquier.xyz 121 :UBC 0 : pg_cryptohash_free(context->raw_context.c_sha2);
2118 122 : 0 : return -1;
123 : : }
2361 rhaas@postgresql.org 124 :CBC 3876 : break;
125 : 3876 : case CHECKSUM_TYPE_SHA512:
2081 michael@paquier.xyz 126 : 3876 : context->raw_context.c_sha2 = pg_cryptohash_create(PG_SHA512);
127 [ - + ]: 3876 : if (context->raw_context.c_sha2 == NULL)
2118 michael@paquier.xyz 128 :UBC 0 : return -1;
2081 michael@paquier.xyz 129 [ - + ]:CBC 3876 : if (pg_cryptohash_init(context->raw_context.c_sha2) < 0)
130 : : {
2081 michael@paquier.xyz 131 :UBC 0 : pg_cryptohash_free(context->raw_context.c_sha2);
2118 132 : 0 : return -1;
133 : : }
2361 rhaas@postgresql.org 134 :CBC 3876 : break;
135 : : }
136 : :
2118 michael@paquier.xyz 137 : 268098 : return 0;
138 : : }
139 : :
140 : : /*
141 : : * Update a checksum context with new data.
142 : : * Returns 0 for a success, -1 for a failure.
143 : : */
144 : : int
2361 rhaas@postgresql.org 145 : 296014 : pg_checksum_update(pg_checksum_context *context, const uint8 *input,
146 : : size_t len)
147 : : {
148 [ + + + - ]: 296014 : switch (context->type)
149 : : {
150 : 14942 : case CHECKSUM_TYPE_NONE:
151 : : /* do nothing */
152 : 14942 : break;
153 : 262604 : case CHECKSUM_TYPE_CRC32C:
154 : 262604 : COMP_CRC32C(context->raw_context.c_crc32c, input, len);
155 : 262604 : break;
156 : 18468 : case CHECKSUM_TYPE_SHA224:
157 : : case CHECKSUM_TYPE_SHA256:
158 : : case CHECKSUM_TYPE_SHA384:
159 : : case CHECKSUM_TYPE_SHA512:
2081 michael@paquier.xyz 160 [ - + ]: 18468 : if (pg_cryptohash_update(context->raw_context.c_sha2, input, len) < 0)
2118 michael@paquier.xyz 161 :UBC 0 : return -1;
2361 rhaas@postgresql.org 162 :CBC 18468 : break;
163 : : }
164 : :
2118 michael@paquier.xyz 165 : 296014 : return 0;
166 : : }
167 : :
168 : : /*
169 : : * Finalize a checksum computation and write the result to an output buffer.
170 : : *
171 : : * The caller must ensure that the buffer is at least PG_CHECKSUM_MAX_LENGTH
172 : : * bytes in length. The return value is the number of bytes actually written,
173 : : * or -1 for a failure.
174 : : */
175 : : int
2361 rhaas@postgresql.org 176 : 251274 : pg_checksum_final(pg_checksum_context *context, uint8 *output)
177 : : {
178 : 251274 : int retval = 0;
179 : :
180 : : StaticAssertDecl(sizeof(pg_crc32c) <= PG_CHECKSUM_MAX_LENGTH,
181 : : "CRC-32C digest too big for PG_CHECKSUM_MAX_LENGTH");
182 : : StaticAssertDecl(PG_SHA224_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH,
183 : : "SHA224 digest too big for PG_CHECKSUM_MAX_LENGTH");
184 : : StaticAssertDecl(PG_SHA256_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH,
185 : : "SHA256 digest too big for PG_CHECKSUM_MAX_LENGTH");
186 : : StaticAssertDecl(PG_SHA384_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH,
187 : : "SHA384 digest too big for PG_CHECKSUM_MAX_LENGTH");
188 : : StaticAssertDecl(PG_SHA512_DIGEST_LENGTH <= PG_CHECKSUM_MAX_LENGTH,
189 : : "SHA512 digest too big for PG_CHECKSUM_MAX_LENGTH");
190 : :
191 [ + + + + : 251274 : switch (context->type)
+ + - ]
192 : : {
193 : 2 : case CHECKSUM_TYPE_NONE:
194 : 2 : break;
195 : 233818 : case CHECKSUM_TYPE_CRC32C:
196 : 233818 : FIN_CRC32C(context->raw_context.c_crc32c);
197 : 233818 : retval = sizeof(pg_crc32c);
198 : 233818 : memcpy(output, &context->raw_context.c_crc32c, retval);
199 : 233818 : break;
200 : 5813 : case CHECKSUM_TYPE_SHA224:
2043 michael@paquier.xyz 201 : 5813 : retval = PG_SHA224_DIGEST_LENGTH;
202 [ - + ]: 5813 : if (pg_cryptohash_final(context->raw_context.c_sha2,
203 : : output, retval) < 0)
2118 michael@paquier.xyz 204 :UBC 0 : return -1;
2081 michael@paquier.xyz 205 :CBC 5813 : pg_cryptohash_free(context->raw_context.c_sha2);
2361 rhaas@postgresql.org 206 : 5813 : break;
207 : 3889 : case CHECKSUM_TYPE_SHA256:
2043 michael@paquier.xyz 208 : 3889 : retval = PG_SHA256_DIGEST_LENGTH;
209 [ - + ]: 3889 : if (pg_cryptohash_final(context->raw_context.c_sha2,
210 : : output, retval) < 0)
2118 michael@paquier.xyz 211 :UBC 0 : return -1;
2081 michael@paquier.xyz 212 :CBC 3889 : pg_cryptohash_free(context->raw_context.c_sha2);
2361 rhaas@postgresql.org 213 : 3889 : break;
214 : 3876 : case CHECKSUM_TYPE_SHA384:
2043 michael@paquier.xyz 215 : 3876 : retval = PG_SHA384_DIGEST_LENGTH;
216 [ - + ]: 3876 : if (pg_cryptohash_final(context->raw_context.c_sha2,
217 : : output, retval) < 0)
2118 michael@paquier.xyz 218 :UBC 0 : return -1;
2081 michael@paquier.xyz 219 :CBC 3876 : pg_cryptohash_free(context->raw_context.c_sha2);
2361 rhaas@postgresql.org 220 : 3876 : break;
221 : 3876 : case CHECKSUM_TYPE_SHA512:
2043 michael@paquier.xyz 222 : 3876 : retval = PG_SHA512_DIGEST_LENGTH;
223 [ - + ]: 3876 : if (pg_cryptohash_final(context->raw_context.c_sha2,
224 : : output, retval) < 0)
2118 michael@paquier.xyz 225 :UBC 0 : return -1;
2081 michael@paquier.xyz 226 :CBC 3876 : pg_cryptohash_free(context->raw_context.c_sha2);
2361 rhaas@postgresql.org 227 : 3876 : break;
228 : : }
229 : :
230 [ - + ]: 251274 : Assert(retval <= PG_CHECKSUM_MAX_LENGTH);
231 : 251274 : return retval;
232 : : }
|