Add gc-pbkdf2-sha1 module.
[gnulib.git] / lib / gc-pbkdf2-sha1.c
1 /* gc-pbkdf2-sha1.c --- Password-Based Key Derivation Function a'la PKCS#5
2    Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 2, or (at your option)
7    any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
17
18 /* Written by Simon Josefsson.  The comments in this file are taken
19    from RFC 2898.  */
20
21 #ifdef HAVE_CONFIG_H
22 # include <config.h>
23 #endif
24
25 #include "gc.h"
26
27 #include <stdlib.h>
28 #include <string.h>
29
30 /*
31  * 5.2 PBKDF2
32  *
33  *  PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
34  *  example) to derive keys. The length of the derived key is essentially
35  *  unbounded. (However, the maximum effective search space for the
36  *  derived key may be limited by the structure of the underlying
37  *  pseudorandom function. See Appendix B.1 for further discussion.)
38  *  PBKDF2 is recommended for new applications.
39  *
40  *  PBKDF2 (P, S, c, dkLen)
41  *
42  *  Options:        PRF        underlying pseudorandom function (hLen
43  *                             denotes the length in octets of the
44  *                             pseudorandom function output)
45  *
46  *  Input:          P          password, an octet string (ASCII or UTF-8)
47  *                  S          salt, an octet string
48  *                  c          iteration count, a positive integer
49  *                  dkLen      intended length in octets of the derived
50  *                             key, a positive integer, at most
51  *                             (2^32 - 1) * hLen
52  *
53  *  Output:         DK         derived key, a dkLen-octet string
54  */
55
56 Gc_rc
57 gc_pbkdf2_sha1 (const char *P, size_t Plen,
58                 const char *S, size_t Slen,
59                 unsigned int c,
60                 char *DK, size_t dkLen)
61 {
62   unsigned int hLen = 20;
63   char U[20];
64   char T[20];
65   unsigned int u;
66   unsigned int l;
67   unsigned int r;
68   unsigned int i;
69   unsigned int k;
70   int rc;
71
72   if (c == 0)
73     return GC_PKCS5_INVALID_ITERATION_COUNT;
74
75   if (dkLen == 0)
76     return GC_PKCS5_INVALID_DERIVED_KEY_LENGTH;
77
78   /*
79    *
80    *  Steps:
81    *
82    *     1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
83    *        stop.
84    */
85
86   if (dkLen > 4294967295U)
87     return GC_PKCS5_DERIVED_KEY_TOO_LONG;
88
89   /*
90    *     2. Let l be the number of hLen-octet blocks in the derived key,
91    *        rounding up, and let r be the number of octets in the last
92    *        block:
93    *
94    *                  l = CEIL (dkLen / hLen) ,
95    *                  r = dkLen - (l - 1) * hLen .
96    *
97    *        Here, CEIL (x) is the "ceiling" function, i.e. the smallest
98    *        integer greater than, or equal to, x.
99    */
100
101   l = dkLen / hLen;
102   if (dkLen % hLen)
103     l++;
104   r = dkLen - (l - 1) * hLen;
105
106   /*
107    *     3. For each block of the derived key apply the function F defined
108    *        below to the password P, the salt S, the iteration count c, and
109    *        the block index to compute the block:
110    *
111    *                  T_1 = F (P, S, c, 1) ,
112    *                  T_2 = F (P, S, c, 2) ,
113    *                  ...
114    *                  T_l = F (P, S, c, l) ,
115    *
116    *        where the function F is defined as the exclusive-or sum of the
117    *        first c iterates of the underlying pseudorandom function PRF
118    *        applied to the password P and the concatenation of the salt S
119    *        and the block index i:
120    *
121    *                  F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
122    *
123    *        where
124    *
125    *                  U_1 = PRF (P, S || INT (i)) ,
126    *                  U_2 = PRF (P, U_1) ,
127    *                  ...
128    *                  U_c = PRF (P, U_{c-1}) .
129    *
130    *        Here, INT (i) is a four-octet encoding of the integer i, most
131    *        significant octet first.
132    *
133    *     4. Concatenate the blocks and extract the first dkLen octets to
134    *        produce a derived key DK:
135    *
136    *                  DK = T_1 || T_2 ||  ...  || T_l<0..r-1>
137    *
138    *     5. Output the derived key DK.
139    *
140    *  Note. The construction of the function F follows a "belt-and-
141    *  suspenders" approach. The iterates U_i are computed recursively to
142    *  remove a degree of parallelism from an opponent; they are exclusive-
143    *  ored together to reduce concerns about the recursion degenerating
144    *  into a small set of values.
145    *
146    */
147
148   for (i = 1; i <= l; i++)
149     {
150       memset (T, 0, hLen);
151
152       for (u = 1; u <= c; u++)
153         {
154           if (u == 1)
155             {
156               char *tmp;
157               size_t tmplen = Slen + 4;
158
159               tmp = malloc (tmplen);
160               if (tmp == NULL)
161                 return GC_MALLOC_ERROR;
162
163               memcpy (tmp, S, Slen);
164               tmp[Slen + 0] = (i & 0xff000000) >> 24;
165               tmp[Slen + 1] = (i & 0x00ff0000) >> 16;
166               tmp[Slen + 2] = (i & 0x0000ff00) >> 8;
167               tmp[Slen + 3] = (i & 0x000000ff) >> 0;
168
169               rc = gc_hmac_sha1 (P, Plen, tmp, tmplen, U);
170
171               free (tmp);
172             }
173           else
174             rc = gc_hmac_sha1 (P, Plen, U, hLen, U);
175
176           if (rc != GC_OK)
177             return rc;
178
179           for (k = 0; k < hLen; k++)
180             T[k] ^= U[k];
181         }
182
183       memcpy (DK + (i - 1) * hLen, T, i == l ? r : hLen);
184     }
185
186   return GC_OK;
187 }