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