summaryrefslogtreecommitdiff
path: root/lib/nettle/ecc_shared_secret.c
blob: 4e3ef358e13603556217fcd6967bd0c1411021a5 (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
/*
 * Copyright (C) 2011 Free Software Foundation, Inc.
 *
 * This file is part of GNUTLS.
 *
 * The GNUTLS library 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; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This library 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/>
 *
 */

/* Based on public domain code of LibTomCrypt by Tom St Denis.
 * Adapted to gmp and nettle by Nikos Mavrogiannopoulos.
 */

#include "ecc.h"
#include <string.h>

/*
  @file ecc_shared_secret.c
  ECC Crypto, Tom St Denis
*/

/*
  Create an ECC shared secret between two keys
  @param private_key      The private ECC key
  @param public_key       The public key
  @param out              [out] Destination of the shared secret (Conforms to EC-DH from ANSI X9.63)
  @param outlen           [in/out] The max size and resulting size of the shared secret
  @return 0 if successful
*/
int
ecc_shared_secret (ecc_key * private_key, ecc_key * public_key,
                   unsigned char *out, unsigned long *outlen)
{
  unsigned long x;
  ecc_point *result;
  int err;

  if (private_key == NULL || public_key == NULL || out == NULL || outlen == NULL)
    return -1;

  /* type valid? */
  if (private_key->type != PK_PRIVATE)
    {
      return -1;
    }

  /* make new point */
  result = ecc_new_point ();
  if (result == NULL)
    {
      return -1;
    }

  if ((err =
       ecc_mulmod (private_key->k, &public_key->pubkey, result,
                       private_key->A, private_key->prime, 1)) != 0)
    {
      goto done;
    }

  x = nettle_mpz_sizeinbase_256_u (private_key->prime);
  if (*outlen < x)
    {
      *outlen = x;
      err = -1;
      goto done;
    }
  nettle_mpz_get_str_256(x, out, result->x);

  err = 0;
  *outlen = x;
done:
  ecc_del_point (result);
  return err;
}

/* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_shared_secret.c,v $ */
/* $Revision: 1.10 $ */
/* $Date: 2007/05/12 14:32:35 $ */