aboutsummaryrefslogtreecommitdiffstats
path: root/common/openpgp-misc.c
blob: 74453996cc70d81aef5b55f9a9791da9a3f15b83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* 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/>.
 */

#include <config.h>
#include <stdlib.h>

#include "util.h"
#include "openpgpdefs.h"

/*
 * Convert from OpenPGP-format with possible prefix, return
 * libgcrypt-format of the key; That is, for Ed448/X448, return key
 * with prefix removed, if any.
 */
gcry_mpi_t
openpgp_to_libgcrypt (pubkey_algo_t pkalgo, const char *curve,
                      gcry_mpi_t key)
{
  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 (key, &nbits);

  /* Either Ed448/X448 non-prefixed or not Ed448/X448.  */
  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 (key);

  /* Ed448 or X448 prefixed.  */
  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);
}


/*
 * Convert from libgcrypt-format with no-prefix, return OpenPGP-format
 * of the key; That is, for Ed448/X448, return key with prefix added.
 */
gpg_error_t
openpgp_from_libgcrypt (int algo, gcry_mpi_t *r_key)
{
  gcry_mpi_t key_mpi;
  gcry_mpi_t a;
  unsigned char *p;
  const unsigned char *p_key;
  unsigned int nbits;
  unsigned int len;

  key_mpi = *r_key;
  *r_key = NULL;
  p_key = gcry_mpi_get_opaque (key_mpi, &nbits);
  len = (nbits+7)/8;
  if ((algo == PUBKEY_ALGO_ECDH && len != 56)
      || (algo == PUBKEY_ALGO_EDDSA && len != 57)
      || (algo != PUBKEY_ALGO_ECDH && algo != PUBKEY_ALGO_EDDSA))
    {
      gcry_mpi_release (key_mpi);
      return gpg_error (GPG_ERR_BAD_PUBKEY);
    }

  p = xtrymalloc (1 + len);
  if (!p)
    {
      gcry_mpi_release (key_mpi);
      return gpg_error_from_syserror ();
    }

  p[0] = 0x40;
  memcpy (p+1, p_key, len);

  a = gcry_mpi_set_opaque (NULL, p, len*8+7);
  gcry_mpi_set_flag (a, GCRYMPI_FLAG_USER2);
  *r_key = a;
  gcry_mpi_release (key_mpi);

  return 0;
}