-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpg_argon2id.c
More file actions
281 lines (247 loc) · 7.53 KB
/
pg_argon2id.c
File metadata and controls
281 lines (247 loc) · 7.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* pg_argon2id.c
*
* PostgreSQL extension for Argon2id password hashing.
* Provides secure password hashing and verification using the Argon2id algorithm.
*
* Copyright (c) 2025 Polyák László
* Licensed under the MIT License
*/
#include "postgres.h"
#include "fmgr.h"
#include "varatt.h"
#include "utils/builtins.h"
#include "libpq/pqformat.h"
#include "mb/pg_wchar.h"
#include "port.h"
#include <argon2.h>
#include <string.h>
PG_MODULE_MAGIC;
/* Default parameters for Argon2id (moderate security) */
#define DEFAULT_TIME_COST 3 /* Number of iterations */
#define DEFAULT_MEMORY_COST 262144 /* Memory usage in KiB (256 MB) */
#define DEFAULT_PARALLELISM 4 /* Number of parallel threads */
#define DEFAULT_SALT_LENGTH 16 /* Salt length in bytes */
#define DEFAULT_HASH_LENGTH 32 /* Hash length in bytes */
/* Minimum and maximum values for security parameters */
#define MIN_TIME_COST 1
#define MAX_TIME_COST 10
#define MIN_MEMORY_COST 8192 /* 8 MB minimum */
#define MAX_MEMORY_COST 2097152 /* 2 GB maximum */
#define MIN_PARALLELISM 1
#define MAX_PARALLELISM 16
#define MIN_SALT_LENGTH 8
#define MAX_SALT_LENGTH 32
/*
* Function declarations
* Note: We use pg_ prefix to avoid naming conflicts with argon2 library functions
*/
PG_FUNCTION_INFO_V1(pg_argon2id_hash);
PG_FUNCTION_INFO_V1(pg_argon2id_verify);
/*
* pg_argon2id_hash
*
* Generate an Argon2id hash from a plaintext password.
*
* Parameters:
* password - The plaintext password to hash
* salt_length - Length of the random salt (default: 16 bytes)
* time_cost - Number of iterations (default: 3)
* memory_cost - Memory usage in KiB (default: 262144 = 256 MB)
* parallelism - Number of parallel threads (default: 4)
*
* Returns:
* A text string containing the encoded hash in Argon2 format
*/
Datum
pg_argon2id_hash(PG_FUNCTION_ARGS)
{
text *password_text;
char *password;
int password_len;
int salt_length = DEFAULT_SALT_LENGTH;
int time_cost = DEFAULT_TIME_COST;
int memory_cost = DEFAULT_MEMORY_COST;
int parallelism = DEFAULT_PARALLELISM;
uint8 *salt;
char *encoded;
size_t encoded_len;
int result;
text *result_text;
/* Get the password argument */
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("password cannot be NULL")));
password_text = PG_GETARG_TEXT_PP(0);
password_len = VARSIZE_ANY_EXHDR(password_text);
/* Allocate memory for null-terminated password string */
password = (char *) palloc(password_len + 1);
memcpy(password, VARDATA_ANY(password_text), password_len);
password[password_len] = '\0';
/* Get optional parameters if provided */
if (!PG_ARGISNULL(1))
{
salt_length = PG_GETARG_INT32(1);
if (salt_length < MIN_SALT_LENGTH || salt_length > MAX_SALT_LENGTH)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("salt_length must be between %d and %d",
MIN_SALT_LENGTH, MAX_SALT_LENGTH)));
}
if (!PG_ARGISNULL(2))
{
time_cost = PG_GETARG_INT32(2);
if (time_cost < MIN_TIME_COST || time_cost > MAX_TIME_COST)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("time_cost must be between %d and %d",
MIN_TIME_COST, MAX_TIME_COST)));
}
if (!PG_ARGISNULL(3))
{
memory_cost = PG_GETARG_INT32(3);
if (memory_cost < MIN_MEMORY_COST || memory_cost > MAX_MEMORY_COST)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("memory_cost must be between %d and %d KiB",
MIN_MEMORY_COST, MAX_MEMORY_COST)));
}
if (!PG_ARGISNULL(4))
{
parallelism = PG_GETARG_INT32(4);
if (parallelism < MIN_PARALLELISM || parallelism > MAX_PARALLELISM)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("parallelism must be between %d and %d",
MIN_PARALLELISM, MAX_PARALLELISM)));
}
/*
* Generate cryptographically secure random salt.
* We use PostgreSQL's pg_strong_random() which provides
* cryptographic-quality random bytes.
*/
salt = (uint8 *) palloc(salt_length);
if (!pg_strong_random(salt, salt_length))
{
pfree(salt);
memset(password, 0, password_len);
pfree(password);
ereport(ERROR,
(errcode(ERRCODE_INTERNAL_ERROR),
errmsg("failed to generate random salt")));
}
/*
* Calculate the maximum length of the encoded hash.
* The argon2_encodedlen function calculates the required buffer size.
*/
encoded_len = argon2_encodedlen(time_cost, memory_cost, parallelism,
salt_length, DEFAULT_HASH_LENGTH,
Argon2_id);
/* Allocate buffer for the encoded hash */
encoded = (char *) palloc(encoded_len);
/*
* Generate the hash using argon2id_hash_encoded.
* We provide the salt we generated using pg_strong_random().
*/
result = argon2id_hash_encoded(time_cost, memory_cost, parallelism,
password, password_len,
salt, salt_length,
DEFAULT_HASH_LENGTH,
encoded, encoded_len);
/* Clean up the salt (no longer needed) */
pfree(salt);
/* Clear the password from memory for security */
memset(password, 0, password_len);
pfree(password);
/* Check for errors during hash generation */
if (result != ARGON2_OK)
{
pfree(encoded);
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("argon2 hash generation failed: %s",
argon2_error_message(result))));
}
/* Convert C string to PostgreSQL text type */
result_text = cstring_to_text(encoded);
pfree(encoded);
PG_RETURN_TEXT_P(result_text);
}
/*
* pg_argon2id_verify
*
* Verify a plaintext password against an Argon2id hash.
*
* Parameters:
* password - The plaintext password to verify
* hash - The encoded Argon2id hash to check against
*
* Returns:
* true if the password matches the hash, false otherwise
*/
Datum
pg_argon2id_verify(PG_FUNCTION_ARGS)
{
text *password_text;
text *hash_text;
char *password;
char *hash;
int password_len;
int hash_len;
int result;
bool verified;
/* Get the password argument */
if (PG_ARGISNULL(0))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("password cannot be NULL")));
/* Get the hash argument */
if (PG_ARGISNULL(1))
ereport(ERROR,
(errcode(ERRCODE_NULL_VALUE_NOT_ALLOWED),
errmsg("hash cannot be NULL")));
password_text = PG_GETARG_TEXT_PP(0);
hash_text = PG_GETARG_TEXT_PP(1);
password_len = VARSIZE_ANY_EXHDR(password_text);
hash_len = VARSIZE_ANY_EXHDR(hash_text);
/* Allocate memory for null-terminated strings */
password = (char *) palloc(password_len + 1);
memcpy(password, VARDATA_ANY(password_text), password_len);
password[password_len] = '\0';
hash = (char *) palloc(hash_len + 1);
memcpy(hash, VARDATA_ANY(hash_text), hash_len);
hash[hash_len] = '\0';
/*
* Verify the password against the hash.
* The encoded hash contains all necessary parameters (time cost,
* memory cost, parallelism, salt), so we only need to provide
* the password and the encoded hash.
* Using argon2_verify which works with encoded hashes.
*/
result = argon2_verify(hash, password, password_len, Argon2_id);
/* Clear the password from memory for security */
memset(password, 0, password_len);
pfree(password);
pfree(hash);
/* Check the verification result */
if (result == ARGON2_OK)
{
/* Password matches */
verified = true;
}
else if (result == ARGON2_VERIFY_MISMATCH)
{
/* Password doesn't match */
verified = false;
}
else
{
/* Some other error occurred */
ereport(ERROR,
(errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION),
errmsg("argon2 verification failed: %s",
argon2_error_message(result))));
}
PG_RETURN_BOOL(verified);
}