/*
 *  Copyright 2014-2026 The GmSSL Project. All Rights Reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the License); you may
 *  not use this file except in compliance with the License.
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 */


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <gmssl/mem.h>
#include <gmssl/error.h>

#ifdef ENABLE_SECP256R1
#include <gmssl/secp256r1_key.h>
#endif
#ifdef ENABLE_SECP384R1
#include <gmssl/secp384r1_key.h>
#endif


#ifdef ENABLE_SECP256R1
int secp256r1_do_ecdh(const SECP256R1_KEY *key, const SECP256R1_KEY *peer_key, uint8_t out[32])
{
	SECP256R1_POINT point;
	secp256r1_t x;
	secp256r1_t y;

	if (!key || !peer_key || !out) {
		error_print();
		return -1;
	}
	if (secp256r1_point_mul(&point, key->private_key, &peer_key->public_key) != 1
		|| secp256r1_point_get_xy(&point, x, y) != 1
		|| secp256r1_to_32bytes(x, out) != 1) {
		error_print();
		gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
		return -1;
	}

	gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
	gmssl_secure_clear(x, sizeof(secp256r1_t));
	gmssl_secure_clear(y, sizeof(secp256r1_t));
	return 1;
}

int secp256r1_ecdh(const SECP256R1_KEY *key, const uint8_t uncompressed_point[65], uint8_t out[32])
{
	SECP256R1_POINT point;
	secp256r1_t x;
	secp256r1_t y;

	if (!key || !uncompressed_point || !out) {
		error_print();
		return -1;
	}
	if (secp256r1_point_from_uncompressed_octets(&point, uncompressed_point) != 1) {
		error_print();
		return -1;
	}
	if (secp256r1_point_mul(&point, key->private_key, &point) != 1
		|| secp256r1_point_get_xy(&point, x,y) != 1
		|| secp256r1_to_32bytes(x, out) != 1) {
		error_print();
		gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
		return -1;
	}

	gmssl_secure_clear(&point, sizeof(SECP256R1_POINT));
	gmssl_secure_clear(x, sizeof(secp256r1_t));
	gmssl_secure_clear(y, sizeof(secp256r1_t));
	return 1;
}
#endif

#ifdef ENABLE_SECP384R1
int secp384r1_do_ecdh(const SECP384R1_KEY *key, const SECP384R1_KEY *peer_key, uint8_t out[48])
{
	SECP384R1_POINT point;
	secp384r1_t x;
	secp384r1_t y;

	if (!key || !peer_key || !out) {
		error_print();
		return -1;
	}
	if (secp384r1_point_mul(&point, key->private_key, &peer_key->public_key) != 1
		|| secp384r1_point_get_xy(&point, x, y) != 1
		|| secp384r1_to_48bytes(x, out) != 1) {
		error_print();
		gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
		return -1;
	}

	gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
	gmssl_secure_clear(x, sizeof(secp384r1_t));
	gmssl_secure_clear(y, sizeof(secp384r1_t));
	return 1;
}

int secp384r1_ecdh(const SECP384R1_KEY *key, const uint8_t uncompressed_point[97], uint8_t out[48])
{
	SECP384R1_POINT point;
	secp384r1_t x;
	secp384r1_t y;

	if (!key || !uncompressed_point || !out) {
		error_print();
		return -1;
	}
	if (secp384r1_point_from_uncompressed_octets(&point, uncompressed_point) != 1) {
		error_print();
		return -1;
	}
	if (secp384r1_point_mul(&point, key->private_key, &point) != 1
		|| secp384r1_point_get_xy(&point, x, y) != 1
		|| secp384r1_to_48bytes(x, out) != 1) {
		error_print();
		gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
		return -1;
	}

	gmssl_secure_clear(&point, sizeof(SECP384R1_POINT));
	gmssl_secure_clear(x, sizeof(secp384r1_t));
	gmssl_secure_clear(y, sizeof(secp384r1_t));
	return 1;
}
#endif
