crypt-argon2.c revision 1.9 1 1.9 jhigh /*
2 1.9 jhigh * Copyright (c) 2009 The NetBSD Foundation, Inc.
3 1.9 jhigh * All rights reserved.
4 1.9 jhigh *
5 1.9 jhigh * Redistribution and use in source and binary forms, with or without
6 1.9 jhigh * modification, are permitted provided that the following conditions
7 1.9 jhigh * are met:
8 1.9 jhigh * 1. Redistributions of source code must retain the above copyright
9 1.9 jhigh * notice, this list of conditions and the following disclaimer.
10 1.9 jhigh * 2. Redistributions in binary form must reproduce the above copyright
11 1.9 jhigh * notice, this list of conditions and the following disclaimer in the
12 1.9 jhigh * documentation and/or other materials provided with the distribution.
13 1.9 jhigh *
14 1.9 jhigh * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15 1.9 jhigh * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16 1.9 jhigh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 1.9 jhigh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18 1.9 jhigh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 1.9 jhigh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 1.9 jhigh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 1.9 jhigh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 1.9 jhigh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 1.9 jhigh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 1.9 jhigh * POSSIBILITY OF SUCH DAMAGE.
25 1.9 jhigh */
26 1.9 jhigh
27 1.1 jhigh #include <stdlib.h>
28 1.1 jhigh #include <stdio.h>
29 1.1 jhigh #include <unistd.h>
30 1.1 jhigh #include <stdio.h>
31 1.1 jhigh #include <string.h>
32 1.1 jhigh #include <time.h>
33 1.1 jhigh #include <pwd.h>
34 1.1 jhigh #include <errno.h>
35 1.1 jhigh #include <argon2.h>
36 1.1 jhigh
37 1.1 jhigh #include <err.h>
38 1.1 jhigh #include "crypt.h"
39 1.1 jhigh
40 1.1 jhigh /* defaults pulled from run.c */
41 1.1 jhigh #define HASHLEN 32
42 1.1 jhigh #define T_COST_DEF 3
43 1.1 jhigh #define LOG_M_COST_DEF 12 /* 2^12 = 4 MiB */
44 1.1 jhigh #define LANES_DEF 1
45 1.1 jhigh #define THREADS_DEF 1
46 1.1 jhigh #define OUTLEN_DEF 32
47 1.1 jhigh #define MAX_PASS_LEN 128
48 1.1 jhigh
49 1.1 jhigh #define ARGON2_CONTEXT_INITIALIZER \
50 1.1 jhigh {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
51 1.1 jhigh T_COST_DEF, LOG_M_COST_DEF,\
52 1.1 jhigh LANES_DEF, THREADS_DEF, \
53 1.1 jhigh ARGON2_VERSION_NUMBER, 0, 0, ARGON2_DEFAULT_FLAGS}
54 1.1 jhigh
55 1.1 jhigh #define ARGON2_ARGON2_STR "argon2"
56 1.1 jhigh #define ARGON2_ARGON2I_STR "argon2i"
57 1.1 jhigh #define ARGON2_ARGON2D_STR "argon2d"
58 1.1 jhigh #define ARGON2_ARGON2ID_STR "argon2id"
59 1.1 jhigh
60 1.7 nia
61 1.7 nia /*
62 1.7 nia * Some macros for constant-time comparisons. These work over values in
63 1.7 nia * the 0..255 range. Returned value is 0x00 on "false", 0xFF on "true".
64 1.7 nia */
65 1.7 nia #define EQ(x, y) ((((0U - ((unsigned)(x) ^ (unsigned)(y))) >> 8) & 0xFF) ^ 0xFF)
66 1.7 nia #define GT(x, y) ((((unsigned)(y) - (unsigned)(x)) >> 8) & 0xFF)
67 1.7 nia #define GE(x, y) (GT(y, x) ^ 0xFF)
68 1.7 nia #define LT(x, y) GT(y, x)
69 1.7 nia #define LE(x, y) GE(y, x)
70 1.7 nia
71 1.7 nia static unsigned
72 1.7 nia b64_char_to_byte(int c)
73 1.7 nia {
74 1.7 nia unsigned x;
75 1.7 nia
76 1.7 nia x = (GE(c, 'A') & LE(c, 'Z') & (c - 'A')) |
77 1.7 nia (GE(c, 'a') & LE(c, 'z') & (c - ('a' - 26))) |
78 1.7 nia (GE(c, '0') & LE(c, '9') & (c - ('0' - 52))) | (EQ(c, '+') & 62) |
79 1.7 nia (EQ(c, '/') & 63);
80 1.7 nia return x | (EQ(x, 0) & (EQ(c, 'A') ^ 0xFF));
81 1.7 nia }
82 1.7 nia
83 1.7 nia static const char *
84 1.7 nia from_base64(void *dst, size_t *dst_len, const char *src)
85 1.7 nia {
86 1.7 nia size_t len;
87 1.7 nia unsigned char *buf;
88 1.7 nia unsigned acc, acc_len;
89 1.7 nia
90 1.7 nia buf = (unsigned char *)dst;
91 1.7 nia len = 0;
92 1.7 nia acc = 0;
93 1.7 nia acc_len = 0;
94 1.7 nia for (;;) {
95 1.7 nia unsigned d;
96 1.7 nia
97 1.7 nia d = b64_char_to_byte(*src);
98 1.7 nia if (d == 0xFF) {
99 1.7 nia break;
100 1.7 nia }
101 1.7 nia src++;
102 1.7 nia acc = (acc << 6) + d;
103 1.7 nia acc_len += 6;
104 1.7 nia if (acc_len >= 8) {
105 1.7 nia acc_len -= 8;
106 1.7 nia if ((len++) >= *dst_len) {
107 1.7 nia return NULL;
108 1.7 nia }
109 1.7 nia *buf++ = (acc >> acc_len) & 0xFF;
110 1.7 nia }
111 1.7 nia }
112 1.7 nia
113 1.7 nia /*
114 1.7 nia * If the input length is equal to 1 modulo 4 (which is
115 1.7 nia * invalid), then there will remain 6 unprocessed bits;
116 1.7 nia * otherwise, only 0, 2 or 4 bits are buffered. The buffered
117 1.7 nia * bits must also all be zero.
118 1.7 nia */
119 1.7 nia if (acc_len > 4 || (acc & (((unsigned)1 << acc_len) - 1)) != 0) {
120 1.7 nia return NULL;
121 1.7 nia }
122 1.7 nia *dst_len = len;
123 1.7 nia return src;
124 1.7 nia }
125 1.7 nia
126 1.1 jhigh /* process params to argon2 */
127 1.1 jhigh /* we don't force param order as input, */
128 1.1 jhigh /* but we do provide the expected order to argon2 api */
129 1.7 nia static int
130 1.7 nia decode_option(argon2_context *ctx, argon2_type *atype, const char *option)
131 1.1 jhigh {
132 1.7 nia size_t tmp = 0;
133 1.7 nia char *in = 0, *inp;
134 1.7 nia char *a = 0;
135 1.7 nia char *p = 0;
136 1.1 jhigh size_t sl;
137 1.7 nia int error = 0;
138 1.1 jhigh
139 1.1 jhigh in = (char *)strdup(option);
140 1.1 jhigh inp = in;
141 1.1 jhigh
142 1.1 jhigh if (*inp == '$') inp++;
143 1.1 jhigh
144 1.1 jhigh a = strsep(&inp, "$");
145 1.1 jhigh
146 1.1 jhigh sl = strlen(a);
147 1.1 jhigh
148 1.1 jhigh if (sl == strlen(ARGON2_ARGON2I_STR) &&
149 1.1 jhigh !(strcmp(ARGON2_ARGON2I_STR, a))) {
150 1.1 jhigh *atype=Argon2_i;
151 1.1 jhigh } else if (sl == strlen(ARGON2_ARGON2D_STR) &&
152 1.1 jhigh !(strcmp(ARGON2_ARGON2D_STR, a))) {
153 1.1 jhigh *atype=Argon2_d;
154 1.1 jhigh }
155 1.1 jhigh else if (sl == strlen(ARGON2_ARGON2ID_STR) &&
156 1.1 jhigh !(strcmp(ARGON2_ARGON2ID_STR, a))) {
157 1.1 jhigh *atype=Argon2_id;
158 1.1 jhigh } else { /* default to id, we assume simple mistake */
159 1.1 jhigh /* don't abandon yet */
160 1.1 jhigh *atype=Argon2_id;
161 1.1 jhigh }
162 1.1 jhigh
163 1.1 jhigh a = strsep(&inp, "$");
164 1.1 jhigh
165 1.3 nia /* parse the version number of the hash, if it's there */
166 1.3 nia if (strncmp(a, "v=", 2) == 0) {
167 1.3 nia a += 2;
168 1.3 nia if ((getnum(a, &tmp))<0) { /* on error, default to current */
169 1.3 nia /* should start thinking about aborting */
170 1.4 nia ctx->version = ARGON2_VERSION_10;
171 1.3 nia } else {
172 1.3 nia ctx->version = tmp;
173 1.3 nia }
174 1.3 nia a = strsep(&inp, "$");
175 1.3 nia } else {
176 1.3 nia /*
177 1.3 nia * This is a parameter list, not a version number, use the
178 1.3 nia * default version.
179 1.3 nia */
180 1.4 nia ctx->version = ARGON2_VERSION_10;
181 1.1 jhigh }
182 1.1 jhigh
183 1.1 jhigh /* parse labelled argon2 params */
184 1.1 jhigh /* m_cost (m)
185 1.1 jhigh * t_cost (t)
186 1.1 jhigh * threads (p)
187 1.1 jhigh */
188 1.1 jhigh while ((p = strsep(&a, ","))) {
189 1.1 jhigh switch (*p) {
190 1.1 jhigh case 'm':
191 1.1 jhigh p += strlen("m=");
192 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
193 1.1 jhigh --error;
194 1.1 jhigh } else {
195 1.1 jhigh ctx->m_cost = tmp;
196 1.1 jhigh }
197 1.1 jhigh break;
198 1.1 jhigh case 't':
199 1.1 jhigh p += strlen("t=");
200 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
201 1.1 jhigh --error;
202 1.1 jhigh } else {
203 1.1 jhigh ctx->t_cost = tmp;
204 1.1 jhigh }
205 1.1 jhigh break;
206 1.1 jhigh case 'p':
207 1.1 jhigh p += strlen("p=");
208 1.1 jhigh if ((getnum(p, &tmp)) < 0) {
209 1.1 jhigh --error;
210 1.1 jhigh } else {
211 1.1 jhigh ctx->threads = tmp;
212 1.1 jhigh }
213 1.1 jhigh break;
214 1.1 jhigh default:
215 1.1 jhigh return -1;
216 1.1 jhigh
217 1.1 jhigh }
218 1.1 jhigh }
219 1.1 jhigh
220 1.1 jhigh a = strsep(&inp, "$");
221 1.1 jhigh
222 1.7 nia sl = ctx->saltlen;
223 1.7 nia
224 1.7 nia if (from_base64(ctx->salt, &sl, a) == NULL)
225 1.7 nia return -1;
226 1.7 nia
227 1.7 nia ctx->saltlen = sl;
228 1.1 jhigh
229 1.1 jhigh a = strsep(&inp, "$");
230 1.1 jhigh
231 1.3 nia if (a) {
232 1.3 nia snprintf((char *)ctx->pwd, ctx->pwdlen, "%s", a);
233 1.1 jhigh } else {
234 1.1 jhigh /* don't care if passwd hash is missing */
235 1.1 jhigh /* if missing, most likely coming from */
236 1.1 jhigh /* pwhash or similar */
237 1.1 jhigh }
238 1.1 jhigh
239 1.1 jhigh /* free our token buffer */
240 1.1 jhigh free(in);
241 1.1 jhigh
242 1.1 jhigh /* 0 on success, <0 otherwise */
243 1.1 jhigh return error;
244 1.1 jhigh }
245 1.1 jhigh
246 1.1 jhigh char *
247 1.1 jhigh __crypt_argon2(const char *pw, const char * salt)
248 1.1 jhigh {
249 1.1 jhigh /* we use the libargon2 api to generate */
250 1.1 jhigh /* return code */
251 1.7 nia int rc = 0;
252 1.1 jhigh /* output buffer */
253 1.1 jhigh char ebuf[32];
254 1.1 jhigh /* argon2 variable, default to id */
255 1.1 jhigh argon2_type atype = Argon2_id;
256 1.1 jhigh /* default to current argon2 version */
257 1.1 jhigh /* argon2 context to collect params */
258 1.1 jhigh argon2_context ctx = ARGON2_CONTEXT_INITIALIZER;
259 1.1 jhigh /* argon2 encoded buffer */
260 1.1 jhigh char encodebuf[256];
261 1.1 jhigh /* argon2 salt buffer */
262 1.1 jhigh char saltbuf[128];
263 1.1 jhigh /* argon2 pwd buffer */
264 1.1 jhigh char pwdbuf[128];
265 1.1 jhigh /* returned static buffer */
266 1.1 jhigh static char rbuf[512];
267 1.1 jhigh
268 1.1 jhigh /* clear buffers */
269 1.6 nia explicit_memset(rbuf, 0, sizeof(rbuf));
270 1.1 jhigh
271 1.1 jhigh /* we use static buffers to avoid allocation */
272 1.1 jhigh /* and easier cleanup */
273 1.1 jhigh ctx.out = (uint8_t *)ebuf;
274 1.1 jhigh ctx.outlen = sizeof(ebuf);
275 1.1 jhigh
276 1.1 jhigh ctx.out = (uint8_t *)encodebuf;
277 1.1 jhigh ctx.outlen = sizeof(encodebuf);
278 1.1 jhigh
279 1.1 jhigh ctx.salt = (uint8_t *)saltbuf;
280 1.1 jhigh ctx.saltlen = sizeof(saltbuf);
281 1.1 jhigh
282 1.7 nia ctx.pwd = (uint8_t *)pwdbuf;
283 1.1 jhigh ctx.pwdlen = sizeof(pwdbuf);
284 1.1 jhigh
285 1.1 jhigh /* decode salt string to argon2 params */
286 1.1 jhigh /* argon2 context for param collection */
287 1.1 jhigh rc = decode_option(&ctx, &atype, salt);
288 1.1 jhigh
289 1.1 jhigh if (rc < 0) {
290 1.3 nia /* unable to parse input params */
291 1.1 jhigh return 0;
292 1.1 jhigh }
293 1.1 jhigh
294 1.1 jhigh rc = argon2_hash(ctx.t_cost, ctx.m_cost,
295 1.7 nia ctx.threads, pw, strlen(pw), ctx.salt, ctx.saltlen,
296 1.7 nia ebuf, sizeof(ebuf), encodebuf, sizeof(encodebuf),
297 1.7 nia atype, ctx.version);
298 1.1 jhigh
299 1.1 jhigh if (rc != ARGON2_OK) {
300 1.3 nia fprintf(stderr, "argon2: failed: %s\n",
301 1.3 nia argon2_error_message(rc));
302 1.1 jhigh return 0;
303 1.1 jhigh }
304 1.1 jhigh
305 1.6 nia memcpy(rbuf, encodebuf, sizeof(encodebuf));
306 1.1 jhigh
307 1.1 jhigh /* clear buffers */
308 1.6 nia explicit_memset(ebuf, 0, sizeof(ebuf));
309 1.5 nia explicit_memset(encodebuf, 0, sizeof(encodebuf));
310 1.5 nia explicit_memset(saltbuf, 0, sizeof(saltbuf));
311 1.5 nia explicit_memset(pwdbuf, 0, sizeof(pwdbuf));
312 1.1 jhigh
313 1.1 jhigh /* return encoded str */
314 1.1 jhigh return rbuf;
315 1.1 jhigh }
316