/* ed448_verify.c
 *
 * Copyright (C) 2006-2020 wolfSSL Inc.
 *
 * This file is part of wolfSSL. (formerly known as CyaSSL)
 *
 * wolfSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * wolfSSL 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

/* Example to demonstrate Ed448 Verify */
/* 
./configure --enable-ed448 && make && sudo make install
gcc -lwolfssl -o ed448_verify ed448_verify.c
*/

#ifndef WOLFSSL_USER_SETTINGS
    #include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/sha256.h>
#include <wolfssl/wolfcrypt/ed448.h>
#include <wolfssl/wolfcrypt/integer.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <wolfssl/wolfcrypt/logging.h>

#include <stdint.h>
#include <stdio.h>

#if defined(HAVE_ED448) && defined(HAVE_ED448_VERIFY)

/* Test Vectors */
static const uint8_t kMsg[] = {
    0x69, 0xbc, 0x9f, 0xce, 0x68, 0x17, 0xc2, 0x10,
    0xea, 0xfc, 0x10, 0x65, 0x67, 0x52, 0xed, 0x78,
    0x6e, 0xb8, 0x83, 0x9c, 0x9a, 0xb4, 0x56, 0x0d,
    0xc1, 0x0d, 0x1f, 0x78, 0x6e, 0x75, 0xd7, 0xbe,
    0x92, 0x6b, 0x12, 0xf6, 0x76, 0x60, 0x8e, 0xb1,
    0xf4, 0x19, 0x0c, 0x81, 0xe7, 0x54, 0x5e, 0xbc,
    0xe0, 0xae, 0xc2, 0x7d, 0x1b, 0xc4, 0x6e, 0xec,
    0xb1, 0x99, 0x6c, 0xbf, 0x0e, 0x38, 0xa8, 0x01,
    0xa6, 0x9a, 0x48, 0x12, 0xe4, 0xc9, 0x3b, 0xf0,
    0x63, 0x46, 0x15, 0xb4, 0x61, 0xa8, 0x1a, 0x60,
    0x71, 0x87, 0x98, 0xd7, 0x6f, 0x98, 0x7b, 0x2d,
    0xb9, 0x19, 0x1b, 0x21, 0x9c, 0x70, 0x58, 0xe8,
    0x0d, 0x0f, 0xe9, 0x2d, 0x9a, 0x9a, 0xf1, 0x55,
    0xa0, 0x4c, 0xd3, 0x07, 0xbd, 0x97, 0x48, 0xec,
    0x88, 0x0a, 0xaf, 0xb3, 0x80, 0x78, 0xa4, 0x59,
    0x43, 0x57, 0xd3, 0xa7, 0x01, 0x66, 0x0e, 0xfc
};
static const uint8_t kPubKey[] = {
    /* y */
    0x5d, 0xf2, 0x8f, 0x9a, 0xbe, 0x91, 0x32, 0x1c,
    0x79, 0xf3, 0x13, 0xd8, 0xf4, 0xd6, 0x55, 0x74,
    0x8a, 0x04, 0xe4, 0xe3, 0xaa, 0x21, 0xf8, 0x6b,
    0xb1, 0xdb, 0x26, 0xa7, 0x74, 0x33, 0x87, 0x35,
    0x85, 0xbc, 0x80, 0xe6, 0x92, 0xee, 0xba, 0x24,
    0x5d, 0x6b, 0x4e, 0x37, 0x76, 0xbc, 0xdb, 0xfe,
    0xcc, 0xe9, 0x8b, 0xc5, 0xc8, 0x88, 0xd5, 0xcb,
    0x80
};
static const uint8_t kSigRS[] = {
    /* R */
    0x7D, 0xC5, 0xC9, 0x10, 0xD3, 0x37, 0x4E, 0x3B,
    0x99, 0x7C, 0x37, 0x6A, 0x38, 0x63, 0x65, 0x7F,
    0x75, 0xFD, 0xF4, 0x2B, 0x89, 0x27, 0xBA, 0xEC,
    0x65, 0x0F, 0x1A, 0xE2, 0xAB, 0xB9, 0xF0, 0xA6,
    0xEC, 0xE6, 0xC8, 0xED, 0x54, 0xDD, 0x9F, 0x22,
    0x00, 0x0C, 0x84, 0x33, 0x07, 0xA3, 0x58, 0x8E,
    0x22, 0x37, 0x4E, 0xD8, 0x78, 0x51, 0x7E, 0x44,
    0x80,
    /* S */
    0x97, 0x1A, 0xF1, 0x9E, 0x07, 0x87, 0x9C, 0x1D,
    0xA0, 0x91, 0x3A, 0xDC, 0xAD, 0x3E, 0xEA, 0x70,
    0xE9, 0x05, 0xBC, 0x39, 0xE2, 0xB7, 0x0B, 0x72,
    0x71, 0x20, 0x35, 0x4F, 0x6F, 0x02, 0x48, 0xA2,
    0xAD, 0xE1, 0x50, 0x64, 0xE0, 0x56, 0x6C, 0x74,
    0xB2, 0xF0, 0x8A, 0x57, 0xF5, 0xE9, 0x89, 0xD9,
    0x0D, 0x44, 0x39, 0x69, 0x89, 0xEC, 0xF7, 0x1C,
    0x00
};


/* perform verify of signature and message using public key */
/* key is public y */
/* sig is r + s */
int crypto_ed448_verify(const uint8_t *key, uint32_t keySz,
    const uint8_t *sig, uint32_t sigSz)
{
    int ret, verify_res = 0;
    ed448_key ed448;

    /* validate arguments */
    if (key == NULL || sig == NULL) {
        return BAD_FUNC_ARG;
    }

    /* Setup the Ed448 key */
    ret = wc_ed448_init(&ed448);
    if (ret < 0) {
        return ret;
    }

    /* Import public key x/y */
    ret = wc_ed448_import_public(
        (byte*)key, keySz,      /* public "y" Coordinate */
        &ed448                  /* key context */
    );

    /* Verify Ed448 Signature */
    if (ret == 0) {
        ret = wc_ed448_verify_msg(
            sig, sigSz,         /* r/s encoded */
            kMsg, sizeof(kMsg), /* message */
            &verify_res,        /* verification result 1=success */
            &ed448,             /* key context */
            NULL, 0             /* contex of signature */
        );
    }
    
    /* check verify result */
    if (ret == 0 && verify_res == 0) {
        ret = SIG_VERIFY_E;
    }

    wc_ed448_free(&ed448);

    return ret;
}

int main()
{
    int ret;
    
#ifdef DEBUG_WOLFSSL
    wolfSSL_Debugging_ON();
#endif

    printf("Running Verify Test Vector\n");

    ret = crypto_ed448_verify(
        kPubKey, sizeof(kPubKey), /* public key point x/y */
        kSigRS, sizeof(kSigRS)    /* signature r/s */
    );

    if (ret == 0) {
        printf("Success\n");
    }
    else {
        printf("Failure %d: %s\n", ret, wc_GetErrorString(ret));
        ret = 1;
    }

    return ret;
}

#else

int main()
{
    printf("wolfSSL requires Ed448\n");
    return 1;
}

#endif /* HAVE_ED448 && HAVE_ED448_VERIFY */

