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