Blame lib/nettle/prf.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2017 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GNUTLS.
Packit aea12f
 *
Packit aea12f
 * The GNUTLS library is free software; you can redistribute it and/or
Packit aea12f
 * modify it under the terms of the GNU Lesser General Public License
Packit aea12f
 * as published by the Free Software Foundation; either version 2.1 of
Packit aea12f
 * the License, or (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * This library is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * Lesser General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU Lesser General Public License
Packit aea12f
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
Packit aea12f
 *
Packit aea12f
 */
Packit aea12f
Packit aea12f
#include <gnutls_int.h>
Packit aea12f
#include "int/tls1-prf.h"
Packit aea12f
#include <nettle/hmac.h>
Packit Service 991b93
#if ENABLE_GOST
Packit Service 991b93
#include "gost/hmac-gost.h"
Packit Service 991b93
#endif
Packit aea12f
Packit aea12f
/*-
Packit aea12f
 * _gnutls_prf_raw:
Packit aea12f
 * @mac: the MAC algorithm to use, set to %GNUTLS_MAC_MD5_SHA1 for the TLS1.0 mac
Packit aea12f
 * @master_size: length of the @master variable.
Packit aea12f
 * @master: the master secret used in PRF computation
Packit aea12f
 * @label_size: length of the @label variable.
Packit aea12f
 * @label: label used in PRF computation, typically a short string.
Packit aea12f
 * @seed_size: length of the @seed variable.
Packit aea12f
 * @seed: optional extra data to seed the PRF with.
Packit aea12f
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit aea12f
 * @out: pre-allocated buffer to hold the generated data.
Packit aea12f
 *
Packit aea12f
 * Apply the TLS Pseudo-Random-Function (PRF) on the master secret
Packit aea12f
 * and the provided data.
Packit aea12f
 *
Packit aea12f
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit aea12f
 -*/
Packit aea12f
int
Packit aea12f
_gnutls_prf_raw(gnutls_mac_algorithm_t mac,
Packit aea12f
		size_t master_size, const void *master,
Packit aea12f
		size_t label_size, const char *label,
Packit aea12f
		size_t seed_size, const uint8_t *seed, size_t outsize, char *out)
Packit aea12f
{
Packit aea12f
	int ret;
Packit aea12f
Packit aea12f
	switch (mac) {
Packit aea12f
	case GNUTLS_MAC_MD5_SHA1:
Packit aea12f
		tls10_prf(master_size, (uint8_t*)master, label_size, label,
Packit aea12f
			  seed_size, seed, outsize, (uint8_t*)out);
Packit aea12f
		return 0;
Packit aea12f
	case GNUTLS_MAC_SHA256:{
Packit aea12f
		struct hmac_sha256_ctx ctx;
Packit aea12f
		hmac_sha256_set_key(&ctx, master_size, (uint8_t*)master);
Packit aea12f
Packit aea12f
		ret = tls12_prf(&ctx,
Packit aea12f
			  (nettle_hash_update_func *)
Packit aea12f
			  hmac_sha256_update,
Packit aea12f
			  (nettle_hash_digest_func *)
Packit aea12f
			  hmac_sha256_digest, SHA256_DIGEST_SIZE,
Packit aea12f
			  label_size, label, seed_size,
Packit aea12f
			  seed, outsize,
Packit aea12f
			  (uint8_t*)out);
Packit aea12f
Packit aea12f
		if (unlikely(ret != 1))
Packit aea12f
			return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
Packit aea12f
		break;
Packit aea12f
	}
Packit aea12f
	case GNUTLS_MAC_SHA384:{
Packit aea12f
		struct hmac_sha384_ctx ctx;
Packit aea12f
		hmac_sha384_set_key(&ctx, master_size, master);
Packit aea12f
Packit aea12f
		ret = tls12_prf(&ctx,
Packit aea12f
			  (nettle_hash_update_func *)
Packit aea12f
			  hmac_sha384_update,
Packit aea12f
			  (nettle_hash_digest_func *)
Packit aea12f
			  hmac_sha384_digest, SHA384_DIGEST_SIZE,
Packit aea12f
			  label_size, label, seed_size,
Packit aea12f
			  seed, outsize,
Packit aea12f
			  (uint8_t*)out);
Packit aea12f
Packit aea12f
		if (unlikely(ret != 1))
Packit aea12f
			return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
Packit aea12f
		break;
Packit aea12f
	}
Packit Service 991b93
#if ENABLE_GOST
Packit Service 991b93
	case GNUTLS_MAC_STREEBOG_256:{
Packit Service 991b93
		struct hmac_streebog256_ctx ctx;
Packit Service 991b93
		hmac_streebog256_set_key(&ctx, master_size, master);
Packit Service 991b93
Packit Service 991b93
		ret = tls12_prf(&ctx,
Packit Service 991b93
			  (nettle_hash_update_func *)
Packit Service 991b93
			  hmac_streebog256_update,
Packit Service 991b93
			  (nettle_hash_digest_func *)
Packit Service 991b93
			  hmac_streebog256_digest, STREEBOG256_DIGEST_SIZE,
Packit Service 991b93
			  label_size, label, seed_size,
Packit Service 991b93
			  seed, outsize,
Packit Service 991b93
			  (uint8_t*)out);
Packit Service 991b93
Packit Service 991b93
		if (unlikely(ret != 1))
Packit Service 991b93
			return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
Packit Service 991b93
		break;
Packit Service 991b93
	}
Packit Service 991b93
	case GNUTLS_MAC_STREEBOG_512:{
Packit Service 991b93
		struct hmac_streebog512_ctx ctx;
Packit Service 991b93
		hmac_streebog512_set_key(&ctx, master_size, master);
Packit Service 991b93
Packit Service 991b93
		ret = tls12_prf(&ctx,
Packit Service 991b93
			  (nettle_hash_update_func *)
Packit Service 991b93
			  hmac_streebog512_update,
Packit Service 991b93
			  (nettle_hash_digest_func *)
Packit Service 991b93
			  hmac_streebog512_digest, STREEBOG512_DIGEST_SIZE,
Packit Service 991b93
			  label_size, label, seed_size,
Packit Service 991b93
			  seed, outsize,
Packit Service 991b93
			  (uint8_t*)out);
Packit Service 991b93
Packit Service 991b93
		if (unlikely(ret != 1))
Packit Service 991b93
			return gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);
Packit Service 991b93
		break;
Packit Service 991b93
	}
Packit Service 991b93
#endif
Packit aea12f
	default:
Packit aea12f
		gnutls_assert();
Packit aea12f
		_gnutls_debug_log("unhandled PRF %s\n",
Packit aea12f
				  gnutls_mac_get_name(mac));
Packit aea12f
		return GNUTLS_E_INVALID_REQUEST;
Packit aea12f
Packit aea12f
	}
Packit aea12f
Packit aea12f
	return 0;
Packit aea12f
}