Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) 2011-2012 Free Software Foundation, Inc. |
3 | | * |
4 | | * Author: Nikos Mavrogiannopoulos |
5 | | * |
6 | | * This file is part of GnuTLS. |
7 | | * |
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. |
12 | | * |
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. |
17 | | * |
18 | | * You should have received a copy of the GNU Lesser General Public License |
19 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
20 | | * |
21 | | */ |
22 | | |
23 | | /* Helper functions for ECC handling |
24 | | * based on public domain code by Tom St. Dennis. |
25 | | */ |
26 | | #include "gnutls_int.h" |
27 | | #include <mpi.h> |
28 | | #include <ecc.h> |
29 | | #include <algorithms.h> |
30 | | #include "errors.h" |
31 | | |
32 | | int |
33 | | _gnutls_ecc_ansi_x962_export(gnutls_ecc_curve_t curve, bigint_t x, |
34 | | bigint_t y, gnutls_datum_t * out) |
35 | 0 | { |
36 | 0 | int numlen = gnutls_ecc_curve_get_size(curve); |
37 | 0 | int byte_size, ret; |
38 | 0 | size_t size; |
39 | |
|
40 | 0 | if (numlen == 0) |
41 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
42 | | |
43 | 0 | out->size = 1 + 2 * numlen; |
44 | |
|
45 | 0 | out->data = gnutls_malloc(out->size); |
46 | 0 | if (out->data == NULL) |
47 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
48 | | |
49 | 0 | memset(out->data, 0, out->size); |
50 | | |
51 | | /* store byte 0x04 */ |
52 | 0 | out->data[0] = 0x04; |
53 | | |
54 | | /* pad and store x */ |
55 | 0 | byte_size = (_gnutls_mpi_get_nbits(x) + 7) / 8; |
56 | 0 | if (numlen < byte_size) { |
57 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
58 | 0 | goto cleanup; |
59 | 0 | } |
60 | | |
61 | 0 | size = out->size - (1 + (numlen - byte_size)); |
62 | 0 | ret = _gnutls_mpi_print(x, &out->data[1 + (numlen - byte_size)], &size); |
63 | 0 | if (ret < 0) { |
64 | 0 | gnutls_assert(); |
65 | 0 | goto cleanup; |
66 | 0 | } |
67 | | |
68 | 0 | byte_size = (_gnutls_mpi_get_nbits(y) + 7) / 8; |
69 | 0 | if (numlen < byte_size) { |
70 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
71 | 0 | goto cleanup; |
72 | 0 | } |
73 | | |
74 | 0 | size = out->size - (1 + (numlen + numlen - byte_size)); |
75 | 0 | ret = |
76 | 0 | _gnutls_mpi_print(y, |
77 | 0 | &out->data[1 + numlen + numlen - byte_size], |
78 | 0 | &size); |
79 | 0 | if (ret < 0) { |
80 | 0 | gnutls_assert(); |
81 | 0 | goto cleanup; |
82 | 0 | } |
83 | | |
84 | | /* pad and store y */ |
85 | 0 | return 0; |
86 | 0 | cleanup: |
87 | 0 | _gnutls_free_datum(out); |
88 | 0 | return ret; |
89 | 0 | } |
90 | | |
91 | | int |
92 | | _gnutls_ecc_ansi_x962_import(const uint8_t * in, |
93 | | unsigned long inlen, bigint_t * x, bigint_t * y) |
94 | 0 | { |
95 | 0 | int ret; |
96 | | |
97 | | /* must be odd */ |
98 | 0 | if ((inlen & 1) == 0) { |
99 | 0 | return GNUTLS_E_INVALID_REQUEST; |
100 | 0 | } |
101 | | |
102 | | /* check for 4 */ |
103 | 0 | if (in[0] != 4) { |
104 | 0 | return gnutls_assert_val(GNUTLS_E_PARSING_ERROR); |
105 | 0 | } |
106 | | |
107 | | /* read data */ |
108 | 0 | ret = _gnutls_mpi_init_scan(x, in + 1, (inlen - 1) >> 1); |
109 | 0 | if (ret < 0) |
110 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
111 | | |
112 | 0 | ret = |
113 | 0 | _gnutls_mpi_init_scan(y, in + 1 + ((inlen - 1) >> 1), |
114 | 0 | (inlen - 1) >> 1); |
115 | 0 | if (ret < 0) { |
116 | 0 | _gnutls_mpi_release(x); |
117 | 0 | return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR); |
118 | 0 | } |
119 | | |
120 | 0 | return 0; |
121 | 0 | } |