|
Packit Service |
4684c1 |
/*
|
|
Packit Service |
4684c1 |
* Copyright (C) 2000-2016 Free Software Foundation, Inc.
|
|
Packit Service |
4684c1 |
* Copyright (C) 2016 Red Hat, Inc.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Author: Nikos Mavrogiannopoulos
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This file is part of GnuTLS.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* The GnuTLS is free software; you can redistribute it and/or
|
|
Packit Service |
4684c1 |
* modify it under the terms of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* as published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
4684c1 |
* the License, or (at your option) any later version.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This library is distributed in the hope that it will be useful, but
|
|
Packit Service |
4684c1 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
4684c1 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
4684c1 |
* Lesser General Public License for more details.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* You should have received a copy of the GNU Lesser General Public License
|
|
Packit Service |
4684c1 |
* along with this program. If not, see <https://www.gnu.org/licenses/>
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#include "gnutls_int.h"
|
|
Packit Service |
4684c1 |
#include "errors.h"
|
|
Packit Service |
4684c1 |
#include <cipher_int.h>
|
|
Packit Service |
4684c1 |
#include <datum.h>
|
|
Packit Service |
4684c1 |
#include <gnutls/crypto.h>
|
|
Packit Service |
4684c1 |
#include <algorithms.h>
|
|
Packit Service |
4684c1 |
#include <random.h>
|
|
Packit Service |
4684c1 |
#include <crypto.h>
|
|
Packit Service |
4684c1 |
#include <fips.h>
|
|
Packit Service |
4684c1 |
#include "crypto-api.h"
|
|
Packit Service |
4684c1 |
#include "iov.h"
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
typedef struct api_cipher_hd_st {
|
|
Packit Service |
4684c1 |
cipher_hd_st ctx_enc;
|
|
Packit Service |
4684c1 |
cipher_hd_st ctx_dec;
|
|
Packit Service |
4684c1 |
} api_cipher_hd_st;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_init:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @cipher: the encryption algorithm to use
|
|
Packit Service |
4684c1 |
* @key: the key to be used for encryption/decryption
|
|
Packit Service |
4684c1 |
* @iv: the IV to use (if not applicable set NULL)
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will initialize the @handle context to be usable
|
|
Packit Service |
4684c1 |
* for encryption/decryption of data. This will effectively use the
|
|
Packit Service |
4684c1 |
* current crypto backend in use by gnutls or the cryptographic
|
|
Packit Service |
4684c1 |
* accelerator in use.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_cipher_init(gnutls_cipher_hd_t * handle,
|
|
Packit Service |
4684c1 |
gnutls_cipher_algorithm_t cipher,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t * key, const gnutls_datum_t * iv)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
const cipher_entry_st* e;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (is_cipher_algo_forbidden(cipher))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
e = cipher_to_entry(cipher);
|
|
Packit Service |
4684c1 |
if (e == NULL || (e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
*handle = gnutls_calloc(1, sizeof(api_cipher_hd_st));
|
|
Packit Service |
4684c1 |
if (*handle == NULL) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
h = *handle;
|
|
Packit Service |
4684c1 |
ret =
|
|
Packit Service |
4684c1 |
_gnutls_cipher_init(&h->ctx_enc, e, key,
|
|
Packit Service |
4684c1 |
iv, 1);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (ret >= 0 && _gnutls_cipher_type(e) == CIPHER_BLOCK)
|
|
Packit Service |
4684c1 |
ret =
|
|
Packit Service |
4684c1 |
_gnutls_cipher_init(&h->ctx_dec, e, key, iv, 0);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return ret;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_tag:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @tag: will hold the tag
|
|
Packit Service |
4684c1 |
* @tag_size: the length of the tag to return
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function operates on authenticated encryption with
|
|
Packit Service |
4684c1 |
* associated data (AEAD) ciphers and will return the
|
|
Packit Service |
4684c1 |
* output tag.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_cipher_tag(gnutls_cipher_hd_t handle, void *tag, size_t tag_size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_is_aead(&h->ctx_enc) == 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_gnutls_cipher_tag(&h->ctx_enc, tag, tag_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_add_auth:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @ptext: the data to be authenticated
|
|
Packit Service |
4684c1 |
* @ptext_size: the length of the data
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function operates on authenticated encryption with
|
|
Packit Service |
4684c1 |
* associated data (AEAD) ciphers and authenticate the
|
|
Packit Service |
4684c1 |
* input data. This function can only be called once
|
|
Packit Service |
4684c1 |
* and before any encryption operations.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_cipher_add_auth(gnutls_cipher_hd_t handle, const void *ptext,
|
|
Packit Service |
4684c1 |
size_t ptext_size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_is_aead(&h->ctx_enc) == 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_auth(&h->ctx_enc, ptext, ptext_size);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_set_iv:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @iv: the IV to set
|
|
Packit Service |
4684c1 |
* @ivlen: the length of the IV
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will set the IV to be used for the next
|
|
Packit Service |
4684c1 |
* encryption block.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void
|
|
Packit Service |
4684c1 |
gnutls_cipher_set_iv(gnutls_cipher_hd_t handle, void *iv, size_t ivlen)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_setiv(&h->ctx_enc, iv, ivlen) < 0) {
|
|
Packit Service |
4684c1 |
_gnutls_switch_lib_state(LIB_STATE_ERROR);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_type(h->ctx_enc.e) == CIPHER_BLOCK)
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_setiv(&h->ctx_dec, iv, ivlen) < 0) {
|
|
Packit Service |
4684c1 |
_gnutls_switch_lib_state(LIB_STATE_ERROR);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/*-
|
|
Packit Service |
4684c1 |
* _gnutls_cipher_get_iv:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @iv: the IV to set
|
|
Packit Service |
4684c1 |
* @ivlen: the length of the IV
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will retrieve the internally calculated IV value. It is
|
|
Packit Service |
4684c1 |
* intended to be used for modes like CFB. @iv must have @ivlen length
|
|
Packit Service |
4684c1 |
* at least.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This is solely for validation purposes of our crypto
|
|
Packit Service |
4684c1 |
* implementation. For other purposes, the IV can be typically
|
|
Packit Service |
4684c1 |
* calculated from the initial IV value and the subsequent ciphertext
|
|
Packit Service |
4684c1 |
* values. As such, this function only works with the internally
|
|
Packit Service |
4684c1 |
* registered ciphers.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: The length of IV or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.8
|
|
Packit Service |
4684c1 |
-*/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
_gnutls_cipher_get_iv(gnutls_cipher_hd_t handle, void *iv, size_t ivlen)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_getiv(&h->ctx_enc, iv, ivlen);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_encrypt:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @ptext: the data to encrypt
|
|
Packit Service |
4684c1 |
* @ptext_len: the length of data to encrypt
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will encrypt the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_cipher_encrypt(gnutls_cipher_hd_t handle, void *ptext,
|
|
Packit Service |
4684c1 |
size_t ptext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_encrypt(&h->ctx_enc, ptext, ptext_len);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_decrypt:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @ctext: the data to decrypt
|
|
Packit Service |
4684c1 |
* @ctext_len: the length of data to decrypt
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will decrypt the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Note that in AEAD ciphers, this will not check the tag. You will
|
|
Packit Service |
4684c1 |
* need to compare the tag sent with the value returned from gnutls_cipher_tag().
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_cipher_decrypt(gnutls_cipher_hd_t handle, void *ctext,
|
|
Packit Service |
4684c1 |
size_t ctext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_type(h->ctx_enc.e) != CIPHER_BLOCK)
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_decrypt(&h->ctx_enc, ctext,
|
|
Packit Service |
4684c1 |
ctext_len);
|
|
Packit Service |
4684c1 |
else
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_decrypt(&h->ctx_dec, ctext,
|
|
Packit Service |
4684c1 |
ctext_len);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_encrypt2:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @ptext: the data to encrypt
|
|
Packit Service |
4684c1 |
* @ptext_len: the length of data to encrypt
|
|
Packit Service |
4684c1 |
* @ctext: the encrypted data
|
|
Packit Service |
4684c1 |
* @ctext_len: the available length for encrypted data
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will encrypt the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context. For block ciphers the @ptext_len must be
|
|
Packit Service |
4684c1 |
* a multiple of the block size. For the supported ciphers the encrypted
|
|
Packit Service |
4684c1 |
* data length will equal the plaintext size.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.12.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_cipher_encrypt2(gnutls_cipher_hd_t handle, const void *ptext,
|
|
Packit Service |
4684c1 |
size_t ptext_len, void *ctext,
|
|
Packit Service |
4684c1 |
size_t ctext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_encrypt2(&h->ctx_enc, ptext, ptext_len,
|
|
Packit Service |
4684c1 |
ctext, ctext_len);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_decrypt2:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
* @ctext: the data to decrypt
|
|
Packit Service |
4684c1 |
* @ctext_len: the length of data to decrypt
|
|
Packit Service |
4684c1 |
* @ptext: the decrypted data
|
|
Packit Service |
4684c1 |
* @ptext_len: the available length for decrypted data
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will decrypt the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context. For block ciphers the @ctext_len must be
|
|
Packit Service |
4684c1 |
* a multiple of the block size. For the supported ciphers the plaintext
|
|
Packit Service |
4684c1 |
* data length will equal the ciphertext size.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Note that in AEAD ciphers, this will not check the tag. You will
|
|
Packit Service |
4684c1 |
* need to compare the tag sent with the value returned from gnutls_cipher_tag().
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.12.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_cipher_decrypt2(gnutls_cipher_hd_t handle, const void *ctext,
|
|
Packit Service |
4684c1 |
size_t ctext_len, void *ptext, size_t ptext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_type(h->ctx_enc.e) != CIPHER_BLOCK)
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_decrypt2(&h->ctx_enc, ctext,
|
|
Packit Service |
4684c1 |
ctext_len, ptext,
|
|
Packit Service |
4684c1 |
ptext_len);
|
|
Packit Service |
4684c1 |
else
|
|
Packit Service |
4684c1 |
return _gnutls_cipher_decrypt2(&h->ctx_dec, ctext,
|
|
Packit Service |
4684c1 |
ctext_len, ptext,
|
|
Packit Service |
4684c1 |
ptext_len);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_cipher_deinit:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_cipher_hd_t type
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will deinitialize all resources occupied by the given
|
|
Packit Service |
4684c1 |
* encryption context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void gnutls_cipher_deinit(gnutls_cipher_hd_t handle)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_gnutls_cipher_deinit(&h->ctx_enc);
|
|
Packit Service |
4684c1 |
if (_gnutls_cipher_type(h->ctx_enc.e) == CIPHER_BLOCK)
|
|
Packit Service |
4684c1 |
_gnutls_cipher_deinit(&h->ctx_dec);
|
|
Packit Service |
4684c1 |
gnutls_free(handle);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* HMAC */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_init:
|
|
Packit Service |
4684c1 |
* @dig: is a #gnutls_hmac_hd_t type
|
|
Packit Service |
4684c1 |
* @algorithm: the HMAC algorithm to use
|
|
Packit Service |
4684c1 |
* @key: the key to be used for encryption
|
|
Packit Service |
4684c1 |
* @keylen: the length of the key
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will initialize an context that can be used to
|
|
Packit Service |
4684c1 |
* produce a Message Authentication Code (MAC) of data. This will
|
|
Packit Service |
4684c1 |
* effectively use the current crypto backend in use by gnutls or the
|
|
Packit Service |
4684c1 |
* cryptographic accelerator in use.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Note that despite the name of this function, it can be used
|
|
Packit Service |
4684c1 |
* for other MAC algorithms than HMAC.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_hmac_init(gnutls_hmac_hd_t * dig,
|
|
Packit Service |
4684c1 |
gnutls_mac_algorithm_t algorithm,
|
|
Packit Service |
4684c1 |
const void *key, size_t keylen)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
/* MD5 is only allowed internally for TLS */
|
|
Packit Service |
4684c1 |
if (is_mac_algo_forbidden(algorithm))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
*dig = gnutls_malloc(sizeof(mac_hd_st));
|
|
Packit Service |
4684c1 |
if (*dig == NULL) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_mac_init(((mac_hd_st *) * dig),
|
|
Packit Service |
4684c1 |
mac_to_entry(algorithm), key, keylen);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_set_nonce:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hmac_hd_t type
|
|
Packit Service |
4684c1 |
* @nonce: the data to set as nonce
|
|
Packit Service |
4684c1 |
* @nonce_len: the length of data
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will set the nonce in the MAC algorithm.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.2.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void
|
|
Packit Service |
4684c1 |
gnutls_hmac_set_nonce(gnutls_hmac_hd_t handle, const void *nonce,
|
|
Packit Service |
4684c1 |
size_t nonce_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
_gnutls_mac_set_nonce((mac_hd_st *) handle, nonce, nonce_len);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hmac_hd_t type
|
|
Packit Service |
4684c1 |
* @ptext: the data to hash
|
|
Packit Service |
4684c1 |
* @ptext_len: the length of data to hash
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will hash the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int gnutls_hmac(gnutls_hmac_hd_t handle, const void *ptext, size_t ptext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return _gnutls_mac((mac_hd_st *) handle, ptext, ptext_len);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_output:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hmac_hd_t type
|
|
Packit Service |
4684c1 |
* @digest: is the output value of the MAC
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will output the current MAC value
|
|
Packit Service |
4684c1 |
* and reset the state of the MAC.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void gnutls_hmac_output(gnutls_hmac_hd_t handle, void *digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
_gnutls_mac_output((mac_hd_st *) handle, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_deinit:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hmac_hd_t type
|
|
Packit Service |
4684c1 |
* @digest: is the output value of the MAC
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will deinitialize all resources occupied by
|
|
Packit Service |
4684c1 |
* the given hmac context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void gnutls_hmac_deinit(gnutls_hmac_hd_t handle, void *digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
_gnutls_mac_deinit((mac_hd_st *) handle, digest);
|
|
Packit Service |
4684c1 |
gnutls_free(handle);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_get_len:
|
|
Packit Service |
4684c1 |
* @algorithm: the hmac algorithm to use
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will return the length of the output data
|
|
Packit Service |
4684c1 |
* of the given hmac algorithm.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: The length or zero on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
unsigned gnutls_hmac_get_len(gnutls_mac_algorithm_t algorithm)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return _gnutls_mac_get_algo_len(mac_to_entry(algorithm));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_get_key_size:
|
|
Packit Service |
4684c1 |
* @algorithm: the mac algorithm to use
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will return the size of the key to be used with this
|
|
Packit Service |
4684c1 |
* algorithm. On the algorithms which may accept arbitrary key sizes,
|
|
Packit Service |
4684c1 |
* the returned size is the MAC key size used in the TLS protocol.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: The key size or zero on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.12
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
unsigned gnutls_hmac_get_key_size(gnutls_mac_algorithm_t algorithm)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return _gnutls_mac_get_key_size(mac_to_entry(algorithm));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_fast:
|
|
Packit Service |
4684c1 |
* @algorithm: the hash algorithm to use
|
|
Packit Service |
4684c1 |
* @key: the key to use
|
|
Packit Service |
4684c1 |
* @keylen: the length of the key
|
|
Packit Service |
4684c1 |
* @ptext: the data to hash
|
|
Packit Service |
4684c1 |
* @ptext_len: the length of data to hash
|
|
Packit Service |
4684c1 |
* @digest: is the output value of the hash
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This convenience function will hash the given data and return output
|
|
Packit Service |
4684c1 |
* on a single call. Note, this call will not work for MAC algorithms
|
|
Packit Service |
4684c1 |
* that require nonce (like UMAC or GMAC).
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_hmac_fast(gnutls_mac_algorithm_t algorithm,
|
|
Packit Service |
4684c1 |
const void *key, size_t keylen,
|
|
Packit Service |
4684c1 |
const void *ptext, size_t ptext_len, void *digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
if (is_mac_algo_forbidden(algorithm))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_mac_fast(algorithm, key, keylen, ptext, ptext_len,
|
|
Packit Service |
4684c1 |
digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hmac_copy:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hmac_hd_t type
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will create a copy of MAC context, containing all its current
|
|
Packit Service |
4684c1 |
* state. Copying contexts for MACs registered using
|
|
Packit Service |
4684c1 |
* gnutls_crypto_register_mac() is not supported and will always result in an
|
|
Packit Service |
4684c1 |
* error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: new MAC context or NULL in case of an error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.9
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_hmac_hd_t gnutls_hmac_copy(gnutls_hmac_hd_t handle)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_hmac_hd_t dig;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
dig = gnutls_malloc(sizeof(mac_hd_st));
|
|
Packit Service |
4684c1 |
if (dig == NULL) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return NULL;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_mac_copy((const mac_hd_st *) handle, (mac_hd_st *)dig) != GNUTLS_E_SUCCESS) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
gnutls_free(dig);
|
|
Packit Service |
4684c1 |
return NULL;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return dig;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* HASH */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hash_init:
|
|
Packit Service |
4684c1 |
* @dig: is a #gnutls_hash_hd_t type
|
|
Packit Service |
4684c1 |
* @algorithm: the hash algorithm to use
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will initialize an context that can be used to
|
|
Packit Service |
4684c1 |
* produce a Message Digest of data. This will effectively use the
|
|
Packit Service |
4684c1 |
* current crypto backend in use by gnutls or the cryptographic
|
|
Packit Service |
4684c1 |
* accelerator in use.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_hash_init(gnutls_hash_hd_t * dig,
|
|
Packit Service |
4684c1 |
gnutls_digest_algorithm_t algorithm)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
if (is_mac_algo_forbidden(algorithm))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
*dig = gnutls_malloc(sizeof(digest_hd_st));
|
|
Packit Service |
4684c1 |
if (*dig == NULL) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_hash_init(((digest_hd_st *) * dig),
|
|
Packit Service |
4684c1 |
hash_to_entry(algorithm));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hash:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hash_hd_t type
|
|
Packit Service |
4684c1 |
* @ptext: the data to hash
|
|
Packit Service |
4684c1 |
* @ptext_len: the length of data to hash
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will hash the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int gnutls_hash(gnutls_hash_hd_t handle, const void *ptext, size_t ptext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return _gnutls_hash((digest_hd_st *) handle, ptext, ptext_len);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hash_output:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hash_hd_t type
|
|
Packit Service |
4684c1 |
* @digest: is the output value of the hash
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will output the current hash value
|
|
Packit Service |
4684c1 |
* and reset the state of the hash.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void gnutls_hash_output(gnutls_hash_hd_t handle, void *digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
_gnutls_hash_output((digest_hd_st *) handle, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hash_deinit:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hash_hd_t type
|
|
Packit Service |
4684c1 |
* @digest: is the output value of the hash
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will deinitialize all resources occupied by
|
|
Packit Service |
4684c1 |
* the given hash context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void gnutls_hash_deinit(gnutls_hash_hd_t handle, void *digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
_gnutls_hash_deinit((digest_hd_st *) handle, digest);
|
|
Packit Service |
4684c1 |
gnutls_free(handle);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hash_get_len:
|
|
Packit Service |
4684c1 |
* @algorithm: the hash algorithm to use
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will return the length of the output data
|
|
Packit Service |
4684c1 |
* of the given hash algorithm.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: The length or zero on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
unsigned gnutls_hash_get_len(gnutls_digest_algorithm_t algorithm)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
return _gnutls_hash_get_algo_len(hash_to_entry(algorithm));
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hash_fast:
|
|
Packit Service |
4684c1 |
* @algorithm: the hash algorithm to use
|
|
Packit Service |
4684c1 |
* @ptext: the data to hash
|
|
Packit Service |
4684c1 |
* @ptext_len: the length of data to hash
|
|
Packit Service |
4684c1 |
* @digest: is the output value of the hash
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This convenience function will hash the given data and return output
|
|
Packit Service |
4684c1 |
* on a single call.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 2.10.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_hash_fast(gnutls_digest_algorithm_t algorithm,
|
|
Packit Service |
4684c1 |
const void *ptext, size_t ptext_len, void *digest)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
if (is_mac_algo_forbidden(algorithm))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_hash_fast(algorithm, ptext, ptext_len, digest);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hash_copy:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_hash_hd_t type
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will create a copy of Message Digest context, containing all
|
|
Packit Service |
4684c1 |
* its current state. Copying contexts for Message Digests registered using
|
|
Packit Service |
4684c1 |
* gnutls_crypto_register_digest() is not supported and will always result in
|
|
Packit Service |
4684c1 |
* an error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: new Message Digest context or NULL in case of an error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.9
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
gnutls_hash_hd_t gnutls_hash_copy(gnutls_hash_hd_t handle)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
gnutls_hash_hd_t dig;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
dig = gnutls_malloc(sizeof(digest_hd_st));
|
|
Packit Service |
4684c1 |
if (dig == NULL) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return NULL;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_gnutls_hash_copy((const digest_hd_st *) handle, (digest_hd_st *)dig) != GNUTLS_E_SUCCESS) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
gnutls_free(dig);
|
|
Packit Service |
4684c1 |
return NULL;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return dig;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_key_generate:
|
|
Packit Service |
4684c1 |
* @key: is a pointer to a #gnutls_datum_t which will contain a newly
|
|
Packit Service |
4684c1 |
* created key
|
|
Packit Service |
4684c1 |
* @key_size: the number of bytes of the key
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Generates a random key of @key_size bytes.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, or an
|
|
Packit Service |
4684c1 |
* error code.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int gnutls_key_generate(gnutls_datum_t * key, unsigned int key_size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
FAIL_IF_LIB_ERROR;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
#ifdef ENABLE_FIPS140
|
|
Packit Service |
4684c1 |
/* The FIPS140 approved RNGs are not allowed to be used
|
|
Packit Service |
4684c1 |
* to extract key sizes longer than their original seed.
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
if (_gnutls_fips_mode_enabled() != 0 &&
|
|
Packit Service |
4684c1 |
key_size > FIPS140_RND_KEY_SIZE)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
#endif
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
key->size = key_size;
|
|
Packit Service |
4684c1 |
key->data = gnutls_malloc(key->size);
|
|
Packit Service |
4684c1 |
if (!key->data) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_rnd(GNUTLS_RND_RANDOM, key->data, key->size);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
_gnutls_free_datum(key);
|
|
Packit Service |
4684c1 |
return ret;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* AEAD API */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_aead_cipher_init:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_aead_cipher_hd_t type.
|
|
Packit Service |
4684c1 |
* @cipher: the authenticated-encryption algorithm to use
|
|
Packit Service |
4684c1 |
* @key: The key to be used for encryption
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will initialize an context that can be used for
|
|
Packit Service |
4684c1 |
* encryption/decryption of data. This will effectively use the
|
|
Packit Service |
4684c1 |
* current crypto backend in use by gnutls or the cryptographic
|
|
Packit Service |
4684c1 |
* accelerator in use.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.4.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int gnutls_aead_cipher_init(gnutls_aead_cipher_hd_t *handle,
|
|
Packit Service |
4684c1 |
gnutls_cipher_algorithm_t cipher,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *key)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_aead_cipher_hd_st *h;
|
|
Packit Service |
4684c1 |
const cipher_entry_st *e;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (is_cipher_algo_forbidden(cipher))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
e = cipher_to_entry(cipher);
|
|
Packit Service |
4684c1 |
if (e == NULL || e->type != CIPHER_AEAD)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
*handle = gnutls_calloc(1, sizeof(api_aead_cipher_hd_st));
|
|
Packit Service |
4684c1 |
if (*handle == NULL) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
return GNUTLS_E_MEMORY_ERROR;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
h = *handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_aead_cipher_init(h, cipher, key);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_aead_cipher_decrypt:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_aead_cipher_hd_t type.
|
|
Packit Service |
4684c1 |
* @nonce: the nonce to set
|
|
Packit Service |
4684c1 |
* @nonce_len: The length of the nonce
|
|
Packit Service |
4684c1 |
* @auth: additional data to be authenticated
|
|
Packit Service |
4684c1 |
* @auth_len: The length of the data
|
|
Packit Service |
4684c1 |
* @tag_size: The size of the tag to use (use zero for the default)
|
|
Packit Service |
4684c1 |
* @ctext: the data to decrypt (including the authentication tag)
|
|
Packit Service |
4684c1 |
* @ctext_len: the length of data to decrypt (includes tag size)
|
|
Packit Service |
4684c1 |
* @ptext: the decrypted data
|
|
Packit Service |
4684c1 |
* @ptext_len: the length of decrypted data (initially must hold the maximum available size)
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will decrypt the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context. This function must be provided the complete
|
|
Packit Service |
4684c1 |
* data to be decrypted, including the authentication tag. On several
|
|
Packit Service |
4684c1 |
* AEAD ciphers, the authentication tag is appended to the ciphertext,
|
|
Packit Service |
4684c1 |
* though this is not a general rule. This function will fail if
|
|
Packit Service |
4684c1 |
* the tag verification fails.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on verification failure or other error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.4.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_aead_cipher_decrypt(gnutls_aead_cipher_hd_t handle,
|
|
Packit Service |
4684c1 |
const void *nonce, size_t nonce_len,
|
|
Packit Service |
4684c1 |
const void *auth, size_t auth_len,
|
|
Packit Service |
4684c1 |
size_t tag_size,
|
|
Packit Service |
4684c1 |
const void *ctext, size_t ctext_len,
|
|
Packit Service |
4684c1 |
void *ptext, size_t *ptext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
api_aead_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag_size == 0)
|
|
Packit Service |
4684c1 |
tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
|
|
Packit Service |
4684c1 |
else if (tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (unlikely(ctext_len < tag_size))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_aead_cipher_decrypt(&h->ctx_enc,
|
|
Packit Service |
4684c1 |
nonce, nonce_len,
|
|
Packit Service |
4684c1 |
auth, auth_len,
|
|
Packit Service |
4684c1 |
tag_size,
|
|
Packit Service |
4684c1 |
ctext, ctext_len,
|
|
Packit Service |
4684c1 |
ptext, *ptext_len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* That assumes that AEAD ciphers are stream */
|
|
Packit Service |
4684c1 |
*ptext_len = ctext_len - tag_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_aead_cipher_encrypt:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_aead_cipher_hd_t type.
|
|
Packit Service |
4684c1 |
* @nonce: the nonce to set
|
|
Packit Service |
4684c1 |
* @nonce_len: The length of the nonce
|
|
Packit Service |
4684c1 |
* @auth: additional data to be authenticated
|
|
Packit Service |
4684c1 |
* @auth_len: The length of the data
|
|
Packit Service |
4684c1 |
* @tag_size: The size of the tag to use (use zero for the default)
|
|
Packit Service |
4684c1 |
* @ptext: the data to encrypt
|
|
Packit Service |
4684c1 |
* @ptext_len: The length of data to encrypt
|
|
Packit Service |
4684c1 |
* @ctext: the encrypted data including authentication tag
|
|
Packit Service |
4684c1 |
* @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will encrypt the given data using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context. The output data will contain the
|
|
Packit Service |
4684c1 |
* authentication tag.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.4.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_aead_cipher_encrypt(gnutls_aead_cipher_hd_t handle,
|
|
Packit Service |
4684c1 |
const void *nonce, size_t nonce_len,
|
|
Packit Service |
4684c1 |
const void *auth, size_t auth_len,
|
|
Packit Service |
4684c1 |
size_t tag_size,
|
|
Packit Service |
4684c1 |
const void *ptext, size_t ptext_len,
|
|
Packit Service |
4684c1 |
void *ctext, size_t *ctext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_aead_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
int ret;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag_size == 0)
|
|
Packit Service |
4684c1 |
tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
|
|
Packit Service |
4684c1 |
else if (tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (unlikely(*ctext_len < ptext_len + tag_size))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_aead_cipher_encrypt(&h->ctx_enc,
|
|
Packit Service |
4684c1 |
nonce, nonce_len,
|
|
Packit Service |
4684c1 |
auth, auth_len,
|
|
Packit Service |
4684c1 |
tag_size,
|
|
Packit Service |
4684c1 |
ptext, ptext_len,
|
|
Packit Service |
4684c1 |
ctext, *ctext_len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* That assumes that AEAD ciphers are stream */
|
|
Packit Service |
4684c1 |
*ctext_len = ptext_len + tag_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
struct iov_store_st {
|
|
Packit Service |
4684c1 |
void *data;
|
|
Packit Service |
4684c1 |
size_t size;
|
|
Packit Service |
4684c1 |
};
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static void iov_store_free(struct iov_store_st *s)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
090db1 |
gnutls_free(s->data);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int iov_store_grow(struct iov_store_st *s, size_t length)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
090db1 |
void *data;
|
|
Packit Service |
090db1 |
|
|
Packit Service |
090db1 |
s->size += length;
|
|
Packit Service |
090db1 |
data = gnutls_realloc(s->data, s->size);
|
|
Packit Service |
090db1 |
if (data == NULL)
|
|
Packit Service |
090db1 |
return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
|
|
Packit Service |
090db1 |
|
|
Packit Service |
090db1 |
s->data = data;
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int
|
|
Packit Service |
4684c1 |
copy_from_iov(struct iov_store_st *dst, const giovec_t *iov, int iovcnt)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
memset(dst, 0, sizeof(*dst));
|
|
Packit Service |
4684c1 |
if (iovcnt == 0) {
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
} else {
|
|
Packit Service |
4684c1 |
int i;
|
|
Packit Service |
4684c1 |
uint8_t *p;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
dst->size = 0;
|
|
Packit Service |
4684c1 |
for (i=0;i
|
|
Packit Service |
4684c1 |
dst->size += iov[i].iov_len;
|
|
Packit Service |
4684c1 |
dst->data = gnutls_malloc(dst->size);
|
|
Packit Service |
4684c1 |
if (dst->data == NULL)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
p = dst->data;
|
|
Packit Service |
4684c1 |
for (i=0;i
|
|
Packit Service |
090db1 |
if (iov[i].iov_len > 0)
|
|
Packit Service |
090db1 |
memcpy(p, iov[i].iov_base, iov[i].iov_len);
|
|
Packit Service |
4684c1 |
p += iov[i].iov_len;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
static int
|
|
Packit Service |
4684c1 |
copy_to_iov(struct iov_store_st *src, size_t size,
|
|
Packit Service |
4684c1 |
const giovec_t *iov, int iovcnt)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
size_t offset = 0;
|
|
Packit Service |
4684c1 |
int i;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (unlikely(src->size < size))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
for (i = 0; i < iovcnt && size > 0; i++) {
|
|
Packit Service |
4684c1 |
size_t to_copy = MIN(size, iov[i].iov_len);
|
|
Packit Service |
4684c1 |
memcpy(iov[i].iov_base, (uint8_t *) src->data + offset, to_copy);
|
|
Packit Service |
4684c1 |
offset += to_copy;
|
|
Packit Service |
4684c1 |
size -= to_copy;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
if (size > 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_aead_cipher_encryptv:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_aead_cipher_hd_t type.
|
|
Packit Service |
4684c1 |
* @nonce: the nonce to set
|
|
Packit Service |
4684c1 |
* @nonce_len: The length of the nonce
|
|
Packit Service |
4684c1 |
* @auth_iov: additional data to be authenticated
|
|
Packit Service |
4684c1 |
* @auth_iovcnt: The number of buffers in @auth_iov
|
|
Packit Service |
4684c1 |
* @tag_size: The size of the tag to use (use zero for the default)
|
|
Packit Service |
4684c1 |
* @iov: the data to be encrypted
|
|
Packit Service |
4684c1 |
* @iovcnt: The number of buffers in @iov
|
|
Packit Service |
4684c1 |
* @ctext: the encrypted data including authentication tag
|
|
Packit Service |
4684c1 |
* @ctext_len: the length of encrypted data (initially must hold the maximum available size, including space for tag)
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will encrypt the provided data buffers using the algorithm
|
|
Packit Service |
4684c1 |
* specified by the context. The output data will contain the
|
|
Packit Service |
4684c1 |
* authentication tag.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.3
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_aead_cipher_encryptv(gnutls_aead_cipher_hd_t handle,
|
|
Packit Service |
4684c1 |
const void *nonce, size_t nonce_len,
|
|
Packit Service |
4684c1 |
const giovec_t *auth_iov, int auth_iovcnt,
|
|
Packit Service |
4684c1 |
size_t tag_size,
|
|
Packit Service |
4684c1 |
const giovec_t *iov, int iovcnt,
|
|
Packit Service |
4684c1 |
void *ctext, size_t *ctext_len)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_aead_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
ssize_t ret;
|
|
Packit Service |
4684c1 |
uint8_t *dst;
|
|
Packit Service |
4684c1 |
size_t dst_size, total = 0;
|
|
Packit Service |
4684c1 |
uint8_t *p;
|
|
Packit Service |
4684c1 |
size_t len;
|
|
Packit Service |
4684c1 |
size_t blocksize = handle->ctx_enc.e->blocksize;
|
|
Packit Service |
4684c1 |
struct iov_iter_st iter;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Limitation: this function provides an optimization under the internally registered
|
|
Packit Service |
4684c1 |
* AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
|
|
Packit Service |
4684c1 |
* then this becomes a convenience function as it missed the lower-level primitives
|
|
Packit Service |
4684c1 |
* necessary for piecemeal encryption. */
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag_size == 0)
|
|
Packit Service |
4684c1 |
tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
|
|
Packit Service |
4684c1 |
else if (tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) || handle->ctx_enc.encrypt == NULL) {
|
|
Packit Service |
4684c1 |
/* ciphertext cannot be produced in a piecemeal approach */
|
|
Packit Service |
4684c1 |
struct iov_store_st auth;
|
|
Packit Service |
4684c1 |
struct iov_store_st ptext;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_from_iov(&auth, auth_iov, auth_iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_from_iov(&ptext, iov, iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
iov_store_free(&auth);
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_aead_cipher_encrypt(handle, nonce, nonce_len,
|
|
Packit Service |
4684c1 |
auth.data, auth.size,
|
|
Packit Service |
4684c1 |
tag_size,
|
|
Packit Service |
4684c1 |
ptext.data, ptext.size,
|
|
Packit Service |
4684c1 |
ctext, ctext_len);
|
|
Packit Service |
4684c1 |
iov_store_free(&auth);
|
|
Packit Service |
4684c1 |
iov_store_free(&ptext);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return ret;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
while (1) {
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_next(&iter, &p);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
if (ret == 0)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
dst = ctext;
|
|
Packit Service |
4684c1 |
dst_size = *ctext_len;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
while (1) {
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_next(&iter, &p);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
if (ret == 0)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
len = ret;
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_encrypt2(&handle->ctx_enc,
|
|
Packit Service |
4684c1 |
p, len,
|
|
Packit Service |
4684c1 |
dst, dst_size);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
DECR_LEN(dst_size, len);
|
|
Packit Service |
4684c1 |
dst += len;
|
|
Packit Service |
4684c1 |
total += len;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (dst_size < tag_size)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
_gnutls_cipher_tag(&handle->ctx_enc, dst, tag_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
total += tag_size;
|
|
Packit Service |
4684c1 |
*ctext_len = total;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_aead_cipher_encryptv2:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_aead_cipher_hd_t type.
|
|
Packit Service |
4684c1 |
* @nonce: the nonce to set
|
|
Packit Service |
4684c1 |
* @nonce_len: The length of the nonce
|
|
Packit Service |
4684c1 |
* @auth_iov: additional data to be authenticated
|
|
Packit Service |
4684c1 |
* @auth_iovcnt: The number of buffers in @auth_iov
|
|
Packit Service |
4684c1 |
* @iov: the data to be encrypted
|
|
Packit Service |
4684c1 |
* @iovcnt: The number of buffers in @iov
|
|
Packit Service |
4684c1 |
* @tag: The authentication tag
|
|
Packit Service |
4684c1 |
* @tag_size: The size of the tag to use (use zero for the default)
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This is similar to gnutls_aead_cipher_encrypt(), but it performs
|
|
Packit Service |
4684c1 |
* in-place encryption on the provided data buffers.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.10
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_aead_cipher_encryptv2(gnutls_aead_cipher_hd_t handle,
|
|
Packit Service |
4684c1 |
const void *nonce, size_t nonce_len,
|
|
Packit Service |
4684c1 |
const giovec_t *auth_iov, int auth_iovcnt,
|
|
Packit Service |
4684c1 |
const giovec_t *iov, int iovcnt,
|
|
Packit Service |
4684c1 |
void *tag, size_t *tag_size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_aead_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
ssize_t ret;
|
|
Packit Service |
4684c1 |
uint8_t *p;
|
|
Packit Service |
4684c1 |
size_t len;
|
|
Packit Service |
4684c1 |
ssize_t blocksize = handle->ctx_enc.e->blocksize;
|
|
Packit Service |
4684c1 |
struct iov_iter_st iter;
|
|
Packit Service |
4684c1 |
size_t _tag_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag_size == NULL || *tag_size == 0)
|
|
Packit Service |
4684c1 |
_tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
|
|
Packit Service |
4684c1 |
else
|
|
Packit Service |
4684c1 |
_tag_size = *tag_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (_tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Limitation: this function provides an optimization under the internally registered
|
|
Packit Service |
4684c1 |
* AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
|
|
Packit Service |
4684c1 |
* then this becomes a convenience function as it missed the lower-level primitives
|
|
Packit Service |
4684c1 |
* necessary for piecemeal encryption. */
|
|
Packit Service |
4684c1 |
if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) || handle->ctx_enc.encrypt == NULL) {
|
|
Packit Service |
4684c1 |
/* ciphertext cannot be produced in a piecemeal approach */
|
|
Packit Service |
4684c1 |
struct iov_store_st auth;
|
|
Packit Service |
4684c1 |
struct iov_store_st ptext;
|
|
Packit Service |
4684c1 |
size_t ptext_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_from_iov(&auth, auth_iov, auth_iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_from_iov(&ptext, iov, iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ptext_size = ptext.size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* append space for tag */
|
|
Packit Service |
4684c1 |
ret = iov_store_grow(&ptext, _tag_size);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_aead_cipher_encrypt(handle, nonce, nonce_len,
|
|
Packit Service |
4684c1 |
auth.data, auth.size,
|
|
Packit Service |
4684c1 |
_tag_size,
|
|
Packit Service |
4684c1 |
ptext.data, ptext_size,
|
|
Packit Service |
4684c1 |
ptext.data, &ptext.size);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_to_iov(&ptext, ptext_size, iov, iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag != NULL)
|
|
Packit Service |
4684c1 |
memcpy(tag,
|
|
Packit Service |
4684c1 |
(uint8_t *) ptext.data + ptext_size,
|
|
Packit Service |
4684c1 |
_tag_size);
|
|
Packit Service |
4684c1 |
if (tag_size != NULL)
|
|
Packit Service |
4684c1 |
*tag_size = _tag_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fallback_fail:
|
|
Packit Service |
4684c1 |
iov_store_free(&auth);
|
|
Packit Service |
4684c1 |
iov_store_free(&ptext);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return ret;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
while (1) {
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_next(&iter, &p);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
if (ret == 0)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
while (1) {
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_next(&iter, &p);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
if (ret == 0)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
len = ret;
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_encrypt2(&handle->ctx_enc, p, len, p, len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_sync(&iter, p, len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag != NULL)
|
|
Packit Service |
4684c1 |
_gnutls_cipher_tag(&handle->ctx_enc, tag, _tag_size);
|
|
Packit Service |
4684c1 |
if (tag_size != NULL)
|
|
Packit Service |
4684c1 |
*tag_size = _tag_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_aead_cipher_decryptv2:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_aead_cipher_hd_t type.
|
|
Packit Service |
4684c1 |
* @nonce: the nonce to set
|
|
Packit Service |
4684c1 |
* @nonce_len: The length of the nonce
|
|
Packit Service |
4684c1 |
* @auth_iov: additional data to be authenticated
|
|
Packit Service |
4684c1 |
* @auth_iovcnt: The number of buffers in @auth_iov
|
|
Packit Service |
4684c1 |
* @iov: the data to decrypt
|
|
Packit Service |
4684c1 |
* @iovcnt: The number of buffers in @iov
|
|
Packit Service |
4684c1 |
* @tag: The authentication tag
|
|
Packit Service |
4684c1 |
* @tag_size: The size of the tag to use (use zero for the default)
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This is similar to gnutls_aead_cipher_decrypt(), but it performs
|
|
Packit Service |
4684c1 |
* in-place encryption on the provided data buffers.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.10
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_aead_cipher_decryptv2(gnutls_aead_cipher_hd_t handle,
|
|
Packit Service |
4684c1 |
const void *nonce, size_t nonce_len,
|
|
Packit Service |
4684c1 |
const giovec_t *auth_iov, int auth_iovcnt,
|
|
Packit Service |
4684c1 |
const giovec_t *iov, int iovcnt,
|
|
Packit Service |
4684c1 |
void *tag, size_t tag_size)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
api_aead_cipher_hd_st *h = handle;
|
|
Packit Service |
4684c1 |
ssize_t ret;
|
|
Packit Service |
4684c1 |
uint8_t *p;
|
|
Packit Service |
4684c1 |
size_t len;
|
|
Packit Service |
4684c1 |
ssize_t blocksize = handle->ctx_enc.e->blocksize;
|
|
Packit Service |
4684c1 |
struct iov_iter_st iter;
|
|
Packit Service |
4684c1 |
uint8_t _tag[MAX_HASH_SIZE];
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag_size == 0)
|
|
Packit Service |
4684c1 |
tag_size = _gnutls_cipher_get_tag_size(h->ctx_enc.e);
|
|
Packit Service |
4684c1 |
else if (tag_size > (unsigned)_gnutls_cipher_get_tag_size(h->ctx_enc.e))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* Limitation: this function provides an optimization under the internally registered
|
|
Packit Service |
4684c1 |
* AEAD ciphers. When an AEAD cipher is used registered with gnutls_crypto_register_aead_cipher(),
|
|
Packit Service |
4684c1 |
* then this becomes a convenience function as it missed the lower-level primitives
|
|
Packit Service |
4684c1 |
* necessary for piecemeal encryption. */
|
|
Packit Service |
4684c1 |
if ((handle->ctx_enc.e->flags & GNUTLS_CIPHER_FLAG_ONLY_AEAD) || handle->ctx_enc.encrypt == NULL) {
|
|
Packit Service |
4684c1 |
/* ciphertext cannot be produced in a piecemeal approach */
|
|
Packit Service |
4684c1 |
struct iov_store_st auth;
|
|
Packit Service |
4684c1 |
struct iov_store_st ctext;
|
|
Packit Service |
4684c1 |
size_t ctext_size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_from_iov(&auth, auth_iov, auth_iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_from_iov(&ctext, iov, iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ctext_size = ctext.size;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/* append tag */
|
|
Packit Service |
4684c1 |
ret = iov_store_grow(&ctext, tag_size);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
memcpy((uint8_t *) ctext.data + ctext_size, tag, tag_size);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = gnutls_aead_cipher_decrypt(handle, nonce, nonce_len,
|
|
Packit Service |
4684c1 |
auth.data, auth.size,
|
|
Packit Service |
4684c1 |
tag_size,
|
|
Packit Service |
4684c1 |
ctext.data, ctext.size,
|
|
Packit Service |
4684c1 |
ctext.data, &ctext_size);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = copy_to_iov(&ctext, ctext_size, iov, iovcnt);
|
|
Packit Service |
4684c1 |
if (ret < 0) {
|
|
Packit Service |
4684c1 |
gnutls_assert();
|
|
Packit Service |
4684c1 |
goto fallback_fail;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
fallback_fail:
|
|
Packit Service |
4684c1 |
iov_store_free(&auth);
|
|
Packit Service |
4684c1 |
iov_store_free(&ctext);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return ret;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_setiv(&handle->ctx_enc, nonce, nonce_len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_init(&iter, auth_iov, auth_iovcnt, blocksize);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
while (1) {
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_next(&iter, &p);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
if (ret == 0)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_auth(&handle->ctx_enc, p, ret);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_init(&iter, iov, iovcnt, blocksize);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
while (1) {
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_next(&iter, &p);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
if (ret == 0)
|
|
Packit Service |
4684c1 |
break;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
len = ret;
|
|
Packit Service |
4684c1 |
ret = _gnutls_cipher_decrypt2(&handle->ctx_enc, p, len, p, len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
ret = _gnutls_iov_iter_sync(&iter, p, len);
|
|
Packit Service |
4684c1 |
if (unlikely(ret < 0))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(ret);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
if (tag != NULL) {
|
|
Packit Service |
4684c1 |
_gnutls_cipher_tag(&handle->ctx_enc, _tag, tag_size);
|
|
Packit Service |
4684c1 |
if (gnutls_memcmp(_tag, tag, tag_size) != 0)
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_DECRYPTION_FAILED);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return 0;
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_aead_cipher_deinit:
|
|
Packit Service |
4684c1 |
* @handle: is a #gnutls_aead_cipher_hd_t type.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will deinitialize all resources occupied by the given
|
|
Packit Service |
4684c1 |
* authenticated-encryption context.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.4.0
|
|
Packit Service |
4684c1 |
**/
|
|
Packit Service |
4684c1 |
void gnutls_aead_cipher_deinit(gnutls_aead_cipher_hd_t handle)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
_gnutls_aead_cipher_deinit(handle);
|
|
Packit Service |
4684c1 |
gnutls_free(handle);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
extern gnutls_crypto_kdf_st _gnutls_kdf_ops;
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hkdf_extract:
|
|
Packit Service |
4684c1 |
* @mac: the mac algorithm used internally
|
|
Packit Service |
4684c1 |
* @key: the initial keying material
|
|
Packit Service |
4684c1 |
* @salt: the optional salt
|
|
Packit Service |
4684c1 |
* @output: the output value of the extract operation
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will derive a fixed-size key using the HKDF-Extract
|
|
Packit Service |
4684c1 |
* function as defined in RFC 5869.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.13
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_hkdf_extract(gnutls_mac_algorithm_t mac,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *key,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *salt,
|
|
Packit Service |
4684c1 |
void *output)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
/* MD5 is only allowed internally for TLS */
|
|
Packit Service |
4684c1 |
if (is_mac_algo_forbidden(mac))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_kdf_ops.hkdf_extract(mac, key->data, key->size,
|
|
Packit Service |
4684c1 |
salt ? salt->data : NULL,
|
|
Packit Service |
4684c1 |
salt ? salt->size : 0,
|
|
Packit Service |
4684c1 |
output);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_hkdf_expand:
|
|
Packit Service |
4684c1 |
* @mac: the mac algorithm used internally
|
|
Packit Service |
4684c1 |
* @key: the pseudorandom key created with HKDF-Extract
|
|
Packit Service |
4684c1 |
* @info: the optional informational data
|
|
Packit Service |
4684c1 |
* @output: the output value of the expand operation
|
|
Packit Service |
4684c1 |
* @length: the desired length of the output key
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will derive a variable length keying material from
|
|
Packit Service |
4684c1 |
* the pseudorandom key using the HKDF-Expand function as defined in
|
|
Packit Service |
4684c1 |
* RFC 5869.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.13
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_hkdf_expand(gnutls_mac_algorithm_t mac,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *key,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *info,
|
|
Packit Service |
4684c1 |
void *output, size_t length)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
/* MD5 is only allowed internally for TLS */
|
|
Packit Service |
4684c1 |
if (is_mac_algo_forbidden(mac))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_kdf_ops.hkdf_expand(mac, key->data, key->size,
|
|
Packit Service |
4684c1 |
info->data, info->size,
|
|
Packit Service |
4684c1 |
output, length);
|
|
Packit Service |
4684c1 |
}
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
/**
|
|
Packit Service |
4684c1 |
* gnutls_pbkdf2:
|
|
Packit Service |
4684c1 |
* @mac: the mac algorithm used internally
|
|
Packit Service |
4684c1 |
* @key: the initial keying material
|
|
Packit Service |
4684c1 |
* @salt: the salt
|
|
Packit Service |
4684c1 |
* @iter_count: the iteration count
|
|
Packit Service |
4684c1 |
* @output: the output value
|
|
Packit Service |
4684c1 |
* @length: the desired length of the output key
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* This function will derive a variable length keying material from
|
|
Packit Service |
4684c1 |
* a password according to PKCS #5 PBKDF2.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Returns: Zero or a negative error code on error.
|
|
Packit Service |
4684c1 |
*
|
|
Packit Service |
4684c1 |
* Since: 3.6.13
|
|
Packit Service |
4684c1 |
*/
|
|
Packit Service |
4684c1 |
int
|
|
Packit Service |
4684c1 |
gnutls_pbkdf2(gnutls_mac_algorithm_t mac,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *key,
|
|
Packit Service |
4684c1 |
const gnutls_datum_t *salt,
|
|
Packit Service |
4684c1 |
unsigned iter_count,
|
|
Packit Service |
4684c1 |
void *output, size_t length)
|
|
Packit Service |
4684c1 |
{
|
|
Packit Service |
4684c1 |
/* MD5 is only allowed internally for TLS */
|
|
Packit Service |
4684c1 |
if (is_mac_algo_forbidden(mac))
|
|
Packit Service |
4684c1 |
return gnutls_assert_val(GNUTLS_E_UNWANTED_ALGORITHM);
|
|
Packit Service |
4684c1 |
|
|
Packit Service |
4684c1 |
return _gnutls_kdf_ops.pbkdf2(mac, key->data, key->size,
|
|
Packit Service |
4684c1 |
salt->data, salt->size, iter_count,
|
|
Packit Service |
4684c1 |
output, length);
|
|
Packit Service |
4684c1 |
}
|