Blame lib/fingerprint.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2001-2012 Free Software Foundation, 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
#include "gnutls_int.h"
Packit 549fdc
#include <auth/srp_kx.h>
Packit 549fdc
#include <auth/anon.h>
Packit 549fdc
#include <auth/cert.h>
Packit 549fdc
#include <auth/psk.h>
Packit 549fdc
#include "errors.h"
Packit 549fdc
#include <auth.h>
Packit 549fdc
#include <state.h>
Packit 549fdc
#include <datum.h>
Packit 549fdc
#include <algorithms.h>
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_fingerprint:
Packit 549fdc
 * @algo: is a digest algorithm
Packit 549fdc
 * @data: is the data
Packit 549fdc
 * @result: is the place where the result will be copied (may be null).
Packit 549fdc
 * @result_size: should hold the size of the result. The actual size
Packit 549fdc
 * of the returned result will also be copied there.
Packit 549fdc
 *
Packit 549fdc
 * This function will calculate a fingerprint (actually a hash), of
Packit 549fdc
 * the given data.  The result is not printable data.  You should
Packit 549fdc
 * convert it to hex, or to something else printable.
Packit 549fdc
 *
Packit 549fdc
 * This is the usual way to calculate a fingerprint of an X.509 DER
Packit 549fdc
 * encoded certificate.  Note however that the fingerprint of an
Packit 549fdc
 * OpenPGP certificate is not just a hash and cannot be calculated with this
Packit 549fdc
 * function.
Packit 549fdc
 *
Packit 549fdc
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise
Packit 549fdc
 *   an error code is returned.
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_fingerprint(gnutls_digest_algorithm_t algo,
Packit 549fdc
		   const gnutls_datum_t * data, void *result,
Packit 549fdc
		   size_t * result_size)
Packit 549fdc
{
Packit 549fdc
	int ret;
Packit 549fdc
	int hash_len = _gnutls_hash_get_algo_len(hash_to_entry(algo));
Packit 549fdc
Packit 549fdc
	if (hash_len < 0 || (unsigned) hash_len > *result_size
Packit 549fdc
	    || result == NULL) {
Packit 549fdc
		*result_size = hash_len;
Packit 549fdc
		return GNUTLS_E_SHORT_MEMORY_BUFFER;
Packit 549fdc
	}
Packit 549fdc
	*result_size = hash_len;
Packit 549fdc
Packit 549fdc
	if (result) {
Packit 549fdc
		ret =
Packit 549fdc
		    _gnutls_hash_fast(algo, data->data, data->size,
Packit 549fdc
				      result);
Packit 549fdc
		if (ret < 0)
Packit 549fdc
			return gnutls_assert_val(ret);
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc