/src/nettle-with-mini-gmp/pbkdf2.c
Line  | Count  | Source  | 
1  |  | /* pbkdf2.c  | 
2  |  |  | 
3  |  |    PKCS #5 password-based key derivation function PBKDF2, see RFC 2898.  | 
4  |  |  | 
5  |  |    Copyright (C) 2012 Simon Josefsson, Niels Möller  | 
6  |  |  | 
7  |  |    This file is part of GNU Nettle.  | 
8  |  |  | 
9  |  |    GNU Nettle is free software: you can redistribute it and/or  | 
10  |  |    modify it under the terms of either:  | 
11  |  |  | 
12  |  |      * the GNU Lesser General Public License as published by the Free  | 
13  |  |        Software Foundation; either version 3 of the License, or (at your  | 
14  |  |        option) any later version.  | 
15  |  |  | 
16  |  |    or  | 
17  |  |  | 
18  |  |      * the GNU General Public License as published by the Free  | 
19  |  |        Software Foundation; either version 2 of the License, or (at your  | 
20  |  |        option) any later version.  | 
21  |  |  | 
22  |  |    or both in parallel, as here.  | 
23  |  |  | 
24  |  |    GNU Nettle is distributed in the hope that it will be useful,  | 
25  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of  | 
26  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
27  |  |    General Public License for more details.  | 
28  |  |  | 
29  |  |    You should have received copies of the GNU General Public License and  | 
30  |  |    the GNU Lesser General Public License along with this program.  If  | 
31  |  |    not, see http://www.gnu.org/licenses/.  | 
32  |  | */  | 
33  |  |  | 
34  |  | #if HAVE_CONFIG_H  | 
35  |  | # include "config.h"  | 
36  |  | #endif  | 
37  |  |  | 
38  |  | #include <assert.h>  | 
39  |  | #include <stdlib.h>  | 
40  |  | #include <string.h>  | 
41  |  |  | 
42  |  | #include "pbkdf2.h"  | 
43  |  |  | 
44  |  | #include "macros.h"  | 
45  |  | #include "memxor.h"  | 
46  |  | #include "nettle-internal.h"  | 
47  |  |  | 
48  |  | void  | 
49  |  | pbkdf2 (void *mac_ctx,  | 
50  |  |   nettle_hash_update_func *update,  | 
51  |  |   nettle_hash_digest_func *digest,  | 
52  |  |   size_t digest_size, unsigned iterations,  | 
53  |  |   size_t salt_length, const uint8_t *salt,  | 
54  |  |   size_t length, uint8_t *dst)  | 
55  | 804  | { | 
56  | 804  |   TMP_DECL(U, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);  | 
57  | 804  |   TMP_DECL(T, uint8_t, NETTLE_MAX_HASH_DIGEST_SIZE);  | 
58  |  |     | 
59  | 804  |   unsigned i;  | 
60  |  |  | 
61  | 804  |   assert (iterations > 0);  | 
62  |  |  | 
63  | 804  |   if (length == 0)  | 
64  | 204  |     return;  | 
65  |  |  | 
66  | 600  |   TMP_ALLOC (U, digest_size);  | 
67  | 600  |   TMP_ALLOC (T, digest_size);  | 
68  |  |  | 
69  | 600  |   for (i = 1;;  | 
70  | 10.6k  |        i++, dst += digest_size, length -= digest_size)  | 
71  | 11.2k  |     { | 
72  | 11.2k  |       uint8_t tmp[4];  | 
73  | 11.2k  |       uint8_t *prev;  | 
74  | 11.2k  |       unsigned u;  | 
75  |  |         | 
76  | 11.2k  |       WRITE_UINT32 (tmp, i);  | 
77  |  |         | 
78  | 11.2k  |       update (mac_ctx, salt_length, salt);  | 
79  | 11.2k  |       update (mac_ctx, sizeof(tmp), tmp);  | 
80  | 11.2k  |       digest (mac_ctx, digest_size, T);  | 
81  |  |  | 
82  | 11.2k  |       prev = T;  | 
83  |  |         | 
84  | 25.1k  |       for (u = 1; u < iterations; u++, prev = U)  | 
85  | 13.8k  |   { | 
86  | 13.8k  |     update (mac_ctx, digest_size, prev);  | 
87  | 13.8k  |     digest (mac_ctx, digest_size, U);  | 
88  |  |  | 
89  | 13.8k  |     memxor (T, U, digest_size);  | 
90  | 13.8k  |   }  | 
91  |  |  | 
92  | 11.2k  |       if (length <= digest_size)  | 
93  | 600  |   { | 
94  | 600  |     memcpy (dst, T, length);  | 
95  | 600  |     return;  | 
96  | 600  |   }  | 
97  | 10.6k  |       memcpy (dst, T, digest_size);  | 
98  | 10.6k  |     }  | 
99  | 600  | }  |