Blame lib/crypto-backend.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2008-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 "errors.h"
Packit 549fdc
#include "gnutls_int.h"
Packit 549fdc
#include <gnutls/crypto.h>
Packit 549fdc
#include <crypto-backend.h>
Packit 549fdc
#include <crypto.h>
Packit 549fdc
#include <mpi.h>
Packit 549fdc
#include <pk.h>
Packit 549fdc
#include <random.h>
Packit 549fdc
#include <cipher_int.h>
Packit 549fdc
Packit 549fdc
/* default values for priorities */
Packit 549fdc
int crypto_mac_prio = INT_MAX;
Packit 549fdc
int crypto_digest_prio = INT_MAX;
Packit 549fdc
int crypto_cipher_prio = INT_MAX;
Packit 549fdc
Packit 549fdc
typedef struct algo_list {
Packit 549fdc
	int algorithm;
Packit 549fdc
	int priority;
Packit 549fdc
	void *alg_data;
Packit 549fdc
	int free_alg_data;
Packit 549fdc
	struct algo_list *next;
Packit 549fdc
} algo_list;
Packit 549fdc
Packit 549fdc
#define cipher_list algo_list
Packit 549fdc
#define mac_list algo_list
Packit 549fdc
#define digest_list algo_list
Packit 549fdc
Packit 549fdc
static int
Packit 549fdc
_algo_register(algo_list * al, int algorithm, int priority, void *s, int free_s)
Packit 549fdc
{
Packit 549fdc
	algo_list *cl;
Packit 549fdc
	algo_list *last_cl = al;
Packit 549fdc
	int ret;
Packit 549fdc
Packit 549fdc
	if (al == NULL) {
Packit 549fdc
		ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
Packit 549fdc
		goto cleanup;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	/* look if there is any cipher with lowest priority. In that case do not add.
Packit 549fdc
	 */
Packit 549fdc
	cl = al;
Packit 549fdc
	while (cl && cl->alg_data) {
Packit 549fdc
		if (cl->algorithm == algorithm) {
Packit 549fdc
			if (cl->priority < priority) {
Packit 549fdc
				gnutls_assert();
Packit 549fdc
				ret = GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
Packit 549fdc
				goto cleanup;
Packit 549fdc
			} else {
Packit 549fdc
				/* the current has higher priority -> overwrite */
Packit 549fdc
				cl->algorithm = algorithm;
Packit 549fdc
				cl->priority = priority;
Packit 549fdc
				cl->alg_data = s;
Packit 549fdc
				cl->free_alg_data = free_s;
Packit 549fdc
				return 0;
Packit 549fdc
			}
Packit 549fdc
		}
Packit 549fdc
		cl = cl->next;
Packit 549fdc
		if (cl)
Packit 549fdc
			last_cl = cl;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	cl = gnutls_calloc(1, sizeof(cipher_list));
Packit 549fdc
Packit 549fdc
	if (cl == NULL) {
Packit 549fdc
		gnutls_assert();
Packit 549fdc
		ret = GNUTLS_E_MEMORY_ERROR;
Packit 549fdc
		goto cleanup;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	last_cl->algorithm = algorithm;
Packit 549fdc
	last_cl->priority = priority;
Packit 549fdc
	last_cl->alg_data = s;
Packit 549fdc
	last_cl->free_alg_data = free_s;
Packit 549fdc
	last_cl->next = cl;
Packit 549fdc
Packit 549fdc
	return 0;
Packit 549fdc
 cleanup:
Packit 549fdc
	if (free_s) gnutls_free(s);
Packit 549fdc
	return ret;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static const void *_get_algo(algo_list * al, int algo)
Packit 549fdc
{
Packit 549fdc
	cipher_list *cl;
Packit 549fdc
Packit 549fdc
	/* look if there is any cipher with lowest priority. In that case do not add.
Packit 549fdc
	 */
Packit 549fdc
	cl = al;
Packit 549fdc
	while (cl && cl->alg_data) {
Packit 549fdc
		if (cl->algorithm == algo) {
Packit 549fdc
			return cl->alg_data;
Packit 549fdc
		}
Packit 549fdc
		cl = cl->next;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return NULL;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static cipher_list glob_cl = { GNUTLS_CIPHER_NULL, 0, NULL, 0, NULL };
Packit 549fdc
static mac_list glob_ml = { GNUTLS_MAC_NULL, 0, NULL, 0, NULL };
Packit 549fdc
static digest_list glob_dl = { GNUTLS_MAC_NULL, 0, NULL, 0, NULL };
Packit 549fdc
Packit 549fdc
static void _deregister(algo_list * cl)
Packit 549fdc
{
Packit 549fdc
	algo_list *next;
Packit 549fdc
Packit 549fdc
	next = cl->next;
Packit 549fdc
	cl->next = NULL;
Packit 549fdc
	cl = next;
Packit 549fdc
Packit 549fdc
	while (cl) {
Packit 549fdc
		next = cl->next;
Packit 549fdc
		if (cl->free_alg_data)
Packit 549fdc
			gnutls_free(cl->alg_data);
Packit 549fdc
		gnutls_free(cl);
Packit 549fdc
		cl = next;
Packit 549fdc
	}
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
void _gnutls_crypto_deregister(void)
Packit 549fdc
{
Packit 549fdc
	_deregister(&glob_cl);
Packit 549fdc
	_deregister(&glob_ml);
Packit 549fdc
	_deregister(&glob_dl);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/*-
Packit 549fdc
 * gnutls_crypto_single_cipher_register:
Packit 549fdc
 * @algorithm: is the gnutls algorithm identifier
Packit 549fdc
 * @priority: is the priority of the algorithm
Packit 549fdc
 * @s: is a structure holding new cipher's data
Packit 549fdc
 *
Packit 549fdc
 * This function will register a cipher algorithm to be used by
Packit 549fdc
 * gnutls.  Any algorithm registered will override the included
Packit 549fdc
 * algorithms and by convention kernel implemented algorithms have
Packit 549fdc
 * priority of 90 and CPU-assisted of 80.  The algorithm with the lowest priority will be
Packit 549fdc
 * used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * In the case the registered init or setkey functions return %GNUTLS_E_NEED_FALLBACK,
Packit 549fdc
 * GnuTLS will attempt to use the next in priority registered cipher.
Packit 549fdc
 *
Packit 549fdc
 * This function should be called before gnutls_global_init().
Packit 549fdc
 *
Packit 549fdc
 * For simplicity you can use the convenience
Packit 549fdc
 * gnutls_crypto_single_cipher_register() macro.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 2.6.0
Packit 549fdc
 -*/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_single_cipher_register(gnutls_cipher_algorithm_t algorithm,
Packit 549fdc
				     int priority,
Packit 549fdc
				     const gnutls_crypto_cipher_st * s,
Packit 549fdc
				     int free_s)
Packit 549fdc
{
Packit 549fdc
	/* we override const in case free_s is set */
Packit 549fdc
	return _algo_register(&glob_cl, algorithm, priority, (void*)s, free_s);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
const gnutls_crypto_cipher_st
Packit 549fdc
    *_gnutls_get_crypto_cipher(gnutls_cipher_algorithm_t algo)
Packit 549fdc
{
Packit 549fdc
	return _get_algo(&glob_cl, algo);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_crypto_register_cipher:
Packit 549fdc
 * @algorithm: is the gnutls algorithm identifier
Packit 549fdc
 * @priority: is the priority of the algorithm
Packit 549fdc
 * @init: A function which initializes the cipher
Packit 549fdc
 * @setkey: A function which sets the key of the cipher
Packit 549fdc
 * @setiv: A function which sets the nonce/IV of the cipher (non-AEAD)
Packit 549fdc
 * @encrypt: A function which performs encryption (non-AEAD)
Packit 549fdc
 * @decrypt: A function which performs decryption (non-AEAD)
Packit 549fdc
 * @deinit: A function which deinitializes the cipher
Packit 549fdc
 *
Packit 549fdc
 * This function will register a cipher algorithm to be used by
Packit 549fdc
 * gnutls.  Any algorithm registered will override the included
Packit 549fdc
 * algorithms and by convention kernel implemented algorithms have
Packit 549fdc
 * priority of 90 and CPU-assisted of 80.  The algorithm with the lowest priority will be
Packit 549fdc
 * used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * In the case the registered init or setkey functions return %GNUTLS_E_NEED_FALLBACK,
Packit 549fdc
 * GnuTLS will attempt to use the next in priority registered cipher.
Packit 549fdc
 *
Packit 549fdc
 * The functions which are marked as non-AEAD they are not required when
Packit 549fdc
 * registering a cipher to be used with the new AEAD API introduced in
Packit 549fdc
 * GnuTLS 3.4.0. Internally GnuTLS uses the new AEAD API.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.4.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_register_cipher(gnutls_cipher_algorithm_t algorithm,
Packit 549fdc
			      int priority,
Packit 549fdc
			      gnutls_cipher_init_func init,
Packit 549fdc
			      gnutls_cipher_setkey_func setkey,
Packit 549fdc
			      gnutls_cipher_setiv_func setiv,
Packit 549fdc
			      gnutls_cipher_encrypt_func encrypt,
Packit 549fdc
			      gnutls_cipher_decrypt_func decrypt,
Packit 549fdc
			      gnutls_cipher_deinit_func deinit)
Packit 549fdc
{
Packit 549fdc
	gnutls_crypto_cipher_st *s = gnutls_calloc(1, sizeof(gnutls_crypto_cipher_st));
Packit 549fdc
	if (s == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
Packit 549fdc
Packit 549fdc
	s->init = init;
Packit 549fdc
	s->setkey = setkey;
Packit 549fdc
	s->setiv = setiv;
Packit 549fdc
	s->encrypt = encrypt;
Packit 549fdc
	s->decrypt = decrypt;
Packit 549fdc
	s->deinit = deinit;
Packit 549fdc
Packit 549fdc
	return gnutls_crypto_single_cipher_register(algorithm, priority, s, 1);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_crypto_register_aead_cipher:
Packit 549fdc
 * @algorithm: is the gnutls AEAD cipher identifier
Packit 549fdc
 * @priority: is the priority of the algorithm
Packit 549fdc
 * @init: A function which initializes the cipher
Packit 549fdc
 * @setkey: A function which sets the key of the cipher
Packit 549fdc
 * @aead_encrypt: Perform the AEAD encryption
Packit 549fdc
 * @aead_decrypt: Perform the AEAD decryption
Packit 549fdc
 * @deinit: A function which deinitializes the cipher
Packit 549fdc
 *
Packit 549fdc
 * This function will register a cipher algorithm to be used by
Packit 549fdc
 * gnutls.  Any algorithm registered will override the included
Packit 549fdc
 * algorithms and by convention kernel implemented algorithms have
Packit 549fdc
 * priority of 90 and CPU-assisted of 80.  The algorithm with the lowest priority will be
Packit 549fdc
 * used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * In the case the registered init or setkey functions return %GNUTLS_E_NEED_FALLBACK,
Packit 549fdc
 * GnuTLS will attempt to use the next in priority registered cipher.
Packit 549fdc
 *
Packit 549fdc
 * The functions registered will be used with the new AEAD API introduced in
Packit 549fdc
 * GnuTLS 3.4.0. Internally GnuTLS uses the new AEAD API.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.4.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_register_aead_cipher(gnutls_cipher_algorithm_t algorithm,
Packit 549fdc
			      int priority,
Packit 549fdc
			      gnutls_cipher_init_func init,
Packit 549fdc
			      gnutls_cipher_setkey_func setkey,
Packit 549fdc
			      gnutls_cipher_aead_encrypt_func aead_encrypt,
Packit 549fdc
			      gnutls_cipher_aead_decrypt_func aead_decrypt,
Packit 549fdc
			      gnutls_cipher_deinit_func deinit)
Packit 549fdc
{
Packit 549fdc
	gnutls_crypto_cipher_st *s = gnutls_calloc(1, sizeof(gnutls_crypto_cipher_st));
Packit 549fdc
	if (s == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
Packit 549fdc
Packit 549fdc
	s->init = init;
Packit 549fdc
	s->setkey = setkey;
Packit 549fdc
	s->aead_encrypt = aead_encrypt;
Packit 549fdc
	s->aead_decrypt = aead_decrypt;
Packit 549fdc
	s->deinit = deinit;
Packit 549fdc
Packit 549fdc
	return gnutls_crypto_single_cipher_register(algorithm, priority, s, 1);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/*-
Packit 549fdc
 * gnutls_crypto_rnd_register:
Packit 549fdc
 * @priority: is the priority of the generator
Packit 549fdc
 * @s: is a structure holding new generator's data
Packit 549fdc
 *
Packit 549fdc
 * This function will register a random generator to be used by
Packit 549fdc
 * gnutls.  Any generator registered will override the included
Packit 549fdc
 * generator and by convention kernel implemented generators have
Packit 549fdc
 * priority of 90 and CPU-assisted of 80. The generator with the lowest priority will be
Packit 549fdc
 * used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * This function should be called before gnutls_global_init().
Packit 549fdc
 *
Packit 549fdc
 * For simplicity you can use the convenience
Packit 549fdc
 * gnutls_crypto_rnd_register() macro.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 2.6.0
Packit 549fdc
 -*/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_rnd_register(int priority, const gnutls_crypto_rnd_st * s)
Packit 549fdc
{
Packit 549fdc
	if (crypto_rnd_prio >= priority) {
Packit 549fdc
		memcpy(&_gnutls_rnd_ops, s, sizeof(*s));
Packit 549fdc
		crypto_rnd_prio = priority;
Packit 549fdc
		return 0;
Packit 549fdc
	}
Packit 549fdc
Packit 549fdc
	return GNUTLS_E_CRYPTO_ALREADY_REGISTERED;
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/*-
Packit 549fdc
 * gnutls_crypto_single_mac_register:
Packit 549fdc
 * @algorithm: is the gnutls algorithm identifier
Packit 549fdc
 * @priority: is the priority of the algorithm
Packit 549fdc
 * @s: is a structure holding new algorithms's data
Packit 549fdc
 *
Packit 549fdc
 * This function will register a MAC algorithm to be used by gnutls.
Packit 549fdc
 * Any algorithm registered will override the included algorithms and
Packit 549fdc
 * by convention kernel implemented algorithms have priority of 90
Packit 549fdc
 *  and CPU-assisted of 80.
Packit 549fdc
 * The algorithm with the lowest priority will be used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * This function should be called before gnutls_global_init().
Packit 549fdc
 *
Packit 549fdc
 * For simplicity you can use the convenience
Packit 549fdc
 * gnutls_crypto_single_mac_register() macro.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 2.6.0
Packit 549fdc
 -*/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_single_mac_register(gnutls_mac_algorithm_t algorithm,
Packit 549fdc
				  int priority,
Packit 549fdc
				  const gnutls_crypto_mac_st * s,
Packit 549fdc
				  int free_s)
Packit 549fdc
{
Packit 549fdc
	return _algo_register(&glob_ml, algorithm, priority, (void*)s, free_s);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
const gnutls_crypto_mac_st *_gnutls_get_crypto_mac(gnutls_mac_algorithm_t
Packit 549fdc
						   algo)
Packit 549fdc
{
Packit 549fdc
	return _get_algo(&glob_ml, algo);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/*-
Packit 549fdc
 * gnutls_crypto_single_digest_register:
Packit 549fdc
 * @algorithm: is the gnutls algorithm identifier
Packit 549fdc
 * @priority: is the priority of the algorithm
Packit 549fdc
 * @s: is a structure holding new algorithms's data
Packit 549fdc
 *
Packit 549fdc
 * This function will register a digest (hash) algorithm to be used by
Packit 549fdc
 * gnutls.  Any algorithm registered will override the included
Packit 549fdc
 * algorithms and by convention kernel implemented algorithms have
Packit 549fdc
 * priority of 90  and CPU-assisted of 80.  The algorithm with the lowest priority will be
Packit 549fdc
 * used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * This function should be called before gnutls_global_init().
Packit 549fdc
 *
Packit 549fdc
 * For simplicity you can use the convenience
Packit 549fdc
 * gnutls_crypto_single_digest_register() macro.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 2.6.0
Packit 549fdc
 -*/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_single_digest_register(gnutls_digest_algorithm_t algorithm,
Packit 549fdc
				     int priority,
Packit 549fdc
				     const gnutls_crypto_digest_st * s,
Packit 549fdc
				     int free_s)
Packit 549fdc
{
Packit 549fdc
	return _algo_register(&glob_dl, algorithm, priority, (void*)s, free_s);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
const gnutls_crypto_digest_st
Packit 549fdc
    *_gnutls_get_crypto_digest(gnutls_digest_algorithm_t algo)
Packit 549fdc
{
Packit 549fdc
	return _get_algo(&glob_dl, algo);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_crypto_register_mac:
Packit 549fdc
 * @algorithm: is the gnutls MAC identifier
Packit 549fdc
 * @priority: is the priority of the algorithm
Packit 549fdc
 * @init: A function which initializes the MAC
Packit 549fdc
 * @setkey: A function which sets the key of the MAC
Packit 549fdc
 * @setnonce: A function which sets the nonce for the mac (may be %NULL for common MAC algorithms)
Packit 549fdc
 * @hash: Perform the hash operation
Packit 549fdc
 * @output: Provide the output of the MAC
Packit 549fdc
 * @deinit: A function which deinitializes the MAC
Packit 549fdc
 * @hash_fast: Perform the MAC operation in one go
Packit 549fdc
 *
Packit 549fdc
 * This function will register a MAC algorithm to be used by gnutls.
Packit 549fdc
 * Any algorithm registered will override the included algorithms and
Packit 549fdc
 * by convention kernel implemented algorithms have priority of 90
Packit 549fdc
 *  and CPU-assisted of 80.
Packit 549fdc
 * The algorithm with the lowest priority will be used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.4.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_register_mac(gnutls_mac_algorithm_t algorithm,
Packit 549fdc
			   int priority,
Packit 549fdc
			   gnutls_mac_init_func init,
Packit 549fdc
			   gnutls_mac_setkey_func setkey,
Packit 549fdc
			   gnutls_mac_setnonce_func setnonce,
Packit 549fdc
			   gnutls_mac_hash_func hash,
Packit 549fdc
			   gnutls_mac_output_func output,
Packit 549fdc
			   gnutls_mac_deinit_func deinit,
Packit 549fdc
			   gnutls_mac_fast_func hash_fast)
Packit 549fdc
{
Packit 549fdc
	gnutls_crypto_mac_st *s = gnutls_calloc(1, sizeof(gnutls_crypto_mac_st));
Packit 549fdc
	if (s == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
Packit 549fdc
Packit 549fdc
	s->init = init;
Packit 549fdc
	s->setkey = setkey;
Packit 549fdc
	s->setnonce = setnonce;
Packit 549fdc
	s->hash = hash;
Packit 549fdc
	s->output = output;
Packit 549fdc
	s->fast = hash_fast;
Packit 549fdc
	s->deinit = deinit;
Packit 549fdc
Packit 549fdc
	return gnutls_crypto_single_mac_register(algorithm, priority, s, 1);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
/**
Packit 549fdc
 * gnutls_crypto_register_digest:
Packit 549fdc
 * @algorithm: is the gnutls digest identifier
Packit 549fdc
 * @priority: is the priority of the algorithm
Packit 549fdc
 * @init: A function which initializes the digest
Packit 549fdc
 * @hash: Perform the hash operation
Packit 549fdc
 * @output: Provide the output of the digest
Packit 549fdc
 * @deinit: A function which deinitializes the digest
Packit 549fdc
 * @hash_fast: Perform the digest operation in one go
Packit 549fdc
 *
Packit 549fdc
 * This function will register a digest algorithm to be used by gnutls.
Packit 549fdc
 * Any algorithm registered will override the included algorithms and
Packit 549fdc
 * by convention kernel implemented algorithms have priority of 90
Packit 549fdc
 *  and CPU-assisted of 80.
Packit 549fdc
 * The algorithm with the lowest priority will be used by gnutls.
Packit 549fdc
 *
Packit 549fdc
 * Returns: %GNUTLS_E_SUCCESS on success, otherwise a negative error code.
Packit 549fdc
 *
Packit 549fdc
 * Since: 3.4.0
Packit 549fdc
 **/
Packit 549fdc
int
Packit 549fdc
gnutls_crypto_register_digest(gnutls_digest_algorithm_t algorithm,
Packit 549fdc
			   int priority,
Packit 549fdc
			   gnutls_digest_init_func init,
Packit 549fdc
			   gnutls_digest_hash_func hash,
Packit 549fdc
			   gnutls_digest_output_func output,
Packit 549fdc
			   gnutls_digest_deinit_func deinit,
Packit 549fdc
			   gnutls_digest_fast_func hash_fast)
Packit 549fdc
{
Packit 549fdc
	gnutls_crypto_digest_st *s = gnutls_calloc(1, sizeof(gnutls_crypto_digest_st));
Packit 549fdc
	if (s == NULL)
Packit 549fdc
		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
Packit 549fdc
Packit 549fdc
	s->init = init;
Packit 549fdc
	s->hash = hash;
Packit 549fdc
	s->output = output;
Packit 549fdc
	s->fast = hash_fast;
Packit 549fdc
	s->deinit = deinit;
Packit 549fdc
Packit 549fdc
	return gnutls_crypto_single_digest_register(algorithm, priority, s, 1);
Packit 549fdc
}