certtool is able to set certificate policies via a template
[gnutls.git] / lib / gnutls_ecc.c
blob5b52a3c3d23509cc1b3b4d39b9953d459be4161a
1 /*
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>
32 int
33 _gnutls_ecc_ansi_x963_export (gnutls_ecc_curve_t curve, bigint_t x, bigint_t y,
34 gnutls_datum_t * out)
36 int numlen = gnutls_ecc_curve_get_size (curve);
37 int byte_size, ret;
38 size_t size;
40 if (numlen == 0)
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);
51 /* store byte 0x04 */
52 out->data[0] = 0x04;
54 /* pad and store x */
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);
58 if (ret < 0)
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));
63 ret =
64 _gnutls_mpi_print (y, &out->data[1 + numlen + numlen - byte_size], &size);
65 if (ret < 0)
66 return gnutls_assert_val (ret);
68 /* pad and store y */
69 return 0;
73 int
74 _gnutls_ecc_ansi_x963_import (const uint8_t * in,
75 unsigned long inlen, bigint_t * x, bigint_t * y)
77 int ret;
79 /* must be odd */
80 if ((inlen & 1) == 0)
82 return GNUTLS_E_INVALID_REQUEST;
85 /* check for 4 */
86 if (in[0] != 4)
88 return gnutls_assert_val (GNUTLS_E_PARSING_ERROR);
91 /* read data */
92 ret = _gnutls_mpi_scan (x, in + 1, (inlen - 1) >> 1);
93 if (ret < 0)
94 return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR);
96 ret = _gnutls_mpi_scan (y, in + 1 + ((inlen - 1) >> 1), (inlen - 1) >> 1);
97 if (ret < 0)
99 _gnutls_mpi_release (x);
100 return gnutls_assert_val (GNUTLS_E_MEMORY_ERROR);
103 return 0;
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];
110 size_t val_size;
111 int ret;
113 st = _gnutls_ecc_curve_get_params(curve);
114 if (st == NULL)
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);
119 if (ret < 0)
121 gnutls_assert();
122 goto cleanup;
125 ret = _gnutls_mpi_scan_nz(&params->params[ECC_PRIME], val, val_size);
126 if (ret < 0)
128 gnutls_assert();
129 goto cleanup;
131 params->params_nr++;
133 val_size = sizeof(val);
134 ret = _gnutls_hex2bin(st->order, strlen(st->order), val, &val_size);
135 if (ret < 0)
137 gnutls_assert();
138 goto cleanup;
141 ret = _gnutls_mpi_scan_nz(&params->params[ECC_ORDER], val, val_size);
142 if (ret < 0)
144 gnutls_assert();
145 goto cleanup;
147 params->params_nr++;
149 val_size = sizeof(val);
150 ret = _gnutls_hex2bin(st->A, strlen(st->A), val, &val_size);
151 if (ret < 0)
153 gnutls_assert();
154 goto cleanup;
157 ret = _gnutls_mpi_scan_nz(&params->params[ECC_A], val, val_size);
158 if (ret < 0)
160 gnutls_assert();
161 goto cleanup;
163 params->params_nr++;
165 val_size = sizeof(val);
166 ret = _gnutls_hex2bin(st->B, strlen(st->B), val, &val_size);
167 if (ret < 0)
169 gnutls_assert();
170 goto cleanup;
173 ret = _gnutls_mpi_scan_nz(&params->params[ECC_B], val, val_size);
174 if (ret < 0)
176 gnutls_assert();
177 goto cleanup;
179 params->params_nr++;
181 val_size = sizeof(val);
182 ret = _gnutls_hex2bin(st->Gx, strlen(st->Gx), val, &val_size);
183 if (ret < 0)
185 gnutls_assert();
186 goto cleanup;
189 ret = _gnutls_mpi_scan_nz(&params->params[ECC_GX], val, val_size);
190 if (ret < 0)
192 gnutls_assert();
193 goto cleanup;
195 params->params_nr++;
197 val_size = sizeof(val);
198 ret = _gnutls_hex2bin(st->Gy, strlen(st->Gy), val, &val_size);
199 if (ret < 0)
201 gnutls_assert();
202 goto cleanup;
205 ret = _gnutls_mpi_scan_nz(&params->params[ECC_GY], val, val_size);
206 if (ret < 0)
208 gnutls_assert();
209 goto cleanup;
211 params->params_nr++;
213 return 0;
215 cleanup:
216 gnutls_pk_params_release(params);
217 return ret;