1*84d9c625SLionel Sambuc /* $NetBSD: crypt-sha1.c,v 1.8 2013/08/28 17:47:07 riastradh Exp $ */
2ebffaa42SBen Gras
3ebffaa42SBen Gras /*
4ebffaa42SBen Gras * Copyright (c) 2004, Juniper Networks, Inc.
5ebffaa42SBen Gras * All rights reserved.
6ebffaa42SBen Gras *
7ebffaa42SBen Gras * Redistribution and use in source and binary forms, with or without
8ebffaa42SBen Gras * modification, are permitted provided that the following conditions
9ebffaa42SBen Gras * are met:
10ebffaa42SBen Gras * 1. Redistributions of source code must retain the above copyright
11ebffaa42SBen Gras * notice, this list of conditions and the following disclaimer.
12ebffaa42SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
13ebffaa42SBen Gras * notice, this list of conditions and the following disclaimer in the
14ebffaa42SBen Gras * documentation and/or other materials provided with the distribution.
15ebffaa42SBen Gras * 3. Neither the name of the copyright holders nor the names of its
16ebffaa42SBen Gras * contributors may be used to endorse or promote products derived
17ebffaa42SBen Gras * from this software without specific prior written permission.
18ebffaa42SBen Gras *
19ebffaa42SBen Gras * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20ebffaa42SBen Gras * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21ebffaa42SBen Gras * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22ebffaa42SBen Gras * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23ebffaa42SBen Gras * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24ebffaa42SBen Gras * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25ebffaa42SBen Gras * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26ebffaa42SBen Gras * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27ebffaa42SBen Gras * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28ebffaa42SBen Gras * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29ebffaa42SBen Gras * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30ebffaa42SBen Gras */
31ebffaa42SBen Gras
32ebffaa42SBen Gras #include <sys/cdefs.h>
33ebffaa42SBen Gras #if !defined(lint)
34*84d9c625SLionel Sambuc __RCSID("$NetBSD: crypt-sha1.c,v 1.8 2013/08/28 17:47:07 riastradh Exp $");
35ebffaa42SBen Gras #endif /* not lint */
36ebffaa42SBen Gras
37ebffaa42SBen Gras #include <stdlib.h>
38ebffaa42SBen Gras #include <unistd.h>
39ebffaa42SBen Gras #include <stdio.h>
40ebffaa42SBen Gras #include <string.h>
41ebffaa42SBen Gras #include <time.h>
42ebffaa42SBen Gras
43ebffaa42SBen Gras #include <err.h>
44ebffaa42SBen Gras #include "crypt.h"
45ebffaa42SBen Gras
46ebffaa42SBen Gras /*
47ebffaa42SBen Gras * The default iterations - should take >0s on a fast CPU
48ebffaa42SBen Gras * but not be insane for a slow CPU.
49ebffaa42SBen Gras */
50ebffaa42SBen Gras #ifndef CRYPT_SHA1_ITERATIONS
51ebffaa42SBen Gras # define CRYPT_SHA1_ITERATIONS 24680
52ebffaa42SBen Gras #endif
53ebffaa42SBen Gras /*
54ebffaa42SBen Gras * Support a reasonably? long salt.
55ebffaa42SBen Gras */
56ebffaa42SBen Gras #ifndef CRYPT_SHA1_SALT_LENGTH
57ebffaa42SBen Gras # define CRYPT_SHA1_SALT_LENGTH 64
58ebffaa42SBen Gras #endif
59ebffaa42SBen Gras
60ebffaa42SBen Gras /*
61ebffaa42SBen Gras * This may be called from crypt_sha1 or gensalt.
62ebffaa42SBen Gras *
63ebffaa42SBen Gras * The value returned will be slightly less than <hint> which defaults
64ebffaa42SBen Gras * to 24680. The goals are that the number of iterations should take
65ebffaa42SBen Gras * non-zero amount of time on a fast cpu while not taking insanely
66ebffaa42SBen Gras * long on a slow cpu. The current default will take about 5 seconds
67ebffaa42SBen Gras * on a 100MHz sparc, and about 0.04 seconds on a 3GHz i386.
68ebffaa42SBen Gras * The number is varied to frustrate those attempting to generate a
69ebffaa42SBen Gras * dictionary of pre-computed hashes.
70ebffaa42SBen Gras */
71ebffaa42SBen Gras unsigned int
__crypt_sha1_iterations(unsigned int hint)72ebffaa42SBen Gras __crypt_sha1_iterations (unsigned int hint)
73ebffaa42SBen Gras {
74ebffaa42SBen Gras static int once = 1;
75ebffaa42SBen Gras
76ebffaa42SBen Gras /*
77ebffaa42SBen Gras * We treat CRYPT_SHA1_ITERATIONS as a hint.
78ebffaa42SBen Gras * Make it harder for someone to pre-compute hashes for a
79ebffaa42SBen Gras * dictionary attack by not using the same iteration count for
80ebffaa42SBen Gras * every entry.
81ebffaa42SBen Gras */
82ebffaa42SBen Gras
83ebffaa42SBen Gras if (once) {
84ebffaa42SBen Gras int pid = getpid();
85ebffaa42SBen Gras
86ebffaa42SBen Gras srandom(time(NULL) ^ (pid * pid));
87ebffaa42SBen Gras once = 0;
88ebffaa42SBen Gras }
89ebffaa42SBen Gras if (hint == 0)
90ebffaa42SBen Gras hint = CRYPT_SHA1_ITERATIONS;
91ebffaa42SBen Gras return hint - (random() % (hint / 4));
92ebffaa42SBen Gras }
93ebffaa42SBen Gras
94ebffaa42SBen Gras /*
95ebffaa42SBen Gras * UNIX password using hmac_sha1
96ebffaa42SBen Gras * This is PBKDF1 from RFC 2898, but using hmac_sha1.
97ebffaa42SBen Gras *
98ebffaa42SBen Gras * The format of the encrypted password is:
99ebffaa42SBen Gras * $<tag>$<iterations>$<salt>$<digest>
100ebffaa42SBen Gras *
101ebffaa42SBen Gras * where:
102ebffaa42SBen Gras * <tag> is "sha1"
103ebffaa42SBen Gras * <iterations> is an unsigned int identifying how many rounds
104ebffaa42SBen Gras * have been applied to <digest>. The number
105ebffaa42SBen Gras * should vary slightly for each password to make
106ebffaa42SBen Gras * it harder to generate a dictionary of
107ebffaa42SBen Gras * pre-computed hashes. See crypt_sha1_iterations.
108ebffaa42SBen Gras * <salt> up to 64 bytes of random data, 8 bytes is
109ebffaa42SBen Gras * currently considered more than enough.
110ebffaa42SBen Gras * <digest> the hashed password.
111ebffaa42SBen Gras *
112ebffaa42SBen Gras * NOTE:
113ebffaa42SBen Gras * To be FIPS 140 compliant, the password which is used as a hmac key,
114ebffaa42SBen Gras * should be between 10 and 20 characters to provide at least 80bits
115ebffaa42SBen Gras * strength, and avoid the need to hash it before using as the
116ebffaa42SBen Gras * hmac key.
117ebffaa42SBen Gras */
118ebffaa42SBen Gras char *
__crypt_sha1(const char * pw,const char * salt)119ebffaa42SBen Gras __crypt_sha1 (const char *pw, const char *salt)
120ebffaa42SBen Gras {
121ebffaa42SBen Gras static const char *magic = SHA1_MAGIC;
122ebffaa42SBen Gras static unsigned char hmac_buf[SHA1_SIZE];
123ebffaa42SBen Gras static char passwd[(2 * sizeof(SHA1_MAGIC)) +
124ebffaa42SBen Gras CRYPT_SHA1_SALT_LENGTH + SHA1_SIZE];
125f5435c74SLionel Sambuc const char *sp;
126ebffaa42SBen Gras char *ep;
127ebffaa42SBen Gras unsigned long ul;
128ebffaa42SBen Gras int sl;
129ebffaa42SBen Gras int pl;
130ebffaa42SBen Gras int dl;
131ebffaa42SBen Gras unsigned int iterations;
132ebffaa42SBen Gras unsigned int i;
133*84d9c625SLionel Sambuc /* XXX silence -Wpointer-sign (would be nice to fix this some other way) */
134*84d9c625SLionel Sambuc const unsigned char *pwu = (const unsigned char *)pw;
135ebffaa42SBen Gras
136ebffaa42SBen Gras /*
137ebffaa42SBen Gras * Salt format is
138ebffaa42SBen Gras * $<tag>$<iterations>$salt[$]
139ebffaa42SBen Gras * If it does not start with $ we use our default iterations.
140ebffaa42SBen Gras */
141ebffaa42SBen Gras
142ebffaa42SBen Gras /* If it starts with the magic string, then skip that */
143f5435c74SLionel Sambuc if (!strncmp(salt, magic, strlen(magic))) {
144f5435c74SLionel Sambuc salt += strlen(magic);
145ebffaa42SBen Gras /* and get the iteration count */
146f5435c74SLionel Sambuc iterations = strtoul(salt, &ep, 10);
147ebffaa42SBen Gras if (*ep != '$')
148ebffaa42SBen Gras return NULL; /* invalid input */
149f5435c74SLionel Sambuc salt = ep + 1; /* skip over the '$' */
150ebffaa42SBen Gras } else {
151ebffaa42SBen Gras iterations = __crypt_sha1_iterations(0);
152ebffaa42SBen Gras }
153ebffaa42SBen Gras
154ebffaa42SBen Gras /* It stops at the next '$', max CRYPT_SHA1_ITERATIONS chars */
155f5435c74SLionel Sambuc for (sp = salt; *sp && *sp != '$' && sp < (salt + CRYPT_SHA1_ITERATIONS); sp++)
156ebffaa42SBen Gras continue;
157ebffaa42SBen Gras
158ebffaa42SBen Gras /* Get the length of the actual salt */
159f5435c74SLionel Sambuc sl = sp - salt;
160ebffaa42SBen Gras pl = strlen(pw);
161ebffaa42SBen Gras
162ebffaa42SBen Gras /*
163ebffaa42SBen Gras * Now get to work...
164ebffaa42SBen Gras * Prime the pump with <salt><magic><iterations>
165ebffaa42SBen Gras */
166ebffaa42SBen Gras dl = snprintf(passwd, sizeof (passwd), "%.*s%s%u",
167f5435c74SLionel Sambuc sl, salt, magic, iterations);
168ebffaa42SBen Gras /*
169ebffaa42SBen Gras * Then hmac using <pw> as key, and repeat...
170ebffaa42SBen Gras */
171*84d9c625SLionel Sambuc __hmac_sha1((unsigned char *)passwd, dl, pwu, pl, hmac_buf);
172ebffaa42SBen Gras for (i = 1; i < iterations; i++) {
173*84d9c625SLionel Sambuc __hmac_sha1(hmac_buf, SHA1_SIZE, pwu, pl, hmac_buf);
174ebffaa42SBen Gras }
175ebffaa42SBen Gras /* Now output... */
176ebffaa42SBen Gras pl = snprintf(passwd, sizeof(passwd), "%s%u$%.*s$",
177f5435c74SLionel Sambuc magic, iterations, sl, salt);
178ebffaa42SBen Gras ep = passwd + pl;
179ebffaa42SBen Gras
180ebffaa42SBen Gras /* Every 3 bytes of hash gives 24 bits which is 4 base64 chars */
181ebffaa42SBen Gras for (i = 0; i < SHA1_SIZE - 3; i += 3) {
182ebffaa42SBen Gras ul = (hmac_buf[i+0] << 16) |
183ebffaa42SBen Gras (hmac_buf[i+1] << 8) |
184ebffaa42SBen Gras hmac_buf[i+2];
185ebffaa42SBen Gras __crypt_to64(ep, ul, 4); ep += 4;
186ebffaa42SBen Gras }
187ebffaa42SBen Gras /* Only 2 bytes left, so we pad with byte0 */
188ebffaa42SBen Gras ul = (hmac_buf[SHA1_SIZE - 2] << 16) |
189ebffaa42SBen Gras (hmac_buf[SHA1_SIZE - 1] << 8) |
190ebffaa42SBen Gras hmac_buf[0];
191ebffaa42SBen Gras __crypt_to64(ep, ul, 4); ep += 4;
192ebffaa42SBen Gras *ep = '\0';
193ebffaa42SBen Gras
194ebffaa42SBen Gras /* Don't leave anything around in vm they could use. */
195*84d9c625SLionel Sambuc explicit_memset(hmac_buf, 0, sizeof hmac_buf);
196ebffaa42SBen Gras
197ebffaa42SBen Gras return passwd;
198ebffaa42SBen Gras }
199