freeswitch/libs/srtp/crypto/hash/hmac_ossl.c

243 lines
7.2 KiB
C
Raw Normal View History

2014-02-24 14:20:03 +00:00
/*
* hmac_ossl.c
*
2017-06-26 17:11:02 +00:00
* Implementation of hmac srtp_auth_type_t that leverages OpenSSL
2014-02-24 14:20:03 +00:00
*
* John A. Foley
* Cisco Systems, Inc.
*/
/*
*
2017-06-26 17:11:02 +00:00
* Copyright(c) 2013-2017, Cisco Systems, Inc.
2014-02-24 14:20:03 +00:00
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* Neither the name of the Cisco Systems, Inc. nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
2017-06-26 17:11:02 +00:00
#ifdef HAVE_CONFIG_H
#include <config.h>
2017-06-26 17:11:02 +00:00
#endif
#include "auth.h"
2014-02-24 14:20:03 +00:00
#include "alloc.h"
#include "err.h" /* for srtp_debug */
2021-08-28 11:19:05 +00:00
#include "auth_test_cases.h"
2014-02-24 14:20:03 +00:00
#include <openssl/evp.h>
2017-06-26 17:11:02 +00:00
#include <openssl/hmac.h>
#define SHA1_DIGEST_SIZE 20
2014-02-24 14:20:03 +00:00
/* the debug module for authentiation */
2017-06-26 17:11:02 +00:00
srtp_debug_module_t srtp_mod_hmac = {
0, /* debugging is off by default */
"hmac sha-1 openssl" /* printable name for module */
2014-02-24 14:20:03 +00:00
};
static srtp_err_status_t srtp_hmac_alloc(srtp_auth_t **a,
int key_len,
int out_len)
2014-02-24 14:20:03 +00:00
{
2017-06-26 17:11:02 +00:00
extern const srtp_auth_type_t srtp_hmac;
debug_print(srtp_mod_hmac, "allocating auth func with key length %d",
key_len);
debug_print(srtp_mod_hmac, " tag length %d",
out_len);
2014-02-24 14:20:03 +00:00
/* check output length - should be less than 20 bytes */
2017-06-26 17:11:02 +00:00
if (out_len > SHA1_DIGEST_SIZE) {
return srtp_err_status_bad_param;
}
/* OpenSSL 1.1.0 made HMAC_CTX an opaque structure, which must be allocated
using HMAC_CTX_new. But this function doesn't exist in OpenSSL 1.0.x. */
#if OPENSSL_VERSION_NUMBER < 0x10100000L || LIBRESSL_VERSION_NUMBER
2017-06-26 17:11:02 +00:00
{
/* allocate memory for auth and HMAC_CTX structures */
uint8_t *pointer;
2017-06-26 17:11:02 +00:00
HMAC_CTX *new_hmac_ctx;
pointer = (uint8_t *)srtp_crypto_alloc(sizeof(HMAC_CTX) +
sizeof(srtp_auth_t));
2017-06-26 17:11:02 +00:00
if (pointer == NULL) {
return srtp_err_status_alloc_fail;
}
*a = (srtp_auth_t *)pointer;
2017-06-26 17:11:02 +00:00
(*a)->state = pointer + sizeof(srtp_auth_t);
new_hmac_ctx = (HMAC_CTX *)((*a)->state);
2017-06-26 17:11:02 +00:00
HMAC_CTX_init(new_hmac_ctx);
}
#else
*a = (srtp_auth_t *)srtp_crypto_alloc(sizeof(srtp_auth_t));
2017-06-26 17:11:02 +00:00
if (*a == NULL) {
return srtp_err_status_alloc_fail;
2014-02-24 14:20:03 +00:00
}
2017-06-26 17:11:02 +00:00
(*a)->state = HMAC_CTX_new();
if ((*a)->state == NULL) {
srtp_crypto_free(*a);
*a = NULL;
return srtp_err_status_alloc_fail;
2014-02-24 14:20:03 +00:00
}
2017-06-26 17:11:02 +00:00
#endif
2014-02-24 14:20:03 +00:00
/* set pointers */
2017-06-26 17:11:02 +00:00
(*a)->type = &srtp_hmac;
2014-02-24 14:20:03 +00:00
(*a)->out_len = out_len;
(*a)->key_len = key_len;
(*a)->prefix_len = 0;
2017-06-26 17:11:02 +00:00
return srtp_err_status_ok;
2014-02-24 14:20:03 +00:00
}
static srtp_err_status_t srtp_hmac_dealloc(srtp_auth_t *a)
2014-02-24 14:20:03 +00:00
{
2017-06-26 17:11:02 +00:00
HMAC_CTX *hmac_ctx;
2014-02-24 14:20:03 +00:00
hmac_ctx = (HMAC_CTX *)a->state;
2017-06-26 17:11:02 +00:00
#if OPENSSL_VERSION_NUMBER < 0x10100000L || LIBRESSL_VERSION_NUMBER
2017-06-26 17:11:02 +00:00
HMAC_CTX_cleanup(hmac_ctx);
2014-02-24 14:20:03 +00:00
/* zeroize entire state*/
2017-06-26 17:11:02 +00:00
octet_string_set_to_zero(a, sizeof(HMAC_CTX) + sizeof(srtp_auth_t));
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
#else
HMAC_CTX_free(hmac_ctx);
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
/* zeroize entire state*/
octet_string_set_to_zero(a, sizeof(srtp_auth_t));
#endif
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
/* free memory */
srtp_crypto_free(a);
return srtp_err_status_ok;
2014-02-24 14:20:03 +00:00
}
static srtp_err_status_t srtp_hmac_start(void *statev)
2014-02-24 14:20:03 +00:00
{
2017-06-26 17:11:02 +00:00
HMAC_CTX *state = (HMAC_CTX *)statev;
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
if (HMAC_Init_ex(state, NULL, 0, NULL, NULL) == 0)
return srtp_err_status_auth_fail;
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
return srtp_err_status_ok;
2014-02-24 14:20:03 +00:00
}
static srtp_err_status_t srtp_hmac_init(void *statev,
const uint8_t *key,
int key_len)
2014-02-24 14:20:03 +00:00
{
2017-06-26 17:11:02 +00:00
HMAC_CTX *state = (HMAC_CTX *)statev;
if (HMAC_Init_ex(state, key, key_len, EVP_sha1(), NULL) == 0)
return srtp_err_status_auth_fail;
return srtp_err_status_ok;
2014-02-24 14:20:03 +00:00
}
static srtp_err_status_t srtp_hmac_update(void *statev,
const uint8_t *message,
int msg_octets)
2014-02-24 14:20:03 +00:00
{
2017-06-26 17:11:02 +00:00
HMAC_CTX *state = (HMAC_CTX *)statev;
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
debug_print(srtp_mod_hmac, "input: %s",
srtp_octet_string_hex_string(message, msg_octets));
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
if (HMAC_Update(state, message, msg_octets) == 0)
return srtp_err_status_auth_fail;
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
return srtp_err_status_ok;
2014-02-24 14:20:03 +00:00
}
static srtp_err_status_t srtp_hmac_compute(void *statev,
const uint8_t *message,
int msg_octets,
int tag_len,
uint8_t *result)
2014-02-24 14:20:03 +00:00
{
2017-06-26 17:11:02 +00:00
HMAC_CTX *state = (HMAC_CTX *)statev;
uint8_t hash_value[SHA1_DIGEST_SIZE];
2014-02-24 14:20:03 +00:00
int i;
2017-06-26 17:11:02 +00:00
unsigned int len;
2014-02-24 14:20:03 +00:00
2021-08-28 11:19:05 +00:00
debug_print(srtp_mod_hmac, "input: %s",
srtp_octet_string_hex_string(message, msg_octets));
2014-02-24 14:20:03 +00:00
/* check tag length, return error if we can't provide the value expected */
2017-06-26 17:11:02 +00:00
if (tag_len > SHA1_DIGEST_SIZE) {
return srtp_err_status_bad_param;
2014-02-24 14:20:03 +00:00
}
/* hash message, copy output into H */
2017-06-26 17:11:02 +00:00
if (HMAC_Update(state, message, msg_octets) == 0)
return srtp_err_status_auth_fail;
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
if (HMAC_Final(state, hash_value, &len) == 0)
return srtp_err_status_auth_fail;
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
if (len < tag_len)
return srtp_err_status_auth_fail;
2014-02-24 14:20:03 +00:00
/* copy hash_value to *result */
for (i = 0; i < tag_len; i++) {
2017-06-26 17:11:02 +00:00
result[i] = hash_value[i];
2014-02-24 14:20:03 +00:00
}
2017-06-26 17:11:02 +00:00
debug_print(srtp_mod_hmac, "output: %s",
srtp_octet_string_hex_string(hash_value, tag_len));
2014-02-24 14:20:03 +00:00
2017-06-26 17:11:02 +00:00
return srtp_err_status_ok;
2014-02-24 14:20:03 +00:00
}
static const char srtp_hmac_description[] =
"hmac sha-1 authentication function";
2014-02-24 14:20:03 +00:00
/*
2017-06-26 17:11:02 +00:00
* srtp_auth_type_t hmac is the hmac metaobject
2014-02-24 14:20:03 +00:00
*/
const srtp_auth_type_t srtp_hmac = {
srtp_hmac_alloc, /* */
srtp_hmac_dealloc, /* */
srtp_hmac_init, /* */
srtp_hmac_compute, /* */
srtp_hmac_update, /* */
srtp_hmac_start, /* */
srtp_hmac_description, /* */
&srtp_hmac_test_case_0, /* */
SRTP_HMAC_SHA1 /* */
2014-02-24 14:20:03 +00:00
};