Commit 1513dc4e authored by Philip Withnall's avatar Philip Withnall Committed by Olivier Crête

stun: Use libgcrypt for SHA1 support

Now that libstun depends on libgcrypt, we might as well use its SHA1 hash
support, rather than carrying around our own.

Differential Revision: https://phabricator.freedesktop.org/D1612
parent ac7f59ba
......@@ -29,7 +29,6 @@ libstun_la_SOURCES = constants.h \
stunmessage.c stunmessage.h \
stun5389.c stun5389.h \
stuncrc32.c stuncrc32.h \
sha1.c sha1.h \
rand.c rand.h \
stunhmac.c stunhmac.h \
utils.c utils.h \
......
This diff is collapsed.
/*
* SHA1 hash implementation and interface functions
* Copyright (c) 2003-2005, Jouni Malinen <j@w1.fi>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* Alternatively, this software may be distributed under the terms of BSD
* license.
*
* See README and COPYING for more details.
*/
#ifndef SHA1_H
#define SHA1_H
#ifdef _WIN32
#include "win32_common.h"
#else
#include <stdint.h>
#endif
#include <stddef.h>
#define SHA1_MAC_LEN 20
struct SHA1Context {
uint32_t state[5];
uint32_t count[2];
unsigned char buffer[64];
};
typedef struct SHA1Context SHA1_CTX;
void SHA1Init(SHA1_CTX *context);
void SHA1Update(SHA1_CTX *context, const void *data, uint32_t len);
void SHA1Final(unsigned char digest[20], SHA1_CTX *context);
struct HMACContext {
SHA1_CTX context;
uint8_t key[64];
size_t key_len;
};
typedef struct HMACContext HMAC_CTX;
void HMACInit(HMAC_CTX *context, const uint8_t *key, size_t key_len);
void HMACUpdate(HMAC_CTX *context, const void *data, uint32_t len);
void HMACFinal(unsigned char digest[20], HMAC_CTX *context);
void sha1_vector(size_t num_elem, const uint8_t *addr[], const size_t *len,
uint8_t *mac);
void hmac_sha1_vector(const uint8_t *key, size_t key_len, size_t num_elem,
const uint8_t *addr[], const size_t *len, uint8_t *mac);
void hmac_sha1(const uint8_t *key, size_t key_len,
const uint8_t *data, size_t data_len, uint8_t *mac);
void sha1_prf(const uint8_t *key, size_t key_len, const char *label,
const uint8_t *data, size_t data_len, uint8_t *buf, size_t buf_len);
#endif /* SHA1_H */
......@@ -40,7 +40,6 @@
# include <config.h>
#endif
#include "sha1.h"
#include "rand.h"
#include "stunmessage.h"
......@@ -48,37 +47,40 @@
#include <string.h>
#include <assert.h>
#include <gcrypt.h>
void stun_sha1 (const uint8_t *msg, size_t len, size_t msg_len, uint8_t *sha,
const void *key, size_t keylen, int padding)
{
uint16_t fakelen = htons (msg_len);
const uint8_t *vector[4];
size_t lengths[4];
uint8_t pad_char[64] = {0};
size_t num_elements;
gcry_mac_hd_t hd;
size_t sha_len = 20;
#define TRY(s) \
if (!(s)) \
abort ();
assert (len >= 44u);
vector[0] = msg;
lengths[0] = 2;
vector[1] = (const uint8_t *)&fakelen;
lengths[1] = 2;
vector[2] = msg + 4;
lengths[2] = len - 28;
num_elements = 3;
TRY (gcry_mac_open (&hd, GCRY_MAC_HMAC_SHA1, 0 /* flags */, NULL) == 0);
TRY (gcry_mac_setkey (hd, key, keylen) == 0);
TRY (gcry_mac_write (hd, msg, 2) == 0);
TRY (gcry_mac_write (hd, &fakelen, 2) == 0);
TRY (gcry_mac_write (hd, msg + 4, len - 28) == 0);
/* RFC 3489 specifies that the message's size should be 64 bytes,
and \x00 padding should be done */
if (padding && ((len - 24) % 64) > 0) {
uint16_t pad_size = 64 - ((len - 24) % 64);
vector[3] = pad_char;
lengths[3] = pad_size;
num_elements++;
TRY (gcry_mac_write (hd, pad_char, pad_size) == 0);
}
hmac_sha1_vector(key, keylen, num_elements, vector, lengths, sha);
TRY (gcry_mac_read (hd, sha, &sha_len) == 0);
assert (sha_len == 20);
gcry_mac_close (hd);
}
static const uint8_t *priv_trim_var (const uint8_t *var, size_t *var_len)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment