Remove elliptic curve stubs from gcrypt/, add PRF implementation.
[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(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 static const size_t blklen = 128;
32
33 static bool hmac_sha512(const char *key, size_t keylen, const char *msg, size_t msglen, char *out) {
34         char tmp[blklen + mdlen];
35         sha512_context md;
36
37         if(keylen <= blklen) {
38                 memcpy(tmp, key, keylen);
39                 memset(tmp + keylen, 0, blklen - keylen);
40         } else {
41                 if(sha512(key, keylen, tmp) != 0)
42                         return false;
43                 memset(tmp + mdlen, 0, blklen - mdlen);
44         }
45
46         if(sha512_init(&md) != 0)
47                 return false;
48
49         // ipad
50         memxor(tmp, 0x36, blklen);
51         if(sha512_update(&md, tmp, blklen) != 0)
52                 return false;
53
54         // message
55         if(sha512_update(&md, msg, msglen) != 0)
56                 return false;
57
58         if(sha512_final(&md, tmp + blklen) != 0)
59                 return false;
60
61         // opad
62         memxor(tmp, 0x36 ^ 0x5c, blklen);
63         if(sha512(tmp, sizeof tmp, out) != 0)
64                 return false;
65
66         return true;
67 }
68
69
70 /* Generate key material from a master secret and a seed, based on RFC 4346 section 5.
71    We use SHA512 instead of MD5 and SHA1.
72  */
73
74 bool prf(const char *secret, size_t secretlen, char *seed, size_t seedlen, char *out, size_t outlen) {
75         /* Data is what the "inner" HMAC function processes.
76            It consists of the previous HMAC result plus the seed.
77          */
78
79         char data[mdlen + seedlen];
80         memset(data, 0, mdlen);
81         memcpy(data + mdlen, seed, seedlen);
82
83         char hash[mdlen];
84
85         while(outlen > 0) {
86                 /* Inner HMAC */
87                 if(!hmac_sha512(secret, secretlen, data, sizeof data, data))
88                         return false;
89
90                 /* Outer HMAC */
91                 if(outlen >= mdlen) {
92                         if(!hmac_sha512(secret, secretlen, data, sizeof data, out))
93                                 return false;
94                         out += mdlen;
95                         outlen -= mdlen;
96                 } else {
97                         if(!hmac_sha512(secret, secretlen, data, sizeof data, hash))
98                                 return false;
99                         memcpy(out, hash, outlen);
100                         out += outlen;
101                         outlen = 0;
102                 }
103         }
104
105         return true;
106 }