<commit_before>/*-
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "config.h"
#include "cryptobox.h"
#include "ed25519.h"
#include "platform_config.h"
#include "libutil/str_util.h"

extern unsigned long cpu_config;

typedef struct ed25519_impl_s {
	unsigned long cpu_flags;
	const char *desc;

	void (*keypair) (unsigned char *pk, unsigned char *sk);
	void (*sign) (unsigned char *sig, size_t *siglen_p,
			const unsigned char *m, size_t mlen,
			const unsigned char *sk);
	bool (*verify) (const unsigned char *sig,
			const unsigned char *m,
			size_t mlen,
			const unsigned char *pk);
} ed25519_impl_t;

#define ED25519_DECLARE(ext) \
    void ed_keypair_##ext(unsigned char *pk, unsigned char *sk); \
    void ed_sign_##ext(unsigned char *sig, size_t *siglen_p, \
        const unsigned char *m, size_t mlen, \
        const unsigned char *sk); \
    bool ed_verify_##ext(const unsigned char *sig, \
        const unsigned char *m, \
		size_t mlen, \
        const unsigned char *pk)

#define ED25519_IMPL(cpuflags, desc, ext) \
    {(cpuflags), desc, ed_keypair_##ext, ed_sign_##ext, ed_verify_##ext}

ED25519_DECLARE(ref);
#define ED25519_REF ED25519_IMPL(0, "ref", ref)

static const ed25519_impl_t ed25519_list[] = {
		ED25519_REF,
};

static const ed25519_impl_t *ed25519_opt = &ed25519_list[0];
static bool ed25519_test (const ed25519_impl_t *impl);

const char*
ed25519_load (void)
{
	guint i;

	if (cpu_config != 0) {
		for (i = 0; i < G_N_ELEMENTS(ed25519_list); i++) {
			if (ed25519_list[i].cpu_flags & cpu_config) {
				ed25519_opt = &ed25519_list[i];
				g_assert (ed25519_test (ed25519_opt));
				break;
			}
		}
	}

	g_assert (ed25519_test (ed25519_opt));

	return ed25519_opt->desc;
}

void
ed25519_keypair (unsigned char *pk, unsigned char *sk)
{
	ed25519_opt->keypair (pk, sk);
}

void
ed25519_sign (unsigned char *sig, size_t *siglen_p,
		const unsigned char *m, size_t mlen,
		const unsigned char *sk)
{
	ed25519_opt->sign (sig, siglen_p, m, mlen, sk);
}

bool
ed25519_verify (const unsigned char *sig,
		const unsigned char *m,
		size_t mlen,
		const unsigned char *pk)
{
	return (ed25519_opt->verify (sig, m, mlen, pk) == 0);
}

struct ed25519_test_vector {
	const char *message;
	const char *pk;
	const char *sk;
	const char *sig;<commit_msg>Fix verification routine for signatures<commit_after>/*-
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "config.h"
#include "cryptobox.h"
#include "ed25519.h"
#include "platform_config.h"
#include "libutil/str_util.h"

extern unsigned long cpu_config;

typedef struct ed25519_impl_s {
	unsigned long cpu_flags;
	const char *desc;

	void (*keypair) (unsigned char *pk, unsigned char *sk);
	void (*sign) (unsigned char *sig, size_t *siglen_p,
			const unsigned char *m, size_t mlen,
			const unsigned char *sk);
	int (*verify) (const unsigned char *sig,
			const unsigned char *m,
			size_t mlen,
			const unsigned char *pk);
} ed25519_impl_t;

#define ED25519_DECLARE(ext) \
    void ed_keypair_##ext(unsigned char *pk, unsigned char *sk); \
    void ed_sign_##ext(unsigned char *sig, size_t *siglen_p, \
        const unsigned char *m, size_t mlen, \
        const unsigned char *sk); \
    int ed_verify_##ext(const unsigned char *sig, \
        const unsigned char *m, \
		size_t mlen, \
        const unsigned char *pk)

#define ED25519_IMPL(cpuflags, desc, ext) \
    {(cpuflags), desc, ed_keypair_##ext, ed_sign_##ext, ed_verify_##ext}

ED25519_DECLARE(ref);
#define ED25519_REF ED25519_IMPL(0, "ref", ref)

static const ed25519_impl_t ed25519_list[] = {
		ED25519_REF,
};

static const ed25519_impl_t *ed25519_opt = &ed25519_list[0];
static bool ed25519_test (const ed25519_impl_t *impl);

const char*
ed25519_load (void)
{
	guint i;

	if (cpu_config != 0) {
		for (i = 0; i < G_N_ELEMENTS(ed25519_list); i++) {
			if (ed25519_list[i].cpu_flags & cpu_config) {
				ed25519_opt = &ed25519_list[i];
				g_assert (ed25519_test (ed25519_opt));
				break;
			}
		}
	}

	g_assert (ed25519_test (ed25519_opt));

	return ed25519_opt->desc;
}

void
ed25519_keypair (unsigned char *pk, unsigned char *sk)
{
	ed25519_opt->keypair (pk, sk);
}

void
ed25519_sign (unsigned char *sig, size_t *siglen_p,
		const unsigned char *m, size_t mlen,
		const unsigned char *sk)
{
	ed25519_opt->sign (sig, siglen_p, m, mlen, sk);
}

bool
ed25519_verify (const unsigned char *sig,
		const unsigned char *m,
		size_t mlen,
		const unsigned char *pk)
{
	int ret = ed25519_opt->verify (sig, m, mlen, pk);

	return (ret == 0 ? true : false);
}

struct ed25519_test_vector {
	const char *message;
	const char *pk;
	const char *sk;
	const char *sig;
{'fraction_non_alphanumeric': 0.11150193457615196, 'fraction_numerical': 0.06260991909954274, 'mean_word_length': 4.100448430493273, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7538561', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2031, 'n_words': 644}