]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cryptsetup/pbkdf2.c
python: update python build configuration to new ape capabilities like getaddrinfo...
[plan9front.git] / sys / src / cmd / cryptsetup / pbkdf2.c
1 /*      $OpenBSD: pbkdf2.c,v 1.1 2008/06/14 06:28:27 djm 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 <u.h>
20 #include <libc.h>
21 #include <mp.h>
22 #include <libsec.h>
23 #define DS DigestState /* only to abbreviate SYNOPSIS */
24 #define SHA1_DIGEST_LENGTH 20
25 #define MIN(a,b) ((a < b) ? a : b)
26
27 /*
28  * Password-Based Key Derivation Function 2 (PKCS #5 v2.0).
29  * Code based on IEEE Std 802.11-2007, Annex H.4.2.
30  */
31 int
32 pkcs5_pbkdf2(const unsigned char *pass, int pass_len, const unsigned char *salt, int salt_len,
33     unsigned char *key, int key_len, int rounds)
34 {
35         unsigned char *asalt, obuf[SHA1_DIGEST_LENGTH];
36         unsigned char d1[SHA1_DIGEST_LENGTH], d2[SHA1_DIGEST_LENGTH];
37         unsigned i, j;
38         unsigned count;
39         unsigned r;
40
41         if (rounds < 1 || key_len == 0)
42                 return -1;
43         if (salt_len == 0)
44                 return -1;
45         if ((asalt = malloc(salt_len + 4)) == nil)
46                 return -1;
47
48         memcpy(asalt, salt, salt_len);
49
50         for (count = 1; key_len > 0; count++) {
51                 asalt[salt_len + 0] = (count >> 24) & 0xff;
52                 asalt[salt_len + 1] = (count >> 16) & 0xff;
53                 asalt[salt_len + 2] = (count >> 8) & 0xff;
54                 asalt[salt_len + 3] = count & 0xff;
55                 hmac_sha1(asalt, salt_len + 4, pass, pass_len, d1, nil);
56                 memcpy(obuf, d1, sizeof(obuf));
57
58                 for (i = 1; i < rounds; i++) {
59                         hmac_sha1(d1, sizeof(d1), pass, pass_len, d2, nil);
60                         memcpy(d1, d2, sizeof(d1));
61                         for (j = 0; j < sizeof(obuf); j++)
62                                 obuf[j] ^= d1[j];
63                 }
64
65                 r = MIN(key_len, SHA1_DIGEST_LENGTH);
66                 memcpy(key, obuf, r);
67                 key += r;
68                 key_len -= r;
69         };
70         memset(asalt, 0, salt_len + 4);
71         free(asalt);
72         memset(d1, 0, sizeof(d1));
73         memset(d2, 0, sizeof(d2));
74         memset(obuf, 0, sizeof(obuf));
75
76         return 0;
77 }