Home | History | Annotate | Line # | Download | only in cgdconfig
pkcs5_pbkdf2.c revision 1.13
      1 /* $NetBSD: pkcs5_pbkdf2.c,v 1.13 2008/04/21 15:23:35 christos Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Roland C. Dowdeswell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * This code is an implementation of PKCS #5 PBKDF2 which is described
     41  * in:
     42  *
     43  * ``PKCS #5 v2.0: Password-Based Cryptography Standard'', RSA Laboratories,
     44  * March 25, 1999.
     45  *
     46  * and can be found at the following URL:
     47  *
     48  *	http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/
     49  *
     50  * It was also republished as RFC 2898.
     51  */
     52 
     53 
     54 #include <sys/cdefs.h>
     55 #ifndef lint
     56 __RCSID("$NetBSD: pkcs5_pbkdf2.c,v 1.13 2008/04/21 15:23:35 christos Exp $");
     57 #endif
     58 
     59 #include <sys/resource.h>
     60 #include <sys/endian.h>
     61 
     62 #include <assert.h>
     63 #include <stdlib.h>
     64 #include <string.h>
     65 #include <err.h>
     66 #include <util.h>
     67 
     68 #include <openssl/hmac.h>
     69 
     70 #include "pkcs5_pbkdf2.h"
     71 #include "utils.h"
     72 
     73 static void	prf_iterate(u_int8_t *, const u_int8_t *, size_t,
     74 			    const u_int8_t *, size_t, size_t, size_t);
     75 static int	pkcs5_pbkdf2_time(size_t, size_t);
     76 
     77 #define PRF_BLOCKLEN	20
     78 
     79 static void
     80 prf_iterate(u_int8_t *r, const u_int8_t *P, size_t Plen,
     81 	    const u_int8_t *S, size_t Slen, size_t c, size_t ind)
     82 {
     83 	int		 first_time = 1;
     84 	size_t		 i;
     85 	size_t		 datalen;
     86 	unsigned int	 tmplen;
     87 	u_int8_t	*data;
     88 	u_int8_t	 tmp[EVP_MAX_MD_SIZE];
     89 
     90 	data = emalloc(Slen + 4);
     91 	(void)memcpy(data, S, Slen);
     92 	be32enc(data + Slen, ind);
     93 	datalen = Slen + 4;
     94 
     95 	for (i=0; i < c; i++) {
     96 		(void)HMAC(EVP_sha1(), P, Plen, data, datalen, tmp, &tmplen);
     97 
     98 		assert(tmplen == PRF_BLOCKLEN);
     99 
    100 		if (first_time) {
    101 			(void)memcpy(r, tmp, PRF_BLOCKLEN);
    102 			first_time = 0;
    103 		} else
    104 			memxor(r, tmp, PRF_BLOCKLEN);
    105 		(void)memcpy(data, tmp, PRF_BLOCKLEN);
    106 		datalen = PRF_BLOCKLEN;
    107 	}
    108 	free(data);
    109 }
    110 
    111 /*
    112  * pkcs5_pbkdf2 takes all of its lengths in bytes.
    113  */
    114 
    115 int
    116 pkcs5_pbkdf2(u_int8_t **r, size_t dkLen, const u_int8_t *P, size_t Plen,
    117 	     const u_int8_t *S, size_t Slen, size_t c, int compat)
    118 {
    119 	size_t	i;
    120 	size_t	l;
    121 
    122 	/* sanity */
    123 	if (!r)
    124 		return -1;
    125 	if (dkLen == 0)
    126 		return -1;
    127 	if (c < 1)
    128 		return -1;
    129 
    130 	/* Step 2 */
    131 	l = (dkLen + PRF_BLOCKLEN - 1) / PRF_BLOCKLEN;
    132 
    133 	/* allocate the output */
    134 	*r = emalloc(l * PRF_BLOCKLEN);
    135 
    136 	/* Step 3 */
    137 	for (i = 0; i < l; i++)
    138 		prf_iterate(*r + (PRF_BLOCKLEN * i), P, Plen, S, Slen, c,
    139 			(compat?i:i+1));
    140 
    141 	/* Step 4 and 5
    142 	 *  by the structure of the code, we do not need to concatenate
    143 	 *  the blocks, they're already concatenated.  We do not extract
    144 	 *  the first dkLen octets, since we [naturally] assume that the
    145 	 *  calling function will use only the octets that it needs and
    146 	 *  the free(3) will free all of the allocated memory.
    147 	 */
    148 	return 0;
    149 }
    150 
    151 /*
    152  * We use predefined lengths for the password and salt to ensure that
    153  * no analysis can be done on the output of the calibration based on
    154  * those parameters.  We do not do the same for dkLen because:
    155  *	1.  dkLen is known to the attacker if they know the iteration
    156  *	    count, and
    157  *	2.  using the wrong dkLen will skew the calibration by an
    158  *	    integral factor n = (dkLen / 160).
    159  */
    160 
    161 #define CAL_PASSLEN	   64
    162 #define CAL_SALTLEN	   64
    163 #define CAL_TIME	30000		/* Minimum number of microseconds that
    164 					 * are considered significant.
    165 					 */
    166 
    167 /*
    168  * We return the user time in microseconds that c iterations
    169  * of the algorithm take.
    170  */
    171 
    172 static int
    173 pkcs5_pbkdf2_time(size_t dkLen, size_t c)
    174 {
    175 	struct rusage	 start;
    176 	struct rusage	 end;
    177 	int		 ret;
    178 	u_int8_t	*r = NULL;
    179 	u_int8_t	 P[CAL_PASSLEN];
    180 	u_int8_t	 S[CAL_SALTLEN];
    181 
    182 	(void)getrusage(RUSAGE_SELF, &start);
    183 	/* XXX compat flag at end to be removed when _OLD keygen method is */
    184 	ret = pkcs5_pbkdf2(&r, dkLen, P, sizeof(P), S, sizeof(S), c, 0);
    185 	if (ret)
    186 		return ret;
    187 	(void)getrusage(RUSAGE_SELF, &end);
    188 	free(r);
    189 
    190 	return (end.ru_utime.tv_sec - start.ru_utime.tv_sec) * 1000000
    191 	     + (end.ru_utime.tv_usec - start.ru_utime.tv_usec);
    192 }
    193 
    194 int
    195 pkcs5_pbkdf2_calibrate(size_t dkLen, int microseconds)
    196 {
    197 	size_t	c;
    198 	int	t = 0;
    199 	size_t	ret, i;
    200 
    201 	for (i = 0; i < 5; i++) {
    202 		/*
    203 		 * First we get a meaningfully long time by doubling the
    204 		 * iteration count until it takes longer than CAL_TIME.  This
    205 		 * should take approximately 2 * CAL_TIME.
    206 		 */
    207 		for (c = 1;; c *= 2) {
    208 			t = pkcs5_pbkdf2_time(dkLen, c);
    209 			if (t > CAL_TIME)
    210 				break;
    211 		}
    212 
    213 		/* Now that we know that, we scale it. */
    214 		ret = (size_t) ((u_int64_t) c * microseconds / t);
    215 
    216 		/*
    217 		 * Since it is quite important to not get this wrong,
    218 		 * we test the result.
    219 		 */
    220 
    221 		t = pkcs5_pbkdf2_time(dkLen, ret);
    222 
    223 		/* if we are over 5% off, return an error */
    224 		if (abs(microseconds - t) > (microseconds / 20))
    225 			continue;
    226 		return ret;
    227 	}
    228 	return -1;
    229 }
    230