Use PRF.
This commit is contained in:
parent
feb3f22fff
commit
82f00ea07b
2 changed files with 17 additions and 23 deletions
|
@ -22,14 +22,14 @@
|
||||||
#include "digest.h"
|
#include "digest.h"
|
||||||
#include "prf.h"
|
#include "prf.h"
|
||||||
|
|
||||||
/* Generate key material from a master secret and a seed, based on RFC 2246.
|
/* Generate key material from a master secret and a seed, based on RFC 4346 section 5.
|
||||||
We use SHA512 and Whirlpool instead of MD5 and SHA1.
|
We use SHA512 and Whirlpool instead of MD5 and SHA1.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static bool prf_xor(int nid, char *secret, size_t secretlen, char *seed, size_t seedlen, char *out, ssize_t outlen) {
|
static bool prf_xor(int nid, char *secret, size_t secretlen, char *seed, size_t seedlen, char *out, ssize_t outlen) {
|
||||||
digest_t digest;
|
digest_t digest;
|
||||||
|
|
||||||
if(!digest_open_by_nid(&digest, nid, 0))
|
if(!digest_open_by_nid(&digest, nid, -1))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if(!digest_set_key(&digest, secret, secretlen))
|
if(!digest_set_key(&digest, secret, secretlen))
|
||||||
|
|
|
@ -29,6 +29,7 @@
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "netutl.h"
|
#include "netutl.h"
|
||||||
#include "node.h"
|
#include "node.h"
|
||||||
|
#include "prf.h"
|
||||||
#include "protocol.h"
|
#include "protocol.h"
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
#include "xalloc.h"
|
#include "xalloc.h"
|
||||||
|
@ -311,25 +312,27 @@ bool ans_key_h(connection_t *c, char *request) {
|
||||||
|
|
||||||
/* Update our crypto end */
|
/* Update our crypto end */
|
||||||
|
|
||||||
char *mykey;
|
|
||||||
size_t mykeylen = cipher_keylength(&myself->incipher);
|
size_t mykeylen = cipher_keylength(&myself->incipher);
|
||||||
keylen = cipher_keylength(&from->outcipher);
|
keylen = cipher_keylength(&from->outcipher);
|
||||||
|
|
||||||
if(ECDH_SHARED_SIZE < mykeylen) {
|
char *mykey;
|
||||||
logger(LOG_ERR, "ECDH key too short for cipher of MYSELF!");
|
char *seed = NULL;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(strcmp(myself->name, from->name) < 0) {
|
if(strcmp(myself->name, from->name) < 0) {
|
||||||
logger(LOG_DEBUG, "Using left half of shared secret");
|
logger(LOG_DEBUG, "Using left half of shared secret");
|
||||||
mykey = shared;
|
mykey = key;
|
||||||
memcpy(key, shared + ECDH_SHARED_SIZE - keylen, keylen);
|
xasprintf(&seed, "tinc key expansion %s %s", myself->name, from->name);
|
||||||
} else {
|
} else {
|
||||||
logger(LOG_DEBUG, "Using right half of shared secret");
|
logger(LOG_DEBUG, "Using right half of shared secret");
|
||||||
mykey = shared + ECDH_SHARED_SIZE - mykeylen;
|
mykey = key + keylen;
|
||||||
memcpy(key, shared, keylen);
|
xasprintf(&seed, "tinc key expansion %s %s", from->name, myself->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!prf(shared, ECDH_SHARED_SIZE, seed, strlen(seed), key, keylen + mykeylen))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
free(seed);
|
||||||
|
|
||||||
cipher_open_by_nid(&from->incipher, cipher_get_nid(&myself->incipher));
|
cipher_open_by_nid(&from->incipher, cipher_get_nid(&myself->incipher));
|
||||||
digest_open_by_nid(&from->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
|
digest_open_by_nid(&from->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
|
||||||
from->incompression = myself->incompression;
|
from->incompression = myself->incompression;
|
||||||
|
@ -343,17 +346,8 @@ bool ans_key_h(connection_t *c, char *request) {
|
||||||
if(replaywin)
|
if(replaywin)
|
||||||
memset(from->late, 0, replaywin);
|
memset(from->late, 0, replaywin);
|
||||||
|
|
||||||
bin2hex(shared, hex, ECDH_SHARED_SIZE);
|
if(strcmp(myself->name, from->name) < 0)
|
||||||
hex[ECDH_SHARED_SIZE * 2] = 0;
|
memmove(key, key + mykeylen, keylen);
|
||||||
logger(LOG_DEBUG, "Shared secret was %s", hex);
|
|
||||||
|
|
||||||
bin2hex(mykey, hex, mykeylen);
|
|
||||||
hex[mykeylen * 2] = 0;
|
|
||||||
logger(LOG_DEBUG, "My part is: %s (%d)", hex, mykeylen);
|
|
||||||
|
|
||||||
bin2hex(key, hex, keylen);
|
|
||||||
hex[keylen * 2] = 0;
|
|
||||||
logger(LOG_DEBUG, "His part is: %s (%d)", hex, keylen);
|
|
||||||
} else {
|
} else {
|
||||||
keylen = strlen(key) / 2;
|
keylen = strlen(key) / 2;
|
||||||
hex2bin(key, key, keylen);
|
hex2bin(key, key, keylen);
|
||||||
|
|
Loading…
Reference in a new issue