xref: /openbsd-src/lib/libutil/pkcs5_pbkdf2.c (revision 50b7afb2c2c0993b0894d4e34bf857cb13ed9c80)
1 /*	$OpenBSD: pkcs5_pbkdf2.c,v 1.6 2014/01/31 16:56:32 tedu Exp $	*/
2 
3 /*-
4  * Copyright (c) 2008 Damien Bergamini <damien.bergamini@free.fr>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/param.h>
21 
22 #include <string.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <util.h>
26 
27 #include <sha1.h>
28 
29 /*
30  * HMAC-SHA-1 (from RFC 2202).
31  */
32 static void
33 hmac_sha1(const u_int8_t *text, size_t text_len, const u_int8_t *key,
34     size_t key_len, u_int8_t digest[SHA1_DIGEST_LENGTH])
35 {
36 	SHA1_CTX ctx;
37 	u_int8_t k_pad[SHA1_BLOCK_LENGTH];
38 	u_int8_t tk[SHA1_DIGEST_LENGTH];
39 	int i;
40 
41 	if (key_len > SHA1_BLOCK_LENGTH) {
42 		SHA1Init(&ctx);
43 		SHA1Update(&ctx, key, key_len);
44 		SHA1Final(tk, &ctx);
45 
46 		key = tk;
47 		key_len = SHA1_DIGEST_LENGTH;
48 	}
49 
50 	bzero(k_pad, sizeof k_pad);
51 	bcopy(key, k_pad, key_len);
52 	for (i = 0; i < SHA1_BLOCK_LENGTH; i++)
53 		k_pad[i] ^= 0x36;
54 
55 	SHA1Init(&ctx);
56 	SHA1Update(&ctx, k_pad, SHA1_BLOCK_LENGTH);
57 	SHA1Update(&ctx, text, text_len);
58 	SHA1Final(digest, &ctx);
59 
60 	bzero(k_pad, sizeof k_pad);
61 	bcopy(key, k_pad, key_len);
62 	for (i = 0; i < SHA1_BLOCK_LENGTH; i++)
63 		k_pad[i] ^= 0x5c;
64 
65 	SHA1Init(&ctx);
66 	SHA1Update(&ctx, k_pad, SHA1_BLOCK_LENGTH);
67 	SHA1Update(&ctx, digest, SHA1_DIGEST_LENGTH);
68 	SHA1Final(digest, &ctx);
69 }
70 
71 /*
72  * Password-Based Key Derivation Function 2 (PKCS #5 v2.0).
73  * Code based on IEEE Std 802.11-2007, Annex H.4.2.
74  */
75 int
76 pkcs5_pbkdf2(const char *pass, size_t pass_len, const uint8_t *salt,
77     size_t salt_len, uint8_t *key, size_t key_len, unsigned int rounds)
78 {
79 	uint8_t *asalt, obuf[SHA1_DIGEST_LENGTH];
80 	uint8_t d1[SHA1_DIGEST_LENGTH], d2[SHA1_DIGEST_LENGTH];
81 	unsigned int i, j;
82 	unsigned int count;
83 	size_t r;
84 
85 	if (rounds < 1 || key_len == 0)
86 		return -1;
87 	if (salt_len == 0 || salt_len > SIZE_MAX - 4)
88 		return -1;
89 	if ((asalt = malloc(salt_len + 4)) == NULL)
90 		return -1;
91 
92 	memcpy(asalt, salt, salt_len);
93 
94 	for (count = 1; key_len > 0; count++) {
95 		asalt[salt_len + 0] = (count >> 24) & 0xff;
96 		asalt[salt_len + 1] = (count >> 16) & 0xff;
97 		asalt[salt_len + 2] = (count >> 8) & 0xff;
98 		asalt[salt_len + 3] = count & 0xff;
99 		hmac_sha1(asalt, salt_len + 4, pass, pass_len, d1);
100 		memcpy(obuf, d1, sizeof(obuf));
101 
102 		for (i = 1; i < rounds; i++) {
103 			hmac_sha1(d1, sizeof(d1), pass, pass_len, d2);
104 			memcpy(d1, d2, sizeof(d1));
105 			for (j = 0; j < sizeof(obuf); j++)
106 				obuf[j] ^= d1[j];
107 		}
108 
109 		r = MIN(key_len, SHA1_DIGEST_LENGTH);
110 		memcpy(key, obuf, r);
111 		key += r;
112 		key_len -= r;
113 	};
114 	explicit_bzero(asalt, salt_len + 4);
115 	free(asalt);
116 	explicit_bzero(d1, sizeof(d1));
117 	explicit_bzero(d2, sizeof(d2));
118 	explicit_bzero(obuf, sizeof(obuf));
119 
120 	return 0;
121 }
122