2 * Copyright (C) 2011-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 2.1 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 /* Helper functions for ECC handling
24 * based on public domain code by Tom St. Dennis.
26 #include <gnutls_int.h>
27 #include <gnutls_mpi.h>
28 #include <gnutls_ecc.h>
29 #include <algorithms.h>
30 #include <gnutls_errors.h>
33 _gnutls_ecc_ansi_x963_export(gnutls_ecc_curve_t curve, bigint_t x,
34 bigint_t y, gnutls_datum_t * out)
36 int numlen = gnutls_ecc_curve_get_size(curve);
41 return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
43 out->size = 1 + 2 * numlen;
45 out->data = gnutls_malloc(out->size);
46 if (out->data == NULL)
47 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
49 memset(out->data, 0, out->size);
55 byte_size = (_gnutls_mpi_get_nbits(x) + 7) / 8;
56 if (numlen < byte_size) {
57 ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
61 size = out->size - (1 + (numlen - byte_size));
63 _gnutls_mpi_print(x, &out->data[1 + (numlen - byte_size)],
70 byte_size = (_gnutls_mpi_get_nbits(y) + 7) / 8;
71 if (numlen < byte_size) {
72 ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
76 size = out->size - (1 + (numlen + numlen - byte_size));
79 &out->data[1 + numlen + numlen - byte_size],
89 _gnutls_free_datum(out);
95 _gnutls_ecc_ansi_x963_import(const uint8_t * in,
96 unsigned long inlen, bigint_t * x,
102 if ((inlen & 1) == 0) {
103 return GNUTLS_E_INVALID_REQUEST;
108 return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
112 ret = _gnutls_mpi_init_scan(x, in + 1, (inlen - 1) >> 1);
114 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
117 _gnutls_mpi_init_scan(y, in + 1 + ((inlen - 1) >> 1),
120 _gnutls_mpi_release(x);
121 return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);