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