1 /* pkcs5.c Partial Password-Based Cryptography (PKCS#5) implementation
2 * Copyright (C) 2002 Free Software Foundation, Inc.
4 * This file is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This file 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this file; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * PBKDF2 applies a pseudorandom function (see Appendix B.1 for an
26 * example) to derive keys. The length of the derived key is essentially
27 * unbounded. (However, the maximum effective search space for the
28 * derived key may be limited by the structure of the underlying
29 * pseudorandom function. See Appendix B.1 for further discussion.)
30 * PBKDF2 is recommended for new applications.
32 * PBKDF2 (P, S, c, dkLen)
34 * Options: PRF underlying pseudorandom function (hLen
35 * denotes the length in octets of the
36 * pseudorandom function output)
38 * Input: P password, an octet string
39 * S salt, an octet string
40 * c iteration count, a positive integer
41 * dkLen intended length in octets of the derived
42 * key, a positive integer, at most
45 * Output: DK derived key, a dkLen-octet string
49 gcry_pbkdf2 (int PRF
, const char *P
, size_t Plen
, const char *S
,
50 size_t Slen
, unsigned int c
, unsigned int dkLen
, char *DK
)
53 gcry_error_t maybeError
;
64 hLen
= gcry_md_get_algo_dlen (PRF
);
66 return GPG_ERR_UNSUPPORTED_ALGORITHM
;
69 return GPG_ERR_INV_ARG
;
72 return GPG_ERR_TOO_SHORT
;
78 * 1. If dkLen > (2^32 - 1) * hLen, output "derived key too long" and
82 if (dkLen
> 4294967295U)
83 return GPG_ERR_TOO_LARGE
;
86 * 2. Let l be the number of hLen-octet blocks in the derived key,
87 * rounding up, and let r be the number of octets in the last
90 * l = CEIL (dkLen / hLen) ,
91 * r = dkLen - (l - 1) * hLen .
93 * Here, CEIL (x) is the "ceiling" function, i.e. the smallest
94 * integer greater than, or equal to, x.
100 r
= dkLen
- (l
- 1) * hLen
;
103 * 3. For each block of the derived key apply the function F defined
104 * below to the password P, the salt S, the iteration count c, and
105 * the block index to compute the block:
107 * T_1 = F (P, S, c, 1) ,
108 * T_2 = F (P, S, c, 2) ,
110 * T_l = F (P, S, c, l) ,
112 * where the function F is defined as the exclusive-or sum of the
113 * first c iterates of the underlying pseudorandom function PRF
114 * applied to the password P and the concatenation of the salt S
115 * and the block index i:
117 * F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c
121 * U_1 = PRF (P, S || INT (i)) ,
122 * U_2 = PRF (P, U_1) ,
124 * U_c = PRF (P, U_{c-1}) .
126 * Here, INT (i) is a four-octet encoding of the integer i, most
127 * significant octet first.
129 * 4. Concatenate the blocks and extract the first dkLen octets to
130 * produce a derived key DK:
132 * DK = T_1 || T_2 || ... || T_l<0..r-1>
134 * 5. Output the derived key DK.
136 * Note. The construction of the function F follows a "belt-and-
137 * suspenders" approach. The iterates U_i are computed recursively to
138 * remove a degree of parallelism from an opponent; they are exclusive-
139 * ored together to reduce concerns about the recursion degenerating
140 * into a small set of values.
143 maybeError
= gcry_md_open (&prf
, PRF
, GCRY_MD_FLAG_HMAC
| GCRY_MD_FLAG_SECURE
);
144 if (maybeError
!= GPG_ERR_NO_ERROR
)
145 return (-1 *maybeError
);
147 U
= (char*)gcry_malloc(hLen
);
154 for (i
= 1; i
<= l
; i
++)
156 memset(DK
+ (i
- 1) * hLen
, 0, i
== l
? r
: hLen
);
158 for (u
= 1; u
<= c
; u
++)
162 rc
= gcry_md_setkey (prf
, P
, Plen
);
163 if (rc
!= GPG_ERR_NO_ERROR
) {
169 gcry_md_write (prf
, S
, Slen
);
170 tmp
[0] = (i
& 0xff000000) >> 24;
171 tmp
[1] = (i
& 0x00ff0000) >> 16;
172 tmp
[2] = (i
& 0x0000ff00) >> 8;
173 tmp
[3] = (i
& 0x000000ff) >> 0;
174 gcry_md_write (prf
, tmp
, 4);
177 gcry_md_write (prf
, U
, hLen
);
179 p
= gcry_md_read (prf
, PRF
);
182 rc
= GPG_ERR_CONFIGURATION
;
187 for (k
= 0; k
< (i
== l
? r
: hLen
); k
++)
188 DK
[(i
- 1) * hLen
+ k
] ^= U
[k
];
192 rc
= GPG_ERR_NO_ERROR
;