// pub-verify -- verify a chain of cm/pub keys
// Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include <monocypher.h>

#include "verifier-ed25519-blake2b.h"

bool
ed25519blake2bSignatureVerifier(
    char **failReason,
    const unsigned char *ai,
    const size_t aiLen,
    const unsigned char *sig,
    const size_t sigLen,
    const unsigned char *pub,
    const size_t pubLen,
    const unsigned char *data,
    const size_t dataLen)
{
    if (pubLen != 32) {
        (*failReason) = "invalid pubkey len";
        return false;
    }
    if (sigLen != 64) {
        (*failReason) = "invalid signature len";
        return false;
    }
    if (crypto_eddsa_check(sig, pub, data, dataLen) != 0) {
        (*failReason) = "bad signature";
        return false;
    }
    return true;
}
