Blame lib/prf.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2002-2015 Free Software Foundation, Inc.
Packit 549fdc
 * Copyright (C) 2014-2015 Nikos Mavrogiannopoulos
Packit 549fdc
 * Copyright (C) 2016-2017 Red Hat, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * The GnuTLS is free software; you can redistribute it and/or
Packit 549fdc
 * modify it under the terms of the GNU Lesser General Public License
Packit 549fdc
 * as published by the Free Software Foundation; either version 2.1 of
Packit 549fdc
 * the License, or (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * This library is distributed in the hope that it will be useful, but
Packit 549fdc
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 549fdc
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 549fdc
 * Lesser General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU Lesser General Public License
Packit 549fdc
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
/* Functions for the TLS PRF handling.
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include "errors.h"
Packit 549fdc
#include <num.h>
Packit 549fdc
#include <state.h>
Packit 549fdc
#include <algorithms.h>
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_prf_raw:
Packit 549fdc
 * @session: is a #gnutls_session_t type.
Packit 549fdc
 * @label_size: length of the @label variable.
Packit 549fdc
 * @label: label used in PRF computation, typically a short string.
Packit 549fdc
 * @seed_size: length of the @seed variable.
Packit 549fdc
 * @seed: optional extra data to seed the PRF with.
Packit 549fdc
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit 549fdc
 * @out: pre-allocated buffer to hold the generated data.
Packit 549fdc
 *
Packit 549fdc
 * Apply the TLS Pseudo-Random-Function (PRF) on the master secret
Packit 549fdc
 * and the provided data.
Packit 549fdc
 *
Packit 549fdc
 * The @label variable usually contains a string denoting the purpose
Packit 549fdc
 * for the generated data.  The @seed usually contains data such as the
Packit 549fdc
 * client and server random, perhaps together with some additional
Packit 549fdc
 * data that is added to guarantee uniqueness of the output for a
Packit 549fdc
 * particular purpose.
Packit 549fdc
 *
Packit 549fdc
 * Because the output is not guaranteed to be unique for a particular
Packit 549fdc
 * session unless @seed includes the client random and server random
Packit 549fdc
 * fields (the PRF would output the same data on another connection
Packit 549fdc
 * resumed from the first one), it is not recommended to use this
Packit 549fdc
 * function directly.  The gnutls_prf() function seeds the PRF with the
Packit 549fdc
 * client and server random fields directly, and is recommended if you
Packit 549fdc
 * want to generate pseudo random data unique for each session.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_prf_raw(gnutls_session_t session,
Packit 549fdc
	       size_t label_size,
Packit 549fdc
	       const char *label,
Packit 549fdc
	       size_t seed_size, const char *seed, size_t outsize,
Packit 549fdc
	       char *out)
Packit 549fdc
{
Packit 549fdc
	int ret;
Packit 549fdc
Packit 549fdc
	ret = _gnutls_prf_raw(session->security_parameters.prf_mac,
Packit 549fdc
			  GNUTLS_MASTER_SIZE, session->security_parameters.master_secret,
Packit 549fdc
			  label_size, label,
Packit 549fdc
			  seed_size, (uint8_t *) seed,
Packit 549fdc
			  outsize, out);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_prf_rfc5705:
Packit 549fdc
 * @session: is a #gnutls_session_t type.
Packit 549fdc
 * @label_size: length of the @label variable.
Packit 549fdc
 * @label: label used in PRF computation, typically a short string.
Packit 549fdc
 * @context_size: length of the @extra variable.
Packit 549fdc
 * @context: optional extra data to seed the PRF with.
Packit 549fdc
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit 549fdc
 * @out: pre-allocated buffer to hold the generated data.
Packit 549fdc
 *
Packit 549fdc
 * Applies the TLS Pseudo-Random-Function (PRF) on the master secret
Packit 549fdc
 * and the provided data, seeded with the client and server random fields,
Packit 549fdc
 * as specified in RFC5705.
Packit 549fdc
 *
Packit 549fdc
 * The @label variable usually contains a string denoting the purpose
Packit 549fdc
 * for the generated data.  The @server_random_first indicates whether
Packit 549fdc
 * the client random field or the server random field should be first
Packit 549fdc
 * in the seed.  Non-zero indicates that the server random field is first,
Packit 549fdc
 * 0 that the client random field is first.
Packit 549fdc
 *
Packit 549fdc
 * The @context variable can be used to add more data to the seed, after
Packit 549fdc
 * the random variables.  It can be used to make sure the
Packit 549fdc
 * generated output is strongly connected to some additional data
Packit 549fdc
 * (e.g., a string used in user authentication). 
Packit 549fdc
 *
Packit 549fdc
 * The output is placed in @out, which must be pre-allocated.
Packit 549fdc
 *
Packit 549fdc
 * Note that, to provide the RFC5705 context, the @context variable
Packit 549fdc
 * must be non-null.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.4.4
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_prf_rfc5705(gnutls_session_t session,
Packit 549fdc
		   size_t label_size, const char *label,
Packit 549fdc
		   size_t context_size, const char *context,
Packit 549fdc
		   size_t outsize, char *out)
Packit 549fdc
{
Packit 549fdc
	char *pctx = NULL;
Packit 549fdc
	int ret;
Packit 549fdc
Packit 549fdc
	if (context != NULL && context_size > 65535)  {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_INVALID_REQUEST;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	if (context != NULL) {
Packit 549fdc
		pctx = gnutls_malloc(context_size+2);
Packit 549fdc
		if (!pctx) {
Packit 549fdc
			gnutls_assert();
Packit 549fdc
			return GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
		}
Packit 549fdc
Packit 549fdc
		memcpy(pctx+2, context, context_size);
Packit 549fdc
		_gnutls_write_uint16(context_size, (void*)pctx);
Packit 549fdc
		context_size += 2;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	ret = gnutls_prf(session, label_size, label, 0,
Packit 549fdc
			 context_size, pctx, outsize, out);
Packit 549fdc
Packit 549fdc
	gnutls_free(pctx);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_prf:
Packit 549fdc
 * @session: is a #gnutls_session_t type.
Packit 549fdc
 * @label_size: length of the @label variable.
Packit 549fdc
 * @label: label used in PRF computation, typically a short string.
Packit 549fdc
 * @server_random_first: non-zero if server random field should be first in seed
Packit 549fdc
 * @extra_size: length of the @extra variable.
Packit 549fdc
 * @extra: optional extra data to seed the PRF with.
Packit 549fdc
 * @outsize: size of pre-allocated output buffer to hold the output.
Packit 549fdc
 * @out: pre-allocated buffer to hold the generated data.
Packit 549fdc
 *
Packit 549fdc
 * Applies the TLS Pseudo-Random-Function (PRF) on the master secret
Packit 549fdc
 * and the provided data, seeded with the client and server random fields.
Packit 549fdc
 * For the key expansion specified in RFC5705 see gnutls_prf_rfc5705().
Packit 549fdc
 *
Packit 549fdc
 * The @label variable usually contains a string denoting the purpose
Packit 549fdc
 * for the generated data.  The @server_random_first indicates whether
Packit 549fdc
 * the client random field or the server random field should be first
Packit 549fdc
 * in the seed.  Non-zero indicates that the server random field is first,
Packit 549fdc
 * 0 that the client random field is first.
Packit 549fdc
 *
Packit 549fdc
 * The @extra variable can be used to add more data to the seed, after
Packit 549fdc
 * the random variables.  It can be used to make sure the
Packit 549fdc
 * generated output is strongly connected to some additional data
Packit 549fdc
 * (e.g., a string used in user authentication).
Packit 549fdc
 *
Packit 549fdc
 * The output is placed in @out, which must be pre-allocated.
Packit 549fdc
 *
Packit 549fdc
 * Note: This function produces identical output with gnutls_prf_rfc5705()
Packit 549fdc
 * when @server_random_first is set to 0 and @extra is %NULL.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, or an error code.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_prf(gnutls_session_t session,
Packit 549fdc
	   size_t label_size,
Packit 549fdc
	   const char *label,
Packit 549fdc
	   int server_random_first,
Packit 549fdc
	   size_t extra_size, const char *extra,
Packit 549fdc
	   size_t outsize, char *out)
Packit 549fdc
{
Packit 549fdc
	int ret;
Packit 549fdc
	uint8_t *seed;
Packit 549fdc
	size_t seedsize = 2 * GNUTLS_RANDOM_SIZE + extra_size;
Packit 549fdc
Packit 549fdc
	seed = gnutls_malloc(seedsize);
Packit 549fdc
	if (!seed) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		return GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	memcpy(seed, server_random_first ?
Packit 549fdc
	       session->security_parameters.server_random :
Packit 549fdc
	       session->security_parameters.client_random,
Packit 549fdc
	       GNUTLS_RANDOM_SIZE);
Packit 549fdc
	memcpy(seed + GNUTLS_RANDOM_SIZE,
Packit 549fdc
	       server_random_first ? session->security_parameters.
Packit 549fdc
	       client_random : session->security_parameters.server_random,
Packit 549fdc
	       GNUTLS_RANDOM_SIZE);
Packit 549fdc
Packit 549fdc
	if (extra && extra_size) {
Packit 549fdc
		memcpy(seed + 2 * GNUTLS_RANDOM_SIZE, extra, extra_size);
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	ret =
Packit 549fdc
	    _gnutls_prf_raw(session->security_parameters.prf_mac,
Packit 549fdc
			GNUTLS_MASTER_SIZE, session->security_parameters.master_secret,
Packit 549fdc
			label_size, label,
Packit 549fdc
			seedsize, seed,
Packit 549fdc
			outsize, out);
Packit 549fdc
Packit 549fdc
	gnutls_free(seed);
Packit 549fdc
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc