]> git.meshlink.io Git - meshlink/blob - src/prf.c
Fix memory leaks from timers.
[meshlink] / src / prf.c
1 /*
2     prf.c -- Pseudo-Random Function for key material generation
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
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
31 #define mdlen 64
32
33 // TODO: separate key setup from hmac_sha512
34
35 static bool hmac_sha512(const char *key, size_t keylen, const char *msg, size_t msglen, char *out) {
36         char tmp[2 * mdlen];
37         sha512_context md;
38
39         if(keylen <= mdlen) {
40                 memcpy(tmp, key, keylen);
41                 memset(tmp + keylen, 0, mdlen - keylen);
42         } else {
43                 if(sha512(key, keylen, tmp) != 0) {
44                         return false;
45                 }
46         }
47
48         if(sha512_init(&md) != 0) {
49                 return false;
50         }
51
52         // ipad
53         memxor(tmp, 0x36, mdlen);
54
55         if(sha512_update(&md, tmp, mdlen) != 0) {
56                 return false;
57         }
58
59         // message
60         if(sha512_update(&md, msg, msglen) != 0) {
61                 return false;
62         }
63
64         if(sha512_final(&md, tmp + mdlen) != 0) {
65                 return false;
66         }
67
68         // opad
69         memxor(tmp, 0x36 ^ 0x5c, mdlen);
70
71         if(sha512(tmp, sizeof(tmp), out) != 0) {
72                 return false;
73         }
74
75         return true;
76 }
77
78 /* Generate key material from a master secret and a seed, based on RFC 4346 section 5.
79    We use SHA512 instead of MD5 and SHA1.
80  */
81
82 bool prf(const char *secret, size_t secretlen, char *seed, size_t seedlen, char *out, size_t outlen) {
83         /* Data is what the "inner" HMAC function processes.
84            It consists of the previous HMAC result plus the seed.
85          */
86
87         char data[mdlen + seedlen];
88         memset(data, 0, mdlen);
89         memcpy(data + mdlen, seed, seedlen);
90
91         char hash[mdlen];
92
93         while(outlen > 0) {
94                 /* Inner HMAC */
95                 if(!hmac_sha512(data, sizeof(data), secret, secretlen, data)) {
96                         return false;
97                 }
98
99                 /* Outer HMAC */
100                 if(outlen >= mdlen) {
101                         if(!hmac_sha512(data, sizeof(data), secret, secretlen, out)) {
102                                 return false;
103                         }
104
105                         out += mdlen;
106                         outlen -= mdlen;
107                 } else {
108                         if(!hmac_sha512(data, sizeof(data), secret, secretlen, hash)) {
109                                 return false;
110                         }
111
112                         memcpy(out, hash, outlen);
113                         out += outlen;
114                         outlen = 0;
115                 }
116         }
117
118         return true;
119 }