Coverage Report

Created: 2025-03-18 06:55

/src/gnutls/lib/ecc.c
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 _gnutls_ecc_ansi_x962_export(gnutls_ecc_curve_t curve, bigint_t x,
33
         bigint_t y, gnutls_datum_t *out)
34
0
{
35
0
  int numlen = gnutls_ecc_curve_get_size(curve);
36
0
  int byte_size, ret;
37
0
  size_t size;
38
39
0
  if (numlen == 0)
40
0
    return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
41
42
0
  out->size = 1 + 2 * numlen;
43
44
0
  out->data = gnutls_malloc(out->size);
45
0
  if (out->data == NULL)
46
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
47
48
0
  memset(out->data, 0, out->size);
49
50
  /* store byte 0x04 */
51
0
  out->data[0] = 0x04;
52
53
  /* pad and store x */
54
0
  byte_size = (_gnutls_mpi_get_nbits(x) + 7) / 8;
55
0
  if (numlen < byte_size) {
56
0
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
57
0
    goto cleanup;
58
0
  }
59
60
0
  size = out->size - (1 + (numlen - byte_size));
61
0
  ret = _gnutls_mpi_print(x, &out->data[1 + (numlen - byte_size)], &size);
62
0
  if (ret < 0) {
63
0
    gnutls_assert();
64
0
    goto cleanup;
65
0
  }
66
67
0
  byte_size = (_gnutls_mpi_get_nbits(y) + 7) / 8;
68
0
  if (numlen < byte_size) {
69
0
    ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
70
0
    goto cleanup;
71
0
  }
72
73
0
  size = out->size - (1 + (numlen + numlen - byte_size));
74
0
  ret = _gnutls_mpi_print(y, &out->data[1 + numlen + numlen - byte_size],
75
0
        &size);
76
0
  if (ret < 0) {
77
0
    gnutls_assert();
78
0
    goto cleanup;
79
0
  }
80
81
  /* pad and store y */
82
0
  return 0;
83
0
cleanup:
84
0
  _gnutls_free_datum(out);
85
0
  return ret;
86
0
}
87
88
int _gnutls_ecc_ansi_x962_import(const uint8_t *in, unsigned long inlen,
89
         bigint_t *x, bigint_t *y)
90
0
{
91
0
  int ret;
92
93
  /* must be odd */
94
0
  if ((inlen & 1) == 0) {
95
0
    return GNUTLS_E_INVALID_REQUEST;
96
0
  }
97
98
  /* check for 4  */
99
0
  if (in[0] != 4) {
100
0
    return gnutls_assert_val(GNUTLS_E_PARSING_ERROR);
101
0
  }
102
103
  /* read data */
104
0
  ret = _gnutls_mpi_init_scan(x, in + 1, (inlen - 1) >> 1);
105
0
  if (ret < 0)
106
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
107
108
0
  ret = _gnutls_mpi_init_scan(y, in + 1 + ((inlen - 1) >> 1),
109
0
            (inlen - 1) >> 1);
110
0
  if (ret < 0) {
111
0
    _gnutls_mpi_release(x);
112
0
    return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
113
0
  }
114
115
0
  return 0;
116
0
}