/*
 * lws-api-test-gencrypto - lws-genhkdf
 *
 * Written in 2010-2026 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 */

#include <libwebsockets.h>

/* RFC 5869 Appendix A.1 */
static const uint8_t rfc5869_a1_ikm[] = {
	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
	0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b
};
static const uint8_t rfc5869_a1_salt[] = {
	0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
	0x08, 0x09, 0x0a, 0x0b, 0x0c
};
static const uint8_t rfc5869_a1_info[] = {
	0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
	0xf8, 0xf9
};
static const uint8_t rfc5869_a1_prk[] = {
	0x07, 0x77, 0x09, 0x36, 0x2c, 0x2e, 0x32, 0xdf,
	0x0d, 0xdc, 0x3f, 0x0d, 0xc4, 0x7b, 0xba, 0x63,
	0x90, 0xb6, 0xc7, 0x3b, 0xb5, 0x0f, 0x9c, 0x31,
	0x22, 0xec, 0x84, 0x4a, 0xd7, 0xc2, 0xb3, 0xe5
};
static const uint8_t rfc5869_a1_okm[] = {
	0x3c, 0xb2, 0x5f, 0x25, 0xfa, 0xac, 0xd5, 0x7a,
	0x90, 0x43, 0x4f, 0x64, 0xd0, 0x36, 0x2f, 0x2a,
	0x2d, 0x2d, 0x0a, 0x90, 0xcf, 0x1a, 0x5a, 0x4c,
	0x5d, 0xb0, 0x2d, 0x56, 0xec, 0xc4, 0xc5, 0xbf,
	0x34, 0x00, 0x72, 0x08, 0xd5, 0xb8, 0x87, 0x18,
	0x58, 0x65
};

int
test_genhkdf(struct lws_context *context)
{
	uint8_t prk[LWS_GENHASH_LARGEST];
	uint8_t okm[42];

	lwsl_user("%s\n", __func__);

	/* HKDF-Extract */
	if (lws_genhkdf_extract(LWS_GENHMAC_TYPE_SHA256,
				rfc5869_a1_salt, sizeof(rfc5869_a1_salt),
				rfc5869_a1_ikm, sizeof(rfc5869_a1_ikm),
				prk)) {
		lwsl_err("%s: extract failed\n", __func__);
		return 1;
	}

	if (memcmp(prk, rfc5869_a1_prk, 32)) {
		lwsl_err("%s: prk mismatch\n", __func__);
		lwsl_hexdump_err(prk, 32);
		return 1;
	}

	/* HKDF-Expand */
	if (lws_genhkdf_expand(LWS_GENHMAC_TYPE_SHA256,
			       prk, 32,
			       rfc5869_a1_info, sizeof(rfc5869_a1_info),
			       okm, sizeof(okm))) {
		lwsl_err("%s: expand failed\n", __func__);
		return 1;
	}

	if (memcmp(okm, rfc5869_a1_okm, sizeof(rfc5869_a1_okm))) {
		lwsl_err("%s: okm mismatch\n", __func__);
		lwsl_hexdump_err(okm, sizeof(okm));
		return 1;
	}

	return 0;
}
