2021-10-11 06:41:44 +02:00
|
|
|
/* openpgp-misc.c - miscellaneous functions for OpenPGP
|
|
|
|
* Copyright (C) 2021 g10 Code GmbH
|
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG 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 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG 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, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-10-08 15:07:14 +02:00
|
|
|
#include <config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
#include "openpgpdefs.h"
|
|
|
|
|
|
|
|
gcry_mpi_t
|
2021-10-08 15:25:05 +02:00
|
|
|
openpgp_ecc_parse_pubkey (pubkey_algo_t pkalgo, const char *curve,
|
2021-10-08 15:07:14 +02:00
|
|
|
gcry_mpi_t pubkey)
|
|
|
|
{
|
|
|
|
unsigned int nbits = 0;
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
|
|
|
|
if ((pkalgo == PUBKEY_ALGO_EDDSA && !strcmp (curve, "Ed448"))
|
|
|
|
|| (pkalgo == PUBKEY_ALGO_ECDH && !strcmp (curve, "X448")))
|
|
|
|
buf = gcry_mpi_get_opaque (pubkey, &nbits);
|
|
|
|
|
|
|
|
if (nbits == 0
|
|
|
|
|| (pkalgo == PUBKEY_ALGO_EDDSA && (nbits+7)/8 == (448 + 8)/8)
|
|
|
|
|| (pkalgo == PUBKEY_ALGO_ECDH && (nbits+7)/8 == 448/8))
|
|
|
|
return gcry_mpi_copy (pubkey);
|
|
|
|
|
|
|
|
if (pkalgo == PUBKEY_ALGO_EDDSA)
|
|
|
|
return gcry_mpi_set_opaque_copy (NULL, buf+1, 8 + 448);
|
|
|
|
else
|
|
|
|
return gcry_mpi_set_opaque_copy (NULL, buf+1, 448);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
gcry_mpi_t
|
2021-10-08 15:25:05 +02:00
|
|
|
openpgp_ecc_parse_seckey (pubkey_algo_t pkalgo, const char *curve,
|
2021-10-08 15:07:14 +02:00
|
|
|
gcry_mpi_t seckey)
|
|
|
|
{
|
|
|
|
unsigned int nbits = 0;
|
|
|
|
unsigned char *buf = NULL;
|
|
|
|
|
|
|
|
if ((pkalgo == PUBKEY_ALGO_EDDSA && !strcmp (curve, "Ed448"))
|
|
|
|
|| (pkalgo == PUBKEY_ALGO_ECDH && !strcmp (curve, "X448")))
|
|
|
|
buf = gcry_mpi_get_opaque (seckey, &nbits);
|
|
|
|
|
|
|
|
if (nbits == 0
|
|
|
|
|| (pkalgo == PUBKEY_ALGO_EDDSA && (nbits+7)/8 == (448 + 8)/8)
|
|
|
|
|| (pkalgo == PUBKEY_ALGO_ECDH && (nbits+7)/8 == 448/8))
|
|
|
|
return gcry_mpi_copy (seckey);
|
|
|
|
|
|
|
|
if (pkalgo == PUBKEY_ALGO_EDDSA)
|
|
|
|
return gcry_mpi_set_opaque_copy (NULL, buf+1, 8 + 448);
|
|
|
|
else
|
|
|
|
return gcry_mpi_set_opaque_copy (NULL, buf+1, 448);
|
|
|
|
}
|