Blame tests/ecdh-compute.c

Packit Service 4684c1
/*
Packit Service 4684c1
 * Copyright (C) 2019 Red Hat, Inc.
Packit Service 4684c1
 *
Packit Service 4684c1
 * Author: Simo Sorce
Packit Service 4684c1
 *
Packit Service 4684c1
 * This file is part of GnuTLS.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS is free software; you can redistribute it and/or modify it
Packit Service 4684c1
 * under the terms of the GNU General Public License as published by
Packit Service 4684c1
 * the Free Software Foundation; either version 3 of the License, or
Packit Service 4684c1
 * (at your option) any later version.
Packit Service 4684c1
 *
Packit Service 4684c1
 * GnuTLS 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
 * General Public License for more details.
Packit Service 4684c1
 *
Packit Service 4684c1
 * You should have received a copy of the GNU General Public License
Packit Service 4684c1
 * along with GnuTLS; if not, write to the Free Software Foundation,
Packit Service 4684c1
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
Packit Service 4684c1
 */
Packit Service 4684c1
Packit Service 4684c1
/* This program tests functionality of DH exchanges */
Packit Service 4684c1
Packit Service 4684c1
#ifdef HAVE_CONFIG_H
Packit Service 4684c1
#include <config.h>
Packit Service 4684c1
#endif
Packit Service 4684c1
Packit Service 4684c1
#include <gnutls/gnutls.h>
Packit Service 4684c1
#include <stdbool.h>
Packit Service 4684c1
#include <string.h>
Packit Service 4684c1
#include <stdlib.h>
Packit Service 4684c1
#include "utils.h"
Packit Service 4684c1
Packit Service 4684c1
#ifdef ENABLE_FIPS140
Packit Service 4684c1
int _gnutls_ecdh_compute_key(gnutls_ecc_curve_t curve,
Packit Service 4684c1
			   const gnutls_datum_t *x, const gnutls_datum_t *y,
Packit Service 4684c1
			   const gnutls_datum_t *k,
Packit Service 4684c1
			   const gnutls_datum_t *peer_x, const gnutls_datum_t *peer_y,
Packit Service 4684c1
			   gnutls_datum_t *Z);
Packit Service 4684c1
Packit Service 4684c1
int _gnutls_ecdh_generate_key(gnutls_ecc_curve_t curve,
Packit Service 4684c1
			      gnutls_datum_t *x, gnutls_datum_t *y,
Packit Service 4684c1
			      gnutls_datum_t *k);
Packit Service 4684c1
Packit Service 4684c1
static void genkey(gnutls_ecc_curve_t curve, gnutls_datum_t *x,
Packit Service 4684c1
                   gnutls_datum_t *y, gnutls_datum_t *key)
