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