Home | History | Annotate | Line # | Download | only in cgdconfig
pkcs5_pbkdf2.c revision 1.1
      1  1.1  elric /* $NetBSD: pkcs5_pbkdf2.c,v 1.1 2002/10/04 18:37:20 elric Exp $ */
      2  1.1  elric 
      3  1.1  elric /*-
      4  1.1  elric  * Copyright (c) 2002 The NetBSD Foundation, Inc.
      5  1.1  elric  * All rights reserved.
      6  1.1  elric  *
      7  1.1  elric  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  elric  * by Roland C. Dowdeswell.
      9  1.1  elric  *
     10  1.1  elric  * Redistribution and use in source and binary forms, with or without
     11  1.1  elric  * modification, are permitted provided that the following conditions
     12  1.1  elric  * are met:
     13  1.1  elric  * 1. Redistributions of source code must retain the above copyright
     14  1.1  elric  *    notice, this list of conditions and the following disclaimer.
     15  1.1  elric  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  elric  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  elric  *    documentation and/or other materials provided with the distribution.
     18  1.1  elric  * 3. All advertising materials mentioning features or use of this software
     19  1.1  elric  *    must display the following acknowledgement:
     20  1.1  elric  *        This product includes software developed by the NetBSD
     21  1.1  elric  *        Foundation, Inc. and its contributors.
     22  1.1  elric  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  elric  *    contributors may be used to endorse or promote products derived
     24  1.1  elric  *    from this software without specific prior written permission.
     25  1.1  elric  *
     26  1.1  elric  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  elric  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  elric  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  elric  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  elric  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  elric  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  elric  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  elric  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  elric  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  elric  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  elric  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  elric  */
     38  1.1  elric 
     39  1.1  elric /*
     40  1.1  elric  * This code is an implementation of PKCS #5 PBKDF2 which is described
     41  1.1  elric  * in:
     42  1.1  elric  *
     43  1.1  elric  * ``PKCS #5 v2.0: Password-Based Cryptography Standard'', RSA Laboratories,
     44  1.1  elric  * March 25, 1999.
     45  1.1  elric  *
     46  1.1  elric  * and can be found at the following URL:
     47  1.1  elric  *
     48  1.1  elric  *	http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/
     49  1.1  elric  *
     50  1.1  elric  * It was also republished as RFC 2898.
     51  1.1  elric  */
     52  1.1  elric 
     53  1.1  elric 
     54  1.1  elric #include <sys/cdefs.h>
     55  1.1  elric #ifndef lint
     56  1.1  elric __RCSID("$NetBSD: pkcs5_pbkdf2.c,v 1.1 2002/10/04 18:37:20 elric Exp $");
     57  1.1  elric #endif
     58  1.1  elric 
     59  1.1  elric #include <assert.h>
     60  1.1  elric #include <malloc.h>
     61  1.1  elric #include <stdlib.h>
     62  1.1  elric #include <string.h>
     63  1.1  elric 
     64  1.1  elric #include <openssl/hmac.h>
     65  1.1  elric 
     66  1.1  elric #include "pkcs5_pbkdf2.h"
     67  1.1  elric #include "utils.h"
     68  1.1  elric 
     69  1.1  elric static void	int_encode(u_int8_t *, int);
     70  1.1  elric static void	prf_iterate(u_int8_t *, const u_int8_t *, int,
     71  1.1  elric 			    const u_int8_t *, int, int, int);
     72  1.1  elric 
     73  1.1  elric #define PRF_BLOCKLEN	20
     74  1.1  elric 
     75  1.1  elric /*
     76  1.1  elric  * int_encode encodes i as a four octet integer, most significant
     77  1.1  elric  * octet first.  (from the end of Step 3).
     78  1.1  elric  */
     79  1.1  elric 
     80  1.1  elric static void
     81  1.1  elric int_encode(u_int8_t *res, int i)
     82  1.1  elric {
     83  1.1  elric 
     84  1.1  elric 	*res++ = (i >> 24) & 0xff;
     85  1.1  elric 	*res++ = (i >> 16) & 0xff;
     86  1.1  elric 	*res++ = (i >>  8) & 0xff;
     87  1.1  elric 	*res   = (i      ) & 0xff;
     88  1.1  elric }
     89  1.1  elric 
     90  1.1  elric void
     91  1.1  elric prf_iterate(u_int8_t *r, const u_int8_t *P, int Plen,
     92  1.1  elric 	    const u_int8_t *S, int Slen, int c, int ind)
     93  1.1  elric {
     94  1.1  elric 	int		 first_time = 1;
     95  1.1  elric 	int		 i;
     96  1.1  elric 	int		 datalen;
     97  1.1  elric 	int		 tmplen;
     98  1.1  elric 	u_int8_t	*data;
     99  1.1  elric 	u_int8_t	 tmp[EVP_MAX_MD_SIZE];
    100  1.1  elric 
    101  1.1  elric 	data = malloc(Slen + 4);
    102  1.1  elric 	memcpy(data, S, Slen);
    103  1.1  elric 	int_encode(data + Slen, ind);
    104  1.1  elric 	datalen = Slen + 4;
    105  1.1  elric 
    106  1.1  elric 	for (i=0; i < c; i++) {
    107  1.1  elric 		HMAC(EVP_sha1(), P, Plen, data, datalen, tmp, &tmplen);
    108  1.1  elric 
    109  1.1  elric 		assert(tmplen == PRF_BLOCKLEN);
    110  1.1  elric 
    111  1.1  elric 		if (first_time) {
    112  1.1  elric 			memcpy(r, tmp, PRF_BLOCKLEN);
    113  1.1  elric 			first_time = 0;
    114  1.1  elric 		} else
    115  1.1  elric 			memxor(r, tmp, PRF_BLOCKLEN);
    116  1.1  elric 		memcpy(data, tmp, PRF_BLOCKLEN);
    117  1.1  elric 		datalen = PRF_BLOCKLEN;
    118  1.1  elric 	}
    119  1.1  elric 	free(data);
    120  1.1  elric }
    121  1.1  elric 
    122  1.1  elric /*
    123  1.1  elric  * pkcs5_pbkdf2 takes all of its lengths in bytes.
    124  1.1  elric  */
    125  1.1  elric 
    126  1.1  elric int
    127  1.1  elric pkcs5_pbkdf2(u_int8_t **r, int dkLen, const u_int8_t *P, int Plen,
    128  1.1  elric 	     const u_int8_t *S, int Slen, int c)
    129  1.1  elric {
    130  1.1  elric 	int	i;
    131  1.1  elric 	int	l;
    132  1.1  elric 
    133  1.1  elric 	/* sanity */
    134  1.1  elric 	if (!r)
    135  1.1  elric 		return -1;
    136  1.1  elric 	if (dkLen <= 0)
    137  1.1  elric 		return -1;
    138  1.1  elric 	if (c < 1)
    139  1.1  elric 		return -1;
    140  1.1  elric 
    141  1.1  elric 	/* Step 2 */
    142  1.1  elric 	l = (dkLen + PRF_BLOCKLEN - 1) / PRF_BLOCKLEN;
    143  1.1  elric 
    144  1.1  elric 	/* allocate the output */
    145  1.1  elric 	*r = malloc(l * PRF_BLOCKLEN);
    146  1.1  elric 	if (!*r)
    147  1.1  elric 		return -1;
    148  1.1  elric 
    149  1.1  elric 	/* Step 3 */
    150  1.1  elric 	for (i=0; i < l; i++)
    151  1.1  elric 		prf_iterate(*r + (PRF_BLOCKLEN * i), P, Plen, S, Slen, c, i);
    152  1.1  elric 
    153  1.1  elric 	/* Step 4 and 5
    154  1.1  elric 	 *  by the structure of the code, we do not need to concatenate
    155  1.1  elric 	 *  the blocks, they're already concatenated.  We do not extract
    156  1.1  elric 	 *  the first dkLen octets, since we [naturally] assume that the
    157  1.1  elric 	 *  calling function will use only the octets that it needs and
    158  1.1  elric 	 *  the free(3) will free all of the allocated memory.
    159  1.1  elric 	 */
    160  1.1  elric 	return 0;
    161  1.1  elric }
    162