Packit Service 4684c1
{
Packit Service 4684c1
	int ret;
Packit Service 4684c1
Packit Service 4684c1
	ret = _gnutls_ecdh_generate_key(curve, x, y, key);
Packit Service 4684c1
	if (ret != 0)
Packit Service 4684c1
		fail("error\n");
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
static void compute_key(gnutls_ecc_curve_t curve, gnutls_datum_t *x,
Packit Service 4684c1
			gnutls_datum_t *y, gnutls_datum_t *key,
Packit Service 4684c1
			const gnutls_datum_t *peer_x,
Packit Service 4684c1
			const gnutls_datum_t *peer_y,
Packit Service 4684c1
                        int expect_error,
Packit Service 4684c1
			gnutls_datum_t *result, bool expect_success)
Packit Service 4684c1
{
Packit Service 4684c1
	gnutls_datum_t Z = { 0 };
Packit Service 4684c1
	bool success;
Packit Service 4684c1
	int ret;
Packit Service 4684c1
Packit Service 4684c1
	ret = _gnutls_ecdh_compute_key(curve, x, y, key, peer_x, peer_y, &Z);
Packit Service 4684c1
	if (expect_error != ret)
Packit Service 4684c1
		fail("error (%d)\n", ret);
Packit Service 4684c1
Packit Service 4684c1
	if (result) {
Packit Service 4684c1
		success = (Z.size != result->size &&
Packit Service 4684c1
			   memcmp(Z.data, result->data, Z.size));
Packit Service 4684c1
		if (success != expect_success)
Packit Service 4684c1
			fail("error\n");
Packit Service 4684c1
	}
Packit Service 4684c1
	gnutls_free(Z.data);
Packit Service 4684c1
}
Packit Service 4684c1
Packit Service 4684c1
struct dh_test_data {
Packit Service 4684c1
	gnutls_ecc_curve_t curve;
Packit Service 4684c1
	const gnutls_datum_t x;
Packit Service 4684c1
	const gnutls_datum_t y;
Packit Service 4684c1
	const gnutls_datum_t key;
Packit Service 4684c1
	const gnutls_datum_t peer_x;
Packit Service 4684c1
	const gnutls_datum_t peer_y;
Packit Service 4684c1
	int expected_error;
Packit Service 4684c1
};
Packit Service 4684c1
Packit Service 4684c1
void doit(void)
Packit Service 4684c1
{
Packit Service 4684c1
	struct dh_test_data test_data[] = {
Packit Service 4684c1
		{
Packit Service 4684c1
                        /* x == 0, y == 0 */
Packit Service 4684c1
			GNUTLS_ECC_CURVE_SECP256R1,
Packit Service 4684c1
                        { 0 }, { 0 }, { 0 },
Packit Service 4684c1
			{ (void *)"\x00", 1 },
Packit Service 4684c1
			{ (void *)"\x00", 1 },
Packit Service 4684c1
			/* Should be GNUTLS_E_PK_INVALID_PUBKEY but mpi scan
Packit Service 4684c1
                         * balks on values of 0 */
Packit Service 4684c1
                        GNUTLS_E_MPI_SCAN_FAILED,
Packit Service 4684c1
		},
Packit Service 4684c1
		{
Packit Service 4684c1
                        /* x > p -1 */
Packit Service 4684c1
			GNUTLS_ECC_CURVE_SECP256R1,
Packit Service 4684c1
                        { 0 }, { 0 }, { 0 },
Packit Service 4684c1
			{ (void *)"\xff\xff\xff\xff\x00\x00\x00\x01"
Packit Service 4684c1
                                  "\x00\x00\x00\x00\x00\x00\x00\x00"
Packit Service 4684c1
                                  "\x00\x00\x00\x00\xff\xff\xff\xff"
Packit Service 4684c1
                                  "\xff\xff\xff\xff\xff\xff\xff\xff", 1 },
Packit Service 4684c1
			{ (void *)"\x02", 1 },
Packit Service 4684c1
			GNUTLS_E_PK_INVALID_PUBKEY,
Packit Service 4684c1
		},
Packit Service 4684c1
		{
Packit Service 4684c1
                        /* y > p -1 */
Packit Service 4684c1
			GNUTLS_ECC_CURVE_SECP256R1,
Packit Service 4684c1
                        { 0 }, { 0 }, { 0 },
Packit Service 4684c1
			{ (void *)"\x02", 1 },
Packit Service 4684c1
			{ (void *)"\xff\xff\xff\xff\x00\x00\x00\x01"
Packit Service 4684c1
                                  "\x00\x00\x00\x00\x00\x00\x00\x00"
Packit Service 4684c1
                                  "\x00\x00\x00\x00\xff\xff\xff\xff"
Packit Service 4684c1
                                  "\xff\xff\xff\xff\xff\xff\xff\xff", 1 },
Packit Service 4684c1
			GNUTLS_E_PK_INVALID_PUBKEY,
Packit Service 4684c1
		},
Packit Service 4684c1
		{
Packit Service 4684c1
                        /* From CAVS tests */
Packit Service 4684c1
			GNUTLS_ECC_CURVE_SECP521R1,
Packit Service 4684c1
                        { (void *)"\xac\xbe\x4a\xd4\xf6\x73\x44\x0a"
Packit Service 4684c1
                                  "\xfc\x31\xf0\xb0\x3d\x28\xd4\xd5"
Packit Service 4684c1
                                  "\x14\xbe\x7b\xdd\x7a\x31\xb0\x32"
Packit Service 4684c1
                                  "\xec\x27\x27\x17\xa5\x7d\xc2\x6c"
Packit Service 4684c1
                                  "\xc4\xc9\x56\x29\xdb\x2d\x8c\x05"
Packit Service 4684c1
                                  "\x86\x2b\xe6\x15\xc6\x06\x28\xa3"
Packit Service 4684c1
                                  "\x24\xf2\x01\x7f\x98\xbd\xf9\x11"
Packit Service 4684c1
                                  "\xcc\xf8\x83\x5e\x43\x9e\xb2\xc1"
Packit Service 4684c1
                                  "\x88", 65 },
Packit Service 4684c1
                        { (void *)"\xd6\x9b\x29\xa2\x37\x82\x36\x92"
Packit Service 4684c1
                                  "\xe8\xdb\x90\xa3\x25\x68\x67\x6c"
Packit Service 4684c1
                                  "\x92\xff\x3d\x23\x85\xe2\xfd\x13"
Packit Service 4684c1
                                  "\x16\x12\x72\xb3\x4b\x55\x88\x72"
Packit Service 4684c1
                                  "\xb0\x35\xab\xb5\x10\x89\x52\x5f"
Packit Service 4684c1
                                  "\x42\x9f\x53\x02\x60\x80\xc3\xd5"
Packit Service 4684c1
                                  "\x36\x6e\xe9\xdd\x28\xae\xd2\x38"
Packit Service 4684c1
                                  "\xab\xbe\x68\x6a\x54\x3e\x19\xf2"
Packit Service 4684c1
                                  "\x77", 65 },
Packit Service 4684c1
                        { (void *)"\xd7\xdd\x17\x7c\xb9\x7f\x19\x09"
Packit Service 4684c1
                                  "\xbe\x56\x79\xba\x38\x7b\xee\x64"
Packit Service 4684c1
                                  "\xf7\xb4\x08\x4a\x4f\xaa\x6c\x31"
Packit Service 4684c1
                                  "\x8b\x82\xe9\xf2\xf7\x50\xc5\xc1"
Packit Service 4684c1
                                  "\x82\x26\x20\xd4\x88\x25\x0b\xf6"
Packit Service 4684c1
                                  "\xb4\x14\xea\x9b\x2c\x07\x93\x50"
Packit Service 4684c1
                                  "\xb9\xad\x78\x0a\x5e\xc6\xa6\xf8"
Packit Service 4684c1
                                  "\xb2\x9f\xa1\xc4\x76\xce\x1d\xa9"
Packit Service 4684c1
                                  "\xf5", 65 },
Packit Service 4684c1
                        { (void *)"\x01\x41\xbe\x1a\xfa\x21\x99\xc9"
Packit Service 4684c1
                                  "\xb2\x2d\xaa\x0a\xff\x90\xb2\x67"
Packit Service 4684c1
                                  "\x18\xa2\x67\x04\x7e\xae\x28\x40"
Packit Service 4684c1
                                  "\xe8\xbc\xa0\xbd\x0c\x75\x41\x51"
Packit Service 4684c1
                                  "\xf1\xa0\x4d\xcf\x09\xa5\x4f\x1e"
Packit Service 4684c1
                                  "\x13\x5e\xa0\xdd\x13\xed\x86\x74"
Packit Service 4684c1
                                  "\x05\xc0\xcb\x6d\xac\x14\x6a\x24"
Packit Service 4684c1
                                  "\xb8\xdc\xf3\x78\xed\xed\x5d\xcd"
Packit Service 4684c1
                                  "\x57\x5b", 66 },
Packit Service 4684c1
                        { (void *)"\x19\x52\xbd\x5d\xe6\x26\x40\xc3"
Packit Service 4684c1
                                  "\xfc\x8c\xc1\x55\xe2\x9c\x71\x14"
Packit Service 4684c1
                                  "\x5e\xdc\x62\x1c\x3a\x94\x4e\x55"
Packit Service 4684c1
                                  "\x56\x75\xf7\x45\x6e\xa4\x9e\x94"
Packit Service 4684c1
                                  "\xb8\xfe\xda\xd4\xac\x7d\x76\xc5"
Packit Service 4684c1
                                  "\xb4\x65\xed\xb4\x49\x34\x71\x14"
Packit Service 4684c1
                                  "\xdb\x8f\x10\x90\xa3\x05\x02\xdc"
Packit Service 4684c1
                                  "\x86\x92\x6c\xbe\x9b\x57\x32\xe3"
Packit Service 4684c1
                                  "\x2c", 65 },
Packit Service 4684c1
			0,
Packit Service 4684c1
		},
Packit Service 4684c1
		{ 0 }
Packit Service 4684c1
	};
Packit Service 4684c1
Packit Service 4684c1
	for (int i = 0; test_data[i].curve != 0; i++) {
Packit Service 4684c1
		gnutls_datum_t x, y, key;
Packit Service 4684c1
Packit Service 4684c1
                if (test_data[i].key.data == NULL) {
Packit Service 4684c1
		        genkey(test_data[i].curve, &x, &y, &key);
Packit Service 4684c1
                } else {
Packit Service 4684c1
                        x = test_data[i].x;
Packit Service 4684c1
                        y = test_data[i].y;
Packit Service 4684c1
                        key = test_data[i].key;
Packit Service 4684c1
                }
Packit Service 4684c1
Packit Service 4684c1
		compute_key(test_data[i].curve, &x, &y, &key,
Packit Service 4684c1
			    &test_data[i].peer_x,
Packit Service 4684c1
			    &test_data[i].peer_y,
Packit Service 4684c1
			    test_data[i].expected_error,
Packit Service 4684c1
			    NULL, 0);
Packit Service 4684c1
Packit Service 4684c1
                if (test_data[i].key.data == NULL) {
Packit Service 4684c1
		        gnutls_free(x.data);
Packit Service 4684c1
		        gnutls_free(y.data);
Packit Service 4684c1
		        gnutls_free(key.data);
Packit Service 4684c1
                }
Packit Service 4684c1
	}
Packit Service 4684c1
Packit Service 4684c1
	success("all ok\n");
Packit Service 4684c1
}
Packit Service 4684c1
#else
Packit Service 4684c1
void doit(void)
Packit Service 4684c1
{
Packit Service 4684c1
	return;
Packit Service 4684c1
}
Packit Service 4684c1
#endif