Add missing nolegacy/crypto.c and prf.c.
[tinc] / src / nolegacy / prf.c
1 /*
2     prf.c -- Pseudo-Random Function for key material generation
3     Copyright (C) 2011-2013 Guus Sliepen <guus@tinc-vpn.org>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "../system.h"
21
22 #include "../prf.h"
23 #include "../ed25519/sha512.h"
24
25 static void memxor(char *buf, char c, size_t len) {
26         for(size_t i = 0; i < len; i++)
27                 buf[i] ^= c;
28 }
29
30 static const size_t mdlen = 64;
31
32 static bool hmac_sha512(const char *key, size_t keylen, const char *msg, size_t msglen, char *out) {
33         char tmp[2 * mdlen];
34         sha512_context md;
35
36         if(keylen <= mdlen) {
37                 memcpy(tmp, key, keylen);
38                 memset(tmp + keylen, 0, mdlen - keylen);
39         } else {
40                 if(sha512(key, keylen, tmp) != 0)
41                         return false;
42         }
43
44         if(sha512_init(&md) != 0)
45                 return false;
46
47         // ipad
48         memxor(tmp, 0x36, mdlen);
49         if(sha512_update(&md, tmp, mdlen) != 0)
50                 return false;
51
52         // message
53         if(sha512_update(&md, msg, msglen) != 0)
54                 return false;
55
56         if(sha512_final(&md, tmp + mdlen) != 0)
57                 return false;
58
59         // opad
60         memxor(tmp, 0x36 ^ 0x5c, mdlen);
61         if(sha512(tmp, sizeof tmp, out) != 0)
62                 return false;
63
64         return true;
65 }
66
67
68 /* Generate key material from a master secret and a seed, based on RFC 4346 section 5.
69    We use SHA512 instead of MD5 and SHA1.
70  */
71
72 bool prf(const char *secret, size_t secretlen, char *seed, size_t seedlen, char *out, size_t outlen) {
73         /* Data is what the "inner" HMAC function processes.
74            It consists of the previous HMAC result plus the seed.
75          */
76
77         char data[mdlen + seedlen];
78         memset(data, 0, mdlen);
79         memcpy(data + mdlen, seed, seedlen);
80
81         char hash[mdlen];
82
83         while(outlen > 0) {
84                 /* Inner HMAC */
85                 if(!hmac_sha512(data, sizeof data, secret, secretlen, data))
86                         return false;
87
88                 /* Outer HMAC */
89                 if(outlen >= mdlen) {
90                         if(!hmac_sha512(data, sizeof data, secret, secretlen, out))
91                                 return false;
92                         out += mdlen;
93                         outlen -= mdlen;
94                 } else {
95                         if(!hmac_sha512(data, sizeof data, secret, secretlen, hash))
96                                 return false;
97                         memcpy(out, hash, outlen);
98                         out += outlen;
99                         outlen = 0;
100                 }
101         }
102
103         return true;
104 }