Fix --disable-valgrind-tests.
[gnutls.git] / lib / gnutls_mpi.c
blob8a71b0a636866bcb828f4507defa0b382fe2726d
1 /*
2 * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2008 Free Software Foundation
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GNUTLS.
8 * The GNUTLS library 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
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
21 * USA
25 /* Here lie everything that has to do with large numbers, libgcrypt and
26 * other stuff that didn't fit anywhere else.
29 #include <gnutls_int.h>
30 #include <libtasn1.h>
31 #include <gnutls_errors.h>
32 #include <gnutls_num.h>
33 #include <gnutls_mpi.h>
34 #include <random.h>
36 /* Functions that refer to the mpi library.
39 #define clearbit(v,n) ((unsigned char)(v) & ~( (unsigned char)(1) << (unsigned)(n)))
41 bigint_t
42 _gnutls_mpi_randomize (bigint_t r, unsigned int bits,
43 gnutls_rnd_level_t level)
45 size_t size = 1 + (bits / 8);
46 int ret;
47 int rem, i;
48 bigint_t tmp;
49 char tmpbuf[512];
50 opaque *buf;
51 int buf_release = 0;
53 if (size < sizeof (tmpbuf))
55 buf = tmpbuf;
57 else
59 buf = gnutls_malloc (size);
60 if (buf == NULL)
62 gnutls_assert ();
63 goto cleanup;
65 buf_release = 1;
69 ret = _gnutls_rnd (level, buf, size);
70 if (ret < 0)
72 gnutls_assert ();
73 goto cleanup;
76 /* mask the bits that weren't requested */
77 rem = bits % 8;
79 if (rem == 0)
81 buf[0] = 0;
83 else
85 for (i = 8; i >= rem; i--)
86 buf[0] = clearbit (buf[0], i);
89 ret = _gnutls_mpi_scan (&tmp, buf, size);
90 if (ret < 0)
92 gnutls_assert ();
93 goto cleanup;
96 if (buf_release != 0)
98 gnutls_free (buf);
99 buf = NULL;
102 if (r != NULL)
104 _gnutls_mpi_set (r, tmp);
105 _gnutls_mpi_release (&tmp);
106 return r;
109 return tmp;
111 cleanup:
112 if (buf_release != 0)
113 gnutls_free (buf);
114 return NULL;
117 void
118 _gnutls_mpi_release (bigint_t * x)
120 if (*x == NULL)
121 return;
123 _gnutls_mpi_ops.bigint_release (*x);
124 *x = NULL;
127 /* returns zero on success
130 _gnutls_mpi_scan (bigint_t * ret_mpi, const void *buffer, size_t nbytes)
132 *ret_mpi =
133 _gnutls_mpi_ops.bigint_scan (buffer, nbytes, GNUTLS_MPI_FORMAT_USG);
134 if (*ret_mpi == NULL)
136 gnutls_assert ();
137 return GNUTLS_E_MPI_SCAN_FAILED;
140 return 0;
143 /* returns zero on success. Fails if the number is zero.
146 _gnutls_mpi_scan_nz (bigint_t * ret_mpi, const void *buffer, size_t nbytes)
148 int ret;
150 ret = _gnutls_mpi_scan (ret_mpi, buffer, nbytes);
151 if (ret < 0)
152 return ret;
154 /* MPIs with 0 bits are illegal
156 if (_gnutls_mpi_get_nbits (*ret_mpi) == 0)
158 _gnutls_mpi_release (ret_mpi);
159 return GNUTLS_E_MPI_SCAN_FAILED;
162 return 0;
166 _gnutls_mpi_scan_pgp (bigint_t * ret_mpi, const void *buffer, size_t nbytes)
168 *ret_mpi =
169 _gnutls_mpi_ops.bigint_scan (buffer, nbytes, GNUTLS_MPI_FORMAT_PGP);
170 if (*ret_mpi == NULL)
172 gnutls_assert ();
173 return GNUTLS_E_MPI_SCAN_FAILED;
176 return 0;
179 /* Always has the first bit zero */
181 _gnutls_mpi_dprint_lz (const bigint_t a, gnutls_datum_t * dest)
183 int ret;
184 opaque *buf = NULL;
185 size_t bytes = 0;
187 if (dest == NULL || a == NULL)
188 return GNUTLS_E_INVALID_REQUEST;
190 _gnutls_mpi_print_lz (a, NULL, &bytes);
192 if (bytes != 0)
193 buf = gnutls_malloc (bytes);
194 if (buf == NULL)
195 return GNUTLS_E_MEMORY_ERROR;
197 ret = _gnutls_mpi_print_lz (a, buf, &bytes);
198 if (ret < 0)
200 gnutls_free (buf);
201 return ret;
204 dest->data = buf;
205 dest->size = bytes;
206 return 0;
210 _gnutls_mpi_dprint (const bigint_t a, gnutls_datum_t * dest)
212 int ret;
213 opaque *buf = NULL;
214 size_t bytes = 0;
216 if (dest == NULL || a == NULL)
217 return GNUTLS_E_INVALID_REQUEST;
219 _gnutls_mpi_print (a, NULL, &bytes);
220 if (bytes != 0)
221 buf = gnutls_malloc (bytes);
222 if (buf == NULL)
223 return GNUTLS_E_MEMORY_ERROR;
225 ret = _gnutls_mpi_print (a, buf, &bytes);
226 if (ret < 0)
228 gnutls_free (buf);
229 return ret;
232 dest->data = buf;
233 dest->size = bytes;
234 return 0;
237 /* This function will copy the mpi data into a datum,
238 * but will set minimum size to 'size'. That means that
239 * the output value is left padded with zeros.
242 _gnutls_mpi_dprint_size (const bigint_t a, gnutls_datum_t * dest, size_t size)
244 int ret;
245 opaque *buf = NULL;
246 size_t bytes = 0;
247 unsigned int i;
249 if (dest == NULL || a == NULL)
250 return GNUTLS_E_INVALID_REQUEST;
252 _gnutls_mpi_print (a, NULL, &bytes);
253 if (bytes != 0)
254 buf = gnutls_malloc (MAX (size, bytes));
255 if (buf == NULL)
256 return GNUTLS_E_MEMORY_ERROR;
258 dest->size = MAX (size, bytes);
260 if (bytes <= size)
262 size_t diff = size - bytes;
263 for (i = 0; i < diff; i++)
264 buf[i] = 0;
265 ret = _gnutls_mpi_print (a, &buf[diff], &bytes);
267 else
269 ret = _gnutls_mpi_print (a, buf, &bytes);
272 if (ret < 0)
274 gnutls_free (buf);
275 return ret;
278 dest->data = buf;
279 dest->size = MAX (size, bytes);
280 return 0;
283 /* this function reads an integer
284 * from asn1 structs. Combines the read and mpi_scan
285 * steps.
288 _gnutls_x509_read_int (ASN1_TYPE node, const char *value, bigint_t * ret_mpi)
290 int result;
291 opaque *tmpstr = NULL;
292 int tmpstr_size;
294 tmpstr_size = 0;
295 result = asn1_read_value (node, value, NULL, &tmpstr_size);
296 if (result != ASN1_MEM_ERROR)
298 gnutls_assert ();
299 return _gnutls_asn2err (result);
302 tmpstr = gnutls_malloc (tmpstr_size);
303 if (tmpstr == NULL)
305 gnutls_assert ();
306 return GNUTLS_E_MEMORY_ERROR;
309 result = asn1_read_value (node, value, tmpstr, &tmpstr_size);
310 if (result != ASN1_SUCCESS)
312 gnutls_assert ();
313 gnutls_free (tmpstr);
314 return _gnutls_asn2err (result);
317 result = _gnutls_mpi_scan (ret_mpi, tmpstr, tmpstr_size);
318 gnutls_free (tmpstr);
320 if (result < 0)
322 gnutls_assert ();
323 return result;
326 return 0;
329 /* Writes the specified integer into the specified node.
332 _gnutls_x509_write_int (ASN1_TYPE node, const char *value, bigint_t mpi,
333 int lz)
335 opaque *tmpstr;
336 size_t s_len;
337 int result;
339 s_len = 0;
340 if (lz)
341 result = _gnutls_mpi_print_lz (mpi, NULL, &s_len);
342 else
343 result = _gnutls_mpi_print (mpi, NULL, &s_len);
345 if (result != 0)
347 gnutls_assert ();
348 return result;
351 tmpstr = gnutls_malloc (s_len);
352 if (tmpstr == NULL)
354 gnutls_assert ();
355 return GNUTLS_E_MEMORY_ERROR;
358 if (lz)
359 result = _gnutls_mpi_print_lz (mpi, tmpstr, &s_len);
360 else
361 result = _gnutls_mpi_print (mpi, tmpstr, &s_len);
363 if (result != 0)
365 gnutls_assert ();
366 gnutls_free (tmpstr);
367 return GNUTLS_E_MPI_PRINT_FAILED;
370 result = asn1_write_value (node, value, tmpstr, s_len);
372 gnutls_free (tmpstr);
374 if (result != ASN1_SUCCESS)
376 gnutls_assert ();
377 return _gnutls_asn2err (result);
380 return 0;