Debian Codegraph @

servers/slapd/pwmods/argon2.c

raw
1 /* argon2.c - Password module for argon2 */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 *
5 * Copyright 2017-2024 The OpenLDAP Foundation.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted only as authorized by the OpenLDAP
10 * Public License.
11 *
12 * A copy of this license is available in the file LICENSE in the
13 * top-level directory of the distribution or, alternatively, at
14 * <http://www.OpenLDAP.org/license.html>.
15 */
16
17 #include "portable.h"
18 #ifdef SLAPD_PWMOD_PW_ARGON2
19 #include "ac/string.h"
20 #include "lber_pvt.h"
21 #include "lutil.h"
22
23 #include "slap.h"
24
25 #include <stdint.h>
26 #include <stdlib.h>
27
28 #ifdef HAVE_LIBARGON2
29 #include <argon2.h>
30
31 /*
32 * Use OWASP recommended values (retrieved on 2023-08-07)
33 * @see https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html
34 */
35 #define SLAPD_ARGON2_ITERATIONS 5
36 #define SLAPD_ARGON2_MEMORY 7168
37 #define SLAPD_ARGON2_PARALLELISM 1
38 #define SLAPD_ARGON2_SALT_LENGTH 16
39 #define SLAPD_ARGON2_HASH_LENGTH 32
40
41 #else /* !HAVE_LIBARGON2 */
42 #include <sodium.h>
43
44 /*
45 * Or libsodium interactive settings
46 */
47 #define SLAPD_ARGON2_ITERATIONS crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE
48 #define SLAPD_ARGON2_MEMORY (crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE / 1024)
49 #define SLAPD_ARGON2_PARALLELISM 1
50 #define SLAPD_ARGON2_SALT_LENGTH crypto_pwhash_argon2id_SALTBYTES
51 #define SLAPD_ARGON2_HASH_LENGTH 32
52
53 #endif
54
55 static unsigned long iterations = SLAPD_ARGON2_ITERATIONS;
56 static unsigned long memory = SLAPD_ARGON2_MEMORY;
57 static unsigned long parallelism = SLAPD_ARGON2_PARALLELISM;
58
59 const struct berval slapd_argon2_scheme = BER_BVC("{ARGON2}");
60
61 static int
62 slapd_argon2_hash(
63 const struct berval *scheme,
64 const struct berval *passwd,
65 struct berval *hash,
66 const char **text )
67 {
68
69 /*
70 * Duplicate these values here so future code which allows
71 * configuration has an easier time.
72 */
73 uint32_t salt_length, hash_length;
74 char *p;
75 int rc = LUTIL_PASSWD_ERR;
76
77 #ifdef HAVE_LIBARGON2
78 struct berval salt;
79 size_t encoded_length;
80
81 salt_length = SLAPD_ARGON2_SALT_LENGTH;
82 hash_length = SLAPD_ARGON2_HASH_LENGTH;
83
84 encoded_length = argon2_encodedlen( iterations, memory, parallelism,
85 salt_length, hash_length, Argon2_id );
86
87 salt.bv_len = salt_length;
88 salt.bv_val = ber_memalloc( salt.bv_len );
89
90 if ( salt.bv_val == NULL ) {
91 return LUTIL_PASSWD_ERR;
92 }
93
94 if ( lutil_entropy( (unsigned char*)salt.bv_val, salt.bv_len ) ) {
95 ber_memfree( salt.bv_val );
96 return LUTIL_PASSWD_ERR;
97 }
98
99 p = hash->bv_val = ber_memalloc( scheme->bv_len + encoded_length );
100 if ( p == NULL ) {
101 ber_memfree( salt.bv_val );
102 return LUTIL_PASSWD_ERR;
103 }
104
105 AC_MEMCPY( p, scheme->bv_val, scheme->bv_len );
106 p += scheme->bv_len;
107
108 /*
109 * Do the actual heavy lifting
110 */
111 if ( argon2id_hash_encoded( iterations, memory, parallelism,
112 passwd->bv_val, passwd->bv_len,
113 salt.bv_val, salt_length, hash_length,
114 p, encoded_length ) == 0 ) {
115 rc = LUTIL_PASSWD_OK;
116 }
117 hash->bv_len = scheme->bv_len + encoded_length;
118 ber_memfree( salt.bv_val );
119
120 #else /* !HAVE_LIBARGON2 */
121 /* Not exposed by libsodium
122 salt_length = SLAPD_ARGON2_SALT_LENGTH;
123 hash_length = SLAPD_ARGON2_HASH_LENGTH;
124 */
125
126 p = hash->bv_val = ber_memalloc( scheme->bv_len + crypto_pwhash_STRBYTES );
127 if ( p == NULL ) {
128 return LUTIL_PASSWD_ERR;
129 }
130
131 AC_MEMCPY( hash->bv_val, scheme->bv_val, scheme->bv_len );
132 p += scheme->bv_len;
133
134 if ( crypto_pwhash_str_alg( p, passwd->bv_val, passwd->bv_len,
135 iterations, memory * 1024,
136 crypto_pwhash_ALG_ARGON2ID13 ) == 0 ) {
137 hash->bv_len = strlen( hash->bv_val );
138 rc = LUTIL_PASSWD_OK;
139 }
140 #endif
141
142 if ( rc ) {
143 ber_memfree( hash->bv_val );
144 return LUTIL_PASSWD_ERR;
145 }
146
147 return LUTIL_PASSWD_OK;
148 }
149
150 static int
151 slapd_argon2_verify(
152 const struct berval *scheme,
153 const struct berval *passwd,
154 const struct berval *cred,
155 const char **text )
156 {
157 int rc = LUTIL_PASSWD_ERR;
158
159 #ifdef HAVE_LIBARGON2
160 if ( strncmp( passwd->bv_val, "$argon2i$", STRLENOF("$argon2i$") ) == 0 ) {
161 rc = argon2i_verify( passwd->bv_val, cred->bv_val, cred->bv_len );
162 } else if ( strncmp( passwd->bv_val, "$argon2d$", STRLENOF("$argon2d$") ) == 0 ) {
163 rc = argon2d_verify( passwd->bv_val, cred->bv_val, cred->bv_len );
164 } else if ( strncmp( passwd->bv_val, "$argon2id$", STRLENOF("$argon2id$") ) == 0 ) {
165 rc = argon2id_verify( passwd->bv_val, cred->bv_val, cred->bv_len );
166 }
167 #else /* !HAVE_LIBARGON2 */
168 rc = crypto_pwhash_str_verify( passwd->bv_val, cred->bv_val, cred->bv_len );
169 #endif
170
171 if ( rc ) {
172 return LUTIL_PASSWD_ERR;
173 }
174 return LUTIL_PASSWD_OK;
175 }
176
177 int init_module( int argc, char *argv[] )
178 {
179 int i;
180
181 #ifdef HAVE_LIBSODIUM
182 if ( sodium_init() == -1 ) {
183 return -1;
184 }
185 #endif
186
187 for ( i=0; i < argc; i++ ) {
188 char *p;
189 unsigned long value;
190
191 switch ( *argv[i] ) {
192 case 'm':
193 p = strchr( argv[i], '=' );
194 if ( !p || lutil_atoulx( &value, p+1, 0 ) ) {
195 return -1;
196 }
197 memory = value;
198 break;
199
200 case 't':
201 p = strchr( argv[i], '=' );
202 if ( !p || lutil_atoulx( &value, p+1, 0 ) ) {
203 return -1;
204 }
205 iterations = value;
206 break;
207
208 case 'p':
209 p = strchr( argv[i], '=' );
210 if ( !p || lutil_atoulx( &value, p+1, 0 ) ) {
211 return -1;
212 }
213 parallelism = value;
214 break;
215
216 default:
217 return -1;
218 }
219 }
220
221 #ifndef HAVE_LIBARGON2
222 /* At the moment, we can only use libargon2 to set parallelism for new
223 * hashes */
224 if ( parallelism != SLAPD_ARGON2_PARALLELISM ) {
225 Debug( LDAP_DEBUG_ANY, "pw-argon2: "
226 "non-default parallelism only supported when linked with "
227 "libargon2, got p=%lu\n",
228 parallelism );
229
230 if ( (slapMode & SLAP_MODE) != SLAP_TOOL_MODE ||
231 slapTool == SLAPPASSWD || slapTool == SLAPTEST ) {
232 return 1;
233 }
234 }
235 #endif
236
237 return lutil_passwd_add( (struct berval *)&slapd_argon2_scheme,
238 slapd_argon2_verify, slapd_argon2_hash );
239 }
240 #endif /* SLAPD_OVER_PW_ARGON2 */