Blame tests/gnutls_record_overhead.c

Packit aea12f
/*
Packit aea12f
 * Copyright (C) 2017 Red Hat, Inc.
Packit aea12f
 *
Packit aea12f
 * Author: Nikos Mavrogiannopoulos
Packit aea12f
 *
Packit aea12f
 * This file is part of GnuTLS.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is free software: you can redistribute it and/or modify it
Packit aea12f
 * under the terms of the GNU General Public License as published by
Packit aea12f
 * the Free Software Foundation, either version 3 of the License, or
Packit aea12f
 * (at your option) any later version.
Packit aea12f
 *
Packit aea12f
 * GnuTLS is distributed in the hope that it will be useful, but
Packit aea12f
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit aea12f
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit aea12f
 * General Public License for more details.
Packit aea12f
 *
Packit aea12f
 * You should have received a copy of the GNU General Public License
Packit aea12f
 * along with GnuTLS.  If not, see <https://www.gnu.org/licenses/>.
Packit aea12f
 *
Packit aea12f
 */
Packit aea12f
Packit aea12f
#ifdef HAVE_CONFIG_H
Packit aea12f
#include <config.h>
Packit aea12f
#endif
Packit aea12f
Packit aea12f
/* This is a unit test of _gnutls_record_overhead. */
Packit aea12f
Packit aea12f
#include <stdio.h>
Packit aea12f
#include <stdlib.h>
Packit aea12f
#include <unistd.h>
Packit aea12f
#include <sys/types.h>
Packit aea12f
#include <sys/time.h>
Packit aea12f
#include <errno.h>
Packit aea12f
#include <stdint.h>
Packit aea12f
#include <stddef.h>
Packit aea12f
#include <setjmp.h>
Packit aea12f
#include <cmocka.h>
Packit aea12f
Packit aea12f
Packit aea12f
#include <gnutls/gnutls.h>
Packit aea12f
#include "../lib/gnutls_int.h"
Packit aea12f
Packit aea12f
#undef _gnutls_debug_log
Packit aea12f
#undef gnutls_assert
Packit aea12f
#undef gnutls_assert_val
Packit aea12f
#define _gnutls_debug_log printf
Packit aea12f
#define gnutls_assert()
Packit aea12f
#define gnutls_assert_val(val) val
Packit aea12f
Packit aea12f
/* #pragma doesn't work to suppress preprocessor warnings like -Wunused-macros.
Packit aea12f
 * So we just use the above defined macros here. */
Packit aea12f
#if defined _gnutls_debug_log && defined gnutls_assert && defined gnutls_assert_val
Packit aea12f
#include "../lib/algorithms.h"
Packit aea12f
#endif
Packit aea12f
Packit aea12f
unsigned _gnutls_record_overhead(const version_entry_st *ver,
Packit aea12f
				 const cipher_entry_st *cipher,
Packit aea12f
				 const mac_entry_st *mac,
Packit aea12f
				 unsigned max);
Packit aea12f
Packit aea12f
#define OVERHEAD(v, c, m)						\
Packit aea12f
	_gnutls_record_overhead(version_to_entry(v), cipher_to_entry(c), mac_to_entry(m), \
Packit aea12f
				0)
Packit aea12f
Packit aea12f
#define MAX_OVERHEAD(v, c, m)						\
Packit aea12f
	_gnutls_record_overhead(version_to_entry(v), cipher_to_entry(c), mac_to_entry(m), \
Packit aea12f
				1)
Packit aea12f
Packit aea12f
static void check_aes_gcm(void **glob_state)
Packit aea12f
{
Packit aea12f
	const unsigned ov = 16+8;
Packit aea12f
	/* Under AES-GCM the overhead is constant */
Packit aea12f
	assert_int_equal(OVERHEAD(GNUTLS_TLS1_2, GNUTLS_CIPHER_AES_128_GCM, GNUTLS_MAC_AEAD), ov);
Packit aea12f
	assert_int_equal(MAX_OVERHEAD(GNUTLS_TLS1_2, GNUTLS_CIPHER_AES_128_GCM, GNUTLS_MAC_AEAD), ov);
Packit aea12f
}
Packit aea12f
Packit aea12f
static void check_tls13_aes_gcm(void **glob_state)
Packit aea12f
{
Packit aea12f
	const unsigned ov = 16+1;
Packit aea12f
	/* Under AES-GCM the overhead is constant */
Packit aea12f
	assert_int_equal(OVERHEAD(GNUTLS_TLS1_3, GNUTLS_CIPHER_AES_128_GCM, GNUTLS_MAC_AEAD), ov);
Packit aea12f
	assert_int_equal(MAX_OVERHEAD(GNUTLS_TLS1_3, GNUTLS_CIPHER_AES_128_GCM, GNUTLS_MAC_AEAD), ov);
Packit aea12f
}
Packit aea12f
Packit aea12f
static void check_aes_sha1_min(void **glob_state)
Packit aea12f
{
Packit aea12f
	const unsigned mac = 20;
Packit aea12f
	const unsigned block = 16;
Packit aea12f
	assert_int_equal(OVERHEAD(GNUTLS_TLS1_2, GNUTLS_CIPHER_AES_128_CBC, GNUTLS_MAC_SHA1), 1+mac+block);
Packit aea12f
}
Packit aea12f
Packit aea12f
static void check_aes_sha1_max(void **glob_state)
Packit aea12f
{
Packit aea12f
	const unsigned mac = 20;
Packit aea12f
	const unsigned block = 16;
Packit aea12f
Packit aea12f
	assert_int_equal(MAX_OVERHEAD(GNUTLS_TLS1_2, GNUTLS_CIPHER_AES_128_CBC, GNUTLS_MAC_SHA1), block+mac+block);
Packit aea12f
}
Packit aea12f
Packit aea12f
int main(void)
Packit aea12f
{
Packit aea12f
	const struct CMUnitTest tests[] = {
Packit aea12f
		cmocka_unit_test(check_aes_gcm),
Packit aea12f
		cmocka_unit_test(check_tls13_aes_gcm),
Packit aea12f
		cmocka_unit_test(check_aes_sha1_min),
Packit aea12f
		cmocka_unit_test(check_aes_sha1_max)
Packit aea12f
	};
Packit aea12f
	return cmocka_run_group_tests(tests, NULL, NULL);
Packit aea12f
}