Blame tests/gnutls_record_overhead.c

Packit 549fdc
/*
Packit 549fdc
 * Copyright (C) 2017 Red Hat, Inc.
Packit 549fdc
 *
Packit 549fdc
 * Author: Nikos Mavrogiannopoulos
Packit 549fdc
 *
Packit 549fdc
 * This file is part of GnuTLS.
Packit 549fdc
 *
Packit 549fdc
 * GnuTLS is free software: you can redistribute it and/or modify it
Packit 549fdc
 * under the terms of the GNU General Public License as published by
Packit 549fdc
 * the Free Software Foundation, either version 3 of the License, or
Packit 549fdc
 * (at your option) any later version.
Packit 549fdc
 *
Packit 549fdc
 * GnuTLS 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
 * General Public License for more details.
Packit 549fdc
 *
Packit 549fdc
 * You should have received a copy of the GNU General Public License
Packit 549fdc
 * along with GnuTLS.  If not, see <http://www.gnu.org/licenses/>.
Packit 549fdc
 *
Packit 549fdc
 */
Packit 549fdc
Packit 549fdc
#ifdef HAVE_CONFIG_H
Packit 549fdc
#include <config.h>
Packit 549fdc
#endif
Packit 549fdc
Packit 549fdc
/* This is a unit test of _gnutls_record_overhead. */
Packit 549fdc
Packit 549fdc
#include <stdio.h>
Packit 549fdc
#include <stdlib.h>
Packit 549fdc
#include <unistd.h>
Packit 549fdc
#include <sys/types.h>
Packit 549fdc
#include <sys/time.h>
Packit 549fdc
#include <errno.h>
Packit 549fdc
#include <stdint.h>
Packit 549fdc
#include <stddef.h>
Packit 549fdc
#include <setjmp.h>
Packit 549fdc
#include <cmocka.h>
Packit 549fdc
Packit 549fdc
Packit 549fdc
#include <gnutls/gnutls.h>
Packit 549fdc
#include "../lib/gnutls_int.h"
Packit 549fdc
Packit 549fdc
#undef _gnutls_debug_log
Packit 549fdc
#undef gnutls_assert
Packit 549fdc
#undef gnutls_assert_val
Packit 549fdc
#define _gnutls_debug_log printf
Packit 549fdc
#define gnutls_assert()
Packit 549fdc
#define gnutls_assert_val(val) val
Packit 549fdc
Packit 549fdc
#include "../lib/algorithms.h"
Packit 549fdc
Packit 549fdc
Packit 549fdc
int _gnutls_record_overhead(const cipher_entry_st * cipher,
Packit 549fdc
			    const mac_entry_st * mac,
Packit 549fdc
			    unsigned max);
Packit 549fdc
Packit 549fdc
#define OVERHEAD(c, m) \
Packit 549fdc
	_gnutls_record_overhead(cipher_to_entry(c), mac_to_entry(m), \
Packit 549fdc
				0)
Packit 549fdc
Packit 549fdc
#define MAX_OVERHEAD(c, m) \
Packit 549fdc
	_gnutls_record_overhead(cipher_to_entry(c), mac_to_entry(m), \
Packit 549fdc
				1)
Packit 549fdc
Packit 549fdc
static void check_aes_gcm(void **glob_state)
Packit 549fdc
{
Packit 549fdc
	const unsigned ov = 16+8;
Packit 549fdc
	/* Under AES-GCM the overhead is constant */
Packit 549fdc
	assert_int_equal(OVERHEAD(GNUTLS_CIPHER_AES_128_GCM, GNUTLS_MAC_AEAD), ov);
Packit 549fdc
	assert_int_equal(MAX_OVERHEAD(GNUTLS_CIPHER_AES_128_GCM, GNUTLS_MAC_AEAD), ov);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static void check_aes_sha1_min(void **glob_state)
Packit 549fdc
{
Packit 549fdc
	const unsigned mac = 20;
Packit 549fdc
	const unsigned block = 16;
Packit 549fdc
	assert_int_equal(OVERHEAD(GNUTLS_CIPHER_AES_128_CBC, GNUTLS_MAC_SHA1), 1+mac+block);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
static void check_aes_sha1_max(void **glob_state)
Packit 549fdc
{
Packit 549fdc
	const unsigned mac = 20;
Packit 549fdc
	const unsigned block = 16;
Packit 549fdc
Packit 549fdc
	assert_int_equal(MAX_OVERHEAD(GNUTLS_CIPHER_AES_128_CBC, GNUTLS_MAC_SHA1), block+mac+block);
Packit 549fdc
}
Packit 549fdc
Packit 549fdc
int main(void)
Packit 549fdc
{
Packit 549fdc
	const struct CMUnitTest tests[] = {
Packit 549fdc
		cmocka_unit_test(check_aes_gcm),
Packit 549fdc
		cmocka_unit_test(check_aes_sha1_min),
Packit 549fdc
		cmocka_unit_test(check_aes_sha1_max)
Packit 549fdc
	};
Packit 549fdc
	return cmocka_run_group_tests(tests, NULL, NULL);
Packit 549fdc
}