check for either iconv or libiconv.
[gnutls.git] / lib / nettle / ecc_shared_secret.c
blobc9ed0065fa54fb50ea930ff88b649b700798711d
1 /*
2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * This file is part of GNUTLS.
6 * The GNUTLS library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * as published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
21 /* Based on public domain code of LibTomCrypt by Tom St Denis.
22 * Adapted to gmp and nettle by Nikos Mavrogiannopoulos.
25 #include "ecc.h"
26 #include <string.h>
29 @file ecc_shared_secret.c
30 ECC Crypto, Tom St Denis
34 Create an ECC shared secret between two keys
35 @param private_key The private ECC key
36 @param public_key The public key
37 @param out [out] Destination of the shared secret (Conforms to EC-DH from ANSI X9.63)
38 @param outlen [in/out] The max size and resulting size of the shared secret
39 @return 0 if successful
41 int
42 ecc_shared_secret (ecc_key * private_key, ecc_key * public_key,
43 unsigned char *out, unsigned long *outlen)
45 unsigned long x;
46 ecc_point *result;
47 int err;
49 if (private_key == NULL || public_key == NULL || out == NULL || outlen == NULL)
50 return -1;
52 /* type valid? */
53 if (private_key->type != PK_PRIVATE)
55 return -1;
58 /* make new point */
59 result = ecc_new_point ();
60 if (result == NULL)
62 return -1;
65 if ((err =
66 ecc_mulmod (private_key->k, &public_key->pubkey, result,
67 private_key->A, private_key->prime, 1)) != 0)
69 goto done;
72 x = nettle_mpz_sizeinbase_256_u (private_key->prime);
73 if (*outlen < x)
75 *outlen = x;
76 err = -1;
77 goto done;
79 nettle_mpz_get_str_256(x, out, result->x);
81 err = 0;
82 *outlen = x;
83 done:
84 ecc_del_point (result);
85 return err;
88 /* $Source: /cvs/libtom/libtomcrypt/src/pk/ecc/ecc_shared_secret.c,v $ */
89 /* $Revision: 1.10 $ */
90 /* $Date: 2007/05/12 14:32:35 $ */