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 3 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
, bigint_t y
,
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 size
= out
->size
- (1 + (numlen
- byte_size
));
57 ret
= _gnutls_mpi_print (x
, &out
->data
[1 + (numlen
- byte_size
)], &size
);
59 return gnutls_assert_val (ret
);
61 byte_size
= (_gnutls_mpi_get_nbits (y
) + 7) / 8;
62 size
= out
->size
- (1 + (numlen
+ numlen
- byte_size
));
64 _gnutls_mpi_print (y
, &out
->data
[1 + numlen
+ numlen
- byte_size
], &size
);
66 return gnutls_assert_val (ret
);
74 _gnutls_ecc_ansi_x963_import (const uint8_t * in
,
75 unsigned long inlen
, bigint_t
* x
, bigint_t
* y
)
82 return GNUTLS_E_INVALID_REQUEST
;
88 return gnutls_assert_val (GNUTLS_E_PARSING_ERROR
);
92 ret
= _gnutls_mpi_scan (x
, in
+ 1, (inlen
- 1) >> 1);
94 return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR
);
96 ret
= _gnutls_mpi_scan (y
, in
+ 1 + ((inlen
- 1) >> 1), (inlen
- 1) >> 1);
99 _gnutls_mpi_release (x
);
100 return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR
);
106 int _gnutls_ecc_curve_fill_params(gnutls_ecc_curve_t curve
, gnutls_pk_params_st
* params
)
108 const gnutls_ecc_curve_entry_st
*st
;
109 uint8_t val
[MAX_ECC_CURVE_SIZE
];
113 st
= _gnutls_ecc_curve_get_params(curve
);
115 return gnutls_assert_val(GNUTLS_E_ECC_UNSUPPORTED_CURVE
);
117 val_size
= sizeof(val
);
118 ret
= _gnutls_hex2bin(st
->prime
, strlen(st
->prime
), val
, &val_size
);
125 ret
= _gnutls_mpi_scan_nz(¶ms
->params
[ECC_PRIME
], val
, val_size
);
133 val_size
= sizeof(val
);
134 ret
= _gnutls_hex2bin(st
->order
, strlen(st
->order
), val
, &val_size
);
141 ret
= _gnutls_mpi_scan_nz(¶ms
->params
[ECC_ORDER
], val
, val_size
);
149 val_size
= sizeof(val
);
150 ret
= _gnutls_hex2bin(st
->A
, strlen(st
->A
), val
, &val_size
);
157 ret
= _gnutls_mpi_scan_nz(¶ms
->params
[ECC_A
], val
, val_size
);
165 val_size
= sizeof(val
);
166 ret
= _gnutls_hex2bin(st
->B
, strlen(st
->B
), val
, &val_size
);
173 ret
= _gnutls_mpi_scan_nz(¶ms
->params
[ECC_B
], val
, val_size
);
181 val_size
= sizeof(val
);
182 ret
= _gnutls_hex2bin(st
->Gx
, strlen(st
->Gx
), val
, &val_size
);
189 ret
= _gnutls_mpi_scan_nz(¶ms
->params
[ECC_GX
], val
, val_size
);
197 val_size
= sizeof(val
);
198 ret
= _gnutls_hex2bin(st
->Gy
, strlen(st
->Gy
), val
, &val_size
);
205 ret
= _gnutls_mpi_scan_nz(¶ms
->params
[ECC_GY
], val
, val_size
);
216 gnutls_pk_params_release(params
);